mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-01-02 09:50:52 +01:00
First step for the dynamic element text : Now user can add directly from the diagram editor an editable text of an element.
git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5005 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
#include "imagepropertieswidget.h"
|
||||
#include "qetshapeitem.h"
|
||||
#include "shapegraphicsitempropertieswidget.h"
|
||||
#include "dynamicelementtextitem.h"
|
||||
|
||||
/**
|
||||
* @brief DiagramPropertiesEditorDockWidget::DiagramPropertiesEditorDockWidget
|
||||
@@ -56,7 +57,7 @@ void DiagramPropertiesEditorDockWidget::setDiagram(Diagram *diagram)
|
||||
if (diagram)
|
||||
{
|
||||
m_diagram = diagram;
|
||||
connect(m_diagram, SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
|
||||
connect(m_diagram, SIGNAL(selectionChanged()), this, SLOT(selectionChanged()), Qt::QueuedConnection);
|
||||
connect(m_diagram, SIGNAL(destroyed()), this, SLOT(diagramWasDeleted()));
|
||||
selectionChanged();
|
||||
}
|
||||
@@ -119,6 +120,22 @@ void DiagramPropertiesEditorDockWidget::selectionChanged()
|
||||
m_edited_qgi_type = type_;
|
||||
addEditor(new ShapeGraphicsItemPropertiesWidget(static_cast<QetShapeItem*>(item), this));
|
||||
break; }
|
||||
|
||||
case DynamicElementTextItem::Type: {
|
||||
DynamicElementTextItem *deti = static_cast<DynamicElementTextItem *>(item);
|
||||
|
||||
//For dynamic element text, we open the element editor
|
||||
//We already edit an element, just update the editor with a new element
|
||||
if (m_edited_qgi_type == Element::Type)
|
||||
{
|
||||
static_cast<ElementPropertiesWidget*>(editors().first())->setElement(deti->ParentElement());
|
||||
return;
|
||||
}
|
||||
|
||||
clear();
|
||||
m_edited_qgi_type = Element::Type;
|
||||
addEditor(new ElementPropertiesWidget(deti->ParentElement(), this));
|
||||
break; }
|
||||
|
||||
default:
|
||||
m_edited_qgi_type = -1;
|
||||
|
||||
152
sources/ui/dynamicelementtextitemeditor.cpp
Normal file
152
sources/ui/dynamicelementtextitemeditor.cpp
Normal file
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
Copyright 2006-2017 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "dynamicelementtextitemeditor.h"
|
||||
#include "ui_dynamicelementtextitemeditor.h"
|
||||
#include "dynamicelementtextitem.h"
|
||||
#include "element.h"
|
||||
#include "dynamicelementtextmodel.h"
|
||||
#include "diagram.h"
|
||||
#include "undocommand/deleteqgraphicsitemcommand.h"
|
||||
#include "undocommand/addelementtextcommand.h"
|
||||
|
||||
#include <QTreeView>
|
||||
#include <QUndoCommand>
|
||||
|
||||
DynamicElementTextItemEditor::DynamicElementTextItemEditor(Element *element, QWidget *parent) :
|
||||
AbstractElementPropertiesEditorWidget(parent),
|
||||
ui(new Ui::DynamicElementTextItemEditor)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
m_tree_view = new QTreeView(this);
|
||||
m_tree_view->header()->setDefaultSectionSize(150);
|
||||
m_tree_view->setItemDelegate(new DynamicTextItemDelegate(m_tree_view));
|
||||
m_tree_view->setAlternatingRowColors(true);
|
||||
ui->verticalLayout->addWidget(m_tree_view);
|
||||
setElement(element);
|
||||
}
|
||||
|
||||
DynamicElementTextItemEditor::~DynamicElementTextItemEditor()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void DynamicElementTextItemEditor::setElement(Element *element)
|
||||
{
|
||||
if (m_element == element)
|
||||
return;
|
||||
|
||||
m_element = element;
|
||||
|
||||
DynamicElementTextModel *old_model = m_model;
|
||||
m_model = new DynamicElementTextModel(m_tree_view);
|
||||
connect(m_model, &DynamicElementTextModel::itemChanged, this, &DynamicElementTextItemEditor::dataEdited);
|
||||
|
||||
for (DynamicElementTextItem *deti : m_element->dynamicTextItems())
|
||||
m_model->addText(deti);
|
||||
|
||||
m_tree_view->setModel(m_model);
|
||||
|
||||
if(old_model)
|
||||
delete old_model;
|
||||
}
|
||||
|
||||
bool DynamicElementTextItemEditor::setLiveEdit(bool live_edit)
|
||||
{
|
||||
m_live_edit = live_edit;
|
||||
return true;
|
||||
}
|
||||
|
||||
void DynamicElementTextItemEditor::apply()
|
||||
{
|
||||
QList <QUndoCommand *> undo_list;
|
||||
for (DynamicElementTextItem *deti : m_element->dynamicTextItems())
|
||||
{
|
||||
QUndoCommand *undo = m_model->undoForEditedText(deti);
|
||||
if(undo->childCount())
|
||||
undo_list << undo;
|
||||
else
|
||||
delete undo;
|
||||
}
|
||||
|
||||
for (DynamicElementTextItem *deti : m_element->dynamicTextItems())
|
||||
deti->blockSignals(true);
|
||||
|
||||
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();
|
||||
us.beginMacro(tr("Modifier des texte d'élément"));
|
||||
for (QUndoCommand *quc : undo_list)
|
||||
us.push(quc);
|
||||
us.endMacro();
|
||||
}
|
||||
}
|
||||
|
||||
for (DynamicElementTextItem *deti : m_element->dynamicTextItems())
|
||||
deti->blockSignals(false);
|
||||
}
|
||||
|
||||
void DynamicElementTextItemEditor::dataEdited(QStandardItem *qsi)
|
||||
{
|
||||
Q_UNUSED(qsi)
|
||||
if (m_live_edit)
|
||||
apply();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DynamicElementTextItemEditor::on_m_add_text_clicked
|
||||
* Add a new dynamic text
|
||||
*/
|
||||
void DynamicElementTextItemEditor::on_m_add_text_clicked()
|
||||
{
|
||||
if (!m_element)
|
||||
return;
|
||||
|
||||
DynamicElementTextItem *deti = new DynamicElementTextItem(m_element);
|
||||
if (m_element->diagram())
|
||||
{
|
||||
m_element->diagram()->undoStack().push(new AddElementTextCommand(m_element, deti));
|
||||
m_model->addText(deti);
|
||||
}
|
||||
else
|
||||
{
|
||||
delete deti;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DynamicElementTextItemEditor::on_m_remove_text_clicked
|
||||
* Remove the selected text field
|
||||
*/
|
||||
void DynamicElementTextItemEditor::on_m_remove_text_clicked()
|
||||
{
|
||||
DynamicElementTextItem *deti = m_model->textFromIndex(m_tree_view->currentIndex());
|
||||
if(deti)
|
||||
{
|
||||
if(m_element->diagram())
|
||||
{
|
||||
DiagramContent dc;
|
||||
dc.m_element_texts << deti;
|
||||
m_element->diagram()->undoStack().push(new DeleteQGraphicsItemCommand(m_element->diagram(), dc));
|
||||
m_model->removeText(deti);
|
||||
}
|
||||
}
|
||||
}
|
||||
59
sources/ui/dynamicelementtextitemeditor.h
Normal file
59
sources/ui/dynamicelementtextitemeditor.h
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
Copyright 2006-2017 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef DYNAMICELEMENTTEXTITEMEDITOR_H
|
||||
#define DYNAMICELEMENTTEXTITEMEDITOR_H
|
||||
|
||||
#include "abstractelementpropertieseditorwidget.h"
|
||||
|
||||
class DynamicElementTextItem;
|
||||
class DynamicElementTextModel;
|
||||
class QTreeView;
|
||||
class QStandardItem;
|
||||
|
||||
namespace Ui {
|
||||
class DynamicElementTextItemEditor;
|
||||
}
|
||||
|
||||
class DynamicElementTextItemEditor : public AbstractElementPropertiesEditorWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit DynamicElementTextItemEditor(Element *element, QWidget *parent = 0);
|
||||
~DynamicElementTextItemEditor();
|
||||
|
||||
virtual void setElement(Element *element);
|
||||
virtual QString title() const {return tr("Textes");}
|
||||
virtual bool setLiveEdit(bool live_edit);
|
||||
virtual void apply();
|
||||
|
||||
private:
|
||||
void dataEdited(QStandardItem *qsi);
|
||||
|
||||
private slots:
|
||||
void on_m_add_text_clicked();
|
||||
void on_m_remove_text_clicked();
|
||||
|
||||
private:
|
||||
Ui::DynamicElementTextItemEditor *ui;
|
||||
QTreeView *m_tree_view = nullptr;
|
||||
DynamicElementTextModel *m_model = nullptr;
|
||||
|
||||
};
|
||||
|
||||
#endif // DYNAMICELEMENTTEXTITEMEDITOR_H
|
||||
62
sources/ui/dynamicelementtextitemeditor.ui
Normal file
62
sources/ui/dynamicelementtextitemeditor.ui
Normal file
@@ -0,0 +1,62 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>DynamicElementTextItemEditor</class>
|
||||
<widget class="QWidget" name="DynamicElementTextItemEditor">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>400</width>
|
||||
<height>300</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<spacer name="horizontalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="m_add_text">
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset resource="../../qelectrotech.qrc">
|
||||
<normaloff>:/ico/16x16/list-add.png</normaloff>:/ico/16x16/list-add.png</iconset>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="m_remove_text">
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset resource="../../qelectrotech.qrc">
|
||||
<normaloff>:/ico/16x16/list-remove.png</normaloff>:/ico/16x16/list-remove.png</iconset>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources>
|
||||
<include location="../../qelectrotech.qrc"/>
|
||||
</resources>
|
||||
<connections/>
|
||||
</ui>
|
||||
407
sources/ui/dynamicelementtextmodel.cpp
Normal file
407
sources/ui/dynamicelementtextmodel.cpp
Normal file
@@ -0,0 +1,407 @@
|
||||
/*
|
||||
Copyright 2006-2017 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "dynamicelementtextmodel.h"
|
||||
#include "dynamicelementtextitem.h"
|
||||
#include <QStandardItem>
|
||||
#include <QHash>
|
||||
#include <QColorDialog>
|
||||
#include <QModelIndex>
|
||||
#include <QComboBox>
|
||||
#include <QUndoCommand>
|
||||
#include "QPropertyUndoCommand/qpropertyundocommand.h"
|
||||
|
||||
DynamicElementTextModel::DynamicElementTextModel(QObject *parent) :
|
||||
QStandardItemModel(parent)
|
||||
{
|
||||
setColumnCount(2);
|
||||
setHeaderData(0, Qt::Horizontal, tr("Propriété"), Qt::DisplayRole);
|
||||
setHeaderData(1, Qt::Horizontal, tr("Valeur"), Qt::DisplayRole);
|
||||
|
||||
connect(this, &DynamicElementTextModel::itemChanged, this, &DynamicElementTextModel::dataEdited);
|
||||
}
|
||||
|
||||
DynamicElementTextModel::~DynamicElementTextModel()
|
||||
{
|
||||
//Connection is not destroy automaticaly,
|
||||
//because was not connected to a slot, but a lambda
|
||||
for(DynamicElementTextItem *deti : m_hash_text_connect.keys())
|
||||
setConnection(deti, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DynamicElementTextModel::addText
|
||||
* @param deti
|
||||
*/
|
||||
void DynamicElementTextModel::addText(DynamicElementTextItem *deti)
|
||||
{
|
||||
if(m_texts_list.keys().contains(deti))
|
||||
return;
|
||||
|
||||
QList <QStandardItem *> qsi_list;
|
||||
|
||||
QStandardItem *qsi = new QStandardItem(deti->toPlainText());
|
||||
qsi->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
|
||||
|
||||
//Source of text
|
||||
QStandardItem *src = new QStandardItem(tr("Source du texte"));
|
||||
src->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
|
||||
QStandardItem *srca = new QStandardItem(deti->textFrom() == DynamicElementTextItem::UserText ? tr("Texte utilisateur") : tr("Information de l'élément"));
|
||||
srca->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
|
||||
srca->setData(textFrom, Qt::UserRole+1);
|
||||
|
||||
qsi_list << src << srca;
|
||||
qsi->appendRow(qsi_list);
|
||||
|
||||
//User text
|
||||
QStandardItem *usr = new QStandardItem(tr("Texte"));
|
||||
usr->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
|
||||
QStandardItem *usra = new QStandardItem(deti->toPlainText());
|
||||
usra->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
|
||||
usra->setData(DynamicElementTextModel::userText, Qt::UserRole+1);
|
||||
|
||||
qsi_list.clear();
|
||||
qsi_list << usr << usra;
|
||||
src->appendRow(qsi_list);
|
||||
|
||||
//Info text
|
||||
QStandardItem *info = new QStandardItem(tr("Information"));
|
||||
info->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
|
||||
QStandardItem *infoa = new QStandardItem(deti->toPlainText());
|
||||
infoa->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
|
||||
infoa->setData(DynamicElementTextModel::infoText, Qt::UserRole+1);
|
||||
|
||||
qsi_list.clear();
|
||||
qsi_list << info << infoa;
|
||||
src->appendRow(qsi_list);
|
||||
|
||||
|
||||
//Size
|
||||
QStandardItem *size = new QStandardItem(tr("Taille"));
|
||||
size->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
|
||||
QStandardItem *siza = new QStandardItem();
|
||||
siza->setData(deti->fontSize(), Qt::EditRole);
|
||||
siza->setData(DynamicElementTextModel::size, Qt::UserRole+1);
|
||||
siza->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
|
||||
|
||||
qsi_list.clear();
|
||||
qsi_list << size << siza;
|
||||
qsi->appendRow(qsi_list);
|
||||
|
||||
//Tagg
|
||||
QStandardItem *tagg = new QStandardItem(tr("Tagg"));
|
||||
tagg->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
|
||||
QStandardItem *tagga = new QStandardItem(deti->tagg());
|
||||
tagga->setData(DynamicElementTextModel::tagg, Qt::UserRole+1);
|
||||
tagga->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
|
||||
|
||||
qsi_list.clear();
|
||||
qsi_list << tagg << tagga;
|
||||
qsi->appendRow(qsi_list);
|
||||
|
||||
//Color
|
||||
QStandardItem *color = new QStandardItem(tr("Couleur"));
|
||||
color->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
|
||||
QStandardItem *colora = new QStandardItem;
|
||||
colora->setData(deti->color(), Qt::ForegroundRole);
|
||||
colora->setData(deti->color(), Qt::EditRole);
|
||||
colora->setData(DynamicElementTextModel::color, Qt::UserRole+1);
|
||||
colora->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
|
||||
|
||||
qsi_list.clear();
|
||||
qsi_list << color << colora;
|
||||
qsi->appendRow(qsi_list);
|
||||
|
||||
qsi_list.clear();
|
||||
QStandardItem *empty_qsi = new QStandardItem(0);
|
||||
empty_qsi->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
qsi_list << qsi << empty_qsi;
|
||||
this->appendRow(qsi_list);
|
||||
|
||||
m_texts_list.insert(deti, qsi);
|
||||
blockSignals(true);
|
||||
enableSourceText(deti, deti->textFrom());
|
||||
blockSignals(false);
|
||||
setConnection(deti, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DynamicElementTextModel::removeText
|
||||
* @param deti
|
||||
*/
|
||||
void DynamicElementTextModel::removeText(DynamicElementTextItem *deti)
|
||||
{
|
||||
if (!m_texts_list.contains(deti))
|
||||
return;
|
||||
|
||||
QModelIndex text_index = m_texts_list.value(deti)->index();
|
||||
this->removeRow(text_index.row(), text_index.parent());
|
||||
m_texts_list.remove(deti);
|
||||
setConnection(deti, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DynamicElementTextModel::textFromIndex
|
||||
* @param index
|
||||
* @return the text associated with @index. Return value can be nullptr
|
||||
* @Index can be a child of an index associated with a text
|
||||
*/
|
||||
DynamicElementTextItem *DynamicElementTextModel::textFromIndex(const QModelIndex &index) const
|
||||
{
|
||||
if(!index.isValid())
|
||||
return nullptr;
|
||||
|
||||
if (QStandardItem *item = itemFromIndex(index))
|
||||
return textFromItem(item);
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DynamicElementTextModel::textFromItem
|
||||
* @param item
|
||||
* @return the text associated with @item. Return value can be nullptr
|
||||
* @item can be a child of an item associated with a text
|
||||
*/
|
||||
DynamicElementTextItem *DynamicElementTextModel::textFromItem(QStandardItem *item) const
|
||||
{
|
||||
QStandardItem *text_item = item;
|
||||
while (text_item->parent())
|
||||
text_item = text_item->parent();
|
||||
|
||||
if (m_texts_list.values().contains(text_item))
|
||||
return m_texts_list.key(text_item);
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DynamicElementTextModel::undoForEditedText
|
||||
* @param deti
|
||||
* @return A QUndoCommand that describe all changes made for @deti.
|
||||
* 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 *undo = new QUndoCommand(tr("Éditer un texte d'élément"));
|
||||
|
||||
if (!m_texts_list.contains(deti))
|
||||
return undo;
|
||||
|
||||
QStandardItem *text_qsi = m_texts_list.value(deti);
|
||||
|
||||
QString from = text_qsi->child(0,1)->data(Qt::DisplayRole).toString();
|
||||
if ((from == tr("Texte utilisateur")) && (deti->textFrom() != DynamicElementTextItem::UserText))
|
||||
new QPropertyUndoCommand(deti, "textFrom", QVariant(deti->textFrom()), QVariant(DynamicElementTextItem::UserText), undo);
|
||||
else if ((from == tr("Information de l'élément")) && (deti->textFrom() != DynamicElementTextItem::ElementInfo))
|
||||
new QPropertyUndoCommand(deti, "textFrom", QVariant(deti->textFrom()), QVariant(DynamicElementTextItem::ElementInfo), undo);
|
||||
|
||||
QString text = text_qsi->child(0,0)->child(0,1)->data(Qt::DisplayRole).toString();
|
||||
if (text != deti->text())
|
||||
new QPropertyUndoCommand(deti, "text", QVariant(deti->text()), QVariant(text), undo);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
QColor color = text_qsi->child(3,1)->data(Qt::EditRole).value<QColor>();
|
||||
if(color != deti->color())
|
||||
new QPropertyUndoCommand(deti, "color", QVariant(deti->color()), QVariant(color), undo);
|
||||
|
||||
return undo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DynamicElementTextModel::enableSourceText
|
||||
* Enable the good field, according to the current source of text, for the edited text @deti
|
||||
* @param deti
|
||||
* @param tf
|
||||
*/
|
||||
void DynamicElementTextModel::enableSourceText(DynamicElementTextItem *deti, DynamicElementTextItem::TextFrom tf)
|
||||
{
|
||||
if (!m_texts_list.contains(deti))
|
||||
return;
|
||||
|
||||
QStandardItem *qsi = m_texts_list.value(deti)->child(0,0);
|
||||
|
||||
bool usr = true, info = false;
|
||||
if(tf == DynamicElementTextItem::ElementInfo) {
|
||||
usr = false; info = true;}
|
||||
|
||||
//User text
|
||||
qsi->child(0,0)->setEnabled(usr);
|
||||
qsi->child(0,1)->setEnabled(usr);
|
||||
//Info text
|
||||
qsi->child(1,0)->setEnabled(info);
|
||||
qsi->child(1,1)->setEnabled(info);
|
||||
}
|
||||
|
||||
void DynamicElementTextModel::dataEdited(QStandardItem *qsi)
|
||||
{
|
||||
DynamicElementTextItem *deti = textFromItem(qsi);
|
||||
if (!deti)
|
||||
return;
|
||||
|
||||
if (qsi->data().toInt() == textFrom)
|
||||
{
|
||||
QString from = qsi->data(Qt::DisplayRole).toString();
|
||||
if (from == tr("Texte utilisateur"))
|
||||
enableSourceText(deti, DynamicElementTextItem::UserText);
|
||||
else
|
||||
enableSourceText(deti, DynamicElementTextItem::ElementInfo);
|
||||
}
|
||||
else if (qsi->data().toInt() == userText)
|
||||
{
|
||||
QString text = qsi->data(Qt::DisplayRole).toString();
|
||||
m_texts_list.value(deti)->setData(text, Qt::DisplayRole);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DynamicElementTextModel::setConnection
|
||||
* Set up the connection for @deti to keep up to date the data of this model and the text.
|
||||
* Is notably use with the use of QUndoCommand.
|
||||
* @param deti - text to setup connection
|
||||
* @param set - true = set connection - false unset connection
|
||||
*/
|
||||
void DynamicElementTextModel::setConnection(DynamicElementTextItem *deti, bool set)
|
||||
{
|
||||
if(set)
|
||||
{
|
||||
if(m_hash_text_connect.keys().contains(deti))
|
||||
return;
|
||||
|
||||
QList<QMetaObject::Connection> connection_list;
|
||||
connection_list << connect(deti, &DynamicElementTextItem::colorChanged, [deti,this](){this->updateDataFromText(deti, color);});
|
||||
connection_list << connect(deti, &DynamicElementTextItem::fontSizeChanged, [deti,this](){this->updateDataFromText(deti, size);});
|
||||
connection_list << connect(deti, &DynamicElementTextItem::taggChanged, [deti,this](){this->updateDataFromText(deti, tagg);});
|
||||
connection_list << connect(deti, &DynamicElementTextItem::textChanged, [deti,this](){this->updateDataFromText(deti, userText);});
|
||||
connection_list << connect(deti, &DynamicElementTextItem::TextFromChanged, [deti,this](){this->updateDataFromText(deti, textFrom);});
|
||||
|
||||
m_hash_text_connect.insert(deti, connection_list);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!m_hash_text_connect.keys().contains(deti))
|
||||
return;
|
||||
|
||||
for (QMetaObject::Connection con : m_hash_text_connect.value(deti))
|
||||
disconnect(con);
|
||||
|
||||
m_hash_text_connect.remove(deti);
|
||||
}
|
||||
}
|
||||
|
||||
void DynamicElementTextModel::updateDataFromText(DynamicElementTextItem *deti, ValueType type)
|
||||
{
|
||||
QStandardItem *qsi = m_texts_list.value(deti);
|
||||
if (!qsi)
|
||||
return;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case textFrom:
|
||||
qsi->child(0,1)->setData(deti->textFrom() == DynamicElementTextItem::UserText ? tr("Texte utilisateur") : tr("Information de l'élément"), Qt::DisplayRole);
|
||||
break;
|
||||
case userText:
|
||||
{
|
||||
QStandardItem *qsia = qsi->child(0,0);
|
||||
qsia->child(0,1)->setData(deti->toPlainText(), Qt::DisplayRole);
|
||||
qsi->setData(deti->toPlainText(), Qt::DisplayRole);
|
||||
break;
|
||||
}
|
||||
case infoText:
|
||||
break;
|
||||
case size:
|
||||
qsi->child(1,1)->setData(deti->fontSize(), Qt::EditRole);
|
||||
break;
|
||||
case tagg:
|
||||
qsi->child(2,1)->setData(deti->tagg(), Qt::DisplayRole);
|
||||
break;
|
||||
case color:
|
||||
{
|
||||
qsi->child(3,1)->setData(deti->color(), Qt::EditRole);
|
||||
qsi->child(3,1)->setData(deti->color(), Qt::ForegroundRole);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/***************************************************
|
||||
* A little delegate only for add a combobox and a color dialog,
|
||||
* for use with the model
|
||||
***************************************************/
|
||||
|
||||
DynamicTextItemDelegate::DynamicTextItemDelegate(QObject *parent) :
|
||||
QStyledItemDelegate(parent)
|
||||
{}
|
||||
|
||||
QWidget *DynamicTextItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
switch (index.data(Qt::UserRole+1).toInt())
|
||||
{
|
||||
case DynamicElementTextModel::textFrom:
|
||||
{
|
||||
QComboBox *qcb = new QComboBox(parent);
|
||||
qcb->addItem(tr("Texte utilisateur"));
|
||||
qcb->addItem(tr("Information de l'élément"));
|
||||
return qcb;
|
||||
}
|
||||
case DynamicElementTextModel::color:
|
||||
{
|
||||
QColorDialog *cd = new QColorDialog(index.data(Qt::EditRole).value<QColor>());
|
||||
return cd;
|
||||
}
|
||||
}
|
||||
return QStyledItemDelegate::createEditor(parent, option, index);
|
||||
}
|
||||
|
||||
void DynamicTextItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
|
||||
{
|
||||
if (index.isValid())
|
||||
{
|
||||
if (QStandardItemModel *qsim = dynamic_cast<QStandardItemModel *>(model))
|
||||
{
|
||||
QStandardItem *qsi = qsim->itemFromIndex(index);
|
||||
if(qsi)
|
||||
{
|
||||
if(QColorDialog *cd = dynamic_cast<QColorDialog *> (editor))
|
||||
{
|
||||
qsi->setData(cd->selectedColor(), Qt::EditRole);
|
||||
qsi->setData(cd->selectedColor(), Qt::ForegroundRole);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
QStyledItemDelegate::setModelData(editor, model, index);
|
||||
}
|
||||
76
sources/ui/dynamicelementtextmodel.h
Normal file
76
sources/ui/dynamicelementtextmodel.h
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
Copyright 2006-2017 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef DYNAMICELEMENTTEXTMODEL_H
|
||||
#define DYNAMICELEMENTTEXTMODEL_H
|
||||
|
||||
#include <QStandardItemModel>
|
||||
#include <qstyleditemdelegate.h>
|
||||
#include "dynamicelementtextitem.h"
|
||||
|
||||
class QUndoCommand;
|
||||
|
||||
/**
|
||||
* @brief The DynamicElementTextModel class
|
||||
* A model to use with QtView.
|
||||
* This model display and can edit the value of dynamic text of an element.
|
||||
* Set the delegate DynamicTextItemDelegate as delegate of this model.
|
||||
*/
|
||||
class DynamicElementTextModel : public QStandardItemModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum ValueType {
|
||||
textFrom =1,
|
||||
userText,
|
||||
infoText,
|
||||
size,
|
||||
tagg,
|
||||
color
|
||||
};
|
||||
|
||||
DynamicElementTextModel(QObject *parent = nullptr);
|
||||
~DynamicElementTextModel();
|
||||
|
||||
void addText(DynamicElementTextItem *deti);
|
||||
void removeText(DynamicElementTextItem *deti);
|
||||
DynamicElementTextItem *textFromIndex(const QModelIndex &index) const;
|
||||
DynamicElementTextItem *textFromItem(QStandardItem *item) const;
|
||||
QUndoCommand *undoForEditedText(DynamicElementTextItem *deti) const;
|
||||
|
||||
private:
|
||||
void enableSourceText(DynamicElementTextItem *deti, DynamicElementTextItem::TextFrom tf );
|
||||
void dataEdited(QStandardItem *qsi);
|
||||
void setConnection(DynamicElementTextItem *deti, bool set);
|
||||
void updateDataFromText(DynamicElementTextItem *deti, DynamicElementTextModel::ValueType type);
|
||||
|
||||
private:
|
||||
QHash <DynamicElementTextItem *, QStandardItem *> m_texts_list;
|
||||
QHash <DynamicElementTextItem *, QList<QMetaObject::Connection>> m_hash_text_connect;
|
||||
};
|
||||
|
||||
class DynamicTextItemDelegate : public QStyledItemDelegate
|
||||
{
|
||||
public:
|
||||
DynamicTextItemDelegate(QObject *parent = Q_NULLPTR);
|
||||
|
||||
virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
|
||||
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
|
||||
};
|
||||
|
||||
#endif // DYNAMICELEMENTTEXTMODEL_H
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "diagram.h"
|
||||
#include "diagramposition.h"
|
||||
#include "qeticons.h"
|
||||
#include "dynamicelementtextitemeditor.h"
|
||||
|
||||
#include <QVBoxLayout>
|
||||
#include <QLabel>
|
||||
@@ -203,9 +204,10 @@ void ElementPropertiesWidget::updateUi()
|
||||
default:
|
||||
break;
|
||||
}
|
||||
m_list_editor << new DynamicElementTextItemEditor(m_element, this);
|
||||
|
||||
//Add each editors in tab widget
|
||||
foreach (AbstractElementPropertiesEditorWidget *aepew, m_list_editor)
|
||||
for (AbstractElementPropertiesEditorWidget *aepew : m_list_editor)
|
||||
{
|
||||
aepew->setLiveEdit(m_live_edit);
|
||||
m_tab->addTab(aepew, aepew->title());
|
||||
@@ -296,7 +298,7 @@ QWidget *ElementPropertiesWidget::generalWidget()
|
||||
connect(find_in_panel, SIGNAL(clicked()), this, SLOT(findInPanel()));
|
||||
QPushButton *edit_element = new QPushButton(QET::Icons::ElementEdit, tr("Éditer l'élément"), general_widget);
|
||||
connect(edit_element, SIGNAL(clicked()), this, SLOT(editElement()));
|
||||
QHBoxLayout *hlayout_ = new QHBoxLayout;
|
||||
QHBoxLayout *hlayout_ = new QHBoxLayout;
|
||||
hlayout_->addWidget(find_in_panel);
|
||||
hlayout_->addWidget(edit_element);
|
||||
vlayout_->addLayout(hlayout_);
|
||||
|
||||
Reference in New Issue
Block a user