Warning, file /frameworks/kconfigwidgets/tests/kcommandbartest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 * SPDX-FileCopyrightText: 2021 Waqar Ahmed <waqar.17a@gmail.com> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 #include <KCommandBar> 0007 #include <QMainWindow> 0008 0009 #include <QAction> 0010 #include <QApplication> 0011 #include <QDebug> 0012 #include <QMenu> 0013 #include <QPlainTextEdit> 0014 #include <QToolBar> 0015 #include <QVBoxLayout> 0016 0017 class Window; 0018 0019 /** 0020 * Fwd decl 0021 * A helper function to generate a QAction 0022 */ 0023 static QAction *genAction(Window *p, const QString &icon, int i, const int shortcut = Qt::CTRL); 0024 0025 class Window : public QMainWindow 0026 { 0027 public: 0028 Window(QWidget *parent = nullptr) 0029 : QMainWindow(parent) 0030 , pe(this) 0031 { 0032 setCentralWidget(&pe); 0033 0034 auto toolBar = new QToolBar(this); 0035 this->addToolBar(toolBar); 0036 0037 auto qo1 = toolBar->addAction(QStringLiteral("Open Command Bar")); 0038 connect(qo1, &QAction::triggered, this, [this] { 0039 KCommandBar bar(this); 0040 bar.setActions(getActions()); 0041 bar.exec(); 0042 }); 0043 0044 auto qo2 = toolBar->addAction(QStringLiteral("Open Command Bar (RTL)")); 0045 connect(qo2, &QAction::triggered, this, [this] { 0046 KCommandBar bar(this); 0047 bar.setActions(getRTLActions()); 0048 bar.exec(); 0049 }); 0050 } 0051 0052 QAction *getMenu() 0053 { 0054 QMenu *file = new QMenu(this); 0055 file->setTitle(QStringLiteral("File")); 0056 0057 auto createActionAndConnect = [file, this](const char *name) { 0058 auto a = file->addAction(QString::fromUtf8(name)); 0059 connect(a, &QAction::triggered, [a, this] { 0060 pe.appendPlainText(a->text() + QStringLiteral(" triggered")); 0061 }); 0062 }; 0063 0064 createActionAndConnect("File Menu action 1"); 0065 createActionAndConnect("File Menu act 2"); 0066 return file->menuAction(); 0067 } 0068 0069 QAction *getAboutToShowMenu() 0070 { 0071 QMenu *menu = new QMenu(this); 0072 menu->setTitle(QStringLiteral("Tool")); 0073 0074 connect(menu, &QMenu::aboutToShow, this, [this, menu] { 0075 if (!menu->actions().isEmpty()) { 0076 return; 0077 } 0078 0079 auto createActionAndConnect = [menu, this](const char *name) { 0080 auto a = menu->addAction(QString::fromUtf8(name)); 0081 connect(a, &QAction::triggered, [a, this] { 0082 pe.appendPlainText(a->text() + QStringLiteral(" triggered")); 0083 }); 0084 }; 0085 0086 createActionAndConnect("About to show action 1"); 0087 createActionAndConnect("About to show 2"); 0088 }); 0089 return menu->menuAction(); 0090 } 0091 0092 QVector<KCommandBar::ActionGroup> getActions() 0093 { 0094 QVector<KCommandBar::ActionGroup> acts(4); 0095 0096 /** 0097 * Menus with actions 0098 */ 0099 acts[0].actions = {getAboutToShowMenu(), getMenu()}; 0100 0101 int i = 0; 0102 acts[1].name = QStringLiteral("First Menu Group"); 0103 for (; i < 2; ++i) { 0104 acts[1].actions.append(genAction(this, QStringLiteral("folder"), i)); 0105 } 0106 acts[1].actions[0]->setShortcut(QStringLiteral("G")); 0107 acts[1].actions[1]->setCheckable(true); 0108 acts[1].actions[1]->setShortcut(QStringLiteral("Ctrl++")); 0109 0110 acts[2].name = QStringLiteral("Second Menu Group - Disabled acts"); 0111 for (; i < 4; ++i) { 0112 auto act = genAction(this, QStringLiteral("zoom-out"), i); 0113 act->setText(QStringLiteral("Disabled Act %1").arg(i)); 0114 act->setEnabled(false); 0115 acts[2].actions.append(act); 0116 } 0117 0118 acts[3].name = QStringLiteral("Third Menu Group"); 0119 for (; i < 6; ++i) { 0120 acts[3].actions.append(genAction(this, QStringLiteral("security-low"), i, Qt::CTRL | Qt::ALT)); 0121 } 0122 acts[3].actions[0]->setCheckable(true); 0123 acts[3].actions[0]->setShortcut(QStringLiteral("Ctrl+,, Ctrl++, Ctrl+K")); 0124 0125 return acts; 0126 } 0127 0128 // Use ./bin/kcommandbartest -reverse to test this 0129 QVector<KCommandBar::ActionGroup> getRTLActions() 0130 { 0131 QVector<KCommandBar::ActionGroup> acts(2); 0132 0133 acts[0].name = QStringLiteral("مینو گروپ"); 0134 acts[0].actions = {new QAction(QIcon::fromTheme("folder"), QStringLiteral("یہ فولڈر ایکشن ہے"), this), 0135 new QAction(QIcon::fromTheme("folder"), QStringLiteral("یہ ایک اور فولڈر ایکشن ہے"), this)}; 0136 acts[0].actions[1]->setCheckable(true); 0137 acts[0].actions[1]->setShortcut(QStringLiteral("Ctrl+Shift++")); 0138 0139 acts[1].name = QStringLiteral("گروپ"); 0140 acts[1].actions = {new QAction(QIcon::fromTheme("zoom-out"), QStringLiteral("یہ فولڈر ایکشن ہے"), this), 0141 new QAction(QIcon::fromTheme("security-low"), QStringLiteral("یہ ایک اور فولڈر ایکشن ہے"), this)}; 0142 acts[1].actions[1]->setCheckable(true); 0143 acts[1].actions[1]->setShortcut(QStringLiteral("Ctrl+-")); 0144 0145 return acts; 0146 } 0147 0148 QPlainTextEdit *textEdit() 0149 { 0150 return &pe; 0151 } 0152 0153 private: 0154 QPlainTextEdit pe; 0155 }; 0156 0157 static QAction *genAction(Window *p, const QString &icon, int i, const int shortcut) 0158 { 0159 QString text = QStringLiteral("A long long Action name %1").arg(i++); 0160 QAction *action = new QAction(QIcon::fromTheme(icon), text, p); 0161 QObject::connect(action, &QAction::triggered, [action, p] { 0162 p->textEdit()->appendPlainText(action->text() + QStringLiteral(" triggered")); 0163 }); 0164 0165 static int key = Qt::Key_1; 0166 key++; 0167 // Reset 0168 if (key == Qt::Key_BraceRight) { 0169 key = Qt::Key_1; 0170 } 0171 const auto ss = shortcut | key; 0172 action->setShortcut(ss); 0173 return action; 0174 } 0175 0176 int main(int argc, char *argv[]) 0177 { 0178 QApplication app(argc, argv); 0179 QApplication::setApplicationName(QStringLiteral("kcommandbartest")); 0180 0181 Window *window = new Window(); 0182 window->resize(1024, 600); 0183 window->show(); 0184 0185 return app.exec(); 0186 }