VariantWidget.h
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 ArmarXGui::ArmarXObjects::VariantWidget
17  * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18  * @date 2017
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #pragma once
24 
25 #include <ArmarXCore/interface/observers/VariantBase.h>
28 
29 #include <QWidget>
30 #include <QFormLayout>
31 
33 {
34  class VariantDataWidgetBase;
35 }
36 
37 namespace armarx
38 {
39  class VariantWidget : public QWidget
40  {
41  using VariantDataWidgetBase = VariantDataWidgets::VariantDataWidgetBase;
42  Q_OBJECT
43  public:
44  VariantWidget(QWidget* parent = nullptr);
45  using QWidget::QWidget;
46  public slots:
47  void update(const VariantBasePtr& v);
48  void update(const std::vector<VariantBasePtr>& vec);
49  void update(const std::map<std::string, VariantBasePtr>& map);
50  private:
51  void updateEntry(const std::string& name, const VariantBasePtr& v);
52 
53  enum class Mode
54  {
55  Empty,
56  SingleEntry,
57  Vector,
58  Map
59  };
60  void reset(Mode newMode = Mode::Empty);
61  Mode mode {Mode::Empty};
62 
63  std::map<std::string, VariantDataWidgetBase*> entries;
64  QFormLayout* l;
65  int getEntryRow(VariantDataWidgetBase* entry) const;
66  };
67 }
68 
70 {
71  class VariantDataWidgetBase: public QWidget
72  {
73  Q_OBJECT
74  public:
75  virtual void update(const VariantDataPtr&) {}
76  virtual std::string getTypeName() const
77  {
78  return typeName;
79  }
80  virtual void setTypeName(const std::string& name)
81  {
82  typeName = name;
83  }
84  private:
85  std::string typeName;
86  };
87  using VariantDataWidgetFactory = Registrar<std::function<VariantDataWidgetBase*(const VariantDataPtr&)>>;
88 
89  template<class T>
91  {
92  VariantDataWidgetFactoryRegistration(const std::string& type)
93  {
95  type,
96  [type](const VariantDataPtr & p)
97  {
98  ARMARX_CHECK_EXPRESSION(p) << "Passed VariantDataPtr is null";
99  ARMARX_CHECK_EXPRESSION(p->ice_id() == type) << p->ice_id() << " == " << type;
100  return new T(p);
101  }
102 
103  );
104  }
105  };
106  VariantDataWidgetBase* makeVariantDataWidget(const VariantBasePtr& p);
107 }
armarx::VariantWidget::update
void update(const VariantBasePtr &v)
Definition: VariantWidget.cpp:423
armarx::VariantDataWidgets::VariantDataWidgetBase::getTypeName
virtual std::string getTypeName() const
Definition: VariantWidget.h:76
armarx::VariantBasePtr
::IceInternal::Handle<::armarx::VariantBase > VariantBasePtr
Definition: ManagedIceObject.h:109
armarx::Registrar::registerElement
static void registerElement(const KeyType &key, RegisteredType element)
Registers an element.
Definition: Registrar.h:105
armarx::VariantType::Map
const VariantContainerType Map
Definition: StringValueMap.h:247
armarx::VariantDataWidgets::VariantDataWidgetBase
Definition: VariantWidget.h:71
IceInternal::Handle<::armarx::VariantBase >
Registrar.h
armarx::VariantDataWidgets::VariantDataWidgetBase::update
virtual void update(const VariantDataPtr &)
Definition: VariantWidget.h:75
ExpressionException.h
armarx::VariantDataWidgets::makeVariantDataWidget
VariantDataWidgetBase * makeVariantDataWidget(const VariantBasePtr &p)
Definition: VariantWidget.cpp:535
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::Registrar
Stores key object pairs.
Definition: Registrar.h:62
Eigen::Matrix
Definition: EigenForwardDeclarations.h:27
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::VariantDataWidgets
Definition: VariantWidget.cpp:37
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::VariantDataWidgets::VariantDataWidgetBase::setTypeName
virtual void setTypeName(const std::string &name)
Definition: VariantWidget.h:80
armarx::VariantWidget::VariantWidget
VariantWidget(VariantPtr variant, QWidget *parent=0, Qt::WindowFlags f=0)
Definition: VariantWidget.cpp:31
armarx::VariantDataWidgets::VariantDataWidgetFactoryRegistration
Definition: VariantWidget.h:90
armarx::VariantDataWidgets::VariantDataWidgetFactoryRegistration::VariantDataWidgetFactoryRegistration
VariantDataWidgetFactoryRegistration(const std::string &type)
Definition: VariantWidget.h:92