scale command is now undo-able command

master
Denes Matetelki 14 years ago
parent 9195dba565
commit f443cd577e

@ -60,7 +60,8 @@ public:
enum MergeableCommandId enum MergeableCommandId
{ {
MoveCommandId = 0 MoveCommandId = 0,
ScaleCommandId
}; };
BaseUndoClass(UndoContext context); BaseUndoClass(UndoContext context);
@ -193,9 +194,8 @@ public:
void undo(); void undo();
void redo(); void redo();
private: bool mergeWith(const QUndoCommand *command);
int id() const;
QMap<Node*, qreal> m_scaleMap;
}; };

@ -64,7 +64,7 @@ public slots:
signals: signals:
void contentChanged(const bool& changed = true); void contentChanged(const bool& changed = true);
void notification(const QString &msg); void notification(const QString &msg);
private: private:

@ -36,6 +36,10 @@ public slots:
void zoomIn(); void zoomIn();
void zoomOut(); void zoomOut();
signals:
void notification(const QString &msg);
protected: protected:
void keyPressEvent(QKeyEvent *event); void keyPressEvent(QKeyEvent *event);
@ -44,7 +48,7 @@ protected:
private: private:
void scaleView(qreal scaleFactor); void scaleView(qreal factor);
MainWindow *m_parent; MainWindow *m_parent;

@ -37,7 +37,7 @@ HEADERS += include/mainwindow.h \
FORMS += ui/mainwindow.ui FORMS += ui/mainwindow.ui
RESOURCES += images/qtmindmap.qrc RESOURCES += images/qtmindmap.qrc
# the translation hack # the translation hack

@ -6,6 +6,7 @@ TARGET = qtmindmap_test
SOURCES += src/mainwindow.cpp \ SOURCES += src/mainwindow.cpp \
src/graphwidget.cpp \ src/graphwidget.cpp \
src/graphlogic.cpp \
src/node.cpp \ src/node.cpp \
src/edge.cpp \ src/edge.cpp \
src/systemtray.cpp \ src/systemtray.cpp \
@ -14,6 +15,7 @@ SOURCES += src/mainwindow.cpp \
HEADERS += include/mainwindow.h \ HEADERS += include/mainwindow.h \
include/graphwidget.h \ include/graphwidget.h \
include/graphlogic.hpp \
include/node.h \ include/node.h \
include/edge.h \ include/edge.h \
include/systemtray.h \ include/systemtray.h \

@ -367,20 +367,18 @@ void NodeTextColorCommand::redo()
ScaleNodeCommand::ScaleNodeCommand(UndoContext context) ScaleNodeCommand::ScaleNodeCommand(UndoContext context)
: BaseUndoClass(context) : BaseUndoClass(context)
{ {
setText(QObject::tr("Changing scale of node: \"").append( setText(QObject::tr("Node \"").append(
m_context.m_activeNode == m_context.m_nodeList->first() ? m_context.m_activeNode == m_context.m_nodeList->first() ?
QObject::tr("Base node") : QObject::tr("Base node") :
m_context.m_activeNode->toPlainText()).append("\""). m_context.m_activeNode->toPlainText()).
append(m_subtree ? QObject::tr(" with subtree") : QString(""))); append("\" scaled (%1%)").arg(int((1+m_context.m_scale)*100)).
append(m_subtree ? QObject::tr(" with subtree") : QString("")));
foreach(Node *node, m_nodeList)
m_scaleMap[node] = node->scale();
} }
void ScaleNodeCommand::undo() void ScaleNodeCommand::undo()
{ {
foreach(Node *node, m_nodeList) foreach(Node *node, m_nodeList)
node->setScale(m_scaleMap[node], m_context.m_graphLogic->graphWidget()->sceneRect()); node->setScale(qreal(-m_context.m_scale), m_context.m_graphLogic->graphWidget()->sceneRect());
m_context.m_graphLogic->setActiveNode(m_activeNode); m_context.m_graphLogic->setActiveNode(m_activeNode);
} }
@ -392,3 +390,33 @@ void ScaleNodeCommand::redo()
m_context.m_graphLogic->setActiveNode(m_activeNode); m_context.m_graphLogic->setActiveNode(m_activeNode);
} }
bool ScaleNodeCommand::mergeWith(const QUndoCommand *command)
{
if (command->id() != id())
return false;
const ScaleNodeCommand *scaleNodeCommand = static_cast<const ScaleNodeCommand *>(command);
if (m_context.m_activeNode != scaleNodeCommand->m_context.m_activeNode)
return false;
if (m_subtree != scaleNodeCommand->m_subtree)
return false;
m_context.m_scale += scaleNodeCommand->m_context.m_scale;
setText(QObject::tr("Node \"").append(
m_context.m_activeNode == m_context.m_nodeList->first() ?
QObject::tr("Base node") :
m_context.m_activeNode->toPlainText()).
append("\" scaled (%1%)").arg(int((1+m_context.m_scale)*100)).
append(m_subtree ? QObject::tr(" with subtree") : QString("")));
return true;
}
int ScaleNodeCommand::id() const
{
return ScaleCommandId;
}

