PARP Research Group University of Murcia, Spain


src/qvgui/qvdesigner/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 
00080         painter->drawEllipse((int)rect.right() - 10, (int)rect.bottom() - 10, 8, 8);
00081     // 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.
00082     // hacer uno recursivo que tenga dentro otro objeto igual con nivel de anidamiento padre+1.
00083 //     if (option->levelOfDetail > 2)
00084 //     {
00085 //      QStyleOptionGraphicsItem *nodesoption = new QStyleOptionGraphicsItem(*option);
00086 // //   nodesoption->levelOfDetail = option->levelOfDetail / 2;
00087 // //   nodesoption->matrix = nodesoption->matrix.scale(0.5, 0.5);
00088 //         foreach (Node *node, myNodes)
00089 //             node->paint(painter, nodesoption, 0);
00090 //     }
00091 }
00092 
00093 // 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
00094 QPointF GroupNode::scenePointPos(int ) const
00095 {
00096 /*      QRectF rect = outlineRect();
00097 
00098         if (point < 0) return this->scenePos();
00099         if (point < numProp) return mapToScene( QPointF(rect.left() + 8, rect.top() + 16*point + 24) );
00100         if (point < 2 * numProp) return mapToScene( QPointF(rect.right() - 8, rect.top() + 16*(point-numProp) + 24) );*/
00101         return this->scenePos();
00102 }
00103 
00104 QPointF GroupNode::scenePointPos(QString, bool) const
00105 {
00106         return this->scenePos();
00107 }
00108 
00109 int GroupNode::propPoint(QString, bool) const
00110 {
00111         return 0;
00112 }
00113 
00114 int GroupNode::numProps() const
00115 {
00116         return 0;
00117 }
00118 
00119 void GroupNode::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
00120 {
00121     QRectF rect = outlineRect();
00122         QPointF click = mapFromScene(event->scenePos());
00123 
00124         if ((click.x() > rect.right() - 10) && (click.y() > rect.bottom() - 10)) {
00125                 // aqui es donde tendría que cambiar de la vista de nodos unidos a los puntos laterales a la vista de los nombres de las
00126                 // propiedades de entrada y salida.
00127         }
00128         else {
00129                 QString text = QInputDialog::getText(event->widget(),
00130                                                         tr("Edit Text"), tr("Enter new text:"),
00131                                                         QLineEdit::Normal, myText);
00132                 if (!text.isEmpty())
00133                         setText(text);
00134         }
00135 }
00136 
00137 QVariant GroupNode::itemChange(GraphicsItemChange change,
00138                           const QVariant &value)
00139 {
00140     if (change == ItemPositionHasChanged)
00141                 updateLinksPos();
00142 
00143     return QGraphicsItem::itemChange(change, value);
00144 }
00145 
00146 void GroupNode::updateLinksPos()
00147 {
00148     foreach (Link *link, getLinks())
00149         link->trackNodes();
00150 
00151         foreach (QGraphicsItem *child, children()) {
00152                 Node *nodeChild = dynamic_cast<Node *>(child);
00153                 if (nodeChild)
00154                         nodeChild->updateLinksPos();
00155         }
00156 }
00157 
00158 QRectF GroupNode::outlineRect() const
00159 {
00160     const int Padding = 8;
00161     QFontMetricsF metrics = qApp->font();
00162     QRectF rect = metrics.boundingRect(myText);
00163     rect.adjust(-Padding, -Padding, +Padding, +Padding);
00164     rect.translate(-rect.center());
00165 
00166     QRectF childRect = childrenBoundingRect();
00167 
00168     //return (childRect.unite(rect)).adjusted(-5.0, -1.0, 5.0, 1.0);
00169         //quitando la union es como si se desplazara el rectángulo sin que la linea de los link se diera cuenta, quedando mál
00170         return (childRect).adjusted(-20.0, -17.0, 20.0, 5.0);
00171 }
00172 
00173 int GroupNode::roundness(double size) const
00174 {
00175     const int Diameter = 12;
00176     return 100 * Diameter / int(size);
00177 }
00178 
00179 int GroupNode::pointAt(QPointF ) const
00180 {
00181         return -1;
00182 }
00183 
00184 QString GroupNode::propName(int ) const
00185 {
00186         return QString();
00187 }
00188 



QVision framework. PARP research group, copyright 2007, 2008.