1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the examples of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
41 #include "GraphWidget.h"
47 #include <cpPlugins/Interface/Workspace.h>
51 GraphWidget::GraphWidget( QWidget* parent )
52 : QGraphicsView( parent ),
56 QGraphicsScene* scene = new QGraphicsScene( this );
57 scene->setItemIndexMethod( QGraphicsScene::NoIndex );
58 // TODO: scene->setSceneRect( -200, -200, 400, 400 );
59 this->setScene( scene );
61 this->setCacheMode(CacheBackground);
62 this->setViewportUpdateMode(BoundingRectViewportUpdate);
63 this->setRenderHint(QPainter::Antialiasing);
64 this->setTransformationAnchor(AnchorUnderMouse);
65 this->scale(qreal(0.8), qreal(0.8));
66 this->setMinimumSize(400, 400);
67 this->setWindowTitle( tr("Elastic Nodes") );
71 Node *node1 = new Node(this);
72 Node *node2 = new Node(this);
73 Node *node3 = new Node(this);
74 Node *node4 = new Node(this);
75 centerNode = new Node(this);
76 Node *node6 = new Node(this);
77 Node *node7 = new Node(this);
78 Node *node8 = new Node(this);
79 Node *node9 = new Node(this);
80 scene->addItem(node1);
81 scene->addItem(node2);
82 scene->addItem(node3);
83 scene->addItem(node4);
84 scene->addItem(centerNode);
85 scene->addItem(node6);
86 scene->addItem(node7);
87 scene->addItem(node8);
88 scene->addItem(node9);
89 scene->addItem(new Edge(node1, node2));
90 scene->addItem(new Edge(node2, node3));
91 scene->addItem(new Edge(node2, centerNode));
92 scene->addItem(new Edge(node3, node6));
93 scene->addItem(new Edge(node4, node1));
94 scene->addItem(new Edge(node4, centerNode));
95 scene->addItem(new Edge(centerNode, node6));
96 scene->addItem(new Edge(centerNode, node8));
97 scene->addItem(new Edge(node6, node9));
98 scene->addItem(new Edge(node7, node4));
99 scene->addItem(new Edge(node8, node7));
100 scene->addItem(new Edge(node9, node8));
102 node1->setPos(-50, -50);
103 node2->setPos(0, -50);
104 node3->setPos(50, -50);
105 node4->setPos(-50, 0);
106 centerNode->setPos(0, 0);
107 node6->setPos(50, 0);
108 node7->setPos(-50, 50);
109 node8->setPos(0, 50);
110 node9->setPos(50, 50);
114 // -------------------------------------------------------------------------
115 cpPlugins::Interface::Workspace* GraphWidget::
118 return( this->m_Workspace );
121 // -------------------------------------------------------------------------
122 const cpPlugins::Interface::Workspace* GraphWidget::
125 return( this->m_Workspace );
128 // -------------------------------------------------------------------------
130 setWorkspace( cpPlugins::Interface::Workspace* ws )
132 if( this->m_Workspace == ws )
134 this->m_Workspace = ws;
135 QGraphicsScene* scene = this->scene( );
138 this->m_Graph = TGraph::New( );
141 auto vIt = this->m_Workspace->GetGraph( )->BeginVertices( );
142 auto vIt_end = this->m_Workspace->GetGraph( )->EndVertices( );
143 for( ; vIt != vIt_end; ++vIt )
145 std::string label = vIt->second->GetName( ) + std::string( "\n" );
146 label += vIt->second->GetClassName( );
148 Node* node = new Node( this, label );
149 this->m_Graph->InsertVertex( vIt->first, node );
150 scene->addItem( node );
155 auto rIt = this->m_Workspace->GetGraph( )->BeginEdgesRows( );
156 auto rIt_end = this->m_Workspace->GetGraph( )->EndEdgesRows( );
157 for( ; rIt != rIt_end; ++rIt )
159 Node* a = this->m_Graph->GetVertex( rIt->first );
162 auto cIt = rIt->second.begin( );
163 for( ; cIt != rIt->second.end( ); ++cIt )
165 Node* b = this->m_Graph->GetVertex( cIt->first );
168 Edge* e = new Edge( a, b );
169 this->m_Graph->AddConnection( rIt->first, cIt->first, e );
177 // -------------------------------------------------------------------------
181 if( this->m_Workspace == NULL )
185 // -------------------------------------------------------------------------
186 void GraphWidget::itemMoved()
189 timerId = startTimer(1000 / 25);
192 void GraphWidget::keyPressEvent(QKeyEvent *event)
194 switch (event->key()) {
196 centerNode->moveBy(0, -20);
199 centerNode->moveBy(0, 20);
202 centerNode->moveBy(-20, 0);
205 centerNode->moveBy(20, 0);
218 QGraphicsView::keyPressEvent(event);
222 void GraphWidget::timerEvent(QTimerEvent *event)
227 foreach (QGraphicsItem *item, scene()->items()) {
228 if (Node *node = qgraphicsitem_cast<Node *>(item))
232 foreach (Node *node, nodes)
233 node->calculateForces();
235 bool itemsMoved = false;
236 foreach (Node *node, nodes) {
247 void GraphWidget::wheelEvent(QWheelEvent *event)
249 scaleView(pow((double)2, -event->delta() / 240.0));
252 void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
257 QRectF sceneRect = rect;//this->sceneRect();
258 QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
259 QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
260 if (rightShadow.intersects(rect) || rightShadow.contains(rect))
261 painter->fillRect(rightShadow, Qt::darkGray);
262 if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
263 painter->fillRect(bottomShadow, Qt::darkGray);
266 QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
267 gradient.setColorAt(0, Qt::white);
268 gradient.setColorAt(1, Qt::lightGray);
269 painter->fillRect(rect.intersect(sceneRect), gradient);
270 painter->setBrush(Qt::NoBrush);
271 painter->drawRect(sceneRect);
273 #if !defined(Q_OS_SYMBIAN) && !defined(Q_WS_MAEMO_5)
275 QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
276 sceneRect.width() - 4, sceneRect.height() - 4);
277 QString message(tr("Click and drag the nodes around, and zoom with the mouse "
278 "wheel or the '+' and '-' keys"));
280 QFont font = painter->font();
282 font.setPointSize(14);
283 painter->setFont(font);
284 painter->setPen(Qt::lightGray);
285 painter->drawText(textRect.translated(2, 2), message);
286 painter->setPen(Qt::black);
287 painter->drawText(textRect, message);
291 void GraphWidget::scaleView(qreal scaleFactor)
293 qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
294 if (factor < 0.07 || factor > 100)
297 scale(scaleFactor, scaleFactor);
300 void GraphWidget::shuffle()
302 foreach (QGraphicsItem *item, scene()->items()) {
303 if (qgraphicsitem_cast<Node *>(item))
304 item->setPos(-150 + qrand() % 300, -150 + qrand() % 300);
308 void GraphWidget::zoomIn()
310 scaleView(qreal(1.2));
313 void GraphWidget::zoomOut()
315 scaleView(1 / qreal(1.2));