GCC Code Coverage Report


Directory: ./
File: src/gui/bodytreewidget.cc
Date: 2024-08-14 11:04:57
Exec Total Coverage
Lines: 0 79 0.0%
Branches: 0 128 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 <QApplication>
18 #include <QHBoxLayout>
19 #include <QSignalMapper>
20 #include <gepetto/gui/bodytreewidget.hh>
21 #include <gepetto/gui/mainwindow.hh>
22 #include <gepetto/gui/node-action.hh>
23 #include <gepetto/gui/osgwidget.hh>
24 #include <gepetto/gui/selection-event.hh>
25 #include <gepetto/gui/tree-item.hh>
26 #include <gepetto/gui/windows-manager.hh>
27
28 namespace gepetto {
29 namespace gui {
30 void BodyTreeWidget::init(QTreeView* view, QWidget* propertyArea) {
31 MainWindow* main = MainWindow::instance();
32 osg_ = main->osg();
33 view_ = view;
34 propertyArea_ = propertyArea;
35 model_ = new QStandardItemModel(this);
36 view_->setModel(model_);
37 view_->setSelectionMode(QAbstractItemView::ExtendedSelection);
38
39 connect(view_, SIGNAL(customContextMenuRequested(QPoint)),
40 SLOT(customContextMenu(QPoint)));
41 connect(view_->selectionModel(),
42 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
43 SLOT(currentChanged(QModelIndex, QModelIndex)));
44
45 /*
46 addSlider(toolBox_, "Scale", this, SLOT(setScale(int)));
47 */
48 propertyArea_->setLayout(new QVBoxLayout);
49 }
50
51 QTreeView* BodyTreeWidget::view() { return view_; }
52
53 void BodyTreeWidget::selectBodyByName(const QString bodyName) {
54 qDebug() << "Use std::string instead of QString";
55 return selectBodyByName(bodyName.toStdString());
56 }
57
58 void BodyTreeWidget::selectBodyByName(const std::string& bodyName) {
59 BodyTreeItems_t bodies = osg_->bodyTreeItems(bodyName);
60 if (bodies.empty()) {
61 qDebug() << "Body" << bodyName.c_str() << "not found.";
62 view_->clearSelection();
63 } else {
64 view_->setCurrentIndex(bodies[0]->index());
65 }
66 }
67
68 void BodyTreeWidget::handleSelectionEvent(const SelectionEvent* event) {
69 disconnect(view_->selectionModel(),
70 SIGNAL(currentChanged(QModelIndex, QModelIndex)), this,
71 SLOT(currentChanged(QModelIndex, QModelIndex)));
72 BodyTreeItem* item = NULL;
73 if (event->node()) {
74 BodyTreeItems_t matches = osg_->bodyTreeItems(event->node()->getID());
75
76 if (matches.empty())
77 view_->clearSelection();
78 else {
79 item = matches[0];
80 if (event->modKey() == Qt::ControlModifier)
81 view_->selectionModel()->setCurrentIndex(item->index(),
82 QItemSelectionModel::Toggle);
83 else
84 view_->selectionModel()->select(item->index(),
85 QItemSelectionModel::ClearAndSelect);
86 view_->scrollTo(matches[0]->index());
87 }
88 } else
89 view_->clearSelection();
90 updatePropertyArea(item);
91 connect(view_->selectionModel(),
92 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
93 SLOT(currentChanged(QModelIndex, QModelIndex)));
94 }
95
96 void BodyTreeWidget::emitBodySelected(SelectionEvent* event) {
97 event->setCounter(receivers(SIGNAL(bodySelected(SelectionEvent*))) + 1);
98 emit bodySelected(event);
99 if (event->type() != SelectionEvent::FromBodyTree) {
100 MainWindow* main = MainWindow::instance();
101 handleSelectionEvent(event);
102 main->requestSelectJointFromBodyName(event->nodeName());
103 }
104 event->done();
105 }
106
107 void BodyTreeWidget::updatePropertyArea(BodyTreeItem* item) {
108 QLayoutItem* child;
109 while ((child = propertyArea_->layout()->takeAt(0)) != 0) {
110 if (child->widget() != NULL) {
111 child->widget()->setParent(NULL);
112 }
113 }
114 if (item != NULL) {
115 propertyArea_->layout()->addWidget(item->propertyEditors());
116 }
117 }
118
119 void BodyTreeWidget::currentChanged(const QModelIndex& current,
120 const QModelIndex& /*previous*/) {
121 // TODO
122 // if (!current.isValid()) {
123 // deselect
124 // }
125 BodyTreeItem* item = dynamic_cast<BodyTreeItem*>(
126 qobject_cast<const QStandardItemModel*>(view_->model())
127 ->itemFromIndex(current));
128 updatePropertyArea(item);
129 if (item) {
130 SelectionEvent* event =
131 new SelectionEvent(SelectionEvent::FromBodyTree, item->node(),
132 QApplication::keyboardModifiers());
133 emitBodySelected(event);
134 }
135 }
136
137 QList<BodyTreeItem*> BodyTreeWidget::selectedBodies() const {
138 QList<BodyTreeItem*> list;
139 foreach (const QModelIndex& index,
140 view_->selectionModel()->selectedIndexes()) {
141 BodyTreeItem* item =
142 dynamic_cast<BodyTreeItem*>(model_->itemFromIndex(index));
143 if (item) list.push_back(item);
144 }
145 return list;
146 }
147
148 void BodyTreeWidget::customContextMenu(const QPoint& pos) {
149 QModelIndex index = view_->indexAt(pos);
150 if (index.isValid()) {
151 BodyTreeItem* item =
152 dynamic_cast<BodyTreeItem*>(model_->itemFromIndex(index));
153 if (!item) return;
154 MainWindow* main = MainWindow::instance();
155 QMenu contextMenu(tr("Node"));
156 item->populateContextMenu(&contextMenu);
157 QMenu* windows = contextMenu.addMenu(tr("Attach to window"));
158 foreach (OSGWidget* w, main->osgWindows()) {
159 NodeAction* ja =
160 new NodeAction(w->objectName(), item->node(), w, windows);
161 windows->addAction(ja);
162 }
163 contextMenu.exec(view_->mapToGlobal(pos));
164 }
165 }
166 } // namespace gui
167 } // namespace gepetto
168