|
|
|
@ -7,7 +7,9 @@
|
|
|
|
|
#include "math.h"
|
|
|
|
|
|
|
|
|
|
GraphWidget::GraphWidget(QWidget *parent) :
|
|
|
|
|
QGraphicsView(parent)
|
|
|
|
|
QGraphicsView(parent),
|
|
|
|
|
m_activeNode(0),
|
|
|
|
|
m_showingNodeNumbers(false)
|
|
|
|
|
{
|
|
|
|
|
qDebug() << __PRETTY_FUNCTION__;
|
|
|
|
|
|
|
|
|
@ -21,35 +23,41 @@ GraphWidget::GraphWidget(QWidget *parent) :
|
|
|
|
|
setTransformationAnchor(AnchorUnderMouse);
|
|
|
|
|
setMinimumSize(400, 400);
|
|
|
|
|
|
|
|
|
|
Node *node1 = new Node();
|
|
|
|
|
Node *node1 = new Node(this);
|
|
|
|
|
node1->setHtml(QString("me"));
|
|
|
|
|
m_scene->addItem(node1);
|
|
|
|
|
node1->setPos(-10, -10);
|
|
|
|
|
m_nodeList.append(node1);
|
|
|
|
|
|
|
|
|
|
Node *node2 = new Node();
|
|
|
|
|
Node *node2 = new Node(this);
|
|
|
|
|
node2->setHtml(QString("work"));
|
|
|
|
|
m_scene->addItem(node2);
|
|
|
|
|
node2->setPos(60, -10);
|
|
|
|
|
m_nodeList.append(node2);
|
|
|
|
|
|
|
|
|
|
Node *node3 = new Node();
|
|
|
|
|
Node *node3 = new Node(this);
|
|
|
|
|
node3->setHtml(QString("read"));
|
|
|
|
|
m_scene->addItem(node3);
|
|
|
|
|
node3->setPos(-70, -10);
|
|
|
|
|
m_nodeList.append(node3);
|
|
|
|
|
|
|
|
|
|
Node *node4 = new Node();
|
|
|
|
|
Node *node4 = new Node(this);
|
|
|
|
|
node4->setHtml(QString("pragmatic programmer"));
|
|
|
|
|
m_scene->addItem(node4);
|
|
|
|
|
node4->setPos(-120, -80);
|
|
|
|
|
m_nodeList.append(node4);
|
|
|
|
|
|
|
|
|
|
Node *node5 = new Node();
|
|
|
|
|
Node *node5 = new Node(this);
|
|
|
|
|
node5->setHtml(QString("joy"));
|
|
|
|
|
m_scene->addItem(node5);
|
|
|
|
|
node5->setPos(-10, 50);
|
|
|
|
|
m_nodeList.append(node5);
|
|
|
|
|
|
|
|
|
|
Node *node6 = new Node();
|
|
|
|
|
Node *node6 = new Node(this);
|
|
|
|
|
node6->setHtml(QString("rape goats"));
|
|
|
|
|
m_scene->addItem(node6);
|
|
|
|
|
node6->setPos(-10, 100);
|
|
|
|
|
m_nodeList.append(node6);
|
|
|
|
|
|
|
|
|
|
m_scene->addItem(new Edge(node1, node2));
|
|
|
|
|
m_scene->addItem(new Edge(node1, node3));
|
|
|
|
@ -57,9 +65,8 @@ GraphWidget::GraphWidget(QWidget *parent) :
|
|
|
|
|
m_scene->addItem(new Edge(node1, node5));
|
|
|
|
|
m_scene->addItem(new Edge(node5, node6));
|
|
|
|
|
|
|
|
|
|
m_activeNode = node1;
|
|
|
|
|
m_activeNode->setFocus();
|
|
|
|
|
|
|
|
|
|
m_activeNode = m_nodeList.first();
|
|
|
|
|
m_activeNode->setActive(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QGraphicsScene *GraphWidget::getScene()
|
|
|
|
@ -69,32 +76,54 @@ QGraphicsScene *GraphWidget::getScene()
|
|
|
|
|
|
|
|
|
|
void GraphWidget::keyPressEvent(QKeyEvent *event)
|
|
|
|
|
{
|
|
|
|
|
qDebug() << __PRETTY_FUNCTION__;
|
|
|
|
|
// qDebug() << event->key();
|
|
|
|
|
|
|
|
|
|
switch (event->key()) {
|
|
|
|
|
// case Qt::Key_Up:
|
|
|
|
|
// centerNode->moveBy(0, -20);
|
|
|
|
|
// break;
|
|
|
|
|
// case Qt::Key_Down:
|
|
|
|
|
// centerNode->moveBy(0, 20);
|
|
|
|
|
// break;
|
|
|
|
|
// case Qt::Key_Left:
|
|
|
|
|
// centerNode->moveBy(-20, 0);
|
|
|
|
|
// break;
|
|
|
|
|
// case Qt::Key_Right:
|
|
|
|
|
// centerNode->moveBy(20, 0);
|
|
|
|
|
// break;
|
|
|
|
|
case Qt::Key_Up:
|
|
|
|
|
m_activeNode->moveBy(0, -20);
|
|
|
|
|
break;
|
|
|
|
|
case Qt::Key_Down:
|
|
|
|
|
m_activeNode->moveBy(0, 20);
|
|
|
|
|
break;
|
|
|
|
|
case Qt::Key_Left:
|
|
|
|
|
m_activeNode->moveBy(-20, 0);
|
|
|
|
|
break;
|
|
|
|
|
case Qt::Key_Right:
|
|
|
|
|
m_activeNode->moveBy(20, 0);
|
|
|
|
|
break;
|
|
|
|
|
case Qt::Key_Plus:
|
|
|
|
|
scaleView(qreal(1.2));
|
|
|
|
|
break;
|
|
|
|
|
case Qt::Key_Minus:
|
|
|
|
|
scaleView(1 / qreal(1.2));
|
|
|
|
|
break;
|
|
|
|
|
// case Qt::Key_Space:
|
|
|
|
|
// case Qt::Key_Enter:
|
|
|
|
|
// foreach (QGraphicsItem *item, scene()->items()) {
|
|
|
|
|
// if (qgraphicsitem_cast<Node *>(item))
|
|
|
|
|
// item->setPos(-150 + qrand() % 300, -150 + qrand() % 300);
|
|
|
|
|
// }
|
|
|
|
|
// break;
|
|
|
|
|
case Qt::Key_F:
|
|
|
|
|
{ // need brackets because of local variable
|
|
|
|
|
m_showingNodeNumbers = !m_showingNodeNumbers;
|
|
|
|
|
int i =0;
|
|
|
|
|
for (QList<Node *>::const_iterator it = m_nodeList.begin(); it != m_nodeList.end(); it++, i++)
|
|
|
|
|
dynamic_cast<Node*>(*it)->showNumber(i,m_showingNodeNumbers);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Qt::Key_0:
|
|
|
|
|
case Qt::Key_1:
|
|
|
|
|
case Qt::Key_2:
|
|
|
|
|
case Qt::Key_3:
|
|
|
|
|
case Qt::Key_4:
|
|
|
|
|
case Qt::Key_5:
|
|
|
|
|
case Qt::Key_6:
|
|
|
|
|
case Qt::Key_7:
|
|
|
|
|
case Qt::Key_8:
|
|
|
|
|
case Qt::Key_9:
|
|
|
|
|
|
|
|
|
|
m_activeNode->setActive(false);
|
|
|
|
|
m_activeNode = m_nodeList[3];
|
|
|
|
|
m_activeNode->setActive(true);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
QGraphicsView::keyPressEvent(event);
|
|
|
|
|
}
|
|
|
|
@ -109,14 +138,7 @@ 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());
|
|
|
|
@ -125,21 +147,6 @@ void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
|
|
|
|
|
painter->fillRect(rect.intersect(sceneRect), gradient);
|
|
|
|
|
painter->setBrush(Qt::NoBrush);
|
|
|
|
|
painter->drawRect(sceneRect);
|
|
|
|
|
|
|
|
|
|
// // Text
|
|
|
|
|
// QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
|
|
|
|
|
// sceneRect.width() - 4, sceneRect.height() - 4);
|
|
|
|
|
// QString message(tr("Click and drag the nodes around, and zoom with the mouse "
|
|
|
|
|
// "wheel or the '+' and '-' keys"));
|
|
|
|
|
|
|
|
|
|
// QFont font = painter->font();
|
|
|
|
|
// font.setBold(true);
|
|
|
|
|
// font.setPointSize(14);
|
|
|
|
|
// painter->setFont(font);
|
|
|
|
|
// painter->setPen(Qt::lightGray);
|
|
|
|
|
// painter->drawText(textRect.translated(2, 2), message);
|
|
|
|
|
// painter->setPen(Qt::black);
|
|
|
|
|
// painter->drawText(textRect, message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -150,3 +157,15 @@ void GraphWidget::scaleView(qreal scaleFactor)
|
|
|
|
|
|
|
|
|
|
scale(scaleFactor, scaleFactor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// @note this == 0? how is it possible?
|
|
|
|
|
void GraphWidget::setActiveNode(Node *node)
|
|
|
|
|
{
|
|
|
|
|
qDebug() << __PRETTY_FUNCTION__;
|
|
|
|
|
|
|
|
|
|
if (m_activeNode!=0)
|
|
|
|
|
m_activeNode->setActive(false);
|
|
|
|
|
|
|
|
|
|
m_activeNode = node;
|
|
|
|
|
m_activeNode->setActive(true);
|
|
|
|
|
}
|
|
|
|
|