doxyfile added. Notifications to parent objects done with signals, not with callbacks. Node is created with factory method in GraphWidget.

master
Denes Matetelki 14 years ago
parent 4abf53400f
commit c9fb8b7c85

1
.gitignore vendored

@ -6,3 +6,4 @@ moc_*
qrc_*
qtmindmap
*.pro.user
html/

@ -17,13 +17,6 @@ class GraphWidget : public QGraphicsView
public:
GraphWidget(MainWindow *parent = 0);
// node reports back it's state change
void nodeSelected(Node *node);
void nodeMoved(QGraphicsSceneMouseEvent *event);
// notify MainWindow: a node/edge has changed
void contentChanged(const bool &changed = true);
// commands from MainWindow
void newScene();
void closeScene();
@ -36,7 +29,6 @@ public slots:
// commands from MainWindow's MainToolBar's actions
void insertNode();
void removeNode();
void editNode();
void zoomIn();
void zoomOut();
void scaleUp();
@ -45,12 +37,23 @@ public slots:
void nodeTextColor();
void addEdge();
void removeEdge();
void nodeLostFocus();
void hintMode();
// bundled signals from statusIconsToolBar
void insertPicture(const QString &picture);
// node reports back it's state change
void nodeChanged();
void nodeSelected();
void nodeEdited();
void nodeMoved(QGraphicsSceneMouseEvent *event);
void nodeLostFocus();
signals:
void contentChanged();
void notification(const QString &msg);
protected:
// key dispathcer of the whole program: long and pedant
@ -60,6 +63,10 @@ protected:
private:
Node * nodeFactory();
void selectNode(Node *node);
// zoom in/out of the view
void scaleView(qreal scaleFactor);

@ -20,14 +20,14 @@ public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
// instead of givin access to private m_ui
void statusBarMsg(const QString &msg);
// indicate that content has changed, modify title, save actions
void contentChanged(const bool &changed = true);
public slots:
// filemenu actions
void newFile();
void openFile(const QString &fileName = "");

