diff --git a/src/widgets/newprojectdialog.h b/src/widgets/newprojectdialog.h --- a/src/widgets/newprojectdialog.h +++ b/src/widgets/newprojectdialog.h @@ -55,7 +55,7 @@ Domain::DataSource::Ptr dataSource() const Q_DECL_OVERRIDE; private slots: - void onNameTextChanged(const QString &text); + void onUserInputChanged(); private: void applyDefaultSource(const QModelIndex &root); diff --git a/src/widgets/newprojectdialog.cpp b/src/widgets/newprojectdialog.cpp --- a/src/widgets/newprojectdialog.cpp +++ b/src/widgets/newprojectdialog.cpp @@ -61,14 +61,16 @@ { ui->setupUi(this); - QObject::connect(ui->nameEdit, &QLineEdit::textChanged, this, &NewProjectDialog::onNameTextChanged); - onNameTextChanged(m_name); + connect(ui->nameEdit, &QLineEdit::textChanged, this, &NewProjectDialog::onUserInputChanged); auto taskSourceProxy = new TaskSourceProxy(this); taskSourceProxy->setSourceModel(m_flattenProxy); ui->sourceCombo->setModel(taskSourceProxy); - m_flattenProxy->setDisplayAncestorData(true); + connect(ui->sourceCombo, static_cast(&QComboBox::currentIndexChanged), + this, &NewProjectDialog::onUserInputChanged); + + onUserInputChanged(); } NewProjectDialog::~NewProjectDialog() @@ -112,10 +114,15 @@ return m_source; } -void NewProjectDialog::onNameTextChanged(const QString &text) +void NewProjectDialog::onUserInputChanged() { + const auto text = ui->nameEdit->text(); + const auto source = ui->sourceCombo->itemData(ui->sourceCombo->currentIndex(), + Presentation::QueryTreeModelBase::ObjectRole) + .value(); + auto buttonOk = ui->buttonBox->button(QDialogButtonBox::Ok); - buttonOk->setEnabled(!text.isEmpty()); + buttonOk->setEnabled(!text.isEmpty() && source); } #include "newprojectdialog.moc" diff --git a/tests/units/widgets/newprojectdialogtest.cpp b/tests/units/widgets/newprojectdialogtest.cpp --- a/tests/units/widgets/newprojectdialogtest.cpp +++ b/tests/units/widgets/newprojectdialogtest.cpp @@ -57,10 +57,8 @@ QTest::keyClicks(nameEdit, nameInput); } - if (sourceComboIndex >= 0) { - auto sourceCombo = dialog->findChild(QStringLiteral("sourceCombo")); - sourceCombo->setCurrentIndex(sourceComboIndex); - } + auto sourceCombo = dialog->findChild(QStringLiteral("sourceCombo")); + sourceCombo->setCurrentIndex(sourceComboIndex); auto buttonBox = dialog->findChild(QStringLiteral("buttonBox")); if (reject) @@ -235,6 +233,30 @@ QCOMPARE(dialog.name(), QString()); QCOMPARE(dialog.dataSource(), Domain::DataSource::Ptr()); } + + void shouldNotAllowNoSelectedSource() + { + // GIVEN + Widgets::NewProjectDialog dialog; + + auto sourceModel = createSourceModel(); + dialog.setDataSourcesModel(sourceModel); + + UserInputSimulator userInput; + userInput.dialog = &dialog; + userInput.sourceComboIndex = -1; + userInput.nameInput = QStringLiteral("name"); + userInput.reject = true; + + // WHEN + userInput.exec(); + + // THEN + auto buttonOk = dialog.findChild(QStringLiteral("buttonBox"))->button(QDialogButtonBox::Ok); + QVERIFY(!buttonOk->isEnabled()); + QCOMPARE(dialog.name(), QString()); + QCOMPARE(dialog.dataSource(), Domain::DataSource::Ptr()); + } }; ZANSHIN_TEST_MAIN(NewProjectDialogTest)