From c5d93d69025f93a5e2fb8ba0f163c06a423dcd5d Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Sat, 2 Jul 2011 20:01:46 +0200 Subject: [PATCH] Refactor: the role of GraphLogic is cleaned up, ready to add undo-command design pattern --- include/graphlogic.h | 59 +++--- include/graphwidget.h | 56 ++---- include/mainwindow.h | 7 +- src/graphlogic.cpp | 408 ++++++++++++++++++++++++++---------------- src/graphwidget.cpp | 275 ++-------------------------- src/mainwindow.cpp | 47 ++--- 6 files changed, 344 insertions(+), 508 deletions(-) diff --git a/include/graphlogic.h b/include/graphlogic.h index 96bdcbd..9863b7c 100644 --- a/include/graphlogic.h +++ b/include/graphlogic.h @@ -17,43 +17,31 @@ public: explicit GraphLogic(GraphWidget *parent = 0); - // functions on nodes + bool processKeyEvent(QKeyEvent *event); void addFirstNode(); void removeAllNodes(); - void setActiveNode(Node *node); bool readContentFromXmlFile(const QString &fileName); void writeContentToXmlFile(const QString &fileName); void writeContentToPngFile(const QString &fileName); - bool editing() const; - void passKey(QKeyEvent *event = 0); - - // undo-able commands: - void insertNode(); - void removeNode(const bool &subtree = false); - void scaleUp(const bool &subtree = false); - void scaleDown(const bool &subtree = false); - void setNodeColor(const QColor &color, const bool &subtree = false); - void setNodeTextColor(const QColor &color, const bool &subtree = false); - void addEdge(); - void removeEdge(); - void hintMode(); - void insertPicture(const QString &picture); - void move(const int &x, const int &y, const bool &subtree = false); - - void appendNumber(const int &unm); - void delNumber(); - void applyNumber(); - - void nodeColor(const bool &subtree = false); - void nodeTextColor(const bool &subtree = false); - public slots: + // commands from toolbars: + void insertNode(); // will be undoable + void removeNode(); // will be undoable + void nodeEdited(); // will be undoable + void scaleUp(); // will be undoable + void scaleDown(); // will be undoable + void nodeColor(); + void nodeTextColor(); + void addEdge(); // will be undoable + void removeEdge(); // will be undoable + void hintMode(); + void insertPicture(const QString &picture); // will be undoable + void nodeChanged(); void nodeSelected(); - void nodeEdited(QKeyEvent *event = 0); void nodeMoved(QGraphicsSceneMouseEvent *event); void nodeLostFocus(); @@ -64,9 +52,23 @@ signals: private: + void moveUp(); + void moveDown(); + void moveLeft(); + void moveRight(); + + void move(const int &x, const int &y); // will be undoable + void setNodeColor(const QColor &color); // will be undoable + void setNodeTextColor(const QColor &color); // will be undoable + + // hint mode + void appendNumber(const int &unm); + void delNumber(); + void applyNumber(); + Node *nodeFactory(); void selectNode(Node *node); - + void setActiveNode(Node *node); // functions on the edges QList allEdges() const; @@ -89,6 +91,9 @@ private: bool m_editingNode; bool m_edgeAdding; bool m_edgeDeleting; + + std::map m_memberMap; + }; #endif // GRAPHLOGIC_H diff --git a/include/graphwidget.h b/include/graphwidget.h index cffab7b..037ac29 100644 --- a/include/graphwidget.h +++ b/include/graphwidget.h @@ -11,79 +11,45 @@ class MainWindow; class GraphLogic; +/** Responsibilities: + * - Handle scene zoom in/out events + * - Close scene (clean), new scene (clean & add first node) + * - Pass key events to GraphLogic + */ class GraphWidget : public QGraphicsView { Q_OBJECT public: + GraphWidget(MainWindow *parent = 0); - // commands from MainWindow void newScene(); void closeScene(); - bool readContentFromXmlFile(const QString &fileName); - void writeContentToXmlFile(const QString &fileName); - void writeContentToPngFile(const QString &fileName); - - static const QColor m_paper; - -public slots: - - // commands from MainWindow's MainToolBar's actions - void insertNode(QKeyEvent *event = 0); - void removeNode(QKeyEvent *event = 0); - void editNode(QKeyEvent *event = 0); - void zoomIn(QKeyEvent *event = 0); - void zoomOut(QKeyEvent *event = 0); - void scaleUp(QKeyEvent *event = 0); - void scaleDown(QKeyEvent *event = 0); - void nodeColor(QKeyEvent *event = 0); - void nodeTextColor(QKeyEvent *event = 0); - void addEdge(QKeyEvent *event = 0); - void removeEdge(QKeyEvent *event = 0); - void nodeLoseFocus(QKeyEvent *event = 0); - void hintMode(QKeyEvent *event = 0); - // bundled signals from statusIconsToolBar - void insertPicture(const QString &picture); + GraphLogic *graphLogic() const; - void contentChangedFromLogic(); - void notificationFromLogic(const QString &msg); + static const QColor m_paperColor; -signals: +public slots: - void contentChanged(); - void notification(const QString &msg); + void zoomIn(); + void zoomOut(); protected: - // key dispathcer of the whole program: long and pedant void keyPressEvent(QKeyEvent *event); void wheelEvent(QWheelEvent *event); void drawBackground(QPainter *painter, const QRectF &rect); private: - // keymap commands - void moveUp(QKeyEvent *event); - void moveDown(QKeyEvent *event); - void moveLeft(QKeyEvent *event); - void moveRight(QKeyEvent *event); - void increment(QKeyEvent *event); - void decrement(QKeyEvent *event); - void appendNumber(QKeyEvent *event); - void delNumber(QKeyEvent *event); - void applyNumber(QKeyEvent *event); - void scaleView(qreal scaleFactor); MainWindow *m_parent; QGraphicsScene *m_scene; GraphLogic *m_graphlogic; - std::map m_memberMap; - - }; #endif // GRAPHWIDGET_H diff --git a/include/mainwindow.h b/include/mainwindow.h index a8f14b2..19a6e4f 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -12,9 +12,10 @@ class MainWindow; } /** Responsibilities: - * - taking care of the menu and toolbars, file operations - * - displaying info in statusbar - * - handle content change + * - Menu and toolbars, file operations + * - Display info in statusbar + * - Handle content change notifications + * - Pass unhandled key events to GraphWidget */ class MainWindow : public QMainWindow { diff --git a/src/graphlogic.cpp b/src/graphlogic.cpp index 37b4e65..728caf6 100644 --- a/src/graphlogic.cpp +++ b/src/graphlogic.cpp @@ -2,6 +2,8 @@ #include #include +#include +#include GraphLogic::GraphLogic(GraphWidget *parent) : QObject(parent) @@ -14,7 +16,84 @@ GraphLogic::GraphLogic(GraphWidget *parent) , m_edgeAdding(false) , m_edgeDeleting(false) { + m_memberMap.insert(std::pair + (Qt::Key_Insert, &GraphLogic::insertNode)); + m_memberMap.insert(std::pair + (Qt::Key_Delete, &GraphLogic::removeNode)); + m_memberMap.insert(std::pair + (Qt::Key_F2, &GraphLogic::nodeEdited)); + + m_memberMap.insert(std::pair + (Qt::Key_C, &GraphLogic::nodeColor)); + m_memberMap.insert(std::pair + (Qt::Key_T, &GraphLogic::nodeTextColor)); + m_memberMap.insert(std::pair + (Qt::Key_A, &GraphLogic::addEdge)); + m_memberMap.insert(std::pair + (Qt::Key_D, &GraphLogic::removeEdge)); + m_memberMap.insert(std::pair + (Qt::Key_F, &GraphLogic::hintMode)); + + m_memberMap.insert(std::pair + (Qt::Key_Up, &GraphLogic::moveUp)); + m_memberMap.insert(std::pair + (Qt::Key_Down, &GraphLogic::moveDown)); + m_memberMap.insert(std::pair + (Qt::Key_Left, &GraphLogic::moveLeft)); + m_memberMap.insert(std::pair + (Qt::Key_Right, &GraphLogic::moveRight)); + + m_memberMap.insert(std::pair + (Qt::Key_Backspace, &GraphLogic::delNumber)); + m_memberMap.insert(std::pair + (Qt::Key_Return, &GraphLogic::applyNumber)); + m_memberMap.insert(std::pair + (Qt::Key_Enter, &GraphLogic::applyNumber)); + m_memberMap.insert(std::pair + (Qt::Key_Delete, &GraphLogic::removeNode)); +} + +bool GraphLogic::processKeyEvent(QKeyEvent *event) +{ + if (event->key() == Qt::Key_Escape) + { + nodeLostFocus(); + return true; + } + + if (m_editingNode) + { + m_activeNode->keyPressEvent(event); + return true; + } + + if (event->key() == Qt::Key_Plus && + event->modifiers() & Qt::ControlModifier) + { + scaleUp(); + return true; + } + if (event->key() == Qt::Key_Minus && + event->modifiers() & Qt::ControlModifier) + { + scaleDown(); + return true; + } + if (m_showingNodeNumbers && + event->key() >= Qt::Key_0 && event->key() <= Qt::Key_9) + { + appendNumber(event->key()-48); + return true; + } + + if (m_memberMap.find(event->key()) != m_memberMap.end()) + { + (this->*m_memberMap[event->key()])(); + return true; + } + + return false; } void GraphLogic::addFirstNode() @@ -37,17 +116,6 @@ void GraphLogic::removeAllNodes() m_hintNode = 0; } -void GraphLogic::setActiveNode(Node *node) -{ - if (m_activeNode!=0) - m_activeNode->setBorder(false); - - m_activeNode = node; - if (m_activeNode) - m_activeNode->setBorder(); -} - - bool GraphLogic::readContentFromXmlFile(const QString &fileName) { // open & parse XML file @@ -200,10 +268,10 @@ void GraphLogic::writeContentToPngFile(const QString &fileName) painter.setRenderHint(QPainter::Antialiasing); // Strange that I have to set this, and scene->render() does not do this - m_graphWidget->scene()->setBackgroundBrush(GraphWidget::m_paper); + m_graphWidget->scene()->setBackgroundBrush(GraphWidget::m_paperColor); m_graphWidget->scene()->render(&painter); - painter.setBackground(GraphWidget::m_paper); + painter.setBackground(GraphWidget::m_paperColor); painter.end(); img.save(fileName); @@ -212,16 +280,6 @@ void GraphLogic::writeContentToPngFile(const QString &fileName) emit notification(tr("MindMap exported as ") + fileName); } -bool GraphLogic::editing() const -{ - return m_editingNode; -} - -void GraphLogic::passKey(QKeyEvent *event) -{ - m_activeNode->keyPressEvent(event); -} - void GraphLogic::insertNode() { nodeLostFocus(); @@ -272,7 +330,7 @@ void GraphLogic::insertNode() showNodeNumbers(); } -void GraphLogic::removeNode(const bool &subtree) +void GraphLogic::removeNode() { if (!m_activeNode) { @@ -288,7 +346,8 @@ void GraphLogic::removeNode(const bool &subtree) // remove just the active Node or it's subtree too? QList nodeList; - if (subtree) + if (QApplication::keyboardModifiers() & Qt::ControlModifier && + QApplication::keyboardModifiers() & Qt::ShiftModifier) { nodeList = m_activeNode->subtree(); } @@ -314,7 +373,7 @@ void GraphLogic::removeNode(const bool &subtree) showNodeNumbers(); } -void GraphLogic::scaleUp(const bool &subtree) +void GraphLogic::nodeEdited() { if (!m_activeNode) { @@ -322,7 +381,21 @@ void GraphLogic::scaleUp(const bool &subtree) return; } - if (subtree) + m_editingNode = true; + m_activeNode->setEditable(); + m_graphWidget->scene()->setFocusItem(m_activeNode); +} + +void GraphLogic::scaleUp() +{ + if (!m_activeNode) + { + emit notification(tr("No active node.")); + return; + } + + if (QApplication::keyboardModifiers() & Qt::ControlModifier && + QApplication::keyboardModifiers() & Qt::ShiftModifier) { QList nodeList = m_activeNode->subtree(); foreach(Node *node, nodeList) @@ -334,7 +407,7 @@ void GraphLogic::scaleUp(const bool &subtree) } } -void GraphLogic::scaleDown(const bool &subtree) +void GraphLogic::scaleDown() { if (!m_activeNode) { @@ -342,7 +415,8 @@ void GraphLogic::scaleDown(const bool &subtree) return; } - if (subtree) + if (QApplication::keyboardModifiers() & Qt::ControlModifier && + QApplication::keyboardModifiers() & Qt::ShiftModifier) { QList nodeList = m_activeNode->subtree(); foreach(Node *node, nodeList) @@ -354,40 +428,40 @@ void GraphLogic::scaleDown(const bool &subtree) } } -void GraphLogic::setNodeColor(const QColor &color, const bool &subtree) +void GraphLogic::nodeColor() { - QList nodeList; - if (subtree) - { - nodeList = m_activeNode->subtree(); - } - else + if (!m_activeNode) { - nodeList.push_back(m_activeNode); + emit notification(tr("No active node.")); + return; } - foreach(Node *node, nodeList) - { - node->setColor(color); - foreach (Edge * edge, node->edgesToThis(false)) - edge->setColor(color); - } + // popup a color selector dialogm def color is the curr one. + QColorDialog dialog(m_graphWidget); + dialog.setWindowTitle(tr("Select node color")); + dialog.setCurrentColor(m_activeNode->color()); + if (!dialog.exec()) + return; + + setNodeColor(dialog.selectedColor()); } -void GraphLogic::setNodeTextColor(const QColor &color, const bool &subtree) +void GraphLogic::nodeTextColor() { - QList nodeList; - if (subtree) - { - nodeList = m_activeNode->subtree(); - } - else + if (!m_activeNode) { - nodeList.push_back(m_activeNode); + emit notification(tr("No active node.")); + return; } - foreach(Node *node, nodeList) - node->setTextColor(color); + // popup a color selector dialogm def color is the curr one. + QColorDialog dialog(m_graphWidget); + dialog.setWindowTitle(tr("Select text color")); + dialog.setCurrentColor(m_activeNode->textColor()); + if (!dialog.exec()) + return; + + setNodeTextColor(dialog.selectedColor()); } void GraphLogic::addEdge() @@ -417,7 +491,6 @@ void GraphLogic::hintMode() showNodeNumbers(); } - void GraphLogic::insertPicture(const QString &picture) { if (!m_activeNode) @@ -429,91 +502,6 @@ void GraphLogic::insertPicture(const QString &picture) m_activeNode->insertPicture(picture); } -void GraphLogic::move(const int &x, const int &y, const bool &subtree) -{ - if (!m_activeNode) - { - emit notification(tr("No active node.")); - return; - } - - if (subtree) - { - QList nodeList = m_activeNode->subtree(); - foreach(Node *node, nodeList) - node->moveBy(x, y); - - emit contentChanged(); - } - else // Move just the active Node. - { - m_activeNode->moveBy(x, y); - emit contentChanged(); - } -} - -void GraphLogic::appendNumber(const int &num) -{ - if (!m_showingNodeNumbers) - return; - - m_hintNumber.append(QString::number(num)); - - showingAllNodeNumbers(false); - showingNodeNumbersBeginWithNumber(m_hintNumber.toInt(), true); -} - -void GraphLogic::delNumber() -{ - if (!m_showingNodeNumbers && m_hintNumber.isEmpty()) - return; - - m_hintNumber.remove(m_hintNumber.length()-1,1); - showNodeNumbers(); -} - -void GraphLogic::applyNumber() -{ - if (m_hintNode && m_showingNodeNumbers) - selectNode(m_hintNode); -} - -void GraphLogic::nodeColor(const bool &subtree) -{ - if (!m_activeNode) - { - emit notification(tr("No active node.")); - return; - } - - // popup a color selector dialogm def color is the curr one. - QColorDialog dialog(m_graphWidget); - dialog.setWindowTitle(tr("Select node color")); - dialog.setCurrentColor(m_activeNode->color()); - if (!dialog.exec()) - return; - - setNodeColor(dialog.selectedColor(), subtree); -} - -void GraphLogic::nodeTextColor(const bool &subtree) -{ - if (!m_activeNode) - { - emit notification(tr("No active node.")); - return; - } - - // popup a color selector dialogm def color is the curr one. - QColorDialog dialog(m_graphWidget); - dialog.setWindowTitle(tr("Select text color")); - dialog.setCurrentColor(m_activeNode->textColor()); - if (!dialog.exec()) - return; - - setNodeTextColor(dialog.selectedColor(), subtree); -} - void GraphLogic::nodeChanged() { emit contentChanged(); @@ -525,20 +513,6 @@ void GraphLogic::nodeSelected() selectNode(dynamic_cast(QObject::sender())); } -void GraphLogic::nodeEdited(QKeyEvent *event) -{ - Q_UNUSED(event) - if (!m_activeNode) - { - emit notification(tr("No active node.")); - return; - } - - m_editingNode = true; - m_activeNode->setEditable(); - m_graphWidget->scene()->setFocusItem(m_activeNode); -} - void GraphLogic::nodeMoved(QGraphicsSceneMouseEvent *event) { // move just the active Node, or it's subtree too? @@ -557,7 +531,6 @@ void GraphLogic::nodeMoved(QGraphicsSceneMouseEvent *event) node->setPos(node->pos() + event->scenePos() - event->lastScenePos()); } - void GraphLogic::nodeLostFocus() { if (m_editingNode) @@ -594,6 +567,123 @@ void GraphLogic::nodeLostFocus() } } +void GraphLogic::moveUp() +{ + QApplication::keyboardModifiers() & Qt::ControlModifier ? + move(0,-20) : + m_graphWidget->verticalScrollBar()->setValue( + m_graphWidget->verticalScrollBar()->value()-20); +} + +void GraphLogic::moveDown() +{ + QApplication::keyboardModifiers() & Qt::ControlModifier ? + move(0,20) : + m_graphWidget->verticalScrollBar()->setValue( + m_graphWidget->verticalScrollBar()->value()+20); +} + +void GraphLogic::moveLeft() +{ + QApplication::keyboardModifiers() & Qt::ControlModifier ? + move(-20,0) : + m_graphWidget->horizontalScrollBar()->setValue( + m_graphWidget->horizontalScrollBar()->value()-20); +} + +void GraphLogic::moveRight() +{ + QApplication::keyboardModifiers() & Qt::ControlModifier ? + move(20,0) : + m_graphWidget->horizontalScrollBar()->setValue( + m_graphWidget->horizontalScrollBar()->value()+20); +} + +void GraphLogic::move(const int &x, const int &y) +{ + if (!m_activeNode) + { + emit notification(tr("No active node.")); + return; + } + + if (QApplication::keyboardModifiers() & Qt::ControlModifier && + QApplication::keyboardModifiers() & Qt::ShiftModifier) + { + QList nodeList = m_activeNode->subtree(); + foreach(Node *node, nodeList) + node->moveBy(x, y); + + emit contentChanged(); + } + else // Move just the active Node. + { + m_activeNode->moveBy(x, y); + emit contentChanged(); + } +} + +void GraphLogic::setNodeColor(const QColor &color) +{ + QList nodeList; + if (QApplication::keyboardModifiers() & Qt::ControlModifier && + QApplication::keyboardModifiers() & Qt::ShiftModifier) + { + nodeList = m_activeNode->subtree(); + } + else + { + nodeList.push_back(m_activeNode); + } + + foreach(Node *node, nodeList) + { + node->setColor(color); + foreach (Edge * edge, node->edgesToThis(false)) + edge->setColor(color); + } +} + +void GraphLogic::setNodeTextColor(const QColor &color) +{ + QList nodeList; + if (QApplication::keyboardModifiers() & Qt::ControlModifier && + QApplication::keyboardModifiers() & Qt::ShiftModifier) + { + nodeList = m_activeNode->subtree(); + } + else + { + nodeList.push_back(m_activeNode); + } + + foreach(Node *node, nodeList) + node->setTextColor(color); +} + +void GraphLogic::appendNumber(const int &num) +{ + m_hintNumber.append(QString::number(num)); + + showingAllNodeNumbers(false); + showingNodeNumbersBeginWithNumber(m_hintNumber.toInt(), true); +} + +void GraphLogic::delNumber() +{ + if (!m_showingNodeNumbers && m_hintNumber.isEmpty()) + return; + + m_hintNumber.remove(m_hintNumber.length()-1,1); + showNodeNumbers(); +} + +void GraphLogic::applyNumber() +{ + if (m_hintNode && m_showingNodeNumbers) + selectNode(m_hintNode); +} + Node * GraphLogic::nodeFactory() { Node *node = new Node(); @@ -633,6 +723,16 @@ void GraphLogic::selectNode(Node *node) } } +void GraphLogic::setActiveNode(Node *node) +{ + if (m_activeNode!=0) + m_activeNode->setBorder(false); + + m_activeNode = node; + if (m_activeNode) + m_activeNode->setBorder(); +} + QList GraphLogic::allEdges() const { QList list; diff --git a/src/graphwidget.cpp b/src/graphwidget.cpp index 815e518..27550d0 100644 --- a/src/graphwidget.cpp +++ b/src/graphwidget.cpp @@ -1,10 +1,6 @@ #include "include/graphwidget.h" #include -#include -#include -#include -#include #include "include/node.h" #include "include/edge.h" @@ -12,7 +8,7 @@ #include -const QColor GraphWidget::m_paper(255,255,153); +const QColor GraphWidget::m_paperColor(255,255,153); GraphWidget::GraphWidget(MainWindow *parent) : QGraphicsView(parent) @@ -29,76 +25,7 @@ GraphWidget::GraphWidget(MainWindow *parent) setTransformationAnchor(AnchorUnderMouse); setMinimumSize(400, 400); - - m_memberMap.insert(std::pair - (Qt::Key_Up, &GraphWidget::moveUp)); - m_memberMap.insert(std::pair - (Qt::Key_Down, &GraphWidget::moveDown)); - m_memberMap.insert(std::pair - (Qt::Key_Left, &GraphWidget::moveLeft)); - m_memberMap.insert(std::pair - (Qt::Key_Right, &GraphWidget::moveRight)); - - m_memberMap.insert(std::pair - (Qt::Key_Plus, &GraphWidget::increment)); - m_memberMap.insert(std::pair - (Qt::Key_Minus, &GraphWidget::decrement)); - - m_memberMap.insert(std::pair - (Qt::Key_F, &GraphWidget::hintMode)); - m_memberMap.insert(std::pair - (Qt::Key_Insert, &GraphWidget::insertNode)); - - m_memberMap.insert(std::pair - (Qt::Key_0, &GraphWidget::appendNumber)); - m_memberMap.insert(std::pair - (Qt::Key_1, &GraphWidget::appendNumber)); - m_memberMap.insert(std::pair - (Qt::Key_2, &GraphWidget::appendNumber)); - m_memberMap.insert(std::pair - (Qt::Key_3, &GraphWidget::appendNumber)); - m_memberMap.insert(std::pair - (Qt::Key_4, &GraphWidget::appendNumber)); - m_memberMap.insert(std::pair - (Qt::Key_5, &GraphWidget::appendNumber)); - m_memberMap.insert(std::pair - (Qt::Key_6, &GraphWidget::appendNumber)); - m_memberMap.insert(std::pair - (Qt::Key_7, &GraphWidget::appendNumber)); - m_memberMap.insert(std::pair - (Qt::Key_8, &GraphWidget::appendNumber)); - m_memberMap.insert(std::pair - (Qt::Key_9, &GraphWidget::appendNumber)); - m_memberMap.insert(std::pair - (Qt::Key_0, &GraphWidget::appendNumber)); - - m_memberMap.insert(std::pair - (Qt::Key_F2, &GraphWidget::editNode)); - - m_memberMap.insert(std::pair - (Qt::Key_Backspace, &GraphWidget::delNumber)); - m_memberMap.insert(std::pair - (Qt::Key_Return, &GraphWidget::applyNumber)); - m_memberMap.insert(std::pair - (Qt::Key_Enter, &GraphWidget::applyNumber)); - m_memberMap.insert(std::pair - (Qt::Key_Delete, &GraphWidget::removeNode)); - - m_memberMap.insert(std::pair - (Qt::Key_A, &GraphWidget::addEdge)); - m_memberMap.insert(std::pair - (Qt::Key_D, &GraphWidget::removeEdge)); - m_memberMap.insert(std::pair - (Qt::Key_C, &GraphWidget::nodeColor)); - m_memberMap.insert(std::pair - (Qt::Key_T, &GraphWidget::nodeTextColor)); - m_graphlogic = new GraphLogic(this); - connect(m_graphlogic, SIGNAL(contentChanged()), - this, SLOT(contentChangedFromLogic())); - - connect(m_graphlogic, SIGNAL(notification(QString)), - this, SLOT(notificationFromLogic(QString))); } void GraphWidget::newScene() @@ -115,156 +42,50 @@ void GraphWidget::closeScene() this->hide(); } -bool GraphWidget::readContentFromXmlFile(const QString &fileName) -{ - return m_graphlogic->readContentFromXmlFile(fileName); -} - -void GraphWidget::writeContentToXmlFile(const QString &fileName) +GraphLogic *GraphWidget::graphLogic() const { - m_graphlogic->writeContentToXmlFile(fileName); + return m_graphlogic; } -void GraphWidget::writeContentToPngFile(const QString &fileName) +void GraphWidget::zoomIn() { - m_graphlogic->writeContentToPngFile(fileName); -} - -void GraphWidget::insertNode(QKeyEvent *event) -{ - Q_UNUSED(event) - m_graphlogic->insertNode(); -} - -void GraphWidget::removeNode(QKeyEvent *event) -{ - m_graphlogic->removeNode( - QApplication::keyboardModifiers() & Qt::ControlModifier && - QApplication::keyboardModifiers() & Qt::ShiftModifier); - Q_UNUSED(event) -} - -void GraphWidget::editNode(QKeyEvent *event) -{ - Q_UNUSED(event) - m_graphlogic->nodeEdited(); -} - -void GraphWidget::zoomIn(QKeyEvent *event) -{ - Q_UNUSED(event) scaleView(qreal(1.2)); } -void GraphWidget::zoomOut(QKeyEvent *event) +void GraphWidget::zoomOut() { - Q_UNUSED(event) scaleView(qreal(1 / 1.2)); } -void GraphWidget::scaleUp(QKeyEvent *event) -{ - Q_UNUSED(event) - m_graphlogic->scaleUp( - QApplication::keyboardModifiers() & Qt::ControlModifier && - QApplication::keyboardModifiers() & Qt::ShiftModifier); -} - -void GraphWidget::scaleDown(QKeyEvent *event) -{ - Q_UNUSED(event) - m_graphlogic->scaleDown( - QApplication::keyboardModifiers() & Qt::ControlModifier && - QApplication::keyboardModifiers() & Qt::ShiftModifier); -} - -void GraphWidget::nodeColor(QKeyEvent *event) -{ - Q_UNUSED(event) - m_graphlogic->nodeColor( - QApplication::keyboardModifiers() & Qt::ControlModifier && - QApplication::keyboardModifiers() & Qt::ShiftModifier); -} - -void GraphWidget::nodeTextColor(QKeyEvent *event) -{ - Q_UNUSED(event) - m_graphlogic->nodeTextColor( - QApplication::keyboardModifiers() & Qt::ControlModifier && - QApplication::keyboardModifiers() & Qt::ShiftModifier); -} - -void GraphWidget::addEdge(QKeyEvent *event) -{ - Q_UNUSED(event) - m_graphlogic->addEdge(); -} - -void GraphWidget::removeEdge(QKeyEvent *event) -{ - Q_UNUSED(event) - m_graphlogic->removeEdge(); -} - -void GraphWidget::nodeLoseFocus(QKeyEvent *event) -{ - Q_UNUSED(event) - m_graphlogic->nodeLostFocus(); -} - -void GraphWidget::hintMode(QKeyEvent *event) -{ - Q_UNUSED(event) - m_graphlogic->hintMode(); -} - - -void GraphWidget::insertPicture(const QString &picture) -{ - Q_UNUSED(picture) - m_graphlogic->insertPicture(picture); -} - -void GraphWidget::contentChangedFromLogic() -{ - emit contentChanged(); -} - -void GraphWidget::notificationFromLogic(const QString &msg) -{ - emit notification(msg); -} - -// All key event arrives here. -// MainWindow::keyPressEvent passes all of them here, except +// MainWindow::keyPressEvent passes all keyevent to here, except // Ctrl + m (show/hide mainToolBar) and Ctrl + i (show/hide statusIconsToolbar) void GraphWidget::keyPressEvent(QKeyEvent *event) { - // Node lost focus: leaving edge adding/deleting or Node editing. - if (event->key() == Qt::Key_Escape) + // if GraphLogic handles the event then stop. + if (m_graphlogic->processKeyEvent(event)) + return; + + if (event->key() == Qt::Key_Plus) { - nodeLoseFocus(); + zoomIn(); return; } - // During Node editing mode, pass every key to the Node's keyPressEvent. - if (m_graphlogic->editing()) + if (event->key() == Qt::Key_Minus) { - m_graphlogic->passKey(event); + zoomOut(); return; } - m_memberMap.find(event->key()) != m_memberMap.end() ? - (this->*m_memberMap[event->key()])(event) : - QGraphicsView::keyPressEvent(event); + QGraphicsView::keyPressEvent(event); } void GraphWidget::wheelEvent(QWheelEvent *event) { event->modifiers() & Qt::ControlModifier ? (event->delta() > 0 ? - scaleUp() : - scaleDown()) : + m_graphlogic->scaleUp() : + m_graphlogic->scaleDown()) : (event->delta() > 0 ? zoomIn() : zoomOut()); @@ -274,71 +95,11 @@ void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect) { Q_UNUSED(rect); - painter->fillRect(m_scene->sceneRect(), GraphWidget::m_paper); + painter->fillRect(m_scene->sceneRect(), GraphWidget::m_paperColor); painter->setBrush(Qt::NoBrush); painter->drawRect(m_scene->sceneRect()); } - -void GraphWidget::moveUp(QKeyEvent *event) -{ - event->modifiers() & Qt::ControlModifier ? - m_graphlogic->move(0,-20, event->modifiers() & Qt::ShiftModifier) : - QGraphicsView::keyPressEvent(event); -} - -void GraphWidget::moveDown(QKeyEvent *event) -{ - event->modifiers() & Qt::ControlModifier ? - m_graphlogic->move(0,20, event->modifiers() & Qt::ShiftModifier) : - QGraphicsView::keyPressEvent(event); -} - -void GraphWidget::moveLeft(QKeyEvent *event) -{ - event->modifiers() & Qt::ControlModifier ? - m_graphlogic->move(-20,0, event->modifiers() & Qt::ShiftModifier) : - QGraphicsView::keyPressEvent(event); -} - -void GraphWidget::moveRight(QKeyEvent *event) -{ - event->modifiers() & Qt::ControlModifier ? - m_graphlogic->move(20,0, event->modifiers() & Qt::ShiftModifier) : - QGraphicsView::keyPressEvent(event); -} - -void GraphWidget::increment(QKeyEvent *event) -{ - event->modifiers() & Qt::ControlModifier ? - scaleUp(event) : - zoomIn(); -} - -void GraphWidget::decrement(QKeyEvent *event) -{ - event->modifiers() & Qt::ControlModifier ? - scaleDown(event) : - zoomOut(); -} - -void GraphWidget::appendNumber(QKeyEvent *event) -{ - m_graphlogic->appendNumber(event->key()-48); -} - -void GraphWidget::delNumber(QKeyEvent *event) -{ - Q_UNUSED(event) - m_graphlogic->delNumber(); -} - -void GraphWidget::applyNumber(QKeyEvent *event) -{ - Q_UNUSED(event) - m_graphlogic->applyNumber(); -} - void GraphWidget::scaleView(qreal scaleFactor) { qreal factor = transform().scale(scaleFactor, scaleFactor). diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index aa8069f..5497d8d 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -27,10 +27,10 @@ MainWindow::MainWindow(QWidget *parent) : setCentralWidget(m_graphicsView); m_graphicsView->hide(); - connect(m_graphicsView, SIGNAL(contentChanged()), + connect(m_graphicsView->graphLogic(), SIGNAL(contentChanged()), this, SLOT(contentChanged())); - connect(m_graphicsView, SIGNAL(notification(QString)), + connect(m_graphicsView->graphLogic(), SIGNAL(notification(QString)), this, SLOT(statusBarMsg(QString))); // setup toolbars, don't show them @@ -120,7 +120,7 @@ void MainWindow::openFile(const QString &fileName) if (!fileInfo.isWritable()) statusBarMsg(tr("Read-only file!")); - if (!m_graphicsView->readContentFromXmlFile(m_fileName)) + if (!m_graphicsView->graphLogic()->readContentFromXmlFile(m_fileName)) { m_fileName = currFilename; return; @@ -151,7 +151,7 @@ void MainWindow::saveFile(const bool &checkIfReadonly) return; } - m_graphicsView->writeContentToXmlFile(m_fileName); + m_graphicsView->graphLogic()->writeContentToXmlFile(m_fileName); contentChanged(false); } @@ -233,7 +233,8 @@ void MainWindow::exportScene() if (dialog.exec()) { - m_graphicsView->writeContentToPngFile(dialog.selectedFiles().first()); + m_graphicsView->graphLogic()->writeContentToPngFile( + dialog.selectedFiles().first()); } } @@ -244,9 +245,11 @@ void MainWindow::about() msgBox.setText(tr("MindMap software written in Qt.")); msgBox.setTextFormat(Qt::RichText); msgBox.setInformativeText(tr("Homepage:"). - append(" https://gitorious.org/qtmindmap

