examples/qvdesigner0.2/slate/groupnode.cpp

00001 /*
00002  *      Copyright (C) 2008. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
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         //se actualiza cambio el padre
00053         node->setParentItem(this);
00054 
00055         //se escala
00056         node->scale(SUBSCALE, SUBSCALE);
00057 
00058         //se escala su posición para que no se separen cuando se unen
00059         node->setPos(node->pos() * SUBSCALE);
00060 }
00061 
00062 void GroupNode::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget * /* widget */)
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     // una forma de mostrar cosas solo cuand tenemos un nivel de detalle concreto, esto se puede poner en función del nivel de anidamiento en que se encuentra.
00080     // hacer uno recursivo que tenga dentro otro objeto igual con nivel de anidamiento padre+1.
00081 //     if (option->levelOfDetail > 2)
00082 //     {
00083 //      QStyleOptionGraphicsItem *nodesoption = new QStyleOptionGraphicsItem(*option);
00084 // //   nodesoption->levelOfDetail = option->levelOfDetail / 2;
00085 // //   nodesoption->matrix = nodesoption->matrix.scale(0.5, 0.5);
00086 //         foreach (Node *node, myNodes)
00087 //             node->paint(painter, nodesoption, 0);
00088 //     }
00089 }
00090 
00091 // devuelve la posición (en cordenadas de la scena) del centro del punto indicado por el índice point, si se sale del rango devuelve la posición del nodo
00092 QPointF GroupNode::scenePointPos(int ) const
00093 {
00094 /*      QRectF rect = outlineRect();
00095 
00096         if (point < 0) return this->scenePos();
00097         if (point < numProp) return mapToScene( QPointF(rect.left() + 8, rect.top() + 16*point + 24) );
00098         if (point < 2 * numProp) return mapToScene( QPointF(rect.right() - 8, rect.top() + 16*(point-numProp) + 24) );*/
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     //return (childRect.unite(rect)).adjusted(-5.0, -1.0, 5.0, 1.0);
00148         //quitando la union es como si se desplazara el rectángulo sin que la linea de los link se diera cuenta, quedando mál
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