diff --git a/sources/QPropertyUndoCommand/qpropertyundocommand.cpp b/sources/QPropertyUndoCommand/qpropertyundocommand.cpp index f29b466ce..6cb06c629 100644 --- a/sources/QPropertyUndoCommand/qpropertyundocommand.cpp +++ b/sources/QPropertyUndoCommand/qpropertyundocommand.cpp @@ -52,6 +52,16 @@ QPropertyUndoCommand::QPropertyUndoCommand(QObject *object, const char *property m_animate(false) {} +QPropertyUndoCommand::QPropertyUndoCommand(const QPropertyUndoCommand *other) +{ + m_object = other->m_object; + m_property_name = other->m_property_name; + m_old_value = other->m_old_value; + m_new_value = other->m_new_value; + m_animate = other->m_animate; + setText(other->text()); +} + /** * @brief QPropertyUndoCommand::setNewValue * Set the new value of the property (set with redo) to @new_value diff --git a/sources/QPropertyUndoCommand/qpropertyundocommand.h b/sources/QPropertyUndoCommand/qpropertyundocommand.h index 90b46df58..9098422a4 100644 --- a/sources/QPropertyUndoCommand/qpropertyundocommand.h +++ b/sources/QPropertyUndoCommand/qpropertyundocommand.h @@ -35,6 +35,7 @@ class QPropertyUndoCommand : public QUndoCommand public: QPropertyUndoCommand(QObject *object, const char *property_name, const QVariant &old_value, const QVariant &new_value, QUndoCommand *parent = nullptr); QPropertyUndoCommand(QObject *object, const char *property_name, const QVariant &old_value, QUndoCommand *parent = nullptr); + QPropertyUndoCommand(const QPropertyUndoCommand *other); void setNewValue(const QVariant &new_value); void enableAnimation (bool animate = true); diff --git a/sources/ui/dynamicelementtextitemeditor.cpp b/sources/ui/dynamicelementtextitemeditor.cpp index 9a63b92ca..ba21294cd 100644 --- a/sources/ui/dynamicelementtextitemeditor.cpp +++ b/sources/ui/dynamicelementtextitemeditor.cpp @@ -23,6 +23,7 @@ #include "diagram.h" #include "undocommand/deleteqgraphicsitemcommand.h" #include "undocommand/addelementtextcommand.h" +#include "QPropertyUndoCommand/qpropertyundocommand.h" #include #include @@ -78,7 +79,16 @@ void DynamicElementTextItemEditor::apply() for (DynamicElementTextItem *deti : m_element->dynamicTextItems()) { QUndoCommand *undo = m_model->undoForEditedText(deti); - if(undo->childCount()) + + if (undo->childCount() == 1) + { + QPropertyUndoCommand *quc = new QPropertyUndoCommand(static_cast(undo->child(0))); + if (quc->text().isEmpty()) + quc->setText(undo->text()); + undo_list << quc; + delete undo; + } + else if(undo->childCount() > 1) undo_list << undo; else delete undo; @@ -90,7 +100,9 @@ void DynamicElementTextItemEditor::apply() if(!undo_list.isEmpty() && m_element->diagram()) { if (undo_list.size() == 1) + { m_element->diagram()->undoStack().push(undo_list.first()); + } else { QUndoStack &us = m_element->diagram()->undoStack(); @@ -117,6 +129,7 @@ void DynamicElementTextItemEditor::setCurrentText(DynamicElementTextItem *text) return; m_tree_view->expand(index); + m_tree_view->expand(index.child(0,0)); m_tree_view->setCurrentIndex(index); } @@ -157,6 +170,8 @@ void DynamicElementTextItemEditor::on_m_add_text_clicked() { m_element->diagram()->undoStack().push(new AddElementTextCommand(m_element, deti)); m_model->addText(deti); + + setCurrentText(deti); } else { diff --git a/sources/ui/dynamicelementtextmodel.cpp b/sources/ui/dynamicelementtextmodel.cpp index a799f9cf5..18570aae1 100644 --- a/sources/ui/dynamicelementtextmodel.cpp +++ b/sources/ui/dynamicelementtextmodel.cpp @@ -314,21 +314,33 @@ QUndoCommand *DynamicElementTextModel::undoForEditedText(DynamicElementTextItem } int fs = text_qsi->child(1,1)->data(Qt::EditRole).toInt(); - if (fs != deti->fontSize()) - new QPropertyUndoCommand(deti, "fontSize", QVariant(deti->fontSize()), QVariant(fs), undo); + if (fs != deti->fontSize()) + { + QUndoCommand *quc = new QPropertyUndoCommand(deti, "fontSize", QVariant(deti->fontSize()), QVariant(fs), undo); + quc->setText(tr("Modifier la taille d'un texte d'élément")); + } QString tagg = text_qsi->child(2,1)->data(Qt::DisplayRole).toString(); if(tagg != deti->tagg()) - new QPropertyUndoCommand(deti, "tagg", QVariant(deti->tagg()), QVariant(tagg), undo); + { + QUndoCommand *quc = new QPropertyUndoCommand(deti, "tagg", QVariant(deti->tagg()), QVariant(tagg), undo); + quc->setText(tr("Modifier le tagg d'un texte d'élément")); + } QColor color = text_qsi->child(3,1)->data(Qt::EditRole).value(); if(color != deti->color()) - new QPropertyUndoCommand(deti, "color", QVariant(deti->color()), QVariant(color), undo); + { + QUndoCommand *quc = new QPropertyUndoCommand(deti, "color", QVariant(deti->color()), QVariant(color), undo); + quc->setText(tr("Modifier la couleur d'un texte d'élément")); + } QPointF p(text_qsi->child(4,1)->data(Qt::EditRole).toDouble(), text_qsi->child(5,1)->data(Qt::EditRole).toDouble()); if(p != deti->pos()) - new QPropertyUndoCommand(deti, "pos", QVariant(deti->pos()), QVariant(p), undo); + { + QPropertyUndoCommand *quc = new QPropertyUndoCommand(deti, "pos", QVariant(deti->pos()), QVariant(p), undo); + quc->setText(tr("Déplacer un texte d'élément")); + } return undo; @@ -598,6 +610,13 @@ QWidget *DynamicTextItemDelegate::createEditor(QWidget *parent, const QStyleOpti cted->setObjectName("composite_text"); return cted; } + case DynamicElementTextModel::size: + { + QSpinBox *sb = new QSpinBox(parent); + sb->setObjectName("font_size"); + sb->setFrame(false); + return sb; + } case DynamicElementTextModel::color: { QColorDialog *cd = new QColorDialog(index.data(Qt::EditRole).value()); @@ -606,15 +625,12 @@ QWidget *DynamicTextItemDelegate::createEditor(QWidget *parent, const QStyleOpti } case DynamicElementTextModel::pos: { - QWidget *w = QStyledItemDelegate::createEditor(parent, option, index); - - if(QDoubleSpinBox *dsb = dynamic_cast(w)) - { - dsb->setDecimals(0); - dsb->setSuffix(" px"); - } - - return w; + QSpinBox *sb = new QSpinBox(parent); + sb->setObjectName("pos_dialog"); + sb->setRange(-1000,10000); + sb->setFrame(false); + sb->setSuffix(" px"); + return sb; } } return QStyledItemDelegate::createEditor(parent, option, index); @@ -685,6 +701,24 @@ void DynamicTextItemDelegate::setModelData(QWidget *editor, QAbstractItemModel * QStyledItemDelegate::setModelData(editor, model, index); } +bool DynamicTextItemDelegate::eventFilter(QObject *object, QEvent *event) +{ + //This is a bad hack, for change the normal behavior : + //in normal behavior, the value is commited when the spinbox lose focus or enter key is pressed + //With this hack the value is commited each time the value change, so the text is moved in live. + //We also use this hack for the font size spinbox + if(object->objectName() == "pos_dialog" || object->objectName() == "font_size") + { + QSpinBox *sb = static_cast(object); + if(event->type() == QEvent::KeyRelease) + emit commitData(sb); + else if (event->type() == QEvent::MouseButtonRelease) + emit commitData(sb); + } + + return QStyledItemDelegate::eventFilter(object, event); +} + /** * @brief DynamicTextItemDelegate::availableInfo * @param deti diff --git a/sources/ui/dynamicelementtextmodel.h b/sources/ui/dynamicelementtextmodel.h index 39ca9abcb..7ea905d3c 100644 --- a/sources/ui/dynamicelementtextmodel.h +++ b/sources/ui/dynamicelementtextmodel.h @@ -79,6 +79,9 @@ class DynamicTextItemDelegate : public QStyledItemDelegate QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; + protected: + bool eventFilter(QObject *object, QEvent *event) override; + private: QStringList availableInfo(DynamicElementTextItem *deti) const; };