QElectroTech  0.70
dynamicelementtextitemeditor.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
19 #include "ui_dynamicelementtextitemeditor.h"
20 #include "dynamicelementtextitem.h"
21 #include "element.h"
23 #include "diagram.h"
27 #include "elementtextitemgroup.h"
29 #include "elementtextpattern.h"
30 
31 #include <QTreeView>
32 #include <QUndoCommand>
33 
37 {
38  ui->setupUi(this);
39 
40  ui->m_tree_view->setItemDelegate(new DynamicTextItemDelegate(ui->m_tree_view));
41  ui->m_remove_selection->setDisabled(true);
42 
43  setElement(element);
44 }
45 
47 {
48  delete ui;
49 }
50 
52 {
53  if (m_element == element)
54  return;
55 
56  m_element = element;
57 
58  DynamicElementTextModel *old_model = m_model;
59  m_model = new DynamicElementTextModel(element, ui->m_tree_view);
61  ui->m_tree_view->setModel(m_model);
62 
63  if(old_model)
64  delete old_model;
65 }
66 
68 {
69  m_live_edit = live_edit;
70  return true;
71 }
72 
74 {
75  QList <QUndoCommand *> undo_list;
76 
77  //Get all dynamic text item of the element
78  QList <DynamicElementTextItem *> deti_list;
79  deti_list << m_element.data()->dynamicTextItems();
80  for(ElementTextItemGroup *group : m_element.data()->textGroups())
81  deti_list << group->texts();
82 
83  for (DynamicElementTextItem *deti : deti_list)
84  {
85  QUndoCommand *undo = m_model->undoForEditedText(deti);
86 
87  if (undo->childCount() == 1)
88  {
89  QPropertyUndoCommand *quc = new QPropertyUndoCommand(static_cast<const QPropertyUndoCommand *>(undo->child(0)));
90  if (quc->text().isEmpty())
91  quc->setText(undo->text());
92  undo_list << quc;
93  delete undo;
94  }
95  else if(undo->childCount() > 1)
96  undo_list << undo;
97  else
98  delete undo;
99  }
100 
101  //Get all texts groups of the edited element
102  for (ElementTextItemGroup *group : m_element.data()->textGroups())
103  {
104  QUndoCommand *undo = m_model->undoForEditedGroup(group);
105 
106  if (undo->childCount() == 1)
107  {
108  QPropertyUndoCommand *quc = new QPropertyUndoCommand(static_cast<const QPropertyUndoCommand *>(undo->child(0)));
109  if (quc->text().isEmpty())
110  quc->setText(undo->text());
111  undo_list << quc;
112  delete undo;
113  }
114  else if(undo->childCount() > 1)
115  undo_list << undo;
116  else
117  delete undo;
118  }
119 
120  if(!undo_list.isEmpty() && m_element->diagram())
121  {
122  if (undo_list.size() == 1)
123  {
124  m_element->diagram()->undoStack().push(undo_list.first());
125  }
126  else
127  {
128  QUndoStack &us = m_element->diagram()->undoStack();
129  us.beginMacro(tr("Modifier des textes d'élément"));
130  for (QUndoCommand *quc : undo_list)
131  us.push(quc);
132  us.endMacro();
133  }
134  }
135 }
136 
143 {
144  QModelIndex index = m_model->indexFromText(text);
145  if(!index.isValid())
146  return;
147 
148  ui->m_tree_view->expand(index);
149  ui->m_tree_view->expand(index.child(0,0));
150  ui->m_tree_view->setCurrentIndex(index);
151  ui->m_remove_selection->setEnabled(true);
152 }
153 
160 {
161  QModelIndex index = m_model->indexFromGroup(group);
162  if(!index.isValid())
163  return;
164 
165  ui->m_tree_view->expand(index);
166  ui->m_tree_view->setCurrentIndex(index);
167  ui->m_remove_selection->setEnabled(true);
168 }
169 
171 {
172  QUndoCommand *parent_undo = new QUndoCommand(tr("Modifier un texte d'élément"));
173  for (DynamicElementTextItem *deti : m_element.data()->dynamicTextItems())
174  m_model->undoForEditedText(deti, parent_undo);
175 
176  for (ElementTextItemGroup *grp : m_element.data()->textGroups())
177  m_model->undoForEditedGroup(grp, parent_undo);
178 
179  if(parent_undo->childCount() >= 1)
180  {
181  if(parent_undo->childCount() >= 2)
182  parent_undo->setText(tr("Modifier %1 textes d'élément").arg(QString::number(parent_undo->childCount())));
183  return parent_undo;
184  }
185  else
186  return nullptr;
187 }
188 
190 {
191  if (m_live_edit)
192  apply();
193 }
194 
200 {
201  if (!m_element)
202  return;
203 
205  if (m_element->diagram())
206  {
207  m_element->diagram()->undoStack().push(new AddElementTextCommand(m_element, deti));
208  setCurrentText(deti);
209  }
210  else
211  {
212  delete deti;
213  }
214 }
215 
221 {
222  DynamicElementTextItem *deti = m_model->textFromIndex(ui->m_tree_view->currentIndex());
223  if(deti)
224  {
225  if(m_element->diagram())
226  {
227  DiagramContent dc;
228  dc.m_element_texts << deti;
229  m_element->diagram()->undoStack().push(new DeleteQGraphicsItemCommand(m_element->diagram(), dc));
230  }
231  return;
232  }
233  ElementTextItemGroup *group = m_model->groupFromIndex(ui->m_tree_view->currentIndex());
234  if(group && m_element.data()->diagram())
235  m_element.data()->diagram()->undoStack().push(new RemoveTextsGroupCommand(m_element.data(), group));
236 }
237 
243 {
244  QString name = QInputDialog::getText(this, tr("Nom du groupe"), tr("Entrer le nom du nouveau groupe"));
245 
246  if(name.isEmpty())
247  return;
248  else if (m_element.data()->diagram())
249  m_element.data()->diagram()->undoStack().push(new AddTextsGroupCommand(m_element, name));
250 }
251 
253 {
254  if(m_model->indexIsText(index) || m_model->indexIsGroup(index))
255  ui->m_remove_selection->setEnabled(true);
256  else
257  ui->m_remove_selection->setDisabled(true);
258 }
259 
261 {
263 }
264 
266 {
268 }
The QPropertyUndoCommand class This undo command manage QProperty of a QObject. This undo command can...
The DynamicElementTextItem class This class provide a simple text field of element who can be added o...
QUndoCommand * associatedUndo() const override
PropertiesEditorWidget::associatedUndo By default, return a nullptr.
bool setLiveEdit(bool live_edit) override
PropertiesEditorWidget::setLiveEdit Set the editor in live edit mode. When an editor is in live edit ...
void setElement(Element *element) override
Ui::DynamicElementTextItemEditor * ui
QUndoCommand * undoForEditedText(DynamicElementTextItem *deti, QUndoCommand *parent_undo=nullptr) const
DynamicElementTextModel::undoForEditedText.
void on_m_remove_selection_clicked()
DynamicElementTextItemEditor::on_m_remove_selection_clicked Remove the selected item.
QString name() const override
Element::name.
Definition: element.cpp:1565
void on_m_add_text_clicked()
DynamicElementTextItemEditor::on_m_add_text_clicked Add a new dynamic text.
void on_m_add_group_clicked()
DynamicElementTextItemEditor::on_m_add_group_clicked Add a new group.
The AbstractElementPropertiesEditorWidget class This class provide common method for all widget used ...
QSet< DynamicElementTextItem * > m_element_texts
void setCurrentGroup(ElementTextItemGroup *group)
DynamicElementTextItemEditor::setCurrentGroup Expand and select the item for group ...
void on_m_tree_view_clicked(const QModelIndex &index)
QIcon tr
Definition: qeticons.cpp:204
DynamicElementTextItemEditor(Element *element, QWidget *parent=nullptr)
The ElementTextItemGroup class This class represent a group of element text Texts in the group can be...
bool indexIsText(const QModelIndex &index) const
DynamicElementTextModel::indexIsText.
QModelIndex indexFromGroup(ElementTextItemGroup *group) const
DynamicElementTextModel::indexFromGroup.
bool indexIsGroup(const QModelIndex &index) const
DynamicElementTextModel::indexIsGroup.
ElementTextItemGroup * groupFromIndex(const QModelIndex &index) const
DynamicElementTextModel::groupFromIndex.
void setCurrentText(DynamicElementTextItem *text)
DynamicElementTextItemEditor::setCurrentText Expand and select the item for text .
The RemoveTextsGroupCommand class Manage the removinf of a texts group.
The AddTextsGroupCommand class Manage the adding of a texts group.
The AddElementTextCommand class Manage the adding of element text.
QModelIndex indexFromText(DynamicElementTextItem *text) const
DynamicElementTextModel::indexFromText.
The DynamicElementTextModel class A model to use with QtView. This model display and can edit the val...
QUndoCommand * undoForEditedGroup(ElementTextItemGroup *group, QUndoCommand *parent_undo=nullptr) const
DynamicElementTextModel::undoForEditedGroup.
DynamicElementTextItem * textFromIndex(const QModelIndex &index) const
DynamicElementTextModel::textFromIndex.