style editor : use QPropertyUndoCommand instead of ChangePartCommand.

Remove ChangePartCommand class.


git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4073 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2015-07-24 13:41:51 +00:00
parent 58d247ced7
commit 871d50c014
6 changed files with 44 additions and 197 deletions

View File

@@ -302,64 +302,6 @@ void AddPartCommand::redo() {
editor_scene_ -> addItem(part); 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 Constructeur
@param element_scene Element edite @param element_scene Element edite

View File

@@ -169,36 +169,6 @@ class AddPartCommand : public ElementEditionCommand {
bool first_redo; 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. This command changes the translated names of an electrical element.
*/ */

View File

@@ -45,68 +45,6 @@ QUndoStack &ElementItemEditor::undoStack() const {
return(elementScene() -> undoStack()); 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<CustomElementPart *> part_list, const char *property, const QVariant &new_value)
{
if (part_list.isEmpty()) return;
//Get only the parts concerned by modification
QList <CustomElementPart *> 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 /// @return Le nom du type d'element edite
QString ElementItemEditor::elementTypeName() const { QString ElementItemEditor::elementTypeName() const {
return(element_type_name); return(element_type_name);

View File

@@ -46,9 +46,6 @@ class ElementItemEditor : public QWidget
virtual ElementScene *elementScene() const; virtual ElementScene *elementScene() const;
virtual QUndoStack &undoStack() const; virtual QUndoStack &undoStack() const;
virtual void addChangePartCommand(const QString &, CustomElementPart *, const char *, const QVariant &);
virtual void addChangePartCommand(const QString &, QList<CustomElementPart *>, const char *, const QVariant &);
virtual QString elementTypeName() const; virtual QString elementTypeName() const;
virtual void setElementTypeName(const QString &); virtual void setElementTypeName(const QString &);
virtual void detach(); virtual void detach();

View File

@@ -17,6 +17,7 @@
*/ */
#include "styleeditor.h" #include "styleeditor.h"
#include "customelementgraphicpart.h" #include "customelementgraphicpart.h"
#include "QPropertyUndoCommand/qpropertyundocommand.h"
/** /**
Constructeur Constructeur
@@ -119,62 +120,30 @@ StyleEditor::StyleEditor(QETElementEditor *editor, CustomElementGraphicPart *p,
/// Destructeur /// Destructeur
StyleEditor::~StyleEditor() { 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 /// Update antialiasing with undo command
void StyleEditor::updatePartAntialiasing() void StyleEditor::updatePartAntialiasing() {
{ makeUndo(tr("style antialiasing"), "antialias", antialiasing -> isChecked());
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());
} }
/// Update color with undo command /// Update color with undo command
void StyleEditor::updatePartColor() void StyleEditor::updatePartColor() {
{ makeUndo(tr("style couleur"),"color", outline_color->itemData(outline_color -> currentIndex()));
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()));
} }
/// Update style with undo command /// Update style with undo command
void StyleEditor::updatePartLineStyle() void StyleEditor::updatePartLineStyle() {
{ makeUndo(tr("style ligne"), "line_style", line_style->itemData(line_style -> currentIndex()));
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()));
} }
/// Update weight with undo command /// Update weight with undo command
void StyleEditor::updatePartLineWeight() void StyleEditor::updatePartLineWeight() {
{ makeUndo(tr("style epaisseur"), "line_weight", size_weight->itemData(size_weight -> currentIndex()));
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()));
} }
/// Update color filling with undo command /// Update color filling with undo command
void StyleEditor::updatePartFilling() void StyleEditor::updatePartFilling() {
{ makeUndo(tr("style remplissage"), "filling", filling_color->itemData(filling_color -> currentIndex()));
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()));
} }
/** /**
@@ -320,3 +289,29 @@ void StyleEditor::activeConnections(bool active) {
disconnect(antialiasing, SIGNAL(stateChanged(int)), this, SLOT(updatePartAntialiasing())); 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);
}
}

View File

@@ -17,9 +17,14 @@
*/ */
#ifndef STYLE_EDITOR_H #ifndef STYLE_EDITOR_H
#define STYLE_EDITOR_H #define STYLE_EDITOR_H
#include <QtWidgets>
#include "elementitemeditor.h" #include "elementitemeditor.h"
class CustomElementGraphicPart; class CustomElementGraphicPart;
class QVBoxLayout;
class QCheckBox;
class QComboBox;
/** /**
This class provides a widget to edit styles (color, pen style and thickness, This class provides a widget to edit styles (color, pen style and thickness,
filling, antialiasing) common to most primitives within the element editor. filling, antialiasing) common to most primitives within the element editor.
@@ -55,7 +60,6 @@ class StyleEditor : public ElementItemEditor
static bool isStyleEditable (QList <CustomElementPart *> cep_list); static bool isStyleEditable (QList <CustomElementPart *> cep_list);
public slots: public slots:
void updatePart();
void updateForm(); void updateForm();
void updatePartAntialiasing(); void updatePartAntialiasing();
void updatePartColor(); void updatePartColor();
@@ -65,5 +69,6 @@ class StyleEditor : public ElementItemEditor
private: private:
void activeConnections(bool); void activeConnections(bool);
void makeUndo(const QString &undo_text, const char *property_name, const QVariant &new_value);
}; };
#endif #endif