GCC Code Coverage Report


Directory: ./
File: src/gui/selection-handler.cc
Date: 2024-12-20 15:53:58
Exec Total Coverage
Lines: 0 65 0.0%
Branches: 0 82 0.0%

Line Branch Exec Source
1 // Copyright (c) 2015-2018, LAAS-CNRS
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3 //
4 // This file is part of gepetto-viewer.
5 // gepetto-viewer is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // gepetto-viewer is distributed in the hope that it will be
11 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Lesser Public License for more details. You should have
14 // received a copy of the GNU Lesser General Public License along with
15 // gepetto-viewer. If not, see <http://www.gnu.org/licenses/>.
16
17 #include "gepetto/gui/selection-handler.hh"
18
19 #include <QAction>
20 #include <QDebug>
21 #include <QHBoxLayout>
22 #include <QLabel>
23 #include <QVector3D>
24 #include <gepetto/gui/bodytreewidget.hh>
25 #include <gepetto/gui/mainwindow.hh>
26 #include <gepetto/gui/selection-event.hh>
27 #include <gepetto/gui/windows-manager.hh>
28
29 namespace gepetto {
30 namespace gui {
31 static const unsigned int LAST_SELECTED = 7;
32 static const unsigned int SELECTED = 8;
33 static const unsigned int CLEAR_SELECT = 0;
34
35 SelectionHandler::SelectionHandler(WindowsManagerPtr_t wsm, QWidget* parent)
36 : QComboBox(parent), index_(-1), wsm_(wsm) {
37 connect(this, SIGNAL(currentIndexChanged(int)), SLOT(changeMode(int)));
38 }
39
40 SelectionHandler::~SelectionHandler() {}
41
42 SelectionMode* SelectionHandler::mode() {
43 assert(index_ >= 0 && index_ < (int)modes_.size());
44 return modes_[index_];
45 }
46
47 void SelectionHandler::changeMode(int index) {
48 BodyTreeWidget* bt = MainWindow::instance()->bodyTree();
49 foreach (QString name, selected_) {
50 wsm_->setHighlight(name.toStdString(), CLEAR_SELECT);
51 }
52 if (index_ != -1) {
53 modes_[index_]->reset();
54 disconnect(bt, SIGNAL(bodySelected(SelectionEvent*)), modes_[index_],
55 SLOT(onSelect(SelectionEvent*)));
56 disconnect(modes_[index_], SIGNAL(selectedBodies(QStringList)), this,
57 SLOT(getBodies(QStringList)));
58 }
59 index_ = index;
60 connect(bt, SIGNAL(bodySelected(SelectionEvent*)), modes_[index],
61 SLOT(onSelect(SelectionEvent*)), Qt::QueuedConnection);
62 connect(modes_[index], SIGNAL(selectedBodies(QStringList)),
63 SLOT(getBodies(QStringList)));
64 }
65
66 void SelectionHandler::addMode(SelectionMode* mode) {
67 modes_.push_back(mode);
68 addItem(mode->getName());
69 }
70
71 void SelectionHandler::getBodies(QStringList selectedBodies) {
72 selected_ = selectedBodies;
73 }
74
75 UniqueSelection::UniqueSelection(WindowsManagerPtr_t wsm)
76 : SelectionMode(wsm) {}
77
78 UniqueSelection::~UniqueSelection() {}
79
80 void UniqueSelection::onSelect(SelectionEvent* event) {
81 if (!event) return;
82 if (currentSelected_ == event->nodeName()) return;
83 if (currentSelected_ != "")
84 wsm_->setHighlight(currentSelected_.toStdString(), CLEAR_SELECT);
85 currentSelected_ = event->nodeName();
86 if (event->node()) {
87 wsm_->setHighlight(event->node()->getID(), LAST_SELECTED);
88 emit selectedBodies(QStringList() << currentSelected_);
89 }
90 event->done();
91 }
92
93 MultiSelection::MultiSelection(WindowsManagerPtr_t wsm) : SelectionMode(wsm) {}
94
95 MultiSelection::~MultiSelection() {}
96
97 void MultiSelection::reset() {
98 SelectionMode::reset();
99 selectedBodies_ = QStringList();
100 }
101
102 void MultiSelection::onSelect(SelectionEvent* event) {
103 if (!event) return;
104 int i = selectedBodies_.indexOf(event->nodeName());
105 if (event->modKey() != Qt::ControlModifier) { // CTRL not pressed
106 foreach (QString n, selectedBodies_) {
107 wsm_->setHighlight(n.toStdString(), CLEAR_SELECT);
108 }
109 selectedBodies_.clear();
110 currentSelected_ = event->nodeName();
111 if (event->node()) {
112 wsm_->setHighlight(event->node()->getID(), LAST_SELECTED);
113 selectedBodies_ << currentSelected_;
114 }
115 } else { // CTRL pressed
116 if (!currentSelected_.isEmpty())
117 wsm_->setHighlight(currentSelected_.toStdString(), SELECTED);
118 if (i >= 0) { // Already selected.
119 wsm_->setHighlight(event->node()->getID(), CLEAR_SELECT);
120 currentSelected_ = "";
121 selectedBodies_.removeAt(i);
122 } else {
123 currentSelected_ = event->nodeName();
124 if (event->node()) { // Add to the list if not empty
125 wsm_->setHighlight(event->node()->getID(), LAST_SELECTED);
126 selectedBodies_ << currentSelected_;
127 }
128 }
129 }
130 qDebug() << selectedBodies_;
131 emit selectedBodies(selectedBodies_);
132 event->done();
133 }
134 } // namespace gui
135 } // namespace gepetto
136