From e039fce7f48b83d07cbf96bd7b288c88a282f6d4 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Fri, 17 Jun 2011 19:51:40 +0200 Subject: [PATCH] Node class refactor: order of definiation of functions in .cpp is the same as the declaration in the .h. Bug corrected: during editing a node, selection another with mouse and remain in editing mode. --- graphwidget.cpp | 5 +- graphwidget.h | 9 -- node.cpp | 298 ++++++++++++++++++++++-------------------------- node.h | 11 +- 4 files changed, 150 insertions(+), 173 deletions(-) diff --git a/graphwidget.cpp b/graphwidget.cpp index d732660..27598b9 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -476,6 +476,9 @@ void GraphWidget::nodeSelected(Node *node) showingAllNodeNumbers(false); m_showingNodeNumbers = false; + node->setEditable(false); + m_editingNode = false; + if (m_edgeAdding) { addEdge(m_activeNode, node); @@ -513,7 +516,7 @@ void GraphWidget::removeEdge(Node *source, Node *destination) } else { - source->removeEdge(destination); + source->deleteEdge(destination); contentChanged(); } } diff --git a/graphwidget.h b/graphwidget.h index 2684428..7938149 100644 --- a/graphwidget.h +++ b/graphwidget.h @@ -6,7 +6,6 @@ #include #include -//#include "mainwindow.h" #include "node.h" class MainWindow; @@ -22,24 +21,16 @@ public: void insertNode(); void setActiveNodeEditable(); void nodeSelected(Node *node); -// int nodeId(Node *node); QList edges() const; void contentChanged(const bool &changed = true); -//public slots: - void newScene(); void closeScene(); void readContentFromXmlFile(const QString &fileName); void writeContentToXmlFile(const QString &fileName); void writeContentToPngFile(const QString &fileName); -// void savetoFile(); -// void saveFileAs(); -// void openFile(const QString &fileName); -// void openFile(); - protected: void keyPressEvent(QKeyEvent *event); diff --git a/node.cpp b/node.cpp index 5bcacad..b877628 100644 --- a/node.cpp +++ b/node.cpp @@ -6,9 +6,9 @@ #include #include -static const double Pi = 3.14159265358979323846264338327950288419717; -static double TwoPi = 2.0 * Pi; -static double OneAndHalfPi = 1.5 * Pi; +const double Node::m_pi = 3.14159265358979323846264338327950288419717; +const double Node::m_oneAndHalfPi = Node::m_pi * 1.5; +const double Node::m_twoPi = Node::m_pi * 2.0; Node::Node(GraphWidget *parent) : m_graph(parent), @@ -19,9 +19,7 @@ Node::Node(GraphWidget *parent) : { setFlag(ItemIsMovable); setFlag(ItemSendsGeometryChanges); - setCacheMode(DeviceCoordinateCache); - setDefaultTextColor(QColor(0,0,0)); } @@ -37,20 +35,7 @@ void Node::addEdge(Edge *edge, bool startsFromThisNode) edge->adjust(); } -void Node::removeEdgeFromList(Edge *edge) -{ - for(QList::iterator it = m_edgeList.begin(); - it != m_edgeList.end(); it++) - { - if (it->edge == edge) - { - m_edgeList.erase(it); - return; - } - } -} - -void Node::removeEdge(Node *otherEnd) +void Node::deleteEdge(Node *otherEnd) { for(QList::iterator it = m_edgeList.begin(); it != m_edgeList.end(); it++) @@ -66,116 +51,41 @@ void Node::removeEdge(Node *otherEnd) } } -QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) +void Node::removeEdgeFromList(Edge *edge) { - switch (change) { - - case ItemPositionChange: - - if (change == ItemPositionChange && scene()) + for(QList::iterator it = m_edgeList.begin(); + it != m_edgeList.end(); it++) + { + if (it->edge == edge) { - // value is the new position. - QPointF newPos = value.toPointF(); - - // the fence is reduced with the size of the node - QRectF rect (scene()->sceneRect().topLeft(), - scene()->sceneRect().bottomRight() - - boundingRect().bottomRight()); - - if (!rect.contains(newPos)) - { - // Keep the item inside the scene rect. - newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left()))); - newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top()))); - return newPos; - } + m_edgeList.erase(it); + return; } - break; - - case ItemPositionHasChanged: - - m_graph->contentChanged(); - foreach (EdgeElement element, m_edgeList) element.edge->adjust(); - break; - - default: - break; - }; - - return QGraphicsItem::itemChange(change, value); + } } -void Node::paint(QPainter *painter, - const QStyleOptionGraphicsItem *option, - QWidget *w) +void Node::setBorder(const bool &hasBorder) { - // draw background in hint mode. num == -1 : not in hint mode - // if m_numberIsSpecial (can be selected with enter) bg is green, not yellow - if (m_number != -1) - { - painter->setPen(Qt::transparent); - painter->setBrush(m_numberIsSpecial ? Qt::green : Qt::yellow); - - /// @bug is there a 1pixel wide yellow line at the - /// bottom of borderless items? - painter->drawRect(QRect(boundingRect().topLeft().toPoint(), - boundingRect().bottomRight().toPoint())); - painter->setBrush(Qt::NoBrush); - } - - // the text itself - QGraphicsTextItem::paint(painter, option, w); - - if (m_hasBorder) - { - painter->setPen(m_isActive ? Qt::red : Qt::blue); - painter->drawRect(QRect(boundingRect().topLeft().toPoint(), - boundingRect().bottomRight().toPoint() - - QPoint(1,1))); - } - - // print num to topleft corner in hint mode. - if (m_number != -1) - { - painter->setPen(Qt::white); - painter->setBackground(Qt::red); - painter->setBackgroundMode(Qt::OpaqueMode); - painter->drawText(boundingRect().topLeft()+QPointF(0,11), - QString("%1").arg(m_number)); - } + m_hasBorder = hasBorder; + update(); } - - void Node::setActive(const bool &active) { m_isActive = active; - // update border color update(); } - -void Node::mousePressEvent(QGraphicsSceneMouseEvent *event) -{ - m_graph->nodeSelected(this); - - QGraphicsItem::mousePressEvent(event); -} - -void Node::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) -{ - Q_UNUSED(event); - m_graph->setActiveNodeEditable(); -} - -void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +void Node::setEditable(const bool &editable) { - QGraphicsItem::mouseReleaseEvent(event); -} + setTextInteractionFlags( + editable ? + Qt::TextEditable : + Qt::NoTextInteraction); -void Node::mouseMoveEvent(QGraphicsSceneMouseEvent *event) -{ - QGraphicsItem::mouseMoveEvent(event); + QTextCursor c = textCursor(); + c.setPosition(c.document()->toPlainText().length()); + setTextCursor(c); } void Node::showNumber(const int &number, @@ -187,42 +97,29 @@ void Node::showNumber(const int &number, update(); } -void Node::setBorder(const bool &hasBorder) -{ - m_hasBorder = hasBorder; - update(); -} - double Node::calculateBiggestAngle() { if (m_edgeList.empty()) - return OneAndHalfPi; + return Node::m_oneAndHalfPi; if (m_edgeList.size()==1) - { - if (m_edgeList.first().startsFromThisNode) - { - return Pi - m_edgeList.first().edge->getAngle(); - } - else - { - return TwoPi - m_edgeList.first().edge->getAngle(); - } - } + return m_edgeList.first().startsFromThisNode ? + Node::m_pi - m_edgeList.first().edge->getAngle() : + Node::m_twoPi - m_edgeList.first().edge->getAngle(); QList tmp; for(QList::iterator it = m_edgeList.begin(); it != m_edgeList.end(); it++) { tmp.push_back(it->startsFromThisNode ? - it->edge->getAngle() : - doubleModulo(Pi + it->edge->getAngle(), TwoPi)); + it->edge->getAngle() : + doubleModulo(Node::m_pi + it->edge->getAngle(), Node::m_twoPi)); } qSort(tmp.begin(), tmp.end()); double prev(tmp.first()); double max_prev(tmp.last()); - double max(TwoPi - tmp.last() + tmp.first()); + double max(Node::m_twoPi - tmp.last() + tmp.first()); for(QList::const_iterator it = ++tmp.begin(); it!=tmp.end(); it++) { @@ -234,32 +131,7 @@ double Node::calculateBiggestAngle() prev = *it; } - return TwoPi - doubleModulo(max_prev + max / 2, TwoPi); -} - -void Node::linkActivated(const QString &link) -{ - qDebug() << __PRETTY_FUNCTION__; - - qDebug() << link; -} - -double Node::doubleModulo(const double &devided, const double &devisor) -{ - return devided - static_cast(devisor * static_cast(devided - / devisor)); -} - -void Node::setEditable(const bool &editable) -{ - setTextInteractionFlags( - editable ? - Qt::TextEditable : - Qt::NoTextInteraction); - - QTextCursor c = textCursor(); - c.setPosition(c.document()->toPlainText().length()); - setTextCursor(c); + return Node::m_twoPi - doubleModulo(max_prev + max / 2, Node::m_twoPi); } void Node::keyPressEvent(QKeyEvent *event) @@ -333,3 +205,111 @@ QList Node::edgesFrom() const return list; } + +void Node::paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, + QWidget *w) +{ + // draw background in hint mode. num == -1 : not in hint mode + // if m_numberIsSpecial (can be selected with enter) bg is green, not yellow + if (m_number != -1) + { + painter->setPen(Qt::transparent); + painter->setBrush(m_numberIsSpecial ? Qt::green : Qt::yellow); + + /// @bug is there a 1pixel wide yellow line at the + /// bottom of borderless items? + painter->drawRect(QRect(boundingRect().topLeft().toPoint(), + boundingRect().bottomRight().toPoint())); + painter->setBrush(Qt::NoBrush); + } + + // the text itself + QGraphicsTextItem::paint(painter, option, w); + + if (m_hasBorder) + { + painter->setPen(m_isActive ? Qt::red : Qt::blue); + painter->drawRect(QRect(boundingRect().topLeft().toPoint(), + boundingRect().bottomRight().toPoint() - + QPoint(1,1))); + } + + // print num to topleft corner in hint mode. + if (m_number != -1) + { + painter->setPen(Qt::white); + painter->setBackground(Qt::red); + painter->setBackgroundMode(Qt::OpaqueMode); + painter->drawText(boundingRect().topLeft()+QPointF(0,11), + QString("%1").arg(m_number)); + } +} + +QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) +{ + switch (change) { + + case ItemPositionChange: + + if (change == ItemPositionChange && scene()) + { + // value is the new position. + QPointF newPos = value.toPointF(); + + // the fence is reduced with the size of the node + QRectF rect (scene()->sceneRect().topLeft(), + scene()->sceneRect().bottomRight() - + boundingRect().bottomRight()); + + if (!rect.contains(newPos)) + { + // Keep the item inside the scene rect. + newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left()))); + newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top()))); + return newPos; + } + } + break; + + case ItemPositionHasChanged: + + m_graph->contentChanged(); + foreach (EdgeElement element, m_edgeList) element.edge->adjust(); + break; + + default: + break; + }; + + return QGraphicsItem::itemChange(change, value); +} + +void Node::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + m_graph->nodeSelected(this); + + QGraphicsItem::mousePressEvent(event); +} + +void Node::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) +{ + Q_UNUSED(event); + m_graph->setActiveNodeEditable(); +} + +void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +{ + QGraphicsItem::mouseReleaseEvent(event); +} + +void Node::mouseMoveEvent(QGraphicsSceneMouseEvent *event) +{ + QGraphicsItem::mouseMoveEvent(event); +} + +double Node::doubleModulo(const double &devided, const double &devisor) +{ + return devided - static_cast(devisor * static_cast(devided + / devisor)); +} diff --git a/node.h b/node.h index 4ceb22b..6b80690 100644 --- a/node.h +++ b/node.h @@ -17,14 +17,16 @@ public: ~Node(); void addEdge(Edge *edge, bool startsFromThisNode); + void deleteEdge(Node *otherEnd); void removeEdgeFromList(Edge *edge); - void removeEdge(Node *otherEnd); + void setBorder(const bool &hasBorder); void setActive(const bool &active = true); + void setEditable(const bool &editable = true); + void showNumber(const int &number, const bool& show = true, const bool &numberIsSpecial = false); double calculateBiggestAngle(); - void setEditable(const bool &editable = true); // changing visibility from prot to pub void keyPressEvent(QKeyEvent *event); @@ -41,7 +43,6 @@ protected: void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event); - void linkActivated(const QString &link); private: @@ -60,8 +61,10 @@ private: int m_number; bool m_hasBorder; bool m_numberIsSpecial; -// QTextCursor m_cursor; + static const double m_pi; + static const double m_oneAndHalfPi; + static const double m_twoPi; }; #endif // NODE_H