fixed force calculation in elastic model. Still need to figure out what connexions we show

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3690 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2010-10-22 20:47:08 +00:00
parent b7b8cfe860
commit c4b5f41446
9 changed files with 941 additions and 1014 deletions

View File

@ -34,6 +34,7 @@
#include "common/RSTreeWidgetItem.h" #include "common/RSTreeWidgetItem.h"
#include "NetworkDialog.h" #include "NetworkDialog.h"
#include "TrustView.h" #include "TrustView.h"
#include "NetworkView.h"
#include "GenCertDialog.h" #include "GenCertDialog.h"
#include "connect/ConfCertDialog.h" #include "connect/ConfCertDialog.h"
#include "settings/rsharesettings.h" #include "settings/rsharesettings.h"
@ -144,8 +145,8 @@ NetworkDialog::NetworkDialog(QWidget *parent)
ui.connecttreeWidget->sortItems( 1, Qt::AscendingOrder ); ui.connecttreeWidget->sortItems( 1, Qt::AscendingOrder );
ui.unvalidGPGkeyWidget->sortItems( 1, Qt::AscendingOrder ); ui.unvalidGPGkeyWidget->sortItems( 1, Qt::AscendingOrder );
//ui.networkTab->addTab(new NetworkView(),QString(tr("Network View")));
ui.networkTab->addTab(new TrustView(),QString(tr("Authentication matrix"))); ui.networkTab->addTab(new TrustView(),QString(tr("Authentication matrix")));
ui.networkTab->addTab(new NetworkView(),QString(tr("Network View")));
QString version = "-"; QString version = "-";
std::map<std::string, std::string>::iterator vit; std::map<std::string, std::string>::iterator vit;

View File

@ -36,18 +36,18 @@ 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);
/* 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 Settings frame */ /* Hide Settings frame */
shownwSettingsFrame(false); shownwSettingsFrame(false);
connect( ui.nviewsettingsButton, SIGNAL(toggled(bool)), this, SLOT(shownwSettingsFrame(bool))); connect( ui.nviewsettingsButton, SIGNAL(toggled(bool)), this, SLOT(shownwSettingsFrame(bool)));
insertPeers(); insertPeers();
/* hide the Tree +/- */ /* hide the Tree +/- */
@ -86,7 +86,7 @@ void NetworkView::clearPeerItems()
for(pit = mPeerItems.begin(); pit != mPeerItems.end(); pit++) for(pit = mPeerItems.begin(); pit != mPeerItems.end(); pit++)
{ {
//mScene->destroyItemGroup((QGraphicsItemGroup *) pit->second); mScene->destroyItemGroup((QGraphicsItemGroup *) pit->second);
} }
mPeerItems.clear(); mPeerItems.clear();
} }
@ -98,7 +98,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();
@ -111,7 +111,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();
@ -126,77 +126,100 @@ void NetworkView::insertPeers()
/* 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->getGPGAllList(ids);
std::string ownId = rsPeers->getGPGOwnId(); std::string ownId = rsPeers->getGPGOwnId();
RsPeerDetails detail ;
if(!rsPeers->getPeerDetails(ownId,detail))
return ;
ids = detail.gpgSigners ;
std::cerr << "NetworkView::insertPeers()" << std::endl; std::cerr << "NetworkView::insertPeers()" << std::endl;
/* get the list of friends' issuers, as we flag them specially */
std::list<std::string> fids;
//rsPeers->getPGPFriendList(fids);
int i = 0; int i = 0;
uint32_t type = 0; uint32_t flags = 0;
std::map<std::string,GraphWidget::NodeId> node_ids ;
for(it = ids.begin(); it != ids.end(); it++, i++) for(it = ids.begin(); it != ids.end(); it++, i++)
{ {
if (*it == ownId) if (*it == ownId)
{ flags |= GraphWidget::ELASTIC_NODE_FLAG_OWN ;
continue;
}
/* *** */ /* *** */
RsPeerDetails detail;
if (!rsPeers->getPeerDetails(*it, detail)) if (!rsPeers->getPeerDetails(*it, detail))
{ {
continue; continue;
} }
switch(detail.validLvl) switch(detail.validLvl)
{ {
default:
case GPGME_VALIDITY_UNKNOWN: case GPGME_VALIDITY_UNKNOWN:
case GPGME_VALIDITY_UNDEFINED: case GPGME_VALIDITY_UNDEFINED:
case GPGME_VALIDITY_NEVER: case GPGME_VALIDITY_NEVER:
/* lots of fall through */ /* lots of fall through */
type = ELASTIC_NODE_TYPE_FOF;
break; break;
case GPGME_VALIDITY_MARGINAL: case GPGME_VALIDITY_MARGINAL:
/* lots of fall through */ /* lots of fall through */
type = ELASTIC_NODE_TYPE_MARGINALAUTH; flags |= GraphWidget::ELASTIC_NODE_FLAG_MARGINALAUTH;
break; break;
case GPGME_VALIDITY_FULL: case GPGME_VALIDITY_FULL:
case GPGME_VALIDITY_ULTIMATE: case GPGME_VALIDITY_ULTIMATE:
/* lots of fall through */ /* lots of fall through */
type = ELASTIC_NODE_TYPE_AUTHED; flags |= GraphWidget::ELASTIC_NODE_FLAG_AUTHED;
if (fids.end() != std::find(fids.begin(), fids.end(), *it))
{
type = ELASTIC_NODE_TYPE_FRIEND;
}
break; break;
default:
break ;
} }
ui.graphicsView->addNode(type, *it, detail.name);
node_ids[*it] = ui.graphicsView->addNode(" "+detail.name+"@"+*it,flags);
std::cerr << "NetworkView::insertPeers() Added Friend: " << *it << std::endl; std::cerr << "NetworkView::insertPeers() Added Friend: " << *it << std::endl;
} }
insertConnections(); /* iterate through all friends */
insertSignatures();
std::cerr << "NetworkView::insertSignatures()" << std::endl;
for(it = ids.begin(); it != ids.end(); it++, i++)
{
RsPeerDetails detail;
if (!rsPeers->getPeerDetails(*it, detail))
{
continue;
}
for(std::list<std::string>::const_iterator sit(detail.gpgSigners.begin()); sit != detail.gpgSigners.end(); sit++)
{
if ((*it == ownId || *sit == ownId) && *it < *sit)
{
std::cerr << "NetworkView: Adding Arrow: ";
std::cerr << *sit << " <-> " << *it;
std::cerr << std::endl;
if(node_ids.find(*sit) != node_ids.end())
ui.graphicsView->addEdge(node_ids[*sit], node_ids[*it]);
}
}
}
} }
void NetworkView::insertConnections() void NetworkView::insertConnections()
{ {
#if 0
/* iterate through all friends */ /* iterate through all friends */
std::list<std::string> fids, ids; std::list<std::string> fids, ids;
std::list<std::string>::iterator it; std::list<std::string>::iterator it;
//std::string ownId = rsPeers->getGPGOwnId(); std::string ownId = rsPeers->getGPGOwnId();
//rsPeers->getPGPAllList(ids); rsPeers->getPGPAllList(ids);
//rsPeers->getPGPFriendList(fids); rsPeers->getPGPFriendList(fids);
std::cerr << "NetworkView::insertConnections()" << std::endl; std::cerr << "NetworkView::insertConnections()" << std::endl;
@ -208,7 +231,6 @@ void NetworkView::insertConnections()
std::cerr << std::endl; std::cerr << std::endl;
} }
#if 0
int i = 0; int i = 0;
for(it = ids.begin(); it != ids.end(); it++, i++) for(it = ids.begin(); it != ids.end(); it++, i++)
{ {
@ -267,37 +289,6 @@ void NetworkView::insertConnections()
void NetworkView::insertSignatures() void NetworkView::insertSignatures()
{ {
/* iterate through all friends */
std::list<std::string> ids;
std::list<std::string>::iterator it, sit;
std::string ownId = rsPeers->getGPGOwnId();
rsPeers->getGPGAllList(ids);
std::cerr << "NetworkView::insertSignatures()" << std::endl;
int i = 0;
for(it = ids.begin(); it != ids.end(); it++, i++)
{
RsPeerDetails detail;
if (!rsPeers->getPeerDetails(*it, detail))
{
continue;
}
for(sit = detail.gpgSigners.begin(); sit != detail.gpgSigners.end(); sit++)
{
if (*it != *sit)
{
std::cerr << "NetworkView: Adding Arrow: ";
std::cerr << *sit << " <-> " << *it;
std::cerr << std::endl;
ui.graphicsView->addArrow(*sit, *it);
}
}
}
} }
void NetworkView::changedScene() void NetworkView::changedScene()

View File

@ -50,8 +50,10 @@ Arrow::Arrow(Node *sourceNode, Node *destNode)
setAcceptedMouseButtons(0); setAcceptedMouseButtons(0);
source = sourceNode; source = sourceNode;
dest = destNode; dest = destNode;
#ifdef SUSP
source->addArrow(this); source->addArrow(this);
dest->addArrow(this); dest->addArrow(this);
#endif
adjust(); adjust();
} }

