Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8e6051afcdb111a0317a58fb64c2abf5 > files > 890

qt4-doc-4.6.3-0.2mdv2010.2.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Qt 4.6: graphwidget.cpp Example File (graphicsview/elasticnodes/graphwidget.cpp)</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><h1 class="title">graphwidget.cpp Example File<br /><span class="small-subtitle">graphicsview/elasticnodes/graphwidget.cpp</span>
</h1>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 ** All rights reserved.
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 **
 ** This file is part of the examples of the Qt Toolkit.
 **
 ** $QT_BEGIN_LICENSE:LGPL$
 ** Commercial Usage
 ** Licensees holding valid Qt Commercial licenses may use this file in
 ** accordance with the Qt Commercial License Agreement provided with the
 ** Software or, alternatively, in accordance with the terms contained in
 ** a written agreement between you and Nokia.
 **
 ** GNU Lesser General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU Lesser
 ** General Public License version 2.1 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.LGPL included in the
 ** packaging of this file.  Please review the following information to
 ** ensure the GNU Lesser General Public License version 2.1 requirements
 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
 ** In addition, as a special exception, Nokia gives you certain additional
 ** rights.  These rights are described in the Nokia Qt LGPL Exception
 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 **
 ** GNU General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU
 ** General Public License version 3.0 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.GPL included in the
 ** packaging of this file.  Please review the following information to
 ** ensure the GNU General Public License version 3.0 requirements will be
 ** met: http://www.gnu.org/copyleft/gpl.html.
 **
 ** If you have questions regarding the use of this file, please contact
 ** Nokia at qt-info@nokia.com.
 ** $QT_END_LICENSE$
 **
 ****************************************************************************/</span>

 #include &quot;graphwidget.h&quot;
 #include &quot;edge.h&quot;
 #include &quot;node.h&quot;

 #include &lt;QDebug&gt;
 #include &lt;QGraphicsScene&gt;
 #include &lt;QWheelEvent&gt;

 #include &lt;math.h&gt;

 GraphWidget::GraphWidget()
     : timerId(0)
 {
     QGraphicsScene *scene = new QGraphicsScene(this);
     scene-&gt;setItemIndexMethod(QGraphicsScene::NoIndex);
     scene-&gt;setSceneRect(-200, -200, 400, 400);
     setScene(scene);
     setCacheMode(CacheBackground);
     setViewportUpdateMode(BoundingRectViewportUpdate);
     setRenderHint(QPainter::Antialiasing);
     setTransformationAnchor(AnchorUnderMouse);
     setResizeAnchor(AnchorViewCenter);

     Node *node1 = new Node(this);
     Node *node2 = new Node(this);
     Node *node3 = new Node(this);
     Node *node4 = new Node(this);
     centerNode = new Node(this);
     Node *node6 = new Node(this);
     Node *node7 = new Node(this);
     Node *node8 = new Node(this);
     Node *node9 = new Node(this);
     scene-&gt;addItem(node1);
     scene-&gt;addItem(node2);
     scene-&gt;addItem(node3);
     scene-&gt;addItem(node4);
     scene-&gt;addItem(centerNode);
     scene-&gt;addItem(node6);
     scene-&gt;addItem(node7);
     scene-&gt;addItem(node8);
     scene-&gt;addItem(node9);
     scene-&gt;addItem(new Edge(node1, node2));
     scene-&gt;addItem(new Edge(node2, node3));
     scene-&gt;addItem(new Edge(node2, centerNode));
     scene-&gt;addItem(new Edge(node3, node6));
     scene-&gt;addItem(new Edge(node4, node1));
     scene-&gt;addItem(new Edge(node4, centerNode));
     scene-&gt;addItem(new Edge(centerNode, node6));
     scene-&gt;addItem(new Edge(centerNode, node8));
     scene-&gt;addItem(new Edge(node6, node9));
     scene-&gt;addItem(new Edge(node7, node4));
     scene-&gt;addItem(new Edge(node8, node7));
     scene-&gt;addItem(new Edge(node9, node8));

     node1-&gt;setPos(-50, -50);
     node2-&gt;setPos(0, -50);
     node3-&gt;setPos(50, -50);
     node4-&gt;setPos(-50, 0);
     centerNode-&gt;setPos(0, 0);
     node6-&gt;setPos(50, 0);
     node7-&gt;setPos(-50, 50);
     node8-&gt;setPos(0, 50);
     node9-&gt;setPos(50, 50);

     scale(qreal(0.8), qreal(0.8));
     setMinimumSize(400, 400);
     setWindowTitle(tr(&quot;Elastic Nodes&quot;));
 }

 void GraphWidget::itemMoved()
 {
     if (!timerId)
         timerId = startTimer(1000 / 25);
 }

 void GraphWidget::keyPressEvent(QKeyEvent *event)
 {
     switch (event-&gt;key()) {
     case Qt::Key_Up:
         centerNode-&gt;moveBy(0, -20);
         break;
     case Qt::Key_Down:
         centerNode-&gt;moveBy(0, 20);
         break;
     case Qt::Key_Left:
         centerNode-&gt;moveBy(-20, 0);
         break;
     case Qt::Key_Right:
         centerNode-&gt;moveBy(20, 0);
         break;
     case Qt::Key_Plus:
         scaleView(qreal(1.2));
         break;
     case Qt::Key_Minus:
         scaleView(1 / qreal(1.2));
         break;
     case Qt::Key_Space:
     case Qt::Key_Enter:
         foreach (QGraphicsItem *item, scene()-&gt;items()) {
             if (qgraphicsitem_cast&lt;Node *&gt;(item))
                 item-&gt;setPos(-150 + qrand() % 300, -150 + qrand() % 300);
         }
         break;
     default:
         QGraphicsView::keyPressEvent(event);
     }
 }

 void GraphWidget::timerEvent(QTimerEvent *event)
 {
     Q_UNUSED(event);

     QList&lt;Node *&gt; nodes;
     foreach (QGraphicsItem *item, scene()-&gt;items()) {
         if (Node *node = qgraphicsitem_cast&lt;Node *&gt;(item))
             nodes &lt;&lt; node;
     }

     foreach (Node *node, nodes)
         node-&gt;calculateForces();

     bool itemsMoved = false;
     foreach (Node *node, nodes) {
         if (node-&gt;advance())
             itemsMoved = true;
     }

     if (!itemsMoved) {
         killTimer(timerId);
         timerId = 0;
     }
 }

 void GraphWidget::wheelEvent(QWheelEvent *event)
 {
     scaleView(pow((double)2, -event-&gt;delta() / 240.0));
 }

 void GraphWidget::drawBackground(QPainter *painter, const QRectF &amp;rect)
 {
     Q_UNUSED(rect);

     <span class="comment">// Shadow</span>
     QRectF sceneRect = this-&gt;sceneRect();
     QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
     QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
     if (rightShadow.intersects(rect) || rightShadow.contains(rect))
         painter-&gt;fillRect(rightShadow, Qt::darkGray);
     if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
         painter-&gt;fillRect(bottomShadow, Qt::darkGray);

     <span class="comment">// Fill</span>
     QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
     gradient.setColorAt(0, Qt::white);
     gradient.setColorAt(1, Qt::lightGray);
     painter-&gt;fillRect(rect.intersect(sceneRect), gradient);
     painter-&gt;setBrush(Qt::NoBrush);
     painter-&gt;drawRect(sceneRect);

     <span class="comment">// Text</span>
     QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
                     sceneRect.width() - 4, sceneRect.height() - 4);
     QString message(tr(&quot;Click and drag the nodes around, and zoom with the mouse &quot;
                        &quot;wheel or the '+' and '-' keys&quot;));

     QFont font = painter-&gt;font();
     font.setBold(true);
     font.setPointSize(14);
     painter-&gt;setFont(font);
     painter-&gt;setPen(Qt::lightGray);
     painter-&gt;drawText(textRect.translated(2, 2), message);
     painter-&gt;setPen(Qt::black);
     painter-&gt;drawText(textRect, message);
 }

 void GraphWidget::scaleView(qreal scaleFactor)
 {
     qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
     if (factor &lt; 0.07 || factor &gt; 100)
         return;

     scale(scaleFactor, scaleFactor);
 }</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="40%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="40%" align="right"><div align="right">Qt 4.6.3</div></td>
</tr></table></div></address></body>
</html>