diff --git a/sources/editor/editorcommands.cpp b/sources/editor/editorcommands.cpp index b0a196158..1e34ee002 100644 --- a/sources/editor/editorcommands.cpp +++ b/sources/editor/editorcommands.cpp @@ -302,64 +302,6 @@ void AddPartCommand::redo() { editor_scene_ -> addItem(part); } -/** - Constructeur - @param name nom de la propriete modifiee - @param part partie modifiee - @param prop propriete modifiee - @param old_v ancienne valeur - @param new_v nouvelle valeur - @param parent qUndoCommand parent -*/ -ChangePartCommand::ChangePartCommand( - const QString &name, - CustomElementPart *part, - const char *prop, - const QVariant &old_v, - const QVariant &new_v, - QUndoCommand *parent -) : - ElementEditionCommand(QString(QObject::tr("modification %1", "undo caption")).arg(name), 0, 0, parent), - cep(part), - property(prop), - m_old_value(old_v), - m_new_value(new_v) -{ -} - -ChangePartCommand::ChangePartCommand(const QString &part_name, CustomElementPart *part, const char *property_name, const QVariant &old_value, QUndoCommand *parent) : - ElementEditionCommand(QString(QObject::tr("modification %1", "undo caption")).arg(part_name), 0, 0, parent), - cep(part), - property(property_name), - m_old_value(old_value) -{} - -/// Destructeur -ChangePartCommand::~ChangePartCommand() { -} - -void ChangePartCommand::setNewValue(const QVariant &new_value) { - m_new_value = new_value; -} - -/** - * @brief ChangePartCommand::undo - */ -void ChangePartCommand::undo() -{ - cep -> setProperty(property, m_old_value); - ElementEditionCommand::undo(); -} - -/** - * @brief ChangePartCommand::redo - */ -void ChangePartCommand::redo() -{ - cep -> setProperty(property, m_new_value); - ElementEditionCommand::redo(); -} - /** Constructeur @param element_scene Element edite diff --git a/sources/editor/editorcommands.h b/sources/editor/editorcommands.h index b74ba2717..61b902c06 100644 --- a/sources/editor/editorcommands.h +++ b/sources/editor/editorcommands.h @@ -169,36 +169,6 @@ class AddPartCommand : public ElementEditionCommand { bool first_redo; }; -/** - This command changes a property of a primitive when editing an electrical - element. -*/ -class ChangePartCommand : public ElementEditionCommand -{ - // constructors, destructor - public: - ChangePartCommand(const QString &, CustomElementPart *, const char *, const QVariant &, const QVariant &, QUndoCommand * = 0); - ChangePartCommand(const QString &part_name, CustomElementPart *part, const char *property_name, const QVariant &old_value, QUndoCommand *parent = 0); - virtual ~ChangePartCommand(); - - void setNewValue(const QVariant &new_value); - - private: - ChangePartCommand(const ChangePartCommand &); - - // methods - public: - virtual void undo(); - virtual void redo(); - - // attributes - private: - CustomElementPart *cep; - const char *property; - QVariant m_old_value; - QVariant m_new_value; -}; - /** This command changes the translated names of an electrical element. */ diff --git a/sources/editor/elementitemeditor.cpp b/sources/editor/elementitemeditor.cpp index 77a0e15b9..f8ca1e2dd 100644 --- a/sources/editor/elementitemeditor.cpp +++ b/sources/editor/elementitemeditor.cpp @@ -45,68 +45,6 @@ QUndoStack &ElementItemEditor::undoStack() const { return(elementScene() -> undoStack()); } -/** - Ajoute une ChangePartCommand a l'UndoStack. L'ancienne valeur sera - automatiquement recuperee. A noter que cette methode ne fait rien si - l'ancienne valeur et la nouvelle sont egales ou encore si part vaut 0 - @param desc nom de la propriete modifiee - @param part partie modifiee - @param prop propriete modifiee - @param new_v nouvelle valeur -*/ -void ElementItemEditor::addChangePartCommand(const QString &desc, CustomElementPart *part, const char *prop, const QVariant &new_v) { - // ne fait rien si part vaut 0 - if (!part) return; - - // recupere l'ancienne valeur - QVariant old_v = part -> property(prop); - - // ne fait rien si l'ancienne valeur et la nouvelle sont egales - if (old_v == new_v) return; - - undoStack().push( - new ChangePartCommand( - desc + " " + element_type_name, - part, - prop, - old_v, - new_v - ) - ); -} - -/** - * @brief ElementItemEditor::addChangePartCommand - * Add a ChangePartCommand with child for each part of part_list to the undo stack - * @param undo_text : text of undo command to display - * @param part_list : list of parts to modify - * @param property : QProperty (of CustomElementPart) to modify - * @param new_value : the new value of the QProperty - */ -void ElementItemEditor::addChangePartCommand(const QString &undo_text, QList part_list, const char *property, const QVariant &new_value) -{ - if (part_list.isEmpty()) return; - - //Get only the parts concerned by modification - QList updated_part; - foreach (CustomElementPart *cep, part_list) - if (cep->property(property) != new_value) - updated_part << cep; - - //There is not part to modify - if(updated_part.isEmpty()) return; - - //Set the first part has parent undo - CustomElementPart *p_cep = updated_part.takeFirst(); - QUndoCommand *parent_undo = new ChangePartCommand (undo_text, p_cep, property, p_cep->property(property), new_value); - - //And other parts are just child of parent - foreach (CustomElementPart *cep, updated_part) - new ChangePartCommand (undo_text, cep, property, cep->property(property), new_value, parent_undo); - - undoStack().push(parent_undo); -} - /// @return Le nom du type d'element edite QString ElementItemEditor::elementTypeName() const { return(element_type_name); diff --git a/sources/editor/elementitemeditor.h b/sources/editor/elementitemeditor.h index aee0baf9d..c698bc3f9 100644 --- a/sources/editor/elementitemeditor.h +++ b/sources/editor/elementitemeditor.h @@ -46,9 +46,6 @@ class ElementItemEditor : public QWidget virtual ElementScene *elementScene() const; virtual QUndoStack &undoStack() const; - virtual void addChangePartCommand(const QString &, CustomElementPart *, const char *, const QVariant &); - virtual void addChangePartCommand(const QString &, QList, const char *, const QVariant &); - virtual QString elementTypeName() const; virtual void setElementTypeName(const QString &); virtual void detach(); diff --git a/sources/editor/styleeditor.cpp b/sources/editor/styleeditor.cpp index 51ced350b..63d2508a4 100644 --- a/sources/editor/styleeditor.cpp +++ b/sources/editor/styleeditor.cpp @@ -17,6 +17,7 @@ */ #include "styleeditor.h" #include "customelementgraphicpart.h" +#include "QPropertyUndoCommand/qpropertyundocommand.h" /** Constructeur @@ -119,62 +120,30 @@ StyleEditor::StyleEditor(QETElementEditor *editor, CustomElementGraphicPart *p, /// Destructeur StyleEditor::~StyleEditor() { } -/** - * @brief StyleEditor::updatePart - * Update the part from the content of the form - */ -void StyleEditor::updatePart() { - if (!part) return; - part->setProperty("antialias", antialiasing -> isChecked()); - part->setProperty("color", outline_color -> itemData(outline_color -> currentIndex())); - part->setProperty("line_style", line_style -> itemData(line_style -> currentIndex())); - part->setProperty("line_weight", size_weight -> itemData(size_weight -> currentIndex())); - part->setProperty("filling", filling_color -> itemData(filling_color -> currentIndex())); -} /// Update antialiasing with undo command -void StyleEditor::updatePartAntialiasing() -{ - if (part) - addChangePartCommand(tr("style antialiasing"), part, "antialias", antialiasing -> isChecked()); - else if (!m_part_list.isEmpty()) - addChangePartCommand(tr("style antialiasing"), m_cep_list, "antialias", antialiasing -> isChecked()); +void StyleEditor::updatePartAntialiasing() { + makeUndo(tr("style antialiasing"), "antialias", antialiasing -> isChecked()); } /// Update color with undo command -void StyleEditor::updatePartColor() -{ - if (part) - addChangePartCommand(tr("style couleur"), part, "color", outline_color->itemData(outline_color -> currentIndex())); - else if (!m_part_list.isEmpty()) - addChangePartCommand(tr("style couleur"), m_cep_list, "color", outline_color->itemData(outline_color -> currentIndex())); +void StyleEditor::updatePartColor() { + makeUndo(tr("style couleur"),"color", outline_color->itemData(outline_color -> currentIndex())); } /// Update style with undo command -void StyleEditor::updatePartLineStyle() -{ - if (part) - addChangePartCommand(tr("style ligne"), part, "line_style", line_style->itemData(line_style -> currentIndex())); - else if (!m_part_list.isEmpty()) - addChangePartCommand(tr("style ligne"), m_cep_list, "line_style", line_style->itemData(line_style -> currentIndex())); +void StyleEditor::updatePartLineStyle() { + makeUndo(tr("style ligne"), "line_style", line_style->itemData(line_style -> currentIndex())); } /// Update weight with undo command -void StyleEditor::updatePartLineWeight() -{ - if (part) - addChangePartCommand(tr("style epaisseur"), part, "line_weight", size_weight->itemData(size_weight -> currentIndex())); - else if (!m_part_list.isEmpty()) - addChangePartCommand(tr("style epaisseur"), m_cep_list, "line_weight", size_weight->itemData(size_weight -> currentIndex())); +void StyleEditor::updatePartLineWeight() { + makeUndo(tr("style epaisseur"), "line_weight", size_weight->itemData(size_weight -> currentIndex())); } /// Update color filling with undo command -void StyleEditor::updatePartFilling() -{ - if(part) - addChangePartCommand(tr("style remplissage"), part, "filling", filling_color->itemData(filling_color -> currentIndex())); - else if (!m_part_list.isEmpty()) - addChangePartCommand(tr("style remplissage"), m_cep_list, "filling", filling_color->itemData(filling_color -> currentIndex())); +void StyleEditor::updatePartFilling() { + makeUndo(tr("style remplissage"), "filling", filling_color->itemData(filling_color -> currentIndex())); } /** @@ -320,3 +289,29 @@ void StyleEditor::activeConnections(bool active) { disconnect(antialiasing, SIGNAL(stateChanged(int)), this, SLOT(updatePartAntialiasing())); } } + +void StyleEditor::makeUndo(const QString &undo_text, const char *property_name, const QVariant &new_value) +{ + QPropertyUndoCommand *undo = nullptr; + if (part && (new_value != part->property(property_name))) + { + undo = new QPropertyUndoCommand(part, property_name, part->property(property_name), new_value); + undo->setText(undo_text); + undoStack().push(undo); + return; + } + else if (!m_part_list.isEmpty()) + { + foreach (CustomElementGraphicPart *cegp, m_part_list) + { + if (!undo) + { + undo = new QPropertyUndoCommand(cegp, property_name, cegp->property(property_name), new_value); + undo->setText(undo_text); + } + else + new QPropertyUndoCommand(cegp, property_name, cegp->property(property_name), new_value, undo); + } + undoStack().push(undo); + } +} diff --git a/sources/editor/styleeditor.h b/sources/editor/styleeditor.h index 695750638..43e27496f 100644 --- a/sources/editor/styleeditor.h +++ b/sources/editor/styleeditor.h @@ -17,9 +17,14 @@ */ #ifndef STYLE_EDITOR_H #define STYLE_EDITOR_H -#include + #include "elementitemeditor.h" + class CustomElementGraphicPart; +class QVBoxLayout; +class QCheckBox; +class QComboBox; + /** This class provides a widget to edit styles (color, pen style and thickness, filling, antialiasing) common to most primitives within the element editor. @@ -55,7 +60,6 @@ class StyleEditor : public ElementItemEditor static bool isStyleEditable (QList cep_list); public slots: - void updatePart(); void updateForm(); void updatePartAntialiasing(); void updatePartColor(); @@ -65,5 +69,6 @@ class StyleEditor : public ElementItemEditor private: void activeConnections(bool); + void makeUndo(const QString &undo_text, const char *property_name, const QVariant &new_value); }; #endif