VariantWidget.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarX::Gui
17 * @author Kai Welke (welke@kit.edu)
18 * @copyright 2012 Humanoids Group, IAIM, IFA
19 * @license http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "VariantWidget.h"
24 #include <QLineEdit>
25 #include <QLabel>
26 #include <QTextEdit>
27 #include <QVBoxLayout>
28 
29 namespace armarx
30 {
31  VariantWidget::VariantWidget(VariantPtr variant, QWidget* parent, Qt::WindowFlags f)
32  : QWidget(parent, f)
33  {
34  this->variant = variant;
35  enableEdit = true;
36  showType = false;
37  layout = new QVBoxLayout(this);
38  variantEdit = new QTextEdit(this);
39  setLayout(layout);
40  setupLayout();
41  }
42 
43  void VariantWidget::setValue(const VariantPtr& variant)
44  {
45  VariantTypeId type = variant->getType();
46  QString valueString;
47  if (variant->getInitialized())
48  {
49  if (type == VariantType::Bool)
50  {
51  QString value("%1");
52  value = value.arg((variant->getBool() == 1));
53  valueString = value;
54  }
55 
56  else if (type == VariantType::Float)
57  {
58  QString value("%1");
59  value = value.arg(variant->getFloat(), 0, 'f', 2);
60  valueString = value;
61  }
62 
63  else if (type == VariantType::Double)
64  {
65  QString value("%1");
66  value = value.arg(variant->getDouble(), 0, 'f', 2);
67  valueString = value;
68  }
69 
70  else if (type == VariantType::Int)
71  {
72  QString value("%1");
73  value = value.arg(variant->getInt());
74  valueString = value;
75  }
76 
77  else if (type == VariantType::Long)
78  {
79  QString value("%1");
80  value = value.arg(variant->getLong());
81  valueString = value;
82  }
83 
84  else if (type == VariantType::String)
85  {
86  QString value(variant->getString().c_str());
87  valueString = value;
88  }
89  else
90  {
91  VariantDataClassPtr var = variant->getClass<VariantDataClass>();
92  QString value(var->output().c_str());
93  valueString = value;
94  }
95 
96  }
97  else
98  {
99  valueString = "<i>not initalized</i>";
100  }
101  variantEdit->setEnabled(variant->getInitialized());
102  variantEdit->setText(valueString);
103  }
104 
105  void VariantWidget::setEnableEdit(bool enableEdit)
106  {
107  this->enableEdit = enableEdit;
108  setupLayout();
109  }
110 
111  void VariantWidget::setShowType(bool showType)
112  {
113  this->showType = showType;
114  setupLayout();
115  }
116 
118  {
119  return enableEdit;
120  }
121 
123  {
124  return showType;
125  }
126 
128  {
129  return variantEdit->toPlainText();
130  }
131 
132 
133  void VariantWidget::setupLayout()
134  {
135  int valueEditWidth = 120;
136 
137 
138  // clean old lineedits
139  std::vector<QWidget*>::iterator iter = widgets.begin();
140 
141  while (iter != widgets.end())
142  {
143  delete *iter;
144  iter++;
145  }
146  widgets.clear();
147  int x_start = 0;
148  int y_start = 0;
149  int x_gap = 10;
150 
151 
152 
153  layout->addWidget(variantEdit);
154  variantEdit->setGeometry(x_start + 0 * (valueEditWidth + x_gap), y_start, valueEditWidth, 20);
155  variantEdit->setReadOnly(!enableEdit);
156 
157  setValue(variant);
158 
159 
160 
161  if (showType)
162  {
163  int x = x_start + 1 * (valueEditWidth + x_gap);
164  QLabel* label = new QLabel(this);
165  label->setTextInteractionFlags(Qt::TextSelectableByMouse);
166  label->setGeometry(x, y_start, 120, 21);
167  label->setText(variant->getTypeName().c_str());
168  layout->addWidget(label);
169 
170  widgets.push_back(label);
171  }
172  }
173 
175  {
176  // NYI
177  }
178 }
armarx::VariantWidget::getShowType
bool getShowType()
Definition: VariantWidget.cpp:122
VariantWidget.h
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
armarx::VariantWidget::setShowType
void setShowType(bool showType)
Definition: VariantWidget.cpp:111
armarx::VariantType::Bool
const VariantTypeId Bool
Definition: Variant.h:915
armarx::VariantWidget::setValue
void setValue(const VariantPtr &variant)
Definition: VariantWidget.cpp:43
IceInternal::Handle< Variant >
armarx::VariantType::Double
const VariantTypeId Double
Definition: Variant.h:919
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::VariantWidget::getEnableEdit
bool getEnableEdit()
Definition: VariantWidget.cpp:117
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::VariantTypeId
Ice::Int VariantTypeId
Definition: Variant.h:44
armarx::VariantWidget::lineEdit_editFinished
void lineEdit_editFinished()
Definition: VariantWidget.cpp:174
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
armarx::VariantWidget::setEnableEdit
void setEnableEdit(bool enableEdit)
Definition: VariantWidget.cpp:105
armarx::VariantType::String
const VariantTypeId String
Definition: Variant.h:920
armarx::VariantWidget::getValueText
QString getValueText()
Definition: VariantWidget.cpp:127
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::VariantWidget::VariantWidget
VariantWidget(VariantPtr variant, QWidget *parent=0, Qt::WindowFlags f=0)
Definition: VariantWidget.cpp:31