Developer Documentation
selectionWidget.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 <QLineEdit>
53 #include <QPushButton>
54 #include <QCheckBox>
55 #include <QComboBox>
56 
57 #include "../parser/context.hh"
58 #include "selectionWidget.hh"
59 
60 //== NAMESPACES ===============================================================
61 namespace VSI {
62 
63 //=============================================================================
64 //
65 // CLASS VSI::SelectionWidget - IMPLEMENTATION
66 //
67 //=============================================================================
68 
70 SelectionWidget::SelectionWidget(QMap< QString, QString > &_hints, QString _typeName, QWidget *_parent) :
71  TypeWidget (_hints, _typeName, _parent),
72  multiple_ (false),
73  default_ (),
74  combo_ (0)
75 {
76  QHBoxLayout *hL = new QHBoxLayout;
77 
78  if (_hints.contains ("names"))
79  {
80  QStringList n = _hints["names"].split (',');
81  QStringList d;
82 
83  if (_hints.contains ("descriptions"))
84  {
85  d = _hints["descriptions"].split (',');
86  if (n.length () != d.length ())
87  d = n;
88  }
89 
90  for (int i = 0; i < n.length (); i++)
91  names_.append (QPair<QString,QString> (n[i], d[i]));
92  }
93  else
94  names_.append (QPair<QString,QString> ("none", "None"));
95 
96  if (_hints.contains ("multiple"))
97  multiple_ = Context::strToBool (_hints["multiple"]);
98 
99  if (_hints.contains ("default"))
100  default_ = _hints["default"].split (',');
101 
102  if (!multiple_)
103  {
104  combo_ = new QComboBox ();
105  for (int i = 0; i < names_.size (); i++)
106  combo_->addItem (names_[i].second, names_[i].first);
107 
108  if (default_.length ())
109  combo_->setCurrentIndex (combo_->findData (default_[0]));
110  else
111  combo_->setCurrentIndex (0);
112 
113  hL->addWidget (combo_);
114  }
115  else
116  {
117  QGridLayout *gL = new QGridLayout;
118  for (int i = 0; i < names_.size (); i++)
119  {
120  QCheckBox *c = new QCheckBox (names_[i].second);
121  checkBox_.append (c);
122  gL->addWidget (c, i / 2, i & 1);
123 
124  if (default_.contains (names_[i].first))
125  c->setChecked (true);
126  else
127  c->setChecked (false);
128  }
129  hL->addLayout (gL);
130  }
131 
132  setLayout (hL);
133 }
134 
135 //------------------------------------------------------------------------------
136 
139 {
140 }
141 
142 //------------------------------------------------------------------------------
143 
146 {
147  QString rv;
148 
149  if (!multiple_)
150  {
151  rv = combo_->itemData (combo_->currentIndex ()).toString ();
152  }
153  else
154  {
155  QStringList sl;
156 
157  for (int i = 0; i < names_.size (); i++)
158  {
159  if (checkBox_[i]->isChecked ())
160  sl << names_[i].first;
161  }
162 
163  rv = sl.join (",");
164  }
165 
166  return "\"" + rv + "\"";
167 }
168 
169 //------------------------------------------------------------------------------
170 
172 void SelectionWidget::fromValue(QString _from)
173 {
174  if (_from.isEmpty ())
175  {
176  if (!multiple_)
177  combo_->setCurrentIndex (0);
178  else
179  foreach (QCheckBox *c, checkBox_)
180  c->setChecked (false);
181  return;
182  }
183 
184  _from.remove (0, 1);
185  _from.remove (_from.length () - 1, 1);
186 
187  QStringList values = _from.split (',');
188 
189  if (!multiple_)
190  {
191  if (values.length ())
192  combo_->setCurrentIndex (combo_->findData (values[0]));
193  else
194  combo_->setCurrentIndex (0);
195  }
196  else
197  for (int i = 0; i < names_.size (); i++)
198  {
199  if (values.contains (names_[i].first))
200  checkBox_[i]->setChecked (true);
201  else
202  checkBox_[i]->setChecked (false);
203  }
204 }
205 
206 //------------------------------------------------------------------------------
207 
210 {
211  if (!multiple_)
212  {
213  if (default_.length ())
214  combo_->setCurrentIndex (combo_->findData (default_[0]));
215  else
216  combo_->setCurrentIndex (0);
217  }
218  else
219  for (int i = 0; i < names_.size (); i++)
220  {
221  if (default_.contains (names_[i].first))
222  checkBox_[i]->setChecked (true);
223  else
224  checkBox_[i]->setChecked (false);
225  }
226 
227 }
228 
229 //------------------------------------------------------------------------------
230 }
static bool strToBool(QString _str)
Converts the given string to bool.
Definition: context.cc:555
SelectionWidget(QMap< QString, QString > &_hints, QString _typeName, QWidget *_parent=NULL)
Constructor.
void toDefault()
Reset to default.
QString toValue()
Convert current value to string.
void fromValue(QString _from)
Read value from string.
~SelectionWidget()
Destructor.