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); showingAllNodeNumbers(false);
m_showingNodeNumbers = false; m_showingNodeNumbers = false;
node->setEditable(false);
m_editingNode = false;
if (m_edgeAdding) if (m_edgeAdding)
{ {
addEdge(m_activeNode, node); addEdge(m_activeNode, node);
@ -513,7 +516,7 @@ void GraphWidget::removeEdge(Node *source, Node *destination)
} }
else else
{ {
source->removeEdge(destination); source->deleteEdge(destination);
contentChanged(); contentChanged();
} }
} }

@ -6,7 +6,6 @@
#include <QKeyEvent> #include <QKeyEvent>
#include <QMovie> #include <QMovie>
//#include "mainwindow.h"
#include "node.h" #include "node.h"
class MainWindow; class MainWindow;
@ -22,24 +21,16 @@ public:
void insertNode(); void insertNode();
void setActiveNodeEditable(); void setActiveNodeEditable();
void nodeSelected(Node *node); void nodeSelected(Node *node);
// int nodeId(Node *node);
QList<Edge *> edges() const; QList<Edge *> edges() const;
void contentChanged(const bool &changed = true); void contentChanged(const bool &changed = true);
//public slots:
void newScene(); void newScene();
void closeScene(); void closeScene();
void readContentFromXmlFile(const QString &fileName); void readContentFromXmlFile(const QString &fileName);
void writeContentToXmlFile(const QString &fileName); void writeContentToXmlFile(const QString &fileName);
void writeContentToPngFile(const QString &fileName); void writeContentToPngFile(const QString &fileName);
// void savetoFile();
// void saveFileAs();
// void openFile(const QString &fileName);
// void openFile();
protected: protected:
void keyPressEvent(QKeyEvent *event); void keyPressEvent(QKeyEvent *event);