@ -416,6 +416,12 @@ void GraphLogic::scaleUp()
return; return;
} }
if (m_activeNode->scale()+qreal(0.2) > qreal(4))
{
emit notification(tr("Too much scaling."));
return;
}
bool subtree(QApplication::keyboardModifiers() & Qt::ControlModifier && bool subtree(QApplication::keyboardModifiers() & Qt::ControlModifier &&
QApplication::keyboardModifiers() & Qt::ShiftModifier); QApplication::keyboardModifiers() & Qt::ShiftModifier);
@ -423,23 +429,11 @@ void GraphLogic::scaleUp()
context.m_graphLogic = this; context.m_graphLogic = this;
context.m_nodeList = &m_nodeList; context.m_nodeList = &m_nodeList;
context.m_activeNode = m_activeNode; context.m_activeNode = m_activeNode;
context.m_scale = qreal(1.2); context.m_scale = qreal(0.2);
context.m_subtree = subtree; context.m_subtree = subtree;
QUndoCommand *scaleNodeCommand = new ScaleNodeCommand(context); QUndoCommand *scaleNodeCommand = new ScaleNodeCommand(context);
m_undoStack->push(scaleNodeCommand); m_undoStack->push(scaleNodeCommand);
// if (QApplication::keyboardModifiers() & Qt::ControlModifier &&
// QApplication::keyboardModifiers() & Qt::ShiftModifier)
// {
// QList <Node *> nodeList = m_activeNode->subtree();
// foreach(Node *node, nodeList)
// node->setScale(qreal(1.2), m_graphWidget->sceneRect());
// }
// else
// {
// m_activeNode->setScale(qreal(1.2),m_graphWidget->sceneRect());
// }
} }
void GraphLogic::scaleDown() void GraphLogic::scaleDown()
@ -450,6 +444,12 @@ void GraphLogic::scaleDown()
return; return;
} }
if (m_activeNode->scale()-qreal(0.2) < qreal(0.1))
{
emit notification(tr("Too much scaling."));
return;
}
bool subtree(QApplication::keyboardModifiers() & Qt::ControlModifier && bool subtree(QApplication::keyboardModifiers() & Qt::ControlModifier &&
QApplication::keyboardModifiers() & Qt::ShiftModifier); QApplication::keyboardModifiers() & Qt::ShiftModifier);
@ -457,24 +457,11 @@ void GraphLogic::scaleDown()
context.m_graphLogic = this; context.m_graphLogic = this;
context.m_nodeList = &m_nodeList; context.m_nodeList = &m_nodeList;
context.m_activeNode = m_activeNode; context.m_activeNode = m_activeNode;
context.m_scale = qreal(1 / 1.2); context.m_scale = qreal(-0.2);
context.m_subtree = subtree; context.m_subtree = subtree;
QUndoCommand *scaleNodeCommand = new ScaleNodeCommand(context); QUndoCommand *scaleNodeCommand = new ScaleNodeCommand(context);
m_undoStack->push(scaleNodeCommand); m_undoStack->push(scaleNodeCommand);
// if (QApplication::keyboardModifiers() & Qt::ControlModifier &&
// QApplication::keyboardModifiers() & Qt::ShiftModifier)
// {
// QList <Node *> nodeList = m_activeNode->subtree();
// foreach(Node *node, nodeList)
// node->setScale(qreal(1 / 1.2),m_graphWidget->sceneRect());
// }
// else
// {
// m_activeNode->setScale(qreal(1 / 1.2),m_graphWidget->sceneRect());
// }
} }
void GraphLogic::nodeColor() void GraphLogic::nodeColor()

@ -50,12 +50,12 @@ GraphLogic *GraphWidget::graphLogic() const
void GraphWidget::zoomIn() void GraphWidget::zoomIn()
{ {
scaleView(qreal(1.2)); scaleView(qreal(0.2));
} }
void GraphWidget::zoomOut() void GraphWidget::zoomOut()
{ {
scaleView(qreal(1 / 1.2)); scaleView(qreal(-0.2));
} }
// MainWindow::keyPressEvent passes all keyevent to here, except // MainWindow::keyPressEvent passes all keyevent to here, except
@ -101,14 +101,15 @@ void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
painter->drawRect(m_scene->sceneRect()); painter->drawRect(m_scene->sceneRect());
} }
void GraphWidget::scaleView(qreal scaleFactor) void GraphWidget::scaleView(qreal factor)
{ {
qreal factor = transform().scale(scaleFactor, scaleFactor).
mapRect(QRectF(0, 0, 1, 1)).width();
// don't allow to scale up/down too much // don't allow to scale up/down too much
if (factor < 0.2 || factor > 10) qreal viewScale = transform().m11() + 1 + factor;
if (viewScale < qreal(1) || viewScale > qreal(10))
{
emit notification(tr("Too much zooming."));
return; return;
}
scale(scaleFactor, scaleFactor); scale(1 + factor, 1 + factor);
} }

