Developer Documentation
objectPickDialog.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 
44 //== INCLUDES =================================================================
45 #include <QHBoxLayout>
46 #include <QPushButton>
47 #include <QTreeView>
48 #include <QMouseEvent>
49 
51 #include <OpenFlipper/widgets/glWidget/simpleViewer.hh>
52 #include <OpenFlipper/widgets/glWidget/QtBaseViewer.hh>
53 
54 #include "objectPickDialog.hh"
55 #include "TreeModel.hh"
56 
57 //== NAMESPACES ===============================================================
58 namespace VSI {
59 
60 //=============================================================================
61 //
62 // CLASS VSI::ObjectPickDialog - IMPLEMENTATION
63 //
64 //=============================================================================
65 
67 ObjectPickDialog::ObjectPickDialog(QWidget* parent, QStringList _flags, QStringList _types, bool _withGroups) :
68  QDialog (parent),
69  selectedId_(0)
70 {
71  QHBoxLayout *hL = new QHBoxLayout;
72  QHBoxLayout *bL = new QHBoxLayout;
73  QVBoxLayout *vL = new QVBoxLayout;
74 
75  model_ = new TreeModel ();
76 
77  treeView_ = new QTreeView;
78  treeView_->setModel (model_);
79  treeView_->resizeColumnToContents (0);
80  treeView_->setSelectionMode (QAbstractItemView::SingleSelection);
81  treeView_->setSelectionBehavior (QAbstractItemView::SelectRows);
82 
83  viewer_ = new SimpleViewer ();
84  viewer_->properties ().objectMarker (&marker_);
85 
86  okButton_ = new QPushButton (tr("OK"));
87  cancelButton_ = new QPushButton (tr("Cancel"));
88 
89  connect (okButton_, SIGNAL (pressed()), this, SLOT (accept()));
90  connect (cancelButton_, SIGNAL (pressed()), this, SLOT (reject()));
91 
92  hL->addWidget (viewer_);
93  hL->setStretchFactor (viewer_, 1);
94  hL->addWidget (treeView_);
95 
96  bL->addStretch (1);
97  bL->addWidget (okButton_);
98  bL->addWidget (cancelButton_);
99 
100  vL->addLayout(hL);
101  vL->addLayout(bL);
102 
103  setLayout (vL);
104 
105  resize (700, 400);
106 
107  setWindowTitle(tr("Click on object or select from list..."));
108 
109  connect (treeView_, SIGNAL (activated( const QModelIndex& )),
110  this, SLOT (activated(const QModelIndex&)));
111  connect (viewer_->viewer(), SIGNAL (signalMouseEventClick(QMouseEvent*, bool)),
112  this, SLOT (slotMouseEventClick(QMouseEvent*, bool)));
113 
115 
116  bool ok = true;
117 
118  if (!_flags.empty ())
119  {
120  bool found = false;
121  foreach (QString flag, _flags)
122  if (o_it->flag (flag))
123  {
124  found = true;
125  break;
126  }
127 
128  if (!found)
129  ok = false;
130  }
131 
132  if (!_types.empty ())
133  {
134  if (!_types.contains (typeName (o_it->dataType())))
135  ok = false;
136  }
137 
138  if (o_it->isGroup() && !_withGroups)
139  continue;
140 
141  if (ok)
142  {
143  if (!_withGroups)
144  model_->objectAdded(o_it, PluginFunctions::objectRoot());
145  else
146  model_->objectAdded (o_it);
147  }
148  }
149 }
150 
151 //------------------------------------------------------------------------------
152 
154 ObjectPickDialog::~ ObjectPickDialog()
155 {
157  o_it->setFlag("vsi_objectId_selected", false);
158  }
159 
160  delete model_;
161 }
162 
163 //------------------------------------------------------------------------------
164 
165 void ObjectPickDialog::activated(const QModelIndex & _index)
166 {
167  if (_index.isValid()) {
168 
169  TreeItem *item = static_cast<TreeItem*>(_index.internalPointer());
170  if (item)
171  {
172  selectedId (item->id());
173  }
174  }
175 }
176 
177 //------------------------------------------------------------------------------
178 
179 void ObjectPickDialog::slotMouseEventClick(QMouseEvent * _event, bool /*_double*/)
180 {
181  size_t nodeIdx, targetIdx;
182 
183  if (viewer_->viewer()->pick(ACG::SceneGraph::PICK_ANYTHING, _event->pos(), nodeIdx, targetIdx))
184  {
185 
186  BaseObjectData *obj;
187 
188  if (PluginFunctions::getPickedObject (nodeIdx, obj))
189  {
190  if (!obj->flag ("vsi_objectId_disabled"))
191  {
192  selectedId (obj->id());
193  }
194  }
195  }
196 }
197 
198 //------------------------------------------------------------------------------
199 
201 {
202  return selectedId_;
203 }
204 
205 //------------------------------------------------------------------------------
206 
207 void VSI::ObjectPickDialog::selectedId(unsigned int _id)
208 {
209  BaseObject *obj = 0;
210 
211  if (PluginFunctions::getObject(_id, obj))
212  {
213  BaseObject* obj2 = 0;
214 
215  if (selectedId_ != _id && PluginFunctions::getObject(selectedId_, obj2))
216  {
217  obj2->setFlag ("vsi_objectId_selected", false);
218  if (obj2->isGroup())
219  setForGroup (obj2, "vsi_objectId_selected", false);
220  }
221  obj->setFlag ("vsi_objectId_selected", true);
222  if (obj->isGroup())
223  setForGroup (obj, "vsi_objectId_selected", true);
224 
225  selectedId_ = _id;
226  viewer_->viewer()->updateGL ();
227  treeView_->setCurrentIndex (model_->getModelIndex(_id, 0));
228  }
229 }
230 
231 //------------------------------------------------------------------------------
232 
233 void VSI::ObjectPickDialog::setForGroup(BaseObject *_obj, QString _flag, bool _enabled)
234 {
236  if (o_it->id () == _obj->id ())
237  continue;
238  if (o_it->isInGroup (_obj->id ()))
239  {
240  o_it->setFlag(_flag, _enabled);
241  if (o_it->isGroup())
242  setForGroup (o_it, _flag, _enabled);
243  }
244  }
245 }
246 
247 //------------------------------------------------------------------------------
248 }
249 
250 
bool isGroup() const
Check if object is a group.
Definition: BaseObject.cc:619
unsigned int selectedId()
Current selected object.
DLLEXPORT QString typeName(DataType _id)
Get the name of a type with given id.
Definition: Types.cc:154
DLLEXPORT BaseObjectIterator baseObjectsEnd()
Return Iterator to Object End.
pick any of the prior targets (should be implemented for all nodes)
Definition: PickTarget.hh:84
bool flag(QString _flag)
Definition: BaseObject.cc:299
int id() const
Definition: BaseObject.cc:190
bool getPickedObject(const size_t _node_idx, BaseObjectData *&_object)
Get the picked mesh.
ObjectPickDialog(QWidget *parent, QStringList _flags, QStringList _types, bool _withGroups)
Constructor.
QModelIndex getModelIndex(TreeItem *_object, int _column)
Return the ModelIndex corresponding to a given TreeItem and Column.
Definition: TreeModel.cc:562
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
BaseObject *& objectRoot()
Get the root of the object structure.
void setFlag(QString _flag, bool _set)
Definition: BaseObject.cc:304
int id()
id
Definition: TreeItem.cc:78
virtual void updateGL()
Redraw scene. Triggers paint event for updating the view (cf. drawNow()).
void objectMarker(ViewObjectMarker *_marker)
set object marker for viewer
glViewer * viewer()
Viewer.
Viewer::ViewerProperties & properties()
Viewer properties.
bool pick(ACG::SceneGraph::PickTarget _pickTarget, const QPoint &_mousePos, size_t &_nodeIdx, size_t &_targetIdx, ACG::Vec3d *_hitPointPtr=0)
void objectAdded(BaseObject *_object)
The object with the given id has been added. add it to the internal tree.
Definition: TreeModel.cc:426
Core Data Iterator used to iterate over all objects (Including groups)