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