tuchulcha  0.10.1
Graphical Workflow Configuration Editor
Node.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011 Jonathan Wuest
2 
3  This file is part of Tuchulcha.
4 
5  Tuchulcha is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Tuchulcha is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with Tuchulcha. If not, see <http://www.gnu.org/licenses/>.
17 */
24 #include "Node.h"
25 #include "NodeHandler.h"
26 #include "GraphModel.h"
27 #include <QFile>
28 #include <QTextStream>
29 #include <QPainter>
30 #include <QPainterPath>
31 #include <QBrush>
32 
33 unsigned int Node::_idCount = 0;
34 
35 Node::Node(const ParameterFileModel* pFile, QString title,
36  int xpos, int ypos) :
37  QGraphicsItem(),
38  _instanceName(title),
39  _width(100),
40  _height(50),
41  _nProps(0),
42  _selectedNode(false),
43  _active(true),
44  _id(_idCount++),
45  _pFile(pFile)
46 {
47  setPos(xpos,ypos);
48  setFlag(QGraphicsItem::ItemIsMovable);
49  setFlag(QGraphicsItem::ItemIsSelectable);
50 }
51 
52 void Node::setId(unsigned int id) {
53  if (id >= Node::_idCount)
54  Node::_idCount = id+1;
55  _id = id;
56 }
57 void Node::setActive(bool activeStatus){
58  _active = activeStatus;
59 }
60 
61 void Node::setName(QString name) {
62  _instanceName = name;
63  _checkWidth();
64 }
65 
67  return _instanceName;
68 }
69 
70 void Node::addProperty(QString name, bool input) {
71  NodeProperty* prop = new NodeProperty(
72  this,name,_nProps,input,_pFile);
73  _height += 25;
74  _properties.insert(name.toLower(),prop);
75  _nProps++;
76  _checkWidth(name.size());
77 }
78 
79 void Node::addProperty(QString name,QString displayname, bool input) {
80  NodeProperty* prop = new NodeProperty(
81  this,name,displayname,_nProps,input,_pFile);
82  _height += 25;
83  _properties.insert(name.toLower(),prop);
84  _nProps++;
85  _checkWidth(name.size());
86 }
87 
88 QRectF Node::boundingRect() const {
89  QRectF bb(0,0,_width,_height);
90  bb.adjust(-1,-1,+1,+1);
91  return bb;
92 }
93 
94 void Node::setSelectedNode(bool s) {
95  _selectedNode = s;
96  if(_selectedNode){
97  changeConnectionLineColor(Qt::blue);
98  }
99  else{
100  changeConnectionLineColor(Qt::black);
101  }
102 }
103 
105  return _selectedNode;
106 }
107 
108 void Node::changeConnectionLineColor(QColor lineColor){
109  QMapIterator<QString,NodeProperty*> i(_properties);
110  NodeProperty* curProp;
111  while(i.hasNext()){
112  curProp = i.next().value();
113  curProp->changeConnectionLineColor(lineColor,false);
114  }
115 }
116 
117 void Node::mouseMoveEvent(QGraphicsSceneMouseEvent* ev) {
118  QGraphicsItem::mouseMoveEvent(ev);
119 
120  qreal dx = (ev->scenePos()-ev->lastScenePos()).x();
121  qreal dy = (ev->scenePos()-ev->lastScenePos()).y();
122  foreach (QGraphicsItem* cur, childItems()) {
123  NodeProperty *np = dynamic_cast<NodeProperty*>(cur);
124  if(np != 0) {
125  np->moveBy(dx,dy);
126  }
127  }
128 
129  // update model
130  NodeHandler* handler = qobject_cast<NodeHandler*>(scene());
131  Q_ASSERT(handler);
132  GraphModel* model = handler->model();
133  QString posString = QString("%1 %2").arg(pos().x()).arg(pos().y());
134  model->setValue(_instanceName+".editorinfo",posString);
135 }
136 
138  QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*) {
139  painter->setPen(QPen(Qt::black,1.f));
140  painter->setOpacity(1);
141  painter->setBrush(_selectedNode ? Qt::blue : Qt::gray);
142  if (!(_active)){
143  painter->setBrush(Qt::red);
144  }
145  painter->drawRoundedRect(0,0,_width,_height,10,10);
146  painter->setBrush(_selectedNode ? QColor("#AAF") : QColor("#BBC"));
147  painter->drawRoundedRect(0,0,_width,22,10,10);
148  painter->drawRoundedRect(0,_height-22,_width,22,10,10);
149  painter->setPen(Qt::black);
150 
151  QFont f = painter->font();
152  f.setPixelSize(15);
153  f.setBold(true);
154  f.setCapitalization(QFont::SmallCaps);
155  painter->setFont(f);
156  painter->drawText(0,0,_width,22,Qt::AlignCenter,_className);
157  f.setCapitalization(QFont::MixedCase);
158  painter->setFont(f);
159  painter->drawText(0,_height-22,_width,22,Qt::AlignCenter,_instanceName);
160 }
161 
162 unsigned int Node::getId() const {
163  return _id;
164 }
165 
166 int Node::getWidth() const {
167  return _width;
168 }
169 
170 int Node::getHeight() const {
171  return _height;
172 }
173 
174 void Node::_checkWidth(int s) {
175  s = qMax(s,qMax(_instanceName.size(), _className.size()));
176  _width = qMax(_width, 50 + s * 7);
177 }
178 
179 NodeProperty* Node::getProperty(QString propName) const {
180  return _properties[propName];
181 }
182 
183 void Node::setClassName(QString modname) {
184  _className = modname;
185  _checkWidth();
186 }
187 
188 QString Node::getClassName() const {
189  return _className;
190 }
191 
192 
void setName(QString name)
resets the name of the node
Definition: Node.cpp:61
void setSelectedNode(bool s)
sets the node selected or not
Definition: Node.cpp:94
This model wraps a ParameterFile instance and provides access to the data interpreted as a (directed)...
Definition: GraphModel.h:32
int getHeight() const
node height
Definition: Node.cpp:170
unsigned int _id
id of the node
Definition: Node.h:142
bool isSelectedNode()
state of node selections
Definition: Node.cpp:104
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
paints the node
Definition: Node.cpp:137
Declaration of class Node.
Declaration of class GraphModel.
void addProperty(QString name, bool input)
adds a property to the node
Definition: Node.cpp:70
void setClassName(QString modname)
set node class name
Definition: Node.cpp:183
void changeConnectionLineColor(QColor lineColor)
change color of connected line
Definition: Node.cpp:108
int _nProps
number of properties in node
Definition: Node.h:133
void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
handles mouse move events
Definition: Node.cpp:117
QMap< QString, NodeProperty * > _properties
list of nodeproperties
Definition: Node.h:118
This model serves to provide a model frontend to access a ParameterFile instance. ...
QString getClassName() const
get class name
Definition: Node.cpp:188
NodeProperty * getProperty(QString propName) const
get node property
Definition: Node.cpp:179
int getWidth() const
node width
Definition: Node.cpp:166
static unsigned int _idCount
static id counter for id creation
Definition: Node.h:145
void moveBy(qreal dx, qreal dy)
moves the property and if connected all connectionlines
GraphModel * model()
get the current GraphModel
int _height
height of the node
Definition: Node.h:130
bool _selectedNode
state of node selection
Definition: Node.h:136
QRectF boundingRect() const
area where node is clickable
Definition: Node.cpp:88
unsigned int getId() const
get node id
Definition: Node.cpp:162
Property(/Parameter) of a node.
Definition: NodeProperty.h:35
void setActive(bool activeStatus)
changes the nodes activity
Definition: Node.cpp:57
QString getInstanceName()
get node name
Definition: Node.cpp:66
int _width
width of the node
Definition: Node.h:127
void setValue(QString parName, QString value)
Set a parameter in the underlying parameter file.
const ParameterFileModel * _pFile
link to parameter file
Definition: Node.h:148
Declaration of class NodeHandler.
QString _className
name of the module the node is representing
Definition: Node.h:124
QGraphicsScene that handles all drag&drop, node move and connect events.
Definition: NodeHandler.h:36
void _checkWidth(int nChars=0)
checks the node width in reference to the node name
Definition: Node.cpp:174
QString _instanceName
name of the node
Definition: Node.h:121
void setId(unsigned int id)
sets the id of the node
Definition: Node.cpp:52
void changeConnectionLineColor(QColor lineColor, bool isHover=true)
change color all connected line of hovering property isHover set to true when the property was hovere...
bool _active
state of activity
Definition: Node.h:139
Node(const ParameterFileModel *pFile, QString title, int xpos, int ypos)
default constructor
Definition: Node.cpp:35