View File

@ -1,36 +1,41 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2006-2007 Trolltech ASA. All rights reserved. ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
** **
** This file is part of the example classes of the Qt Toolkit. ** 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 ** $QT_BEGIN_LICENSE:LGPL$
** License version 2.0 as published by the Free Software Foundation ** Commercial Usage
** and appearing in the file LICENSE.GPL included in the packaging of ** Licensees holding valid Qt Commercial licenses may use this file in
** this file. Please review the following information to ensure GNU ** accordance with the Qt Commercial License Agreement provided with the
** General Public Licensing requirements will be met: ** Software or, alternatively, in accordance with the terms contained in
** http://trolltech.com/products/qt/licenses/licensing/opensource/ ** 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.0, 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 are unsure which license is appropriate for your use, please ** If you are unsure which license is appropriate for your use, please
** review the following information: ** contact the sales department at qt-sales@nokia.com.
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview ** $QT_END_LICENSE$
** 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.
** **
****************************************************************************/ ****************************************************************************/
@ -88,6 +93,8 @@ void Edge::adjust()
QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0)); QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
qreal length = line.length(); qreal length = line.length();
if(length==0.0f)
length=1.0 ;
QPointF edgeOffset((line.dx() * 10) / length, (line.dy() * 10) / length); QPointF edgeOffset((line.dx() * 10) / length, (line.dy() * 10) / length);
prepareGeometryChange(); prepareGeometryChange();
@ -119,21 +126,48 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter->drawLine(line); painter->drawLine(line);
return ;
// Draw the arrows if there's enough room // 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, float length = line.length() ;
cos(angle + Pi / 3) * arrowSize); float cos_theta,sin_theta ;
QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi / 3) * arrowSize,
cos(angle + Pi - Pi / 3) * arrowSize); if(length == 0.0f)
QPointF destArrowP1 = destPoint + QPointF(sin(angle - Pi / 3) * arrowSize, {
cos(angle - Pi / 3) * arrowSize); cos_theta = 1.0f ;
QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize, sin_theta = 0.0f ;
cos(angle - Pi + Pi / 3) * arrowSize); }
else
{
cos_theta = line.dx() / length ;
sin_theta =-line.dy() / length ;
}
static const float cos_pi_over_3 = 0.5 ;
static const float sin_pi_over_3 = 0.5 * sqrt(3) ;
static const float cos_2_pi_over_3 =-0.5 ;
static const float sin_2_pi_over_3 = 0.5 * sqrt(3) ;
float cos_theta_plus_pi_over_3 = cos_theta * cos_pi_over_3 - sin_theta * sin_pi_over_3 ;
float sin_theta_plus_pi_over_3 = sin_theta * cos_pi_over_3 + cos_theta * sin_pi_over_3 ;
float cos_theta_mins_pi_over_3 = cos_theta_plus_pi_over_3 + 2 * sin_theta * sin_pi_over_3 ;
float sin_theta_mins_pi_over_3 = sin_theta_plus_pi_over_3 - 2 * cos_theta * sin_pi_over_3 ;
float cos_theta_plus_2_pi_over_3 = cos_theta * cos_2_pi_over_3 - sin_theta * sin_2_pi_over_3 ;
float sin_theta_plus_2_pi_over_3 = sin_theta * cos_2_pi_over_3 + cos_theta * sin_2_pi_over_3 ;
float cos_theta_mins_2_pi_over_3 = cos_theta_plus_2_pi_over_3 + 2 * sin_theta * sin_2_pi_over_3 ;
float sin_theta_mins_2_pi_over_3 = sin_theta_plus_2_pi_over_3 - 2 * cos_theta * sin_2_pi_over_3 ;
QPointF sourceArrowP1 = sourcePoint + QPointF( sin_theta_plus_pi_over_3 * arrowSize, cos_theta_plus_pi_over_3 * arrowSize);
QPointF sourceArrowP2 = sourcePoint + QPointF(sin_theta_plus_2_pi_over_3 * arrowSize, cos_theta_plus_2_pi_over_3 * arrowSize);
QPointF destArrowP1 = destPoint + QPointF( sin_theta_mins_pi_over_3 * arrowSize, cos_theta_mins_pi_over_3 * arrowSize);
QPointF destArrowP2 = destPoint + QPointF(sin_theta_mins_2_pi_over_3 * arrowSize, cos_theta_mins_2_pi_over_3 * arrowSize);
painter->setBrush(Qt::black); painter->setBrush(Qt::black);
painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2); painter->drawConvexPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2);
painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2); painter->drawConvexPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);
} }

View File

