pops up a dialog to ask for an instance name when renaming
| 71 | // pops up a dialog to ask for an instance name when renaming |
| 72 | // |
| 73 | QString getInstanceName(QWidget* parent, const QString& title, const QString& moreText, |
| 74 | const QString& label, const QString& oldName = {}) |
| 75 | { |
| 76 | auto& m = InstanceManager::singleton(); |
| 77 | |
| 78 | QDialog dlg(parent); |
| 79 | dlg.setWindowTitle(title); |
| 80 | |
| 81 | auto* ly = new QVBoxLayout(&dlg); |
| 82 | |
| 83 | auto* bb = new QDialogButtonBox(QDialogButtonBox::Cancel | QDialogButtonBox::Ok); |
| 84 | |
| 85 | auto* text = new QLineEdit(oldName); |
| 86 | text->selectAll(); |
| 87 | |
| 88 | auto* error = new QLabel; |
| 89 | |
| 90 | if (!moreText.isEmpty()) { |
| 91 | auto* lb = new QLabel(moreText); |
| 92 | lb->setWordWrap(true); |
| 93 | ly->addWidget(lb); |
| 94 | ly->addSpacing(10); |
| 95 | } |
| 96 | |
| 97 | auto* lb = new QLabel(label); |
| 98 | lb->setWordWrap(true); |
| 99 | ly->addWidget(lb); |
| 100 | |
| 101 | ly->addWidget(text); |
| 102 | ly->addWidget(error); |
| 103 | ly->addStretch(); |
| 104 | ly->addWidget(bb); |
| 105 | |
| 106 | auto check = [&] { |
| 107 | bool okay = false; |
| 108 | |
| 109 | if (text->text().isEmpty()) { |
| 110 | error->setText(""); |
| 111 | } else if (!MOBase::validFileName(text->text())) { |
| 112 | error->setText(QObject::tr("The instance name must be a valid folder name.")); |
| 113 | } else { |
| 114 | const auto name = MOBase::sanitizeFileName(text->text()); |
| 115 | |
| 116 | if ((name != oldName) && m.instanceExists(text->text())) { |
| 117 | error->setText(QObject::tr("An instance with this name already exists.")); |
| 118 | } else { |
| 119 | okay = true; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | error->setVisible(!okay); |
| 124 | bb->button(QDialogButtonBox::Ok)->setEnabled(okay); |
| 125 | }; |
| 126 | |
| 127 | QObject::connect(text, &QLineEdit::textChanged, [&] { |
| 128 | check(); |
| 129 | }); |
| 130 | QObject::connect(bb, &QDialogButtonBox::accepted, [&] { |
no test coverage detected