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