-
Notifications
You must be signed in to change notification settings - Fork 428
/
clipboardbrowser.h
446 lines (335 loc) · 12.9 KB
/
clipboardbrowser.h
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
Copyright (c) 2016, Lukas Holecek <hluk@email.cz>
This file is part of CopyQ.
CopyQ is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CopyQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CopyQ. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CLIPBOARDBROWSER_H
#define CLIPBOARDBROWSER_H
#include "common/command.h"
#include "gui/configtabshortcuts.h"
#include "item/clipboardmodel.h"
#include "item/itemdelegate.h"
#include "item/itemwidget.h"
#include <QListView>
#include <QPointer>
#include <QScopedPointer>
#include <QSharedPointer>
#include <QTimer>
#include <QVariantMap>
class ItemEditorWidget;
class ItemFactory;
class QProgressBar;
class QPushButton;
class Theme;
enum SelectAction {
NoSelectAction,
MoveToTop = 1 << 1,
MoveToClipboard = 1 << 2
};
Q_DECLARE_FLAGS(SelectActions, SelectAction)
Q_DECLARE_OPERATORS_FOR_FLAGS(SelectActions)
struct ClipboardBrowserShared {
explicit ClipboardBrowserShared(ItemFactory *itemFactory);
void loadFromConfiguration();
QString editor;
int maxItems;
bool textWrap;
bool viMode;
bool saveOnReturnKey;
bool moveItemOnReturnKey;
int minutesToExpire;
ItemFactory *itemFactory;
};
typedef QSharedPointer<ClipboardBrowserShared> ClipboardBrowserSharedPtr;
QVariantMap itemData(const QModelIndex &index);
/** List view of clipboard items. */
class ClipboardBrowser : public QListView
{
Q_OBJECT
public:
/**
* Disable ClipboardBrowser's automatic visual and clipboard updates.
*/
class Lock {
public:
Lock(ClipboardBrowser *self) : c(self) { c->lock(); }
~Lock() { if (!c.isNull()) c->unlock(); }
private:
QPointer<ClipboardBrowser> c;
};
explicit ClipboardBrowser(const ClipboardBrowserSharedPtr &sharedData, QWidget *parent = NULL);
/** Close all external editors and save items if needed. */
~ClipboardBrowser();
/** Load settings. */
void loadSettings();
/**
* Select item with given @a hash and move it to clipboard.
*
* @return true only if item exists
*/
bool select(uint itemHash, SelectActions selectActions);
/** Sort selected items. */
void sortItems(const QModelIndexList &indexes);
/** Reverse order of selected items. */
void reverseItems(const QModelIndexList &indexes);
/** Index of item in given row. */
QModelIndex index(int i) const { return model()->index(i,0); }
/** Returns concatenation of selected items. */
const QString selectedText() const;
/** Invalidate item cache. */
void invalidateItemCache();
/**
* Set ID. Used to save items. If ID is empty saving is disabled.
*/
void setTabName(const QString &id);
const QString &tabName() const { return m_tabName; }
/**
* Return true if editing is active.
*/
bool editing() const;
/**
* Return true if items are loaded (loadItems() called or tabName() is empty).
*/
bool isLoaded() const;
/**
* Close editor if unless user don't want to discard changed (show message box).
*
* @return true only if editor was closed
*/
bool maybeCloseEditor();
/**
* Get data of selected item, NULL if none or multiple items selected.
*/
QVariantMap getSelectedItemData() const;
/**
* Override to disable default QAbstractItemView search.
*/
void keyboardSearch(const QString &) {}
/** Return true if user defined a selection and it shouldn't change programmatically. */
bool hasUserSelection() const;
QVariantMap copyIndexes(const QModelIndexList &indexes, bool serializeItems = true) const;
/** Remove items and return row number of last removed item. */
int removeIndexes(const QModelIndexList &indexes);
/** Paste items. */
void paste(const QVariantMap &data, int destinationRow);
/** Render preview image with items. */
QPixmap renderItemPreview(const QModelIndexList &indexes, int maxWidth, int maxHeight);
/** Add new item to the browser. */
bool add(
const QString &txt, //!< Text of new item.
int row = 0 //!< Target row for the new item (negative to append item).
);
/**
* Add new item to the browser.
* @a item is automatically deleted after it's no longer needed.
*/
bool add(
const QVariantMap &item, //!< Data for new item.
int row = 0 //!< Target row for the new item (negative to append item).
);
/**
* Add item and remove duplicates.
*/
void addUnique(const QVariantMap &data);
/** Number of items in list. */
int length() const { return model()->rowCount(); }
/** Receive key event. */
void keyEvent(QKeyEvent *event) { keyPressEvent(event); }
/** Move item to clipboard. */
void moveToClipboard(const QModelIndex &ind);
/** Show only items matching the regular expression. */
void filterItems(const QRegExp &re);
/** Show all items. */
void clearFilter() { filterItems( QRegExp() ); }
/** Open editor. */
bool openEditor(const QByteArray &textData, bool changeClipboard = false);
/** Open editor for an item. */
bool openEditor(const QModelIndex &index);
/** Add items. */
void addItems(const QStringList &items);
void removeRow(int row);
/** Set current item. */
void setCurrent(
int row, //!< Row of the item.
bool cycle = false, //!< If true @a row is relative number of rows from top.
bool keepSelection = false, //!< Keeps current selection.
bool setCurrentOnly = false //! Sets item as current.
);
/**
* Load items from configuration even if model is disabled.
* @see loadItems
*/
void loadItemsAgain();
/**
* Save items to configuration if needed.
*/
void saveUnsavedItems();
/**
* Clear all items from configuration.
* @see setID, loadItems, saveItems
*/
void purgeItems();
/**
* Create and edit new item.
*/
void editNew(
const QString &text = QString(), //!< Text of new item.
bool changeClipboard = false //!< Change clipboard if item is modified.
);
/** Edit item in given @a row. */
void editRow(int row);
void otherItemLoader(bool next);
void move(int key);
/// Decorate browser and items with @a theme.
void decorate(const Theme &theme);
public slots:
/**
* Load items from configuration.
* This function does nothing if model is disabled (e.g. loading failed previously).
* @see setID, saveItems, purgeItems
*/
void loadItems();
/**
* Save items to configuration.
* @see setID, loadItems, purgeItems
*/
bool saveItems();
/** Move current item to clipboard. */
void moveToClipboard();
/** Edit selected unhidden items. */
void editSelected();
/** Edit notes for current item. */
void editNotes();
/** Open editor with text of all selected items or for single selected item. */
bool openEditor();
/** Remove selected unhidden items. */
void remove();
/** Show content of current item. */
void showItemContent();
/** Item modified in external editor. */
void itemModified(const QByteArray &bytes, const QString &mime);
/** Called if editor was closed. */
void closeExternalEditor(QObject *editor);
signals:
/** Action dialog requested. */
void requestActionDialog(const QVariantMap &data);
/** Action dialog requested. */
void requestActionDialog(const QVariantMap &data, const Command &cmd, const QModelIndex &index);
/** Show list request. */
void requestShow(const ClipboardBrowser *self);
/** Request clipboard change. */
void changeClipboard(const QVariantMap &data);
/** Emitted on error. */
void error(const QString &errorString);
void itemCountChanged(const QString &tabName, int count);
void showContextMenu(const QPoint &position);
void updateContextMenu();
protected:
void keyPressEvent(QKeyEvent *event);
void contextMenuEvent(QContextMenuEvent *);
void resizeEvent(QResizeEvent *event);
void showEvent(QShowEvent *event);
void hideEvent(QHideEvent *event);
void currentChanged(const QModelIndex ¤t, const QModelIndex &previous);
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
void focusInEvent(QFocusEvent *event);
void dragEnterEvent(QDragEnterEvent *event);
void dragLeaveEvent(QDragLeaveEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);
void paintEvent(QPaintEvent *e);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
private slots:
void onModelDataChanged();
void onDataChanged(const QModelIndex &a, const QModelIndex &b);
void onItemCountChanged();
void onTabNameChanged(const QString &tabName);
/** Delayed update. */
void updateCurrentPage();
/** Immediate update if possible. */
void doUpdateCurrentPage();
void expire();
void onEditorDestroyed();
void onEditorSave();
void onEditorCancel();
void onModelUnloaded();
void onEditorNeedsChangeClipboard();
void onEditorNeedsChangeClipboard(const QByteArray &bytes, const QString &mime);
void filterItems();
private:
/**
* Save items to configuration after an interval.
*/
void delayedSaveItems();
bool isFiltered(int row) const;
/**
* Hide row if filtered out, otherwise show.
* @return true only if hidden
*/
bool hideFiltered(int row);
/**
* Connects signals and starts external editor.
*/
bool startEditor(QObject *editor, bool changeClipboard = false);
/**
* Select next/previous item and copy it to clipboard.
*/
void copyItemToClipboard(int d);
/**
* Preload items in given range (relative to current scroll offset).
*/
void preload(int minY, int maxY);
void setEditorWidget(ItemEditorWidget *widget, bool changeClipboard = false);
void editItem(const QModelIndex &index, bool editNotes = false, bool changeClipboard = false);
void updateEditorGeometry();
bool canExpire();
void restartExpiring();
void stopExpiring();
void updateCurrentItem();
/**
* Get index near given @a point.
* If space between items is at the @a point, return next item.
*/
QModelIndex indexNear(int offset) const;
void updateSearchProgress();
int getDropRow(const QPoint &position);
void connectModelAndDelegate();
void disconnectModel();
void updateItemMaximumSize();
void lock();
void unlock();
void refilterItems();
ItemLoaderInterface *m_itemLoader;
QString m_tabName;
int m_lastFiltered;
ClipboardModel m;
ItemDelegate d;
QTimer m_timerSave;
QTimer m_timerScroll;
QTimer m_timerUpdate;
QTimer m_timerFilter;
QTimer m_timerExpire;
bool m_invalidateCache;
bool m_expireAfterEditing;
ItemEditorWidget *m_editor;
bool m_editClipboard;
ClipboardBrowserSharedPtr m_sharedData;
QPushButton *m_loadButton;
QProgressBar *m_searchProgress;
int m_dragTargetRow;
QPoint m_dragStartPosition;
int m_spinLock;
QScopedPointer<class ScrollSaver> m_scrollSaver;
};
#endif // CLIPBOARDBROWSER_H