Developer Documentation
objectIdWidget.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 <QComboBox>
48 
50 
51 #include "objectIdWidget.hh"
52 #include "objectPickDialog.hh"
53 
54 //== NAMESPACES ===============================================================
55 namespace VSI {
56 
57 //=============================================================================
58 //
59 // CLASS VSI::ObjectIdWidget - IMPLEMENTATION
60 //
61 //=============================================================================
62 
64 ObjectIdWidget::ObjectIdWidget(QMap< QString, QString > &_hints, QString _typeName, QWidget *_parent) :
65  TypeWidget (_hints, _typeName, _parent),
66  pickButton_ (0),
67  combo_ (0)
68 {
69  int n_ok = 0;
70 
71  QHBoxLayout *hL = new QHBoxLayout;
72 
73  combo_ = new QComboBox ();
74  pickButton_ = new QPushButton (tr("Pick Object"));
75 
76  hL->addWidget (combo_);
77  hL->setStretchFactor (combo_, 1);
78  hL->addWidget (pickButton_);
79 
80  if (_hints.contains ("flags"))
81  flags_ = _hints["flags"].split(',');
82 
83  if (_hints.contains ("types"))
84  types_ = _hints["types"].split(',');
85 
86  if (flags_.contains("all", Qt::CaseInsensitive))
87  flags_.clear ();
88 
89  withGroups_ = types_.contains("Group");
90 
91  if (types_.contains("All"))
92  types_.clear ();
93 
94  setLayout (hL);
95 
96  connect (pickButton_, SIGNAL (pressed()), this, SLOT (showPickDialog ()));
97 
99 
100  bool ok = true;
101 
102  if (!flags_.empty ())
103  {
104  bool found = false;
105  foreach (QString flag, flags_)
106  if (o_it->flag (flag))
107  {
108  found = true;
109  break;
110  }
111 
112  if (!found)
113  ok = false;
114  }
115 
116  if (!types_.empty ())
117  {
118  if (!types_.contains (typeName (o_it->dataType())))
119  ok = false;
120  }
121 
122  if (o_it->isGroup() && !withGroups_)
123  continue;
124 
125  if (ok)
126  {
127  combo_->addItem (o_it->name() + " (" + QString::number (o_it->id ()) + ")", QVariant (o_it->id()));
128  n_ok++;
129  }
130  }
131 
132  if (n_ok < 2)
133  {
134  pickButton_->setEnabled (false);
135  combo_->setEnabled (false);
136  }
137 }
138 
140 ObjectIdWidget::~ ObjectIdWidget()
141 {
143  o_it->setFlag("vsi_objectId_selected", false);
144  }
145 }
146 
147 //------------------------------------------------------------------------------
148 
151 {
152  if (combo_->count ())
153  {
154  return QString::number (combo_->itemData (combo_->currentIndex()).toInt ());
155  }
156  else
157  return 0;
158 }
159 
160 //------------------------------------------------------------------------------
161 
163 void ObjectIdWidget::fromValue(QString _from)
164 {
165  combo_->setCurrentIndex (combo_->findData (QVariant (_from.toInt ())));
166 }
167 
168 //------------------------------------------------------------------------------
169 
172 {
173  combo_->setCurrentIndex (0);
174 }
175 
176 //------------------------------------------------------------------------------
177 
178 void ObjectIdWidget::showPickDialog()
179 {
180  ObjectPickDialog d(this, flags_, types_, withGroups_);
181  d.selectedId (combo_->itemData (combo_->currentIndex()).toInt ());
182 
183  if (d.exec () == QDialog::Accepted)
184  combo_->setCurrentIndex (combo_->findData (QVariant (d.selectedId())));
185 }
186 
187 //------------------------------------------------------------------------------
188 }
189 
190 
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.
void fromValue(QString _from)
Read value from string.
QString toValue()
Convert current value to string.
ObjectIdWidget(QMap< QString, QString > &_hints, QString _typeName, QWidget *_parent=NULL)
Constructor.
void toDefault()
Reset to default.
Core Data Iterator used to iterate over all objects (Including groups)