@ -1,73 +1,78 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2006-2007 Trolltech ASA. All rights reserved. ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** ** Contact: Qt Software Information (qt-info@nokia.com)
** This file is part of the example classes of the Qt Toolkit. **
** ** 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 ** $QT_BEGIN_LICENSE:LGPL$
** and appearing in the file LICENSE.GPL included in the packaging of ** Commercial Usage
** this file. Please review the following information to ensure GNU ** Licensees holding valid Qt Commercial licenses may use this file in
** General Public Licensing requirements will be met: ** accordance with the Qt Commercial License Agreement provided with the
** http://trolltech.com/products/qt/licenses/licensing/opensource/ ** Software or, alternatively, in accordance with the terms contained in
** ** a written agreement between you and Nokia.
** If you are unsure which license is appropriate for your use, please **
** review the following information: ** GNU Lesser General Public License Usage
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview ** Alternatively, this file may be used under the terms of the GNU Lesser
** or contact the sales department at sales@trolltech.com. ** General Public License version 2.1 as published by the Free Software
** ** Foundation and appearing in the file LICENSE.LGPL included in the
** In addition, as a special exception, Trolltech gives you certain ** packaging of this file. Please review the following information to
** additional rights. These rights are described in the Trolltech GPL ** ensure the GNU Lesser General Public License version 2.1 requirements
** Exception version 1.0, which can be found at ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
** http://www.trolltech.com/products/qt/gplexception/ and in the file **
** GPL_EXCEPTION.txt in this package. ** In addition, as a special exception, Nokia gives you certain
** ** additional rights. These rights are described in the Nokia Qt LGPL
** In addition, as a special exception, Trolltech, as the sole copyright ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** holder for Qt Designer, grants users of the Qt/Eclipse Integration ** package.
** plug-in the right for the Qt/Eclipse Integration to link to **
** functionality provided by Qt Designer and its related libraries. ** GNU General Public License Usage
** ** Alternatively, this file may be used under the terms of the GNU
** Trolltech reserves all rights not expressly granted herein. ** General Public License version 3.0 as published by the Free Software
** ** Foundation and appearing in the file LICENSE.GPL included in the
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** packaging of this file. Please review the following information to
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ensure the GNU General Public License version 3.0 requirements will be
** ** met: http://www.gnu.org/copyleft/gpl.html.
****************************************************************************/ **
** If you are unsure which license is appropriate for your use, please
#ifndef EDGE_H ** contact the sales department at qt-sales@nokia.com.
#define EDGE_H ** $QT_END_LICENSE$
**
#include <QGraphicsItem> ****************************************************************************/
class Node; #ifndef EDGE_H
#define EDGE_H
class Edge : public QGraphicsItem
{ #include <QGraphicsItem>
public:
Edge(Node *sourceNode, Node *destNode); class Node;
~Edge();
class Edge : public QGraphicsItem
Node *sourceNode() const; {
void setSourceNode(Node *node); public:
Edge(Node *sourceNode, Node *destNode);
Node *destNode() const; ~Edge();
void setDestNode(Node *node);
Node *sourceNode() const;
void adjust(); void setSourceNode(Node *node);
enum { Type = UserType + 2 }; Node *destNode() const;
int type() const { return Type; } void setDestNode(Node *node);
protected: void adjust();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); enum { Type = UserType + 2 };
int type() const { return Type; }
private:
Node *source, *dest; protected:
QRectF boundingRect() const;
QPointF sourcePoint; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QPointF destPoint;
qreal arrowSize; private:
}; Node *source, *dest;
#endif QPointF sourcePoint;
QPointF destPoint;
qreal arrowSize;
};
#endif

View File

