Dynamic element text editor :

When add a new text, the item text in the tree view is selected and expanded.
When edit the pos of a text in the tree view, the new position is set directly, no need to lose focus or press enter key anymore for update the pos of the text.


git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5089 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2017-11-05 14:33:03 +00:00
parent 50fc46e162
commit 7fa942e3c1
5 changed files with 78 additions and 15 deletions

View File

@@ -52,6 +52,16 @@ QPropertyUndoCommand::QPropertyUndoCommand(QObject *object, const char *property
m_animate(false) 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 * @brief QPropertyUndoCommand::setNewValue
* Set the new value of the property (set with redo) to @new_value * Set the new value of the property (set with redo) to @new_value

View File

@@ -35,6 +35,7 @@ class QPropertyUndoCommand : public QUndoCommand
public: 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, const QVariant &new_value, QUndoCommand *parent = nullptr);
QPropertyUndoCommand(QObject *object, const char *property_name, const QVariant &old_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 setNewValue(const QVariant &new_value);
void enableAnimation (bool animate = true); void enableAnimation (bool animate = true);

View File

@@ -23,6 +23,7 @@
#include "diagram.h" #include "diagram.h"
#include "undocommand/deleteqgraphicsitemcommand.h" #include "undocommand/deleteqgraphicsitemcommand.h"
#include "undocommand/addelementtextcommand.h" #include "undocommand/addelementtextcommand.h"
#include "QPropertyUndoCommand/qpropertyundocommand.h"
#include <QTreeView> #include <QTreeView>
#include <QUndoCommand> #include <QUndoCommand>
@@ -78,7 +79,16 @@ void DynamicElementTextItemEditor::apply()
for (DynamicElementTextItem *deti : m_element->dynamicTextItems()) for (DynamicElementTextItem *deti : m_element->dynamicTextItems())
{ {
QUndoCommand *undo = m_model->undoForEditedText(deti); QUndoCommand *undo = m_model->undoForEditedText(deti);
if(undo->childCount())
if (undo->childCount() == 1)
{
QPropertyUndoCommand *quc = new QPropertyUndoCommand(static_cast<const QPropertyUndoCommand *>(undo->child(0)));
if (quc->text().isEmpty())
quc->setText(undo->text());
undo_list << quc;
delete undo;
}
else if(undo->childCount() > 1)
undo_list << undo; undo_list << undo;
else else
delete undo; delete undo;
@@ -90,7 +100,9 @@ void DynamicElementTextItemEditor::apply()
if(!undo_list.isEmpty() && m_element->diagram()) if(!undo_list.isEmpty() && m_element->diagram())
{ {
if (undo_list.size() == 1) if (undo_list.size() == 1)
{
m_element->diagram()->undoStack().push(undo_list.first()); m_element->diagram()->undoStack().push(undo_list.first());
}
else else
{ {
QUndoStack &us = m_element->diagram()->undoStack(); QUndoStack &us = m_element->diagram()->undoStack();
@@ -117,6 +129,7 @@ void DynamicElementTextItemEditor::setCurrentText(DynamicElementTextItem *text)
return; return;
m_tree_view->expand(index); m_tree_view->expand(index);
m_tree_view->expand(index.child(0,0));
m_tree_view->setCurrentIndex(index); 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_element->diagram()->undoStack().push(new AddElementTextCommand(m_element, deti));
m_model->addText(deti); m_model->addText(deti);
setCurrentText(deti);
} }
else else
{ {

View File

@@ -314,21 +314,33 @@ QUndoCommand *DynamicElementTextModel::undoForEditedText(DynamicElementTextItem
} }
int fs = text_qsi->child(1,1)->data(Qt::EditRole).toInt(); int fs = text_qsi->child(1,1)->data(Qt::EditRole).toInt();
if (fs != deti->fontSize()) if (fs != deti->fontSize())
new QPropertyUndoCommand(deti, "fontSize", QVariant(deti->fontSize()), QVariant(fs), undo); {
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(); QString tagg = text_qsi->child(2,1)->data(Qt::DisplayRole).toString();
if(tagg != deti->tagg()) 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<QColor>(); QColor color = text_qsi->child(3,1)->data(Qt::EditRole).value<QColor>();
if(color != deti->color()) 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(), QPointF p(text_qsi->child(4,1)->data(Qt::EditRole).toDouble(),
text_qsi->child(5,1)->data(Qt::EditRole).toDouble()); text_qsi->child(5,1)->data(Qt::EditRole).toDouble());
if(p != deti->pos()) 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; return undo;
@@ -598,6 +610,13 @@ QWidget *DynamicTextItemDelegate::createEditor(QWidget *parent, const QStyleOpti
cted->setObjectName("composite_text"); cted->setObjectName("composite_text");
return cted; return cted;
} }
case DynamicElementTextModel::size:
{
QSpinBox *sb = new QSpinBox(parent);
sb->setObjectName("font_size");
sb->setFrame(false);
return sb;
}
case DynamicElementTextModel::color: case DynamicElementTextModel::color:
{ {
QColorDialog *cd = new QColorDialog(index.data(Qt::EditRole).value<QColor>()); QColorDialog *cd = new QColorDialog(index.data(Qt::EditRole).value<QColor>());
@@ -606,15 +625,12 @@ QWidget *DynamicTextItemDelegate::createEditor(QWidget *parent, const QStyleOpti
} }
case DynamicElementTextModel::pos: case DynamicElementTextModel::pos:
{ {
QWidget *w = QStyledItemDelegate::createEditor(parent, option, index); QSpinBox *sb = new QSpinBox(parent);
sb->setObjectName("pos_dialog");
if(QDoubleSpinBox *dsb = dynamic_cast<QDoubleSpinBox *>(w)) sb->setRange(-1000,10000);
{ sb->setFrame(false);
dsb->setDecimals(0); sb->setSuffix(" px");
dsb->setSuffix(" px"); return sb;
}
return w;
} }
} }
return QStyledItemDelegate::createEditor(parent, option, index); return QStyledItemDelegate::createEditor(parent, option, index);
@@ -685,6 +701,24 @@ void DynamicTextItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *
QStyledItemDelegate::setModelData(editor, model, index); 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<QSpinBox *>(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 * @brief DynamicTextItemDelegate::availableInfo
* @param deti * @param deti

View File

@@ -79,6 +79,9 @@ class DynamicTextItemDelegate : public QStyledItemDelegate
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setModelData(QWidget *editor, QAbstractItemModel *model, 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: private:
QStringList availableInfo(DynamicElementTextItem *deti) const; QStringList availableInfo(DynamicElementTextItem *deti) const;
}; };