00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <QtGui>
00023
00024 #include "groupnode.h"
00025 #include <QGraphicsSceneDragDropEvent>
00026 #include "itemnode.h"
00027
00028
00029 GroupNode::GroupNode(QGraphicsItem * parent, QGraphicsScene * scene): Node(parent, scene)
00030 {
00031 myTextColor = Qt::darkGreen;
00032 myOutlineColor = Qt::darkBlue;
00033 myBackgroundColor = Qt::white;
00034
00035 setFlags(ItemIsMovable | ItemIsSelectable);
00036
00037 type = "Group";
00038 name = type;
00039 }
00040
00041 GroupNode::~GroupNode()
00042 {
00043 foreach (Link *link, getLinks())
00044 delete link;
00045
00046 foreach (QGraphicsItem *child, children())
00047 delete child;
00048 }
00049
00050 void GroupNode::addNode(Node *node)
00051 {
00052
00053 node->setParentItem(this);
00054
00055
00056 node->scale(SUBSCALE, SUBSCALE);
00057
00058
00059 node->setPos(node->pos() * SUBSCALE);
00060 }
00061
00062 void GroupNode::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget * )
00063 {
00064 QPen pen(myOutlineColor);
00065 if (option->state & QStyle::State_Selected) {
00066 pen.setStyle(Qt::DotLine);
00067 pen.setWidth(2);
00068 }
00069 painter->setPen(pen);
00070 painter->setBrush(myBackgroundColor);
00071
00072 QRectF rect = outlineRect();
00073 painter->drawRoundRect(rect.adjusted(0.0, 12.0, 0.0, .0), roundness(rect.width()),
00074 roundness(rect.height()));
00075
00076 painter->setPen(myTextColor);
00077 painter->drawText(QPointF((int)rect.left(), (int)rect.top() + 10), myText);
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 }
00090
00091
00092 QPointF GroupNode::scenePointPos(int ) const
00093 {
00094
00095
00096
00097
00098
00099 return this->scenePos();
00100 }
00101
00102 QPointF GroupNode::scenePointPos(QString, bool) const
00103 {
00104 return this->scenePos();
00105 }
00106
00107 void GroupNode::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
00108 {
00109 QString text = QInputDialog::getText(event->widget(),
00110 tr("Edit Text"), tr("Enter new text:"),
00111 QLineEdit::Normal, myText);
00112 if (!text.isEmpty())
00113 setText(text);
00114 }
00115
00116 QVariant GroupNode::itemChange(GraphicsItemChange change,
00117 const QVariant &value)
00118 {
00119 if (change == ItemPositionHasChanged)
00120 updateLinksPos();
00121
00122 return QGraphicsItem::itemChange(change, value);
00123 }
00124
00125 void GroupNode::updateLinksPos()
00126 {
00127 foreach (Link *link, getLinks())
00128 link->trackNodes();
00129
00130 foreach (QGraphicsItem *child, children()) {
00131 Node *nodeChild = dynamic_cast<Node *>(child);
00132 if (nodeChild)
00133 nodeChild->updateLinksPos();
00134 }
00135 }
00136
00137 QRectF GroupNode::outlineRect() const
00138 {
00139 const int Padding = 8;
00140 QFontMetricsF metrics = qApp->font();
00141 QRectF rect = metrics.boundingRect(myText);
00142 rect.adjust(-Padding, -Padding, +Padding, +Padding);
00143 rect.translate(-rect.center());
00144
00145 QRectF childRect = childrenBoundingRect();
00146
00147
00148
00149 return (childRect).adjusted(-20.0, -17.0, 20.0, 5.0);
00150 }
00151
00152 int GroupNode::roundness(double size) const
00153 {
00154 const int Diameter = 12;
00155 return 100 * Diameter / int(size);
00156 }
00157
00158 int GroupNode::pointAt(QPointF ) const
00159 {
00160 return -1;
00161 }
00162
00163 QString GroupNode::propName(int ) const
00164 {
00165 return QString();
00166 }
00167