@ -1,210 +1,207 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2006-2007 Trolltech ASA. All rights reserved. ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
** **
** This file is part of the example classes of the Qt Toolkit. ** 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 ** $QT_BEGIN_LICENSE:LGPL$
** License version 2.0 as published by the Free Software Foundation ** Commercial Usage
** and appearing in the file LICENSE.GPL included in the packaging of ** Licensees holding valid Qt Commercial licenses may use this file in
** this file. Please review the following information to ensure GNU ** accordance with the Qt Commercial License Agreement provided with the
** General Public Licensing requirements will be met: ** Software or, alternatively, in accordance with the terms contained in
** http://trolltech.com/products/qt/licenses/licensing/opensource/ ** 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.0, 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 are unsure which license is appropriate for your use, please ** If you are unsure which license is appropriate for your use, please
** review the following information: ** contact the sales department at qt-sales@nokia.com.
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview ** $QT_END_LICENSE$
** 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 "graphwidget.h"
#include "edge.h" #include "edge.h"
#include "arrow.h"
#include "node.h" #include "node.h"
#include <retroshare/rspeers.h> #include <iostream>
#include <QDebug> #include <QDebug>
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QWheelEvent> #include <QWheelEvent>
#include <math.h> #include <math.h>
GraphWidget::GraphWidget(QWidget *parent) #define SWAP(a,b) tempr=(a);(a)=(b);(b)=tempr
:QGraphicsView(parent), timerId(0)
{
#if 0 void fourn(double data[],unsigned long nn[],unsigned long ndim,int isign)
{
int i1,i2,i3,i2rev,i3rev,ip1,ip2,ip3,ifp1,ifp2;
int ibit,idim,k1,k2,n,nprev,nrem,ntot;
double tempi,tempr;
double theta,wi,wpi,wpr,wr,wtemp;
ntot=1;
for (idim=1;idim<=ndim;idim++)
ntot *= nn[idim];
nprev=1;
for (idim=ndim;idim>=1;idim--) {
n=nn[idim];
nrem=ntot/(n*nprev);
ip1=nprev << 1;
ip2=ip1*n;
ip3=ip2*nrem;
i2rev=1;
for (i2=1;i2<=ip2;i2+=ip1) {
if (i2 < i2rev) {
for (i1=i2;i1<=i2+ip1-2;i1+=2) {
for (i3=i1;i3<=ip3;i3+=ip2) {
i3rev=i2rev+i3-i2;
SWAP(data[i3],data[i3rev]);
SWAP(data[i3+1],data[i3rev+1]);
}
}
}
ibit=ip2 >> 1;
while (ibit >= ip1 && i2rev > ibit) {
i2rev -= ibit;
ibit >>= 1;
}
i2rev += ibit;
}
ifp1=ip1;
while (ifp1 < ip2) {
ifp2=ifp1 << 1;
theta=isign*6.28318530717959/(ifp2/ip1);
wtemp=sin(0.5*theta);
wpr = -2.0*wtemp*wtemp;
wpi=sin(theta);
wr=1.0;
wi=0.0;
for (i3=1;i3<=ifp1;i3+=ip1) {
for (i1=i3;i1<=i3+ip1-2;i1+=2) {
for (i2=i1;i2<=ip3;i2+=ifp2) {
k1=i2;
k2=k1+ifp1;
tempr=wr*data[k2]-wi*data[k2+1];
tempi=wr*data[k2+1]+wi*data[k2];
data[k2]=data[k1]-tempr;
data[k2+1]=data[k1+1]-tempi;
data[k1] += tempr;
data[k1+1] += tempi;
}
}
wr=(wtemp=wr)*wpr-wi*wpi+wr;
wi=wi*wpr+wtemp*wpi+wi;
}
ifp1=ifp2;
}
nprev *= n;
}
}
#undef SWAP
GraphWidget::GraphWidget(QWidget *)
: timerId(0)
{
QGraphicsScene *scene = new QGraphicsScene(this); QGraphicsScene *scene = new QGraphicsScene(this);
scene->setItemIndexMethod(QGraphicsScene::NoIndex); scene->setItemIndexMethod(QGraphicsScene::NoIndex);
//scene->setSceneRect(-200, -200, 400, 400);
scene->setSceneRect(-200, -200, 1000, 1000); scene->setSceneRect(-200, -200, 1000, 1000);
setScene(scene); setScene(scene);
centerNode = new Node(this, 1, "You");
scene->addItem(centerNode);
centerNode->setPos(0, 0);
#endif
centerNode = NULL;
setCacheMode(CacheBackground); setCacheMode(CacheBackground);
setViewportUpdateMode(BoundingRectViewportUpdate);
setRenderHint(QPainter::Antialiasing); setRenderHint(QPainter::Antialiasing);
setTransformationAnchor(AnchorUnderMouse); setTransformationAnchor(AnchorUnderMouse);
setResizeAnchor(AnchorViewCenter); setResizeAnchor(AnchorViewCenter);
scale(0.8, 0.8);
//setMinimumSize(400, 400);
//setMinimumSize(1000, 1000);
scale(qreal(0.8), qreal(0.8));
setMinimumSize(400, 400);
setWindowTitle(tr("Elastic Nodes")); setWindowTitle(tr("Elastic Nodes"));
clearGraph();
} }
bool GraphWidget::clearGraph() void GraphWidget::clearGraph()
{ {
QGraphicsScene *oldscene = scene(); QGraphicsScene *oldscene = scene();
QGraphicsScene *scene = new QGraphicsScene(this); QGraphicsScene *scene = new QGraphicsScene(this);
scene->setItemIndexMethod(QGraphicsScene::NoIndex); scene->setItemIndexMethod(QGraphicsScene::NoIndex);
//scene->setSceneRect(-200, -200, 400, 400); scene->setSceneRect(-200, -200, 1000, 1000);
scene->setSceneRect(-200, -200, 1000, 1000); setScene(scene);
setScene(scene);
centerNode = new Node(this, 1, rsPeers->getGPGOwnId(), "You"); // scene->addItem(centerNode);
scene->addItem(centerNode); // centerNode->setPos(0, 0);
centerNode->setPos(0, 0);
if (oldscene) if (oldscene != NULL)
{ {
delete oldscene; delete oldscene;
} }
nodeMap.clear(); _edges.clear();
edgeList.clear(); _nodes.clear();
arrowList.clear();
return true;
} }
void GraphWidget::addNode(uint32_t type, std::string id, std::string name) GraphWidget::NodeId GraphWidget::addNode(const std::string& node_string,uint32_t flags)
{ {
Node *node = new Node(this, type, id, name); Node *node = new Node(node_string,flags,this);
_nodes.push_back(node) ;
/* store node */
nodeMap[id] = node;
scene()->addItem(node); scene()->addItem(node);
node->setPos(-50 + qrand() % 100 , -50 + qrand() % 100); node->setPos(-50+rand()%1000/53.0f, -50+rand()%1000/53.0f);
return _nodes.size()-1 ;
} }
GraphWidget::EdgeId GraphWidget::addEdge(NodeId n1,NodeId n2)
void GraphWidget::addEdge(std::string id1, std::string id2)
{ {
std::map<std::string, Node *>::iterator it; Edge *edge = new Edge(_nodes[n1],_nodes[n2]);
Node *n1 = NULL; scene()->addItem(edge);
Node *n2 = NULL; return 0 ;
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::addArrow(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))
{
Arrow *arrow = new Arrow(n1, n2);
scene()->addItem(arrow);
arrowList.push_back(arrow);
}
}
void GraphWidget::itemMoved() void GraphWidget::itemMoved()
{ {
if (!timerId) if (!timerId)
timerId = startTimer(1000 / 1); timerId = startTimer(1000 / 25);
} }
void GraphWidget::keyPressEvent(QKeyEvent *event) void GraphWidget::keyPressEvent(QKeyEvent *event)
{ {
switch (event->key()) { switch (event->key()) {
case Qt::Key_Up: // case Qt::Key_Up:
centerNode->moveBy(0, -20); // centerNode->moveBy(0, -20);
break; // break;
case Qt::Key_Down: // case Qt::Key_Down:
centerNode->moveBy(0, 20); // centerNode->moveBy(0, 20);
break; // break;
case Qt::Key_Left: // case Qt::Key_Left:
centerNode->moveBy(-20, 0); // centerNode->moveBy(-20, 0);
break; // break;
case Qt::Key_Right: // case Qt::Key_Right:
centerNode->moveBy(20, 0); // centerNode->moveBy(20, 0);
break; // break;
case Qt::Key_Plus: case Qt::Key_Plus:
scaleView(1.2); scaleView(qreal(1.2));
break; break;
case Qt::Key_Minus: case Qt::Key_Minus:
scaleView(1 / 1.2); scaleView(1 / qreal(1.2));
break; break;
case Qt::Key_Space: case Qt::Key_Space:
case Qt::Key_Enter: case Qt::Key_Enter:
@ -218,22 +215,95 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
} }
} }
static void convolveWithGaussian(double *forceMap,int S,int s)
{
static double *bf = NULL ;
if(bf == NULL)
{
bf = new double[S*S*2] ;
for(int i=0;i<S;++i)
for(int j=0;j<S;++j)
{
int x = (i<S/2)?i:(S-i) ;
int y = (j<S/2)?j:(S-j) ;
int l=2*(x*x+y*y);
bf[2*(i+S*j)] = log(sqrtf(0.1 + x*x+y*y)); // linear -> derivative is constant
bf[2*(i+S*j)+1] = 0 ;
}
unsigned long nn[2] = {S,S};
fourn(&bf[-1],&nn[-1],2,1) ;
}
unsigned long nn[2] = {S,S};
fourn(&forceMap[-1],&nn[-1],2,1) ;
for(int i=0;i<S;++i)
for(int j=0;j<S;++j)
{
float a = forceMap[2*(i+S*j)]*bf[2*(i+S*j)] - forceMap[2*(i+S*j)+1]*bf[2*(i+S*j)+1] ;
float b = forceMap[2*(i+S*j)]*bf[2*(i+S*j)+1] + forceMap[2*(i+S*j)+1]*bf[2*(i+S*j)] ;
forceMap[2*(i+S*j)] = a ;
forceMap[2*(i+S*j)+1] = b ;
}
fourn(&forceMap[-1],&nn[-1],2,-1) ;
for(int i=0;i<S*S*2;++i)
forceMap[i] /= S*S;
}
void GraphWidget::timerEvent(QTimerEvent *event) void GraphWidget::timerEvent(QTimerEvent *event)
{ {
if(!isVisible()) Q_UNUSED(event);
return;
QList<Node *> nodes; static const int S = 256 ;
foreach (QGraphicsItem *item, scene()->items()) { static double *forceMap = new double[2*S*S] ;
if (Node *node = qgraphicsitem_cast<Node *>(item))
nodes << node;
}
foreach (Node *node, nodes) memset(forceMap,0,2*S*S*sizeof(double)) ;
node->calculateForces(); QRectF R(scene()->sceneRect()) ;
foreach (Node *node, _nodes)
{
QPointF pos = node->mapToScene(QPointF(0,0)) ;
float x = S*(pos.x()-R.left())/R.width() ;
float y = S*(pos.y()- R.top())/R.height() ;
int i=(int)floor(x) ;
int j=(int)floor(y) ;
float di = x-i ;
float dj = y-j ;
if( i>=0 && i<S-1 && j>=0 && j<S-1)
{
forceMap[2*(i +S*(j ))] += (1-di)*(1-dj) ;
forceMap[2*(i+1+S*(j ))] += di *(1-dj) ;
forceMap[2*(i +S*(j+1))] += (1-di)*dj ;
forceMap[2*(i+1+S*(j+1))] += di *dj ;
}
}
// compute convolution with 1/omega kernel.
convolveWithGaussian(forceMap,S,20) ;
static int toto=0 ;
static float speedf=1.0f;
foreach (Node *node, _nodes)
{
QPointF pos = node->mapToScene(QPointF(0,0)) ;
float x = S*(pos.x()-R.left())/R.width() ;
float y = S*(pos.y()-R.top())/R.height() ;
node->calculateForces(forceMap,R.width(),R.height(),S,S,x,y,speedf);
}
bool itemsMoved = false; bool itemsMoved = false;
foreach (Node *node, nodes) { foreach (Node *node, _nodes) {
if (node->advance()) if (node->advance())
itemsMoved = true; itemsMoved = true;
} }
@ -271,19 +341,19 @@ void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
painter->drawRect(sceneRect); painter->drawRect(sceneRect);
// Text // Text
// QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4, QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
// sceneRect.width() - 4, sceneRect.height() - 4); sceneRect.width() - 4, sceneRect.height() - 4);
// QString message(tr("Click and drag the nodes around, and zoom with the mouse " QString message(tr("Click and drag the nodes around, and zoom with the mouse "
// "wheel or the '+' and '-' keys")); "wheel or the '+' and '-' keys"));
//
// QFont font = painter->font(); QFont font = painter->font();
// font.setBold(true); font.setBold(true);
// font.setPointSize(14); font.setPointSize(14);
// painter->setFont(font); painter->setFont(font);
// painter->setPen(Qt::lightGray); painter->setPen(Qt::lightGray);
// painter->drawText(textRect.translated(2, 2), message); painter->drawText(textRect.translated(2, 2), message);
// painter->setPen(Qt::black); painter->setPen(Qt::black);
// painter->drawText(textRect, message); painter->drawText(textRect, message);
} }
void GraphWidget::scaleView(qreal scaleFactor) void GraphWidget::scaleView(qreal scaleFactor)

