From 50fc46e162e02afc62f73265de8010640366713c Mon Sep 17 00:00:00 2001 From: blacksun Date: Sun, 29 Oct 2017 10:43:34 +0000 Subject: [PATCH] Diagram editor : add in the tree widget use to edit the property of dynamic text item, two news items for edit the X and Y pos of the text. git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5088 bfdf4180-ca20-0410-9c96-a3a8aa849046 --- sources/ui/dynamicelementtextitemeditor.cpp | 16 +++++ sources/ui/dynamicelementtextitemeditor.h | 1 + sources/ui/dynamicelementtextmodel.cpp | 69 ++++++++++++++++++++- sources/ui/dynamicelementtextmodel.h | 6 +- 4 files changed, 87 insertions(+), 5 deletions(-) diff --git a/sources/ui/dynamicelementtextitemeditor.cpp b/sources/ui/dynamicelementtextitemeditor.cpp index c30b6fc93..9a63b92ca 100644 --- a/sources/ui/dynamicelementtextitemeditor.cpp +++ b/sources/ui/dynamicelementtextitemeditor.cpp @@ -120,6 +120,22 @@ void DynamicElementTextItemEditor::setCurrentText(DynamicElementTextItem *text) m_tree_view->setCurrentIndex(index); } +QUndoCommand *DynamicElementTextItemEditor::associatedUndo() const +{ + QUndoCommand *parent_undo = new QUndoCommand(tr("Modifier un texte d'élément")); + for (DynamicElementTextItem *deti : m_element->dynamicTextItems()) + m_model->undoForEditedText(deti, parent_undo); + + if(parent_undo->childCount() >= 1) + { + if(parent_undo->childCount() >= 2) + parent_undo->setText(tr("Modifier %1 textes d'élément").arg(QString::number(parent_undo->childCount()))); + return parent_undo; + } + else + return nullptr; +} + void DynamicElementTextItemEditor::dataEdited(DynamicElementTextItem *deti) { Q_UNUSED(deti) diff --git a/sources/ui/dynamicelementtextitemeditor.h b/sources/ui/dynamicelementtextitemeditor.h index ecd72cd84..2562ee504 100644 --- a/sources/ui/dynamicelementtextitemeditor.h +++ b/sources/ui/dynamicelementtextitemeditor.h @@ -42,6 +42,7 @@ class DynamicElementTextItemEditor : public AbstractElementPropertiesEditorWidge bool setLiveEdit(bool live_edit) override; void apply() override; void setCurrentText(DynamicElementTextItem *text); + QUndoCommand *associatedUndo() const override; private: void dataEdited(DynamicElementTextItem *deti); diff --git a/sources/ui/dynamicelementtextmodel.cpp b/sources/ui/dynamicelementtextmodel.cpp index ffc8d515e..a799f9cf5 100644 --- a/sources/ui/dynamicelementtextmodel.cpp +++ b/sources/ui/dynamicelementtextmodel.cpp @@ -160,7 +160,35 @@ void DynamicElementTextModel::addText(DynamicElementTextItem *deti) qsi_list.clear(); qsi_list << color << colora; qsi->appendRow(qsi_list); + + //X pos + QStandardItem *x_pos = new QStandardItem(tr("Position X")); + x_pos->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); + + QStandardItem *x_pos_a = new QStandardItem; + x_pos_a->setData(deti->pos().x(), Qt::EditRole); + x_pos_a->setData(DynamicElementTextModel::pos, Qt::UserRole+1); + x_pos_a->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable); + + qsi_list.clear(); + qsi_list << x_pos << x_pos_a; + qsi->appendRow(qsi_list); + + //Y pos + QStandardItem *y_pos = new QStandardItem(tr("Position Y")); + y_pos->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); + + QStandardItem *y_pos_a = new QStandardItem; + y_pos_a->setData(deti->pos().y(), Qt::EditRole); + y_pos_a->setData(DynamicElementTextModel::pos, Qt::UserRole+1); + y_pos_a->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable); + + qsi_list.clear(); + qsi_list << y_pos << y_pos_a; + qsi->appendRow(qsi_list); + + qsi_list.clear(); QStandardItem *empty_qsi = new QStandardItem(0); empty_qsi->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); @@ -244,9 +272,14 @@ QModelIndex DynamicElementTextModel::indexFromText(DynamicElementTextItem *text) * Each change made for @deti is append as a child of the returned QUndoCommand. * In other word, if the returned QUndoCommand have no child, that mean there is no change. */ -QUndoCommand *DynamicElementTextModel::undoForEditedText(DynamicElementTextItem *deti) const +QUndoCommand *DynamicElementTextModel::undoForEditedText(DynamicElementTextItem *deti, QUndoCommand *parent_undo) const { - QUndoCommand *undo = new QUndoCommand(tr("Éditer un texte d'élément")); + + QUndoCommand *undo = nullptr; + if(parent_undo) + undo = parent_undo; + else + undo = new QUndoCommand(tr("Éditer un texte d'élément")); if (!m_texts_list.contains(deti)) return undo; @@ -292,6 +325,12 @@ QUndoCommand *DynamicElementTextModel::undoForEditedText(DynamicElementTextItem if(color != deti->color()) new QPropertyUndoCommand(deti, "color", QVariant(deti->color()), QVariant(color), undo); + 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); + + return undo; } @@ -381,7 +420,7 @@ void DynamicElementTextModel::itemDataChanged(QStandardItem *qsi) //We emit the signal only if @qsi is in the second column, because the data are stored on this column //the first column is use only for display the title of the property - if(qsi->column() == 1) + if(qsi->column() == 1 && !m_block_dataForTextChanged) emit dataForTextChanged(deti); } @@ -406,6 +445,8 @@ void DynamicElementTextModel::setConnection(DynamicElementTextItem *deti, bool s connection_list << connect(deti, &DynamicElementTextItem::textFromChanged, [deti,this](){this->updateDataFromText(deti, textFrom);}); connection_list << connect(deti, &DynamicElementTextItem::textChanged, [deti,this](){this->updateDataFromText(deti, userText);}); connection_list << connect(deti, &DynamicElementTextItem::infoNameChanged, [deti,this](){this->updateDataFromText(deti, infoText);}); + connection_list << connect(deti, &DynamicElementTextItem::xChanged, [deti,this](){this->updateDataFromText(deti, pos);}); + connection_list << connect(deti, &DynamicElementTextItem::yChanged, [deti,this](){this->updateDataFromText(deti, pos);}); connection_list << connect(deti, &DynamicElementTextItem::compositeTextChanged, [deti, this]() {this->updateDataFromText(deti, compositeText);}); m_hash_text_connect.insert(deti, connection_list); @@ -428,6 +469,8 @@ void DynamicElementTextModel::updateDataFromText(DynamicElementTextItem *deti, V if (!qsi) return; + m_block_dataForTextChanged = true; + switch (type) { case textFrom: @@ -480,7 +523,15 @@ void DynamicElementTextModel::updateDataFromText(DynamicElementTextItem *deti, V qsi->child(3,1)->setData(deti->color(), Qt::ForegroundRole); break; } + case pos: + { + qsi->child(4,1)->setData(deti->pos().x(), Qt::EditRole); + qsi->child(5,1)->setData(deti->pos().y(), Qt::EditRole); + break; + } } + + m_block_dataForTextChanged = false; } @@ -553,6 +604,18 @@ QWidget *DynamicTextItemDelegate::createEditor(QWidget *parent, const QStyleOpti cd->setObjectName("color_dialog"); return cd; } + case DynamicElementTextModel::pos: + { + QWidget *w = QStyledItemDelegate::createEditor(parent, option, index); + + if(QDoubleSpinBox *dsb = dynamic_cast(w)) + { + dsb->setDecimals(0); + dsb->setSuffix(" px"); + } + + return w; + } } return QStyledItemDelegate::createEditor(parent, option, index); } diff --git a/sources/ui/dynamicelementtextmodel.h b/sources/ui/dynamicelementtextmodel.h index 87aaae3c5..39ca9abcb 100644 --- a/sources/ui/dynamicelementtextmodel.h +++ b/sources/ui/dynamicelementtextmodel.h @@ -42,7 +42,8 @@ class DynamicElementTextModel : public QStandardItemModel compositeText, size, tagg, - color + color, + pos }; DynamicElementTextModel(QObject *parent = nullptr); @@ -53,7 +54,7 @@ class DynamicElementTextModel : public QStandardItemModel DynamicElementTextItem *textFromIndex(const QModelIndex &index) const; DynamicElementTextItem *textFromItem(QStandardItem *item) const; QModelIndex indexFromText(DynamicElementTextItem *text) const; - QUndoCommand *undoForEditedText(DynamicElementTextItem *deti) const; + QUndoCommand *undoForEditedText(DynamicElementTextItem *deti, QUndoCommand *parent_undo = nullptr) const; signals: void dataForTextChanged(DynamicElementTextItem *text); @@ -67,6 +68,7 @@ class DynamicElementTextModel : public QStandardItemModel private: QHash m_texts_list; QHash > m_hash_text_connect; + bool m_block_dataForTextChanged = false; }; class DynamicTextItemDelegate : public QStyledItemDelegate