@ -12,6 +12,8 @@ class GraphWidget;
class Node : public QGraphicsTextItem
{
Q_OBJECT
public:
Node(GraphWidget *graphWidget = 0);
@ -54,6 +56,17 @@ public:
// returns with the biggest angle between the edges
double calculateBiggestAngle() const;
static const QPointF newNodeCenter;
static const QPointF newNodeBottomRigth;
signals:
void nodeChanged();
void nodeSelected();
void nodeEdited();
void nodeMoved(QGraphicsSceneMouseEvent *event);
void nodeLostFocus();
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,

File diff suppressed because it is too large Load Diff

@ -40,26 +40,10 @@ GraphWidget::GraphWidget(MainWindow *parent)
}
void GraphWidget::nodeSelected(Node *node)
void GraphWidget::nodeSelected()
{
// leave hint mode
showingAllNodeNumbers(false);
m_showingNodeNumbers = false;
if (m_edgeAdding)
{
addEdge(m_activeNode, node);
m_edgeAdding = false;
}
else if (m_edgeDeleting)
{
removeEdge(m_activeNode, node);
m_edgeDeleting = false;
}
else
{
setActiveNode(node);
}
// if node == 0 then nodeSelected invoked after a signal from a Node
selectNode(dynamic_cast<Node*>(QObject::sender()));
}
void GraphWidget::nodeMoved(QGraphicsSceneMouseEvent *event)
@ -80,9 +64,9 @@ void GraphWidget::nodeMoved(QGraphicsSceneMouseEvent *event)
node->setPos(node->pos() + event->scenePos() - event->lastScenePos());
}
void GraphWidget::contentChanged(const bool &changed)
void GraphWidget::nodeChanged()
{
m_parent->contentChanged(changed);
emit contentChanged();
}
void GraphWidget::newScene()
@ -105,13 +89,13 @@ bool GraphWidget::readContentFromXmlFile(const QString &fileName)
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
{
m_parent->statusBarMsg(tr("Couldn't read file."));
emit notification(tr("Couldn't read file."));
return false;
}
if (!doc.setContent(&file))
{
m_parent->statusBarMsg(tr("Couldn't parse XML file."));
emit notification(tr("Couldn't parse XML file."));
file.close();
return false;
}
@ -126,9 +110,8 @@ bool GraphWidget::readContentFromXmlFile(const QString &fileName)
QDomElement e = nodes.item(i).toElement();
if(!e.isNull())
{
Node *node = new Node(this);
Node *node = nodeFactory();
node->setHtml(e.attribute("htmlContent"));
m_scene->addItem(node);
node->setPos(e.attribute("x").toFloat(),
e.attribute("y").toFloat());
node->setScale(e.attribute("scale").toFloat(),sceneRect());
@ -138,8 +121,6 @@ bool GraphWidget::readContentFromXmlFile(const QString &fileName)
node->setTextColor(QColor(e.attribute("text_red").toFloat(),
e.attribute("text_green").toFloat(),
e.attribute("text_blue").toFloat()));
m_nodeList.append(node);
}
}
@ -227,7 +208,7 @@ void GraphWidget::writeContentToXmlFile(const QString &fileName)
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
m_parent->statusBarMsg(tr("Couldn't open file to write."));
emit notification(tr("Couldn't open file to write."));
return;
}
QTextStream ts( &file );
@ -235,7 +216,7 @@ void GraphWidget::writeContentToXmlFile(const QString &fileName)
file.close();
// show a statusBar message to the user
m_parent->statusBarMsg(tr("Saved."));
emit notification(tr("Saved."));
}
void GraphWidget::writeContentToPngFile(const QString &fileName)
@ -257,7 +238,7 @@ void GraphWidget::writeContentToPngFile(const QString &fileName)
img.save(fileName);
// show a statusBar message to the user
m_parent->statusBarMsg(tr("MindMap exported as ") + fileName);
emit notification(tr("MindMap exported as ") + fileName);
}
void GraphWidget::insertNode()
@ -266,7 +247,7 @@ void GraphWidget::insertNode()
if (!m_activeNode)
{
m_parent->statusBarMsg(tr("No active node."));
emit notification(tr("No active node."));
return;
}
@ -278,36 +259,32 @@ void GraphWidget::insertNode()
QPointF pos(length * cos(angle), length * sin(angle));
// add a new node which inherits the color and textColor
Node *node = new Node(this);
node->setColor(m_activeNode->color());
node->setTextColor(m_activeNode->textColor());
node->setHtml(QString(""));
m_scene->addItem(node);
QPointF newPos(m_activeNode->sceneBoundingRect().center() +
pos -
node->boundingRect().center());
pos - Node::newNodeCenter);
QRectF rect (scene()->sceneRect().topLeft(),
scene()->sceneRect().bottomRight() -
node->boundingRect().bottomRight());
scene()->sceneRect().bottomRight()
- Node::newNodeBottomRigth);
if (!rect.contains(newPos))
{
delete node;
m_parent->statusBarMsg(tr("New node would be placed outside of the scene"));
emit notification(tr("New node would be placed outside of the scene"));
return;
}
// add a new node which inherits the color and textColor
Node *node = nodeFactory();
node->setColor(m_activeNode->color());
node->setTextColor(m_activeNode->textColor());
node->setHtml(QString(""));
node->setPos(newPos);
m_nodeList.append(node);
addEdge(m_activeNode, node);
// set it the active Node and editable, so the user can edit it at once
setActiveNode(node);
editNode();
nodeEdited();
contentChanged();
emit contentChanged();
// it we are in hint mode, the numbers shall be re-calculated
if (m_showingNodeNumbers)
@ -318,13 +295,13 @@ void GraphWidget::removeNode()
{
if (!m_activeNode)
{
m_parent->statusBarMsg(tr("No active node."));
emit notification(tr("No active node."));
return;
}
if (m_activeNode == m_nodeList.first())
{
m_parent->statusBarMsg(tr("Base node cannot be deleted."));
emit notification(tr("Base node cannot be deleted."));
return;
}
@ -350,18 +327,18 @@ void GraphWidget::removeNode()
}
m_activeNode = 0;
contentChanged();
emit contentChanged();
// it we are in hint mode, the numbers shall be re-calculated
if (m_showingNodeNumbers)
showNodeNumbers();
}
void GraphWidget::editNode()
void GraphWidget::nodeEdited()
{
if (!m_activeNode)
{
m_parent->statusBarMsg(tr("No active node."));
emit notification(tr("No active node."));
return;
}
@ -384,7 +361,7 @@ void GraphWidget::scaleUp()
{
if (!m_activeNode)
{
m_parent->statusBarMsg(tr("No active node."));
emit notification(tr("No active node."));
return;
}
@ -405,7 +382,7 @@ void GraphWidget::scaleDown()
{
if (!m_activeNode)
{
m_parent->statusBarMsg(tr("No active node."));
emit notification(tr("No active node."));
return;
}
@ -426,7 +403,7 @@ void GraphWidget::nodeColor()
{
if (!m_activeNode)
{
m_parent->statusBarMsg(tr("No active node."));
emit notification(tr("No active node."));
return;
}
@ -462,7 +439,7 @@ void GraphWidget::nodeTextColor()
{
if (!m_activeNode)
{
m_parent->statusBarMsg(tr("No active node."));
emit notification(tr("No active node."));
return;
}
@ -492,13 +469,13 @@ void GraphWidget::nodeTextColor()
void GraphWidget::addEdge()
{
m_parent->statusBarMsg(tr("Add edge: select destination node."));
emit notification(tr("Add edge: select destination node."));
m_edgeAdding = true;
}
void GraphWidget::removeEdge()
{
m_parent->statusBarMsg(tr("Delete edge: select other end-node."));
emit notification(tr("Delete edge: select other end-node."));
m_edgeDeleting = true;
}
@ -518,14 +495,14 @@ void GraphWidget::nodeLostFocus()
if (m_edgeAdding)
{
m_edgeAdding = false;
m_parent->statusBarMsg(tr("Edge adding cancelled."));
emit notification(tr("Edge adding cancelled."));
return;
}
if (m_edgeDeleting)
{
m_edgeDeleting = false;
m_parent->statusBarMsg(tr("Edge deleting cancelled."));
emit notification(tr("Edge deleting cancelled."));
return;
}
@ -558,7 +535,7 @@ void GraphWidget::insertPicture(const QString &picture)
{
if (!m_activeNode)
{
m_parent->statusBarMsg(tr("No active node."));
emit notification(tr("No active node."));
return;
}
@ -595,7 +572,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
if (!m_activeNode)
{
m_parent->statusBarMsg(tr("No active node."));
emit notification(tr("No active node."));
return;
}
@ -612,7 +589,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
else if (event->key() == Qt::Key_Left) node->moveBy(-20, 0);
else if (event->key() == Qt::Key_Right) node->moveBy(20, 0);
contentChanged();
emit contentChanged();
}
}
else // Move just the active Node.
@ -626,7 +603,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
else if (event->key() == Qt::Key_Right)
m_activeNode->moveBy(20, 0);
contentChanged();
emit contentChanged();
}
}
else // Move scene.
@ -695,13 +672,13 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
case Qt::Key_Enter:
if (m_hintNode && m_showingNodeNumbers)
nodeSelected(m_hintNode);
selectNode(m_hintNode);
break;
case Qt::Key_F2:
editNode();
nodeEdited();
break;
case Qt::Key_Delete:
@ -754,6 +731,45 @@ void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
painter->drawRect(m_scene->sceneRect());
}
Node * GraphWidget::nodeFactory()
{
Node *node = new Node(this);
connect(node, SIGNAL(nodeChanged()), this, SLOT(nodeChanged()));
connect(node, SIGNAL(nodeSelected()), this, SLOT(nodeSelected()));
connect(node, SIGNAL(nodeEdited()), this, SLOT(nodeEdited()));
connect(node, SIGNAL(nodeMoved(QGraphicsSceneMouseEvent*)),
this, SLOT(nodeMoved(QGraphicsSceneMouseEvent*)));
connect(node, SIGNAL(nodeLostFocus()), this, SLOT(nodeLostFocus()));
m_scene->addItem(node);
m_nodeList.append(node);
return node;
}
void GraphWidget::selectNode(Node *node)
{
// leave hint mode
showingAllNodeNumbers(false);
m_showingNodeNumbers = false;
if (m_edgeAdding)
{
addEdge(m_activeNode, node);
m_edgeAdding = false;
}
else if (m_edgeDeleting)
{
removeEdge(m_activeNode, node);
m_edgeDeleting = false;
}
else
{
setActiveNode(node);
}
}
void GraphWidget::scaleView(qreal scaleFactor)
{
qreal factor = transform().scale(scaleFactor, scaleFactor).
@ -786,20 +802,20 @@ void GraphWidget::addEdge(Node *source, Node *destination)
{
if (!m_activeNode)
{
m_parent->statusBarMsg(tr("No active node."));
emit notification(tr("No active node."));
return;
}
if (destination == m_nodeList.first())
{
m_parent->statusBarMsg(
emit notification(
tr("Root element cannot be an edge target."));
return;
}
if (source->isConnected(destination))
{
m_parent->statusBarMsg(
emit notification(
tr("There is already an edge between these two nodes."));
}
else
@ -808,7 +824,7 @@ void GraphWidget::addEdge(Node *source, Node *destination)
bool sec(false);
if (!destination->edgesToThis().empty())
{
m_parent->statusBarMsg(
emit notification(
tr("The graph is acyclic, edge added as secondary edge."));
sec = true;
}
@ -821,7 +837,7 @@ void GraphWidget::addEdge(Node *source, Node *destination)
edge->setSecondary(sec);
m_scene->addItem(edge);
contentChanged();
emit contentChanged();
}
}
@ -829,29 +845,26 @@ void GraphWidget::removeEdge(Node *source, Node *destination)
{
if (!m_activeNode)
{
m_parent->statusBarMsg(tr("No active node."));
emit notification(tr("No active node."));
return;
}
if (!source->isConnected(destination))
{
m_parent->statusBarMsg(tr("There no edge between these two nodes."));
emit notification(tr("There no edge between these two nodes."));
}
else
{
source->deleteEdge(destination);
contentChanged();
emit contentChanged();
}
}
void GraphWidget::addFirstNode()
{
Node *node = new Node(this);
Node *node = nodeFactory();
node->setHtml(
QString("<img src=:/qtmindmap.svg width=50 height=50></img>"));
m_scene->addItem(node);
m_nodeList.append(node);
m_activeNode = m_nodeList.first();
m_activeNode->setBorder();
@ -931,7 +944,7 @@ void GraphWidget::showingNodeNumbersBeginWithNumber(const int &prefix,
}
if (hit==1)
{
nodeSelected(m_hintNode);
selectNode(m_hintNode);
}
else if (hit == 0)
{

@ -22,12 +22,17 @@ MainWindow::MainWindow(QWidget *parent) :
connect(m_ui->actionAbout_QtMindMap, SIGNAL(activated()),
this, SLOT(about()));
// graphwidget is hided by def, new/open file will show it
m_graphicsView = new GraphWidget(this);
setCentralWidget(m_graphicsView);
m_graphicsView->hide();
connect(m_graphicsView, SIGNAL(contentChanged()),
this, SLOT(contentChanged()));
connect(m_graphicsView, SIGNAL(notification(QString)),
this, SLOT(statusBarMsg(QString)));
// setup toolbars, don't show them
setUpMainToolbar();
m_ui->mainToolBar->hide();
@ -309,7 +314,7 @@ void MainWindow::setUpMainToolbar()
m_editNode = new QAction(tr("Edit node (F2, dubclick)"), this);
connect(m_editNode, SIGNAL(activated()), m_graphicsView,
SLOT(editNode()));
SLOT(nodeEdited()));
m_scaleUpNode = new QAction(tr("ScaleUp Node (Ctrl +)"), this);
connect(m_scaleUpNode, SIGNAL(activated()), m_graphicsView,

@ -6,6 +6,9 @@
#include <QGraphicsSceneMouseEvent>
#include <QTextDocument>
const QPointF Node::newNodeCenter = QPointF(4, 11.5);
const QPointF Node::newNodeBottomRigth = QPointF(8, 23);
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;
@ -233,14 +236,14 @@ void Node::insertPicture(const QString &picture)
c.insertHtml(QString("<img src=").append(picture).
append(" width=15 height=15></img>"));
m_graph->contentChanged();
foreach (EdgeElement element, m_edgeList) element.edge->adjust();
emit nodeChanged();
}
QPointF Node::intersection(const QLineF &line, const bool &reverse) const
{
/// @note What a shame, the following does not work,
/// @note What a pity, the following does not work,
/// doing it with brute (unaccurate) force
// QPainterPath nodeShape(shape());
@ -358,8 +361,9 @@ void Node::keyPressEvent(QKeyEvent *event)
// not cursor movement: editing
QGraphicsTextItem::keyPressEvent(event);
m_graph->contentChanged();
foreach (EdgeElement element, m_edgeList) element.edge->adjust();
emit nodeChanged();
}
///@note leaving editing mode is done with esc, handled by graphwidget
@ -432,8 +436,8 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
case ItemPositionHasChanged:
// Notify parent, adjust edges that a move has happended.
m_graph->contentChanged();
foreach (EdgeElement element, m_edgeList) element.edge->adjust();
emit nodeChanged();
break;
default:
@ -445,7 +449,7 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
m_graph->nodeSelected(this);
emit nodeSelected();
QGraphicsItem::mousePressEvent(event);
}
@ -453,7 +457,8 @@ void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
void Node::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
Q_UNUSED(event);
m_graph->editNode();
emit nodeEdited();
}
void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
@ -464,7 +469,7 @@ void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
// notify parent so subtree can be moved too if necessary
void Node::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
m_graph->nodeMoved(event);
emit nodeMoved(event);
}
QPainterPath Node::shape () const
@ -479,7 +484,8 @@ void Node::focusOutEvent(QFocusEvent *event)
{
Q_UNUSED(event);
setEditable(false);
m_graph->nodeLostFocus();
emit nodeLostFocus();
}
// there is no such thing as modulo operator for double :P

Loading…
Cancel
Save