|
| 1 | +#include "addtraindialog.h" |
| 2 | + |
| 3 | +#include <QSpinBox> |
| 4 | +#include <QLineEdit> |
| 5 | +#include <QLabel> |
| 6 | + |
| 7 | +#include <QFormLayout> |
| 8 | + |
| 9 | +#include <QDialogButtonBox> |
| 10 | + |
| 11 | +#include <QMessageBox> |
| 12 | + |
| 13 | +#include "trainsmodel.h" |
| 14 | + |
| 15 | +AddTrainDialog::AddTrainDialog(size_t segmentIndex, const QString& segName, TrainsModel *trainsModel, QWidget *parent) |
| 16 | + : QDialog{parent} |
| 17 | + , mTrainsModel(trainsModel) |
| 18 | + , mSegmentIndex(segmentIndex) |
| 19 | +{ |
| 20 | + QFormLayout *lay = new QFormLayout(this); |
| 21 | + |
| 22 | + mLabel = new QLabel; |
| 23 | + lay->addRow(mLabel); |
| 24 | + |
| 25 | + mTrainEdit = new QLineEdit; |
| 26 | + lay->addRow(tr("Train name:"), mTrainEdit); |
| 27 | + |
| 28 | + mNumWagonsSpin = new QSpinBox; |
| 29 | + mNumWagonsSpin->setRange(1, 100); |
| 30 | + mNumWagonsSpin->setValue(5); |
| 31 | + lay->addRow(tr("Num wagons:"), mNumWagonsSpin); |
| 32 | + |
| 33 | + QDialogButtonBox *butBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, |
| 34 | + Qt::Horizontal); |
| 35 | + connect(butBox, &QDialogButtonBox::accepted, |
| 36 | + this, &QDialog::accept); |
| 37 | + connect(butBox, &QDialogButtonBox::rejected, |
| 38 | + this, &QDialog::reject); |
| 39 | + lay->addRow(butBox); |
| 40 | + |
| 41 | + QString segName_ = segName; |
| 42 | + if(segName_.isEmpty()) |
| 43 | + segName_ = tr("<i>(index %1)</i>").arg(segmentIndex); |
| 44 | + |
| 45 | + mLabel->setText(tr("Add Train on segment %1") |
| 46 | + .arg(segName_)); |
| 47 | + |
| 48 | + setWindowTitle(tr("Add Train")); |
| 49 | +} |
| 50 | + |
| 51 | +void AddTrainDialog::done(int result) |
| 52 | +{ |
| 53 | + if(result == QDialog::Accepted) |
| 54 | + { |
| 55 | + QString errStr; |
| 56 | + if(!mTrainsModel->addTrain(mTrainEdit->text(), |
| 57 | + Color::Blue, mNumWagonsSpin->value(), |
| 58 | + mSegmentIndex, &errStr)) |
| 59 | + { |
| 60 | + QMessageBox::warning(this, tr("Cannot add Train"), |
| 61 | + errStr); |
| 62 | + return; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + QDialog::done(result); |
| 67 | +} |
0 commit comments