-
Notifications
You must be signed in to change notification settings - Fork 105
/
item_delegates.cpp
266 lines (213 loc) · 7.81 KB
/
item_delegates.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
/*
* Copyright 2013 Kai Pastor
*
* This file is part of OpenOrienteering.
*
* OpenOrienteering 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.
*
* OpenOrienteering 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 OpenOrienteering. If not, see <http://www.gnu.org/licenses/>.
*/
#include "item_delegates.h"
#include <QAbstractTextDocumentLayout>
#include <QApplication>
#include <QPainter>
#include <QSignalMapper>
#include <QSpinBox>
#include <QTextDocument>
#include "util/backports.h" // IWYU pragma: keep
#include "gui/util_gui.h"
namespace OpenOrienteering {
// ### ColorItemDelegate ###
ColorItemDelegate::ColorItemDelegate(QObject* parent)
: QStyledItemDelegate(parent)
{
// Nothing.
}
void ColorItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
if (!option.state.testFlag(QStyle::State_Selected))
{
// Use the style options as is.
QStyledItemDelegate::paint(painter, option, index);
}
else
{
// Use the model's background and/or foreground.
QStyleOptionViewItem background_option(option);
QPalette& palette(background_option.palette);
QVariant background_var(index.data(Qt::BackgroundRole));
if (!background_var.isNull())
{
palette.setColor(QPalette::Highlight, background_var.value<QColor>());
}
QVariant foreground_var(index.data(Qt::ForegroundRole));
if (!foreground_var.isNull())
{
palette.setColor(QPalette::HighlightedText, foreground_var.value<QColor>());
}
QStyledItemDelegate::paint(painter, background_option, index);
// Draw an extra frame.
painter->save();
painter->setPen(QPen(option.palette.color(QPalette::Highlight), 1));
painter->drawRect(option.rect.adjusted(1, 1, -2, -2));
painter->restore();
}
}
//### SpinBoxDelegate ###
SpinBoxDelegate::SpinBoxDelegate(QObject* parent, int min, int max, const QString& unit, int step)
: QItemDelegate(parent),
min(min),
max(max),
step(step),
unit(unit)
{
// nothing
}
SpinBoxDelegate::SpinBoxDelegate(int min, int max, const QString& unit, int step)
: QItemDelegate(),
min(min),
max(max),
step(step),
unit(unit)
{
// nothing
}
QWidget* SpinBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
Q_UNUSED(option);
Q_UNUSED(index);
QSpinBox* spinbox = Util::SpinBox::create(min, max, unit, step);
spinbox->setParent(parent);
// Commit each change immediately when returning to event loop
QSignalMapper* signal_mapper = new QSignalMapper(spinbox);
signal_mapper->setMapping(spinbox, spinbox);
connect(spinbox, QOverload<int>::of(&QSpinBox::valueChanged), signal_mapper, QOverload<>::of(&QSignalMapper::map), Qt::QueuedConnection);
connect(signal_mapper, QOverload<QWidget*>::of(&QSignalMapper::mapped), this, &QItemDelegate::commitData);
return spinbox;
}
void SpinBoxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
int value = index.model()->data(index, Qt::UserRole).toInt();
static_cast< QSpinBox* >(editor)->setValue(value);
}
void SpinBoxDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
QSpinBox* spinBox = static_cast< QSpinBox* >(editor);
spinBox->interpretText();
setModelData(model, index, spinBox->value());
}
void SpinBoxDelegate::setModelData(QAbstractItemModel* model, const QModelIndex& index, int value) const
{
QMap< int, QVariant > data(model->itemData(index));
data[Qt::UserRole] = value;
data[Qt::DisplayRole] = unit.isEmpty() ?
QLocale().toString(value) :
(QLocale().toString(value) + QLatin1Char(' ') + unit);
model->setItemData(index, data);
}
void SpinBoxDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
Q_UNUSED(index);
editor->setGeometry(option.rect);
}
//### PercentageDelegate ###
PercentageDelegate::PercentageDelegate(QObject* parent, int step)
: QStyledItemDelegate(parent),
step(step)
{
unit = QLatin1Char(' ') + tr("%");
}
PercentageDelegate::PercentageDelegate(int step)
: QStyledItemDelegate(),
step(step)
{
unit = QLatin1Char(' ') + tr("%");
}
QString PercentageDelegate::displayText(const QVariant& value, const QLocale& locale) const
{
int int_value = qRound(value.toFloat() * 100);
return locale.toString(int_value) + unit;
}
QWidget* PercentageDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
Q_UNUSED(option);
Q_UNUSED(index);
QSpinBox* spinbox = Util::SpinBox::create(0, 100, unit, step);
spinbox->setParent(parent);
// Commit each change immediately when returning to event loop
QSignalMapper* signal_mapper = new QSignalMapper(spinbox);
signal_mapper->setMapping(spinbox, spinbox);
connect(spinbox, QOverload<int>::of(&QSpinBox::valueChanged), signal_mapper, QOverload<>::of(&QSignalMapper::map), Qt::QueuedConnection);
connect(signal_mapper, QOverload<QWidget*>::of(&QSignalMapper::mapped), this, &QItemDelegate::commitData);
return spinbox;
}
void PercentageDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
int value = qRound(index.model()->data(index, Qt::DisplayRole).toFloat() * 100);
static_cast< QSpinBox* >(editor)->setValue(value);
}
void PercentageDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
QSpinBox* spinBox = static_cast< QSpinBox* >(editor);
spinBox->interpretText();
QMap< int, QVariant > data(model->itemData(index));
data[Qt::DisplayRole] = (float)spinBox->value() / 100.0;
model->setItemData(index, data);
}
void PercentageDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
Q_UNUSED(index);
editor->setGeometry(option.rect);
}
// ### TextDocItemDelegate ###
TextDocItemDelegate::TextDocItemDelegate(QObject* parent, const Provider* provider)
: QStyledItemDelegate(parent)
, provider(provider)
{
Q_ASSERT(provider);
}
TextDocItemDelegate::~TextDocItemDelegate()
{
; // nothing
}
void TextDocItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
const QTextDocument* const text_doc = provider->textDoc(index);
if (!text_doc)
{
QStyledItemDelegate::paint(painter, option, index);
return;
}
QStyleOptionViewItem options = option;
initStyleOption(&options, index);
QAbstractTextDocumentLayout::PaintContext context;
if (options.state.testFlag(QStyle::State_Selected))
context.palette.setColor(QPalette::Text, options.palette.color(QPalette::Active, QPalette::HighlightedText));
else
context.palette.setColor(QPalette::Text, options.palette.color(QPalette::Active, QPalette::Text));
painter->save();
const QStyle* const style = options.widget ? options.widget->style() : QApplication::style();
style->drawControl(QStyle::CE_ItemViewItem, &options, painter);
QRect rect = style->subElementRect(QStyle::SE_ItemViewItemText, &options);
painter->translate(rect.topLeft());
rect.moveTopLeft(QPoint());
text_doc->documentLayout()->draw(painter, context);
painter->restore();
}
QSize TextDocItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
const QTextDocument* const text_doc = provider->textDoc(index);
if (!text_doc)
return QStyledItemDelegate::sizeHint(option, index);
return QSize(text_doc->idealWidth(), text_doc->size().height());
}
} // namespace OpenOrienteering