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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2009-05-26
* Description : History view.
*
* SPDX-FileCopyrightText: 2009-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "dhistoryview.h"
// Qt includes
#include <QHeaderView>
#include <QPixmap>
#include <QStringList>
#include <QMouseEvent>
#include <QMimeData>
#include <QClipboard>
#include <QTime>
#include <QApplication>
#include <QMenu>
#include <QAction>
#include <QIcon>
// KDE includes
#include <klocalizedstring.h>
namespace Digikam
{
class Q_DECL_HIDDEN DHistoryViewItem : public QTreeWidgetItem
{
public:
DHistoryViewItem(QTreeWidget* const parent, const QString& msg, DHistoryView::EntryType type, const QVariant& metadata)
: QTreeWidgetItem(parent, QStringList()),
m_metadata (metadata)
{
switch (type)
{
case DHistoryView::StartingEntry:
{
setIcon(0, QIcon::fromTheme(QLatin1String("system-run")));
break;
}
case DHistoryView::SuccessEntry:
{
setIcon(0, QIcon::fromTheme(QLatin1String("dialog-ok-apply")));
break;
}
case DHistoryView::WarningEntry:
{
setIcon(0, QIcon::fromTheme(QLatin1String("dialog-warning")));
setForeground(2, QBrush(QColor(Qt::darkYellow)));
break;
}
case DHistoryView::ErrorEntry:
{
setIcon(0, QIcon::fromTheme(QLatin1String("dialog-error")));
setForeground(2, QBrush(QColor(Qt::red)));
break;
}
case DHistoryView::ProgressEntry:
{
setIcon(0, QIcon::fromTheme(QLatin1String("dialog-information")));
break;
}
case DHistoryView::CancelEntry:
{
setIcon(0, QIcon::fromTheme(QLatin1String("dialog-cancel")));
setForeground(2, QBrush(QColor(Qt::darkBlue)));
break;
}
default:
{
setIcon(0, QIcon::fromTheme(QLatin1String("dialog-information")));
break;
}
}
setText(1, QTime::currentTime().toString(Qt::ISODate));
setText(2, msg);
}
QVariant metadata() const
{
return m_metadata;
}
private:
QVariant m_metadata;
private:
Q_DISABLE_COPY(DHistoryViewItem)
};
// ---------------------------------------------------------------------------
DHistoryView::DHistoryView(QWidget* const parent)
: QTreeWidget(parent)
{
qRegisterMetaType<EntryType>("DHistoryView::EntryType");
setContextMenuPolicy(Qt::CustomContextMenu);
setIconSize(QSize(22, 22));
setSelectionMode(QAbstractItemView::SingleSelection);
setSortingEnabled(false);
setAllColumnsShowFocus(true);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setColumnCount(3);
setHeaderHidden(true);
setRootIsDecorated(false);
setUniformRowHeights(true);
setDragEnabled(true);
viewport()->setMouseTracking(true);
header()->setSectionResizeMode(0, QHeaderView::ResizeToContents); // Icon
header()->setSectionResizeMode(1, QHeaderView::ResizeToContents); // Time
header()->setSectionResizeMode(2, QHeaderView::Stretch); // Message
connect(this, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
this, SLOT(slotItemDoubleClicked(QTreeWidgetItem*)));
connect(this, SIGNAL(customContextMenuRequested(QPoint)),
this, SLOT(slotContextMenu()));
}
void DHistoryView::slotContextMenu()
{
QMenu popmenu(this);
QAction* const action = new QAction(QIcon::fromTheme(QLatin1String("edit-copy")), i18n("Copy to Clipboard"), this);
connect(action, SIGNAL(triggered(bool)),
this, SLOT(slotCopy2ClipBoard()));
popmenu.addAction(action);
popmenu.exec(QCursor::pos());
}
void DHistoryView::slotCopy2ClipBoard()
{
QString textInfo;
QTreeWidgetItemIterator it(this);
while (*it)
{
textInfo.append((*it)->text(1));
textInfo.append(QLatin1String(" :: "));
textInfo.append((*it)->text(2));
textInfo.append(QLatin1Char('\n'));
++it;
}
QMimeData* const mimeData = new QMimeData();
mimeData->setText(textInfo);
QApplication::clipboard()->setMimeData(mimeData, QClipboard::Clipboard);
}
void DHistoryView::addEntry(const QString& msg, EntryType type, const QVariant& metadata)
{
DHistoryViewItem* const item = new DHistoryViewItem(this, msg, type, metadata);
// Dispatch events to Qt loop in case of bombarding of messages. See bug #338629
qApp->processEvents();
if (item)
{
setCurrentItem(item);
}
}
void DHistoryView::slotItemDoubleClicked(QTreeWidgetItem* item)
{
DHistoryViewItem* const lvi = dynamic_cast<DHistoryViewItem*>(item);<--- Variable 'lvi' can be declared as pointer to const
if (lvi)
{
if (!lvi->metadata().isNull())
{
Q_EMIT signalEntryClicked(lvi->metadata());
}
}
}
void DHistoryView::mouseMoveEvent(QMouseEvent* e)
{
DHistoryViewItem* const lvi = dynamic_cast<DHistoryViewItem*>(itemAt(e->pos()));<--- Variable 'lvi' can be declared as pointer to const
if (lvi)
{
if (!lvi->metadata().isNull())
{
setCursor(Qt::PointingHandCursor);
}
else
{
unsetCursor();
}
}
else
{
unsetCursor();
}
QTreeWidget::mouseMoveEvent(e);
}
} // namespace Digikam
#include "moc_dhistoryview.cpp"
|