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.

master
Denes Matetelki 14 years ago
parent d0eb7ce0f3
commit e039fce7f4

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

@ -6,7 +6,6 @@
#include <QKeyEvent>
#include <QMovie>
//#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<Edge *> 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);

@ -6,9 +6,9 @@
#include <QGraphicsSceneMouseEvent>
#include <QTextDocument>
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<EdgeElement>::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<EdgeElement>::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<EdgeElement>::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<double> tmp;
for(QList<EdgeElement>::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<double>::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<double>(devisor * static_cast<int>(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<Edge *> 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<double>(devisor * static_cast<int>(devided
/ devisor));
}

@ -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

Loading…
Cancel
Save