GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/gui/selection-handler.cc Lines: 1 67 1.5 %
Date: 2020-05-14 11:23:33 Branches: 2 112 1.8 %

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 <QHBoxLayout>
18
#include <QLabel>
19
#include <QVector3D>
20
#include <QAction>
21
#include <QDebug>
22
23
#include <gepetto/gui/bodytreewidget.hh>
24
#include <gepetto/gui/mainwindow.hh>
25
#include <gepetto/gui/selection-event.hh>
26
#include <gepetto/gui/windows-manager.hh>
27
#include "gepetto/gui/selection-handler.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),
37
	index_(-1),
38
	wsm_(wsm)
39
    {
40
      connect(this, SIGNAL(currentIndexChanged(int)), SLOT(changeMode(int)));
41
    }
42
43
    SelectionHandler::~SelectionHandler()
44
    {
45
    }
46
47
    SelectionMode* SelectionHandler::mode ()
48
    {
49
      assert(index_ >= 0 && index_ < (int)modes_.size());
50
      return modes_[index_];
51
    }
52
53
    void SelectionHandler::changeMode(int index)
54
    {
55
      BodyTreeWidget* bt = MainWindow::instance()->bodyTree();
56
      foreach(QString name, selected_) {
57
        wsm_->setHighlight(name.toStdString(), CLEAR_SELECT);
58
      }
59
      if (index_ != -1) {
60
        modes_[index_]->reset();
61
        disconnect(bt, SIGNAL(bodySelected(SelectionEvent*)),
62
                   modes_[index_], SLOT(onSelect(SelectionEvent*)));
63
        disconnect(modes_[index_], SIGNAL(selectedBodies(QStringList)),
64
                   this, SLOT(getBodies(QStringList)));
65
      }
66
      index_ = index;
67
      connect(bt, SIGNAL(bodySelected(SelectionEvent*)),
68
              modes_[index], SLOT(onSelect(SelectionEvent*)),
69
              Qt::QueuedConnection);
70
      connect(modes_[index], SIGNAL(selectedBodies(QStringList)),
71
              SLOT(getBodies(QStringList)));
72
    }
73
74
    void SelectionHandler::addMode(SelectionMode* mode)
75
    {
76
      modes_.push_back(mode);
77
      addItem(mode->getName());
78
    }
79
80
    void SelectionHandler::getBodies(QStringList selectedBodies)
81
    {
82
      selected_ = selectedBodies;
83
    }
84
85
    UniqueSelection::UniqueSelection(WindowsManagerPtr_t wsm)
86
      : SelectionMode(wsm)
87
    {
88
    }
89
90
    UniqueSelection::~UniqueSelection()
91
    {
92
    }
93
94
    void UniqueSelection::onSelect(SelectionEvent* event)
95
    {
96
      if (!event) return;
97
      if (currentSelected_ == event->nodeName()) return;
98
      if (currentSelected_ != "") wsm_->setHighlight(currentSelected_.toStdString(), CLEAR_SELECT);
99
      currentSelected_ = event->nodeName();
100
      if (event->node()) {
101
        wsm_->setHighlight(event->node()->getID(), LAST_SELECTED);
102
        emit selectedBodies(QStringList() << currentSelected_);
103
      }
104
      event->done();
105
    }
106
107
    MultiSelection::MultiSelection(WindowsManagerPtr_t wsm)
108
      : SelectionMode(wsm)
109
    {
110
    }
111
112
    MultiSelection::~MultiSelection()
113
    {
114
    }
115
116
    void MultiSelection::reset()
117
    {
118
      SelectionMode::reset();
119
      selectedBodies_ = QStringList();
120
    }
121
122
    void MultiSelection::onSelect(SelectionEvent* event)
123
    {
124
      if (!event) return;
125
      int i = selectedBodies_.indexOf(event->nodeName());
126
      if (event->modKey() != Qt::ControlModifier) { // CTRL not pressed
127
        foreach (QString n, selectedBodies_) {
128
          wsm_->setHighlight(n.toStdString(), CLEAR_SELECT);
129
        }
130
        selectedBodies_.clear();
131
        currentSelected_ = event->nodeName();
132
        if (event->node()) {
133
          wsm_->setHighlight(event->node()->getID(), LAST_SELECTED);
134
          selectedBodies_ << currentSelected_;
135
        }
136
      } else {                                    // CTRL pressed
137
        if (!currentSelected_.isEmpty())
138
          wsm_->setHighlight(currentSelected_.toStdString(), SELECTED);
139
        if (i >= 0) {                             // Already selected.
140
          wsm_->setHighlight(event->node()->getID(), CLEAR_SELECT);
141
          currentSelected_ = "";
142
          selectedBodies_.removeAt(i);
143
        } else {
144
          currentSelected_ = event->nodeName();
145
          if (event->node()){   // Add to the list if not empty
146
            wsm_->setHighlight(event->node()->getID(), LAST_SELECTED);
147
            selectedBodies_ << currentSelected_;
148
          }
149
        }
150
      }
151
      qDebug() << selectedBodies_;
152
      emit selectedBodies(selectedBodies_);
153
      event->done();
154
    }
155
  }
156

3
}