File indexing completed on 2024-05-05 04:40:52
0001 /* 0002 SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <aleixpol@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "actionsquickopenprovider.h" 0008 0009 #include <KActionCollection> 0010 #include <KLocalizedString> 0011 #include <QIcon> 0012 #include <QAction> 0013 #include <QRegularExpression> 0014 0015 using namespace KDevelop; 0016 0017 class ActionsQuickOpenItem 0018 : public QuickOpenDataBase 0019 { 0020 public: 0021 ActionsQuickOpenItem(const QString& display, QAction* action) 0022 : QuickOpenDataBase() 0023 , m_action(action) 0024 , m_display(display) 0025 {} 0026 0027 QString text() const override 0028 { 0029 return m_display; 0030 } 0031 QString htmlDescription() const override 0032 { 0033 QString desc = m_action->toolTip(); 0034 const QKeySequence shortcut = m_action->shortcut(); 0035 if (!shortcut.isEmpty()) { 0036 desc = i18nc("description (shortcut)", "%1 (%2)", desc, shortcut.toString()); 0037 } 0038 return desc; 0039 } 0040 bool execute(QString&) override 0041 { 0042 m_action->trigger(); 0043 return true; 0044 } 0045 QIcon icon() const override 0046 { 0047 const QIcon icon = m_action->icon(); 0048 if (icon.isNull()) { 0049 // note: not the best fallback icon, but can't find anything better 0050 return QIcon::fromTheme(QStringLiteral("system-run")); 0051 } 0052 0053 return icon; 0054 } 0055 0056 private: 0057 QAction* const m_action; 0058 0059 ///needed because it won't have the "E&xit" ampersand 0060 const QString m_display; 0061 }; 0062 0063 ActionsQuickOpenProvider::ActionsQuickOpenProvider() 0064 { 0065 } 0066 0067 void ActionsQuickOpenProvider::setFilterText(const QString& text) 0068 { 0069 if (text.size() < 2) { 0070 return; 0071 } 0072 m_results.clear(); 0073 const QList<KActionCollection*> collections = KActionCollection::allCollections(); 0074 QRegularExpression mnemonicRx(QStringLiteral("^(.*)&(.+)$")); 0075 for (KActionCollection* c : collections) { 0076 const QList<QAction*> actions = c->actions(); 0077 for (QAction* action : actions) { 0078 if (!action->isEnabled()) { 0079 continue; 0080 } 0081 0082 QString display = action->text(); 0083 QRegularExpressionMatch match = mnemonicRx.match(display); 0084 if (match.hasMatch()) { 0085 display = match.capturedRef(1) + match.capturedRef(2); 0086 } 0087 0088 if (display.contains(text, Qt::CaseInsensitive)) { 0089 m_results += QuickOpenDataPointer(new ActionsQuickOpenItem(display, action)); 0090 } 0091 } 0092 } 0093 } 0094 0095 uint ActionsQuickOpenProvider::unfilteredItemCount() const 0096 { 0097 uint ret = 0; 0098 const QList<KActionCollection*> collections = KActionCollection::allCollections(); 0099 for (KActionCollection* c : collections) { 0100 ret += c->count(); 0101 } 0102 0103 return ret; 0104 } 0105 0106 QuickOpenDataPointer ActionsQuickOpenProvider::data(uint row) const 0107 { 0108 return m_results.at(row); 0109 } 0110 0111 uint ActionsQuickOpenProvider::itemCount() const 0112 { 0113 return m_results.count(); 0114 } 0115 0116 void ActionsQuickOpenProvider::reset() 0117 { 0118 m_results.clear(); 0119 } 0120 0121 #include "moc_actionsquickopenprovider.cpp"