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,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.
** **
****************************************************************************/ ****************************************************************************/

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,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.
** **
****************************************************************************/ ****************************************************************************/
@ -38,27 +43,31 @@
#define GRAPHWIDGET_H #define GRAPHWIDGET_H
#include <QtGui/QGraphicsView> #include <QtGui/QGraphicsView>
#include <string>
#include <map>
#include <stdint.h>
class Node; class Node;
class Edge;
class Arrow;
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(); typedef enum {
void addNode(uint32_t type, std::string id, std::string name); ELASTIC_NODE_FLAG_OWN = 0x0001,
void addEdge(std::string id1, std::string id2); ELASTIC_NODE_FLAG_FRIEND = 0x0002,
void addArrow(std::string id1, std::string id2); ELASTIC_NODE_FLAG_AUTHED = 0x0004,
ELASTIC_NODE_FLAG_MARGINALAUTH = 0x0008
} NodeFlags ;
virtual void itemMoved();
NodeId addNode(const std::string& NodeText,uint32_t flags) ;
EdgeId addEdge(NodeId n1,NodeId n2) ;
void clearGraph() ;
protected: protected:
void keyPressEvent(QKeyEvent *event); void keyPressEvent(QKeyEvent *event);
@ -70,11 +79,11 @@ protected:
private: private:
int timerId; int timerId;
Node *centerNode; //Node *centerNode;
bool mDeterminedBB ;
std::map<std::string, Node *> nodeMap; std::vector<Node *> _nodes ;
std::list<Edge *> edgeList; std::vector<Node *> _edges ;
std::list<Arrow *> arrowList;
}; };
#endif #endif

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.
** **
****************************************************************************/ ****************************************************************************/
@ -38,25 +43,26 @@
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QPainter> #include <QPainter>
#include <QStyleOption> #include <QStyleOption>
#include <QMenu>
#include "edge.h"
#include "arrow.h"
#include "node.h"
#include "graphwidget.h"
#include <math.h>
#include "../connect/ConfCertDialog.h"
#include <retroshare/rspeers.h>
#include <iostream> #include <iostream>
Node::Node(GraphWidget *graphWidget, uint32_t t, std::string id_in, std::string n) #include <math.h>
: graph(graphWidget), ntype(t), id(id_in), name(n),
mDeterminedBB(false) #include "edge.h"
#include "node.h"
#include "graphwidget.h"
Node::Node(const std::string& node_string,uint32_t flags,GraphWidget *graphWidget)
: graph(graphWidget),_desc_string(node_string),_flags(flags)
{ {
setFlag(ItemIsMovable); setFlag(ItemIsMovable);
setFlag(ItemSendsGeometryChanges);
setCacheMode(DeviceCoordinateCache);
setZValue(1); setZValue(1);
mDeterminedBB = false ;
mBBWidth = 0 ;
_speedx=_speedy=0;
_steps=0;
} }
void Node::addEdge(Edge *edge) void Node::addEdge(Edge *edge)
@ -70,89 +76,107 @@ QList<Edge *> Node::edges() const
return edgeList; return edgeList;
} }
void Node::addArrow(Arrow *arrow) static double interpolate(const double *map,int W,int H,float x,float y)
{ {
arrowList << arrow; if(x>W-2) x=W-2 ;
arrow->adjust(); if(y>H-2) y=H-2 ;
if(x<0 ) x=0 ;
if(y<0 ) y=0 ;
int i=(int)floor(x) ;
int j=(int)floor(y) ;
double di = x-i ;
double dj = y-j ;
return (1-di)*( (1-dj)*map[2*(i+W*j)] + dj*map[2*(i+W*(j+1))])
+di *( (1-dj)*map[2*(i+1+W*j)] + dj*map[2*(i+1+W*(j+1))]) ;
} }
QList<Arrow *> Node::arrows() const void Node::calculateForces(const double *map,int width,int height,int W,int H,float x,float y,float speedf)
{ {
return arrowList; if (!scene() || scene()->mouseGrabberItem() == this)
} {
newPos = pos();
void Node::calculateForces() return;
{ }
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 // Sum up all forces pushing this item away
double weight = sqrt(edgeList.size() + 1) * 10; qreal xforce = 0;
foreach (Edge *edge, edgeList) { qreal yforce = 0;
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;
}
float dei=0.0f ;
float dej=0.0f ;
// Now subtract all forces pulling items together static float *e = NULL ;
// alternative weight?? static const int KS = 5 ;
weight = sqrt(arrowList.size() + 1) * 10;
foreach (Arrow *arrow, arrowList) {
QPointF pos;
if (arrow->sourceNode() == this)
pos = mapFromItem(arrow->destNode(), 0, 0);
else
pos = mapFromItem(arrow->sourceNode(), 0, 0);
xvel += pos.x() / weight;
yvel += pos.y() / weight;
}
// push away from edges too. if(e == NULL)
QRectF sceneRect = scene()->sceneRect(); {
int mid_x = (sceneRect.left() + sceneRect.right()) / 2; e = new float[(2*KS+1)*(2*KS+1)] ;
int mid_y = (sceneRect.top() + sceneRect.bottom()) / 2;
if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1) for(int i=-KS;i<=KS;++i)
xvel = yvel = 0; 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
}
//newPos = pos() + QPointF(xvel, yvel); for(int i=-KS;i<=KS;++i)
// Increased the velocity for faster settling period. for(int j=-KS;j<=KS;++j)
newPos = pos() + QPointF(5 * xvel, 5 * yvel); {
newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10)); int X = std::min(W-1,std::max(0,(int)rint(x))) ;
newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10)); int Y = std::min(H-1,std::max(0,(int)rint(y))) ;
if (ntype == ELASTIC_NODE_TYPE_OWN) float val = map[2*((i+X)%W + W*((j+Y)%H))] ;
{
/* own one always goes in the middle */ dei += i * e[i+KS+(2*KS+1)*(j+KS)] * val ;
newPos.setX(mid_x); dej += j * e[i+KS+(2*KS+1)*(j+KS)] * val ;
newPos.setY(mid_y); }
}
xforce = REPULSION_FACTOR * dei/25.0;
yforce = REPULSION_FACTOR * dej/25.0;
// Now subtract all forces pulling items together
double weight = (edgeList.size() + 1) ;
foreach (Edge *edge, edgeList) {
QPointF pos;
if (edge->sourceNode() == this)
pos = mapFromItem(edge->destNode(), 0, 0);
else
pos = mapFromItem(edge->sourceNode(), 0, 0);
float dist = sqrtf(pos.x()*pos.x() + pos.y()*pos.y()) ;
float val = dist - NODE_DISTANCE ;
xforce += 0.01*pos.x() * val / weight;
yforce += 0.01*pos.y() * val / weight;
}
static const float friction = 0.4 ;
xforce -= FRICTION_FACTOR * _speedx ;
yforce -= FRICTION_FACTOR * _speedy ;
// This term drags nodes away from the sides.
//
if(x < 15) xforce += 100.0/(x+0.1) ;
if(y < 15) yforce += 100.0/(y+0.1) ;
if(x > width-15) xforce -= 100.0/(width-x+0.1) ;
if(y > height-15) yforce -= 100.0/(height-y+0.1) ;
// now time filter:
_speedx += xforce / MASS_FACTOR;
_speedy += yforce / MASS_FACTOR;
if(_speedx > 10) _speedx = 10.0f ;
if(_speedy > 10) _speedy = 10.0f ;
if(_speedx <-10) _speedx =-10.0f ;
if(_speedy <-10) _speedy =-10.0f ;
QRectF sceneRect = scene()->sceneRect();
newPos = pos() + QPointF(_speedx, _speedy);
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() bool Node::advance()
@ -166,7 +190,7 @@ bool Node::advance()
QRectF Node::boundingRect() const QRectF Node::boundingRect() const
{ {
qreal adjust = 2; qreal adjust = 2;
/* add in the size of the text */ /* add in the size of the text */
qreal realwidth = 40; qreal realwidth = 40;
if (mDeterminedBB) if (mDeterminedBB)
@ -180,75 +204,69 @@ QRectF Node::boundingRect() const
return QRectF(-10 - adjust, -10 - adjust, return QRectF(-10 - adjust, -10 - adjust,
realwidth, 23 + adjust); realwidth, 23 + adjust);
// 23 + adjust, 23 + adjust);
} }
QPainterPath Node::shape() const
//QPainterPath Node::shape() const {
//{ QPainterPath path;
// QPainterPath path; path.addEllipse(-10, -10, 20, 20);
// path.addEllipse(-10, -10, 20, 20); return path;
// return path; }
//}
void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{ {
painter->setPen(Qt::NoPen); painter->setPen(Qt::NoPen);
painter->setBrush(Qt::darkGray); painter->setBrush(Qt::darkGray);
painter->drawEllipse(-7, -7, 20, 20); painter->drawEllipse(-7, -7, 20, 20);
QColor col0, col1; QColor col0, col1;
if (ntype == ELASTIC_NODE_TYPE_OWN) if (_flags & GraphWidget::ELASTIC_NODE_FLAG_OWN)
{ {
col0 = QColor(Qt::yellow); col0 = QColor(Qt::yellow);
col1 = QColor(Qt::darkYellow); col1 = QColor(Qt::darkYellow);
} }
else if (ntype == ELASTIC_NODE_TYPE_FRIEND) else if (_flags & GraphWidget::ELASTIC_NODE_FLAG_FRIEND)
{ {
col0 = QColor(Qt::green); col0 = QColor(Qt::green);
col1 = QColor(Qt::darkGreen); col1 = QColor(Qt::darkGreen);
} }
else if (ntype == ELASTIC_NODE_TYPE_AUTHED) else if (_flags & GraphWidget::ELASTIC_NODE_FLAG_AUTHED)
{ {
//col0 = QColor(Qt::cyan); col0 = QColor(Qt::cyan);
//col1 = QColor(Qt::darkCyan); col1 = QColor(Qt::darkBlue);
//col0 = QColor(Qt::blue); }
else if (_flags & GraphWidget::ELASTIC_NODE_FLAG_MARGINALAUTH)
{
col0 = QColor(Qt::magenta);
col1 = QColor(Qt::darkMagenta);
}
else
{
col0 = QColor(Qt::red);
col1 = QColor(Qt::darkRed);
}
col0 = QColor(Qt::cyan); QRadialGradient gradient(-3, -3, 10);
col1 = QColor(Qt::darkBlue); if (option->state & QStyle::State_Sunken) {
} gradient.setCenter(3, 3);
else if (ntype == ELASTIC_NODE_TYPE_MARGINALAUTH) gradient.setFocalPoint(3, 3);
{ gradient.setColorAt(1, col0.light(120));
col0 = QColor(Qt::magenta); gradient.setColorAt(0, col1.light(120));
col1 = QColor(Qt::darkMagenta); } else {
} gradient.setColorAt(0, col0);
else gradient.setColorAt(1, col1);
{ }
col0 = QColor(Qt::red); painter->setBrush(gradient);
col1 = QColor(Qt::darkRed); painter->setPen(QPen(Qt::black, 0));
} painter->drawEllipse(-10, -10, 20, 20);
painter->drawText(-10, 0, QString::fromStdString(_desc_string));
QRadialGradient gradient(-3, -3, 10); if (!mDeterminedBB)
if (option->state & QStyle::State_Sunken) { {
gradient.setCenter(3, 3); QRect textBox = painter->boundingRect(-10, 0, 400, 20, 0, QString::fromStdString(_desc_string));
gradient.setFocalPoint(3, 3); mBBWidth = textBox.width();
gradient.setColorAt(1, col0.light(120)); mDeterminedBB = true;
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) QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
@ -257,8 +275,6 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
case ItemPositionHasChanged: case ItemPositionHasChanged:
foreach (Edge *edge, edgeList) foreach (Edge *edge, edgeList)
edge->adjust(); edge->adjust();
foreach (Arrow *arrow, arrowList)
arrow->adjust();
graph->itemMoved(); graph->itemMoved();
break; break;
default: default:
@ -279,195 +295,3 @@ void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
update(); update();
QGraphicsItem::mouseReleaseEvent(event); QGraphicsItem::mouseReleaseEvent(event);
} }
void Node::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
RsPeerDetails details;
if (!rsPeers->getPeerDetails(id, details))
{
event->accept();
return;
}
/* no events for self */
#if 0
if (ntype == ELASTIC_NODE_TYPE_OWN)
{
event->accept();
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,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.
** **
****************************************************************************/ ****************************************************************************/
@ -38,67 +43,53 @@
#define NODE_H #define NODE_H
#include <QGraphicsItem> #include <QGraphicsItem>
#include <stdint.h> #include <QList>
#define ELASTIC_NODE_TYPE_OWN 1 #include "graphwidget.h"
#define ELASTIC_NODE_TYPE_FRIEND 2
#define ELASTIC_NODE_TYPE_AUTHED 3
#define ELASTIC_NODE_TYPE_MARGINALAUTH 4
#define ELASTIC_NODE_TYPE_FOF 5
class Edge; class Edge;
class Arrow; QT_BEGIN_NAMESPACE
class GraphWidget;
class QGraphicsSceneMouseEvent; class QGraphicsSceneMouseEvent;
QT_END_NAMESPACE
class Node : public QObject, public QGraphicsItem class Node : public QGraphicsItem
{ {
Q_OBJECT
public: public:
Node(GraphWidget *graphWidget, uint32_t t, std::string id_in, std::string n); Node(const std::string& node_string,uint32_t flags,GraphWidget *graphWidget);
void addEdge(Edge *edge); void addEdge(Edge *edge);
QList<Edge *> edges() const; QList<Edge *> edges() const;
void addArrow(Arrow *arrow);
QList<Arrow *> arrows() const;
enum { Type = UserType + 1 };
int type() const { return Type; } int type() const { return Type; }
void calculateForces(); void calculateForces(const double *data,int width,int height,int W,int H,float x,float y,float speedf);
bool advance(); bool advance();
QRectF boundingRect() const; QRectF boundingRect() const;
//QPainterPath shape() const; QPainterPath shape() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
public slots:
void peerdetails();
protected: protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value); virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
void mousePressEvent(QGraphicsSceneMouseEvent *event); virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
private: private:
QList<Edge *> edgeList; QList<Edge *> edgeList;
QList<Arrow *> arrowList;
QPointF newPos; QPointF newPos;
GraphWidget *graph; GraphWidget *graph;
qreal _speedx,_speedy;
int _steps ;
std::string _desc_string ;
uint32_t _flags ;
bool mDeterminedBB ;
int mBBWidth ;
/* extra information */ static const float MASS_FACTOR = 10 ;
uint32_t ntype; /* Ourself, friend, fof */ static const float FRICTION_FACTOR = 6.8 ;
std::string id; static const float REPULSION_FACTOR = 4 ;
std::string name; static const float NODE_DISTANCE = 130.0 ;
bool mDeterminedBB;
uint32_t mBBWidth;
}; };
#endif #endif