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