1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
/*
Copyright 2006-2023 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 "../QPropertyUndoCommand/qpropertyundocommand.h"
#include "../diagram.h"
#include "../elementtextpattern.h"
#include "../qetgraphicsitem/dynamicelementtextitem.h"
#include "../qetgraphicsitem/element.h"
#include "../qetgraphicsitem/elementtextitemgroup.h"
#include "../ui_dynamicelementtextitemeditor.h"
#include "../undocommand/addelementtextcommand.h"
#include "../undocommand/deleteqgraphicsitemcommand.h"
#include "dynamicelementtextmodel.h"
#include <QTreeView>
#include <QUndoCommand>
DynamicElementTextItemEditor::DynamicElementTextItemEditor(Element *element, QWidget *parent) :
AbstractElementPropertiesEditorWidget(parent),
ui(new Ui::DynamicElementTextItemEditor)
{
ui->setupUi(this);
ui->m_tree_view->setItemDelegate(new DynamicTextItemDelegate(ui->m_tree_view));
ui->m_remove_selection->setDisabled(true);
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(element, ui->m_tree_view);
connect(m_model, &DynamicElementTextModel::dataChanged, this, &DynamicElementTextItemEditor::dataEdited);
ui->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;
//Get all dynamic text item of the element
QList <DynamicElementTextItem *> deti_list;
deti_list << m_element.data()->dynamicTextItems();
for(ElementTextItemGroup *group : m_element.data()->textGroups())
deti_list << group->texts();
for (DynamicElementTextItem *deti : deti_list)
{
QUndoCommand *undo = m_model->undoForEditedText(deti);
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;
else
delete undo;
}
//Get all texts groups of the edited element
for (ElementTextItemGroup *group : m_element.data()->textGroups())
{
QUndoCommand *undo = m_model->undoForEditedGroup(group);
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;
else
delete undo;
}
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 textes d'élément"));
for (QUndoCommand *quc : undo_list)
us.push(quc);
us.endMacro();
}
}
}
/**
@brief DynamicElementTextItemEditor::setCurrentText
Expand and select the item for text text
@param text
*/
void DynamicElementTextItemEditor::setCurrentText(DynamicElementTextItem *text)
{
QModelIndex index = m_model->indexFromText(text);
if(!index.isValid())
return;
ui->m_tree_view->expand(index);
ui->m_tree_view->expand(index.QModelIndex::model()->index(0,0));
ui->m_tree_view->setCurrentIndex(index);
ui->m_remove_selection->setEnabled(true);
}
/**
@brief DynamicElementTextItemEditor::setCurrentGroup
Expand and select the item for group group
@param group
*/
void DynamicElementTextItemEditor::setCurrentGroup(ElementTextItemGroup *group)
{
QModelIndex index = m_model->indexFromGroup(group);
if(!index.isValid())
return;
ui->m_tree_view->expand(index);
ui->m_tree_view->setCurrentIndex(index);
ui->m_remove_selection->setEnabled(true);
}
QUndoCommand *DynamicElementTextItemEditor::associatedUndo() const
{
QUndoCommand *parent_undo = new QUndoCommand(tr("Modifier un texte d'élément"));
for (DynamicElementTextItem *deti : m_element.data()->dynamicTextItems())
m_model->undoForEditedText(deti, parent_undo);
for (ElementTextItemGroup *grp : m_element.data()->textGroups())
m_model->undoForEditedGroup(grp, 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()
{
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));
setCurrentText(deti);
}
else
{
delete deti;
}
}
/**
@brief DynamicElementTextItemEditor::on_m_remove_selection_clicked
Remove the selected item
*/
void DynamicElementTextItemEditor::on_m_remove_selection_clicked()
{
DynamicElementTextItem *deti = m_model->textFromIndex(ui->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));
}
return;
}
ElementTextItemGroup *group = m_model->groupFromIndex(ui->m_tree_view->currentIndex());
if(group && m_element.data()->diagram())
m_element.data()->diagram()->undoStack().push(new RemoveTextsGroupCommand(m_element.data(), group));
}
/**
@brief DynamicElementTextItemEditor::on_m_add_group_clicked
Add a new group
*/
void DynamicElementTextItemEditor::on_m_add_group_clicked()
{
QString name = QInputDialog::getText(this, tr("Nom du groupe"), tr("Entrer le nom du nouveau groupe"));
if(name.isEmpty())
return;
else if (m_element.data()->diagram())
m_element.data()->diagram()->undoStack().push(new AddTextsGroupCommand(m_element, name));
}
void DynamicElementTextItemEditor::on_m_tree_view_clicked(const QModelIndex &index)
{
if(m_model->indexIsText(index) || m_model->indexIsGroup(index))
ui->m_remove_selection->setEnabled(true);
else
ui->m_remove_selection->setDisabled(true);
}
void DynamicElementTextItemEditor::on_m_export_pb_clicked()
{
ExportElementTextPattern eetp(m_element);
}
void DynamicElementTextItemEditor::on_m_import_pb_clicked()
{
ImportElementTextPattern ietp(m_element);
}
|