"). - append(tr("Report bugs to:")). - append(" denes.matetelki@gmail.com")); + append(" " \ + "https://gitorious.org/qtmindmap

"). + append(tr("Report bugs to:")). + append(" " \ + "denes.matetelki@gmail.com")); QPixmap pixMap(":/qtmindmap.svg"); msgBox.setIconPixmap(pixMap.scaled(50,50)); msgBox.exec(); @@ -308,38 +311,38 @@ void MainWindow::setUpMainToolbar() /// @bug or a feature? no underline here m_addNode = new QAction(tr("Add node (ins)"), this); - connect(m_addNode, SIGNAL(activated()), m_graphicsView, + connect(m_addNode, SIGNAL(activated()), m_graphicsView->graphLogic(), SLOT(insertNode())); m_delNode = new QAction(tr("Del node (del)"), this); - connect(m_delNode, SIGNAL(activated()), m_graphicsView, + connect(m_delNode, SIGNAL(activated()), m_graphicsView->graphLogic(), SLOT(removeNode())); m_editNode = new QAction(tr("Edit node (F2, dubclick)"), this); - connect(m_editNode, SIGNAL(activated()), m_graphicsView, - SLOT(editNode())); + connect(m_editNode, SIGNAL(activated()), m_graphicsView->graphLogic(), + SLOT(nodeEdited())); m_scaleUpNode = new QAction(tr("ScaleUp Node (Ctrl +)"), this); - connect(m_scaleUpNode, SIGNAL(activated()), m_graphicsView, + connect(m_scaleUpNode, SIGNAL(activated()), m_graphicsView->graphLogic(), SLOT(scaleUp())); m_scaleDownNode = new QAction(tr("ScaleDown Node (Ctrl -)"), this); - connect(m_scaleDownNode, SIGNAL(activated()), m_graphicsView, + connect(m_scaleDownNode, SIGNAL(activated()), m_graphicsView->graphLogic(), SLOT(scaleDown())); m_nodeColor = new QAction(tr("Node color (c)"), this); - connect(m_nodeColor, SIGNAL(activated()), m_graphicsView, + connect(m_nodeColor, SIGNAL(activated()), m_graphicsView->graphLogic(), SLOT(nodeColor())); m_nodeTextColor = new QAction(tr("Node textcolor (t)"), this); - connect(m_nodeTextColor, SIGNAL(activated()), m_graphicsView, + connect(m_nodeTextColor, SIGNAL(activated()), m_graphicsView->graphLogic(), SLOT(nodeTextColor())); m_addEdge = new QAction(tr("Add edge (a)"), this); - connect(m_addEdge, SIGNAL(activated()), m_graphicsView, + connect(m_addEdge, SIGNAL(activated()), m_graphicsView->graphLogic(), SLOT(addEdge())); m_delEdge = new QAction(tr("Del edge (d)"), this); - connect(m_delEdge, SIGNAL(activated()), m_graphicsView, + connect(m_delEdge, SIGNAL(activated()), m_graphicsView->graphLogic(), SLOT(removeEdge())); m_moveNode = new QAction(tr("Move node\n(Ctrl cursor, drag)"), this); @@ -357,11 +360,11 @@ void MainWindow::setUpMainToolbar() SLOT(zoomOut())); m_esc = new QAction(tr("Leave editing,\nedge eadd/remove (esc)"), this); - connect(m_esc, SIGNAL(activated()), m_graphicsView, - SLOT(nodeLoseFocus())); + connect(m_esc, SIGNAL(activated()), m_graphicsView->graphLogic(), + SLOT(nodeLostFocus())); m_hintMode = new QAction(tr("Hint mode (f)"), this); - connect(m_hintMode, SIGNAL(activated()), m_graphicsView, + connect(m_hintMode, SIGNAL(activated()), m_graphicsView->graphLogic(), SLOT(hintMode())); m_showMainToolbar = new QAction(tr("Show main toolbar\n(Ctrl m)"), this); @@ -448,7 +451,7 @@ void MainWindow::setUpStatusIconToolbar() m_signalMapper->setMapping(m_maybe, ":/dialog-information.svg"); connect(m_signalMapper, SIGNAL(mapped(const QString &)), - m_graphicsView, SLOT(insertPicture(const QString &))); + m_graphicsView->graphLogic(), SLOT(insertPicture(const QString &))); m_ui->statusIcons_toolBar->addAction(m_insertIcon); m_ui->statusIcons_toolBar->addAction(m_doIt);