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