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

@ -7,7 +7,9 @@
#include "math.h" #include "math.h"
GraphWidget::GraphWidget(QWidget *parent) : GraphWidget::GraphWidget(QWidget *parent) :
QGraphicsView(parent) QGraphicsView(parent),
m_activeNode(0),
m_showingNodeNumbers(false)
{ {
qDebug() << __PRETTY_FUNCTION__; qDebug() << __PRETTY_FUNCTION__;
@ -21,35 +23,41 @@ GraphWidget::GraphWidget(QWidget *parent) :
setTransformationAnchor(AnchorUnderMouse); setTransformationAnchor(AnchorUnderMouse);
setMinimumSize(400, 400); setMinimumSize(400, 400);
Node *node1 = new Node(); Node *node1 = new Node(this);
node1->setHtml(QString("me")); node1->setHtml(QString("me"));
m_scene->addItem(node1); m_scene->addItem(node1);
node1->setPos(-10, -10); node1->setPos(-10, -10);
m_nodeList.append(node1);
Node *node2 = new Node(); Node *node2 = new Node(this);
node2->setHtml(QString("work")); node2->setHtml(QString("work"));
m_scene->addItem(node2); m_scene->addItem(node2);
node2->setPos(60, -10); node2->setPos(60, -10);
m_nodeList.append(node2);
Node *node3 = new Node(); Node *node3 = new Node(this);
node3->setHtml(QString("read")); node3->setHtml(QString("read"));
m_scene->addItem(node3); m_scene->addItem(node3);
node3->setPos(-70, -10); node3->setPos(-70, -10);
m_nodeList.append(node3);
Node *node4 = new Node(); Node *node4 = new Node(this);
node4->setHtml(QString("pragmatic programmer")); node4->setHtml(QString("pragmatic programmer"));
m_scene->addItem(node4); m_scene->addItem(node4);
node4->setPos(-120, -80); node4->setPos(-120, -80);
m_nodeList.append(node4);
Node *node5 = new Node(); Node *node5 = new Node(this);
node5->setHtml(QString("joy")); node5->setHtml(QString("joy"));
m_scene->addItem(node5); m_scene->addItem(node5);
node5->setPos(-10, 50); node5->setPos(-10, 50);
m_nodeList.append(node5);
Node *node6 = new Node(); Node *node6 = new Node(this);
node6->setHtml(QString("rape goats")); node6->setHtml(QString("rape goats"));
m_scene->addItem(node6); m_scene->addItem(node6);
node6->setPos(-10, 100); node6->setPos(-10, 100);
m_nodeList.append(node6);
m_scene->addItem(new Edge(node1, node2)); m_scene->addItem(new Edge(node1, node2));
m_scene->addItem(new Edge(node1, node3)); 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(node1, node5));
m_scene->addItem(new Edge(node5, node6)); m_scene->addItem(new Edge(node5, node6));
m_activeNode = node1; m_activeNode = m_nodeList.first();
m_activeNode->setFocus(); m_activeNode->setActive(true);
} }
QGraphicsScene *GraphWidget::getScene() QGraphicsScene *GraphWidget::getScene()
@ -69,32 +76,54 @@ QGraphicsScene *GraphWidget::getScene()
void GraphWidget::keyPressEvent(QKeyEvent *event) void GraphWidget::keyPressEvent(QKeyEvent *event)
{ {
qDebug() << __PRETTY_FUNCTION__;
// qDebug() << event->key();
switch (event->key()) { switch (event->key()) {
// case Qt::Key_Up: case Qt::Key_Up:
// centerNode->moveBy(0, -20); m_activeNode->moveBy(0, -20);
// break; break;
// case Qt::Key_Down: case Qt::Key_Down:
// centerNode->moveBy(0, 20); m_activeNode->moveBy(0, 20);
// break; break;
// case Qt::Key_Left: case Qt::Key_Left:
// centerNode->moveBy(-20, 0); m_activeNode->moveBy(-20, 0);
// break; break;
// case Qt::Key_Right: case Qt::Key_Right:
// centerNode->moveBy(20, 0); m_activeNode->moveBy(20, 0);
// break; break;
case Qt::Key_Plus: case Qt::Key_Plus:
scaleView(qreal(1.2)); scaleView(qreal(1.2));
break; break;
case Qt::Key_Minus: case Qt::Key_Minus:
scaleView(1 / qreal(1.2)); scaleView(1 / qreal(1.2));
break; break;
// case Qt::Key_Space: case Qt::Key_F:
// case Qt::Key_Enter: { // need brackets because of local variable
// foreach (QGraphicsItem *item, scene()->items()) { m_showingNodeNumbers = !m_showingNodeNumbers;
// if (qgraphicsitem_cast<Node *>(item)) int i =0;
// item->setPos(-150 + qrand() % 300, -150 + qrand() % 300); for (QList<Node *>::const_iterator it = m_nodeList.begin(); it != m_nodeList.end(); it++, i++)
// } dynamic_cast<Node*>(*it)->showNumber(i,m_showingNodeNumbers);
// break; }
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: default:
QGraphicsView::keyPressEvent(event); QGraphicsView::keyPressEvent(event);
} }
@ -109,14 +138,7 @@ void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
{ {
Q_UNUSED(rect); Q_UNUSED(rect);
// Shadow
QRectF sceneRect = this->sceneRect(); 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 // Fill
QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight()); 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->fillRect(rect.intersect(sceneRect), gradient);
painter->setBrush(Qt::NoBrush); painter->setBrush(Qt::NoBrush);
painter->drawRect(sceneRect); 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); 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: public:
GraphWidget(QWidget *parent = 0); GraphWidget(QWidget *parent = 0);
QGraphicsScene *getScene(); QGraphicsScene *getScene();
void setActiveNode(Node *node);
protected: protected:
void keyPressEvent(QKeyEvent *event); void keyPressEvent(QKeyEvent *event);
void wheelEvent(QWheelEvent *event); void wheelEvent(QWheelEvent *event);
void scaleView(qreal scaleFactor); void scaleView(qreal scaleFactor);
void drawBackground(QPainter *painter, const QRectF &rect); void drawBackground(QPainter *painter, const QRectF &rect);
private: private:
QList<Node *> m_nodeList;
Node *m_activeNode; Node *m_activeNode;
QGraphicsScene *m_scene; QGraphicsScene *m_scene;
bool m_showingNodeNumbers;
}; };

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

