Debian Codegraph @

src/Gui/CommandCompleter.cpp

raw
1 /****************************************************************************
2 * Copyright (c) 2022 Zheng Lei (realthunder) <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ****************************************************************************/
22
23 #include <QApplication>
24 #include <QKeyEvent>
25 #include <QLineEdit>
26 #include <QAbstractItemView>
27
28 #include "Application.h"
29 #include "ShortcutManager.h"
30 #include "Command.h"
31 #include "Action.h"
32 #include "BitmapFactory.h"
33 #include "CommandCompleter.h"
34
35 using namespace Gui;
36
37 namespace
38 {
39
40 struct CmdInfo
41 {
42 Command* cmd = nullptr;
43 QIcon icon;
44 bool iconChecked = false;
45 };
46 std::vector<CmdInfo> _Commands;
47 int _CommandRevision;
48 const int CommandNameRole = Qt::UserRole;
49 bool _ShortcutSignalConnected = false;
50
51 class CommandModel: public QAbstractItemModel
52 {
53 int revision = 0;
54
55 public:
56 explicit CommandModel(QObject* parent)
57 : QAbstractItemModel(parent)
58 {
59 update();
60 if (!_ShortcutSignalConnected) {
61 _ShortcutSignalConnected = true;
62 QObject::connect(ShortcutManager::instance(), &ShortcutManager::shortcutChanged, [] {
63 _CommandRevision = 0;
64 });
65 }
66 }
67
68 void update()
69 {
70 auto& manager = Application::Instance->commandManager();
71 if (revision == _CommandRevision && _CommandRevision == manager.getRevision()) {
72 return;
73 }
74 beginResetModel();
75 revision = manager.getRevision();
76 if (revision != _CommandRevision) {
77 _CommandRevision = revision;
78 _CommandRevision = manager.getRevision();
79 _Commands.clear();
80 for (auto& v : manager.getCommands()) {
81 _Commands.emplace_back();
82 auto& info = _Commands.back();
83 info.cmd = v.second;
84 }
85 }
86 endResetModel();
87 }
88
89 QModelIndex parent(const QModelIndex&) const override
90 {
91 return {};
92 }
93
94 QVariant data(const QModelIndex& index, int role) const override
95 {
96 if (index.row() < 0 || index.row() >= (int)_Commands.size()) {
97 return {};
98 }
99
100 auto& info = _Commands[index.row()];
101
102 switch (role) {
103 case Qt::DisplayRole:
104 case Qt::EditRole: {
105 QString title = QStringLiteral("%1 (%2)").arg(
106 Action::commandMenuText(info.cmd),
107 QString::fromUtf8(info.cmd->getName())
108 );
109 QString shortcut = info.cmd->getShortcut();
110 if (!shortcut.isEmpty()) {
111 title += QStringLiteral(" (%1)").arg(shortcut);
112 }
113 return title;
114 }
115 case Qt::ToolTipRole:
116 return Action::commandToolTip(info.cmd);
117
118 case Qt::DecorationRole:
119 if (!info.iconChecked) {
120 info.iconChecked = true;
121 if (info.cmd->getPixmap()) {
122 info.icon = BitmapFactory().iconFromTheme(info.cmd->getPixmap());
123 }
124 }
125 return info.icon;
126
127 case CommandNameRole:
128 return QByteArray(info.cmd->getName());
129
130 default:
131 break;
132 }
133 return {};
134 }
135
136 QModelIndex index(int row, int, const QModelIndex&) const override
137 {
138 return this->createIndex(row, 0);
139 }
140
141 int rowCount(const QModelIndex&) const override
142 {
143 return (int)(_Commands.size());
144 }
145
146 int columnCount(const QModelIndex&) const override
147 {
148 return 1;
149 }
150 };
151
152 } // anonymous namespace
153
154 // --------------------------------------------------------------------
155
156 CommandCompleter::CommandCompleter(QLineEdit* lineedit, QObject* parent)
157 : QCompleter(parent)
158 {
159 this->setModel(new CommandModel(this));
160 this->setFilterMode(Qt::MatchContains);
161 this->setCaseSensitivity(Qt::CaseInsensitive);
162 this->setCompletionMode(QCompleter::PopupCompletion);
163 this->setWidget(lineedit);
164 connect(lineedit, &QLineEdit::textEdited, this, &CommandCompleter::onTextChanged);
165 connect(
166 this,
167 qOverload<const QModelIndex&>(&CommandCompleter::activated),
168 this,
169 &CommandCompleter::onCommandActivated
170 );
171 connect(this, qOverload<const QString&>(&CommandCompleter::highlighted), lineedit, &QLineEdit::setText);
172 }
173
174 bool CommandCompleter::eventFilter(QObject* o, QEvent* ev)
175 {
176 if (ev->type() == QEvent::KeyPress && (o == this->widget() || o == this->popup())) {
177 QKeyEvent* ke = static_cast<QKeyEvent*>(ev);
178 switch (ke->key()) {
179 case Qt::Key_Escape: {
180 auto edit = qobject_cast<QLineEdit*>(this->widget());
181 if (edit && edit->text().size()) {
182 edit->setText(QString());
183 popup()->hide();
184 return true;
185 }
186 else if (popup()->isVisible()) {
187 popup()->hide();
188 return true;
189 }
190 break;
191 }
192 case Qt::Key_Tab: {
193 if (this->popup()->isVisible()) {
194 QKeyEvent kevent(ke->type(), Qt::Key_Down, Qt::NoModifier);
195 qApp->sendEvent(this->popup(), &kevent);
196 return true;
197 }
198 break;
199 }
200 case Qt::Key_Backtab: {
201 if (this->popup()->isVisible()) {
202 QKeyEvent kevent(ke->type(), Qt::Key_Up, Qt::NoModifier);
203 qApp->sendEvent(this->popup(), &kevent);
204 return true;
205 }
206 break;
207 }
208 case Qt::Key_Enter:
209 case Qt::Key_Return:
210 if (o == this->widget()) {
211 auto index = currentIndex();
212 if (index.isValid()) {
213 onCommandActivated(index);
214 }
215 else {
216 complete();
217 }
218 ev->setAccepted(true);
219 return true;
220 }
221 default:
222 break;
223 }
224 }
225 return QCompleter::eventFilter(o, ev);
226 }
227
228 void CommandCompleter::onCommandActivated(const QModelIndex& index)
229 {
230 QByteArray name = completionModel()->data(index, CommandNameRole).toByteArray();
231 Q_EMIT commandActivated(name);
232 }
233
234 void CommandCompleter::onTextChanged(const QString& txt)
235 {
236 // Do not activate completer if less than 3 characters for better
237 // performance.
238 if (txt.size() < 3 || !widget()) {
239 return;
240 }
241
242 static_cast<CommandModel*>(this->model())->update();
243
244 this->setCompletionPrefix(txt);
245 QRect rect = widget()->rect();
246 if (rect.width() < 300) {
247 rect.setWidth(300);
248 }
249 this->complete(rect);
250 }
251
252 #include "moc_CommandCompleter.cpp"