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
67 ElementInput::ElementInput (Input *_in, SceneElement *_parent) :
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);
97 descriptionTextItem ()->setBackgroundBrush (Qt::white);
98 descriptionTextItem ()->setBrush (Qt::black);
99
100 connectionPointItem ()->setWidth (qMax (typeTextItem ()->preferredHeight (),
101 descriptionTextItem ()->preferredHeight ()));
102 }
103
104 //------------------------------------------------------------------------------
105
107 ElementInput::~ElementInput ()
108 {
109 }
110
111 //------------------------------------------------------------------------------
112
114 bool ElementInput::valid()
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
128 unsigned int VSI::ElementInput::state()
129 {
130 return in_->state ();
131 }
132
133 //------------------------------------------------------------------------------
134
136 void ElementInput::addConnection(Connection * _conn)
137 {
138 ElementInOut::addConnection (_conn);
139
140 // update state
141 connectionPointItem ()->setState (ConnectionPoint::Connected);
142 }
143
144 //------------------------------------------------------------------------------
145
147 void ElementInput::removeConnection (Connection * _conn)
148 {
149 ElementInOut::removeConnection (_conn);
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)
175 element ()->graphicsScene ()->contentChange ();
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
199 bool ElementInput::isSet ()
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)
293 element ()->graphicsScene ()->contentChange ();
294 value_ = _value;
295 }
296
297 //------------------------------------------------------------------------------
298 }
299
VSI::ElementInOut::inOut
InOut * inOut() const
InOut context object.
Definition: elementInOut.hh:110
VSI::ElementInput::~ElementInput
~ElementInput()
Destructor.
Definition: elementInput.cc:107
VSI::ElementInput::set
void set(bool _set)
"Set" flag to mark input as set by user
Definition: elementInput.cc:172
VSI::ElementInOut::addConnection
virtual void addConnection(Connection *_conn)
Add the connection.
Definition: elementInOut.cc:156
VSI::Type::widget
virtual TypeWidget * widget(QMap< QString, QString >, QString, QWidget *=NULL)
Returns the configuration widget.
Definition: type.hh:87
VSI::ElementInput::valid
bool valid()
Returns state of valid flag (needed during code generation)
Definition: elementInput.cc:114
VSI::ElementInput::state
unsigned int state()
VSI::Input state passthrough.
Definition: elementInput.cc:128
VSI::Type
Definition: type.hh:65
VSI::GraphicsScene::contentChange
void contentChange()
handle content changes
Definition: graphicsScene.cc:1107
VSI::ElementInOut
Definition: elementInOut.hh:69
VSI::ElementInput::addConnection
void addConnection(Connection *_conn) override
Add connection.
Definition: elementInput.cc:136
VSI::InOut::element
const Element * element() const
Element of this input/output.
Definition: inout.hh:79
VSI::ElementInput::removeConnection
void removeConnection(Connection *_conn) override
Remove connection.
Definition: elementInput.cc:147
VSI::InOut::name
const QString & name() const
Name.
Definition: inout.hh:70
VSI::Context::getXmlString
static QString getXmlString(QXmlQuery &_xml, QString _expr, QString _default="")
Gets the string of a xml query.
Definition: context.cc:534
VSI::SceneElement::id
int id()
Unique id for identification.
Definition: sceneElement.hh:115
VSI::Connection
Definition: connection.hh:66
VSI::ElementInOut::connectionPointItem
ConnectionPoint * connectionPointItem() const
Connection point widget.
Definition: elementInOut.hh:89
VSI::Context::getType
Type * getType(QString _type)
Get type object for given type name.
Definition: context.cc:590
VSI::ElementInput::setValue
void setValue(QString _value)
Set to a user value.
Definition: elementInput.cc:290
VSI::ElementInOut::descriptionTextItem
Text * descriptionTextItem() const
Short description widget.
Definition: elementInOut.hh:95
VSI::Context::strToBool
static bool strToBool(QString _str)
Converts the given string to bool.
Definition: context.cc:549
VSI::ElementInput::isSet
bool isSet()
Return "set" flag.
Definition: elementInput.cc:199
VSI::TypeWidget::toValue
virtual QString toValue()=0
Return the type configuration result to a string.
VSI::ElementInOut::element
SceneElement * element()
Scene element.
Definition: elementInOut.hh:113
VSI::ElementInput::ElementInput
ElementInput(Input *_in, SceneElement *_parent)
Constructor.
Definition: elementInput.cc:67
VSI::ElementInOut::typeTextItem
Text * typeTextItem() const
Type text widget.
Definition: elementInOut.hh:92
VSI::TypeWidget
Definition: typeWidget.hh:59
VSI::Text::setBackgroundBrush
virtual void setBackgroundBrush(QBrush _brush)
Sets the background brush.
Definition: text.cc:379
VSI::ElementInput::loadFromXml
void loadFromXml(QXmlQuery &_xml)
Load from xml.
Definition: elementInput.cc:255
VSI::Element::context
Context * context() const
Context of element.
Definition: element.hh:79
VSI::ElementInOut::removeConnection
virtual void removeConnection(Connection *_conn)
Remove the Connection.
Definition: elementInOut.cc:164
VSI::Element::name
QString name() const
Element name.
Definition: element.hh:82
VSI::Connection::output
ElementOutput * output()
Output of this connection.
Definition: connection.cc:248
VSI::ConnectionPoint::setWidth
void setWidth(int _width)
Sets the diameter.
Definition: connectionPoint.cc:228
VSI
Definition: baseWidget.cc:72
VSI::ConnectionPoint::setState
void setState(State _state)
sets the state
Definition: connectionPoint.cc:153
VSI::Input
Definition: input.hh:60
VSI::SceneElement::element
Element * element() const
Context VSI::Element.
Definition: sceneElement.hh:88
VSI::Input::state
unsigned int state()
Current state.
Definition: input.cc:69
VSI::SceneElement
Definition: sceneElement.hh:73
VSI::SceneElement::graphicsScene
GraphicsScene * graphicsScene()
Scene.
Definition: sceneElement.hh:133
VSI::Text::setBackground
void setBackground(bool _leftOut, bool _rightOut)
Enables background painting.
Definition: text.cc:300
VSI::ElementInput::saveToXml
void saveToXml(QDomDocument &_doc, QDomElement &_root)
Save to xml.
Definition: elementInput.cc:210
VSI::ElementInOut::connections
QList< Connection * > connections() const
Connections.
Definition: elementInOut.hh:98