2008-03-20 19:49:12 -04:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2010-10-22 16:47:08 -04:00
|
|
|
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
** Contact: Qt Software Information (qt-info@nokia.com)
|
2008-03-20 19:49:12 -04:00
|
|
|
**
|
|
|
|
** This file is part of the example classes of the Qt Toolkit.
|
|
|
|
**
|
2010-10-22 16:47:08 -04:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
|
|
|
** Commercial Usage
|
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and Nokia.
|
2008-03-20 19:49:12 -04:00
|
|
|
**
|
2010-10-22 16:47:08 -04:00
|
|
|
** 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.
|
2008-03-20 19:49:12 -04:00
|
|
|
**
|
2010-10-22 16:47:08 -04:00
|
|
|
** 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.
|
2008-03-20 19:49:12 -04:00
|
|
|
**
|
2010-10-22 16:47:08 -04:00
|
|
|
** 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.
|
2008-03-20 19:49:12 -04:00
|
|
|
**
|
2010-10-22 16:47:08 -04:00
|
|
|
** If you are unsure which license is appropriate for your use, please
|
|
|
|
** contact the sales department at qt-sales@nokia.com.
|
|
|
|
** $QT_END_LICENSE$
|
2008-03-20 19:49:12 -04:00
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "graphwidget.h"
|
|
|
|
#include "edge.h"
|
|
|
|
#include "node.h"
|
|
|
|
|
2010-10-22 16:47:08 -04:00
|
|
|
#include <iostream>
|
2008-03-20 19:49:12 -04:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QGraphicsScene>
|
|
|
|
#include <QWheelEvent>
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
2010-10-22 16:47:08 -04:00
|
|
|
#define SWAP(a,b) tempr=(a);(a)=(b);(b)=tempr
|
|
|
|
|
|
|
|
void fourn(double data[],unsigned long nn[],unsigned long ndim,int isign)
|
2008-03-20 19:49:12 -04:00
|
|
|
{
|
2010-10-22 16:47:08 -04:00
|
|
|
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
|
2008-03-20 19:49:12 -04:00
|
|
|
|
2010-10-22 16:47:08 -04:00
|
|
|
GraphWidget::GraphWidget(QWidget *)
|
|
|
|
: timerId(0)
|
|
|
|
{
|
2010-11-10 15:18:53 -05:00
|
|
|
// QGraphicsScene *scene = new QGraphicsScene(QRectF(0,0,500,500),this);
|
|
|
|
// scene->setItemIndexMethod(QGraphicsScene::NoIndex);
|
|
|
|
// scene->clear() ;
|
|
|
|
// setScene(scene);
|
|
|
|
// scene->setSceneRect(0, 0, 500, 500);
|
2008-03-20 19:49:12 -04:00
|
|
|
|
|
|
|
setCacheMode(CacheBackground);
|
2010-10-22 16:47:08 -04:00
|
|
|
setViewportUpdateMode(BoundingRectViewportUpdate);
|
2008-03-20 19:49:12 -04:00
|
|
|
setRenderHint(QPainter::Antialiasing);
|
|
|
|
setTransformationAnchor(AnchorUnderMouse);
|
|
|
|
setResizeAnchor(AnchorViewCenter);
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-22 16:47:08 -04:00
|
|
|
scale(qreal(0.8), qreal(0.8));
|
2008-03-20 19:49:12 -04:00
|
|
|
}
|
|
|
|
|
2010-10-22 16:47:08 -04:00
|
|
|
void GraphWidget::clearGraph()
|
2008-03-20 19:49:12 -04:00
|
|
|
{
|
2010-11-01 10:17:18 -04:00
|
|
|
// QGraphicsScene *scene = new QGraphicsScene(this);
|
|
|
|
// scene->setItemIndexMethod(QGraphicsScene::NoIndex);
|
|
|
|
// setScene(scene);
|
2008-03-20 19:49:12 -04:00
|
|
|
|
2010-10-22 16:47:08 -04:00
|
|
|
// scene->addItem(centerNode);
|
|
|
|
// centerNode->setPos(0, 0);
|
2008-03-20 19:49:12 -04:00
|
|
|
|
2010-11-01 10:17:18 -04:00
|
|
|
// if (oldscene != NULL)
|
|
|
|
// {
|
|
|
|
// delete oldscene;
|
|
|
|
// }
|
2008-03-20 19:49:12 -04:00
|
|
|
|
2010-11-01 10:17:18 -04:00
|
|
|
scene()->clear();
|
|
|
|
scene()->setSceneRect(-200, -200, 1000, 1000);
|
2010-10-22 16:47:08 -04:00
|
|
|
_edges.clear();
|
|
|
|
_nodes.clear();
|
2008-03-20 19:49:12 -04:00
|
|
|
}
|
|
|
|
|
2010-11-09 16:37:24 -05:00
|
|
|
GraphWidget::NodeId GraphWidget::addNode(const std::string& node_short_string,const std::string& node_complete_string,NodeType type,AuthType auth,const std::string& ssl_id,const std::string& gpg_id)
|
2009-05-23 11:13:01 -04:00
|
|
|
{
|
2010-11-09 16:37:24 -05:00
|
|
|
Node *node = new Node(node_short_string,type,auth,this,ssl_id,gpg_id);
|
2010-11-01 10:17:18 -04:00
|
|
|
node->setToolTip(QString::fromStdString(node_complete_string)) ;
|
2010-10-22 16:47:08 -04:00
|
|
|
_nodes.push_back(node) ;
|
|
|
|
scene()->addItem(node);
|
2010-11-05 18:46:40 -04:00
|
|
|
node->setPos(rand()%1000/53.0f, rand()%1000/53.0f);
|
|
|
|
std::cerr << "Added node " << _nodes.size()-1 << std::endl ;
|
2010-10-22 16:47:08 -04:00
|
|
|
return _nodes.size()-1 ;
|
|
|
|
}
|
|
|
|
GraphWidget::EdgeId GraphWidget::addEdge(NodeId n1,NodeId n2)
|
|
|
|
{
|
2010-11-05 18:46:40 -04:00
|
|
|
std::pair<NodeId,NodeId> ed(std::min(n1,n2),std::max(n1,n2)) ;
|
|
|
|
|
|
|
|
if( _edges.find(ed) == _edges.end() )
|
|
|
|
{
|
|
|
|
Edge *edge = new Edge(_nodes[n1],_nodes[n2]);
|
|
|
|
scene()->addItem(edge);
|
|
|
|
_edges[ed] = edge ;
|
|
|
|
std::cerr << "Added edge " << n1 << " - " << n2 << std::endl ;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0 ;
|
2009-05-23 11:13:01 -04:00
|
|
|
}
|
2010-11-05 18:46:40 -04:00
|
|
|
|
2008-03-20 19:49:12 -04:00
|
|
|
void GraphWidget::itemMoved()
|
|
|
|
{
|
|
|
|
if (!timerId)
|
2010-11-01 10:17:18 -04:00
|
|
|
{
|
|
|
|
std::cout << "starting timer" << std::endl;
|
2010-10-22 16:47:08 -04:00
|
|
|
timerId = startTimer(1000 / 25);
|
2010-11-01 10:17:18 -04:00
|
|
|
}
|
2008-03-20 19:49:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphWidget::keyPressEvent(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
switch (event->key()) {
|
2010-10-22 16:47:08 -04:00
|
|
|
// case Qt::Key_Up:
|
|
|
|
// centerNode->moveBy(0, -20);
|
|
|
|
// break;
|
|
|
|
// case Qt::Key_Down:
|
|
|
|
// centerNode->moveBy(0, 20);
|
|
|
|
// break;
|
|
|
|
// case Qt::Key_Left:
|
|
|
|
// centerNode->moveBy(-20, 0);
|
|
|
|
// break;
|
|
|
|
// case Qt::Key_Right:
|
|
|
|
// centerNode->moveBy(20, 0);
|
|
|
|
// break;
|
2008-03-20 19:49:12 -04:00
|
|
|
case Qt::Key_Plus:
|
2010-10-22 16:47:08 -04:00
|
|
|
scaleView(qreal(1.2));
|
2008-03-20 19:49:12 -04:00
|
|
|
break;
|
|
|
|
case Qt::Key_Minus:
|
2010-10-22 16:47:08 -04:00
|
|
|
scaleView(1 / qreal(1.2));
|
2008-03-20 19:49:12 -04:00
|
|
|
break;
|
|
|
|
case Qt::Key_Space:
|
|
|
|
case Qt::Key_Enter:
|
|
|
|
foreach (QGraphicsItem *item, scene()->items()) {
|
|
|
|
if (qgraphicsitem_cast<Node *>(item))
|
|
|
|
item->setPos(-150 + qrand() % 300, -150 + qrand() % 300);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
QGraphicsView::keyPressEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-22 16:47:08 -04:00
|
|
|
static void convolveWithGaussian(double *forceMap,int S,int s)
|
2008-03-20 19:49:12 -04:00
|
|
|
{
|
2010-10-22 16:47:08 -04:00
|
|
|
static double *bf = NULL ;
|
2008-03-20 19:49:12 -04:00
|
|
|
|
2010-10-22 16:47:08 -04:00
|
|
|
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) ;
|
2008-03-20 19:49:12 -04:00
|
|
|
|
2010-10-22 16:47:08 -04:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
|
2010-10-22 16:54:32 -04:00
|
|
|
if(!isVisible())
|
|
|
|
return ;
|
|
|
|
|
2010-10-22 16:47:08 -04:00
|
|
|
static const int S = 256 ;
|
|
|
|
static double *forceMap = new double[2*S*S] ;
|
|
|
|
|
|
|
|
memset(forceMap,0,2*S*S*sizeof(double)) ;
|
|
|
|
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);
|
|
|
|
}
|
2008-03-20 19:49:12 -04:00
|
|
|
|
|
|
|
bool itemsMoved = false;
|
2010-11-01 10:17:18 -04:00
|
|
|
foreach (Node *node, _nodes)
|
|
|
|
if(node->advance())
|
2008-03-20 19:49:12 -04:00
|
|
|
itemsMoved = true;
|
|
|
|
|
|
|
|
if (!itemsMoved) {
|
|
|
|
killTimer(timerId);
|
2010-11-01 10:17:18 -04:00
|
|
|
std::cerr << "Killing timr" << std::endl ;
|
2008-03-20 19:49:12 -04:00
|
|
|
timerId = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-01 10:17:18 -04:00
|
|
|
void GraphWidget::setEdgeLength(uint32_t l)
|
|
|
|
{
|
|
|
|
_edge_length = l ;
|
|
|
|
|
|
|
|
if(!timerId)
|
|
|
|
{
|
|
|
|
std::cout << "starting timer" << std::endl;
|
|
|
|
timerId = startTimer(1000 / 25);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-20 19:49:12 -04:00
|
|
|
void GraphWidget::wheelEvent(QWheelEvent *event)
|
|
|
|
{
|
|
|
|
scaleView(pow((double)2, -event->delta() / 240.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
|
|
|
|
{
|
|
|
|
Q_UNUSED(rect);
|
|
|
|
|
|
|
|
// Shadow
|
|
|
|
QRectF sceneRect = this->sceneRect();
|
|
|
|
QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
|
|
|
|
QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
|
|
|
|
if (rightShadow.intersects(rect) || rightShadow.contains(rect))
|
|
|
|
painter->fillRect(rightShadow, Qt::darkGray);
|
|
|
|
if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
|
|
|
|
painter->fillRect(bottomShadow, Qt::darkGray);
|
|
|
|
|
|
|
|
// Fill
|
|
|
|
QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
|
|
|
|
gradient.setColorAt(0, Qt::white);
|
|
|
|
gradient.setColorAt(1, Qt::lightGray);
|
|
|
|
painter->fillRect(rect.intersect(sceneRect), gradient);
|
|
|
|
painter->setBrush(Qt::NoBrush);
|
|
|
|
painter->drawRect(sceneRect);
|
|
|
|
|
|
|
|
// Text
|
2010-10-22 16:47:08 -04:00
|
|
|
QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
|
|
|
|
sceneRect.width() - 4, sceneRect.height() - 4);
|
|
|
|
QString message(tr("Click and drag the nodes around, and zoom with the mouse "
|
|
|
|
"wheel or the '+' and '-' keys"));
|
|
|
|
|
|
|
|
QFont font = painter->font();
|
|
|
|
font.setBold(true);
|
|
|
|
font.setPointSize(14);
|
|
|
|
painter->setFont(font);
|
|
|
|
painter->setPen(Qt::lightGray);
|
|
|
|
painter->drawText(textRect.translated(2, 2), message);
|
|
|
|
painter->setPen(Qt::black);
|
|
|
|
painter->drawText(textRect, message);
|
2008-03-20 19:49:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphWidget::scaleView(qreal scaleFactor)
|
|
|
|
{
|
|
|
|
qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
|
|
|
|
if (factor < 0.07 || factor > 100)
|
|
|
|
return;
|
|
|
|
|
|
|
|
scale(scaleFactor, scaleFactor);
|
|
|
|
}
|