mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-07-23 14:41:04 -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
|
@ -30,7 +30,8 @@ DEPENDPATH += . \
|
||||||
gui\common\
|
gui\common\
|
||||||
gui\Settings \
|
gui\Settings \
|
||||||
gui\toaster \
|
gui\toaster \
|
||||||
gui\authdlg
|
gui\authdlg \
|
||||||
|
gui\elastic
|
||||||
|
|
||||||
INCLUDEPATH += . \
|
INCLUDEPATH += . \
|
||||||
games/qcheckers \
|
games/qcheckers \
|
||||||
|
@ -138,7 +139,10 @@ HEADERS += rshare.h \
|
||||||
gui/channels/channelBrowserDialog.h \
|
gui/channels/channelBrowserDialog.h \
|
||||||
gui/channels/channelStatsDialog.h \
|
gui/channels/channelStatsDialog.h \
|
||||||
gui/channels/myChannelsDialog.h \
|
gui/channels/myChannelsDialog.h \
|
||||||
gui/channels/mySubscriptionsDialog.h
|
gui/channels/mySubscriptionsDialog.h \
|
||||||
|
gui/elastic/graphwidget.h \
|
||||||
|
gui/elastic/edge.h \
|
||||||
|
gui/elastic/node.h
|
||||||
|
|
||||||
|
|
||||||
FORMS += gui/ChatDialog.ui \
|
FORMS += gui/ChatDialog.ui \
|
||||||
|
@ -283,7 +287,10 @@ SOURCES += main.cpp \
|
||||||
gui/channels/channelBrowserDialog.cpp \
|
gui/channels/channelBrowserDialog.cpp \
|
||||||
gui/channels/channelStatsDialog.cpp \
|
gui/channels/channelStatsDialog.cpp \
|
||||||
gui/channels/myChannelsDialog.cpp \
|
gui/channels/myChannelsDialog.cpp \
|
||||||
gui/channels/mySubscriptionsDialog.cpp
|
gui/channels/mySubscriptionsDialog.cpp \
|
||||||
|
gui/elastic/graphwidget.cpp \
|
||||||
|
gui/elastic/edge.cpp \
|
||||||
|
gui/elastic/node.cpp
|
||||||
|
|
||||||
RESOURCES += gui/images.qrc lang/lang.qrc games/qcheckers/qcheckers.qrc apps/smplayer/icons.qrc
|
RESOURCES += gui/images.qrc lang/lang.qrc games/qcheckers/qcheckers.qrc apps/smplayer/icons.qrc
|
||||||
TRANSLATIONS += \
|
TRANSLATIONS += \
|
||||||
|
|
|
@ -36,22 +36,14 @@ NetworkView::NetworkView(QWidget *parent)
|
||||||
/* Invoke the Qt Designer generated object setup routine */
|
/* Invoke the Qt Designer generated object setup routine */
|
||||||
ui.setupUi(this);
|
ui.setupUi(this);
|
||||||
|
|
||||||
mScene = new QGraphicsScene();
|
//mScene = new QGraphicsScene();
|
||||||
ui.graphicsView->setScene(mScene);
|
//ui.graphicsView->setScene(mScene);
|
||||||
|
|
||||||
|
|
||||||
//connect( ui.linkTreeWidget, SIGNAL( customContextMenuRequested( QPoint ) ), this, SLOT( linkTreeWidgetCostumPopupMenu( QPoint ) ) );
|
|
||||||
|
|
||||||
|
|
||||||
/* link combos */
|
|
||||||
// connect( ui.rankComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( changedSortRank( int ) ) );
|
|
||||||
// connect( ui.periodComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( changedSortPeriod( int ) ) );
|
|
||||||
// connect( ui.fromComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( changedSortFrom( int ) ) );
|
|
||||||
// connect( ui.topComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( changedSortTop( int ) ) );
|
|
||||||
|
|
||||||
/* add button */
|
/* add button */
|
||||||
connect( ui.refreshButton, SIGNAL( clicked( void ) ), this, SLOT( insertPeers( void ) ) );
|
connect( ui.refreshButton, SIGNAL( clicked( void ) ), this, SLOT( insertPeers( void ) ) );
|
||||||
connect( mScene, SIGNAL( changed ( const QList<QRectF> & ) ), this, SLOT ( changedScene( void ) ) );
|
// connect( mScene, SIGNAL( changed ( const QList<QRectF> & ) ), this, SLOT ( changedScene( void ) ) );
|
||||||
|
|
||||||
|
|
||||||
/* hide the Tree +/- */
|
/* hide the Tree +/- */
|
||||||
// ui.linkTreeWidget -> setRootIsDecorated( false );
|
// ui.linkTreeWidget -> setRootIsDecorated( false );
|
||||||
|
|
||||||
|
@ -102,9 +94,7 @@ void NetworkView::clearPeerItems()
|
||||||
|
|
||||||
for(pit = mPeerItems.begin(); pit != mPeerItems.end(); pit++)
|
for(pit = mPeerItems.begin(); pit != mPeerItems.end(); pit++)
|
||||||
{
|
{
|
||||||
//mScene->removeItem(pit->second);
|
//mScene->destroyItemGroup((QGraphicsItemGroup *) pit->second);
|
||||||
mScene->destroyItemGroup((QGraphicsItemGroup *) pit->second);
|
|
||||||
//delete (pit->second);
|
|
||||||
}
|
}
|
||||||
mPeerItems.clear();
|
mPeerItems.clear();
|
||||||
}
|
}
|
||||||
|
@ -116,7 +106,7 @@ void NetworkView::clearOtherItems()
|
||||||
|
|
||||||
for(oit = mOtherItems.begin(); oit != mOtherItems.end(); oit++)
|
for(oit = mOtherItems.begin(); oit != mOtherItems.end(); oit++)
|
||||||
{
|
{
|
||||||
mScene->removeItem(*oit);
|
//mScene->removeItem(*oit);
|
||||||
delete (*oit);
|
delete (*oit);
|
||||||
}
|
}
|
||||||
mOtherItems.clear();
|
mOtherItems.clear();
|
||||||
|
@ -129,7 +119,7 @@ void NetworkView::clearLineItems()
|
||||||
|
|
||||||
for(oit = mLineItems.begin(); oit != mLineItems.end(); oit++)
|
for(oit = mLineItems.begin(); oit != mLineItems.end(); oit++)
|
||||||
{
|
{
|
||||||
mScene->removeItem(*oit);
|
//mScene->removeItem(*oit);
|
||||||
delete (*oit);
|
delete (*oit);
|
||||||
}
|
}
|
||||||
mLineItems.clear();
|
mLineItems.clear();
|
||||||
|
@ -139,80 +129,33 @@ void NetworkView::clearLineItems()
|
||||||
void NetworkView::insertPeers()
|
void NetworkView::insertPeers()
|
||||||
{
|
{
|
||||||
/* clear graphics scene */
|
/* clear graphics scene */
|
||||||
clearPeerItems();
|
ui.graphicsView->clearGraph();
|
||||||
clearOtherItems();
|
|
||||||
|
|
||||||
/* add all friends */
|
/* add all friends */
|
||||||
std::list<std::string> ids;
|
std::list<std::string> ids;
|
||||||
std::list<std::string>::iterator it;
|
std::list<std::string>::iterator it;
|
||||||
rsPeers->getOthersList(ids);
|
rsPeers->getOthersList(ids);
|
||||||
ids.push_back(rsPeers->getOwnId()); /* add yourself too */
|
|
||||||
|
|
||||||
std::cerr << "NetworkView::insertPeers()" << std::endl;
|
std::cerr << "NetworkView::insertPeers()" << std::endl;
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
uint32_t type = 0;
|
||||||
for(it = ids.begin(); it != ids.end(); it++, i++)
|
for(it = ids.begin(); it != ids.end(); it++, i++)
|
||||||
{
|
{
|
||||||
/* *** */
|
/* *** */
|
||||||
|
std::string name = rsPeers->getPeerName(*it);
|
||||||
|
|
||||||
QString name = QString::fromStdString(rsPeers->getPeerName(*it));
|
if (rsPeers->isFriend(*it))
|
||||||
QGraphicsTextItem *gti = new QGraphicsTextItem(name);
|
|
||||||
mScene->addItem(gti);
|
|
||||||
|
|
||||||
//QPointF textPoint( i * 10, i * 20);
|
|
||||||
//gti->setPos(textPoint);
|
|
||||||
gti->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
|
|
||||||
gti->setZValue(20);
|
|
||||||
|
|
||||||
QRectF textBound = gti->boundingRect();
|
|
||||||
|
|
||||||
/* work out bounds for circle */
|
|
||||||
qreal diameter = textBound.width() + 10;
|
|
||||||
if (diameter < 40)
|
|
||||||
diameter = 40;
|
|
||||||
|
|
||||||
qreal x = textBound.left() + (textBound.width() / 2.0) - (diameter/ 2.0);
|
|
||||||
qreal y = textBound.top() + (textBound.height() / 2.0) - (diameter/ 2.0);
|
|
||||||
|
|
||||||
QGraphicsEllipseItem *gei = new QGraphicsEllipseItem(x, y, diameter, diameter);
|
|
||||||
mScene->addItem(gei);
|
|
||||||
gei->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
|
|
||||||
gei->setZValue(10);
|
|
||||||
|
|
||||||
/* colour depends on Friend... */
|
|
||||||
if (*it == rsPeers->getOwnId())
|
|
||||||
{
|
{
|
||||||
gei->setBrush(QBrush(Qt::green));
|
type = 2;
|
||||||
}
|
|
||||||
else if (rsPeers->isFriend(*it))
|
|
||||||
{
|
|
||||||
gei->setBrush(QBrush(Qt::blue));
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
gei->setBrush(QBrush(Qt::red));
|
type = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
QGraphicsItemGroup *gig = new QGraphicsItemGroup();
|
ui.graphicsView->addNode(type, *it, name);
|
||||||
mScene->addItem(gig);
|
|
||||||
|
|
||||||
gig->addToGroup(gei);
|
|
||||||
gig->addToGroup(gti);
|
|
||||||
gig->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//, const QPen & pen = QPen(), const QBrush & brush = QBrush() )
|
|
||||||
|
|
||||||
mPeerItems[*it] = gig;
|
|
||||||
mOtherItems.push_back(gti);
|
|
||||||
mOtherItems.push_back(gei);
|
|
||||||
|
|
||||||
std::cerr << "NetworkView::insertPeers() Added Friend: " << *it << std::endl;
|
std::cerr << "NetworkView::insertPeers() Added Friend: " << *it << std::endl;
|
||||||
std::cerr << "\t At: " << i*5 << "," << i*10 << std::endl;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
insertConnections();
|
insertConnections();
|
||||||
|
@ -221,106 +164,73 @@ void NetworkView::insertPeers()
|
||||||
|
|
||||||
void NetworkView::insertConnections()
|
void NetworkView::insertConnections()
|
||||||
{
|
{
|
||||||
clearLineItems();
|
/* iterate through all friends */
|
||||||
|
std::list<std::string> ids;
|
||||||
/* iterate through all peerItems .... and find any proxies */
|
|
||||||
std::map<std::string, QGraphicsItem *>::const_iterator pit, pit2;
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
for(pit = mPeerItems.begin(); pit != mPeerItems.end(); pit++, i++)
|
|
||||||
{
|
|
||||||
std::list<std::string> friendList;
|
|
||||||
std::list<std::string>::iterator it;
|
std::list<std::string>::iterator it;
|
||||||
|
std::map<std::string, std::list<std::string> > connLists;
|
||||||
|
|
||||||
rsDisc->getDiscFriends(pit->first, friendList);
|
std::string ownId = rsPeers->getOwnId();
|
||||||
int j = 0;
|
rsPeers->getOthersList(ids);
|
||||||
|
|
||||||
for(it = friendList.begin(); it != friendList.end(); it++)
|
std::cerr << "NetworkView::insertConnections()" << std::endl;
|
||||||
{
|
|
||||||
//pit2 = std::find(pit, mPeerItems.end(), *it);
|
|
||||||
pit2 = mPeerItems.find(*it);
|
|
||||||
if (pit2 == mPeerItems.end())
|
|
||||||
{
|
|
||||||
std::cerr << " Failed to Find: " << *it;
|
|
||||||
std::cerr << std::endl;
|
|
||||||
continue; /* missing */
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pit == pit2)
|
|
||||||
continue; /* skip same one */
|
|
||||||
|
|
||||||
std::cerr << " Connecting: " << pit->first << " to " << pit2->first;
|
|
||||||
std::cerr << std::endl;
|
|
||||||
|
|
||||||
QPointF pos1 = (pit->second)->pos();
|
|
||||||
QRectF bound1 = (pit->second)->boundingRect();
|
|
||||||
|
|
||||||
QPointF pos2 = (pit2->second)->pos();
|
|
||||||
QRectF bound2 = (pit2->second)->boundingRect();
|
|
||||||
|
|
||||||
pos1 += QPointF(bound1.width() / 2.0, bound1.height() / 2.0);
|
|
||||||
pos2 += QPointF(bound2.width() / 2.0, bound2.height() / 2.0);
|
|
||||||
|
|
||||||
QLineF line(pos1, pos2);
|
|
||||||
|
|
||||||
QGraphicsLineItem *gli = mScene->addLine(line);
|
|
||||||
|
|
||||||
mLineItems.push_back(gli);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* debugging all lines */
|
|
||||||
#if 0
|
|
||||||
/* iterate through all peerItems .... and find any proxies */
|
|
||||||
std::map<std::string, QGraphicsItem *>::const_iterator pit, pit2;
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for(pit = mPeerItems.begin(); pit != mPeerItems.end(); pit++, i++)
|
for(it = ids.begin(); it != ids.end(); it++, i++)
|
||||||
{
|
{
|
||||||
int j = 0;
|
if (rsPeers->isFriend(*it))
|
||||||
for(pit2 = mPeerItems.begin(); (pit2 != mPeerItems.end()) && (j < i); pit2++, j++)
|
|
||||||
{
|
{
|
||||||
if (pit == pit2)
|
/* add own connection (check for it first) */
|
||||||
continue; /* skip same one */
|
std::list<std::string> &refList = connLists[*it];
|
||||||
|
if (refList.end() == std::find(refList.begin(), refList.end(), ownId))
|
||||||
|
{
|
||||||
|
connLists[ownId].push_back(*it);
|
||||||
|
connLists[*it].push_back(ownId);
|
||||||
|
|
||||||
std::cerr << " Connecting: " << pit->first << " to " << pit2->first;
|
ui.graphicsView->addEdge("", *it);
|
||||||
|
std::cerr << "NetworkView: Adding Edge: Self -> " << *it;
|
||||||
|
std::cerr << std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cerr << "NetworkView: Already Edge: Self -> " << *it;
|
||||||
std::cerr << std::endl;
|
std::cerr << std::endl;
|
||||||
|
|
||||||
QPointF pos1 = (pit->second)->pos();
|
|
||||||
QPointF pos2 = (pit2->second)->pos();
|
|
||||||
QLineF line(pos1, pos2);
|
|
||||||
|
|
||||||
QGraphicsLineItem *gli = mScene->addLine(line);
|
|
||||||
|
|
||||||
mLineItems.push_back(gli);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
mLineChanged = true;
|
std::list<std::string> friendList;
|
||||||
|
std::list<std::string>::iterator it2;
|
||||||
|
|
||||||
|
rsDisc->getDiscFriends(*it, friendList);
|
||||||
|
int j = 0;
|
||||||
|
|
||||||
|
for(it2 = friendList.begin(); it2 != friendList.end(); it2++)
|
||||||
|
{
|
||||||
|
if (*it == *it2)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
/* check that we haven't added this one already */
|
||||||
|
std::list<std::string> &refList = connLists[*it];
|
||||||
|
if (refList.end() == std::find(refList.begin(), refList.end(), *it2))
|
||||||
|
{
|
||||||
|
connLists[*it2].push_back(*it);
|
||||||
|
connLists[*it].push_back(*it2);
|
||||||
|
|
||||||
|
ui.graphicsView->addEdge(*it, *it2);
|
||||||
|
std::cerr << "NetworkView: Adding Edge: " << *it << " <-> " << *it2;
|
||||||
|
std::cerr << std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cerr << "NetworkView: Already Edge: " << *it << " <-> " << *it2;
|
||||||
|
std::cerr << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void NetworkView::changedScene()
|
void NetworkView::changedScene()
|
||||||
{
|
{
|
||||||
std::cerr << "NetworkView::changedScene()" << std::endl;
|
|
||||||
|
|
||||||
QList<QGraphicsItem *> items = mScene->selectedItems();
|
|
||||||
|
|
||||||
std::cerr << "NetworkView::changedScene() Items selected: " << items.size() << std::endl;
|
|
||||||
|
|
||||||
/* if an item was selected and moved - then redraw lines */
|
|
||||||
if (mLineChanged)
|
|
||||||
{
|
|
||||||
mLineChanged = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (items.size() > 0)
|
|
||||||
{
|
|
||||||
mScene->clearSelection();
|
|
||||||
insertConnections();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -78,7 +78,7 @@ p, li { white-space: pre-wrap; }
|
||||||
</spacer>
|
</spacer>
|
||||||
</item>
|
</item>
|
||||||
<item row="1" column="0" colspan="2" >
|
<item row="1" column="0" colspan="2" >
|
||||||
<widget class="QGraphicsView" name="graphicsView" />
|
<widget class="GraphWidget" name="graphicsView" />
|
||||||
</item>
|
</item>
|
||||||
<item row="2" column="0" colspan="2" >
|
<item row="2" column="0" colspan="2" >
|
||||||
<layout class="QGridLayout" >
|
<layout class="QGridLayout" >
|
||||||
|
@ -131,6 +131,13 @@ p, li { white-space: pre-wrap; }
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
|
<customwidgets>
|
||||||
|
<customwidget>
|
||||||
|
<class>GraphWidget</class>
|
||||||
|
<extends>QGraphicsView</extends>
|
||||||
|
<header>gui/elastic/graphwidget.h</header>
|
||||||
|
</customwidget>
|
||||||
|
</customwidgets>
|
||||||
<resources/>
|
<resources/>
|
||||||
<connections/>
|
<connections/>
|
||||||
</ui>
|
</ui>
|
||||||
|
|
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