@ -5,7 +5,11 @@
#include <QDebug> #include <QDebug>
#include <QGraphicsSceneMouseEvent> #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__; qDebug() << __PRETTY_FUNCTION__;
@ -38,7 +42,8 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
case ItemPositionChange: case ItemPositionChange:
if (change == ItemPositionChange && scene()) { if (change == ItemPositionChange && scene())
{
// value is the new position. // value is the new position.
QPointF newPos = value.toPointF(); QPointF newPos = value.toPointF();
QRectF rect (scene()->sceneRect().topLeft(), QRectF rect (scene()->sceneRect().topLeft(),
@ -71,8 +76,59 @@ void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
QGraphicsTextItem::paint(painter, option, w); QGraphicsTextItem::paint(painter, option, w);
painter->setPen(QPen(Qt::blue)); painter->setPen(m_isActive ? Qt::red : Qt::blue);
painter->drawRect(QRect(boundingRect().topLeft().toPoint(), painter->drawRect(QRect(boundingRect().topLeft().toPoint(),
boundingRect().bottomRight().toPoint() - boundingRect().bottomRight().toPoint() -
QPoint(1,1))); 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); Node(GraphWidget *graphWidget = 0);
void addEdge(Edge *edge); 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: protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); 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: private:
QList<Edge *> m_edgeList; QList<Edge *> m_edgeList;
GraphWidget *m_graph; GraphWidget *m_graph;
bool m_isActive;
Edge *m_activeEdge;
int m_number;
}; };
#endif // NODE_H #endif // NODE_H

Loading…
Cancel
Save