From ed75c57c1d1341c66f14fe4e2ec61ea1bd306efa Mon Sep 17 00:00:00 2001 From: blacksun Date: Sun, 9 Aug 2015 12:06:31 +0000 Subject: [PATCH] Use QPropertyUndoCommand instead of ChangeSeveralConductorsPropertiesCommand and ChangeConductorsPropertiesCommand. Remove class ChangeSeveralConductorsPropertiesCommand and ChangeConductorsPropertiesCommand. git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4092 bfdf4180-ca20-0410-9c96-a3a8aa849046 --- sources/conductorautonumerotation.cpp | 54 ++++----- sources/conductorproperties.h | 10 +- sources/diagramcommands.cpp | 138 ----------------------- sources/diagramcommands.h | 69 +----------- sources/diagramview.cpp | 36 +++--- sources/qetgraphicsitem/conductor.cpp | 14 ++- sources/qetgraphicsitem/conductor.h | 6 +- sources/qetproject.h | 1 + sources/ui/conductorpropertiesdialog.cpp | 46 ++++---- sources/ui/potentialtextsdialog.cpp | 1 + 10 files changed, 87 insertions(+), 288 deletions(-) diff --git a/sources/conductorautonumerotation.cpp b/sources/conductorautonumerotation.cpp index 9da3870bd..5dccbd9d9 100644 --- a/sources/conductorautonumerotation.cpp +++ b/sources/conductorautonumerotation.cpp @@ -16,13 +16,13 @@ along with QElectroTech. If not, see . */ #include "conductorautonumerotation.h" -#include "diagramcommands.h" #include "numerotationcontextcommands.h" #include "qetdiagrameditor.h" #include "conductor.h" #include "diagram.h" #include "potentialtextsdialog.h" #include "qet.h" +#include "QPropertyUndoCommand/qpropertyundocommand.h" /** * @brief ConductorAutoNumerotation::ConductorAutoNumerotation @@ -80,42 +80,34 @@ void ConductorAutoNumerotation::checkPotential(Conductor *conductor, QUndoComman * @brief ConductorAutoNumerotation::applyText * apply the text @t to @conductor_ and all conductors at the same potential */ -void ConductorAutoNumerotation::applyText(QString t) { +void ConductorAutoNumerotation::applyText(QString t) +{ if (!conductor_) return; - if (conductor_list.empty()) - { - //initialize the corresponding UndoCommand object - ChangeConductorPropertiesCommand *ccpc = new ChangeConductorPropertiesCommand (conductor_, m_parent_undo); - ccpc -> setOldSettings (conductor_ -> properties()); - ConductorProperties cp = conductor_ -> properties(); - cp.text = t; - ccpc -> setNewSettings(cp); - if (!m_parent_undo) - m_diagram -> undoStack().push(ccpc); - } - else - { - QList clist = conductor_list.toList(); - clist << conductor_; - QList old_properties, new_properties; - ConductorProperties cp; + QVariant old_value, new_value; + ConductorProperties cp = conductor_ -> properties(); + old_value.setValue(cp); + cp.text = t; + new_value.setValue(cp); - foreach (Conductor *c, clist) + QPropertyUndoCommand *undo = new QPropertyUndoCommand(conductor_, "properties", old_value, new_value, m_parent_undo); + undo->setText(QObject::tr("Modifier les propriétés d'un conducteur", "undo caption")); + + if (!conductor_list.isEmpty()) + { + undo->setText(QObject::tr("Modifier les propriétés de plusieurs conducteurs", "undo caption")); + foreach (Conductor *cond, conductor_list) { - old_properties << c -> properties(); - cp = c -> properties(); - cp.text = t; - new_properties << cp; + ConductorProperties cp2 = cond -> properties(); + old_value.setValue(cp2); + cp2.text = t; + new_value.setValue(cp2); + new QPropertyUndoCommand(cond, "properties", old_value, new_value, undo); } - - //initialize the corresponding UndoCommand object - ChangeSeveralConductorsPropertiesCommand *cscpc = new ChangeSeveralConductorsPropertiesCommand(clist, m_parent_undo); - cscpc -> setOldSettings(old_properties); - cscpc -> setNewSettings(new_properties); - if (!m_parent_undo) - m_diagram -> undoStack().push(cscpc); } + + if (!m_parent_undo) + m_diagram->undoStack().push(undo); } /** diff --git a/sources/conductorproperties.h b/sources/conductorproperties.h index f8f5f56d0..91ce40582 100644 --- a/sources/conductorproperties.h +++ b/sources/conductorproperties.h @@ -17,9 +17,12 @@ */ #ifndef CONDUCTOR_PROPERTIES_H #define CONDUCTOR_PROPERTIES_H + #include "qet.h" -#include -#include +#include + +class QPainter; + /** This class represents the properties of a singleline conductor. */ @@ -109,4 +112,7 @@ class ConductorProperties { void readStyle(const QString &); QString writeStyle() const; }; + +Q_DECLARE_METATYPE(ConductorProperties) + #endif diff --git a/sources/diagramcommands.cpp b/sources/diagramcommands.cpp index a631c3ef0..585d449ea 100644 --- a/sources/diagramcommands.cpp +++ b/sources/diagramcommands.cpp @@ -26,7 +26,6 @@ #include "diagram.h" #include "qetgraphicsitem/diagramtextitem.h" #include "qetgraphicsitem/diagramimageitem.h" -#include "conductorautonumerotation.h" #include QString itemText(const QetGraphicsItem *item) { @@ -810,140 +809,3 @@ void ChangeBorderCommand::redo() { diagram -> showMe(); diagram -> border_and_titleblock.importBorder(new_properties); } - -/** - Constructeur - @param c Le conducteur dont on modifie les proprietes - @param parent QUndoCommand parent -*/ -ChangeConductorPropertiesCommand::ChangeConductorPropertiesCommand(Conductor *c, QUndoCommand *parent) : - QUndoCommand(QObject::tr("modifier les propriétés d'un conducteur", "undo caption"), parent), - conductor(c), - old_settings_set(false), - new_settings_set(false) -{ -} - -/// Destructeur -ChangeConductorPropertiesCommand::~ChangeConductorPropertiesCommand() { -} - -/// definit l'ancienne configuration -void ChangeConductorPropertiesCommand::setOldSettings(const ConductorProperties &properties) { - old_properties = properties; - old_settings_set = true; -} - -/// definit la nouvelle configuration -void ChangeConductorPropertiesCommand::setNewSettings(const ConductorProperties &properties) { - new_properties = properties; - new_settings_set = true; -} - -/** - Annule les changements - Attention : les anciens et nouveaux parametres - doivent avoir ete definis a l'aide de setNewSettings et setOldSettings -*/ -void ChangeConductorPropertiesCommand::undo() { - if (conductor -> diagram()) conductor -> diagram() -> showMe(); - if (old_settings_set && new_settings_set) { - conductor -> setProperties(old_properties); - conductor -> update(); - } -} - -/** - Refait les changements - Attention : les anciens et nouveaux parametres - doivent avoir ete definis a l'aide de setNewSettings et setOldSettings -*/ -void ChangeConductorPropertiesCommand::redo() { - if (conductor -> diagram()) conductor -> diagram() -> showMe(); - if (old_settings_set && new_settings_set) { - conductor -> setProperties(new_properties); - conductor -> update(); - } -} - -/** - Constructeur - @param c La liste des conducteurs dont on modifie les proprietes - @param parent QUndoCommand parent -*/ -ChangeSeveralConductorsPropertiesCommand::ChangeSeveralConductorsPropertiesCommand(QListc, QUndoCommand *parent) : - QUndoCommand(QObject::tr("modifier les propriétés de plusieurs conducteurs", "undo caption"), parent), - conductors(c), - old_settings_set(false), - new_settings_set(false) -{ -} - -/// Destructeur -ChangeSeveralConductorsPropertiesCommand::~ChangeSeveralConductorsPropertiesCommand() { -} - -/// definit l'ancienne configuration -void ChangeSeveralConductorsPropertiesCommand::setOldSettings(const QList &properties) { - if (!old_settings_set) { - old_properties = properties; - old_settings_set = true; - } -} - -/// definit la nouvelle configuration -void ChangeSeveralConductorsPropertiesCommand::setNewSettings(const QList &properties) { - if (!new_settings_set) { - new_properties = properties; - new_settings_set = true; - } -} - -void ChangeSeveralConductorsPropertiesCommand::setNewSettings(const ConductorProperties &properties) { - if (!new_settings_set) { - single_new_properties = properties; - new_settings_set = true; - } -} - -/** - Annule les changements - Attention : les anciens et nouveaux parametres - doivent avoir ete definis a l'aide de setNewSettings et setOldSettings -*/ -void ChangeSeveralConductorsPropertiesCommand::undo() { - if (conductors.first() -> diagram()) conductors.first() -> diagram(); - if (old_settings_set && new_settings_set) { - int i=0; - foreach(Conductor *c, conductors) { - c -> setProperties(old_properties.at(i)); - c -> update(); - i++; - } - } -} - -/** - Refait les changements - Attention : les anciens et nouveaux parametres - doivent avoir ete definis a l'aide de setNewSettings et setOldSettings -*/ -void ChangeSeveralConductorsPropertiesCommand::redo() { - if (conductors.first() -> diagram()) conductors.first() -> diagram(); - if (old_settings_set && new_settings_set) { - - //new propertie are the same for each conductor - if (new_properties.isEmpty()) { - foreach(Conductor *c, conductors) { - c -> setProperties(single_new_properties); - c -> update(); - } - } - - //new propertie are different for each conductor - else { - int i=0; - foreach(Conductor *c, conductors) { - c -> setProperties(new_properties.at(i)); - c -> update(); - i++; - } - } - } -} diff --git a/sources/diagramcommands.h b/sources/diagramcommands.h index dcd521374..14acd80d3 100644 --- a/sources/diagramcommands.h +++ b/sources/diagramcommands.h @@ -17,10 +17,9 @@ */ #ifndef DIAGRAM_COMMANDS_H #define DIAGRAM_COMMANDS_H -#include + #include "borderproperties.h" #include "qetgraphicsitem/conductor.h" -#include "conductorproperties.h" #include "diagramcontent.h" #include "titleblockproperties.h" #include "qet.h" @@ -416,70 +415,4 @@ class ChangeBorderCommand : public QUndoCommand { BorderProperties new_properties; }; -/** - This command changes the properties for a particular conductor. -*/ -class ChangeConductorPropertiesCommand : public QUndoCommand { - // constructors, destructor - public: - ChangeConductorPropertiesCommand(Conductor *, QUndoCommand * = 0); - virtual ~ChangeConductorPropertiesCommand(); - private: - ChangeConductorPropertiesCommand(const ChangeConductorPropertiesCommand &); - - // methods - public: - virtual void undo(); - virtual void redo(); - virtual void setOldSettings(const ConductorProperties &); - virtual void setNewSettings(const ConductorProperties &); - - // attributes - private: - /// modified conductor - Conductor *conductor; - /// properties before the change - ConductorProperties old_properties; - /// properties after the change - ConductorProperties new_properties; - /// track whether pre-change properties were set - bool old_settings_set; - /// track whether post-change properties were set - bool new_settings_set; -}; - -/** - This command changes the properties for several conductors. -*/ -class ChangeSeveralConductorsPropertiesCommand : public QUndoCommand { - // constructors, destructor - public: - ChangeSeveralConductorsPropertiesCommand(QList, QUndoCommand * = 0); - virtual ~ChangeSeveralConductorsPropertiesCommand(); - private: - ChangeSeveralConductorsPropertiesCommand(const ChangeSeveralConductorsPropertiesCommand &); - - // methods - public: - virtual void undo(); - virtual void redo(); - virtual void setOldSettings(const QList &); - virtual void setNewSettings(const QList &); - virtual void setNewSettings(const ConductorProperties &); - - // attributes - private: - /// modified conductor - QList conductors; - /// properties before the change - QList old_properties; - /// properties after the change - QList new_properties; - /// single properties for each conductor - ConductorProperties single_new_properties; - /// track whether pre-change properties were set - bool old_settings_set; - /// track whether post-change properties were set - bool new_settings_set; -}; #endif diff --git a/sources/diagramview.cpp b/sources/diagramview.cpp index 6d684cea8..b8d202cb6 100644 --- a/sources/diagramview.cpp +++ b/sources/diagramview.cpp @@ -43,6 +43,7 @@ #include "diagrampropertiesdialog.h" #include "dveventinterface.h" #include "diagrameventaddelement.h" +#include "QPropertyUndoCommand/qpropertyundocommand.h" /** Constructeur @@ -947,14 +948,14 @@ void DiagramView::editSelectedConductorColor() { Edit the color of the given conductor @param edited_conductor Conductor we want to change the color */ -void DiagramView::editConductorColor(Conductor *edited_conductor) { - if (scene -> isReadOnly()) return; - if (!edited_conductor) return; +void DiagramView::editConductorColor(Conductor *edited_conductor) +{ + if (scene -> isReadOnly() || !edited_conductor) return; - // store the initial properties of the provided conductor + // store the initial properties of the provided conductor ConductorProperties initial_properties = edited_conductor -> properties(); - // prepare a color dialog showing the initial conductor color + // prepare a color dialog showing the initial conductor color QColorDialog *color_dialog = new QColorDialog(this); color_dialog -> setWindowTitle(tr("Choisir la nouvelle couleur de ce conducteur")); #ifdef Q_OS_MAC @@ -962,18 +963,21 @@ void DiagramView::editConductorColor(Conductor *edited_conductor) { #endif color_dialog -> setCurrentColor(initial_properties.color); - // asks the user what color he wishes to apply - if (color_dialog -> exec() == QDialog::Accepted) { + // asks the user what color he wishes to apply + if (color_dialog -> exec() == QDialog::Accepted) + { QColor new_color = color_dialog -> selectedColor(); - if (new_color != initial_properties.color) { - // the user chose a different color - ConductorProperties new_properties = initial_properties; - new_properties.color = new_color; - - ChangeConductorPropertiesCommand *ccpc = new ChangeConductorPropertiesCommand(edited_conductor); - ccpc -> setOldSettings(initial_properties); - ccpc -> setNewSettings(new_properties); - diagram() -> undoStack().push(ccpc); + if (new_color != initial_properties.color) + { + // the user chose a different color + QVariant old_value, new_value; + old_value.setValue(initial_properties); + initial_properties.color = new_color; + new_value.setValue(initial_properties); + + QPropertyUndoCommand *undo = new QPropertyUndoCommand(edited_conductor, "properties", old_value, new_value); + undo->setText(tr("Modifier les propriétés d'un conducteur", "undo caption")); + diagram() -> undoStack().push(undo); } } } diff --git a/sources/qetgraphicsitem/conductor.cpp b/sources/qetgraphicsitem/conductor.cpp index 965e4f316..b9e4f7026 100644 --- a/sources/qetgraphicsitem/conductor.cpp +++ b/sources/qetgraphicsitem/conductor.cpp @@ -27,6 +27,8 @@ #include "terminal.h" #include "conductorautonumerotation.h" #include "conductorpropertiesdialog.h" +#include "QPropertyUndoCommand/qpropertyundocommand.h" + #define PR(x) qDebug() << #x " = " << x; bool Conductor::pen_and_brush_initialized = false; @@ -1371,6 +1373,7 @@ void Conductor::readProperties() { text_item -> setVisible(properties_.m_show_text); } calculateTextItemPosition(); + update(); } /** @@ -1415,14 +1418,15 @@ void Conductor::displayedTextChanged() { if (qmbreturn == 0 || qmbreturn == QMessageBox::No) { - // initialise l'objet UndoCommand correspondant + QVariant old_value, new_value; + old_value.setValue(properties_); ConductorProperties new_properties(properties_); new_properties.text = text_item -> toPlainText(); + new_value.setValue(new_properties); - ChangeConductorPropertiesCommand *ccpc = new ChangeConductorPropertiesCommand(this); - ccpc -> setOldSettings(properties_); - ccpc -> setNewSettings(new_properties); - my_diagram -> undoStack().push(ccpc); + QPropertyUndoCommand *undo = new QPropertyUndoCommand(this, "properties", old_value, new_value); + undo->setText(tr("Modifier les propriétés d'un conducteur", "undo caption")); + my_diagram -> undoStack().push(undo); } } } diff --git a/sources/qetgraphicsitem/conductor.h b/sources/qetgraphicsitem/conductor.h index 2e7180816..ad6cbbb06 100644 --- a/sources/qetgraphicsitem/conductor.h +++ b/sources/qetgraphicsitem/conductor.h @@ -19,6 +19,7 @@ #define CONDUCTOR_H #include "conductorproperties.h" +#include class ConductorProfile; class ConductorSegmentProfile; @@ -34,12 +35,13 @@ typedef QHash ConductorProfilesGroup; This class represents a conductor, i.e. a wire between two element terminals. */ -class Conductor : public QObject, public QGraphicsPathItem { - +class Conductor : public QObject, public QGraphicsPathItem +{ Q_OBJECT Q_PROPERTY(QPointF pos READ pos WRITE setPos) Q_PROPERTY(int animPath READ fakePath WRITE updatePathAnimate) + Q_PROPERTY(ConductorProperties properties READ properties WRITE setProperties) // constructors, destructor public: diff --git a/sources/qetproject.h b/sources/qetproject.h index 81f707dbd..ffe25e142 100644 --- a/sources/qetproject.h +++ b/sources/qetproject.h @@ -38,6 +38,7 @@ class XmlElementsCollection; class MoveElementsHandler; class MoveTitleBlockTemplatesHandler; class NumerotationContext; +class QUndoStack; /** This class represents a QET project. Typically saved as a .qet file, it diff --git a/sources/ui/conductorpropertiesdialog.cpp b/sources/ui/conductorpropertiesdialog.cpp index cf0189303..0b35752f9 100644 --- a/sources/ui/conductorpropertiesdialog.cpp +++ b/sources/ui/conductorpropertiesdialog.cpp @@ -18,11 +18,10 @@ #include "conductorpropertiesdialog.h" #include "ui_conductorpropertiesdialog.h" - #include "conductor.h" #include "conductorpropertieswidget.h" -#include "diagramcommands.h" #include "diagram.h" +#include "QPropertyUndoCommand/qpropertyundocommand.h" /** * @brief ConductorPropertiesDialog::ConductorPropertiesDialog @@ -58,36 +57,31 @@ ConductorPropertiesDialog::~ConductorPropertiesDialog() * @param conductor, conductor to edit propertie * @param parent, parent widget */ -void ConductorPropertiesDialog::PropertiesDialog(Conductor *conductor, QWidget *parent) { +void ConductorPropertiesDialog::PropertiesDialog(Conductor *conductor, QWidget *parent) +{ ConductorPropertiesDialog cpd (conductor, parent); - if (cpd.exec() == QDialog::Accepted && cpd.properties() != conductor->properties()) { + if (cpd.exec() == QDialog::Accepted && cpd.properties() != conductor->properties()) + { + QVariant old_value, new_value; + old_value.setValue(conductor->properties()); + new_value.setValue(cpd.properties()); - if (cpd.applyAll()) { - QList conductorslist = conductor -> relatedPotentialConductors().toList(); - conductorslist << conductor; - QList old_properties_list; + QPropertyUndoCommand *undo = new QPropertyUndoCommand(conductor, "properties", old_value, new_value); + undo->setText(tr("Modifier les propriétés d'un conducteur", "undo caption")); - foreach (Conductor *c, conductorslist) { - if (c == conductor) { - old_properties_list << conductor -> properties(); - } else { - old_properties_list << c -> properties(); - c -> setProperties( cpd.properties() ); - } + //Make undo for all related potiential conductors + if (cpd.applyAll() && !conductor->relatedPotentialConductors().isEmpty()) + { + undo->setText(tr("Modifier les propriétés de plusieurs conducteurs", "undo caption")); + foreach (Conductor *cond, conductor->relatedPotentialConductors()) + { + old_value.setValue(cond->properties()); + new QPropertyUndoCommand (cond, "properties", old_value, new_value, undo); } - //initialize the corresponding UndoCommand object - ChangeSeveralConductorsPropertiesCommand *cscpc = new ChangeSeveralConductorsPropertiesCommand(conductorslist); - cscpc -> setOldSettings(old_properties_list); - cscpc -> setNewSettings(cpd.properties()); - conductor -> diagram() -> undoStack().push(cscpc); - } else { - // initialize the corresponding UndoCommand object - ChangeConductorPropertiesCommand *ccpc = new ChangeConductorPropertiesCommand(conductor); - ccpc -> setOldSettings(conductor -> properties()); - ccpc -> setNewSettings(cpd.properties()); - conductor -> diagram() -> undoStack().push(ccpc); } + + conductor->diagram()->undoStack().push(undo); } } diff --git a/sources/ui/potentialtextsdialog.cpp b/sources/ui/potentialtextsdialog.cpp index c259de49f..0c0076f4d 100644 --- a/sources/ui/potentialtextsdialog.cpp +++ b/sources/ui/potentialtextsdialog.cpp @@ -19,6 +19,7 @@ #include "potentialtextsdialog.h" #include "ui_potentialtextsdialog.h" #include +#include /** * @brief PotentialTextsDialog::PotentialTextsDialog