From 0783cc24e1e9a7eb923c55a217f7fea4f7054ca8 Mon Sep 17 00:00:00 2001 From: dismine Date: Mon, 9 Jun 2014 16:13:15 +0300 Subject: [PATCH] New undo command MoveSPoint. --HG-- branch : feature --- src/app/mainwindow.cpp | 22 +++-- src/app/mainwindow.h | 2 +- src/app/tools/drawTools/vdrawtool.cpp | 1 - src/app/tools/drawTools/vtoolpoint.cpp | 1 + src/app/tools/drawTools/vtoolsinglepoint.cpp | 28 +++--- src/app/xml/vpattern.cpp | 5 +- src/app/xml/vpattern.h | 15 ---- src/app/xml/vundocommands.cpp | 89 ++++++++++++++++++-- src/app/xml/vundocommands.h | 56 +++++++++++- 9 files changed, 170 insertions(+), 49 deletions(-) diff --git a/src/app/mainwindow.cpp b/src/app/mainwindow.cpp index 66ef96266..606e1e886 100644 --- a/src/app/mainwindow.cpp +++ b/src/app/mainwindow.cpp @@ -93,10 +93,11 @@ MainWindow::MainWindow(QWidget *parent) pattern = new VContainer(); doc = new VPattern(pattern, &mode, sceneDraw, sceneDetails); - connect(doc, &VPattern::patternChanged, this, &MainWindow::PatternWasModified); connect(doc, &VPattern::ClearMainWindow, this, &MainWindow::Clear); connect(doc, &VPattern::UndoCommand, this, &MainWindow::FullParseFile); + connect(qApp->getUndoStack(), &QUndoStack::cleanChanged, this, &MainWindow::PatternWasModified); + InitAutoSave(); ui->toolBox->setCurrentIndex(0); @@ -1319,7 +1320,6 @@ void MainWindow::Clear() setCurrentFile(""); pattern->Clear(); doc->clear(); - doc->setPatternModified(false); sceneDraw->clear(); sceneDetails->clear(); CancelTool(); @@ -1429,7 +1429,7 @@ void MainWindow::FullParseFile() */ void MainWindow::NewPattern() { - if (doc->isPatternModified() || curFile.isEmpty() == false) + if (this->isWindowModified() || curFile.isEmpty() == false) { QProcess *v = new QProcess(this); v->startDetached(QCoreApplication::applicationFilePath ()); @@ -1441,10 +1441,10 @@ void MainWindow::NewPattern() /** * @brief haveChange enable action save if we have unsaved change. */ -void MainWindow::PatternWasModified() +void MainWindow::PatternWasModified(bool saved) { - setWindowModified(doc->isPatternModified()); - ui->actionSave->setEnabled(true); + setWindowModified(!saved); + ui->actionSave->setEnabled(!saved); } //--------------------------------------------------------------------------------------------------------------------- @@ -1657,7 +1657,7 @@ bool MainWindow::SavePattern(const QString &fileName) */ void MainWindow::AutoSavePattern() { - if (curFile.isEmpty() == false && doc->isPatternModified() == true) + if (curFile.isEmpty() == false && this->isWindowModified() == true) { QString autofile = curFile +".autosave"; if (SavePattern(autofile) == false) @@ -1676,7 +1676,6 @@ void MainWindow::AutoSavePattern() void MainWindow::setCurrentFile(const QString &fileName) { curFile = fileName; - doc->setPatternModified(false); setWindowModified(false); QString shownName = strippedName(curFile); @@ -1761,7 +1760,7 @@ void MainWindow::WriteSettings() */ bool MainWindow::MaybeSave() { - if (doc->isPatternModified()) + if (this->isWindowModified()) { QMessageBox::StandardButton ret; ret = QMessageBox::warning(this, tr("Unsaved change"), tr("The pattern has been modified.\n" @@ -1994,13 +1993,12 @@ void MainWindow::LoadPattern(const QString &fileName) FullParseFile(); - bool patternModified = doc->isPatternModified(); + bool patternModified = this->isWindowModified(); setCurrentFile(fileName); if (patternModified) { //For situation where was fixed wrong formula need return for document status was modified. - doc->setPatternModified(patternModified); - PatternWasModified(); + PatternWasModified(patternModified); } helpLabel->setText(tr("File loaded")); } diff --git a/src/app/mainwindow.h b/src/app/mainwindow.h index 2b80248b4..09a548a0b 100644 --- a/src/app/mainwindow.h +++ b/src/app/mainwindow.h @@ -80,7 +80,7 @@ public slots: void ChangedSize(const QString &text); void ChangedHeight(const QString & text); - void PatternWasModified(); + void PatternWasModified(bool saved); void ToolEndLine(bool checked); void ToolLine(bool checked); diff --git a/src/app/tools/drawTools/vdrawtool.cpp b/src/app/tools/drawTools/vdrawtool.cpp index 265e15c90..253384ed9 100644 --- a/src/app/tools/drawTools/vdrawtool.cpp +++ b/src/app/tools/drawTools/vdrawtool.cpp @@ -191,7 +191,6 @@ qreal VDrawTool::CheckFormula(QString &formula, VContainer *data) void VDrawTool::AddToCalculation(const QDomElement &domElement) { AddToCal *addToCal = new AddToCal(domElement, doc); - connect(addToCal, &AddToCal::UnsavedChange, doc, &VPattern::haveLiteChange); connect(addToCal, &AddToCal::NeedFullParsing, doc, &VPattern::NeedFullParsing); qApp->getUndoStack()->push(addToCal); } diff --git a/src/app/tools/drawTools/vtoolpoint.cpp b/src/app/tools/drawTools/vtoolpoint.cpp index e054b5c61..94aa6353a 100644 --- a/src/app/tools/drawTools/vtoolpoint.cpp +++ b/src/app/tools/drawTools/vtoolpoint.cpp @@ -169,6 +169,7 @@ void VToolPoint::RefreshPointGeometry(const VPointF &point) rec.translate(-rec.center().x(), -rec.center().y()); this->setRect(rec); this->setPos(point.toQPointF()); + this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); disconnect(namePoint, &VGraphicsSimpleTextItem::NameChangePosition, this, &VToolPoint::NameChangePosition); QFont font = namePoint->font(); font.setPointSize(static_cast(namePoint->FontSize()/factor)); diff --git a/src/app/tools/drawTools/vtoolsinglepoint.cpp b/src/app/tools/drawTools/vtoolsinglepoint.cpp index c8ea5cfbe..59c870614 100644 --- a/src/app/tools/drawTools/vtoolsinglepoint.cpp +++ b/src/app/tools/drawTools/vtoolsinglepoint.cpp @@ -134,21 +134,27 @@ QVariant VToolSinglePoint::itemChange(QGraphicsItem::GraphicsItemChange change, } if (change == ItemPositionHasChanged && scene()) { + this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, false); // value - this is new position. QPointF newPos = value.toPointF(); - QDomElement domElement = doc->elementById(QString().setNum(id)); - if (domElement.isElement()) - { - doc->SetAttribute(domElement, AttrX, QString().setNum(qApp->fromPixel(newPos.x()))); - doc->SetAttribute(domElement, AttrY, QString().setNum(qApp->fromPixel(newPos.y()))); - QList list = this->scene()->views(); - VAbstractTool::NewSceneRect(this->scene(), list[0]); + MoveSPoint *moveSP = new MoveSPoint(doc, newPos.x(), newPos.y(), id, this->scene()); + connect(moveSP, &MoveSPoint::NeedLiteParsing, doc, &VPattern::LiteParseTree); + qApp->getUndoStack()->push(moveSP); - //I don't now why but signal does not work. - doc->LiteParseTree(); - emit toolhaveChange(); - } +// QDomElement domElement = doc->elementById(QString().setNum(id)); +// if (domElement.isElement()) +// { +// doc->SetAttribute(domElement, AttrX, QString().setNum(qApp->fromPixel(newPos.x()))); +// doc->SetAttribute(domElement, AttrY, QString().setNum(qApp->fromPixel(newPos.y()))); + +// QList list = this->scene()->views(); +// VAbstractTool::NewSceneRect(this->scene(), list[0]); + +// //I don't now why but signal does not work. +// doc->LiteParseTree(); +// emit toolhaveChange(); +// } } return QGraphicsItem::itemChange(change, value); } diff --git a/src/app/xml/vpattern.cpp b/src/app/xml/vpattern.cpp index cbecbd9ac..803f1109f 100644 --- a/src/app/xml/vpattern.cpp +++ b/src/app/xml/vpattern.cpp @@ -73,8 +73,8 @@ const QString VPattern::IncrementDescription = QStringLiteral("description"); VPattern::VPattern(VContainer *data, Valentina::Draws *mode, VMainGraphicsScene *sceneDraw, VMainGraphicsScene *sceneDetail, QObject *parent) : QObject(parent), VDomDocument(data), nameActivDraw(QString()), tools(QHash()), - history(QVector()), cursor(0), patternPieces(QStringList()), mode(mode), patternModified(false), - sceneDraw(sceneDraw), sceneDetail(sceneDetail) + history(QVector()), cursor(0), patternPieces(QStringList()), mode(mode), sceneDraw(sceneDraw), + sceneDetail(sceneDetail) { SCASSERT(sceneDraw != nullptr); SCASSERT(sceneDetail != nullptr); @@ -724,7 +724,6 @@ void VPattern::LiteParseTree() */ void VPattern::haveLiteChange() { - patternModified = true; emit patternChanged(); } diff --git a/src/app/xml/vpattern.h b/src/app/xml/vpattern.h index bb491e015..2cbe9a839 100644 --- a/src/app/xml/vpattern.h +++ b/src/app/xml/vpattern.h @@ -177,9 +177,6 @@ private: /** @brief mode current draw mode. */ Valentina::Draws *mode; - /** @brief fileModified true if exist change in file. */ - bool patternModified; - VMainGraphicsScene *sceneDraw; VMainGraphicsScene *sceneDetail; @@ -250,18 +247,6 @@ inline quint32 VPattern::getCursor() const return cursor; } -//--------------------------------------------------------------------------------------------------------------------- -inline bool VPattern::isPatternModified() const -{ - return patternModified; -} - -//--------------------------------------------------------------------------------------------------------------------- -inline void VPattern::setPatternModified(bool value) -{ - patternModified = value; -} - //--------------------------------------------------------------------------------------------------------------------- inline QStringList VPattern::getPatternPieces() const { diff --git a/src/app/xml/vundocommands.cpp b/src/app/xml/vundocommands.cpp index e99c55536..f38f98eb9 100644 --- a/src/app/xml/vundocommands.cpp +++ b/src/app/xml/vundocommands.cpp @@ -28,6 +28,8 @@ #include "vundocommands.h" #include +#include "../widgets/vapplication.h" +#include "../tools/vabstracttool.h" //--------------------------------------------------------------------------------------------------------------------- AddToCal::AddToCal(const QDomElement &xml, VPattern *doc, QUndoCommand *parent) @@ -94,11 +96,7 @@ void AddToCal::redo() { qDebug()<<"Can't find tag Calculation"<< Q_FUNC_INFO; } - if (redoFlag == false) - { - emit UnsavedChange(); - } - else + if (redoFlag) { emit NeedFullParsing(); } @@ -157,3 +155,84 @@ void AddPatternPiece::redo() } redoFlag = true; } + +//--------------------------------------------MoveSPoint--------------------------------------------------------------- +MoveSPoint::MoveSPoint(VPattern *doc, const double &x, const double &y, const quint32 &id, QGraphicsScene *scene, + QUndoCommand *parent) + : QObject(), QUndoCommand(parent), doc(doc), oldX(10.0), oldY(10.0), newX(x), newY(y), sPointId(id), scene(scene) +{ + setText(tr("Move single point")); + + SCASSERT(scene != nullptr); + QDomElement domElement = doc->elementById(QString().setNum(id)); + if (domElement.isElement()) + { + oldX = qApp->toPixel(doc->GetParametrDouble(domElement, VAbstractTool::AttrX, "10.0")); + oldY = qApp->toPixel(doc->GetParametrDouble(domElement, VAbstractTool::AttrY, "10.0")); + } + else + { + qDebug()<<"Can't find spoint with id ="<< sPointId << Q_FUNC_INFO; + } +} + +//--------------------------------------------------------------------------------------------------------------------- +MoveSPoint::~MoveSPoint() +{} + +//--------------------------------------------------------------------------------------------------------------------- +void MoveSPoint::undo() +{ + QDomElement domElement = doc->elementById(QString().setNum(sPointId)); + if (domElement.isElement()) + { + doc->SetAttribute(domElement, VAbstractTool::AttrX, QString().setNum(qApp->fromPixel(oldX))); + doc->SetAttribute(domElement, VAbstractTool::AttrY, QString().setNum(qApp->fromPixel(oldY))); + + emit NeedLiteParsing(); + + QList list = scene->views(); + VAbstractTool::NewSceneRect(scene, list[0]); + } + else + { + qDebug()<<"Can't find spoint with id ="<< sPointId << Q_FUNC_INFO; + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void MoveSPoint::redo() +{ + QDomElement domElement = doc->elementById(QString().setNum(sPointId)); + if (domElement.isElement()) + { + doc->SetAttribute(domElement, VAbstractTool::AttrX, QString().setNum(qApp->fromPixel(newX))); + doc->SetAttribute(domElement, VAbstractTool::AttrY, QString().setNum(qApp->fromPixel(newY))); + + emit NeedLiteParsing(); + + QList list = scene->views(); + VAbstractTool::NewSceneRect(scene, list[0]); + } + else + { + qDebug()<<"Can't find spoint with id ="<< sPointId << Q_FUNC_INFO; + } +} + +//--------------------------------------------------------------------------------------------------------------------- +bool MoveSPoint::mergeWith(const QUndoCommand *command) +{ + const MoveSPoint *moveCommand = static_cast(command); + SCASSERT(moveCommand != nullptr); + const quint32 id = moveCommand->getSPointId(); + + if (id != sPointId) + { + return false; + } + + newX = moveCommand->getNewX(); + newY = moveCommand->getNewY(); + return true; +} diff --git a/src/app/xml/vundocommands.h b/src/app/xml/vundocommands.h index cccec03cf..e1525fda7 100644 --- a/src/app/xml/vundocommands.h +++ b/src/app/xml/vundocommands.h @@ -32,6 +32,7 @@ #include "vpattern.h" #include +#include #include class AddToCal : public QObject, public QUndoCommand @@ -43,7 +44,6 @@ public: virtual void undo(); virtual void redo(); signals: - void UnsavedChange(); void NeedFullParsing(); private: Q_DISABLE_COPY(AddToCal) @@ -54,6 +54,7 @@ private: bool redoFlag; }; +//-------------------------------------------AddPatternPiece----------------------------------------------------------- class AddPatternPiece : public QObject, public QUndoCommand { Q_OBJECT @@ -76,4 +77,57 @@ private: QString mPath; }; +//---------------------------------------------MoveSPoint-------------------------------------------------------------- +class MoveSPoint : public QObject, public QUndoCommand +{ + Q_OBJECT +public: + MoveSPoint(VPattern *doc, const double &x, const double &y, const quint32 &id, QGraphicsScene *scene, + QUndoCommand *parent = 0); + virtual ~MoveSPoint(); + virtual void undo(); + virtual void redo(); + virtual bool mergeWith(const QUndoCommand *command); + virtual int id() const; + quint32 getSPointId() const; + double getNewX() const; + double getNewY() const; +signals: + void NeedLiteParsing(); +private: + Q_DISABLE_COPY(MoveSPoint) + enum { Id = 0 }; + VPattern *doc; + double oldX; + double oldY; + double newX; + double newY; + quint32 sPointId; + QGraphicsScene *scene; +}; + +//--------------------------------------------------------------------------------------------------------------------- +inline int MoveSPoint::id() const +{ + return Id; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline quint32 MoveSPoint::getSPointId() const +{ + return sPointId; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline double MoveSPoint::getNewX() const +{ + return newX; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline double MoveSPoint::getNewY() const +{ + return newY; +} + #endif // VUNDOCOMMANDS_H