/
itempinned.cpp
329 lines (263 loc) · 8.6 KB
/
itempinned.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
/*
Copyright (c) 2014, 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/>.
*/
#include "itempinned.h"
#include "ui_itempinnedsettings.h"
#include "common/common.h"
#include "common/command.h"
#include "common/contenttype.h"
#ifdef HAS_TESTS
# include "tests/itempinnedtests.h"
#endif
#include <QApplication>
#include <QBoxLayout>
#include <QMessageBox>
#include <QModelIndex>
#include <algorithm>
namespace {
const char mimePinned[] = "application/x-copyq-item-pinned";
int pinnedRow(const QModelIndex &index)
{
bool ok;
const auto dataMap = index.data(contentType::data).toMap();
const auto pinnedRow = dataMap.value(mimePinned).toInt(&ok);
return ok ? pinnedRow : -1;
}
bool isPinned(const QModelIndex &index)
{
return pinnedRow(index) >= 0;
}
Command dummyPinCommand()
{
Command c;
c.icon = QString(QChar(IconThumbTack));
c.inMenu = true;
c.shortcuts = QStringList()
<< ItemPinnedLoader::tr("Ctrl+Shift+P", "Shortcut to pin and unpin items");
return c;
}
} // namespace
ItemPinned::ItemPinned(ItemWidget *childItem, int pinnedRow)
: QWidget( childItem->widget()->parentWidget() )
, ItemWidget(this)
, m_border(new QWidget(this))
, m_childItem(childItem)
, m_pinnedRow(pinnedRow)
{
m_childItem->widget()->setObjectName("item_child");
m_childItem->widget()->setParent(this);
m_border->setFixedWidth( pointsToPixels(8) );
m_border->setStyleSheet("background: rgba(0,0,0,0.2)");
QBoxLayout *layout;
layout = new QHBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing( pointsToPixels(5) );
layout->addWidget(m_childItem->widget());
layout->addWidget(m_border);
}
void ItemPinned::highlight(const QRegExp &re, const QFont &highlightFont, const QPalette &highlightPalette)
{
m_childItem->setHighlight(re, highlightFont, highlightPalette);
}
QWidget *ItemPinned::createEditor(QWidget *parent) const
{
return m_childItem->createEditor(parent);
}
void ItemPinned::setEditorData(QWidget *editor, const QModelIndex &index) const
{
return m_childItem->setEditorData(editor, index);
}
void ItemPinned::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
return m_childItem->setModelData(editor, model, index);
}
bool ItemPinned::hasChanges(QWidget *editor) const
{
return m_childItem->hasChanges(editor);
}
QObject *ItemPinned::createExternalEditor(const QModelIndex &index, QWidget *parent) const
{
return m_childItem->createExternalEditor(index, parent);
}
void ItemPinned::updateSize(const QSize &maximumSize, int idealWidth)
{
const int childItemWidth = idealWidth - m_border->width() - layout()->spacing();
m_childItem->updateSize(maximumSize, childItemWidth);
}
ItemPinnedSaver::ItemPinnedSaver(QAbstractItemModel *model, QVariantMap &settings, const ItemSaverPtr &saver)
: m_model(model)
, m_settings(settings)
, m_saver(saver)
{
connect( model, SIGNAL(rowsInserted(QModelIndex, int, int)),
SLOT(onRowsInserted(QModelIndex, int, int)) );
connect( model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
SLOT(onRowsRemoved(QModelIndex,int,int)) );
}
bool ItemPinnedSaver::saveItems(const QAbstractItemModel &model, QIODevice *file)
{
return m_saver->saveItems(model, file);
}
bool ItemPinnedSaver::canRemoveItems(const QList<QModelIndex> &indexList, QString *error)
{
const bool containsPinnedItems = std::any_of(
std::begin(indexList), std::end(indexList), isPinned);
if (!containsPinnedItems)
return m_saver->canRemoveItems(indexList, error);
if (error) {
*error = "Removing pinned items is not allowed (plugin.itempinned.unpin() items first)";
return false;
}
QMessageBox::information(
QApplication::activeWindow(),
ItemPinnedLoader::tr("Cannot Remove Pinned Items"),
ItemPinnedLoader::tr("Unpin items first to remove them.") );
return false;
}
bool ItemPinnedSaver::canMoveItems(const QList<QModelIndex> &indexList)
{
return m_saver->canMoveItems(indexList);
}
void ItemPinnedSaver::itemsRemovedByUser(const QList<QModelIndex> &indexList)
{
m_saver->itemsRemovedByUser(indexList);
}
QVariantMap ItemPinnedSaver::copyItem(const QAbstractItemModel &model, const QVariantMap &itemData)
{
return m_saver->copyItem(model, itemData);
}
void ItemPinnedSaver::onRowsInserted(const QModelIndex &, int start, int end)
{
if (!m_model)
return;
// Shift rows below inserted up.
const int rowCount = end - start + 1;
for (int row = end + 1; row < m_model->rowCount(); ++row) {
const auto index = m_model->index(row, 0);
if ( isPinned(index) )
moveRow(row, row - rowCount);
}
}
void ItemPinnedSaver::onRowsRemoved(const QModelIndex &, int start, int end)
{
if (!m_model)
return;
// Shift rows below removed down.
const int rowCount = end - start + 1;
for (int row = m_model->rowCount() - 1; row >= end; --row) {
const auto index = m_model->index(row, 0);
if ( isPinned(index) )
moveRow(row, row + rowCount + 1);
}
}
void ItemPinnedSaver::moveRow(int from, int to)
{
#if QT_VERSION < 0x050000
QMetaObject::invokeMethod(m_model, "moveRow", Q_ARG(int, from), Q_ARG(int, to));
#else
m_model->moveRow(QModelIndex(), from, QModelIndex(), to);
#endif
}
ItemPinnedLoader::ItemPinnedLoader()
{
}
ItemPinnedLoader::~ItemPinnedLoader()
{
}
QStringList ItemPinnedLoader::formatsToSave() const
{
return QStringList() << mimePinned;
}
QVariantMap ItemPinnedLoader::applySettings()
{
return m_settings;
}
QWidget *ItemPinnedLoader::createSettingsWidget(QWidget *parent)
{
ui.reset(new Ui::ItemPinnedSettings);
QWidget *w = new QWidget(parent);
ui->setupUi(w);
return w;
}
ItemWidget *ItemPinnedLoader::transform(ItemWidget *itemWidget, const QModelIndex &index)
{
const auto row = pinnedRow(index);
if (row < 0)
return nullptr;
return new ItemPinned(itemWidget, row);
}
ItemSaverPtr ItemPinnedLoader::transformSaver(const ItemSaverPtr &saver, QAbstractItemModel *model)
{
return std::make_shared<ItemPinnedSaver>(model, m_settings, saver);
}
QObject *ItemPinnedLoader::tests(const TestInterfacePtr &test) const
{
#ifdef HAS_TESTS
QObject *tests = new ItemPinnedTests(test);
return tests;
#else
Q_UNUSED(test);
return nullptr;
#endif
}
QString ItemPinnedLoader::script() const
{
return "plugins." + id() + R"SCRIPT( = {
mime: ')SCRIPT" + QString(mimePinned) + R"SCRIPT(',
_pin: function(pin, args) {
var rows = Array.prototype.slice.call(args, 0)
if (rows.length == 0)
rows = selecteditems()
for (var i in rows) {
var row = rows[i]
change(row, this.mime, pin ? str(row) : '')
}
},
isPinned: function(rows) {
var rows = Array.prototype.slice.call(arguments, 0)
if (rows.length == 0)
rows = selecteditems()
for (var i in rows) {
var pinnedRowText = str(read(this.mime, rows[i]))
pinnedRow = parseInt(pinnedRowText)
if (pinnedRow >= 0)
return true
}
return false
},
pin: function() {
this._pin(true, arguments)
},
unpin: function() {
this._pin(false, arguments)
},
})SCRIPT";
}
QList<Command> ItemPinnedLoader::commands() const
{
QList<Command> commands;
Command c;
c = dummyPinCommand();
c.name = tr("Pin");
c.matchCmd = "copyq: plugins.itempinned.isPinned() && fail()";
c.cmd = "copyq: plugins.itempinned.pin()";
commands.append(c);
c = dummyPinCommand();
c.name = tr("Unpin");
c.matchCmd = "copyq: plugins.itempinned.isPinned() || fail()";
c.cmd = "copyq: plugins.itempinned.unpin()";
commands.append(c);
return commands;
}
Q_EXPORT_PLUGIN2(itempinned, ItemPinnedLoader)