mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-08-18 19:08:35 -04:00
Added Qt Elastic Example into NetworkView code.
works quite well! git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@392 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
5c75ee5ead
commit
6451e4ebe2
9 changed files with 959 additions and 155 deletions
139
retroshare-gui/src/gui/elastic/edge.cpp
Normal file
139
retroshare-gui/src/gui/elastic/edge.cpp
Normal file
|
@ -0,0 +1,139 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2006-2007 Trolltech ASA. All rights reserved.
|
||||
**
|
||||
** This file is part of the example classes of the Qt Toolkit.
|
||||
**
|
||||
** This file may be used under the terms of the GNU General Public
|
||||
** License version 2.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 GNU
|
||||
** General Public Licensing requirements will be met:
|
||||
** http://trolltech.com/products/qt/licenses/licensing/opensource/
|
||||
**
|
||||
** If you are unsure which license is appropriate for your use, please
|
||||
** review the following information:
|
||||
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
|
||||
** or contact the sales department at sales@trolltech.com.
|
||||
**
|
||||
** In addition, as a special exception, Trolltech gives you certain
|
||||
** additional rights. These rights are described in the Trolltech GPL
|
||||
** Exception version 1.0, which can be found at
|
||||
** http://www.trolltech.com/products/qt/gplexception/ and in the file
|
||||
** GPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** In addition, as a special exception, Trolltech, as the sole copyright
|
||||
** holder for Qt Designer, grants users of the Qt/Eclipse Integration
|
||||
** plug-in the right for the Qt/Eclipse Integration to link to
|
||||
** functionality provided by Qt Designer and its related libraries.
|
||||
**
|
||||
** Trolltech reserves all rights not expressly granted herein.
|
||||
**
|
||||
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
||||
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QPainter>
|
||||
|
||||
#include "edge.h"
|
||||
#include "node.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
static const double Pi = 3.14159265358979323846264338327950288419717;
|
||||
static double TwoPi = 2.0 * Pi;
|
||||
|
||||
Edge::Edge(Node *sourceNode, Node *destNode)
|
||||
: arrowSize(10)
|
||||
{
|
||||
setAcceptedMouseButtons(0);
|
||||
source = sourceNode;
|
||||
dest = destNode;
|
||||
source->addEdge(this);
|
||||
dest->addEdge(this);
|
||||
adjust();
|
||||
}
|
||||
|
||||
Edge::~Edge()
|
||||
{
|
||||
}
|
||||
|
||||
Node *Edge::sourceNode() const
|
||||
{
|
||||
return source;
|
||||
}
|
||||
|
||||
void Edge::setSourceNode(Node *node)
|
||||
{
|
||||
source = node;
|
||||
adjust();
|
||||
}
|
||||
|
||||
Node *Edge::destNode() const
|
||||
{
|
||||
return dest;
|
||||
}
|
||||
|
||||
void Edge::setDestNode(Node *node)
|
||||
{
|
||||
dest = node;
|
||||
adjust();
|
||||
}
|
||||
|
||||
void Edge::adjust()
|
||||
{
|
||||
if (!source || !dest)
|
||||
return;
|
||||
|
||||
QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
|
||||
qreal length = line.length();
|
||||
QPointF edgeOffset((line.dx() * 10) / length, (line.dy() * 10) / length);
|
||||
|
||||
prepareGeometryChange();
|
||||
sourcePoint = line.p1() + edgeOffset;
|
||||
destPoint = line.p2() - edgeOffset;
|
||||
}
|
||||
|
||||
QRectF Edge::boundingRect() const
|
||||
{
|
||||
if (!source || !dest)
|
||||
return QRectF();
|
||||
|
||||
qreal penWidth = 1;
|
||||
qreal extra = (penWidth + arrowSize) / 2.0;
|
||||
|
||||
return QRectF(sourcePoint, QSizeF(destPoint.x() - sourcePoint.x(),
|
||||
destPoint.y() - sourcePoint.y()))
|
||||
.normalized()
|
||||
.adjusted(-extra, -extra, extra, extra);
|
||||
}
|
||||
|
||||
void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
|
||||
{
|
||||
if (!source || !dest)
|
||||
return;
|
||||
|
||||
// Draw the line itself
|
||||
QLineF line(sourcePoint, destPoint);
|
||||
painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
|
||||
painter->drawLine(line);
|
||||
|
||||
// Draw the arrows if there's enough room
|
||||
double angle = ::acos(line.dx() / line.length());
|
||||
if (line.dy() >= 0)
|
||||
angle = TwoPi - angle;
|
||||
|
||||
QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi / 3) * arrowSize,
|
||||
cos(angle + Pi / 3) * arrowSize);
|
||||
QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi / 3) * arrowSize,
|
||||
cos(angle + Pi - Pi / 3) * arrowSize);
|
||||
QPointF destArrowP1 = destPoint + QPointF(sin(angle - Pi / 3) * arrowSize,
|
||||
cos(angle - Pi / 3) * arrowSize);
|
||||
QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize,
|
||||
cos(angle - Pi + Pi / 3) * arrowSize);
|
||||
|
||||
painter->setBrush(Qt::black);
|
||||
painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2);
|
||||
painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);
|
||||
}
|
73
retroshare-gui/src/gui/elastic/edge.h
Normal file
73
retroshare-gui/src/gui/elastic/edge.h
Normal file
|
@ -0,0 +1,73 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2006-2007 Trolltech ASA. All rights reserved.
|
||||
**
|
||||
** This file is part of the example classes of the Qt Toolkit.
|
||||
**
|
||||
** This file may be used under the terms of the GNU General Public
|
||||
** License version 2.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 GNU
|
||||
** General Public Licensing requirements will be met:
|
||||
** http://trolltech.com/products/qt/licenses/licensing/opensource/
|
||||
**
|
||||
** If you are unsure which license is appropriate for your use, please
|
||||
** review the following information:
|
||||
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
|
||||
** or contact the sales department at sales@trolltech.com.
|
||||
**
|
||||
** In addition, as a special exception, Trolltech gives you certain
|
||||
** additional rights. These rights are described in the Trolltech GPL
|
||||
** Exception version 1.0, which can be found at
|
||||
** http://www.trolltech.com/products/qt/gplexception/ and in the file
|
||||
** GPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** In addition, as a special exception, Trolltech, as the sole copyright
|
||||
** holder for Qt Designer, grants users of the Qt/Eclipse Integration
|
||||
** plug-in the right for the Qt/Eclipse Integration to link to
|
||||
** functionality provided by Qt Designer and its related libraries.
|
||||
**
|
||||
** Trolltech reserves all rights not expressly granted herein.
|
||||
**
|
||||
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
||||
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef EDGE_H
|
||||
#define EDGE_H
|
||||
|
||||
#include <QGraphicsItem>
|
||||
|
||||
class Node;
|
||||
|
||||
class Edge : public QGraphicsItem
|
||||
{
|
||||
public:
|
||||
Edge(Node *sourceNode, Node *destNode);
|
||||
~Edge();
|
||||
|
||||
Node *sourceNode() const;
|
||||
void setSourceNode(Node *node);
|
||||
|
||||
Node *destNode() const;
|
||||
void setDestNode(Node *node);
|
||||
|
||||
void adjust();
|
||||
|
||||
enum { Type = UserType + 2 };
|
||||
int type() const { return Type; }
|
||||
|
||||
protected:
|
||||
QRectF boundingRect() const;
|
||||
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
|
||||
|
||||
private:
|
||||
Node *source, *dest;
|
||||
|
||||
QPointF sourcePoint;
|
||||
QPointF destPoint;
|
||||
qreal arrowSize;
|
||||
};
|
||||
|
||||
#endif
|
282
retroshare-gui/src/gui/elastic/graphwidget.cpp
Normal file
282
retroshare-gui/src/gui/elastic/graphwidget.cpp
Normal file
|
@ -0,0 +1,282 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2006-2007 Trolltech ASA. All rights reserved.
|
||||
**
|
||||
** This file is part of the example classes of the Qt Toolkit.
|
||||
**
|
||||
** This file may be used under the terms of the GNU General Public
|
||||
** License version 2.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 GNU
|
||||
** General Public Licensing requirements will be met:
|
||||
** http://trolltech.com/products/qt/licenses/licensing/opensource/
|
||||
**
|
||||
** If you are unsure which license is appropriate for your use, please
|
||||
** review the following information:
|
||||
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
|
||||
** or contact the sales department at sales@trolltech.com.
|
||||
**
|
||||
** In addition, as a special exception, Trolltech gives you certain
|
||||
** additional rights. These rights are described in the Trolltech GPL
|
||||
** Exception version 1.0, which can be found at
|
||||
** http://www.trolltech.com/products/qt/gplexception/ and in the file
|
||||
** GPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** In addition, as a special exception, Trolltech, as the sole copyright
|
||||
** holder for Qt Designer, grants users of the Qt/Eclipse Integration
|
||||
** plug-in the right for the Qt/Eclipse Integration to link to
|
||||
** functionality provided by Qt Designer and its related libraries.
|
||||
**
|
||||
** Trolltech reserves all rights not expressly granted herein.
|
||||
**
|
||||
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
||||
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "graphwidget.h"
|
||||
#include "edge.h"
|
||||
#include "node.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QGraphicsScene>
|
||||
#include <QWheelEvent>
|
||||
|
||||
#include <math.h>
|
||||
|
||||
GraphWidget::GraphWidget(QWidget *parent)
|
||||
:QGraphicsView(parent), timerId(0)
|
||||
{
|
||||
|
||||
#if 0
|
||||
QGraphicsScene *scene = new QGraphicsScene(this);
|
||||
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
|
||||
scene->setSceneRect(-200, -200, 400, 400);
|
||||
setScene(scene);
|
||||
|
||||
centerNode = new Node(this, 1, "You");
|
||||
scene->addItem(centerNode);
|
||||
centerNode->setPos(0, 0);
|
||||
|
||||
#endif
|
||||
centerNode = NULL;
|
||||
|
||||
setCacheMode(CacheBackground);
|
||||
setRenderHint(QPainter::Antialiasing);
|
||||
setTransformationAnchor(AnchorUnderMouse);
|
||||
setResizeAnchor(AnchorViewCenter);
|
||||
scale(0.8, 0.8);
|
||||
setMinimumSize(400, 400);
|
||||
setWindowTitle(tr("Elastic Nodes"));
|
||||
|
||||
clearGraph();
|
||||
|
||||
clearGraph();
|
||||
}
|
||||
|
||||
bool GraphWidget::clearGraph()
|
||||
{
|
||||
QGraphicsScene *oldscene = scene();
|
||||
Node *oldcenterNode = centerNode;
|
||||
|
||||
QGraphicsScene *scene = new QGraphicsScene(this);
|
||||
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
|
||||
scene->setSceneRect(-200, -200, 400, 400);
|
||||
setScene(scene);
|
||||
//setCacheMode(CacheBackground);
|
||||
//setRenderHint(QPainter::Antialiasing);
|
||||
//setTransformationAnchor(AnchorUnderMouse);
|
||||
//setResizeAnchor(AnchorViewCenter);
|
||||
|
||||
centerNode = new Node(this, 1, "OwnId", "You");
|
||||
scene->addItem(centerNode);
|
||||
centerNode->setPos(0, 0);
|
||||
|
||||
//scale(0.8, 0.8);
|
||||
//setMinimumSize(400, 400);
|
||||
//setWindowTitle(tr("Elastic Nodes"));
|
||||
|
||||
if (oldscene)
|
||||
{
|
||||
delete oldscene;
|
||||
}
|
||||
|
||||
if (oldcenterNode)
|
||||
{
|
||||
//delete oldcenterNode;
|
||||
}
|
||||
|
||||
std::list<Edge *>::iterator eit;
|
||||
std::map<std::string, Node *>::iterator it;
|
||||
for(eit = edgeList.begin(); eit != edgeList.end(); eit++)
|
||||
{
|
||||
//delete(*eit);
|
||||
}
|
||||
for(it = nodeMap.begin(); it != nodeMap.end(); it++)
|
||||
{
|
||||
//delete(it->second);
|
||||
}
|
||||
|
||||
nodeMap.clear();
|
||||
edgeList.clear();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void GraphWidget::addNode(uint32_t type, std::string id, std::string name)
|
||||
{
|
||||
Node *node = new Node(this, type, id, name);
|
||||
|
||||
/* store node */
|
||||
nodeMap[id] = node;
|
||||
scene()->addItem(node);
|
||||
node->setPos(-50 + qrand() % 100 , -50 + qrand() % 100);
|
||||
}
|
||||
|
||||
void GraphWidget::addEdge(std::string id1, std::string id2)
|
||||
{
|
||||
std::map<std::string, Node *>::iterator it;
|
||||
Node *n1 = NULL;
|
||||
Node *n2 = NULL;
|
||||
|
||||
if (id1 == "")
|
||||
{
|
||||
n1 = centerNode;
|
||||
}
|
||||
else
|
||||
{
|
||||
it = nodeMap.find(id1);
|
||||
if (it != nodeMap.end())
|
||||
{
|
||||
n1 = it->second;
|
||||
}
|
||||
}
|
||||
|
||||
it = nodeMap.find(id2);
|
||||
if (it != nodeMap.end())
|
||||
{
|
||||
n2 = it->second;
|
||||
}
|
||||
|
||||
if ((n1) && (n2))
|
||||
{
|
||||
Edge *edge = new Edge(n1, n2);
|
||||
scene()->addItem(edge);
|
||||
edgeList.push_back(edge);
|
||||
}
|
||||
}
|
||||
|
||||
void GraphWidget::itemMoved()
|
||||
{
|
||||
if (!timerId)
|
||||
timerId = startTimer(1000 / 25);
|
||||
}
|
||||
|
||||
void GraphWidget::keyPressEvent(QKeyEvent *event)
|
||||
{
|
||||
switch (event->key()) {
|
||||
case Qt::Key_Up:
|
||||
centerNode->moveBy(0, -20);
|
||||
break;
|
||||
case Qt::Key_Down:
|
||||
centerNode->moveBy(0, 20);
|
||||
break;
|
||||
case Qt::Key_Left:
|
||||
centerNode->moveBy(-20, 0);
|
||||
break;
|
||||
case Qt::Key_Right:
|
||||
centerNode->moveBy(20, 0);
|
||||
break;
|
||||
case Qt::Key_Plus:
|
||||
scaleView(1.2);
|
||||
break;
|
||||
case Qt::Key_Minus:
|
||||
scaleView(1 / 1.2);
|
||||
break;
|
||||
case Qt::Key_Space:
|
||||
case Qt::Key_Enter:
|
||||
foreach (QGraphicsItem *item, scene()->items()) {
|
||||
if (qgraphicsitem_cast<Node *>(item))
|
||||
item->setPos(-150 + qrand() % 300, -150 + qrand() % 300);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
QGraphicsView::keyPressEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
void GraphWidget::timerEvent(QTimerEvent *event)
|
||||
{
|
||||
Q_UNUSED(event);
|
||||
|
||||
QList<Node *> nodes;
|
||||
foreach (QGraphicsItem *item, scene()->items()) {
|
||||
if (Node *node = qgraphicsitem_cast<Node *>(item))
|
||||
nodes << node;
|
||||
}
|
||||
|
||||
foreach (Node *node, nodes)
|
||||
node->calculateForces();
|
||||
|
||||
bool itemsMoved = false;
|
||||
foreach (Node *node, nodes) {
|
||||
if (node->advance())
|
||||
itemsMoved = true;
|
||||
}
|
||||
|
||||
if (!itemsMoved) {
|
||||
killTimer(timerId);
|
||||
timerId = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void GraphWidget::wheelEvent(QWheelEvent *event)
|
||||
{
|
||||
scaleView(pow((double)2, -event->delta() / 240.0));
|
||||
}
|
||||
|
||||
void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
|
||||
{
|
||||
Q_UNUSED(rect);
|
||||
|
||||
// Shadow
|
||||
QRectF sceneRect = this->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->fillRect(rightShadow, Qt::darkGray);
|
||||
if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
|
||||
painter->fillRect(bottomShadow, Qt::darkGray);
|
||||
|
||||
// Fill
|
||||
QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
|
||||
gradient.setColorAt(0, Qt::white);
|
||||
gradient.setColorAt(1, Qt::lightGray);
|
||||
painter->fillRect(rect.intersect(sceneRect), gradient);
|
||||
painter->setBrush(Qt::NoBrush);
|
||||
painter->drawRect(sceneRect);
|
||||
|
||||
// Text
|
||||
// QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
|
||||
// sceneRect.width() - 4, sceneRect.height() - 4);
|
||||
// QString message(tr("Click and drag the nodes around, and zoom with the mouse "
|
||||
// "wheel or the '+' and '-' keys"));
|
||||
//
|
||||
// QFont font = painter->font();
|
||||
// font.setBold(true);
|
||||
// font.setPointSize(14);
|
||||
// painter->setFont(font);
|
||||
// painter->setPen(Qt::lightGray);
|
||||
// painter->drawText(textRect.translated(2, 2), message);
|
||||
// painter->setPen(Qt::black);
|
||||
// painter->drawText(textRect, message);
|
||||
}
|
||||
|
||||
void GraphWidget::scaleView(qreal scaleFactor)
|
||||
{
|
||||
qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
|
||||
if (factor < 0.07 || factor > 100)
|
||||
return;
|
||||
|
||||
scale(scaleFactor, scaleFactor);
|
||||
}
|
76
retroshare-gui/src/gui/elastic/graphwidget.h
Normal file
76
retroshare-gui/src/gui/elastic/graphwidget.h
Normal file
|
@ -0,0 +1,76 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2006-2007 Trolltech ASA. All rights reserved.
|
||||
**
|
||||
** This file is part of the example classes of the Qt Toolkit.
|
||||
**
|
||||
** This file may be used under the terms of the GNU General Public
|
||||
** License version 2.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 GNU
|
||||
** General Public Licensing requirements will be met:
|
||||
** http://trolltech.com/products/qt/licenses/licensing/opensource/
|
||||
**
|
||||
** If you are unsure which license is appropriate for your use, please
|
||||
** review the following information:
|
||||
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
|
||||
** or contact the sales department at sales@trolltech.com.
|
||||
**
|
||||
** In addition, as a special exception, Trolltech gives you certain
|
||||
** additional rights. These rights are described in the Trolltech GPL
|
||||
** Exception version 1.0, which can be found at
|
||||
** http://www.trolltech.com/products/qt/gplexception/ and in the file
|
||||
** GPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** In addition, as a special exception, Trolltech, as the sole copyright
|
||||
** holder for Qt Designer, grants users of the Qt/Eclipse Integration
|
||||
** plug-in the right for the Qt/Eclipse Integration to link to
|
||||
** functionality provided by Qt Designer and its related libraries.
|
||||
**
|
||||
** Trolltech reserves all rights not expressly granted herein.
|
||||
**
|
||||
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
||||
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef GRAPHWIDGET_H
|
||||
#define GRAPHWIDGET_H
|
||||
|
||||
#include <QtGui/QGraphicsView>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
class Node;
|
||||
class Edge;
|
||||
|
||||
class GraphWidget : public QGraphicsView
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
GraphWidget(QWidget *parent);
|
||||
|
||||
void itemMoved();
|
||||
|
||||
bool clearGraph();
|
||||
void addNode(uint32_t type, std::string id, std::string name);
|
||||
void addEdge(std::string id1, std::string id2);
|
||||
|
||||
protected:
|
||||
void keyPressEvent(QKeyEvent *event);
|
||||
void timerEvent(QTimerEvent *event);
|
||||
void wheelEvent(QWheelEvent *event);
|
||||
void drawBackground(QPainter *painter, const QRectF &rect);
|
||||
|
||||
void scaleView(qreal scaleFactor);
|
||||
|
||||
private:
|
||||
int timerId;
|
||||
Node *centerNode;
|
||||
|
||||
std::map<std::string, Node *> nodeMap;
|
||||
std::list<Edge *> edgeList;
|
||||
};
|
||||
|
||||
#endif
|
218
retroshare-gui/src/gui/elastic/node.cpp
Normal file
218
retroshare-gui/src/gui/elastic/node.cpp
Normal file
|
@ -0,0 +1,218 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2006-2007 Trolltech ASA. All rights reserved.
|
||||
**
|
||||
** This file is part of the example classes of the Qt Toolkit.
|
||||
**
|
||||
** This file may be used under the terms of the GNU General Public
|
||||
** License version 2.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 GNU
|
||||
** General Public Licensing requirements will be met:
|
||||
** http://trolltech.com/products/qt/licenses/licensing/opensource/
|
||||
**
|
||||
** If you are unsure which license is appropriate for your use, please
|
||||
** review the following information:
|
||||
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
|
||||
** or contact the sales department at sales@trolltech.com.
|
||||
**
|
||||
** In addition, as a special exception, Trolltech gives you certain
|
||||
** additional rights. These rights are described in the Trolltech GPL
|
||||
** Exception version 1.0, which can be found at
|
||||
** http://www.trolltech.com/products/qt/gplexception/ and in the file
|
||||
** GPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** In addition, as a special exception, Trolltech, as the sole copyright
|
||||
** holder for Qt Designer, grants users of the Qt/Eclipse Integration
|
||||
** plug-in the right for the Qt/Eclipse Integration to link to
|
||||
** functionality provided by Qt Designer and its related libraries.
|
||||
**
|
||||
** Trolltech reserves all rights not expressly granted herein.
|
||||
**
|
||||
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
||||
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QGraphicsScene>
|
||||
#include <QGraphicsSceneMouseEvent>
|
||||
#include <QPainter>
|
||||
#include <QStyleOption>
|
||||
|
||||
#include "edge.h"
|
||||
#include "node.h"
|
||||
#include "graphwidget.h"
|
||||
|
||||
Node::Node(GraphWidget *graphWidget, uint32_t t, std::string id_in, std::string n)
|
||||
: graph(graphWidget), ntype(t), id(id_in), name(n),
|
||||
mDeterminedBB(false)
|
||||
{
|
||||
setFlag(ItemIsMovable);
|
||||
setZValue(1);
|
||||
}
|
||||
|
||||
void Node::addEdge(Edge *edge)
|
||||
{
|
||||
edgeList << edge;
|
||||
edge->adjust();
|
||||
}
|
||||
|
||||
QList<Edge *> Node::edges() const
|
||||
{
|
||||
return edgeList;
|
||||
}
|
||||
|
||||
void Node::calculateForces()
|
||||
{
|
||||
if (!scene() || scene()->mouseGrabberItem() == this) {
|
||||
newPos = pos();
|
||||
return;
|
||||
}
|
||||
|
||||
// Sum up all forces pushing this item away
|
||||
qreal xvel = 0;
|
||||
qreal yvel = 0;
|
||||
foreach (QGraphicsItem *item, scene()->items()) {
|
||||
Node *node = qgraphicsitem_cast<Node *>(item);
|
||||
if (!node)
|
||||
continue;
|
||||
|
||||
QLineF line(mapFromItem(node, 0, 0), QPointF(0, 0));
|
||||
qreal dx = line.dx();
|
||||
qreal dy = line.dy();
|
||||
double l = 2.0 * (dx * dx + dy * dy);
|
||||
if (l > 0) {
|
||||
xvel += (dx * 150.0) / l;
|
||||
yvel += (dy * 150.0) / l;
|
||||
}
|
||||
}
|
||||
|
||||
// Now subtract all forces pulling items together
|
||||
double weight = (edgeList.size() + 1) * 10;
|
||||
foreach (Edge *edge, edgeList) {
|
||||
QPointF pos;
|
||||
if (edge->sourceNode() == this)
|
||||
pos = mapFromItem(edge->destNode(), 0, 0);
|
||||
else
|
||||
pos = mapFromItem(edge->sourceNode(), 0, 0);
|
||||
xvel += pos.x() / weight;
|
||||
yvel += pos.y() / weight;
|
||||
}
|
||||
|
||||
if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1)
|
||||
xvel = yvel = 0;
|
||||
|
||||
QRectF sceneRect = scene()->sceneRect();
|
||||
newPos = pos() + QPointF(xvel, yvel);
|
||||
newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10));
|
||||
newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10));
|
||||
}
|
||||
|
||||
bool Node::advance()
|
||||
{
|
||||
if (newPos == pos())
|
||||
return false;
|
||||
|
||||
setPos(newPos);
|
||||
return true;
|
||||
}
|
||||
|
||||
QRectF Node::boundingRect() const
|
||||
{
|
||||
qreal adjust = 2;
|
||||
/* add in the size of the text */
|
||||
qreal realwidth = 40;
|
||||
if (mDeterminedBB)
|
||||
{
|
||||
realwidth = mBBWidth + adjust;
|
||||
}
|
||||
if (realwidth < 23 + adjust)
|
||||
{
|
||||
realwidth = 23 + adjust;
|
||||
}
|
||||
|
||||
return QRectF(-10 - adjust, -10 - adjust,
|
||||
realwidth, 23 + adjust);
|
||||
// 23 + adjust, 23 + adjust);
|
||||
}
|
||||
|
||||
|
||||
//QPainterPath Node::shape() const
|
||||
//{
|
||||
// QPainterPath path;
|
||||
// path.addEllipse(-10, -10, 20, 20);
|
||||
// return path;
|
||||
//}
|
||||
|
||||
void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
|
||||
{
|
||||
painter->setPen(Qt::NoPen);
|
||||
painter->setBrush(Qt::darkGray);
|
||||
painter->drawEllipse(-7, -7, 20, 20);
|
||||
|
||||
QColor col0, col1;
|
||||
if (ntype == ELASTIC_NODE_TYPE_OWN)
|
||||
{
|
||||
col0 = QColor(Qt::yellow);
|
||||
col1 = QColor(Qt::darkYellow);
|
||||
}
|
||||
else if (ntype == ELASTIC_NODE_TYPE_FRIEND)
|
||||
{
|
||||
col0 = QColor(Qt::cyan);
|
||||
col1 = QColor(Qt::blue);
|
||||
}
|
||||
else
|
||||
{
|
||||
col0 = QColor(Qt::red);
|
||||
col1 = QColor(Qt::darkRed);
|
||||
}
|
||||
|
||||
QRadialGradient gradient(-3, -3, 10);
|
||||
if (option->state & QStyle::State_Sunken) {
|
||||
gradient.setCenter(3, 3);
|
||||
gradient.setFocalPoint(3, 3);
|
||||
gradient.setColorAt(1, col0.light(120));
|
||||
gradient.setColorAt(0, col1.light(120));
|
||||
} else {
|
||||
gradient.setColorAt(0, col0);
|
||||
gradient.setColorAt(1, col1);
|
||||
}
|
||||
painter->setBrush(gradient);
|
||||
painter->setPen(QPen(Qt::black, 0));
|
||||
painter->drawEllipse(-10, -10, 20, 20);
|
||||
painter->drawText(-10, 0, QString::fromStdString(name));
|
||||
|
||||
if (!mDeterminedBB)
|
||||
{
|
||||
QRect textBox = painter->boundingRect(-10, 0, 400, 20, 0, QString::fromStdString(name));
|
||||
mBBWidth = textBox.width();
|
||||
mDeterminedBB = true;
|
||||
}
|
||||
}
|
||||
|
||||
QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
|
||||
{
|
||||
switch (change) {
|
||||
case ItemPositionHasChanged:
|
||||
foreach (Edge *edge, edgeList)
|
||||
edge->adjust();
|
||||
graph->itemMoved();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
|
||||
return QGraphicsItem::itemChange(change, value);
|
||||
}
|
||||
|
||||
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
update();
|
||||
QGraphicsItem::mousePressEvent(event);
|
||||
}
|
||||
|
||||
void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
update();
|
||||
QGraphicsItem::mouseReleaseEvent(event);
|
||||
}
|
92
retroshare-gui/src/gui/elastic/node.h
Normal file
92
retroshare-gui/src/gui/elastic/node.h
Normal file
|
@ -0,0 +1,92 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2006-2007 Trolltech ASA. All rights reserved.
|
||||
**
|
||||
** This file is part of the example classes of the Qt Toolkit.
|
||||
**
|
||||
** This file may be used under the terms of the GNU General Public
|
||||
** License version 2.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 GNU
|
||||
** General Public Licensing requirements will be met:
|
||||
** http://trolltech.com/products/qt/licenses/licensing/opensource/
|
||||
**
|
||||
** If you are unsure which license is appropriate for your use, please
|
||||
** review the following information:
|
||||
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
|
||||
** or contact the sales department at sales@trolltech.com.
|
||||
**
|
||||
** In addition, as a special exception, Trolltech gives you certain
|
||||
** additional rights. These rights are described in the Trolltech GPL
|
||||
** Exception version 1.0, which can be found at
|
||||
** http://www.trolltech.com/products/qt/gplexception/ and in the file
|
||||
** GPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** In addition, as a special exception, Trolltech, as the sole copyright
|
||||
** holder for Qt Designer, grants users of the Qt/Eclipse Integration
|
||||
** plug-in the right for the Qt/Eclipse Integration to link to
|
||||
** functionality provided by Qt Designer and its related libraries.
|
||||
**
|
||||
** Trolltech reserves all rights not expressly granted herein.
|
||||
**
|
||||
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
||||
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef NODE_H
|
||||
#define NODE_H
|
||||
|
||||
#include <QGraphicsItem>
|
||||
#include <QList>
|
||||
|
||||
#include <string>
|
||||
|
||||
#define ELASTIC_NODE_TYPE_OWN 1
|
||||
#define ELASTIC_NODE_TYPE_FRIEND 2
|
||||
#define ELASTIC_NODE_TYPE_FOF 3
|
||||
|
||||
class Edge;
|
||||
class GraphWidget;
|
||||
class QGraphicsSceneMouseEvent;
|
||||
|
||||
class Node : public QGraphicsItem
|
||||
{
|
||||
public:
|
||||
Node(GraphWidget *graphWidget, uint32_t t, std::string id_in, std::string n);
|
||||
|
||||
void addEdge(Edge *edge);
|
||||
QList<Edge *> edges() const;
|
||||
|
||||
enum { Type = UserType + 1 };
|
||||
int type() const { return Type; }
|
||||
|
||||
void calculateForces();
|
||||
bool advance();
|
||||
|
||||
QRectF boundingRect() const;
|
||||
//QPainterPath shape() const;
|
||||
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
|
||||
|
||||
protected:
|
||||
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
|
||||
|
||||
void mousePressEvent(QGraphicsSceneMouseEvent *event);
|
||||
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
|
||||
|
||||
private:
|
||||
QList<Edge *> edgeList;
|
||||
QPointF newPos;
|
||||
GraphWidget *graph;
|
||||
|
||||
/* extra information */
|
||||
uint32_t ntype; /* Ourself, friend, fof */
|
||||
std::string id;
|
||||
std::string name;
|
||||
|
||||
bool mDeterminedBB;
|
||||
uint32_t mBBWidth;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue