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 "comboboxbutton.h"
31 #include "scenarioitem.h"
35 #include <QPainter>
36 #include <QStyleOptionViewItem>
37 #include <QModelIndex>
38 #include <QObject>
39 #include <QApplication>
40 #include <QMouseEvent>
41 
42 
43 
44 ButtonDelegate::ButtonDelegate(QWidget* parent) : 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 
51 QWidget* ButtonDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
52 {
53  // Create the combobox and populate it
54  auto model = index.model();
56  ComboBoxButton* cb = new ComboBoxButton(model, index.model()->index(index.row(), 0).data().toString(), parent);
57  cb->setFrame(true);
58  int size = appIconSize;
59  //int row = index.row();
60  if (!index.parent().isValid())
61  {
62  size = scenarioIconSize;
63  }
64  if (index.data().toString().compare("Start") == 0)
65  {
66  cb->addItem(startPixmap, "Local");
67 
68  // ICE START SUCKS HARD!!!!!!!!
69  // IceStartQuickFix: TODO: This is just a very dirty fix. Either completely remove the Ice Start feature or fix it.
70  //cb->addItem(startPixmap, "Ice");
71 
72  //ScenarioManager::StatusManager statusManager;
73  //ScenarioItem* item = index.model()->data(index, SCENARIOITEMSOURCE).value<ScenarioItem*>();
74  //if (statusManager.isIceScenario(item->getScenario()))
75  //{
76  // cb->setCurrentIndex(1);
77  //}
78  cb->setIconSize(QSize(size, size));
79  cb->setToolTip("Start");
80  cb->setIceStatesActive(true);
81  }
82  else if (index.data().toString().compare("Stop") == 0)
83  {
84  cb->addItem(stopPixmap, "Stop");
85  cb->setIconSize(QSize(size, size));
86  cb->setToolTip("Stop");
87  }
88  else if (index.data().toString().compare("Restart") == 0)
89  {
90  cb->addItem(restartPixmap, "Restart");
91  cb->setIconSize(QSize(size, size));
92  cb->setToolTip("Restart");
93  }
94  else
95  {
96  cb->addItem(index.data().toString());
97  }
98 
99  if (index.data().toString().compare("HIDE") == 0)
100  {
101  const_cast<ButtonDelegate*>(this)->buttonStates[index] = 0;
102  cb->setVisible(false);
103  }
104 
105  QObject::connect(cb, SIGNAL(released(QString, const QAbstractItemModel*, QString)), this,
106  SLOT(onComboBoxRelease(QString, const QAbstractItemModel*, QString)));
107 
108  return cb;
109 }
110 
111 void ButtonDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
112 {
113  if (QComboBox* cb = qobject_cast<QComboBox*>(editor))
114  {
115  // get the index of the text in the combobox that matches the current value of the item
116  QString currentText = index.data(Qt::EditRole).toString();
117  int cbIndex = cb->findText(currentText);
118  // if it is valid, adjust the combobox
119  if (cbIndex >= 0)
120  {
121  cb->setCurrentIndex(cbIndex);
122  }
123  }
124  else
125  {
126  QStyledItemDelegate::setEditorData(editor, index);
127  }
128 }
129 
130 void ButtonDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
131 {
132  if (ComboBoxButton* cb = qobject_cast<ComboBoxButton*>(editor))
133  {
134  model->setData(index, cb->currentText(), Qt::EditRole);
135  // cb->setScenarioIndex(index);
136  }
137  else
138  {
139  QStyledItemDelegate::setModelData(editor, model, index);
140  }
141 }
142 
143 void ButtonDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
144 {
145  QStyleOptionButton button;
146 
147  button.rect = option.rect;
148 
149  button.features = QStyleOptionButton::AutoDefaultButton;
150 
151  int size = appIconSize;
152  if (!index.parent().isValid())
153  {
154  size = scenarioIconSize;
155  }
156  if (index.data().toString().compare("Start") == 0)
157  {
158  button.icon = startPixmap;
159 
160  button.iconSize = QSize(size, size);
161  }
162  else if (index.data().toString().compare("Stop") == 0)
163  {
164  button.icon = stopPixmap;
165  button.iconSize = QSize(size, size);
166  }
167  else if (index.data().toString().compare("Restart") == 0)
168  {
169  button.icon = restartPixmap;
170  button.iconSize = QSize(size, size);
171  }
172  else
173  {
174  button.text = index.data().toString();
175  }
176 
177  if (index.data().toString().compare("HIDE") == 0)
178  {
179  const_cast<ButtonDelegate*>(this)->buttonStates[index] = 0;
180  return;
181  }
182 
183  if (const_cast<ButtonDelegate*>(this)->buttonStates[index] != 0)
184  {
185  button.state = const_cast<ButtonDelegate*>(this)->buttonStates[index];
186  }
187  else
188  {
189  const_cast<ButtonDelegate*>(this)->buttonStates[index] = QStyle::State_Raised | QStyle::State_Enabled;
190  button.state = const_cast<ButtonDelegate*>(this)->buttonStates[index];
191  }
192 
193  QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter, 0);
194 
195  //Draw 'Combobox' arrow if it is an Scenario Button
196  // if (!index.parent().isValid())
197  // {
198  // QStyleOptionHeader arrow;
199  // arrow.iconAlignment = Qt::AlignRight;
200  // arrow.rect = option.rect;
201  // arrow.rect.setLeft(arrow.rect.left() + arrow.rect.width() * 0.7);
202  // arrow.state = QStyle::State_DownArrow;
203  // QApplication::style()->drawPrimitive(QStyle::PE_IndicatorHeaderArrow, &arrow, painter);
204  // }
205 }
206 
207 bool ButtonDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index)
208 {
209  //button should be hidden
210  if ((buttonStates[index] & QStyle::State_Enabled) == 0)
211  {
212  return false;
213  }
214 
215  if (event->type() == QEvent::MouseButtonPress)
216  {
217  buttonStates[index] = QStyle::State_Sunken | QStyle::State_Enabled;
218 
219  return true;
220  }
221  else if (event->type() == QEvent::MouseButtonRelease)
222  {
223  buttonStates[index] = QStyle::State_Raised | QStyle::State_Enabled;
224  ARMARX_DEBUG << index.data().toString().toStdString() << ": editor event " << index.row() << " " << index.column();
225  emit buttonClicked(index.row(), index.column(), index.parent());
226 
227  return true;
228  }
229  return false;
230 }
231 
233 {
234  return appIconSize;
235 }
236 
238 {
239  appIconSize = value;
240 }
241 
243 {
244  return scenarioIconSize;
245 }
246 
248 {
249  scenarioIconSize = value;
250 }
251 
252 QSize ButtonDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
253 {
254  int size = appIconSize;
255  if (!index.parent().isValid())
256  {
257  size = scenarioIconSize;
258  }
259  return QSize(option.rect.width(), option.rect.height() + size + 6);
260  //QItemDelegate::sizeHint(option,index);
261 }
262 
263 void ButtonDelegate::onComboBoxRelease(QString text, const QAbstractItemModel* model, const QString& scenarioName)
264 {
265  // find current row of scenario (might have changed due to reordering or insertion)
266  int currentScenarioIndex = ScenarioModel::FindScenarioIndex(model, scenarioName);
267  ARMARX_CHECK_GREATER(currentScenarioIndex, -1);
268  auto index = model->index(currentScenarioIndex, 1);
269  emit comboBoxButtonClicked(currentScenarioIndex, index.column(), index.parent(), text);
270 }
buttondelegate.h
ButtonDelegate::createEditor
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: buttondelegate.cpp:51
ScenarioModel::FindScenarioIndex
static int FindScenarioIndex(const QAbstractItemModel *model, const QString &scenarioName)
Definition: scenariomodel.cpp:260
ButtonDelegate::buttonClicked
void buttonClicked(int row, int column, QModelIndex parent)
ButtonDelegate::setScenarioIconSize
void setScenarioIconSize(int value)
Definition: buttondelegate.cpp:247
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:252
filterabletreemodelsortfilterproxymodel.h
scenarioitem.h
ARMARX_CHECK_GREATER
#define ARMARX_CHECK_GREATER(lhs, rhs)
This macro evaluates whether lhs is greater (>) than rhs and if it turns out to be false it will thro...
Definition: ExpressionException.h:116
comboboxbutton.h
StatusManager.h
ButtonDelegate::setEditorData
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const
Definition: buttondelegate.cpp:111
ButtonDelegate::getAppIconSize
int getAppIconSize() const
Definition: buttondelegate.cpp:232
ButtonDelegate::editorEvent
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
Definition: buttondelegate.cpp:207
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:237
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
ButtonDelegate::paint
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Sets button style and draws it.
Definition: buttondelegate.cpp:143
ButtonDelegate::getScenarioIconSize
int getScenarioIconSize() const
Definition: buttondelegate.cpp:242
ComboBoxButton::setIceStatesActive
void setIceStatesActive(bool active)
Definition: comboboxbutton.cpp:21
ButtonDelegate::ButtonDelegate
ButtonDelegate(QWidget *parent=0)
Constructor which doesn't do anything.
Definition: buttondelegate.cpp:44
ButtonDelegate::setModelData
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
Definition: buttondelegate.cpp:130
Logging.h
ComboBoxButton
Definition: comboboxbutton.h:7
ButtonDelegate::comboBoxButtonClicked
void comboBoxButtonClicked(int row, int column, QModelIndex parent, QString text)