MIRA
VisualizationTextBasic.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_VISUALIZATIONTEXTBASIC_H_
48 #define _MIRA_VISUALIZATIONTEXTBASIC_H_
49 
52 
54 
55 namespace mira {
56 
58 
59 template<typename T>
61 {
62 public:
63 
64  VisualizationTextBasic(const std::string& channelName) :
65  mName(channelName),
66  mFormatted(true),
67  mItem(NULL)
68  {
69  mChannel.setDataChangedCallback(boost::bind(&VisualizationTextBasic::dataChanged, this, _1));
70  mChannel.setChannelChangedCallback(boost::bind(&VisualizationTextBasic::channelChanged, this));
71  }
72 
74  {
75  if (getSite() && mItem)
77  }
78 
79  template <typename Reflector>
80  void reflect(Reflector& r)
81  {
84  r.property("Formatted", mFormatted,
86  "Formatted json output", true);
87  }
88 
89  virtual void setupScene(IVisualizationTextSite* site)
90  {
91  mItem = site->addItem();
92  mItem->setName(mChannel.getID());
95  }
96 
97  virtual void setSize(const Size2i& size)
98  {
100  if (mItem)
101  mItem->setSize(size);
102  }
103 
104  virtual Size2i getSize() const
105  {
106  if (mItem)
107  return mItem->getSize();
108  return mSize;
109  }
110 
111  virtual void setupScene(IVisualizationTextItem* item) {}
112 
114 
115 protected:
116 
117  virtual void channelChanged()
118  {
119  try {
120  this->setName(mChannel.getID());
121  }
122  catch (XRuntime&) {}
123  if (!mItem)
124  return;
125  mItem->setName(mChannel.getID());
126  mItem->setText("");
127  }
128 
129  virtual void dataChanged(ChannelRead<T> data) {}
130 
131  virtual std::string defaultText() { return ""; }
132 
133  virtual void onEdit()
134  {
135  if (mChannel.isValid())
136  {
137  MultiInputDialog dialog("Add new channel data", NULL);
138  dialog.addInputField("time", "Timestamp", toString(Time::now()).c_str());
139  dialog.addInputField("frame", "FrameID", "");
140  dialog.addInputField("sequence", "SequenceID", "0");
141  if (mLastValue)
142  dialog.addInputField("value", "Value", json::write(*mLastValue, true, mPrecision).c_str(), true);
143  else
144  dialog.addInputField("value", "Value", defaultText().c_str(), true);
145  if (dialog.exec() == 0)
146  return;
147 
148  json::Value value;
149  json::read(dialog.getInput("value").toStdString(), value);
150  writeJSON(boost::posix_time::time_from_string(dialog.getInput("time").toStdString()),
151  dialog.getInput("frame").toStdString(),
152  fromString<int>(dialog.getInput("sequence").toStdString()),
153  value);
154  }
155  }
156 
157  virtual void redraw()
158  {
159  if (mLastValue)
161  }
162 
163  virtual void writeJSON(const Time& timestamp, const std::string& frameID,
164  int sequenceID, const json::Value& value)
165  {
166  auto internalChannel = this->mChannel.getChannel();
167  ConcreteChannel<void>* channel = MIRA_FW.getChannelManager().publish<void>(
168  internalChannel.getID(), getGlobalID(), false, internalChannel.getTypename());
169  ChannelWrite<void> cw = channel->write();
170 
171  cw->timestamp = timestamp;
172  cw->frameID = frameID;
173  cw->sequenceID = sequenceID;
174 
175  try {
177  cw.writeJSON(jds);
178  }
179  catch (XNotImplemented&) {
180  cw.writeJSON(value);
181  }
182  }
183 
184  virtual IVisualizationTextItem* getItem() { return mItem; }
185 
186 protected:
187  std::string mName;
191 
192  boost::optional<json::Value> mLastValue;
193 };
194 
196 
197 }
198 
199 #endif
virtual void channelChanged()
Definition: VisualizationTextBasic.h:117
bool mFormatted
Definition: VisualizationTextBasic.h:189
virtual void setupScene(IVisualizationTextSite *site)
Must be implemented in derived classes.
Definition: VisualizationTextBasic.h:89
Definition: VisualizationTextBasic.h:60
Abstract base class for all text visualization.
Definition: VisualizationText.h:92
void setName(const std::string &name)
Sets the name of this visualization.
Definition: Visualization.h:157
virtual void setName(const std::string &name)=0
virtual IVisualizationTextItem * addItem()=0
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...
An object that allows exclusive write access to data of a channel.
Definition: ChannelReadWrite.h:652
virtual void setSize(const Size2i &size)
#define MIRA_REFLECT_BASE(reflector, BaseClass)
Macro that can be used to reflect the base class easily.
Definition: ReflectorInterface.h:912
void channelProperty(Reflector &r, const std::string &name, ChannelProperty< T > &channel, const std::string &comment, OptionalMode optional=REQUIRED)
Special property-method that should be called within the reflect method to specify a ChannelProperty...
Definition: Visualization.h:249
#define MIRA_FW
Macro for accessing the framework instance.
Definition: Framework.h:74
virtual Size2i getSize() const =0
An object that allows read access to data of a channel.
Definition: ChannelReadWrite.h:494
IVisualizationTextItem * mItem
Definition: VisualizationTextBasic.h:190
The data structure that is returned by getDataConnection.
Definition: Visualization.h:183
virtual void destroyItem(IVisualizationTextItem *item)=0
std::string toString(const T &value, int precision=-1)
Converts any data type to string (the data type must support the stream << operator).
Definition: ToString.h:256
virtual DataConnection getDataConnection()
Returns information about the direct data connection for this visualization.
Definition: VisualizationTextBasic.h:113
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:418
virtual void redraw()
Definition: VisualizationTextBasic.h:157
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.
Deserializer for serializing objects from JSON format.
Definition: JSONSerializer.h:400
std::string getGlobalID() const
Return the fully qualified global id (includes namespace)
Definition: AbstractAuthority.h:111
virtual std::string defaultText()
Definition: VisualizationTextBasic.h:131
VisualizationTextBasic(const std::string &channelName)
Definition: VisualizationTextBasic.h:64
virtual void setupScene(IVisualizationTextItem *item)
Definition: VisualizationTextBasic.h:111
virtual void onEdit()
Definition: VisualizationTextBasic.h:133
virtual IVisualizationTextItem * getItem()
Get the visualization item.
Definition: VisualizationTextBasic.h:184
std::string mName
Definition: VisualizationTextBasic.h:187
MIRA_BASE_EXPORT void read(const std::string &s, Value &oValue)
Read a json::Value from a string that contains JSON format.
virtual void dataChanged(ChannelRead< T > data)
Definition: VisualizationTextBasic.h:129
Dialog with configurable amount of input fields.
Declaration of VisualizationText and related classes.
virtual void setText(const std::string &text)=0
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
QString getInput(const QString &name)
Accept associative containers with string keys as JSON object (keys = member names), in ADDITION to the standard array format (i.e.
Definition: JSONSerializer.h:416
The concrete typed ChannelProperty template class.
Definition: ChannelProperty.h:214
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
void addInputField(const QString &name, const QString &description, const QString &data, bool multiLine=false)
virtual Size2i getSize() const
Definition: VisualizationTextBasic.h:104
boost::optional< json::Value > mLastValue
Definition: VisualizationTextBasic.h:192
Definition: MultiInputDialog.h:62
Size2i mSize
Definition: VisualizationText.h:167
static Time now() static Time eternity()
Returns the current utc based time.
Definition: Time.h:481
uint mPrecision
Definition: VisualizationText.h:168
virtual void setSize(const Size2i &size)=0
virtual ~VisualizationTextBasic()
Definition: VisualizationTextBasic.h:73
void reflect(Reflector &r)
Definition: VisualizationTextBasic.h:80
virtual void setSize(const Size2i &size)
Definition: VisualizationTextBasic.h:97
virtual IVisualizationTextSite * getSite()
Returns a pointer to the IVisualizationTextSite interface that was set in the init() method...
virtual void writeJSON(const Time &timestamp, const std::string &frameID, int sequenceID, const json::Value &value)
Definition: VisualizationTextBasic.h:163
Definition: VisualizationText.h:77