forked from mercutiodesign/texmaker-3.3.3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icondelegate.cpp
322 lines (280 loc) · 11.7 KB
/
icondelegate.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
/***************************************************************************
* copyright : (C) 2003-2009 by Pascal Brachet *
* http://www.xm1math.net/texmaker/ *
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <QtGui>
#include <QAbstractItemModel>
#include <QPainter>
#include "icondelegate.h"
static const int textMargin = 2;
void IconDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const {
Q_ASSERT(index.isValid());
const QAbstractItemModel *model = index.model();
Q_ASSERT(model);
QStyleOptionViewItem opt = option;
// set font
QVariant value = model->data(index, Qt::FontRole);
if (value.isValid())
opt.font = qvariant_cast<QFont>(value);
// set text alignment
value = model->data(index, Qt::TextAlignmentRole);
if (value.isValid())
opt.displayAlignment = QFlag(value.toInt());
// set text color
value = model->data(index, Qt::TextColorRole);
if (value.isValid() && qvariant_cast<QColor>(value).isValid())
opt.palette.setColor(QPalette::Text, qvariant_cast<QColor>(value));
// do layout
value = model->data(index, Qt::DecorationRole);
QPixmap pixmap = decoration(opt, value);
QRect pixmapRect = pixmap.rect();
QFontMetrics fontMetrics(opt.font);
QString text = model->data(index, Qt::DisplayRole).toString();
QRect textRect(0, 0, 0,0);
//QRect textRect(0, 0, fontMetrics.width(text), fontMetrics.lineSpacing());
value = model->data(index, Qt::CheckStateRole);
QRect checkRect = check(opt, opt.rect, value);
Qt::CheckState checkState = static_cast<Qt::CheckState>(value.toInt());
doLayout(opt, &checkRect, &pixmapRect, &textRect, false);
// draw the background color
if (option.showDecorationSelected && (option.state & QStyle::State_Selected)) {
QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
? QPalette::Normal : QPalette::Disabled;
painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
//painter->fillRect(option.rect,QColor("#78A9dc"));
painter->fillRect(option.rect,QColor("#cdd2d8"));
} else {
value = model->data(index, Qt::BackgroundColorRole);
if (value.isValid() && qvariant_cast<QColor>(value).isValid())
painter->fillRect(option.rect, qvariant_cast<QColor>(value));
}
// draw the item
// drawCheck(painter, opt, checkRect, checkState);
drawDecoration(painter, opt, pixmapRect, pixmap);
// drawDisplay(painter, opt, textRect, text);
// drawFocus(painter, opt, textRect);
}
QSize IconDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const {
Q_ASSERT(index.isValid());
const QAbstractItemModel *model = index.model();
Q_ASSERT(model);
QVariant value = model->data(index, Qt::FontRole);
QFont fnt = value.isValid() ? qvariant_cast<QFont>(value) : option.font;
QString text = model->data(index, Qt::DisplayRole).toString();
QRect pixmapRect;
if (model->data(index, Qt::DecorationRole).isValid())
pixmapRect = QRect(0, 0, option.decorationSize.width(),
option.decorationSize.height());
QFontMetrics fontMetrics(fnt);
QRect textRect(0, 0, 0,0);
//QRect textRect(0, 0, fontMetrics.width(text), fontMetrics.lineSpacing());
QRect checkRect = check(option, textRect, model->data(index, Qt::CheckStateRole));
doLayout(option, &checkRect, &pixmapRect, &textRect, true);
return (pixmapRect|textRect|checkRect).size();
}
/*!
Renders the decoration \a pixmap within the rectangle specified by
\a rect using the given \a painter and style \a option.
*/
void IconDelegate::drawDecoration(QPainter *painter, const QStyleOptionViewItem &option,
const QRect &rect, const QPixmap &pixmap) const {
if (!pixmap.isNull() && !rect.isEmpty()) {
if (option.state & QStyle::State_Selected) {
bool enabled = option.state & QStyle::State_Enabled;
QPixmap *pm = selected(pixmap, option.palette, enabled);
painter->drawPixmap(rect.topLeft(), *pm);
} else {
painter->drawPixmap(rect.topLeft(), pixmap);
}
}
}
/*!
Renders the region within the rectangle specified by \a rect, indicating
that it has the focus, using the given \a painter and style \a option.
*/
void IconDelegate::drawFocus(QPainter *painter,
const QStyleOptionViewItem &option,
const QRect &rect) const {
if (option.state & QStyle::State_HasFocus) {
QStyleOptionFocusRect o;
o.QStyleOption::operator=(option);
o.rect = rect;
QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled)
? QPalette::Normal : QPalette::Disabled;
o.backgroundColor = option.palette.color(cg, (option.state & QStyle::State_Selected)
? QPalette::Highlight : QPalette::Background);
QApplication::style()->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter);
}
}
/*!
Renders a check indicator within the rectangle specified by \a
rect, using the given \a painter and style \a option, using the
given \a state.
*/
void IconDelegate::drawCheck(QPainter *painter,
const QStyleOptionViewItem &option,
const QRect &rect, Qt::CheckState state) const {
if (!rect.isValid())
return;
QStyleOptionViewItem opt;
opt.QStyleOption::operator=(option);
opt.rect = rect;
opt.state = opt.state & ~QStyle::State_HasFocus;
switch (state) {
case Qt::Unchecked:
opt.state |= QStyle::State_Off;
break;
case Qt::PartiallyChecked:
opt.state |= QStyle::State_NoChange;
break;
case Qt::Checked:
opt.state |= QStyle::State_On;
break;
}
QApplication::style()->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &opt, painter);
}
void IconDelegate::doLayout(const QStyleOptionViewItem &option,
QRect *checkRect, QRect *pixmapRect, QRect *textRect,
bool hint) const {
Q_ASSERT(checkRect && pixmapRect && textRect);
int x = option.rect.left();
int y = option.rect.top();
int w, h;
textRect->adjust(-textMargin, 0, textMargin, 0); // add width padding
QSize pm(0, 0);
if (pixmapRect->isValid())
pm = option.decorationSize;
if (hint) {
w = qMax(textRect->width(), pm.width());
h = qMax(textRect->height(), pm.height());
} else {
w = option.rect.width();
h = option.rect.height();
}
int cw = 0;
QRect check;
if (checkRect->isValid()) {
check.setRect(x, y, checkRect->width() + textMargin * 2, h);
cw = check.width();
if (option.direction == Qt::LeftToRight)
x += cw;
}
QRect display;
QRect decoration;
QStyleOptionViewItem::Position position = option.decorationPosition;
if (option.direction == Qt::RightToLeft) {
if (position == QStyleOptionViewItem::Right)
position = QStyleOptionViewItem::Left;
else if (position == QStyleOptionViewItem::Left)
position = QStyleOptionViewItem::Right;
}
switch (position) {
case QStyleOptionViewItem::Top: {
if (!pm.isEmpty())
pm.setHeight(pm.height() + textMargin); // add space
decoration.setRect(x, y, w, pm.height());
h = hint ? textRect->height() : h - pm.height();
display.setRect(x, y + pm.height(), w, h);
break;
}
case QStyleOptionViewItem::Bottom: {
if (!textRect->isEmpty())
textRect->setHeight(textRect->height() + textMargin); // add space
h = hint ? textRect->height() + pm.height() : h;
decoration.setRect(x, y + h - pm.height(), w, pm.height());
h = hint ? textRect->height() : h - pm.height();
display.setRect(x, y, w, h);
break;
}
case QStyleOptionViewItem::Left: {
if (!pm.isEmpty())
pm.setWidth(pm.width() + textMargin); // add space
decoration.setRect(x, y, pm.width(), h);
w = hint ? textRect->width() : w - pm.width() - cw;
display.setRect(x + pm.width(), y, w, h);
break;
}
case QStyleOptionViewItem::Right: {
if (!textRect->isEmpty())
textRect->setWidth(textRect->width() + textMargin); // add space
w = hint ? textRect->width() + pm.width() : w;
decoration.setRect(x + w - pm.width() - cw, y, pm.width(), h);
w = hint ? textRect->width() : w - pm.width() - cw;
display.setRect(x, y, w, h);
break;
}
default:
qWarning("doLayout: decoration positon is invalid");
decoration = *pixmapRect;
break;
}
if (!hint) { // we only need to do the internal layout if we are going to paint
*checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
checkRect->size(), check);
*pixmapRect = QStyle::alignedRect(option.direction, option.decorationAlignment,
pixmapRect->size(), decoration);
} else {
*checkRect = check;
*pixmapRect = decoration;
}
*textRect = display;
}
QPixmap IconDelegate::decoration(const QStyleOptionViewItem &option, const QVariant &variant) const {
switch (variant.type()) {
case QVariant::Icon:
return qvariant_cast<QIcon>(variant).pixmap(option.decorationSize,
option.state & QStyle::State_Enabled
? QIcon::Normal : QIcon::Disabled,
option.state & QStyle::State_Open
? QIcon::On : QIcon::Off);
case QVariant::Color: {
static QPixmap pixmap(20, 20);
pixmap.fill(qvariant_cast<QColor>(variant));
return pixmap;
}
default:
break;
}
return qvariant_cast<QPixmap>(variant);
}
/*!
\internal
*/
QPixmap *IconDelegate::selected(const QPixmap &pixmap, const QPalette &palette, bool enabled) const {
QString key;
key.sprintf("%d-%d", pixmap.serialNumber(), enabled);
QPixmap *pm = QPixmapCache::find(key);
if (!pm) {
QImage img = pixmap.toImage().convertToFormat(QImage::Format_ARGB32_Premultiplied);
QColor color = palette.color(enabled ? QPalette::Normal : QPalette::Disabled,QPalette::Highlight);
color.setAlphaF(0.3);
QPainter painter(&img);
painter.setCompositionMode(QPainter::CompositionMode_SourceAtop);
painter.fillRect(0, 0, img.width(), img.height(), color);
painter.end();
pm = new QPixmap(QPixmap::fromImage(img));
QPixmapCache::insert(key, *pm);
}
return pm;
}
/*!
\internal
*/
QRect IconDelegate::check(const QStyleOptionViewItem &option,
const QRect &bounding, const QVariant &value) const {
if (value.isValid()) {
QStyleOptionButton opt;
opt.QStyleOption::operator=(option);
opt.rect = bounding;
return QApplication::style()->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt);
}
return QRect();
}