NeTrainSim 0.1.1 beta
The Open-Source Network Trains Simulator
 
Loading...
Searching...
No Matches
nonemptydelegate.h
Go to the documentation of this file.
1
17#ifndef NONEMPTYDELEGATE_H
18#define NONEMPTYDELEGATE_H
19
21#include <QLineEdit>
22#include <QStyledItemDelegate>
23
28class NonEmptyDelegate : public QStyledItemDelegate {
29 Q_OBJECT
30
31public:
36 NonEmptyDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent) {}
37
44 void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override {
45 QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor);
46 if (lineEdit && lineEdit->text().trimmed().isEmpty()) {
47 if (parent() != nullptr) {
48 NeTrainSim* netP = qobject_cast<NeTrainSim*>(parent());
49 netP->showWarning("The cell cannot be empty!");
50 }
51 return;
52 }
53
54 QString value = lineEdit->text().trimmed();
55 bool isNumeric;
56 value.toDouble(&isNumeric);
57
58 if (!isNumeric) {
59 if (parent() != nullptr) {
60 NeTrainSim* netP = qobject_cast<NeTrainSim*>(parent());
61 netP->showWarning("The value must be numeric!");
62 }
63 return;
64 }
65
66 QStyledItemDelegate::setModelData(editor, model, index);
67 }
68};
69
70#endif // NONEMPTYDELEGATE_H
A network train simulation GUI.
Definition netrainsimmainwindow.h:40
void showWarning(QString text)
Displays a warning message with the given text.
Definition netrainsimmainwindow.cpp:1718
The NonEmptyDelegate class provides functionality to ensure that table cells are not empty.
Definition nonemptydelegate.h:28
NonEmptyDelegate(QObject *parent=nullptr)
Constructs a NonEmptyDelegate object with the given parent.
Definition nonemptydelegate.h:36
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
Sets the model data based on the data of the editor widget.
Definition nonemptydelegate.h:44