View File

@ -1,80 +1,89 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2006-2007 Trolltech ASA. All rights reserved. ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** ** Contact: Qt Software Information (qt-info@nokia.com)
** This file is part of the example classes of the Qt Toolkit. **
** ** 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 ** $QT_BEGIN_LICENSE:LGPL$
** and appearing in the file LICENSE.GPL included in the packaging of ** Commercial Usage
** this file. Please review the following information to ensure GNU ** Licensees holding valid Qt Commercial licenses may use this file in
** General Public Licensing requirements will be met: ** accordance with the Qt Commercial License Agreement provided with the
** http://trolltech.com/products/qt/licenses/licensing/opensource/ ** Software or, alternatively, in accordance with the terms contained in
** ** a written agreement between you and Nokia.
** If you are unsure which license is appropriate for your use, please **
** review the following information: ** GNU Lesser General Public License Usage
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview ** Alternatively, this file may be used under the terms of the GNU Lesser
** or contact the sales department at sales@trolltech.com. ** General Public License version 2.1 as published by the Free Software
** ** Foundation and appearing in the file LICENSE.LGPL included in the
** In addition, as a special exception, Trolltech gives you certain ** packaging of this file. Please review the following information to
** additional rights. These rights are described in the Trolltech GPL ** ensure the GNU Lesser General Public License version 2.1 requirements
** Exception version 1.0, which can be found at ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
** http://www.trolltech.com/products/qt/gplexception/ and in the file **
** GPL_EXCEPTION.txt in this package. ** In addition, as a special exception, Nokia gives you certain
** ** additional rights. These rights are described in the Nokia Qt LGPL
** In addition, as a special exception, Trolltech, as the sole copyright ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** holder for Qt Designer, grants users of the Qt/Eclipse Integration ** package.
** plug-in the right for the Qt/Eclipse Integration to link to **
** functionality provided by Qt Designer and its related libraries. ** GNU General Public License Usage
** ** Alternatively, this file may be used under the terms of the GNU
** Trolltech reserves all rights not expressly granted herein. ** General Public License version 3.0 as published by the Free Software
** ** Foundation and appearing in the file LICENSE.GPL included in the
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** packaging of this file. Please review the following information to
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ensure the GNU General Public License version 3.0 requirements will be
** ** met: http://www.gnu.org/copyleft/gpl.html.
****************************************************************************/ **
** If you are unsure which license is appropriate for your use, please
#ifndef GRAPHWIDGET_H ** contact the sales department at qt-sales@nokia.com.
#define GRAPHWIDGET_H ** $QT_END_LICENSE$
**
#include <QtGui/QGraphicsView> ****************************************************************************/
#include <string>
#include <map> #ifndef GRAPHWIDGET_H
#include <stdint.h> #define GRAPHWIDGET_H
class Node; #include <QtGui/QGraphicsView>
class Edge;
class Arrow; class Node;
class GraphWidget : public QGraphicsView class GraphWidget : public QGraphicsView
{ {
Q_OBJECT Q_OBJECT
public: public:
GraphWidget(QWidget *parent); GraphWidget(QWidget * = NULL);
void itemMoved(); typedef int NodeId ;
typedef int EdgeId ;
bool clearGraph();
void addNode(uint32_t type, std::string id, std::string name); typedef enum {
void addEdge(std::string id1, std::string id2); ELASTIC_NODE_FLAG_OWN = 0x0001,
void addArrow(std::string id1, std::string id2); ELASTIC_NODE_FLAG_FRIEND = 0x0002,
ELASTIC_NODE_FLAG_AUTHED = 0x0004,
protected: ELASTIC_NODE_FLAG_MARGINALAUTH = 0x0008
void keyPressEvent(QKeyEvent *event); } NodeFlags ;
void timerEvent(QTimerEvent *event);
void wheelEvent(QWheelEvent *event); virtual void itemMoved();
void drawBackground(QPainter *painter, const QRectF &rect); NodeId addNode(const std::string& NodeText,uint32_t flags) ;
EdgeId addEdge(NodeId n1,NodeId n2) ;
void scaleView(qreal scaleFactor);
void clearGraph() ;
private:
int timerId; protected:
Node *centerNode; void keyPressEvent(QKeyEvent *event);
void timerEvent(QTimerEvent *event);
std::map<std::string, Node *> nodeMap; void wheelEvent(QWheelEvent *event);
std::list<Edge *> edgeList; void drawBackground(QPainter *painter, const QRectF &rect);
std::list<Arrow *> arrowList;
}; void scaleView(qreal scaleFactor);
#endif private:
int timerId;
//Node *centerNode;
bool mDeterminedBB ;
std::vector<Node *> _nodes ;
std::vector<Node *> _edges ;
};
#endif

View File

