node can be set active with mouseRelease, button 'f' show/hides nodenumbers

master
Denes Matetelki 14 years ago
parent c10b56fe63
commit a6c1ec643f

@ -8,6 +8,7 @@ class Node;
class Edge : public QGraphicsItem
{
public:
Edge(Node *sourceNode, Node *destNode);
Node *sourceNode() const;
@ -15,20 +16,20 @@ public:
void adjust();
// enum { Type = UserType + 2 };
// int type() const { return Type; }
protected:
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
private:
Node *m_sourceNode;
Node *m_destNode;
QPointF m_sourcePoint;
QPointF m_destPoint;
qreal m_arrowSize;
};
#endif

@ -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);
}

@ -16,16 +16,23 @@ class GraphWidget : public QGraphicsView
public:
GraphWidget(QWidget *parent = 0);
QGraphicsScene *getScene();
void setActiveNode(Node *node);
protected:
void keyPressEvent(QKeyEvent *event);
void wheelEvent(QWheelEvent *event);
void scaleView(qreal scaleFactor);
void drawBackground(QPainter *painter, const QRectF &rect);
private:
QList<Node *> m_nodeList;
Node *m_activeNode;
QGraphicsScene *m_scene;
bool m_showingNodeNumbers;
};

@ -89,6 +89,7 @@ void MainWindow::exportScene()
m_graphicsView->getScene()->sceneRect().height(),
QImage::Format_ARGB32_Premultiplied);
QPainter painter(&img);
painter.setRenderHint(QPainter::Antialiasing);
/// @bug scene background is not rendered
@ -110,7 +111,6 @@ void MainWindow::about()
if (m_aboutDialog == 0) m_aboutDialog = new AboutDialog(this);
m_aboutDialog->setEnabled(true); // children inherits enabled status
m_aboutDialog->show();
// aboutDialog->layout()->setSizeConstraint( QLayout::SetFixedSize );
}
void MainWindow::aboutDestroyed()

@ -5,7 +5,11 @@
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
Node::Node(GraphWidget *parent) : m_graph(parent)
Node::Node(GraphWidget *parent) :
m_graph(parent),
m_isActive(false),
m_activeEdge(0),
m_number(-1)
{
qDebug() << __PRETTY_FUNCTION__;
@ -38,7 +42,8 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
case ItemPositionChange:
if (change == ItemPositionChange && scene()) {
if (change == ItemPositionChange && scene())
{
// value is the new position.
QPointF newPos = value.toPointF();
QRectF rect (scene()->sceneRect().topLeft(),
@ -71,8 +76,59 @@ void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
QGraphicsTextItem::paint(painter, option, w);
painter->setPen(QPen(Qt::blue));
painter->setPen(m_isActive ? Qt::red : Qt::blue);
painter->drawRect(QRect(boundingRect().topLeft().toPoint(),
boundingRect().bottomRight().toPoint() -
QPoint(1,1)));
qDebug() << m_number;
if (m_number != -1)
{
painter->setPen(Qt::yellow);
painter->setBackground(Qt::black);
painter->setBackgroundMode(Qt::OpaqueMode);
painter->drawText(boundingRect().topLeft()+QPointF(0,11), QString("%1").arg(m_number));
}
}
void Node::setActive(const bool &active)
{
qDebug() << __PRETTY_FUNCTION__;
m_isActive = active;
update();
}
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << __PRETTY_FUNCTION__;
QGraphicsItem::mousePressEvent(event);
}
void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << __PRETTY_FUNCTION__;
m_graph->setActiveNode(this);
QGraphicsItem::mouseReleaseEvent(event);
}
void Node::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << __PRETTY_FUNCTION__;
QGraphicsItem::mouseMoveEvent(event);
}
void Node::showNumber(const int &number, const bool& show)
{
qDebug() << __PRETTY_FUNCTION__;
m_number = show ? number : -1;
update();
}

@ -15,17 +15,26 @@ public:
Node(GraphWidget *graphWidget = 0);
void addEdge(Edge *edge);
QList<Edge *> edges() const;
// QList<Edge *> edges() const;
void setActive(const bool &active);
void showNumber(const int &number, const bool& show);
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
private:
QList<Edge *> m_edgeList;
GraphWidget *m_graph;
bool m_isActive;
Edge *m_activeEdge;
int m_number;
};
#endif // NODE_H

Loading…
Cancel
Save