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"
28 
29 #include "treeitem.h"
30 #include "scenarioitem.h"
34 #include <QPainter>
35 #include <QStyleOptionViewItem>
36 #include <QModelIndex>
37 #include <QObject>
38 #include <QApplication>
39 #include <QMouseEvent>
40 
41 
42 
43 ButtonDelegate::ButtonDelegate(QWidget* parent) : QStyledItemDelegate(parent),
44  startPixmap(":/icons/media-playback-start.ico"),
45  stopPixmap(":/icons/process-stop-7.ico"),
46  restartPixmap(":/icons/view-refresh-7.png")
47 {
48 }
49 
50 void ButtonDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
51 {
52  QStyledItemDelegate::setEditorData(editor, index);
53 }
54 
55 void ButtonDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
56 {
57  QStyledItemDelegate::setModelData(editor, model, index);
58 }
59 
60 void ButtonDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
61 {
62  QStyleOptionButton button;
63 
64  button.rect = option.rect;
65 
66  button.features = QStyleOptionButton::AutoDefaultButton;
67 
68  int size = appIconSize;
69  if (!index.parent().isValid())
70  {
71  size = scenarioIconSize;
72  }
73  if (index.data().toString().compare("Start") == 0)
74  {
75  button.icon = startPixmap;
76 
77  button.iconSize = QSize(size, size);
78  }
79  else if (index.data().toString().compare("Stop") == 0)
80  {
81  button.icon = stopPixmap;
82  button.iconSize = QSize(size, size);
83  }
84  else if (index.data().toString().compare("Restart") == 0)
85  {
86  button.icon = restartPixmap;
87  button.iconSize = QSize(size, size);
88  }
89  else
90  {
91  button.text = index.data().toString();
92  }
93 
94  if (index.data().toString().compare("HIDE") == 0)
95  {
96  const_cast<ButtonDelegate*>(this)->buttonStates[index] = {};
97  return;
98  }
99 
100  if (const_cast<ButtonDelegate*>(this)->buttonStates[index] != 0)
101  {
102  button.state = const_cast<ButtonDelegate*>(this)->buttonStates[index];
103  }
104  else
105  {
106  const_cast<ButtonDelegate*>(this)->buttonStates[index] = QStyle::State_Raised | QStyle::State_Enabled;
107  button.state = const_cast<ButtonDelegate*>(this)->buttonStates[index];
108  }
109 
110  QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter, 0);
111 }
112 
113 bool ButtonDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index)
114 {
115  //button should be hidden
116  if ((buttonStates[index] & QStyle::State_Enabled) == 0)
117  {
118  return false;
119  }
120 
121  if (event->type() == QEvent::MouseButtonPress)
122  {
123  buttonStates[index] = QStyle::State_Sunken | QStyle::State_Enabled;
124 
125  return true;
126  }
127  else if (event->type() == QEvent::MouseButtonRelease)
128  {
129  buttonStates[index] = QStyle::State_Raised | QStyle::State_Enabled;
130  ARMARX_DEBUG << index.data().toString().toStdString() << ": editor event " << index.row() << " " << index.column();
131  emit buttonClicked(index.row(), index.column(), index.parent());
132 
133  return true;
134  }
135  return false;
136 }
137 
139 {
140  return appIconSize;
141 }
142 
144 {
145  appIconSize = value;
146 }
147 
149 {
150  return scenarioIconSize;
151 }
152 
154 {
155  scenarioIconSize = value;
156 }
157 
158 QSize ButtonDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
159 {
160  int size = appIconSize;
161  if (!index.parent().isValid())
162  {
163  size = scenarioIconSize;
164  }
165  return QSize(option.rect.width(), option.rect.height() + size + 6);
166  //QItemDelegate::sizeHint(option,index);
167 }
buttondelegate.h
ButtonDelegate::buttonClicked
void buttonClicked(int row, int column, QModelIndex parent)
ButtonDelegate::setScenarioIconSize
void setScenarioIconSize(int value)
Definition: buttondelegate.cpp:153
index
uint8_t index
Definition: EtherCATFrame.h:59
treeitem.h
ButtonDelegate::sizeHint
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
Calculates and returns the size of the button.
Definition: buttondelegate.cpp:158
filterabletreemodelsortfilterproxymodel.h
scenarioitem.h
StatusManager.h
ButtonDelegate::setEditorData
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const
Definition: buttondelegate.cpp:50
ButtonDelegate::getAppIconSize
int getAppIconSize() const
Definition: buttondelegate.cpp:138
ButtonDelegate::editorEvent
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
Definition: buttondelegate.cpp:113
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
ButtonDelegate
Manages a button.
Definition: buttondelegate.h:40
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
ExpressionException.h
option
#define option(type, fn)
ButtonDelegate::setAppIconSize
void setAppIconSize(int value)
Definition: buttondelegate.cpp:143
ButtonDelegate::paint
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Sets button style and draws it.
Definition: buttondelegate.cpp:60
ButtonDelegate::getScenarioIconSize
int getScenarioIconSize() const
Definition: buttondelegate.cpp:148
ButtonDelegate::ButtonDelegate
ButtonDelegate(QWidget *parent=0)
Constructor which doesn't do anything.
Definition: buttondelegate.cpp:43
ButtonDelegate::setModelData
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
Definition: buttondelegate.cpp:55
Logging.h