@ -30,9 +30,13 @@ MainWindow::MainWindow(QWidget *parent) :
connect(m_graphicsView->graphLogic(), SIGNAL(contentChanged(const bool&)), connect(m_graphicsView->graphLogic(), SIGNAL(contentChanged(const bool&)),
this, SLOT(contentChanged(const bool&))); this, SLOT(contentChanged(const bool&)));
connect(m_graphicsView, SIGNAL(notification(QString)),
this, SLOT(statusBarMsg(QString)));
connect(m_graphicsView->graphLogic(), SIGNAL(notification(QString)), connect(m_graphicsView->graphLogic(), SIGNAL(notification(QString)),
this, SLOT(statusBarMsg(QString))); this, SLOT(statusBarMsg(QString)));
// setup toolbars, don't show them // setup toolbars, don't show them
setupMainToolbar(); setupMainToolbar();
m_ui->mainToolBar->hide(); m_ui->mainToolBar->hide();

@ -220,24 +220,19 @@ QColor Node::textColor() const
void Node::setScale(const qreal &factor,const QRectF &sceneRect) void Node::setScale(const qreal &factor,const QRectF &sceneRect)
{ {
// limit scale to a reasonable size
// if (factor * scale() < 0.4 ||
// factor * scale() > 4 )
// return;
// cannot scale out the Node from the scene // cannot scale out the Node from the scene
if (!sceneRect.contains(pos() + if (!sceneRect.contains(pos() +
boundingRect().bottomRight() * scale() * factor)) boundingRect().bottomRight() * scale() * factor))
return; return;
prepareGeometryChange(); prepareGeometryChange();
QGraphicsTextItem::setScale(factor * scale()); QGraphicsTextItem::setScale(factor + scale());
// scale edges to this Node too // scale edges to this Node too
foreach(EdgeElement element, m_edgeList) foreach(EdgeElement element, m_edgeList)
{ {
if (!element.startsFromThisNode) if (!element.startsFromThisNode)
element.edge->setWidth(element.edge->width() * factor ); element.edge->setWidth(element.edge->width() + factor );
element.edge->adjust(); element.edge->adjust();
} }

@ -33,16 +33,17 @@ void AlgorithmTests::calculateBiggestAngle()
{ {
MainWindow *mainWindow = new MainWindow; MainWindow *mainWindow = new MainWindow;
GraphWidget *graphWidget = new GraphWidget(mainWindow); GraphWidget *graphWidget = new GraphWidget(mainWindow);
GraphLogic *graphLogic = new GraphLogic(graphWidget);
// no edges // no edges
Node *node1 = new Node(graphWidget); Node *node1 = new Node(graphLogic);
node1->setPos(0,0); node1->setPos(0,0);
QCOMPARE(node1->calculateBiggestAngle(), Pi * 1.5); QCOMPARE(node1->calculateBiggestAngle(), Pi * 1.5);
// one egde // one egde
// 1 // 1
Node *node2 = new Node(graphWidget); Node *node2 = new Node(graphLogic);
node2->setPos(30,0); node2->setPos(30,0);
Edge *edge1 = new Edge(node1, node2); Edge *edge1 = new Edge(node1, node2);
@ -65,12 +66,12 @@ void AlgorithmTests::calculateBiggestAngle()
node2->setPos(30,0); node2->setPos(30,0);
edge1->m_angle = angleOfPoints(node1->pos(), node2->pos()); edge1->m_angle = angleOfPoints(node1->pos(), node2->pos());
Node *node3 = new Node(graphWidget); Node *node3 = new Node(graphLogic);
node3->setPos(-30,0); node3->setPos(-30,0);
Edge *edge2 = new Edge(node1, node3); Edge *edge2 = new Edge(node1, node3);
edge2->m_angle = angleOfPoints(node1->pos(), node3->pos()); edge2->m_angle = angleOfPoints(node1->pos(), node3->pos());
Node *node4 = new Node(graphWidget); Node *node4 = new Node(graphLogic);
node4->setPos(0, -30); node4->setPos(0, -30);
Edge *edge3 = new Edge(node1, node4); Edge *edge3 = new Edge(node1, node4);
edge3->m_angle = angleOfPoints(node1->pos(), node4->pos()); edge3->m_angle = angleOfPoints(node1->pos(), node4->pos());
@ -85,6 +86,7 @@ void AlgorithmTests::calculateBiggestAngle()
delete node3; delete node3;
delete node4; delete node4;
delete graphLogic;
delete graphWidget; delete graphWidget;
delete mainWindow; delete mainWindow;
} }

Loading…
Cancel
Save