From fc3c8acc4677ee279ce3e93a2f6c477d740f25d4 Mon Sep 17 00:00:00 2001 From: dismine Date: Fri, 6 Jun 2014 18:06:41 +0300 Subject: [PATCH] Undo add pattern piece now work. --HG-- branch : feature --- src/app/dialogs/app/dialoghistory.cpp | 2 +- src/app/mainwindow.cpp | 12 +-- src/app/tools/drawTools/vdrawtool.cpp | 2 +- src/app/tools/drawTools/vtoolsinglepoint.cpp | 41 +++++++--- src/app/tools/drawTools/vtoolsinglepoint.h | 3 + src/app/xml/vpattern.cpp | 82 +++++++++++++------- src/app/xml/vpattern.h | 14 ++-- src/app/xml/vundocommands.cpp | 55 +++++++++++-- src/app/xml/vundocommands.h | 9 ++- 9 files changed, 157 insertions(+), 63 deletions(-) diff --git a/src/app/dialogs/app/dialoghistory.cpp b/src/app/dialogs/app/dialoghistory.cpp index 5639febce..d2221cb94 100644 --- a/src/app/dialogs/app/dialoghistory.cpp +++ b/src/app/dialogs/app/dialoghistory.cpp @@ -58,7 +58,7 @@ DialogHistory::DialogHistory(VContainer *data, VPattern *doc, QWidget *parent) connect(this, &DialogHistory::ShowHistoryTool, doc, &VPattern::ShowHistoryTool); connect(doc, &VPattern::ChangedCursor, this, &DialogHistory::ChangedCursor); connect(doc, &VPattern::patternChanged, this, &DialogHistory::UpdateHistory); - connect(doc, &VPattern::ChangedActivDraw, this, &DialogHistory::UpdateHistory); + connect(doc, &VPattern::ChangedActivPP, this, &DialogHistory::UpdateHistory); ShowPoint(); } diff --git a/src/app/mainwindow.cpp b/src/app/mainwindow.cpp index 179b17030..02565d3dc 100644 --- a/src/app/mainwindow.cpp +++ b/src/app/mainwindow.cpp @@ -111,9 +111,9 @@ MainWindow::MainWindow(QWidget *parent) void MainWindow::ActionNewPP() { QString patternPieceName = QString(tr("Pattern piece %1")).arg(comboBoxDraws->count()+1); + QString path; if (comboBoxDraws->count() == 0) { - QString path; DialogMeasurements measurements(this); if (measurements.exec() == QDialog::Rejected) { @@ -155,7 +155,6 @@ void MainWindow::ActionNewPP() return; } } - doc->CreateEmptyFile(path); } else { @@ -165,9 +164,9 @@ void MainWindow::ActionNewPP() return; } } - if (doc->appendDraw(patternPieceName) == false) + if (doc->appendPP(patternPieceName) == false) { - qDebug()<<"Error creating pattern with the name "<(&QComboBox::currentIndexChanged), @@ -178,7 +177,7 @@ void MainWindow::ActionNewPP() //Create single point const quint32 id = pattern->AddGObject(new VPointF(qApp->toPixel((10+comboBoxDraws->count()*5)), qApp->toPixel(10), "А", 5, 10)); - VToolSinglePoint *spoint = new VToolSinglePoint(doc, pattern, id, Valentina::FromGui); + VToolSinglePoint *spoint = new VToolSinglePoint(doc, pattern, id, Valentina::FromGui, patternPieceName, path); sceneDraw->addItem(spoint); connect(spoint, &VToolPoint::ChoosedTool, sceneDraw, &VMainGraphicsScene::ChoosedItem); connect(sceneDraw, &VMainGraphicsScene::NewFactor, spoint, &VToolSinglePoint::SetFactor); @@ -902,7 +901,7 @@ void MainWindow::currentDrawChanged( int index ) { if (index != -1) { - doc->ChangeActivDraw(comboBoxDraws->itemText(index)); + doc->ChangeActivPP(comboBoxDraws->itemText(index)); doc->setCurrentData(); if (drawMode) { @@ -1644,6 +1643,7 @@ bool MainWindow::SavePattern(const QString &fileName) { setCurrentFile(fileName); helpLabel->setText(tr("File saved")); + qApp->getUndoStack()->setClean(); } } return result; diff --git a/src/app/tools/drawTools/vdrawtool.cpp b/src/app/tools/drawTools/vdrawtool.cpp index 7a6b628e7..797750657 100644 --- a/src/app/tools/drawTools/vdrawtool.cpp +++ b/src/app/tools/drawTools/vdrawtool.cpp @@ -46,7 +46,7 @@ VDrawTool::VDrawTool(VPattern *doc, VContainer *data, quint32 id) :VAbstractTool(doc, data, id), ignoreContextMenuEvent(false), ignoreFullUpdate(false), nameActivDraw(doc->GetNameActivDraw()), dialog(nullptr) { - connect(this->doc, &VPattern::ChangedActivDraw, this, &VDrawTool::ChangedActivDraw); + connect(this->doc, &VPattern::ChangedActivPP, this, &VDrawTool::ChangedActivDraw); connect(this->doc, &VPattern::ChangedNameDraw, this, &VDrawTool::ChangedNameDraw); connect(this->doc, &VPattern::ShowTool, this, &VDrawTool::ShowTool); } diff --git a/src/app/tools/drawTools/vtoolsinglepoint.cpp b/src/app/tools/drawTools/vtoolsinglepoint.cpp index 288f09050..dc56afa12 100644 --- a/src/app/tools/drawTools/vtoolsinglepoint.cpp +++ b/src/app/tools/drawTools/vtoolsinglepoint.cpp @@ -29,12 +29,14 @@ #include "vtoolsinglepoint.h" #include "../../dialogs/tools/dialogsinglepoint.h" +#include + const QString VToolSinglePoint::ToolType = QStringLiteral("single"); //--------------------------------------------------------------------------------------------------------------------- VToolSinglePoint::VToolSinglePoint (VPattern *doc, VContainer *data, quint32 id, const Valentina::Sources &typeCreation, - QGraphicsItem * parent ) - :VToolPoint(doc, data, id, parent) + const QString &namePP, const QString &mPath, QGraphicsItem * parent ) + :VToolPoint(doc, data, id, parent), namePP(namePP), mPath(mPath) { baseColor = Qt::red; currentColor = baseColor; @@ -67,18 +69,35 @@ void VToolSinglePoint::setDialog() //--------------------------------------------------------------------------------------------------------------------- void VToolSinglePoint::AddToFile() { + Q_ASSERT_X(namePP.isEmpty() == false, "AddToFile", "name pattern piece is empty"); + const VPointF *point = VAbstractTool::data.GeometricObject(id); - QDomElement domElement = doc->createElement(TagName); + QDomElement sPoint = doc->createElement(TagName); - doc->SetAttribute(domElement, VDomDocument::AttrId, id); - doc->SetAttribute(domElement, AttrType, ToolType); - doc->SetAttribute(domElement, AttrName, point->name()); - doc->SetAttribute(domElement, AttrX, qApp->fromPixel(point->x())); - doc->SetAttribute(domElement, AttrY, qApp->fromPixel(point->y())); - doc->SetAttribute(domElement, AttrMx, qApp->fromPixel(point->mx())); - doc->SetAttribute(domElement, AttrMy, qApp->fromPixel(point->my())); + // Create SPoint tag + doc->SetAttribute(sPoint, VDomDocument::AttrId, id); + doc->SetAttribute(sPoint, AttrType, ToolType); + doc->SetAttribute(sPoint, AttrName, point->name()); + doc->SetAttribute(sPoint, AttrX, qApp->fromPixel(point->x())); + doc->SetAttribute(sPoint, AttrY, qApp->fromPixel(point->y())); + doc->SetAttribute(sPoint, AttrMx, qApp->fromPixel(point->mx())); + doc->SetAttribute(sPoint, AttrMy, qApp->fromPixel(point->my())); - AddToCalculation(domElement); + //Create pattern piece structure + QDomElement patternPiece = doc->createElement(VPattern::TagDraw); + doc->SetAttribute(patternPiece, AttrName, namePP); + + QDomElement calcElement = doc->createElement(VPattern::TagCalculation); + calcElement.appendChild(sPoint); + + patternPiece.appendChild(calcElement); + patternPiece.appendChild(doc->createElement(VPattern::TagModeling)); + patternPiece.appendChild(doc->createElement(VPattern::TagDetails)); + + AddPatternPiece *addPP = new AddPatternPiece(patternPiece, doc, namePP, mPath); + connect(addPP, &AddPatternPiece::ClearScene, doc, &VPattern::ClearScene); + connect(addPP, &AddPatternPiece::NeedFullParsing, doc, &VPattern::NeedFullParsing); + qApp->getUndoStack()->push(addPP); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/tools/drawTools/vtoolsinglepoint.h b/src/app/tools/drawTools/vtoolsinglepoint.h index 116734b6f..7c07bea75 100644 --- a/src/app/tools/drawTools/vtoolsinglepoint.h +++ b/src/app/tools/drawTools/vtoolsinglepoint.h @@ -48,6 +48,7 @@ public: * @param parent parent object. */ VToolSinglePoint (VPattern *doc, VContainer *data, quint32 id, const Valentina::Sources &typeCreation, + const QString &namePP = QString(), const QString &mPath = QString(), QGraphicsItem * parent = nullptr ); /** * @brief setDialog set dialog when user want change tool option. @@ -114,6 +115,8 @@ protected: */ virtual void SaveDialog(QDomElement &domElement); private: + QString namePP; + QString mPath; /** * @brief setColorLabel change color for label and label line. * @param color new color. diff --git a/src/app/xml/vpattern.cpp b/src/app/xml/vpattern.cpp index 7ebeb3878..8eb844288 100644 --- a/src/app/xml/vpattern.cpp +++ b/src/app/xml/vpattern.cpp @@ -91,6 +91,7 @@ void VPattern::CreateEmptyFile(const QString &tablePath) { throw VException("Path to measurement table empty."); } + this->clear(); QDomElement patternElement = this->createElement(TagPattern); patternElement.appendChild(createComment("Valentina pattern format.")); @@ -117,21 +118,21 @@ void VPattern::CreateEmptyFile(const QString &tablePath) //--------------------------------------------------------------------------------------------------------------------- /** - * @brief ChangeActivDraw set new pattern peace name. + * @brief ChangeActivPP set new active pattern piece name. * @param name new name. * @param parse parser file mode. */ -void VPattern::ChangeActivDraw(const QString &name, const Document::Documents &parse) +void VPattern::ChangeActivPP(const QString &name, const Document::Documents &parse) { - Q_ASSERT_X(name.isEmpty() == false, "ChangeActivDraw", "name draw is empty"); + Q_ASSERT_X(name.isEmpty() == false, "ChangeActivPP", "name pattern piece is empty"); if (this->nameActivDraw != name) { - if (CheckNameDraw(name)) + if (CheckNamePP(name)) { this->nameActivDraw = name; if (parse == Document::FullParse) { - emit ChangedActivDraw(name); + emit ChangedActivPP(name); } } } @@ -170,37 +171,29 @@ bool VPattern::GetActivDrawElement(QDomElement &element) const //--------------------------------------------------------------------------------------------------------------------- /** - * @brief appendDraw add new pattern peace structure to the file. + * @brief appendPP add new pattern piece. + * + * Method check if not exist pattern piece with the same name and change name active pattern piece name, send signal + * about change pattern piece. Doen't add pattern piece to file structure. This task make SPoint tool. * @param name pattern peace name. * @return true if success. */ -bool VPattern::appendDraw(const QString &name) +bool VPattern::appendPP(const QString &name) { - Q_ASSERT_X(name.isEmpty() == false, "appendDraw", "name draw is empty"); + Q_ASSERT_X(name.isEmpty() == false, "appendPP", "name pattern piece is empty"); if (name.isEmpty()) { return false; } - if (CheckNameDraw(name) == false) + if (CheckNamePP(name) == false) { - QDomElement rootElement = this->documentElement(); - - QDomElement drawElement = this->createElement(TagDraw); - SetAttribute(drawElement, AttrName, name); - - drawElement.appendChild(createElement(TagCalculation)); - drawElement.appendChild(createElement(TagModeling)); - drawElement.appendChild(createElement(TagDetails)); - - rootElement.appendChild(drawElement); - if (nameActivDraw.isEmpty()) { - SetActivDraw(name); + SetActivPP(name); } else { - ChangeActivDraw(name); + ChangeActivPP(name); } return true; } @@ -268,17 +261,17 @@ void VPattern::Parse(const Document::Documents &parse) { if (nameActivDraw.isEmpty()) { - SetActivDraw(GetParametrString(domElement, AttrName)); + SetActivPP(GetParametrString(domElement, AttrName)); } else { - ChangeActivDraw(GetParametrString(domElement, AttrName)); + ChangeActivPP(GetParametrString(domElement, AttrName)); } patternPieces << GetParametrString(domElement, AttrName); } else { - ChangeActivDraw(GetParametrString(domElement, AttrName), Document::LiteParse); + ChangeActivPP(GetParametrString(domElement, AttrName), Document::LiteParse); } ParseDrawElement(sceneDraw, sceneDetail, domElement, parse); break; @@ -487,7 +480,7 @@ quint32 VPattern::SPointActiveDraw() * @param name pattern peace name. * @return true if exist. */ -bool VPattern::CheckNameDraw(const QString &name) const +bool VPattern::CheckNamePP(const QString &name) const { Q_ASSERT_X(name.isEmpty() == false, "CheckNameDraw", "name draw is empty"); const QDomNodeList elements = this->documentElement().elementsByTagName( TagDraw ); @@ -511,12 +504,12 @@ bool VPattern::CheckNameDraw(const QString &name) const //--------------------------------------------------------------------------------------------------------------------- /** - * @brief SetActivDraw set current pattern peace. + * @brief SetActivPP set current pattern piece. * @param name pattern peace name. */ -void VPattern::SetActivDraw(const QString &name) +void VPattern::SetActivPP(const QString &name) { - Q_ASSERT_X(name.isEmpty() == false, "SetActivDraw", "name draw is empty"); + Q_ASSERT_X(name.isEmpty() == false, "SetActivPP", "name pattern piece is empty"); this->nameActivDraw = name; } @@ -747,11 +740,17 @@ void VPattern::ShowHistoryTool(quint32 id, Qt::GlobalColor color, bool enable) emit ShowTool(id, color, enable); } +//--------------------------------------------------------------------------------------------------------------------- void VPattern::NeedFullParsing() { emit UndoCommand(); } +//--------------------------------------------------------------------------------------------------------------------- +void VPattern::ClearScene() +{ + emit ClearMainWindow(); +} //--------------------------------------------------------------------------------------------------------------------- /** @@ -1877,3 +1876,28 @@ void VPattern::UpdateMeasurements() m.Measurements(); } } + + +QDomElement VPattern::GetPPElement(const QString &name) +{ + if (name.isEmpty() == false) + { + const QDomNodeList elements = this->documentElement().elementsByTagName( TagDraw ); + if (elements.size() == 0) + { + return QDomElement(); + } + for ( qint32 i = 0; i < elements.count(); i++ ) + { + QDomElement element = elements.at( i ).toElement(); + if (element.isNull() == false) + { + if ( element.attribute( AttrName ) == name ) + { + return element; + } + } + } + } + return QDomElement(); +} diff --git a/src/app/xml/vpattern.h b/src/app/xml/vpattern.h index 6936adb9e..77324570c 100644 --- a/src/app/xml/vpattern.h +++ b/src/app/xml/vpattern.h @@ -65,10 +65,10 @@ public: VPattern(VContainer *data, Valentina::Draws *mode, VMainGraphicsScene *sceneDraw, VMainGraphicsScene *sceneDetail, QObject *parent = nullptr); void CreateEmptyFile(const QString &tablePath); - void ChangeActivDraw(const QString& name, const Document::Documents &parse = Document::FullParse); + void ChangeActivPP(const QString& name, const Document::Documents &parse = Document::FullParse); QString GetNameActivDraw() const; bool GetActivDrawElement(QDomElement &element) const; - bool appendDraw(const QString& name); + bool appendPP(const QString& name); bool SetNameDraw(const QString& name); void Parse(const Document::Documents &parse); QHash* getTools(); @@ -116,12 +116,13 @@ public: static const QString IncrementDescription; virtual bool SaveDocument(const QString &fileName); QStringList getPatternPieces() const; + QDomElement GetPPElement(const QString &name); signals: /** * @brief ChangedActivDraw change active pattern peace. * @param newName new pattern peace name. */ - void ChangedActivDraw(const QString &newName); + void ChangedActivPP(const QString &newName); /** * @brief ChangedNameDraw save new name active pattern peace. * @param oldName old name. @@ -155,6 +156,7 @@ public slots: void haveLiteChange(); void ShowHistoryTool(quint32 id, Qt::GlobalColor color, bool enable); void NeedFullParsing(); + void ClearScene(); private: Q_DISABLE_COPY(VPattern) @@ -181,8 +183,8 @@ private: VMainGraphicsScene *sceneDraw; VMainGraphicsScene *sceneDetail; - bool CheckNameDraw(const QString& name) const; - void SetActivDraw(const QString& name); + bool CheckNamePP(const QString& name) const; + void SetActivPP(const QString& name); void ParseDrawElement(VMainGraphicsScene *sceneDraw, VMainGraphicsScene *sceneDetail, const QDomNode& node, const Document::Documents &parse); void ParseDrawMode(VMainGraphicsScene *sceneDraw, VMainGraphicsScene *sceneDetail, @@ -266,4 +268,6 @@ inline QStringList VPattern::getPatternPieces() const return patternPieces; } + + #endif // VPATTERN_H diff --git a/src/app/xml/vundocommands.cpp b/src/app/xml/vundocommands.cpp index 588b2f57b..e99c55536 100644 --- a/src/app/xml/vundocommands.cpp +++ b/src/app/xml/vundocommands.cpp @@ -31,9 +31,10 @@ //--------------------------------------------------------------------------------------------------------------------- AddToCal::AddToCal(const QDomElement &xml, VPattern *doc, QUndoCommand *parent) - : QObject(), QUndoCommand(parent), xml(xml), doc(doc), nameActivDraw(doc->GetNameActivDraw()), cursor(doc->getCursor()) + : QObject(), QUndoCommand(parent), xml(xml), doc(doc), nameActivDraw(doc->GetNameActivDraw()), + cursor(doc->getCursor()), redoFlag(false) { - setText(tr("Add to section %1").arg(VPattern::TagCalculation)); + setText(tr("Add object")); } //--------------------------------------------------------------------------------------------------------------------- @@ -43,7 +44,7 @@ AddToCal::~AddToCal() //--------------------------------------------------------------------------------------------------------------------- void AddToCal::undo() { - doc->ChangeActivDraw(nameActivDraw); + doc->ChangeActivPP(nameActivDraw); doc->setCursor(cursor); QDomElement calcElement; @@ -65,7 +66,7 @@ void AddToCal::undo() //--------------------------------------------------------------------------------------------------------------------- void AddToCal::redo() { - doc->ChangeActivDraw(nameActivDraw); + doc->ChangeActivPP(nameActivDraw); doc->setCursor(cursor); QDomElement calcElement; @@ -93,28 +94,66 @@ void AddToCal::redo() { qDebug()<<"Can't find tag Calculation"<< Q_FUNC_INFO; } - emit UnsavedChange(); + if (redoFlag == false) + { + emit UnsavedChange(); + } + else + { + emit NeedFullParsing(); + } + redoFlag = true; } //--------------------------------------------AddPatternPiece---------------------------------------------------------- int AddPatternPiece::countPP = 0; -AddPatternPiece::AddPatternPiece(const QDomElement &xml, VPattern *doc, QUndoCommand *parent) - : QObject(), QUndoCommand(parent), xml(xml), doc(doc) +AddPatternPiece::AddPatternPiece(const QDomElement &xml, VPattern *doc, const QString &namePP, const QString &mPath, + QUndoCommand *parent) + : QObject(), QUndoCommand(parent), xml(xml), doc(doc), namePP(namePP), redoFlag(false), mPath(mPath) { - setText(tr("Add to new pattern piece")); + setText(tr("Add pattern piece %1").arg(namePP)); } +//--------------------------------------------------------------------------------------------------------------------- AddPatternPiece::~AddPatternPiece() {} +//--------------------------------------------------------------------------------------------------------------------- void AddPatternPiece::undo() { + if (countPP <= 1) + { + emit ClearScene(); + } + else + { + QDomElement rootElement = doc->documentElement(); + QDomElement patternPiece = doc->GetPPElement(namePP); + rootElement.removeChild(patternPiece); + emit NeedFullParsing(); + } --countPP; } +//--------------------------------------------------------------------------------------------------------------------- void AddPatternPiece::redo() { ++countPP; + if (countPP == 1 && mPath.isEmpty() == false) + { + doc->CreateEmptyFile(mPath); + } + + QDomElement rootElement = doc->documentElement(); + + rootElement.appendChild(xml); + doc->haveLiteChange(); + + if (redoFlag) + { + emit NeedFullParsing(); + } + redoFlag = true; } diff --git a/src/app/xml/vundocommands.h b/src/app/xml/vundocommands.h index 2e29de79e..cccec03cf 100644 --- a/src/app/xml/vundocommands.h +++ b/src/app/xml/vundocommands.h @@ -51,24 +51,29 @@ private: VPattern *doc; const QString nameActivDraw; quint32 cursor; + bool redoFlag; }; class AddPatternPiece : public QObject, public QUndoCommand { Q_OBJECT public: - AddPatternPiece(const QDomElement &xml, VPattern *doc, QUndoCommand *parent = 0); + AddPatternPiece(const QDomElement &xml, VPattern *doc, const QString &namePP, const QString &mPath, + QUndoCommand *parent = 0); virtual ~AddPatternPiece(); virtual void undo(); virtual void redo(); signals: - void UnsavedChange(); + void ClearScene(); void NeedFullParsing(); private: Q_DISABLE_COPY(AddPatternPiece) const QDomElement xml; VPattern *doc; static int countPP; + QString namePP; + bool redoFlag; + QString mPath; }; #endif // VUNDOCOMMANDS_H