MIRA
VisualizationTextView.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_VISUALIZATIONTEXTVIEW_H_
48 #define _MIRA_VISUALIZATIONTEXTVIEW_H_
49 
50 #include <QTableWidget>
51 #include <QPlainTextEdit>
52 #include <QPushButton>
53 
56 
59 
60 namespace mira {
61 
63 
64 class VisualizationTextItem : public QObject, public IVisualizationTextItem
65 {
66  Q_OBJECT;
67 public:
68 
69  VisualizationTextItem(QTableWidget* t, int row);
70 
71 public slots:
72  void onSetClicked();
73 
74 public:
75 
76  void setName(const std::string& name);
77 
78  void setText(const std::string& text);
79 
80  virtual void setStamped(const Time& timestamp, const std::string& frameID,
81  uint32 sequenceID);
82 
83  int row() const;
84 
85  virtual void enableEdit(bool enable);
86  virtual void registerTextEditRequestCallback(boost::function<void ()> fn);
87  virtual void setSize(const Size2i& size);
88  virtual Size2i getSize() const;
89 
90  QTableWidget* table;
91  QTableWidgetItem* nameItem;
92  QPlainTextEdit* textItem;
93  QTableWidgetItem* timestampItem;
94  QTableWidgetItem* sequenceidItem;
95  QTableWidgetItem* frameidItem;
96  QPushButton* setButton;
97  QWidget* setItem;
98 
99 protected:
100 
101  boost::function<void ()> mEditRequestCb;
102 };
103 
105 
106 template<typename T>
108 {
109 public:
110 
112 
113  DefaultTextVisualizationBase(const std::string& channelName)
114  : Base(channelName), mShowROProperties(true), mStringMapAsObject(true)
115  {}
116 
117  template <typename Reflector>
118  void reflect(Reflector& r) {
120  r.property("ShowReadOnlyProperties", mShowROProperties,
122  "Show objects' read-only properties", true);
123  r.property("StringMapAsObject", mStringMapAsObject,
125  "Serialize any map<string, T> to json object "
126  "(enables simple access to map items by key)", true);
127  }
128 
129  virtual void dataChanged(ChannelRead<T> data)
130  {
131  this->mItem->setStamped(data->timestamp, data->frameID, data->sequenceID);
132 
133  try
134  {
135  json::Value v;
136  try {
140  data.readJSON(v, js);
141  }
142  catch(XNotImplemented&)
143  {
144  data.readJSON(v);
145  }
146  this->mLastValue.reset(v);
147  this->mItem->setText(json::write(v, this->mFormatted, this->mPrecision));
148  }
149  catch(Exception& ex)
150  {
151  int typeID = MIRA_FW.getChannelManager().getTypeId(Base::mChannel.getID());
152  if (typeID < 0)
153  this->mItem->setText(MakeString() << "Conversion to JSON format (from untyped channel's "
154  "serialized data + metadata) failed:\n" << ex.message());
155  else
156  this->mItem->setText(MakeString() << "Serialization to JSON format not supported by "
157  "channel type:\n" << ex.message());
158  }
159  catch(...)
160  {
161  int typeID = MIRA_FW.getChannelManager().getTypeId(Base::mChannel.getID());
162  if (typeID < 0)
163  this->mItem->setText("Conversion to JSON format (from untyped channel's "
164  "serialized data + metadata) failed");
165  else
166  this->mItem->setText("Serialization to JSON format not supported by channel type");
167  }
168  }
169 
170  virtual void setupScene(IVisualizationTextItem* item)
171  {
173  item->enableEdit(true);
174  }
175 
176  virtual void redraw()
177  {
178  try {
179  if (this->mChannel.isValid())
180  dataChanged(this->mChannel.getChannel().read());
181  } catch (...) {}
182  }
183 
184 protected:
187 };
188 
190 
191 template<typename T>
193 {
194 public:
195 
197 
198  TypedDefaultTextVisualizationBase(const std::string& channelName)
199  : Base(channelName)
200  {}
201 
202 public:
203 
204  virtual std::string defaultText()
205  {
209  json::Value value = js.serialize(T());
210  return json::write(value, true, this->mPrecision);
211  }
212 };
213 
215 
216 }
217 
218 #endif
Serializer for serializing objects in JSON format.
Definition: JSONSerializer.h:93
virtual void setStamped(const Time &timestamp, const std::string &frameID, uint32 sequenceID)=0
bool mFormatted
Definition: VisualizationTextBasic.h:189
virtual void setSize(const Size2i &size)
QTableWidget * table
Definition: VisualizationTextView.h:90
DefaultTextVisualizationBase< T > Base
Definition: VisualizationTextView.h:196
std::string getID() const
Return the id of the authority.
Definition: AbstractAuthority.h:119
Definition: VisualizationTextBasic.h:60
Definition: VisualizationTextView.h:107
virtual void registerTextEditRequestCallback(boost::function< void()> fn)
QWidget * setItem
Definition: VisualizationTextView.h:97
Definition: VisualizationText.h:57
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
ChannelProperty< T > mChannel
Definition: VisualizationTextBasic.h:188
Provides a special setter, that simply sets the value of the member to the given value AND calls a us...
#define MIRA_REFLECT_BASE(reflector, BaseClass)
Macro that can be used to reflect the base class easily.
Definition: ReflectorInterface.h:912
boost::function< void()> mEditRequestCb
Definition: VisualizationTextView.h:101
#define MIRA_FW
Macro for accessing the framework instance.
Definition: Framework.h:73
QTableWidgetItem * timestampItem
Definition: VisualizationTextView.h:93
TypedDefaultTextVisualizationBase(const std::string &channelName)
Definition: VisualizationTextView.h:198
Provides MIRA_CLASS_SERIALIZATION macro and includes the major headers of the serialization framework...
DefaultTextVisualizationBase(const std::string &channelName)
Definition: VisualizationTextView.h:113
void serialize(const std::string &name, const T &value, const std::string &comment="")
Serializes the specified object value under the given name.
Definition: Serializer.h:204
An object that allows read access to data of a channel.
Definition: ChannelReadWrite.h:439
QPushButton * setButton
Definition: VisualizationTextView.h:96
IVisualizationTextItem * mItem
Definition: VisualizationTextBasic.h:190
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:416
QPlainTextEdit * textItem
Definition: VisualizationTextView.h:92
MIRA_BASE_EXPORT void write(const Value &value, std::ostream &ioStream, bool formatted=false, int precision=-1)
Writes a json::Value into a given stream using the JSON format.
virtual void setupScene(IVisualizationTextItem *item)
Definition: VisualizationTextView.h:170
Definition: VisualizationTextView.h:192
bool mShowROProperties
Definition: VisualizationTextView.h:185
std::string message() const MIRA_NOEXCEPT_OR_NOTHROW
Similar to what().
virtual void onEdit()
Definition: VisualizationTextBasic.h:133
bool mStringMapAsObject
Definition: VisualizationTextView.h:186
virtual std::string defaultText()
Definition: VisualizationTextView.h:204
void reflect(Reflector &r)
Definition: VisualizationTextView.h:118
virtual void redraw()
Definition: VisualizationTextView.h:176
virtual void dataChanged(ChannelRead< T > data)
Definition: VisualizationTextView.h:129
QTableWidgetItem * frameidItem
Definition: VisualizationTextView.h:95
Definition: VisualizationTextView.h:64
virtual void enableEdit(bool enable)=0
Declaration of VisualizationText and related classes.
Base class for exceptions.
Definition: Exception.h:199
virtual void setText(const std::string &text)=0
virtual void enableEdit(bool enable)
json_spirit::mValue Value
A value is an abstract description of data in JSON (underlying data can either be one of the JSON bas...
Definition: JSON.h:176
virtual void setStamped(const Time &timestamp, const std::string &frameID, uint32 sequenceID)
Setter< T > setterNotify(T &member, boost::function< void()> notifyFn)
Creates a setter that simply sets the value of the specified member to the given value AND calls a us...
Definition: SetterNotify.h:86
QTableWidgetItem * sequenceidItem
Definition: VisualizationTextView.h:94
boost::optional< json::Value > mLastValue
Definition: VisualizationTextBasic.h:192
uint mPrecision
Definition: VisualizationText.h:168
void setName(const std::string &name)
QTableWidgetItem * nameItem
Definition: VisualizationTextView.h:91
void readJSON(JSONValue &oValue)
Definition: ChannelReadWrite.h:531
VisualizationTextBasic< T > Base
Definition: VisualizationTextView.h:111
Default format.
Definition: JSONSerializer.h:102
A base class for simple text visualizations.
void setText(const std::string &text)
VisualizationTextItem(QTableWidget *t, int row)
Serialize associative containers with string keys as JSON object (keys = member names) ...
Definition: JSONSerializer.h:105
virtual void registerTextEditRequestCallback(boost::function< void()> fn)=0
virtual Size2i getSize() const
Class for in-place stream formatting Used for constructs like:
Definition: MakeString.h:63