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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2012-12-18
* Description : Customized Workflow Settings list.
*
* SPDX-FileCopyrightText: 2012-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "workflowlist.h"
// Qt includes
#include <QApplication>
#include <QDrag>
#include <QHeaderView>
#include <QMimeData>
#include <QPainter>
#include <QMenu>
#include <QAction>
#include <QIcon>
#include <QMessageBox>
// KDE includes
#include <klocalizedstring.h>
// Local includes
#include "workflowmanager.h"
#include "workflowdlg.h"
#include "dmessagebox.h"
namespace Digikam
{
WorkflowItem::WorkflowItem(WorkflowList* const parent, const QString& title)
: QTreeWidgetItem(parent)
{
setDisabled(false);
setSelected(false);
Workflow q = WorkflowManager::instance()->findByTitle(title);
setIcon(0, QIcon::fromTheme(QLatin1String("step")));
setItem(title, q.desc, q.aTools.count());
}
QString WorkflowItem::title() const
{
return text(0);
}
int WorkflowItem::count() const
{
return text(1).toInt();
}
void WorkflowItem::setItem(const QString& title, const QString& desc, int count)
{
if (!title.isEmpty())
{
setText(0, title);
}
if (!desc.isEmpty())
{
setText(2, desc);
}
if (count > 0)
{
setText(1, QString::number(count));
}
}
// ---------------------------------------------------------------------------
WorkflowList::WorkflowList(QWidget* const parent)
: QTreeWidget(parent)
{
setContextMenuPolicy(Qt::CustomContextMenu);
setIconSize(QSize(22, 22));
setSelectionMode(QAbstractItemView::SingleSelection);
setSortingEnabled(true);
setRootIsDecorated(false);
setUniformRowHeights(true);
setAllColumnsShowFocus(true);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setColumnCount(3);
setHeaderHidden(false);
setDragEnabled(true);
QStringList titles;
titles.append(i18nc("@title: batch workflow name", "Title"));
titles.append(i18nc("@title: batch workflow tools list", "Tools"));
titles.append(i18nc("@title: batch workflow description", "Description"));
setHeaderLabels(titles);
header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
header()->setSectionResizeMode(2, QHeaderView::Stretch);
connect(this, SIGNAL(customContextMenuRequested(QPoint)),
this, SLOT(slotContextMenu()));
WorkflowManager* const mngr = WorkflowManager::instance();
QStringList failed;
mngr->load(failed);
const auto qs = mngr->queueSettingsList();
for (const Workflow& q : qs)
{
slotAddQueueSettings(q.title);
}
if (!failed.isEmpty())
{
DMessageBox::showInformationList(QMessageBox::Information,
qApp->activeWindow(),
i18nc("@title:window", "Batch Queue Manager"),
i18nc("@info", "Some Workflows cannot be loaded from your config file due to an incompatible "
"version of a tool."),
failed);
}
}
void WorkflowList::slotAddQueueSettings(const QString& title)
{
WorkflowItem* const item = findByTitle(title);<--- Variable 'item' can be declared as pointer to const
if (!item)
{
new WorkflowItem(this, title);
}
}
void WorkflowList::slotRemoveQueueSettings(const QString& title)
{
WorkflowItem* const item = findByTitle(title);
if (item)
{
delete item;
}
}
WorkflowItem* WorkflowList::findByTitle(const QString& title)
{
QTreeWidgetItemIterator it(this);
while (*it)
{
WorkflowItem* const item = dynamic_cast<WorkflowItem*>(*it);
if (item && (item->title() == title))
{
return item;
}
++it;
}
return nullptr;
}
void WorkflowList::startDrag(Qt::DropActions /*supportedActions*/)
{
QList<QTreeWidgetItem*> list = selectedItems();
if (!list.isEmpty())
{
WorkflowItem* const item = dynamic_cast<WorkflowItem*>(list.first());<--- Variable 'item' can be declared as pointer to const
if (!item)
{
return;
}
QPixmap icon(QIcon::fromTheme(QLatin1String("step")).pixmap(48));
int w = icon.width();
int h = icon.height();
QPixmap pix(w + 4, h + 4);
QString text(QString::number(item->count()));
QPainter p(&pix);
p.fillRect(0, 0, pix.width() - 1, pix.height() - 1, QColor(Qt::white));
p.setPen(QPen(Qt::black, 1));
p.drawRect(0, 0, pix.width() - 1, pix.height() - 1);
p.drawPixmap(2, 2, icon);
QRect r = p.boundingRect(2, 2, w, h, Qt::AlignLeft | Qt::AlignTop, text);
r.setWidth(qMax(r.width(), r.height()));
r.setHeight(qMax(r.width(), r.height()));
p.fillRect(r, QColor(0, 80, 0));
p.setPen(Qt::white);
QFont f(font());
f.setBold(true);
p.setFont(f);
p.drawText(r, Qt::AlignCenter, text);
p.end();
QDrag* const drag = new QDrag(this);
drag->setMimeData(mimeData(list));
drag->setPixmap(pix);
drag->exec();
if (drag->target())
{
m_lastAssignedTitle = item->title();
}
}
}
QStringList WorkflowList::mimeTypes() const
{
return QStringList() << QLatin1String("digikam/workflow");
}
void WorkflowList::mouseDoubleClickEvent(QMouseEvent*)
{
if (viewport()->isEnabled())
{
slotAssignQueueSettings();
}
}
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
QMimeData* WorkflowList::mimeData(const QList<QTreeWidgetItem*>& items) const
#else
// cppcheck-suppress passedByValue
QMimeData* WorkflowList::mimeData(const QList<QTreeWidgetItem*> items) const // clazy:exclude=function-args-by-ref
#endif
{
QMimeData* const mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
if (!items.isEmpty())
{
WorkflowItem* const item = dynamic_cast<WorkflowItem*>(items.first());<--- Variable 'item' can be declared as pointer to const
if (item)
{
stream << item->title();
}
}
mimeData->setData(QLatin1String("digikam/workflow"), encodedData);
return mimeData;
}
void WorkflowList::slotContextMenu()
{
QList<QTreeWidgetItem*> list = selectedItems();
if (list.isEmpty())
{
return;
}
WorkflowItem* const item = dynamic_cast<WorkflowItem*>(list.first());
WorkflowManager* const mngr = WorkflowManager::instance();
if (!item)
{
return;
}
QMenu popmenu(this);
QAction* const assignAction = new QAction(QIcon::fromTheme(QLatin1String("list-add")), i18nc("@action", "Assign Workflow to current queue"), this);
QAction* const propAction = new QAction(QIcon::fromTheme(QLatin1String("configure")), i18nc("@action", "Edit Workflow"), this);
QAction* const updAction = new QAction(QIcon::fromTheme(QLatin1String("view-refresh")), i18nc("@action", "Update Workflow"), this);
QAction* const delAction = new QAction(QIcon::fromTheme(QLatin1String("edit-delete")), i18nc("@action", "Delete Workflow"), this);
popmenu.addAction(assignAction);
popmenu.addAction(propAction);
popmenu.addAction(updAction);
if (m_lastAssignedTitle != item->title())
{
updAction->setDisabled(true);
}
popmenu.addSeparator();
popmenu.addAction(delAction);
QAction* const choice = popmenu.exec(QCursor::pos());<--- Variable 'choice' can be declared as pointer to const
if (choice == assignAction)
{
slotAssignQueueSettings();
}
else if (choice == propAction)
{
Workflow wfOld = mngr->findByTitle(item->title());
Workflow wfNew = wfOld;
if (WorkflowDlg::editProps(wfNew))
{
mngr->remove(wfOld);
mngr->insert(wfNew);
mngr->save();
if (wfOld.title != wfNew.title)
{
removeItemWidget(item, 0);
delete item;
}
else
{
item->setItem(QString(), wfNew.desc);
}
}
}
else if (choice == updAction)
{
Q_EMIT signalUpdateQueueSettings(item->title());
Workflow q = WorkflowManager::instance()->findByTitle(item->title());
item->setItem(QString(), QString(), q.aTools.count());
}
else if (choice == delAction)
{
int result = QMessageBox::warning(qApp->activeWindow(),
i18nc("@title:window", "Delete Workflow?"),
i18nc("@info", "Are you sure you want to "
"delete the selected workflow "
"\"%1\"?", item->title()),
QMessageBox::Yes | QMessageBox::Cancel);
if (result == QMessageBox::Yes)
{
Workflow wf = mngr->findByTitle(item->title());
mngr->remove(wf);
mngr->save();
removeItemWidget(item, 0);
delete item;
}
}
}
void WorkflowList::slotAssignQueueSettings()
{
QList<QTreeWidgetItem*> list = selectedItems();
if (!list.isEmpty())
{
WorkflowItem* const item = dynamic_cast<WorkflowItem*>(list.first());<--- Variable 'item' can be declared as pointer to const
if (item)
{
Q_EMIT signalAssignQueueSettings(item->title());
m_lastAssignedTitle = item->title();
}
}
}
} // namespace Digikam
#include "moc_workflowlist.cpp"
|