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 "TreeModelObjectSelection.hh"
56 
57 //== NAMESPACES ===============================================================
58 
59 //=============================================================================
60 //
61 // CLASS ObjectPickDialog - IMPLEMENTATION
62 //
63 //=============================================================================
64 
66 ObjectPickDialog::ObjectPickDialog(QStringList _flags, QStringList _types, bool _withGroups) :
67  QDialog (),
68  selectedId_(0)
69 {
70  QHBoxLayout *hL = new QHBoxLayout;
71  QHBoxLayout *bL = new QHBoxLayout;
72  QVBoxLayout *vL = new QVBoxLayout;
73 
74  model_ = new TreeModelObjectSelection ();
75 
76  treeView_ = new QTreeView;
77  treeView_->setModel (model_);
78  treeView_->resizeColumnToContents (0);
79  treeView_->setSelectionMode (QAbstractItemView::SingleSelection);
80  treeView_->setSelectionBehavior (QAbstractItemView::SelectRows);
81 
82  viewer_ = new SimpleViewer ();
83  viewer_->properties ().objectMarker (&marker_);
84 
85  okButton_ = new QPushButton (tr("OK"));
86  cancelButton_ = new QPushButton (tr("Cancel"));
87 
88  connect (okButton_, SIGNAL (pressed()), this, SLOT (accept()));
89  connect (cancelButton_, SIGNAL (pressed()), this, SLOT (reject()));
90 
91  hL->addWidget (viewer_);
92  hL->setStretchFactor (viewer_, 1);
93  hL->addWidget (treeView_);
94 
95  bL->addStretch (1);
96  bL->addWidget (okButton_);
97  bL->addWidget (cancelButton_);
98 
99  vL->addLayout(hL);
100  vL->addLayout(bL);
101 
102  setLayout (vL);
103 
104  resize (700, 400);
105 
106  setWindowTitle(tr("Click on object or select from list..."));
107 
108  connect (treeView_, SIGNAL (activated( const QModelIndex& )),
109  this, SLOT (activated(const QModelIndex&)));
110  connect (viewer_->viewer(), SIGNAL (signalMouseEventClick(QMouseEvent*, bool)),
111  this, SLOT (slotMouseEventClick(QMouseEvent*, bool)));
112 
114 
115  bool ok = true;
116 
117  if (!_flags.empty ())
118  {
119  bool found = false;
120  foreach (QString flag, _flags)
121  if (o_it->flag (flag))
122  {
123  found = true;
124  break;
125  }
126 
127  if (!found)
128  ok = false;
129  }
130 
131  if (!_types.empty ())
132  {
133  if (!_types.contains (typeName (o_it->dataType())))
134  ok = false;
135  }
136 
137  if (o_it->isGroup() && !_withGroups)
138  continue;
139 
140  if (ok)
141  {
142  if (!_withGroups)
143  model_->objectAdded(o_it, PluginFunctions::objectRoot());
144  else
145  model_->objectAdded (o_it);
146  }
147  }
148 }
149 
150 //------------------------------------------------------------------------------
151 
153 ObjectPickDialog::~ ObjectPickDialog()
154 {
156  o_it->setFlag("vsi_objectId_selected", false);
157  }
158 
159  delete model_;
160 }
161 
162 //------------------------------------------------------------------------------
163 
164 void ObjectPickDialog::activated(const QModelIndex & _index)
165 {
166  if (_index.isValid()) {
167 
168  TreeItemObjectSelection *item = static_cast<TreeItemObjectSelection*>(_index.internalPointer());
169  if (item)
170  {
171  selectedId (item->id());
172  }
173  }
174 }
175 
176 //------------------------------------------------------------------------------
177 
178 void ObjectPickDialog::slotMouseEventClick(QMouseEvent * _event, bool /*_double*/)
179 {
180  size_t nodeIdx, targetIdx;
181 
182 
183  if (viewer_->viewer()->pick(ACG::SceneGraph::PICK_ANYTHING, _event->pos(), nodeIdx, targetIdx))
184  {
185  BaseObjectData *obj = 0;
186  if (PluginFunctions::getPickedObject (nodeIdx, obj))
187  {
188  if (!obj->flag ("vsi_objectId_disabled"))
189  {
190  selectedId (obj->id());
191  }
192  }
193  }
194 }
195 
196 //------------------------------------------------------------------------------
197 
199 {
200  return selectedId_;
201 }
202 
203 //------------------------------------------------------------------------------
204 
205 void ObjectPickDialog::selectedId(unsigned int _id)
206 {
207  BaseObject* obj = 0;
208 
209  if (PluginFunctions::getObject(_id, obj))
210  {
211  BaseObject* obj2 = 0;
212 
213  if (selectedId_ != _id && PluginFunctions::getObject(selectedId_, obj2))
214  {
215  obj2->setFlag ("vsi_objectId_selected", false);
216  if (obj2->isGroup())
217  setForGroup (obj2, "vsi_objectId_selected", false);
218  }
219  obj->setFlag ("vsi_objectId_selected", true);
220  if (obj->isGroup())
221  setForGroup (obj, "vsi_objectId_selected", true);
222 
223  selectedId_ = _id;
224  viewer_->viewer()->updateGL ();
225  treeView_->setCurrentIndex (model_->getModelIndex(_id, 0));
226  }
227 }
228 
229 //------------------------------------------------------------------------------
230 
231 void ObjectPickDialog::setForGroup(BaseObject *_obj, QString _flag, bool _enabled)
232 {
234  if (o_it->id () == _obj->id ())
235  continue;
236  if (o_it->isInGroup (_obj->id ()))
237  {
238  o_it->setFlag(_flag, _enabled);
239  if (o_it->isGroup())
240  setForGroup (o_it, _flag, _enabled);
241  }
242  }
243 }
244 
245 //------------------------------------------------------------------------------
246 
247 
248 
void setFlag(QString _flag, bool _set)
Definition: BaseObject.cc:304
glViewer * viewer()
Viewer.
unsigned int selectedId()
Current selected object.
ObjectPickDialog(QStringList _flags, QStringList _types, bool _withGroups)
Constructor.
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 objectAdded(BaseObject *_object)
The object with the given id has been added. add it to the internal tree.
int id() const
Definition: BaseObject.cc:190
void objectMarker(ViewObjectMarker *_marker)
set object marker for viewer
QModelIndex getModelIndex(TreeItemObjectSelection *_object, int _column)
Return the ModelIndex corresponding to a given TreeItemObjectSelection and Column.
pick any of the prior targets (should be implemented for all nodes)
Definition: PickTarget.hh:84
Core Data Iterator used to iterate over all objects (Including groups)
bool flag(QString _flag)
Definition: BaseObject.cc:299
DLLEXPORT QString typeName(DataType _id)
Get the name of a type with given id.
Definition: Types.cc:154
bool isGroup() const
Check if object is a group.
Definition: BaseObject.cc:619
virtual void updateGL()
Redraw scene. Triggers paint event for updating the view (cf. drawNow()).
DLLEXPORT BaseObjectIterator baseObjectsEnd()
Return Iterator to Object End.
Viewer::ViewerProperties & properties()
Viewer properties.
bool getPickedObject(const size_t _node_idx, BaseObjectData *&_object)
Get the picked mesh.
bool pick(ACG::SceneGraph::PickTarget _pickTarget, const QPoint &_mousePos, size_t &_nodeIdx, size_t &_targetIdx, ACG::Vec3d *_hitPointPtr=0)