| 66 | } |
| 67 | |
| 68 | void QuickCommandsPlugin::activeViewChanged(Konsole::SessionController *controller, Konsole::MainWindow *mainWindow) |
| 69 | { |
| 70 | if (mainWindow == nullptr || controller == nullptr) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | priv->showQuickAccess->deleteLater(); |
| 75 | priv->showQuickAccess = new QAction(i18n("Show Quick Access")); |
| 76 | |
| 77 | QSettings settings; |
| 78 | settings.beginGroup(QStringLiteral("plugins")); |
| 79 | settings.beginGroup(QStringLiteral("quickcommands")); |
| 80 | |
| 81 | const QKeySequence def(Qt::CTRL | Qt::ALT | Qt::Key_G); |
| 82 | const QString defText = def.toString(); |
| 83 | const QString entry = settings.value(QStringLiteral("shortcut"), defText).toString(); |
| 84 | const QKeySequence shortcutEntry(entry); |
| 85 | |
| 86 | mainWindow->actionCollection()->setDefaultShortcut(priv->showQuickAccess, shortcutEntry); |
| 87 | |
| 88 | // controller may exist while its view is being torn down |
| 89 | QPointer<Konsole::TerminalDisplay> terminalDisplay = controller->view(); |
| 90 | if (terminalDisplay == nullptr) { |
| 91 | return; |
| 92 | } |
| 93 | terminalDisplay->addAction(priv->showQuickAccess); |
| 94 | |
| 95 | QPointer<Konsole::SessionController> controllerPtr(controller); |
| 96 | connect(priv->showQuickAccess, &QAction::triggered, this, [this, terminalDisplay, controllerPtr] { |
| 97 | if (terminalDisplay == nullptr) { |
| 98 | return; |
| 99 | } |
| 100 | auto bar = new KCommandBar(terminalDisplay->topLevelWidget()); |
| 101 | QList<QAction *> actions; |
| 102 | for (int i = 0; i < priv->model.rowCount(); i++) { |
| 103 | QModelIndex folder = priv->model.index(i, 0); |
| 104 | for (int e = 0; e < priv->model.rowCount(folder); e++) { |
| 105 | QModelIndex idx = priv->model.index(e, 0, folder); |
| 106 | QAction *act = new QAction(idx.data().toString()); |
| 107 | connect(act, &QAction::triggered, this, [this, idx, controllerPtr] { |
| 108 | if (controllerPtr == nullptr) { |
| 109 | return; |
| 110 | } |
| 111 | const auto item = priv->model.itemFromIndex(idx); |
| 112 | const auto data = item->data(QuickCommandsModel::QuickCommandRole).value<QuickCommandData>(); |
| 113 | if (controllerPtr->session() != nullptr) { |
| 114 | controllerPtr->session()->sendTextToTerminal(data.command, QLatin1Char('\r')); |
| 115 | } |
| 116 | }); |
| 117 | actions.append(act); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | if (actions.isEmpty()) // no quick commands found, must give feedback to the user about that |
| 122 | { |
| 123 | const QString feedbackMessage = i18n("No quick commands found. You can add one on Plugins -> Quick Commands"); |
| 124 | const QString feedbackTitle = i18n("Plugins - Quick Commands"); |
| 125 | KMessageBox::error(terminalDisplay->topLevelWidget(), feedbackMessage, feedbackTitle); |
nothing calls this directly
no test coverage detected