buttondelegate.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXCore::core
19 * @author Cedric Seehausen (usdnr at kit dot edu)
20 * @date 2016
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
25
26#include "buttondelegate.h"
27
28#include <QApplication>
29#include <QModelIndex>
30#include <QMouseEvent>
31#include <QObject>
32#include <QPainter>
33#include <QStyleOptionViewItem>
34
38
40#include "scenarioitem.h"
41#include "treeitem.h"
42
44 QStyledItemDelegate(parent),
45 startPixmap(":/icons/media-playback-start.ico"),
46 stopPixmap(":/icons/process-stop-7.ico"),
47 restartPixmap(":/icons/view-refresh-7.png")
48{
49}
50
51void
52ButtonDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
53{
54 QStyledItemDelegate::setEditorData(editor, index);
55}
56
57void
59 QAbstractItemModel* model,
60 const QModelIndex& index) const
61{
62 QStyledItemDelegate::setModelData(editor, model, index);
63}
64
65void
66ButtonDelegate::paint(QPainter* painter,
67 const QStyleOptionViewItem& option,
68 const QModelIndex& index) const
69{
70 QStyleOptionButton button;
71
72 button.rect = option.rect;
73
74 button.features = QStyleOptionButton::AutoDefaultButton;
75
76 int size = appIconSize;
77 if (!index.parent().isValid())
78 {
79 size = scenarioIconSize;
80 }
81 if (index.data().toString().compare("Start") == 0)
82 {
83 button.icon = startPixmap;
84
85 button.iconSize = QSize(size, size);
86 }
87 else if (index.data().toString().compare("Stop") == 0)
88 {
89 button.icon = stopPixmap;
90 button.iconSize = QSize(size, size);
91 }
92 else if (index.data().toString().compare("Restart") == 0)
93 {
94 button.icon = restartPixmap;
95 button.iconSize = QSize(size, size);
96 }
97 else
98 {
99 button.text = index.data().toString();
100 }
101
102 if (index.data().toString().compare("HIDE") == 0)
103 {
104 const_cast<ButtonDelegate*>(this)->buttonStates[index] = {};
105 return;
106 }
107
108 if (const_cast<ButtonDelegate*>(this)->buttonStates[index] != 0)
109 {
110 button.state = const_cast<ButtonDelegate*>(this)->buttonStates[index];
111 }
112 else
113 {
114 const_cast<ButtonDelegate*>(this)->buttonStates[index] =
115 QStyle::State_Raised | QStyle::State_Enabled;
116 button.state = const_cast<ButtonDelegate*>(this)->buttonStates[index];
117 }
118
119 QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter, 0);
120}
121
122bool
124 QAbstractItemModel* model,
125 const QStyleOptionViewItem& option,
126 const QModelIndex& index)
127{
128 //button should be hidden
129 if ((buttonStates[index] & QStyle::State_Enabled) == 0)
130 {
131 return false;
132 }
133
134 if (event->type() == QEvent::MouseButtonPress)
135 {
136 buttonStates[index] = QStyle::State_Sunken | QStyle::State_Enabled;
137
138 return true;
139 }
140 else if (event->type() == QEvent::MouseButtonRelease)
141 {
142 buttonStates[index] = QStyle::State_Raised | QStyle::State_Enabled;
143 ARMARX_DEBUG << index.data().toString().toStdString() << ": editor event " << index.row()
144 << " " << index.column();
145 emit buttonClicked(index.row(), index.column(), index.parent());
146
147 return true;
148 }
149 return false;
150}
151
152int
154{
155 return appIconSize;
156}
157
158void
160{
161 appIconSize = value;
162}
163
164int
166{
167 return scenarioIconSize;
168}
169
170void
172{
173 scenarioIconSize = value;
174}
175
176QSize
177ButtonDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
178{
179 int size = appIconSize;
180 if (!index.parent().isValid())
181 {
182 size = scenarioIconSize;
183 }
184 return QSize(option.rect.width(), option.rect.height() + size + 6);
185 //QItemDelegate::sizeHint(option,index);
186}
uint8_t index
#define option(type, fn)
int getAppIconSize() const
int getScenarioIconSize() const
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Sets button style and draws it.
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
void buttonClicked(int row, int column, QModelIndex parent)
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const
void setScenarioIconSize(int value)
void setAppIconSize(int value)
ButtonDelegate(QWidget *parent=0)
Constructor which doesn't do anything.
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
Calculates and returns the size of the button.
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184