| @@ -11,9 +11,10 @@ | |||
| #include "ui_main_window.h" | |||
| #include <QAction> | |||
| #include <QDialogButtonBox> | |||
| #include <QInputDialog> | |||
| #include <QLineEdit> | |||
| #include <QObject> | |||
| #include <QPushButton> | |||
| #include <QSignalBlocker> | |||
| #include <QTabWidget> | |||
| #include <QTreeWidget> | |||
| @@ -38,6 +39,43 @@ std::string toUtf8(const QString &value) | |||
| return std::string(bytes.constData(), static_cast<std::size_t>(bytes.size())); | |||
| } | |||
| bool requestRequiredName( | |||
| QWidget &parent, | |||
| const QString &title, | |||
| const QString &label, | |||
| const QString &initial_value, | |||
| QString *name) | |||
| { | |||
| QInputDialog dialog(&parent); | |||
| dialog.setWindowTitle(title); | |||
| dialog.setLabelText(label); | |||
| dialog.setInputMode(QInputDialog::TextInput); | |||
| dialog.setTextValue(initial_value); | |||
| QDialogButtonBox *button_box = dialog.findChild<QDialogButtonBox *>(); | |||
| QPushButton *ok_button = button_box != nullptr | |||
| ? button_box->button(QDialogButtonBox::Ok) | |||
| : nullptr; | |||
| if (ok_button != nullptr) | |||
| { | |||
| ok_button->setEnabled(!initial_value.trimmed().isEmpty()); | |||
| QObject::connect( | |||
| &dialog, | |||
| &QInputDialog::textValueChanged, | |||
| &dialog, | |||
| [ok_button](const QString &text) { | |||
| ok_button->setEnabled(!text.trimmed().isEmpty()); | |||
| }); | |||
| } | |||
| if (dialog.exec() != QDialog::Accepted) | |||
| { | |||
| return false; | |||
| } | |||
| *name = dialog.textValue().trimmed(); | |||
| return true; | |||
| } | |||
| } // namespace | |||
| ProjectWorkspaceController::ProjectWorkspaceController( | |||
| @@ -295,9 +333,9 @@ void ProjectWorkspaceController::updateActions() | |||
| void ProjectWorkspaceController::addPage() | |||
| { | |||
| const QString name = QInputDialog::getText( | |||
| &parent_, QObject::tr("新建页面"), QObject::tr("页面名称")); | |||
| if (name.isEmpty()) | |||
| QString name; | |||
| if (!requestRequiredName( | |||
| parent_, QObject::tr("新建页面"), QObject::tr("页面名称"), {}, &name)) | |||
| { | |||
| return; | |||
| } | |||
| @@ -317,9 +355,9 @@ void ProjectWorkspaceController::addPage() | |||
| void ProjectWorkspaceController::addLogic() | |||
| { | |||
| const QString name = QInputDialog::getText( | |||
| &parent_, QObject::tr("新建控制逻辑"), QObject::tr("逻辑名称")); | |||
| if (name.isEmpty()) | |||
| QString name; | |||
| if (!requestRequiredName( | |||
| parent_, QObject::tr("新建控制逻辑"), QObject::tr("逻辑名称"), {}, &name)) | |||
| { | |||
| return; | |||
| } | |||
| @@ -362,11 +400,9 @@ void ProjectWorkspaceController::renameSelectedItem() | |||
| { | |||
| return; | |||
| } | |||
| bool accepted = false; | |||
| const QString name = QInputDialog::getText( | |||
| &parent_, QObject::tr("重命名"), QObject::tr("新名称"), | |||
| QLineEdit::Normal, current_name, &accepted); | |||
| if (!accepted) | |||
| QString name; | |||
| if (!requestRequiredName( | |||
| parent_, QObject::tr("重命名"), QObject::tr("新名称"), current_name, &name)) | |||
| { | |||
| return; | |||
| } | |||