@ -1,473 +1,297 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2006-2007 Trolltech ASA. All rights reserved. ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** ** Contact: Qt Software Information (qt-info@nokia.com)
** This file is part of the example classes of the Qt Toolkit. **
** ** 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 ** $QT_BEGIN_LICENSE:LGPL$
** and appearing in the file LICENSE.GPL included in the packaging of ** Commercial Usage
** this file. Please review the following information to ensure GNU ** Licensees holding valid Qt Commercial licenses may use this file in
** General Public Licensing requirements will be met: ** accordance with the Qt Commercial License Agreement provided with the
** http://trolltech.com/products/qt/licenses/licensing/opensource/ ** Software or, alternatively, in accordance with the terms contained in
** ** a written agreement between you and Nokia.
** If you are unsure which license is appropriate for your use, please **
** review the following information: ** GNU Lesser General Public License Usage
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview ** Alternatively, this file may be used under the terms of the GNU Lesser
** or contact the sales department at sales@trolltech.com. ** General Public License version 2.1 as published by the Free Software
** ** Foundation and appearing in the file LICENSE.LGPL included in the
** In addition, as a special exception, Trolltech gives you certain ** packaging of this file. Please review the following information to
** additional rights. These rights are described in the Trolltech GPL ** ensure the GNU Lesser General Public License version 2.1 requirements
** Exception version 1.0, which can be found at ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
** http://www.trolltech.com/products/qt/gplexception/ and in the file **
** GPL_EXCEPTION.txt in this package. ** In addition, as a special exception, Nokia gives you certain
** ** additional rights. These rights are described in the Nokia Qt LGPL
** In addition, as a special exception, Trolltech, as the sole copyright ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** holder for Qt Designer, grants users of the Qt/Eclipse Integration ** package.
** plug-in the right for the Qt/Eclipse Integration to link to **
** functionality provided by Qt Designer and its related libraries. ** GNU General Public License Usage
** ** Alternatively, this file may be used under the terms of the GNU
** Trolltech reserves all rights not expressly granted herein. ** General Public License version 3.0 as published by the Free Software
** ** Foundation and appearing in the file LICENSE.GPL included in the
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** packaging of this file. Please review the following information to
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ensure the GNU General Public License version 3.0 requirements will be
** ** met: http://www.gnu.org/copyleft/gpl.html.
****************************************************************************/ **
** If you are unsure which license is appropriate for your use, please
#include <QGraphicsScene> ** contact the sales department at qt-sales@nokia.com.
#include <QGraphicsSceneMouseEvent> ** $QT_END_LICENSE$
#include <QPainter> **
#include <QStyleOption> ****************************************************************************/
#include <QMenu>
#include <QGraphicsScene>
#include "edge.h" #include <QGraphicsSceneMouseEvent>
#include "arrow.h" #include <QPainter>
#include "node.h" #include <QStyleOption>
#include "graphwidget.h" #include <iostream>
#include <math.h>
#include "../connect/ConfCertDialog.h" #include <math.h>
#include <retroshare/rspeers.h> #include "edge.h"
#include "node.h"
#include <iostream> #include "graphwidget.h"
Node::Node(GraphWidget *graphWidget, uint32_t t, std::string id_in, std::string n) Node::Node(const std::string& node_string,uint32_t flags,GraphWidget *graphWidget)
: graph(graphWidget), ntype(t), id(id_in), name(n), : graph(graphWidget),_desc_string(node_string),_flags(flags)
mDeterminedBB(false) {
{ setFlag(ItemIsMovable);
setFlag(ItemIsMovable); setFlag(ItemSendsGeometryChanges);
setZValue(1); setCacheMode(DeviceCoordinateCache);
} setZValue(1);
mDeterminedBB = false ;
void Node::addEdge(Edge *edge) mBBWidth = 0 ;
{
edgeList << edge; _speedx=_speedy=0;
edge->adjust(); _steps=0;
} }
QList<Edge *> Node::edges() const void Node::addEdge(Edge *edge)
{ {
return edgeList; edgeList << edge;
} edge->adjust();
}
void Node::addArrow(Arrow *arrow)
{ QList<Edge *> Node::edges() const
arrowList << arrow; {
arrow->adjust(); return edgeList;
} }
QList<Arrow *> Node::arrows() const static double interpolate(const double *map,int W,int H,float x,float y)
{ {
return arrowList; if(x>W-2) x=W-2 ;
} if(y>H-2) y=H-2 ;
if(x<0 ) x=0 ;
void Node::calculateForces() if(y<0 ) y=0 ;
{
if (!scene() || scene()->mouseGrabberItem() == this) { int i=(int)floor(x) ;
newPos = pos(); int j=(int)floor(y) ;
return; double di = x-i ;
} double dj = y-j ;
// Sum up all forces pushing this item away return (1-di)*( (1-dj)*map[2*(i+W*j)] + dj*map[2*(i+W*(j+1))])
qreal xvel = 0; +di *( (1-dj)*map[2*(i+1+W*j)] + dj*map[2*(i+1+W*(j+1))]) ;
qreal yvel = 0; }
foreach (QGraphicsItem *item, scene()->items()) {
Node *node = qgraphicsitem_cast<Node *>(item); void Node::calculateForces(const double *map,int width,int height,int W,int H,float x,float y,float speedf)
if (!node) {
continue; if (!scene() || scene()->mouseGrabberItem() == this)
{
QLineF line(mapFromItem(node, 0, 0), QPointF(0, 0)); newPos = pos();
qreal dx = line.dx(); return;
qreal dy = line.dy(); }
double l = 2.0 * (dx * dx + dy * dy);
if (l > 0) {
xvel += (dx * 150.0) / l; // Sum up all forces pushing this item away
yvel += (dy * 150.0) / l; qreal xforce = 0;
} qreal yforce = 0;
}
float dei=0.0f ;
float dej=0.0f ;
// Now subtract all forces pulling items together
double weight = sqrt(edgeList.size() + 1) * 10; static float *e = NULL ;
foreach (Edge *edge, edgeList) { static const int KS = 5 ;
QPointF pos;
if (edge->sourceNode() == this) if(e == NULL)
pos = mapFromItem(edge->destNode(), 0, 0); {
else e = new float[(2*KS+1)*(2*KS+1)] ;
pos = mapFromItem(edge->sourceNode(), 0, 0);
xvel += pos.x() / weight; for(int i=-KS;i<=KS;++i)
yvel += pos.y() / weight; for(int j=-KS;j<=KS;++j)
} e[i+KS+(2*KS+1)*(j+KS)] = exp( -(i*i+j*j)/30.0 ) ; // can be precomputed
}
// Now subtract all forces pulling items together for(int i=-KS;i<=KS;++i)
// alternative weight?? for(int j=-KS;j<=KS;++j)
weight = sqrt(arrowList.size() + 1) * 10; {
foreach (Arrow *arrow, arrowList) { int X = std::min(W-1,std::max(0,(int)rint(x))) ;
QPointF pos; int Y = std::min(H-1,std::max(0,(int)rint(y))) ;
if (arrow->sourceNode() == this)
pos = mapFromItem(arrow->destNode(), 0, 0); float val = map[2*((i+X)%W + W*((j+Y)%H))] ;
else
pos = mapFromItem(arrow->sourceNode(), 0, 0); dei += i * e[i+KS+(2*KS+1)*(j+KS)] * val ;
xvel += pos.x() / weight; dej += j * e[i+KS+(2*KS+1)*(j+KS)] * val ;
yvel += pos.y() / weight; }
}
xforce = REPULSION_FACTOR * dei/25.0;
// push away from edges too. yforce = REPULSION_FACTOR * dej/25.0;
QRectF sceneRect = scene()->sceneRect();
int mid_x = (sceneRect.left() + sceneRect.right()) / 2; // Now subtract all forces pulling items together
int mid_y = (sceneRect.top() + sceneRect.bottom()) / 2; double weight = (edgeList.size() + 1) ;
foreach (Edge *edge, edgeList) {
if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1) QPointF pos;
xvel = yvel = 0; if (edge->sourceNode() == this)
pos = mapFromItem(edge->destNode(), 0, 0);
//newPos = pos() + QPointF(xvel, yvel); else
// Increased the velocity for faster settling period. pos = mapFromItem(edge->sourceNode(), 0, 0);
newPos = pos() + QPointF(5 * xvel, 5 * yvel);
newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10)); float dist = sqrtf(pos.x()*pos.x() + pos.y()*pos.y()) ;
newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10)); float val = dist - NODE_DISTANCE ;
if (ntype == ELASTIC_NODE_TYPE_OWN) xforce += 0.01*pos.x() * val / weight;
{ yforce += 0.01*pos.y() * val / weight;
/* own one always goes in the middle */ }
newPos.setX(mid_x);
newPos.setY(mid_y); static const float friction = 0.4 ;
}
} xforce -= FRICTION_FACTOR * _speedx ;
yforce -= FRICTION_FACTOR * _speedy ;
bool Node::advance()
{ // This term drags nodes away from the sides.
if (newPos == pos()) //
return false; if(x < 15) xforce += 100.0/(x+0.1) ;
if(y < 15) yforce += 100.0/(y+0.1) ;
setPos(newPos); if(x > width-15) xforce -= 100.0/(width-x+0.1) ;
return true; if(y > height-15) yforce -= 100.0/(height-y+0.1) ;
}
// now time filter:
QRectF Node::boundingRect() const
{ _speedx += xforce / MASS_FACTOR;
qreal adjust = 2; _speedy += yforce / MASS_FACTOR;
/* add in the size of the text */
qreal realwidth = 40; if(_speedx > 10) _speedx = 10.0f ;
if (mDeterminedBB) if(_speedy > 10) _speedy = 10.0f ;
{ if(_speedx <-10) _speedx =-10.0f ;
realwidth = mBBWidth + adjust; if(_speedy <-10) _speedy =-10.0f ;
}
if (realwidth < 23 + adjust) QRectF sceneRect = scene()->sceneRect();
{ newPos = pos() + QPointF(_speedx, _speedy);
realwidth = 23 + adjust; newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10));
} newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10));
}
return QRectF(-10 - adjust, -10 - adjust,
realwidth, 23 + adjust); bool Node::advance()
// 23 + adjust, 23 + adjust); {
} if (newPos == pos())
return false;
//QPainterPath Node::shape() const setPos(newPos);
//{ return true;
// QPainterPath path; }
// path.addEllipse(-10, -10, 20, 20);
// return path; QRectF Node::boundingRect() const
//} {
qreal adjust = 2;
void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) /* add in the size of the text */
{ qreal realwidth = 40;
painter->setPen(Qt::NoPen); if (mDeterminedBB)
painter->setBrush(Qt::darkGray); {
painter->drawEllipse(-7, -7, 20, 20); realwidth = mBBWidth + adjust;
}
QColor col0, col1; if (realwidth < 23 + adjust)
if (ntype == ELASTIC_NODE_TYPE_OWN) {
{ realwidth = 23 + adjust;
col0 = QColor(Qt::yellow); }
col1 = QColor(Qt::darkYellow);
} return QRectF(-10 - adjust, -10 - adjust,
else if (ntype == ELASTIC_NODE_TYPE_FRIEND) realwidth, 23 + adjust);
{ }
col0 = QColor(Qt::green);
col1 = QColor(Qt::darkGreen); QPainterPath Node::shape() const
} {
else if (ntype == ELASTIC_NODE_TYPE_AUTHED) QPainterPath path;
{ path.addEllipse(-10, -10, 20, 20);
//col0 = QColor(Qt::cyan); return path;
//col1 = QColor(Qt::darkCyan); }
//col0 = QColor(Qt::blue);
void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
col0 = QColor(Qt::cyan); {
col1 = QColor(Qt::darkBlue); painter->setPen(Qt::NoPen);
} painter->setBrush(Qt::darkGray);
else if (ntype == ELASTIC_NODE_TYPE_MARGINALAUTH) painter->drawEllipse(-7, -7, 20, 20);
{
col0 = QColor(Qt::magenta); QColor col0, col1;
col1 = QColor(Qt::darkMagenta); if (_flags & GraphWidget::ELASTIC_NODE_FLAG_OWN)
} {
else col0 = QColor(Qt::yellow);
{ col1 = QColor(Qt::darkYellow);
col0 = QColor(Qt::red); }
col1 = QColor(Qt::darkRed); else if (_flags & GraphWidget::ELASTIC_NODE_FLAG_FRIEND)
} {
col0 = QColor(Qt::green);
QRadialGradient gradient(-3, -3, 10); col1 = QColor(Qt::darkGreen);
if (option->state & QStyle::State_Sunken) { }
gradient.setCenter(3, 3); else if (_flags & GraphWidget::ELASTIC_NODE_FLAG_AUTHED)
gradient.setFocalPoint(3, 3); {
gradient.setColorAt(1, col0.light(120)); col0 = QColor(Qt::cyan);
gradient.setColorAt(0, col1.light(120)); col1 = QColor(Qt::darkBlue);
} else { }
gradient.setColorAt(0, col0); else if (_flags & GraphWidget::ELASTIC_NODE_FLAG_MARGINALAUTH)
gradient.setColorAt(1, col1); {
} col0 = QColor(Qt::magenta);
painter->setBrush(gradient); col1 = QColor(Qt::darkMagenta);
painter->setPen(QPen(Qt::black, 0)); }
painter->drawEllipse(-10, -10, 20, 20); else
painter->drawText(-10, 0, QString::fromStdString(name)); {
col0 = QColor(Qt::red);
if (!mDeterminedBB) col1 = QColor(Qt::darkRed);
{ }
QRect textBox = painter->boundingRect(-10, 0, 400, 20, 0, QString::fromStdString(name));
mBBWidth = textBox.width(); QRadialGradient gradient(-3, -3, 10);
mDeterminedBB = true; if (option->state & QStyle::State_Sunken) {
} gradient.setCenter(3, 3);
} gradient.setFocalPoint(3, 3);
gradient.setColorAt(1, col0.light(120));
QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) gradient.setColorAt(0, col1.light(120));
{ } else {
switch (change) { gradient.setColorAt(0, col0);
case ItemPositionHasChanged: gradient.setColorAt(1, col1);
foreach (Edge *edge, edgeList) }
edge->adjust(); painter->setBrush(gradient);
foreach (Arrow *arrow, arrowList) painter->setPen(QPen(Qt::black, 0));
arrow->adjust(); painter->drawEllipse(-10, -10, 20, 20);
graph->itemMoved(); painter->drawText(-10, 0, QString::fromStdString(_desc_string));
break;
default: if (!mDeterminedBB)
break; {
}; QRect textBox = painter->boundingRect(-10, 0, 400, 20, 0, QString::fromStdString(_desc_string));
mBBWidth = textBox.width();
return QGraphicsItem::itemChange(change, value); mDeterminedBB = true;
} }
}
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
{ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
update(); {
QGraphicsItem::mousePressEvent(event); switch (change) {
} case ItemPositionHasChanged:
foreach (Edge *edge, edgeList)
void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) edge->adjust();
{ graph->itemMoved();
update(); break;
QGraphicsItem::mouseReleaseEvent(event); default:
} break;
};
void Node::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{ return QGraphicsItem::itemChange(change, value);
RsPeerDetails details; }
if (!rsPeers->getPeerDetails(id, details))
{ void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
event->accept(); {
return; update();
} QGraphicsItem::mousePressEvent(event);
}
/* no events for self */
#if 0 void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
if (ntype == ELASTIC_NODE_TYPE_OWN) {
{ update();
event->accept(); QGraphicsItem::mouseReleaseEvent(event);
return; }
}
#endif
QString menuTitle = QString::fromStdString(details.name);
menuTitle += " : ";
std::cerr << "Node Menu for " << details.name << std::endl;
QMenu menu;
switch(ntype)
{
case ELASTIC_NODE_TYPE_OWN:
{
menuTitle += "Ourselves";
break;
}
case ELASTIC_NODE_TYPE_FRIEND:
{
menuTitle += "Friend";
break;
}
case ELASTIC_NODE_TYPE_AUTHED:
{
menuTitle += "Authenticated";
break;
}
case ELASTIC_NODE_TYPE_MARGINALAUTH:
{
menuTitle += "Friend of a Friend";
break;
}
default:
case ELASTIC_NODE_TYPE_FOF:
{
menuTitle += " Unknown";
break;
}
}
QAction *titleAction = menu.addAction(menuTitle);
titleAction->setEnabled(false);
menu.addSeparator();
/* find all the peers which have this pgp peer as issuer */
std::list<std::string> ids;
std::list<std::string>::iterator it;
bool haveSSLcerts = false;
if ((ntype == ELASTIC_NODE_TYPE_AUTHED) ||
(ntype == ELASTIC_NODE_TYPE_FRIEND) ||
(ntype == ELASTIC_NODE_TYPE_OWN))
{
rsPeers->getFriendList(ids);
QAction *addAction = menu.addAction("Add All as Friends");
QAction *rmAction = menu.addAction("Remove All as Friends");
std::cerr << "OthersList looking for ::Issuer " << id << std::endl;
int nssl = 0;
for(it = ids.begin(); it != ids.end(); it++)
{
RsPeerDetails d2;
if (!rsPeers->getPeerDetails(*it, d2))
continue;
std::cerr << "OthersList::Id " << d2.id;
std::cerr << " ::Issuer " << d2.issuer << std::endl;
if (d2.issuer == id)
{
QString sslTitle = " SSL ID: ";
sslTitle += QString::fromStdString(d2.location);
if (RS_PEER_STATE_FRIEND & d2.state)
{
sslTitle += " Allowed";
}
else
{
sslTitle += " Denied";
}
QMenu *sslMenu = menu.addMenu (sslTitle);
if (RS_PEER_STATE_FRIEND & d2.state)
{
sslMenu->addAction("Deny");
}
else
{
sslMenu->addAction("Allow");
}
nssl++;
}
}
if (nssl > 0)
{
menu.addSeparator();
haveSSLcerts = true;
}
else
{
addAction->setVisible(false);
rmAction->setVisible(false);
QAction *noAction = menu.addAction("No SSL Certificates for Peer");
noAction->setEnabled(false);
menu.addSeparator();
}
}
switch(ntype)
{
case ELASTIC_NODE_TYPE_OWN:
{
break;
}
case ELASTIC_NODE_TYPE_FRIEND:
{
menu.addAction("Chat");
menu.addAction("Msg");
menu.addSeparator();
menu.addAction("Connect");
menu.addSeparator();
break;
}
case ELASTIC_NODE_TYPE_AUTHED:
{
break;
}
case ELASTIC_NODE_TYPE_MARGINALAUTH:
{
if (haveSSLcerts)
{
menu.addAction("Sign Peer and Add Friend");
menu.addSeparator();
}
else
{
menu.addAction("Sign Peer to Authenticate");
menu.addSeparator();
}
break;
}
default:
case ELASTIC_NODE_TYPE_FOF:
{
if (haveSSLcerts)
{
menu.addAction("Sign Peer and Add Friend");
menu.addSeparator();
}
else
{
menu.addAction("Sign Peer to Authenticate");
menu.addSeparator();
}
break;
}
}
QAction *detailAction = menu.addAction("Peer Details");
QObject::connect( detailAction , SIGNAL( triggered() ), this, SLOT( peerdetails() ) );
connect( detailAction , SIGNAL( triggered() ), this, SLOT( peerdetails() ) );
menu.addAction("Export Certificate");
menu.exec(event->screenPos());
}
void Node::peerdetails()
{
ConfCertDialog::show(id);
}

