-
Notifications
You must be signed in to change notification settings - Fork 426
/
traymenu.cpp
379 lines (321 loc) · 10.5 KB
/
traymenu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// SPDX-License-Identifier: GPL-3.0-or-later
#include "traymenu.h"
#include "common/contenttype.h"
#include "common/common.h"
#include "common/display.h"
#include "common/mimetypes.h"
#include "common/textdata.h"
#include "common/timer.h"
#include "gui/icons.h"
#include "gui/iconfactory.h"
#include <QAction>
#include <QApplication>
#include <QKeyEvent>
#include <QModelIndex>
#include <QPixmap>
#include <QRegularExpression>
namespace {
const char propertyTextFormat[] = "CopyQ_text_format";
const QIcon iconClipboard() { return getIcon("clipboard", IconPaste); }
bool canActivate(const QAction &action)
{
return !action.isSeparator() && action.isEnabled();
}
QAction *firstEnabledAction(QMenu *menu)
{
// First action is active when menu pops up.
for (auto action : menu->actions()) {
if ( canActivate(*action) )
return action;
}
return nullptr;
}
QAction *lastEnabledAction(QMenu *menu)
{
// First action is active when menu pops up.
QList<QAction *> actions = menu->actions();
for (int i = actions.size() - 1; i >= 0; --i) {
if ( canActivate(*actions[i]) )
return actions[i];
}
return nullptr;
}
} // namespace
TrayMenu::TrayMenu(QWidget *parent)
: QMenu(parent)
, m_clipboardItemActionCount(0)
, m_omitPaste(false)
, m_viMode(false)
, m_numberSearch(false)
{
m_clipboardItemActionsSeparator = addSeparator();
m_customActionsSeparator = addSeparator();
initSingleShotTimer( &m_timerUpdateActiveAction, 0, this, &TrayMenu::doUpdateActiveAction );
setAttribute(Qt::WA_InputMethodEnabled);
}
void TrayMenu::updateTextFromData(QAction *act, const QVariantMap &data)
{
const QString format = act->property(propertyTextFormat).toString();
const QString label = textLabelForData( data, act->font(), format, true );
act->setText(label);
}
bool TrayMenu::updateIconFromData(QAction *act, const QVariantMap &data)
{
if ( !act->parentWidget() )
return false;
const QString icon = data.value(mimeIcon).toString();
const QString tag = data.value(COPYQ_MIME_PREFIX "item-tag").toString();
if ( icon.isEmpty() && tag.isEmpty() )
return false;
const QColor color = getDefaultIconColor(*act->parentWidget());
act->setIcon( iconFromFile(icon, tag, color) );
return true;
}
QAction *TrayMenu::addClipboardItemAction(const QVariantMap &data, bool showImages)
{
// Show search text at top of the menu.
if ( m_clipboardItemActionCount == 0 && m_searchText.isEmpty() )
setSearchMenuItem( m_viMode ? tr("Press '/' to search") : tr("Type to search") );
QAction *act = addAction(QString());
m_clipboardActions.append(act);
act->setData(data);
insertAction(m_clipboardItemActionsSeparator, act);
QString format;
// Add number key hint.
const int rowNumber = m_clipboardItemActionCount + static_cast<int>(m_rowIndexFromOne);
if (rowNumber < 10) {
format = tr("&%1. %2",
"Key hint (number shortcut) for items in tray menu (%1 is number, %2 is item label)")
.arg(rowNumber);
act->setProperty(propertyTextFormat, format);
}
m_clipboardItemActionCount++;
updateTextFromData(act, data);
// Menu item icon from image.
if (!updateIconFromData(act, data) && showImages) {
const QStringList formats = data.keys();
static const QRegularExpression reImage("^image/.*");
const int imageIndex = formats.indexOf(reImage);
if (imageIndex != -1) {
const auto &mime = formats[imageIndex];
QPixmap pix;
pix.loadFromData( data.value(mime).toByteArray(), mime.toLatin1().data() );
const int iconSize = smallIconSize();
int x = 0;
int y = 0;
if (pix.width() > pix.height()) {
pix = pix.scaledToHeight(iconSize);
x = (pix.width() - iconSize) / 2;
} else {
pix = pix.scaledToWidth(iconSize);
y = (pix.height() - iconSize) / 2;
}
pix = pix.copy(x, y, iconSize, iconSize);
act->setIcon(pix);
}
}
connect(act, &QAction::triggered, this, &TrayMenu::onClipboardItemActionTriggered);
return act;
}
void TrayMenu::clearClipboardItems()
{
const auto actions = m_clipboardActions;
m_clipboardActions = {};
for (QAction *action : actions) {
removeAction(action);
delete action;
}
m_clipboardItemActionCount = 0;
// Show search text at top of the menu.
if ( !m_searchText.isEmpty() )
setSearchMenuItem(m_searchText);
}
void TrayMenu::clearCustomActions()
{
const auto actions = m_customActions;
m_customActions = {};
for (QAction *action : actions) {
removeAction(action);
delete action;
}
}
void TrayMenu::setCustomActions(QList<QAction*> actions)
{
clearCustomActions();
m_customActions = actions;
insertActions(m_customActionsSeparator, actions);
}
void TrayMenu::clearAllActions()
{
m_clipboardActions = {};
m_customActions = {};
clear();
m_clipboardItemActionCount = 0;
m_searchText.clear();
}
void TrayMenu::setViModeEnabled(bool enabled)
{
m_viMode = enabled;
}
void TrayMenu::setNumberSearchEnabled(bool enabled)
{
m_numberSearch = enabled;
}
void TrayMenu::keyPressEvent(QKeyEvent *event)
{
const int key = event->key();
m_omitPaste = false;
if ( m_viMode && m_searchText.isEmpty() && handleViKey(event, this) ) {
return;
} else {
// Movement in tray menu.
switch (key) {
case Qt::Key_PageDown:
case Qt::Key_End: {
QAction *action = lastEnabledAction(this);
if (action != nullptr)
setActiveAction(action);
break;
}
case Qt::Key_PageUp:
case Qt::Key_Home: {
QAction *action = firstEnabledAction(this);
if (action != nullptr)
setActiveAction(action);
break;
}
case Qt::Key_Escape:
close();
break;
case Qt::Key_Backspace:
search( m_searchText.left(m_searchText.size() - 1) );
break;
case Qt::Key_Delete:
search(QString());
break;
case Qt::Key_Alt:
return;
default:
// Type text for search.
if ( (m_clipboardItemActionCount > 0 || !m_searchText.isEmpty())
&& (!m_viMode || !m_searchText.isEmpty() || key == Qt::Key_Slash)
&& !event->modifiers().testFlag(Qt::AltModifier)
&& !event->modifiers().testFlag(Qt::ControlModifier) )
{
const QString txt = event->text();
if ( !txt.isEmpty() && txt[0].isPrint() ) {
// Activate item at row when number is entered.
if ( !m_numberSearch && m_searchText.isEmpty() ) {
bool ok;
const int row = txt.toInt(&ok);
if (ok && row < m_clipboardItemActionCount) {
// Allow keypad digit to activate appropriate item in context menu.
if (event->modifiers() == Qt::KeypadModifier)
event->setModifiers(Qt::NoModifier);
break;
}
}
search(m_searchText + txt);
return;
}
}
break;
}
}
QMenu::keyPressEvent(event);
}
void TrayMenu::mousePressEvent(QMouseEvent *event)
{
m_omitPaste = (event->button() == Qt::RightButton);
QMenu::mousePressEvent(event);
}
void TrayMenu::showEvent(QShowEvent *event)
{
search(QString());
// If appmenu is used to handle the menu, most events won't be received
// so search won't work.
// This shows the search menu item only if show event is received.
if ( !m_searchAction.isNull() )
m_searchAction->setVisible(true);
if ( m_timerUpdateActiveAction.isActive() )
doUpdateActiveAction();
QMenu::showEvent(event);
}
void TrayMenu::hideEvent(QHideEvent *event)
{
QMenu::hideEvent(event);
if ( !m_searchAction.isNull() )
m_searchAction->setVisible(false);
}
void TrayMenu::actionEvent(QActionEvent *event)
{
delayedUpdateActiveAction();
QMenu::actionEvent(event);
}
void TrayMenu::leaveEvent(QEvent *event)
{
// Omit clearing active action if menu is resizes and mouse pointer leaves menu.
auto action = activeAction();
QMenu::leaveEvent(event);
setActiveAction(action);
}
void TrayMenu::inputMethodEvent(QInputMethodEvent *event)
{
if (!event->commitString().isEmpty())
search(m_searchText + event->commitString());
event->ignore();
}
void TrayMenu::search(const QString &text)
{
if (m_searchText == text)
return;
m_searchText = text;
emit searchRequest(m_viMode ? m_searchText.mid(1) : m_searchText);
}
void TrayMenu::markItemInClipboard(const QVariantMap &clipboardData)
{
const auto text = getTextData(clipboardData);
for ( auto action : actions() ) {
const auto actionData = action->data().toMap();
const auto itemText = getTextData(actionData);
if ( !itemText.isEmpty() ) {
const auto hideIcon = itemText != text;
const auto isIconHidden = action->icon().isNull();
if ( isIconHidden != hideIcon )
action->setIcon(hideIcon ? QIcon() : iconClipboard());
}
}
}
void TrayMenu::setSearchMenuItem(const QString &text)
{
if ( m_searchAction.isNull() ) {
const QIcon icon = getIcon("edit-find", IconMagnifyingGlass);
m_searchAction = new QAction(icon, text, this);
m_searchAction->setEnabled(false);
// Search menu item is hidden by default, see showEvent().
m_searchAction->setVisible( isVisible() );
insertAction( actions().value(0), m_searchAction );
} else {
m_searchAction->setText(text);
}
}
void TrayMenu::onClipboardItemActionTriggered()
{
QAction *act = qobject_cast<QAction *>(sender());
Q_ASSERT(act != nullptr);
const auto actionData = act->data().toMap();
emit clipboardItemActionTriggered(actionData, m_omitPaste);
close();
}
void TrayMenu::delayedUpdateActiveAction()
{
if ( !isVisible() || activeAction() == nullptr )
m_timerUpdateActiveAction.start();
}
void TrayMenu::doUpdateActiveAction()
{
m_timerUpdateActiveAction.stop();
const auto action = firstEnabledAction(this);
if (action != nullptr)
setActiveAction(action);
}