GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/gui/bodytreewidget.cc Lines: 1 81 1.2 %
Date: 2020-05-14 11:23:33 Branches: 2 144 1.4 %

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

3
}