-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMappingItemDelegate.cpp
276 lines (249 loc) · 10 KB
/
MappingItemDelegate.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
/*
* MppingItemDelegate.h
*
* (c) 2016 Dame Diongue -- baydamd(@)gmail(.)com
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MappingItemDelegate.h"
namespace mmp {
MappingItemDelegate::MappingItemDelegate(QObject *parent) :
QStyledItemDelegate(parent)
{
}
void MappingItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
painter->setRenderHint(QPainter::Antialiasing);
if (index.isValid())
{
QRect rect = option.rect;
int x = rect.x();
int y = rect.y();
if (option.state & QStyle::State_Selected)
{
painter->fillRect(rect, MM::DARK_GRAY);
}
if (index.column() == MM::HideColumn)
{
bool isVisible = index.model()->data(index, Qt::CheckStateRole).toBool();
if (isVisible)
{
QStyleOptionToolButton mappingMuteButton;
mappingMuteButton.state |= QStyle::State_Enabled;
mappingMuteButton.rect = QRect(x + 4, y + 12,
MM::MAPPING_LIST_ICON_SIZE, MM::MAPPING_LIST_ICON_SIZE);
mappingMuteButton.icon = QIcon(":/visible-mapping");
mappingMuteButton.iconSize = QSize(MM::MAPPING_LIST_ICON_SIZE,
MM::MAPPING_LIST_ICON_SIZE);
QApplication::style()->drawControl(
QStyle::CE_ToolButtonLabel, &mappingMuteButton, painter);
}
}
if (index.column() == MM::IconAndNameColum)
{
// Draw Icon
QIcon mappingIcon = qvariant_cast<QIcon>(index.model()->data(index,
Qt::DecorationRole));
QPixmap iconPixmap = mappingIcon.pixmap(24, 24);
QRect iconRect(x + 5, y, 24, rect.height());
QApplication::style()->drawItemPixmap(painter, iconRect,
Qt::AlignLeft | Qt::AlignVCenter, iconPixmap);
// Draw Text
QString mappingName = index.model()->data(index, Qt::DisplayRole).toString();
QRect textRect(x + 40, y, rect.width() - 40, rect.height());
QPalette textColor = QPalette(MM::WHITE);
QApplication::style()->drawItemText(painter, textRect,
Qt::AlignLeft | Qt::AlignVCenter,
textColor, true, mappingName, QPalette::Window);
}
if (index.column() == MM::GroupButtonColum)
{
bool isSolo = index.model()->data(index, Qt::CheckStateRole + 1).toBool();
bool isLocked = index.model()->data(index, Qt::CheckStateRole + 2).toBool();
// Here is need to always align button on right side
int buttonX = rect.width() + x;
// Create all mappings buttons:
// solo button
QStyleOptionToolButton mappingSoloButton;
mappingSoloButton.state |= QStyle::State_Enabled;
mappingSoloButton.rect = QRect(buttonX - 118, y + 12,
MM::MAPPING_LIST_ICON_SIZE, MM::MAPPING_LIST_ICON_SIZE);
mappingSoloButton.icon = QIcon(":/solo-mapping");
mappingSoloButton.iconSize = QSize(MM::MAPPING_LIST_ICON_SIZE,
MM::MAPPING_LIST_ICON_SIZE);
mappingSoloButton.text = tr("Solo mapping");
//TODO: show tooltip
//mappingSoloButton.toolButtonStyle = Qt::ToolButtonTextUnderIcon;
// lock button
QStyleOptionToolButton mappingLockButton;
mappingLockButton.state |= QStyle::State_Enabled;
mappingLockButton.rect = QRect(buttonX - 88, y + 12, MM::MAPPING_LIST_ICON_SIZE,
MM::MAPPING_LIST_ICON_SIZE);
mappingLockButton.icon = QIcon(":/lock-mapping");
mappingLockButton.iconSize = QSize(MM::MAPPING_LIST_ICON_SIZE,
MM::MAPPING_LIST_ICON_SIZE);
mappingLockButton.text = tr("Lock mapping");
//TODO: show tooltip
//mappingLockButton.toolButtonStyle = Qt::ToolButtonTextUnderIcon;
// duplicate button
QStyleOptionToolButton mappingDuplicateButton;
mappingDuplicateButton.state |= QStyle::State_Enabled;
mappingDuplicateButton.rect = QRect(buttonX - 58, y + 12,
MM::MAPPING_LIST_ICON_SIZE, MM::MAPPING_LIST_ICON_SIZE);
mappingDuplicateButton.icon = QIcon(":/duplicate-mapping");
mappingDuplicateButton.iconSize = QSize(MM::MAPPING_LIST_ICON_SIZE,
MM::MAPPING_LIST_ICON_SIZE);
mappingDuplicateButton.text = tr("Duplicate mapping");
//TODO: show tooltip
//mappingDuplicateButton.toolButtonStyle = Qt::ToolButtonTextUnderIcon;
// delete button
QStyleOptionToolButton mappingDeleteButton;
mappingDeleteButton.state |= QStyle::State_Enabled;
mappingDeleteButton.rect = QRect(buttonX - 28, y + 12,
MM::MAPPING_LIST_ICON_SIZE, MM::MAPPING_LIST_ICON_SIZE);
mappingDeleteButton.icon = QIcon(":/delete-mapping");
mappingDeleteButton.iconSize = QSize(MM::MAPPING_LIST_ICON_SIZE,
MM::MAPPING_LIST_ICON_SIZE);
mappingDeleteButton.text = tr("Delete mapping");
//TODO: show tooltip
//mappingDeleteButton.toolButtonStyle = Qt::ToolButtonTextUnderIcon;
if (isSolo)
{
QPainterPath mappingSoloPanel;
mappingSoloPanel.addRoundedRect(mappingSoloButton.rect.adjusted(
-2, -2, 2, 2), 4, 4);
painter->setPen(QPen()); // No pen
painter->fillPath(mappingSoloPanel, MM::DARK_BLUE);
painter->drawPath(mappingSoloPanel);
}
QApplication::style()->drawControl( QStyle::CE_ToolButtonLabel,
&mappingSoloButton, painter);
if (isLocked)
{
QPainterPath mappingLockPanel;
mappingLockPanel.addRoundedRect(QRectF(mappingLockButton.rect.adjusted(
-2, -2, 2, 2)), 4, 4);
painter->setPen(QPen()); // No pen
painter->fillPath(mappingLockPanel, MM::DARK_BLUE);
painter->drawPath(mappingLockPanel);
}
QApplication::style()->drawControl(
QStyle::CE_ToolButtonLabel, &mappingLockButton, painter);
QApplication::style()->drawControl(
QStyle::CE_ToolButtonLabel, &mappingDuplicateButton, painter);
QApplication::style()->drawControl(
QStyle::CE_ToolButtonLabel, &mappingDeleteButton, painter);
}
}
else
{
QStyledItemDelegate::paint(painter, option, index);
}
}
QWidget *MappingItemDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.column() == MM::IconAndNameColum)
{
QLineEdit *editor = new QLineEdit(parent);
editor->setFixedHeight(option.rect.height());
editor->setContentsMargins(option.rect.x() + 4, 0, 0, 0);
return editor;
}
else
{
return 0;
}
}
void MappingItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString value = index.model()->data(index, Qt::EditRole).toString();
QLineEdit *nameEdit = static_cast<QLineEdit*>(editor); // TODO: use reinterpret_cast instead static_cast
nameEdit->setText(value);
}
void MappingItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QLineEdit *nameEdit = static_cast<QLineEdit*>(editor); // TODO: use reinterpret_cast instead static_cast
model->setData(index, nameEdit->text(), Qt::EditRole);
}
void MappingItemDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(index);
editor->setGeometry(option.rect);
}
bool MappingItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem &option, const QModelIndex &index)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
QRect rect = option.rect;
int x = rect.x();
int y = rect.y();
int buttonX = rect.width() + x;
QRect hideButtonRect = QRect(x + 4, y + 12, MM::MAPPING_LIST_ICON_SIZE,
MM::MAPPING_LIST_ICON_SIZE);
QRect soloButtonRect = QRect(buttonX - 118, y + 12, MM::MAPPING_LIST_ICON_SIZE,
MM::MAPPING_LIST_ICON_SIZE);
QRect lockButtonRect = QRect(buttonX - 88, y + 12, MM::MAPPING_LIST_ICON_SIZE,
MM::MAPPING_LIST_ICON_SIZE);
QRect duplicateButtonRect = QRect(buttonX - 58, y + 12, MM::MAPPING_LIST_ICON_SIZE,
MM::MAPPING_LIST_ICON_SIZE);
QRect deleteButtonRect = QRect(buttonX - 28, y + 12, MM::MAPPING_LIST_ICON_SIZE,
MM::MAPPING_LIST_ICON_SIZE);
if (event->type() == QEvent::MouseMove)
{
// TODO: handle tooltip
}
else if (event->type() == QEvent::MouseButtonPress)
{
if (mouseEvent->buttons() & Qt::LeftButton && index.isValid())
{
if (index.column() == MM::HideColumn)
{
if (hideButtonRect.contains(mouseEvent->pos()))
{
model->setData(index, !(index.data(Qt::CheckStateRole).toBool()),
Qt::CheckStateRole);
}
}
if (index.column() == MM::GroupButtonColum)
{
if (soloButtonRect.contains(mouseEvent->pos()))
{
model->setData(index, !(index.data(Qt::CheckStateRole + 1).toBool()),
Qt::CheckStateRole + 1);
}
else if (lockButtonRect.contains(mouseEvent->pos()))
{
model->setData(index, !(index.data(Qt::CheckStateRole + 2).toBool()),
Qt::CheckStateRole + 2);
}
else if (duplicateButtonRect.contains(mouseEvent->pos()))
{
emit itemDuplicated(index.data(Qt::UserRole).toInt());
}
else if (deleteButtonRect.contains(mouseEvent->pos()))
{
emit itemRemoved(index.data(Qt::UserRole).toInt());
}
}
}
else if (mouseEvent->buttons() & Qt::RightButton && index.isValid())
{
emit itemContextMenuRequested(mouseEvent->pos());
}
}
return QAbstractItemDelegate::editorEvent(event, model, option, index);
}
}