objectPickDialog.cc 8.39 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

//== INCLUDES =================================================================
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QTreeView>
#include <QMouseEvent>

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <OpenFlipper/widgets/glWidget/simpleViewer.hh>
#include <OpenFlipper/widgets/glWidget/QtBaseViewer.hh>

#include "objectPickDialog.hh"
#include "TreeModel.hh"
#include "TreeItem.hh"

//== NAMESPACES ===============================================================
namespace VSI {

//=============================================================================
//
//  CLASS VSI::ObjectPickDialog - IMPLEMENTATION
//
//=============================================================================

/// Constructor
ObjectPickDialog::ObjectPickDialog(QStringList _flags, QStringList _types, bool _withGroups) :
  QDialog (),
  selectedId_(0)
{
  QHBoxLayout *hL = new QHBoxLayout;
  QHBoxLayout *bL = new QHBoxLayout;
  QVBoxLayout *vL = new QVBoxLayout;

  model_ = new TreeModel ();

  treeView_ = new QTreeView;
  treeView_->setModel (model_);
  treeView_->resizeColumnToContents (0);
  treeView_->setSelectionMode (QAbstractItemView::SingleSelection);
  treeView_->setSelectionBehavior (QAbstractItemView::SelectRows);

  viewer_ = new SimpleViewer ();
  viewer_->properties ().objectMarker (&marker_);

  okButton_ = new QPushButton (tr("OK"));
  cancelButton_ = new QPushButton (tr("Cancel"));

  connect (okButton_, SIGNAL (pressed()), this, SLOT (accept()));
  connect (cancelButton_, SIGNAL (pressed()), this, SLOT (reject()));

  hL->addWidget (viewer_);
  hL->setStretchFactor (viewer_, 1);
  hL->addWidget (treeView_);

  bL->addStretch (1);
  bL->addWidget (okButton_);
  bL->addWidget (cancelButton_);

  vL->addLayout(hL);
  vL->addLayout(bL);

  setLayout (vL);

  resize (700, 400);

  setWindowTitle(tr("Click on object or select from list..."));

  connect (treeView_, SIGNAL (activated( const QModelIndex& )),
           this, SLOT (activated(const QModelIndex&)));
  connect (viewer_->viewer(), SIGNAL (signalMouseEventClick(QMouseEvent*, bool)),
           this, SLOT (slotMouseEventClick(QMouseEvent*, bool)));

  for (PluginFunctions::BaseObjectIterator o_it; o_it != PluginFunctions::baseObjectsEnd(); ++o_it)  {

    bool ok = true;

    if (!_flags.empty ())
    {
      bool found = false;
      foreach (QString flag, _flags)
        if (o_it->flag (flag))
        {
          found = true;
          break;
        }

      if (!found)
        ok = false;
    }

    if (!_types.empty ())
    {
      if (!_types.contains (typeName (o_it->dataType())))
        ok = false;
    }

    if (o_it->isGroup() && !_withGroups)
      continue;

    if (ok)
    {
      if (!_withGroups)
        model_->objectAdded(o_it, PluginFunctions::objectRoot());
      else
        model_->objectAdded (o_it);
    }
  }
}

//------------------------------------------------------------------------------

/// Destructor
ObjectPickDialog::~ ObjectPickDialog()
{
  for (PluginFunctions::BaseObjectIterator o_it; o_it != PluginFunctions::baseObjectsEnd(); ++o_it)  {
    o_it->setFlag("vsi_objectId_selected", false);
  }

  delete model_;
}

//------------------------------------------------------------------------------

void ObjectPickDialog::activated(const QModelIndex & _index)
{
  if (_index.isValid()) {

    TreeItem *item = static_cast<TreeItem*>(_index.internalPointer());
    if (item)
    {
      selectedId (item->id());
    }
  }
}

//------------------------------------------------------------------------------

Dirk Wilden's avatar
Dirk Wilden committed
180
void ObjectPickDialog::slotMouseEventClick(QMouseEvent * _event, bool /*_double*/)
Jan Möbius's avatar
 
Jan Möbius committed
181
182
183
184
185
{
  unsigned int nodeIdx, targetIdx;

  if (viewer_->viewer()->pick(ACG::SceneGraph::PICK_ANYTHING, _event->pos(), nodeIdx, targetIdx))
  {
Jan Möbius's avatar
Jan Möbius committed
186
187
188

    BaseObjectData *obj;

Jan Möbius's avatar
 
Jan Möbius committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
    if (PluginFunctions::getPickedObject (nodeIdx, obj))
    {
      if (!obj->flag ("vsi_objectId_disabled"))
      {
        selectedId (obj->id());
      }
    }
  }
}

//------------------------------------------------------------------------------

unsigned int VSI::ObjectPickDialog::selectedId()
{
  return selectedId_;
}

//------------------------------------------------------------------------------

void VSI::ObjectPickDialog::selectedId(unsigned int _id)
{
Jan Möbius's avatar
Jan Möbius committed
210
  BaseObject *obj = 0;
Jan Möbius's avatar
 
Jan Möbius committed
211
212
213

  if (PluginFunctions::getObject(_id, obj))
  {
Jan Möbius's avatar
Jan Möbius committed
214
215
    BaseObject* obj2 = 0;

Jan Möbius's avatar
 
Jan Möbius committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
    if (selectedId_ != _id && PluginFunctions::getObject(selectedId_, obj2))
    {
      obj2->setFlag ("vsi_objectId_selected", false);
      if (obj2->isGroup())
        setForGroup (obj2, "vsi_objectId_selected", false);
    }
    obj->setFlag ("vsi_objectId_selected", true);
    if (obj->isGroup())
      setForGroup (obj, "vsi_objectId_selected", true);

    selectedId_ = _id;
    viewer_->viewer()->updateGL ();
    treeView_->setCurrentIndex (model_->getModelIndex(_id, 0));
  }
}

//------------------------------------------------------------------------------

void VSI::ObjectPickDialog::setForGroup(BaseObject *_obj, QString _flag, bool _enabled)
{
  for (PluginFunctions::BaseObjectIterator o_it; o_it != PluginFunctions::baseObjectsEnd(); ++o_it)  {
    if (o_it->id () == _obj->id ())
      continue;
    if (o_it->isInGroup (_obj->id ()))
    {
      o_it->setFlag(_flag, _enabled);
      if (o_it->isGroup())
        setForGroup (o_it, _flag, _enabled);
    }
  }
}

//------------------------------------------------------------------------------
}