46 #include <QVBoxLayout> 48 #include <QMessageBox> 50 #include <QGraphicsSceneMouseEvent> 51 #include <QGraphicsLinearLayout> 52 #include <QGraphicsProxyWidget> 53 #include <QGraphicsGridLayout> 54 #include <QGraphicsView> 57 #include <QXmlResultItems> 59 #include "sceneElement.hh" 60 #include "graphicsScene.hh" 64 #include "connectionPoint.hh" 65 #include "connection.hh" 67 #include "elementInput.hh" 68 #include "elementOutput.hh" 69 #include "elementFunction.hh" 71 #include "config/configDialog.hh" 73 #include "../parser/element.hh" 74 #include "../parser/context.hh" 75 #include "../parser/function.hh" 77 #define BACKGROUND_RED 0x00 78 #define BACKGROUND_GREEN 0x00 79 #define BACKGROUND_BLUE 0x00 80 #define BACKGROUND_ALPHA 0xff 82 #define SELECTED_BACKGROUND_RED 0x00 83 #define SELECTED_BACKGROUND_GREEN 0x00 84 #define SELECTED_BACKGROUND_BLUE 0x6f 85 #define SELECTED_BACKGROUND_ALPHA 0xff 103 id_ (_element->getNewId ())
106 bool hasConfig =
false;
108 QGraphicsLinearLayout *layout =
new QGraphicsLinearLayout (Qt::Vertical);
109 layout->setSpacing (0);
111 nameLayout_ =
new QGraphicsLinearLayout (Qt::Vertical);
112 nameLayout_->setSpacing(0);
113 nameLayout_->setContentsMargins(15, 0, 15, 0);
119 QFont font = name_->font ();
120 if (font.pointSize () != -1)
121 font.setPointSize (font.pointSize () + 1);
123 font.setPixelSize (font.pixelSize () + 1);
125 name_->setFont (font);
127 nameLayout_->addItem(name_);
128 nameLayout_->setAlignment (name_, Qt::AlignCenter);
131 font = elementName_->font ();
132 if (font.pointSize () != -1)
133 font.setPointSize (font.pointSize () - 2);
135 font.setPixelSize (font.pixelSize () - 2);
136 elementName_->setFont (font);
138 nameLayout_->addItem(elementName_);
139 nameLayout_->setAlignment (elementName_, Qt::AlignCenter);
141 elementName_->hide ();
143 layout->addItem(nameLayout_);
147 QGraphicsGridLayout *inGrid =
new QGraphicsGridLayout;
148 inGrid->setContentsMargins (0,0,5,0);
158 if (!(in->state () & Input::NoUserInput))
161 configInputs_.append (i);
164 if (in->state () & Input::NoExternalInput)
173 inGrid->addItem (i->
typeTextItem (), row, 1, Qt::AlignLeft | Qt::AlignVCenter);
176 height = qMin (height, i->
typeTextItem ()->preferredHeight () / 2);
177 typeWidth = qMax (i->
typeTextItem ()->preferredWidth (), typeWidth);
181 if (!element_->
inputs ().isEmpty ())
183 inGrid->setColumnMinimumWidth (1, qMin (typeWidth, 100.0));
184 inGrid->setHorizontalSpacing (1);
185 layout->insertItem (index, inGrid);
187 layout->setItemSpacing (index - 1, 10);
192 QGraphicsGridLayout *outGrid =
new QGraphicsGridLayout;
193 outGrid->setContentsMargins (5,0,0,0);
203 outGrid->addItem (o->
typeTextItem (), row, 1, Qt::AlignRight | Qt::AlignVCenter);
206 height = qMin (height, o->
typeTextItem ()->preferredHeight () / 2);
207 typeWidth = qMax (o->
typeTextItem ()->preferredWidth (), typeWidth);
213 if (!element_->
outputs ().isEmpty ())
215 outGrid->setColumnMinimumWidth (1, qMin (typeWidth, 100.0));
216 outGrid->setHorizontalSpacing (1);
217 layout->insertItem(index, outGrid);
218 layout->setAlignment (outGrid, Qt::AlignRight);
219 layout->setItemSpacing (index - 1, 10);
223 if (hasConfig || !element_->
functions ().isEmpty ())
225 QGraphicsLinearLayout *hl =
new QGraphicsLinearLayout (Qt::Horizontal);
226 QGraphicsLinearLayout *vl =
new QGraphicsLinearLayout (Qt::Vertical);
227 hl->setContentsMargins(15, 0, 15, 0);
233 configButton->setBrush (Qt::black);
239 vl->addItem (configButton);
241 connect (configButton, SIGNAL (pressed()),
this, SLOT (showInputConfig()));
250 config->setBrush (Qt::black);
255 vl->addItem (config);
257 connect (config, SIGNAL (pressed()), ef, SLOT (editFunction ()));
259 functions_.append (ef);
266 layout->insertItem (index, hl);
267 layout->setAlignment (hl, Qt::AlignCenter);
268 layout->setItemSpacing (index - 1, 10);
275 QGraphicsGridLayout *sIO =
new QGraphicsGridLayout ();
284 sIO->addItem (dataIn_->
connectionPointItem (), 0, col++, Qt::AlignLeft | Qt::AlignVCenter);
303 sIO->addItem (dataOut_->
connectionPointItem (), 0, col, Qt::AlignRight | Qt::AlignVCenter);
307 sIO->setContentsMargins (lo, 0, ro, 0);
308 sIO->setHorizontalSpacing (1);
310 layout->insertItem (index, sIO);
311 layout->setAlignment (sIO, Qt::AlignCenter);
312 layout->setItemSpacing (index - 1, 10);
316 layout->setItemSpacing (0, 1);
318 layout->setContentsMargins (0, 2, 0, 6);
322 if (layout->preferredWidth() < 200)
323 setMinimumWidth (layout->preferredWidth());
325 setMinimumWidth (200);
327 setZValue (scene_->
getNewZ ());
329 setFlag (QGraphicsItem::ItemIsSelectable,
true);
330 setFlag (QGraphicsItem::ItemIsMovable,
true);
332 setMaximumWidth (200);
358 int w = geometry().width() - 20;
359 int h = geometry().height();
361 _painter->setRenderHint(QPainter::Antialiasing,
true);
363 _painter->setBrush(QBrush(QColor(SELECTED_BACKGROUND_RED,
364 SELECTED_BACKGROUND_GREEN,
365 SELECTED_BACKGROUND_BLUE,
366 SELECTED_BACKGROUND_ALPHA)));
368 _painter->setBrush(QBrush(QColor(BACKGROUND_RED,
373 _painter->setPen (Qt::NoPen);
374 _painter->drawRoundedRect(10, 0, w, h, 5, 5);
383 path.moveTo (10, 20);
385 path.arcTo (10, 0, 10, 10, 180, -90);
386 path.lineTo (w + 6, 0);
387 path.arcTo (w, 0, 10, 10, 90, -90);
388 path.lineTo (w + 10, 10);
390 path.cubicTo (QPointF (10 + wH, 10), QPointF (10 + wH, 20), QPointF (10, 20));
392 rG.setCenter (10 + wT, 0);
393 rG.setFocalPoint(10 + wT, 0);
395 rG.setColorAt(0, QColor (255, 255, 255, 128));
396 rG.setColorAt(1, Qt::transparent);
397 _painter->setBrush (rG);
398 _painter->setPen (Qt::NoPen);
399 _painter->drawPath(path);
401 path = QPainterPath ();
402 path.moveTo (w + 5, 0);
403 path.arcTo (w, 0, 10, 10, 90, -90);
404 path.lineTo (w + 10, h - 5);
405 path.lineTo (w + 5, h - 5);
406 path.lineTo (w + 5, 0);
408 lG.setStart (w + 5, 0);
409 lG.setFinalStop(w + 10, 0);
410 lG.setColorAt(0, Qt::transparent);
411 lG.setColorAt(1, QColor (255, 255, 255, 128));
412 _painter->setBrush (lG);
413 _painter->drawPath(path);
415 path = QPainterPath ();
416 path.moveTo (10, h - 5);
417 path.arcTo (10, h - 10, 10, 10, 180, 90);
418 path.lineTo (w + 5, h);
419 path.lineTo (w + 5, h - 5);
420 path.lineTo (10, h - 5);
421 lG.setStart (0, h - 5);
422 lG.setFinalStop (0, h);
423 _painter->setBrush (lG);
424 _painter->drawPath (path);
426 path = QPainterPath ();
427 path.moveTo (w + 5, h);
428 path.arcTo (w, h - 10, 10, 10, -90, 90);
429 path.lineTo (w + 5, h - 5);
430 path.lineTo (w + 5, h);
431 rG.setCenter (w + 5, h - 5);
432 rG.setFocalPoint(w + 5, h - 5);
434 rG.setColorAt (0, Qt::transparent);
435 rG.setColorAt (1, QColor (255, 255, 255, 128));
436 _painter->setBrush (rG);
437 _painter->drawPath(path);
439 QGraphicsWidget::paint (_painter, _option, _widget);
445 void SceneElement::mousePressEvent (QGraphicsSceneMouseEvent *_event)
447 QGraphicsWidget::mousePressEvent (_event);
448 setZValue (scene_->
getNewZ ());
454 void SceneElement::mouseMoveEvent (QGraphicsSceneMouseEvent *_event)
456 QGraphicsWidget::mouseMoveEvent (_event);
462 void SceneElement::mouseReleaseEvent (QGraphicsSceneMouseEvent *_event)
464 QGraphicsWidget::mouseReleaseEvent (_event);
473 QPointF _scenePos = _event->scenePos ();
474 if (name_->
boundingRect ().contains (name_->mapFromScene (_scenePos)) ||
475 elementName_->
boundingRect ().contains (elementName_->mapFromScene (_scenePos)))
477 ConfigDialog d (QVector<ElementInput *> (), name_->
text (), scene ()->views ()[0]);
478 d.setWindowTitle (
"Element Name Configuration");
479 if (d.exec () == QDialog::Accepted)
481 if (d.name ().isEmpty())
487 elementName_->hide ();
489 elementName_->show ();
491 nameLayout_->invalidate ();
512 if (configInputs_.contains (i))
514 QVector<ElementInput *>
inputs;
517 ConfigDialog d (inputs, QString (), scene ()->views ()[0]);
522 QMessageBox msgBox (scene ()->views ()[0]);
523 msgBox.setText(
"No configuration available.");
533 void SceneElement::moveEvent (QGraphicsSceneMoveEvent *_event)
535 QGraphicsWidget::moveEvent (_event);
566 code_ = element_->
code ();
574 QString regex =
"\\[\\s*" + _name +
"\\s*=\\s*\"" + _id +
"\"\\s*\\]";
575 code_ = code_.replace (QRegExp (regex), _value);
583 return element_->
name () +
"_" + QString::number (id_);
589 void SceneElement::showInputConfig()
592 d.setWindowTitle (name_->
text () +
" Input Configuration");
593 if (d.exec () == QDialog::Accepted)
595 if (d.name ().isEmpty())
601 elementName_->hide ();
603 elementName_->show ();
605 nameLayout_->invalidate ();
670 void VSI::SceneElement::invalidateConnections()
697 QDomElement main = _doc.createElement(
"element");
698 _root.appendChild(main);
699 main.setAttribute (
"name",element_->
name ());
703 QDomElement name = _doc.createElement(
"visible_name");
704 main.appendChild(name);
705 t = _doc.createTextNode(name_->
text ());
709 QDomElement
id = _doc.createElement(
"id");
710 main.appendChild(
id);
711 t = _doc.createTextNode(QString::number (id_));
714 QDomElement px = _doc.createElement(
"x");
715 main.appendChild(px);
716 t = _doc.createTextNode(QString::number (pos ().x ()));
719 QDomElement
py = _doc.createElement(
"y");
720 main.appendChild(py);
721 t = _doc.createTextNode(QString::number (pos ().y ()));
724 if (!inputs_.isEmpty() || dataIn_)
726 QDomElement
inputs = _doc.createElement(
"inputs");
727 main.appendChild(inputs);
733 if (!functions_.isEmpty ())
735 QDomElement
functions = _doc.createElement(
"functions");
736 main.appendChild(functions);
758 elementName_->hide ();
760 elementName_->show ();
762 nameLayout_->invalidate ();
766 id = val.toUInt (&ok1);
775 x = val.toDouble (&ok1);
778 y = val.toDouble (&ok2);
785 _xml.setQuery (
"inputs/input");
791 _xml.evaluateTo (&i);
792 QXmlItem item (i.next ());
793 while (!item.isNull ())
802 if (val ==
"data" && dataIn_)
820 _xml.setQuery (
"functions/function");
824 _xml.evaluateTo (&i);
825 QXmlItem item (i.next ());
826 while (!item.isNull ())
const QString & name() const
Name.
void resetCodeGeneration()
Reset code block for code generation.
void setBackgroundBrush(QBrush _brush)
Sets the background brush.
void contentChange()
handle content changes
void setValid(bool _value)
Sets the valid flag (needed during code generation)
virtual QRectF boundingRect() const
Bounding rectangle.
QString name() const
Name.
void replaceCodeBlock(QString _name, QString _id, QString _value)
Replace block with name _name and id _id with _value.
const QString & shortDescription() const
Short description.
QList< Connection * > connections() const
Connections.
QString text()
Get displayed text.
Output * dataOut()
Scenegraph output.
Input * dataIn()
Scenegraph input.
void setBackground(bool _leftOut, bool _rightOut)
Enables background painting.
ElementOutput * output()
Output of this connection.
~SceneElement()
Destructor.
void loadFromXml(QXmlQuery &_xml)
Load from xml.
void saveToXml(QDomDocument &_doc, QDomElement &_root)
Save to xml.
void setText(QString _text)
Set displayed text.
ConnectionPoint * connectionPointItem() const
Connection point widget.
QString name() const
Element name.
qreal getNewZ()
Returns a new Z value that is above all elements.
void updatePositions()
called to update position on element movement
void saveToXml(QDomDocument &_doc, QDomElement &_root)
Save to xml.
const QString & longDescription() const
Long description.
const QString & shortDescription() const
Short description.
Text * descriptionTextItem() const
Short description widget.
void paint(QPainter *_painter, const QStyleOptionGraphicsItem *_option, QWidget *_widget=0)
Background painting.
QString variableId()
Unique variable name for code generation.
Text * typeTextItem() const
Type text widget.
bool isBefore(SceneElement *_e)
Will this element be executed before _e bacause of its connections?
void setHorizontalStretch(bool _stretch)
Should this widget be stretchable in horizontal direction.
QVector< ElementFunction * > functions()
Functions.
bool isAfter(SceneElement *_e)
Will this element be executed after _e bacause of its connections?
const QVector< Output * > & outputs() const
Outputs.
Function * function()
Function class.
const QVector< Input * > & inputs() const
Inputs.
SceneElement * element()
Scene element.
void setAlignment(Qt::Alignment _alignment)
Placement of the text in a stretched widget.
void mouseRelease(QPointF _pos, QGraphicsItem *_item)
Redirect mouse release to tools area.
void loadFromXml(QXmlQuery &_xml)
Load from xml.
QString code() const
Code segment.
QPainterPath shape() const
Returns the shape for proper repainting/event handling.
void saveToXml(QDomDocument &_doc, QDomElement &_root)
Save to xml.
void invalidate()
invalidate way
const QString & shortDescription() const
Short description.
ElementInput * input()
Input of this connection.
QVector< ElementInput * > inputs()
Inputs.
SceneElement(GraphicsScene *_scene, Element *_element)
Constructor.
void loadFromXml(QXmlQuery &_xml)
Load from xml.
void setMinId(unsigned int _id)
sets the minimum for an unused id
InOut * inOut() const
InOut context object.
ElementOutput * dataOut()
Scene output.
static QString getXmlString(QXmlQuery &_xml, QString _expr, QString _default="")
Gets the string of a xml query.
const QVector< Function * > & functions() const
Functions.
int id()
Unique id for identification.
ElementInput * dataIn()
Scene input.
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *_event)
Double click occured. We can't use mouseDoubleClickEvent because we won't get one for the ConecctionP...