View File

@ -1,104 +1,95 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2006-2007 Trolltech ASA. All rights reserved. ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** ** Contact: Qt Software Information (qt-info@nokia.com)
** This file is part of the example classes of the Qt Toolkit. **
** ** 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 ** $QT_BEGIN_LICENSE:LGPL$
** and appearing in the file LICENSE.GPL included in the packaging of ** Commercial Usage
** this file. Please review the following information to ensure GNU ** Licensees holding valid Qt Commercial licenses may use this file in
** General Public Licensing requirements will be met: ** accordance with the Qt Commercial License Agreement provided with the
** http://trolltech.com/products/qt/licenses/licensing/opensource/ ** Software or, alternatively, in accordance with the terms contained in
** ** a written agreement between you and Nokia.
** If you are unsure which license is appropriate for your use, please **
** review the following information: ** GNU Lesser General Public License Usage
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview ** Alternatively, this file may be used under the terms of the GNU Lesser
** or contact the sales department at sales@trolltech.com. ** General Public License version 2.1 as published by the Free Software
** ** Foundation and appearing in the file LICENSE.LGPL included in the
** In addition, as a special exception, Trolltech gives you certain ** packaging of this file. Please review the following information to
** additional rights. These rights are described in the Trolltech GPL ** ensure the GNU Lesser General Public License version 2.1 requirements
** Exception version 1.0, which can be found at ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
** http://www.trolltech.com/products/qt/gplexception/ and in the file **
** GPL_EXCEPTION.txt in this package. ** In addition, as a special exception, Nokia gives you certain
** ** additional rights. These rights are described in the Nokia Qt LGPL
** In addition, as a special exception, Trolltech, as the sole copyright ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** holder for Qt Designer, grants users of the Qt/Eclipse Integration ** package.
** plug-in the right for the Qt/Eclipse Integration to link to **
** functionality provided by Qt Designer and its related libraries. ** GNU General Public License Usage
** ** Alternatively, this file may be used under the terms of the GNU
** Trolltech reserves all rights not expressly granted herein. ** General Public License version 3.0 as published by the Free Software
** ** Foundation and appearing in the file LICENSE.GPL included in the
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** packaging of this file. Please review the following information to
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ensure the GNU General Public License version 3.0 requirements will be
** ** met: http://www.gnu.org/copyleft/gpl.html.
****************************************************************************/ **
** If you are unsure which license is appropriate for your use, please
#ifndef NODE_H ** contact the sales department at qt-sales@nokia.com.
#define NODE_H ** $QT_END_LICENSE$
**
#include <QGraphicsItem> ****************************************************************************/
#include <stdint.h>
#ifndef NODE_H
#define ELASTIC_NODE_TYPE_OWN 1 #define NODE_H
#define ELASTIC_NODE_TYPE_FRIEND 2
#define ELASTIC_NODE_TYPE_AUTHED 3 #include <QGraphicsItem>
#define ELASTIC_NODE_TYPE_MARGINALAUTH 4 #include <QList>
#define ELASTIC_NODE_TYPE_FOF 5
#include "graphwidget.h"
class Edge;
class Arrow; class Edge;
class GraphWidget; QT_BEGIN_NAMESPACE
class QGraphicsSceneMouseEvent; class QGraphicsSceneMouseEvent;
QT_END_NAMESPACE
class Node : public QObject, public QGraphicsItem
{ class Node : public QGraphicsItem
Q_OBJECT {
public:
Node(const std::string& node_string,uint32_t flags,GraphWidget *graphWidget);
public:
Node(GraphWidget *graphWidget, uint32_t t, std::string id_in, std::string n); void addEdge(Edge *edge);
QList<Edge *> edges() const;
void addEdge(Edge *edge);
QList<Edge *> edges() const; int type() const { return Type; }
void addArrow(Arrow *arrow); void calculateForces(const double *data,int width,int height,int W,int H,float x,float y,float speedf);
QList<Arrow *> arrows() const; bool advance();
enum { Type = UserType + 1 }; QRectF boundingRect() const;
int type() const { return Type; } QPainterPath shape() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void calculateForces();
bool advance(); protected:
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
QRectF boundingRect() const;
//QPainterPath shape() const; virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
public slots: private:
void peerdetails(); QList<Edge *> edgeList;
QPointF newPos;
protected: GraphWidget *graph;
QVariant itemChange(GraphicsItemChange change, const QVariant &value); qreal _speedx,_speedy;
int _steps ;
void mousePressEvent(QGraphicsSceneMouseEvent *event); std::string _desc_string ;
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); uint32_t _flags ;
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event); bool mDeterminedBB ;
int mBBWidth ;
private:
QList<Edge *> edgeList; static const float MASS_FACTOR = 10 ;
QList<Arrow *> arrowList; static const float FRICTION_FACTOR = 6.8 ;
QPointF newPos; static const float REPULSION_FACTOR = 4 ;
GraphWidget *graph; static const float NODE_DISTANCE = 130.0 ;
};
/* extra information */
uint32_t ntype; /* Ourself, friend, fof */ #endif
std::string id;
std::string name;
bool mDeterminedBB;
uint32_t mBBWidth;
};
#endif