Developer Documentation
elementInput.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 "elementInput.hh"
46 #include "elementOutput.hh"
47 #include "sceneElement.hh"
48 #include "connectionPoint.hh"
49 #include "connection.hh"
50 #include "graphicsScene.hh"
51 #include "text.hh"
52 #include "../parser/element.hh"
53 #include "../parser/context.hh"
54 #include "../parser/type.hh"
55 #include "../parser/typeWidget.hh"
56 
57 //== NAMESPACES ===============================================================
58 namespace VSI {
59 
60 //=============================================================================
61 //
62 // CLASS VSI::ElementInput - IMPLEMENTATION
63 //
64 //=============================================================================
65 
68  ElementInOut (_in, _parent),
69  in_ (_in),
70  isSet_ (false),
71  value_ (""),
72  forceAsk_ (false)
73 {
74  if (state () & Input::Optional)
75  connectionPointItem ()->setState (ConnectionPoint::Optional);
76  else if (state () & Input::NoUserInput)
77  connectionPointItem ()->setState (ConnectionPoint::NoValue);
78  else
79  connectionPointItem ()->setState (ConnectionPoint::RuntimeInput);
80 
81  VSI::Type *t = inOut ()->element ()->context ()->getType (inOut ()->typeString ());
82 
83  if (t)
84  {
85  TypeWidget *tW = t->widget (inOut ()->hints (), inOut ()->typeString ());
86  if (tW)
87  {
88  value_ = tW->toValue ();
89  delete tW;
90  }
91  }
92 
93  typeTextItem ()->setBackground (false, true);
94  typeTextItem ()->setBackgroundBrush (Qt::gray);
95  typeTextItem ()->setBrush (Qt::black);
96  descriptionTextItem ()->setBackground (false, true);
98  descriptionTextItem ()->setBrush (Qt::black);
99 
100  connectionPointItem ()->setWidth (qMax (typeTextItem ()->preferredHeight (),
101  descriptionTextItem ()->preferredHeight ()));
102 }
103 
104 //------------------------------------------------------------------------------
105 
108 {
109 }
110 
111 //------------------------------------------------------------------------------
112 
115 {
116  if (!connections ().isEmpty ())
117  return valid_;
118 
119  if (isSet () || !(state () & Input::NoRuntimeUserInput) || state () & Input::Optional)
120  return true;
121 
122  return false;
123 }
124 
125 //------------------------------------------------------------------------------
126 
129 {
130  return in_->state ();
131 }
132 
133 //------------------------------------------------------------------------------
134 
137 {
139 
140  // update state
141  connectionPointItem ()->setState (ConnectionPoint::Connected);
142 }
143 
144 //------------------------------------------------------------------------------
145 
148 {
150 
151  // update state
152  if (connections().isEmpty())
153  {
154  if (isSet ())
155  connectionPointItem ()->setState (ConnectionPoint::UserInput);
156  else if (state () & Input::Optional && !forceAsk_)
157  {
158  connectionPointItem ()->setState (ConnectionPoint::Optional);
159  }
160  else if (state () & Input::NoUserInput)
161  {
162  connectionPointItem ()->setState (ConnectionPoint::NoValue);
163  }
164  else
165  connectionPointItem ()->setState (ConnectionPoint::RuntimeInput);
166  }
167 }
168 
169 //------------------------------------------------------------------------------
170 
172 void ElementInput::set(bool _set)
173 {
174  if (isSet_ != _set)
176  isSet_ = _set;
177 
178  // update state
179  if (connections().isEmpty())
180  {
181  if (isSet ())
182  connectionPointItem ()->setState (ConnectionPoint::UserInput);
183  else if (state () & Input::Optional && !forceAsk_)
184  {
185  connectionPointItem ()->setState (ConnectionPoint::Optional);
186  }
187  else if (state () & Input::NoUserInput)
188  {
189  connectionPointItem ()->setState (ConnectionPoint::NoValue);
190  }
191  else
192  connectionPointItem ()->setState (ConnectionPoint::RuntimeInput);
193  }
194 }
195 
196 //------------------------------------------------------------------------------
197 
200 {
201  // Only configured input
202  if ((state () & Input::NoRuntimeUserInput) && (state () & Input::NoExternalInput))
203  return true;
204  return isSet_;
205 }
206 
207 //------------------------------------------------------------------------------
208 
210 void ElementInput::saveToXml(QDomDocument & _doc, QDomElement & _root)
211 {
212  QDomText t;
213 
214  QDomElement main = _doc.createElement("input");
215  _root.appendChild(main);
216  main.setAttribute ("name",in_->name ());
217 
218 
219  QDomElement set = _doc.createElement("is_set");
220  main.appendChild(set);
221  t = _doc.createTextNode(isSet_ ? "true" : "false");
222  set.appendChild(t);
223 
224  if (state () & Input::Optional && forceAsk_)
225  {
226  QDomElement fA = _doc.createElement("force_ask");
227  main.appendChild(fA);
228  t = _doc.createTextNode(isSet_ ? "true" : "false");
229  fA.appendChild(t);
230  }
231 
232  if (isSet_)
233  {
234  QDomElement val = _doc.createElement("value");
235  main.appendChild(val);
236  t = _doc.createTextNode(value_);
237  val.appendChild(t);
238  }
239 
240  foreach (Connection *c, connections ())
241  {
242  QDomElement conn = _doc.createElement("connection");
243  main.appendChild(conn);
244  conn.setAttribute ("element",c->output ()->element ()->element ()->name ());
245  conn.setAttribute ("element_id",QString::number (c->output ()->element ()->id ()));
246  conn.setAttribute ("output",c->output ()->inOut ()->name ());
247  }
248 
249 
250 }
251 
252 //------------------------------------------------------------------------------
253 
255 void ElementInput::loadFromXml(QXmlQuery & _xml)
256 {
257  QString val = Context::getXmlString (_xml, "is_set/string(text())");
258 
259  isSet_ = Context::strToBool (val);
260 
261  if (state () & Input::Optional)
262  {
263  val = Context::getXmlString (_xml, "force_ask/string(text())");
264  forceAsk_ = Context::strToBool (val);
265  }
266 
267  if (isSet_)
268  {
269  value_ = Context::getXmlString (_xml, "value/string(text())");
270  }
271 
272  if (isSet ())
273  connectionPointItem ()->setState (ConnectionPoint::UserInput);
274  else if (state () & Input::Optional && !forceAsk_)
275  {
276  connectionPointItem ()->setState (ConnectionPoint::Optional);
277  }
278  else if (state () & Input::NoUserInput)
279  {
280  connectionPointItem ()->setState (ConnectionPoint::NoValue);
281  }
282  else
283  connectionPointItem ()->setState (ConnectionPoint::RuntimeInput);
284 
285 }
286 
287 //------------------------------------------------------------------------------
288 
290 void ElementInput::setValue(QString _value)
291 {
292  if (value_ != _value)
294  value_ = _value;
295 }
296 
297 //------------------------------------------------------------------------------
298 }
299 
InOut * inOut() const
InOut context object.
~ElementInput()
Destructor.
void set(bool _set)
"Set" flag to mark input as set by user
virtual void addConnection(Connection *_conn)
Add the connection.
virtual TypeWidget * widget(QMap< QString, QString >, QString, QWidget *=NULL)
Returns the configuration widget.
Definition: type.hh:87
bool valid()
Returns state of valid flag (needed during code generation)
unsigned int state()
VSI::Input state passthrough.
void contentChange()
handle content changes
void addConnection(Connection *_conn) override
Add connection.
const Element * element() const
Element of this input/output.
Definition: inout.hh:79
void removeConnection(Connection *_conn) override
Remove connection.
const QString & name() const
Name.
Definition: inout.hh:70
static QString getXmlString(QXmlQuery &_xml, QString _expr, QString _default="")
Gets the string of a xml query.
Definition: context.cc:534
int id()
Unique id for identification.
ConnectionPoint * connectionPointItem() const
Connection point widget.
Definition: elementInOut.hh:89
Type * getType(QString _type)
Get type object for given type name.
Definition: context.cc:590
void setValue(QString _value)
Set to a user value.
Text * descriptionTextItem() const
Short description widget.
Definition: elementInOut.hh:95
static bool strToBool(QString _str)
Converts the given string to bool.
Definition: context.cc:549
bool isSet()
Return "set" flag.
virtual QString toValue()=0
Return the type configuration result to a string.
SceneElement * element()
Scene element.
ElementInput(Input *_in, SceneElement *_parent)
Constructor.
Definition: elementInput.cc:67
Text * typeTextItem() const
Type text widget.
Definition: elementInOut.hh:92
virtual void setBackgroundBrush(QBrush _brush)
Sets the background brush.
Definition: text.cc:379
void loadFromXml(QXmlQuery &_xml)
Load from xml.
Context * context() const
Context of element.
Definition: element.hh:79
virtual void removeConnection(Connection *_conn)
Remove the Connection.
QString name() const
Element name.
Definition: element.hh:82
ElementOutput * output()
Output of this connection.
Definition: connection.cc:248
void setWidth(int _width)
Sets the diameter.
void setState(State _state)
sets the state
Element * element() const
Context VSI::Element.
Definition: sceneElement.hh:88
unsigned int state()
Current state.
Definition: input.cc:69
GraphicsScene * graphicsScene()
Scene.
void setBackground(bool _leftOut, bool _rightOut)
Enables background painting.
Definition: text.cc:300
void saveToXml(QDomDocument &_doc, QDomElement &_root)
Save to xml.
QList< Connection * > connections() const
Connections.
Definition: elementInOut.hh:98