QMenu *WulforUtil::buildUserCmdMenu(const StringList& hub_list, int ctx, QWidget* parent) { UserCommand::List userCommands = FavoriteManager::getInstance()->getUserCommands(ctx, hub_list); if (userCommands.empty()) return nullptr; QMenu *ucMenu = new QMenu(tr("User commands"), parent); QMenu *menuPtr = ucMenu; for (size_t n = 0; n < userCommands.size(); ++n) { UserCommand *uc = &userCommands[n]; if (uc->getType() == UserCommand::TYPE_SEPARATOR) { // Avoid double separators... if (!menuPtr->actions().isEmpty() && !menuPtr->actions().last()->isSeparator()) { menuPtr->addSeparator(); } } else if (uc->isRaw() || uc->isChat()) { menuPtr = ucMenu; auto _begin = uc->getDisplayName().begin(); auto _end = uc->getDisplayName().end(); for(; _begin != _end; ++_begin) { const QString name = _q(*_begin); if (_begin + 1 == _end) { menuPtr->addAction(name)->setData(uc->getId()); } else { bool found = false; QListIterator<QAction*> iter(menuPtr->actions()); while(iter.hasNext()) { QAction *item = iter.next(); if (item->menu() && item->text() == name) { found = true; menuPtr = item->menu(); break; } } if (!found) menuPtr = menuPtr->addMenu(name); } } } } return ucMenu; }
bool WulforUtil::getUserCommandParams(const UserCommand& uc, StringMap& params) { StringList names; string::size_type i = 0, j = 0; const string cmd_str = uc.getCommand(); while((i = cmd_str.find("%[line:", i)) != string::npos) { if ((j = cmd_str.find("]", (i += 7))) == string::npos) break; names.push_back(cmd_str.substr(i, j - i)); i = j + 1; } if (names.empty()) return true; QDialog dlg(MainWindow::getInstance()); dlg.setWindowTitle(_q(uc.getDisplayName().back())); QVBoxLayout *vlayout = new QVBoxLayout(&dlg); std::vector<std::function<void ()> > valueFs; for (const auto &name : names) { QString caption = _q(name); if (uc.adc()) { caption.replace("\\\\", "\\"); caption.replace("\\s", " "); } int combo_sel = -1; QString combo_caption = caption; combo_caption.replace("//", "\t"); QStringList combo_values = combo_caption.split("/"); if (combo_values.size() > 2) { QString tmp = combo_values.takeFirst(); bool isNumber = false; combo_sel = combo_values.takeFirst().toInt(&isNumber); if (!isNumber || combo_sel >= combo_values.size()) combo_sel = -1; else caption = tmp; } QGroupBox *box = new QGroupBox(caption, &dlg); QHBoxLayout *hlayout = new QHBoxLayout(box); if (combo_sel >= 0) { for (auto &val : combo_values) val.replace("\t", "/"); QComboBox *combo = new QComboBox(box); hlayout->addWidget(combo); combo->addItems(combo_values); combo->setEditable(true); combo->setCurrentIndex(combo_sel); combo->lineEdit()->setReadOnly(true); valueFs.push_back([combo, name, ¶ms] { params["line:" + name] = combo->currentText().toStdString(); }); } else { QLineEdit *line = new QLineEdit(box); hlayout->addWidget(line); valueFs.push_back([line, name, ¶ms] { params["line:" + name] = line->text().toStdString(); }); } vlayout->addWidget(box); } QDialogButtonBox *buttonBox = new QDialogButtonBox(&dlg); buttonBox->setOrientation(Qt::Horizontal); buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); vlayout->addWidget(buttonBox); dlg.setFixedHeight(vlayout->sizeHint().height()); connect(buttonBox, SIGNAL(accepted()), &dlg, SLOT(accept())); connect(buttonBox, SIGNAL(rejected()), &dlg, SLOT(reject())); if (dlg.exec() != QDialog::Accepted) return false; for (const auto &fs : valueFs) fs(); return true; }
bool WulforUtil::getUserCommandParams(const UserCommand& uc, StringMap& params) { StringList names; string::size_type i = 0, j = 0; const string cmd_str = uc.getCommand(); while((i = cmd_str.find("%[line:", i)) != string::npos) { if ((j = cmd_str.find("]", (i += 7))) == string::npos) break; names.push_back(cmd_str.substr(i, j - i)); i = j + 1; } if (names.empty()) return true; QDialog dlg(MainWindow::getInstance()); dlg.setWindowTitle(_q(uc.getDisplayName().back())); QVBoxLayout *vlayout = new QVBoxLayout(&dlg); std::vector<std::function<void ()> > valueFs; foreach(const string name, names) { QString caption = _q(name); if (uc.adc()) { caption.replace("\\\\", "\\"); caption.replace("\\s", " "); } int combo_sel = -1; QString combo_caption = caption; combo_caption.replace("//", "\t"); QStringList combo_values = combo_caption.split("/"); if (combo_values.size() > 2) { QString tmp = combo_values.takeFirst(); bool isNumber = false; combo_sel = combo_values.takeFirst().toInt(&isNumber); if (!isNumber || combo_sel >= combo_values.size()) combo_sel = -1; else caption = tmp; } QGroupBox *box = new QGroupBox(caption, &dlg); QHBoxLayout *hlayout = new QHBoxLayout(box); if (combo_sel >= 0) { for(auto it = combo_values.begin(); it != combo_values.end(); it++) it->replace("\t", "/"); QComboBox *combo = new QComboBox(box); hlayout->addWidget(combo); combo->addItems(combo_values); combo->setEditable(true); combo->setCurrentIndex(combo_sel); combo->lineEdit()->setReadOnly(true); valueFs.push_back([combo, name, ¶ms] { params["line:" + name] = combo->currentText().toStdString(); }); } else { QLineEdit *line = new QLineEdit(box); hlayout->addWidget(line); valueFs.push_back([line, name, ¶ms] { params["line:" + name] = line->text().toStdString(); }); } vlayout->addWidget(box); }