scenariolistview.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 "scenariolistview.h"
27
28#include <algorithm>
29
30#include <QAction>
31#include <QComboBox>
32#include <QFontMetrics>
33#include <QHeaderView>
34
36
37#include <ArmarXGui/gui-plugins/ScenarioManager/gui/ui_scenariolistview.h>
38
39#include "scenarioitem.h"
40#include "treeitem.h"
41
42using namespace ScenarioManager;
43using namespace Data_Structure;
44using namespace armarx;
45
47 QWidget(parent),
48 ui(new Ui::ScenarioListView),
49 startButtonDelegate(this),
50 stopButtonDelegate(this),
51 restartButtonDelegate(this),
52 removeAction("Remove", &contextMenu),
53 expandAction("Expand", &contextMenu),
54 collapseAction("Collapse", &contextMenu)
55{
56 ui->setupUi(this);
57
58 ui->treeView->setModel(model.get());
59 ui->treeView->setSortingEnabled(true);
60 ui->treeView->setItemDelegateForColumn(1, &startButtonDelegate);
61 ui->treeView->setItemDelegateForColumn(2, &stopButtonDelegate);
62 ui->treeView->setItemDelegateForColumn(3, &restartButtonDelegate);
63
64
65 contextMenu.setParent(ui->treeView);
66 ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
67 QObject::connect(ui->treeView,
68 SIGNAL(customContextMenuRequested(const QPoint&)),
69 this,
70 SLOT(onCustomContextMenu(const QPoint&)));
71
72 ui->treeView->addAction(&removeAction);
73
74 ui->treeView->setDragEnabled(true);
75 ui->treeView->setDefaultDropAction(Qt::CopyAction);
76 ui->treeView->setAcceptDrops(true);
77 ui->treeView->setDropIndicatorShown(true);
78
79 QObject::connect(&removeAction, SIGNAL(triggered()), this, SLOT(removeItemTriggered()));
80 QObject::connect(&expandAction, SIGNAL(triggered()), this, SLOT(expandItemTriggered()));
81 QObject::connect(&collapseAction, SIGNAL(triggered()), this, SLOT(collapseItemTriggered()));
82
83 QObject::connect(&startButtonDelegate,
84 SIGNAL(buttonClicked(int, int, QModelIndex)),
85 this,
86 SLOT(startButtonClicked(int, int, QModelIndex)));
87
88
89 QObject::connect(&stopButtonDelegate,
90 SIGNAL(buttonClicked(int, int, QModelIndex)),
91 this,
92 SLOT(stopButtonClicked(int, int, QModelIndex)));
93
94 QObject::connect(&restartButtonDelegate,
95 SIGNAL(buttonClicked(int, int, QModelIndex)),
96 this,
97 SLOT(restartButtonClicked(int, int, QModelIndex)));
98}
99
101{
102 delete ui;
103}
104
105void
107{
108 this->model = treeModel;
109 ui->treeView->setModel(model.get());
110
111 QHeaderView* header = ui->treeView->header();
112 header->setStretchLastSection(false);
113 header->setSectionResizeMode(0, QHeaderView::Stretch);
114 header->setSectionResizeMode(1, QHeaderView::Fixed);
115 header->setSectionResizeMode(2, QHeaderView::Fixed);
116 header->setSectionResizeMode(3, QHeaderView::Fixed);
117 header->setSectionResizeMode(4, QHeaderView::Fixed);
118 const int buttonWidth = 60;
119 header->resizeSection(1, buttonWidth);
120 header->resizeSection(2, buttonWidth);
121 header->resizeSection(3, buttonWidth);
122 header->resizeSection(4, statusColumnWidth());
123}
124
125int
126ScenarioListView::statusColumnWidth() const
127{
128 // The status column used to be ResizeToContents, which made every dataChanged() emitted by the
129 // status refresh re-measure the delegate of every item in the tree and relayout it. The set of
130 // status strings is fixed, so the column can simply be sized for the longest of them once.
131 const QFontMetrics metrics(ui->treeView->font());
132
133 int width = 0;
134 for (const std::string& status : {std::string("Status"),
142 {
143 width = std::max(width,
144 metrics.boundingRect(QString::fromStdString(status)).width());
145 }
146
147 const int padding = 16;
148 return width + padding;
149}
150
151void
152ScenarioListView::on_searchBar_textEdited(const QString& text)
153{
154 model->setFilterRegExp(QRegExp(text, Qt::CaseInsensitive, QRegExp::FixedString));
155 ui->treeView->expandAll();
156}
157
158void
159ScenarioListView::startButtonClicked(int row, int column, QModelIndex parent)
160{
161 emit startApplication(row, column, parent);
162}
163
164void
165ScenarioListView::stopButtonClicked(int row, int column, QModelIndex parent)
166{
167 emit stopApplication(row, column, parent);
168}
169
170void
171ScenarioListView::restartButtonClicked(int row, int column, QModelIndex parent)
172{
173 emit restartApplication(row, column, parent);
174}
175
176void
177ScenarioListView::on_newButton_clicked()
178{
179 emit createScenario();
180}
181
182void
183ScenarioListView::on_openButton_clicked()
184{
185 emit showOpenDialog();
186}
187
188void
189ScenarioListView::on_expandAllButton_clicked()
190{
191 ui->treeView->expandAll();
192}
193
194void
195ScenarioListView::on_collapseAllButton_clicked()
196{
197 ui->treeView->collapseAll();
198}
199
200void
201ScenarioListView::removeItemTriggered()
202{
203 QModelIndex index = ui->treeView->currentIndex();
204 emit removeItem(index);
205}
206
207void
208ScenarioListView::on_treeView_clicked(const QModelIndex& index)
209{
210 emit itemClicked(index);
211}
212
213void
214ScenarioListView::onCustomContextMenu(const QPoint& point)
215{
216 QModelIndex index = ui->treeView->currentIndex();
217
218 QList<QAction*> actions;
219 actions.append(&expandAction);
220 actions.append(&collapseAction);
221 QAction separator;
222 separator.setSeparator(true);
223 actions.append(&separator);
224 actions.append(&removeAction);
225
226 QMenu::exec(actions, ui->treeView->mapToGlobal(point));
227}
228
229// Generated by ChatGPT
230inline void
231expandRecursively(QTreeView* view, const QModelIndex& index)
232{
233 if (!index.isValid())
234 {
235 return;
236 }
237
238 view->expand(index);
239
240 const QAbstractItemModel* model = index.model();
241 int rows = model->rowCount(index);
242 int cols = model->columnCount(index);
243
244 for (int r = 0; r < rows; ++r)
245 {
246 for (int c = 0; c < cols; ++c)
247 {
248 QModelIndex child = model->index(r, c, index);
249 if (child.isValid())
250 {
251 expandRecursively(view, child);
252 }
253 }
254 }
255}
256
257void
258ScenarioListView::expandItemTriggered()
259{
260 QModelIndex index = ui->treeView->currentIndex();
261
262 // Support for Ubuntu 18 with older Qt version (>=5.9.5)
263 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
264 ui->treeView->expandRecursively(index);
265 #else
266 expandRecursively(ui->treeView, index);
267 #endif
268}
269
270void
271ScenarioListView::collapseItemTriggered()
272{
273 QModelIndex index = ui->treeView->currentIndex();
274 ui->treeView->collapse(index);
275}
uint8_t index
constexpr T c
~ScenarioListView() override
Destructor.
void itemClicked(const QModelIndex &index)
ScenarioListView(QWidget *parent=0)
Constructor that sets up the ui and behaviour of this view.
void stopApplication(int row, int column, QModelIndex parent)
void setModel(FilterableTreeModelSortFilterProxyModelPtr model)
Sets the model of this view.
void restartApplication(int row, int column, QModelIndex parent)
void startApplication(int row, int column, QModelIndex parent)
void removeItem(QModelIndex index)
std::shared_ptr< FilterableTreeModelSortFilterProxyModel > FilterableTreeModelSortFilterProxyModelPtr
ArmarX Headers.
bool buttonClicked(RemoteGui::ValueMap const &newValues, RemoteGui::ValueMap const &oldValues, std::string const &name)
Definition Storage.cpp:7
This file offers overloads of toIce() and fromIce() functions for STL container types.
void expandRecursively(QTreeView *view, const QModelIndex &index)