@ -6,9 +6,9 @@
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QTextDocument> #include <QTextDocument>
static const double Pi = 3.14159265358979323846264338327950288419717; const double Node::m_pi = 3.14159265358979323846264338327950288419717;
static double TwoPi = 2.0 * Pi; const double Node::m_oneAndHalfPi = Node::m_pi * 1.5;
static double OneAndHalfPi = 1.5 * Pi; const double Node::m_twoPi = Node::m_pi * 2.0;
Node::Node(GraphWidget *parent) : Node::Node(GraphWidget *parent) :
m_graph(parent), m_graph(parent),
@ -19,9 +19,7 @@ Node::Node(GraphWidget *parent) :
{ {
setFlag(ItemIsMovable); setFlag(ItemIsMovable);
setFlag(ItemSendsGeometryChanges); setFlag(ItemSendsGeometryChanges);
setCacheMode(DeviceCoordinateCache); setCacheMode(DeviceCoordinateCache);
setDefaultTextColor(QColor(0,0,0)); setDefaultTextColor(QColor(0,0,0));
} }
@ -37,20 +35,7 @@ void Node::addEdge(Edge *edge, bool startsFromThisNode)
edge->adjust(); edge->adjust();
} }
void Node::removeEdgeFromList(Edge *edge) void Node::deleteEdge(Node *otherEnd)
{
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)
{ {
for(QList<EdgeElement>::iterator it = m_edgeList.begin(); for(QList<EdgeElement>::iterator it = m_edgeList.begin();
it != m_edgeList.end(); it++) 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) { for(QList<EdgeElement>::iterator it = m_edgeList.begin();
it != m_edgeList.end(); it++)
case ItemPositionChange:
if (change == ItemPositionChange && scene())
{ {
// value is the new position. if (it->edge == edge)
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. m_edgeList.erase(it);
newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left()))); return;
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::paint(QPainter *painter, void Node::setBorder(const bool &hasBorder)
const QStyleOptionGraphicsItem *option,
QWidget *w)
{ {
// draw background in hint mode. num == -1 : not in hint mode m_hasBorder = hasBorder;
// if m_numberIsSpecial (can be selected with enter) bg is green, not yellow update();
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));
}
} }
void Node::setActive(const bool &active) void Node::setActive(const bool &active)
{ {
m_isActive = active; m_isActive = active;
// update border color
update(); update();
} }
void Node::setEditable(const bool &editable)
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); setTextInteractionFlags(
} editable ?
Qt::TextEditable :
Qt::NoTextInteraction);
void Node::mouseMoveEvent(QGraphicsSceneMouseEvent *event) QTextCursor c = textCursor();
{ c.setPosition(c.document()->toPlainText().length());
QGraphicsItem::mouseMoveEvent(event); setTextCursor(c);
} }
void Node::showNumber(const int &number, void Node::showNumber(const int &number,
@ -187,28 +97,15 @@ void Node::showNumber(const int &number,
update(); update();
} }
void Node::setBorder(const bool &hasBorder)
{
m_hasBorder = hasBorder;
update();
}
double Node::calculateBiggestAngle() double Node::calculateBiggestAngle()
{ {
if (m_edgeList.empty()) if (m_edgeList.empty())
return OneAndHalfPi; return Node::m_oneAndHalfPi;
if (m_edgeList.size()==1) if (m_edgeList.size()==1)
{ return m_edgeList.first().startsFromThisNode ?
if (m_edgeList.first().startsFromThisNode) Node::m_pi - m_edgeList.first().edge->getAngle() :
{ Node::m_twoPi - m_edgeList.first().edge->getAngle();
return Pi - m_edgeList.first().edge->getAngle();
}
else
{
return TwoPi - m_edgeList.first().edge->getAngle();
}
}
QList<double> tmp; QList<double> tmp;
for(QList<EdgeElement>::iterator it = m_edgeList.begin(); for(QList<EdgeElement>::iterator it = m_edgeList.begin();
@ -216,13 +113,13 @@ double Node::calculateBiggestAngle()
{ {
tmp.push_back(it->startsFromThisNode ? tmp.push_back(it->startsFromThisNode ?
it->edge->getAngle() : it->edge->getAngle() :
doubleModulo(Pi + it->edge->getAngle(), TwoPi)); doubleModulo(Node::m_pi + it->edge->getAngle(), Node::m_twoPi));
} }
qSort(tmp.begin(), tmp.end()); qSort(tmp.begin(), tmp.end());
double prev(tmp.first()); double prev(tmp.first());
double max_prev(tmp.last()); 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++) for(QList<double>::const_iterator it = ++tmp.begin(); it!=tmp.end(); it++)
{ {
@ -234,32 +131,7 @@ double Node::calculateBiggestAngle()
prev = *it; prev = *it;
} }
return TwoPi - doubleModulo(max_prev + max / 2, TwoPi); return Node::m_twoPi - doubleModulo(max_prev + max / 2, Node::m_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);
} }
void Node::keyPressEvent(QKeyEvent *event) void Node::keyPressEvent(QKeyEvent *event)
@ -333,3 +205,111 @@ QList<Edge *> Node::edgesFrom() const
return list; 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(); ~Node();
void addEdge(Edge *edge, bool startsFromThisNode); void addEdge(Edge *edge, bool startsFromThisNode);
void deleteEdge(Node *otherEnd);
void removeEdgeFromList(Edge *edge); void removeEdgeFromList(Edge *edge);
void removeEdge(Node *otherEnd);
void setBorder(const bool &hasBorder); void setBorder(const bool &hasBorder);
void setActive(const bool &active = true); void setActive(const bool &active = true);
void setEditable(const bool &editable = true);
void showNumber(const int &number, const bool& show = true, void showNumber(const int &number, const bool& show = true,
const bool &numberIsSpecial = false); const bool &numberIsSpecial = false);
double calculateBiggestAngle(); double calculateBiggestAngle();
void setEditable(const bool &editable = true);
// changing visibility from prot to pub // changing visibility from prot to pub
void keyPressEvent(QKeyEvent *event); void keyPressEvent(QKeyEvent *event);
@ -41,7 +43,6 @@ protected:
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event); void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void linkActivated(const QString &link);
private: private:
@ -60,8 +61,10 @@ private:
int m_number; int m_number;
bool m_hasBorder; bool m_hasBorder;
bool m_numberIsSpecial; bool m_numberIsSpecial;
// QTextCursor m_cursor;
static const double m_pi;
static const double m_oneAndHalfPi;
static const double m_twoPi;
}; };
#endif // NODE_H #endif // NODE_H

Loading…
Cancel
Save