555 lines
18 KiB
C++
Raw Permalink Normal View History

/*
2021-02-20 12:13:46 +01:00
Copyright 2006-2021 The QElectroTech Team
This file is part of QElectroTech.
2020-09-24 22:39:11 +02:00
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.
2020-09-24 22:39:11 +02:00
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.
2020-09-24 22:39:11 +02:00
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "searchandreplaceworker.h"
2020-12-08 19:57:35 +01:00
#include "../QPropertyUndoCommand/qpropertyundocommand.h"
#include "../diagram.h"
#include "../diagramcommands.h"
#include "../qetapp.h"
#include "../qetgraphicsitem/element.h"
#include "../qetgraphicsitem/independenttextitem.h"
#include "../qetinformation.h"
#include "../undocommand/changeelementinformationcommand.h"
#include "../undocommand/changetitleblockcommand.h"
SearchAndReplaceWorker::SearchAndReplaceWorker()
{
m_conductor_properties = invalidConductorProperties();
}
/**
2020-08-16 11:19:36 +02:00
@brief SearchAndReplaceWorker::replaceDiagram
2020-08-18 20:07:55 +02:00
Replace all properties of each diagram in diagram_list,
2020-08-16 11:19:36 +02:00
by the current titleblock propertie of this worker
2020-08-20 17:44:59 +02:00
@param diagram_list : list of diagram to be changed,
2020-08-16 11:19:36 +02:00
all diagrams must belong to the same project;
*/
void SearchAndReplaceWorker::replaceDiagram(QList<Diagram *> diagram_list)
{
if (diagram_list.isEmpty()) {
return;
}
2020-09-24 22:39:11 +02:00
QETProject *project = diagram_list.first()->project();
for (Diagram *d : diagram_list) {
if (d->project() != project) {
return;
}
}
2020-09-24 22:39:11 +02:00
QUndoStack *us = project->undoStack();
us->beginMacro(QObject::tr("Chercher/remplacer les propriétés de folio"));
for (Diagram *d : diagram_list)
{
TitleBlockProperties old_propertie = d->border_and_titleblock.exportTitleBlock();
TitleBlockProperties new_properties = old_propertie;
2020-09-24 22:39:11 +02:00
new_properties.title = applyChange(new_properties.title, m_titleblock_properties.title);
new_properties.author = applyChange(new_properties.author, m_titleblock_properties.author);
new_properties.filename = applyChange(new_properties.filename, m_titleblock_properties.filename);
new_properties.plant = applyChange(new_properties.plant, m_titleblock_properties.plant);
new_properties.locmach = applyChange(new_properties.locmach, m_titleblock_properties.locmach);
new_properties.indexrev = applyChange(new_properties.indexrev, m_titleblock_properties.indexrev);
new_properties.folio = applyChange(new_properties.folio, m_titleblock_properties.folio);
if (m_titleblock_properties.date.isValid())
{
if (m_titleblock_properties.date == eraseDate()) {
new_properties.date = QDate();
} else {
new_properties.date = m_titleblock_properties.date;
}
}
2020-09-24 22:39:11 +02:00
new_properties.context.add(m_titleblock_properties.context);
2020-09-24 22:39:11 +02:00
if (old_propertie != new_properties) {
project->undoStack()->push(new ChangeTitleBlockCommand(d, old_propertie, new_properties));
}
}
us->endMacro();
}
void SearchAndReplaceWorker::replaceDiagram(Diagram *diagram)
{
QList<Diagram *> list;
list.append(diagram);
replaceDiagram(list);
}
/**
2020-08-16 11:19:36 +02:00
@brief SearchAndReplaceWorker::replaceElement
2020-08-18 20:07:55 +02:00
Replace all properties of each elements in list
2020-08-16 11:19:36 +02:00
All element must belong to the same project,
if not this function do nothing.
All change are made through a undo command append
to undo list of the project.
@param list
*/
void SearchAndReplaceWorker::replaceElement(QList<Element *> list)
{
if (list.isEmpty() || !list.first()->diagram()) {
return;
}
2020-09-24 22:39:11 +02:00
QETProject *project_ = list.first()->diagram()->project();
for (Element *elmt : list)
{
if (elmt->diagram()) {
if (elmt->diagram()->project() != project_) {
return;
}
}
}
2020-09-24 22:39:11 +02:00
project_->undoStack()->beginMacro(QObject::tr("Chercher/remplacer les propriétés d'éléments."));
for (Element *elmt : list)
{
//We apply change only for master, slave, and terminal element.
if (elmt->linkType() == Element::Master ||
elmt->linkType() == Element::Simple ||
2022-05-16 08:59:25 +02:00
elmt->linkType() == Element::Terminale ||
elmt->linkType() == Element::Thumbnail)
{
DiagramContext old_context;
DiagramContext new_context = old_context = elmt->elementInformations();
for (QString key : QETInformation::elementInfoKeys())
{
new_context.addValue(key, applyChange(old_context.value(key).toString(),
m_element_context.value(key).toString()));
}
2020-09-24 22:39:11 +02:00
if (old_context != new_context)
{
ChangeElementInformationCommand *undo = new ChangeElementInformationCommand(elmt, old_context, new_context);
project_->undoStack()->push(undo);
}
}
}
project_->undoStack()->endMacro();
}
void SearchAndReplaceWorker::replaceElement(Element *element)
{
QList<Element *>list;
list.append(element);
replaceElement(list);
}
/**
2020-08-16 11:19:36 +02:00
@brief SearchAndReplaceWorker::replaceIndiText
2020-08-18 20:07:55 +02:00
Replace all displayed text of independent text of list
2020-08-16 11:19:36 +02:00
Each must belong to the same project, if not this function do nothing
@param list
*/
void SearchAndReplaceWorker::replaceIndiText(QList<IndependentTextItem *> list)
{
if (list.isEmpty() || !list.first()->diagram()) {
return;
}
QETProject *project_ = list.first()->diagram()->project();
for (IndependentTextItem *text : list) {
if (!text->diagram() ||
text->diagram()->project() != project_) {
return;
}
}
2020-09-24 22:39:11 +02:00
project_->undoStack()->beginMacro(QObject::tr("Chercher/remplacer des textes independants"));
for (IndependentTextItem *text : list)
{
QString before = text->toPlainText();
text->setPlainText(m_indi_text);
project_->undoStack()->push(new ChangeDiagramTextCommand(text, before, m_indi_text));
}
project_->undoStack()->endMacro();
}
void SearchAndReplaceWorker::replaceIndiText(IndependentTextItem *text)
{
QList<IndependentTextItem *>list;
list.append(text);
replaceIndiText(list);
}
/**
2020-08-16 11:19:36 +02:00
@brief SearchAndReplaceWorker::replaceConductor
2020-08-18 20:07:55 +02:00
Replace all properties of each conductor in list
2020-08-16 11:19:36 +02:00
All conductor must belong to the same project,
if not this function do nothing.
All change are made through a undo command append
to undo list of the project.
@param list
*/
void SearchAndReplaceWorker::replaceConductor(QList<Conductor *> list)
{
if (list.isEmpty() || !list.first()->diagram()) {
return;
}
2020-09-24 22:39:11 +02:00
QETProject *project_ = list.first()->diagram()->project();
for (Conductor *c : list) {
if (!c->diagram() ||
c->diagram()->project() != project_) {
return;
}
}
2020-09-24 22:39:11 +02:00
project_->undoStack()->beginMacro(QObject::tr("Chercher/remplacer les propriétés de conducteurs."));
for (Conductor *c : list)
{
ConductorProperties cp = applyChange(c->properties(), m_conductor_properties);
2020-09-24 22:39:11 +02:00
if (cp != c->properties())
{
QSet <Conductor *> conductors_list = c->relatedPotentialConductors(true);
conductors_list << c;
for (Conductor *cc : conductors_list)
{
QVariant old_value, new_value;
old_value.setValue(cc->properties());
new_value.setValue(cp);
project_->undoStack()->push(new QPropertyUndoCommand(cc, "properties", old_value, new_value));
}
}
}
project_->undoStack()->endMacro();
}
void SearchAndReplaceWorker::replaceConductor(Conductor *conductor)
{
QList<Conductor *>list;
list.append(conductor);
replaceConductor(list);
}
/**
2020-08-16 11:19:36 +02:00
@brief SearchAndReplaceWorker::replaceAdvanced
Apply the change of text according to the current advancedStruct
All items in the 4 list must belong to the same QETProject,
if not this function do nothing
2020-08-20 17:44:59 +02:00
@param diagrams :
@param elements :
@param texts :
@param conductors :
2020-08-16 11:19:36 +02:00
*/
2020-08-20 17:45:24 +02:00
void SearchAndReplaceWorker::replaceAdvanced(
QList<Diagram *> diagrams,
QList<Element *> elements,
QList<IndependentTextItem *> texts,
QList<Conductor *> conductors)
{
QETProject *project_ = nullptr;
2020-09-24 22:39:11 +02:00
2020-08-20 17:44:59 +02:00
//Some test to check if a least one list have one item
//and if all items belong to the same project
if (!diagrams.isEmpty()) {
project_ = diagrams.first()->project();
} else if (!elements.isEmpty() && elements.first()->diagram()) {
project_ = elements.first()->diagram()->project();
} else if (!texts.isEmpty() && texts.first()->diagram()) {
project_ = texts.first()->diagram()->project();
} else if (!conductors.isEmpty() && conductors.first()->diagram()) {
project_ = conductors.first()->diagram()->project();
} else {
return;
}
2020-09-24 22:39:11 +02:00
for (Diagram *dd : diagrams) {
if (dd->project() != project_) {
return;
}
}
for (Element *elmt : elements) {
if (!elmt->diagram() || elmt->diagram()->project() != project_) {
return;
}
}
for (IndependentTextItem *text : texts) {
if (!text->diagram() || text->diagram()->project() != project_) {
return;
}
}
for (Conductor *cc : conductors) {
if (!cc->diagram() || cc->diagram()->project() != project_) {
return;
}
}
2020-08-20 17:44:59 +02:00
//The end of the test
2020-09-24 22:39:11 +02:00
int who = m_advanced_struct.who;
if (who == -1) {
return;
}
2020-09-24 22:39:11 +02:00
project_->undoStack()->beginMacro(QObject::tr("Rechercher / remplacer avancé"));
if (who == 0)
{
for (Diagram *diagram : diagrams)
{
TitleBlockProperties old_properties = diagram->border_and_titleblock.exportTitleBlock();
TitleBlockProperties new_properties = replaceAdvanced(diagram);
if (old_properties != new_properties) {
project_->undoStack()->push(new ChangeTitleBlockCommand(diagram, old_properties, new_properties));
}
}
}
else if (who == 1)
{
for (Element *element : elements)
{
DiagramContext old_context = element->elementInformations();
DiagramContext new_context = replaceAdvanced(element);
if (old_context != new_context) {
project_->undoStack()->push(new ChangeElementInformationCommand(element, old_context, new_context));
}
}
}
else if (who == 2)
{
for (Conductor *conductor : conductors)
{
ConductorProperties old_properties = conductor->properties();
ConductorProperties new_properties = replaceAdvanced(conductor);
if (old_properties != new_properties)
{
QSet <Conductor *> potential_conductors = conductor->relatedPotentialConductors(true);
potential_conductors << conductor;
2020-09-24 22:39:11 +02:00
for (Conductor *c : potential_conductors)
{
QVariant old_value, new_value;
old_value.setValue(c->properties());
new_value.setValue(new_properties);
project_->undoStack()->push(new QPropertyUndoCommand(c, "properties", old_value, new_value));
}
}
}
}
else if (who == 3)
{
for (IndependentTextItem *text : texts)
{
QRegularExpression rx(m_advanced_struct.search);
2020-09-24 22:39:11 +02:00
if (!rx.isValid())
{
qWarning() <<QObject::tr("this is an error in the code")
<< rx.errorString()
<< rx.patternErrorOffset();
}
QString replace = m_advanced_struct.replace;
QString after = text->toPlainText();
after = after.replace(rx, replace);
2020-09-24 22:39:11 +02:00
if (after != text->toPlainText()) {
project_->undoStack()->push(new ChangeDiagramTextCommand(text, text->toPlainText(), after));
}
}
}
project_->undoStack()->endMacro();
}
/**
2020-08-16 11:19:36 +02:00
@brief SearchAndReplaceWorker::setupLineEdit
With search and replace, when the variable to edit is a text,
the editor is always the same no matter if it is for a folio,
element or conductor.
The editor is a QLineEdit to edit the text
and checkbox to erase the text if checked.
This function fill the editor, from the current string
@param l
@param cb
@param str
*/
2020-08-20 17:45:24 +02:00
void SearchAndReplaceWorker::setupLineEdit(QLineEdit *l,
QCheckBox *cb,
QString str)
{
l->setText(str);
cb->setChecked(str == eraseText() ? true : false);
l->setDisabled(str == eraseText() ? true : false);
}
ConductorProperties SearchAndReplaceWorker::invalidConductorProperties()
{
ConductorProperties cp;
2020-09-24 22:39:11 +02:00
//init with invalid value the conductor properties
cp.text_size = 0;
cp.text.clear();
cp.m_vertical_alignment = Qt::AlignAbsolute;
cp.m_horizontal_alignment = Qt::AlignAbsolute;
cp.verti_rotate_text = -1;
cp.horiz_rotate_text = -1;
cp.color = QColor();
cp.style = Qt::NoPen;
cp.cond_size = 0;
cp.m_color_2 = QColor();
cp.m_dash_size = 0;
2020-09-24 22:39:11 +02:00
return cp;
}
/**
2020-08-16 11:19:36 +02:00
@brief SearchAndReplaceWorker::applyChange
@param original : the original properties
2020-08-18 20:07:55 +02:00
@param change : the change properties, to be merged with original
2020-08-16 11:19:36 +02:00
@return a new conductor properties with the change applyed.
*/
2020-08-18 20:08:32 +02:00
ConductorProperties SearchAndReplaceWorker::applyChange(
const ConductorProperties &original,
const ConductorProperties &change)
{
ConductorProperties new_properties = original;
2020-09-24 22:39:11 +02:00
if (change.text_size > 2) {new_properties.text_size = change.text_size;}
new_properties.m_formula = applyChange(new_properties.m_formula, change.m_formula);
new_properties.text = applyChange(new_properties.text, change.text);
new_properties.m_show_text = change.m_show_text;
new_properties.m_function = applyChange(new_properties.m_function, change.m_function);
new_properties.m_tension_protocol = applyChange(new_properties.m_tension_protocol, change.m_tension_protocol);
new_properties.m_wire_color = applyChange(new_properties.m_wire_color, change.m_wire_color);
new_properties.m_wire_section = applyChange(new_properties.m_wire_section, change.m_wire_section);
if(change.m_vertical_alignment == Qt::AlignLeft ||
change.m_vertical_alignment == Qt::AlignRight) {new_properties.m_vertical_alignment = change.m_vertical_alignment;}
if(change.m_horizontal_alignment == Qt::AlignTop ||
change.m_horizontal_alignment == Qt::AlignBottom) {new_properties.m_horizontal_alignment = change.m_horizontal_alignment;}
if (change.verti_rotate_text >= 0) {new_properties.verti_rotate_text = change.verti_rotate_text;}
if (change.horiz_rotate_text >= 0) {new_properties.horiz_rotate_text = change.horiz_rotate_text;}
if (change.color.isValid()) {new_properties.color = change.color;}
if (change.style != Qt::NoPen) {new_properties.style = change.style;}
if (change.cond_size >= 0.4) {new_properties.cond_size = change.cond_size;}
new_properties.m_bicolor = change.m_bicolor;
if (change.m_color_2.isValid()) {new_properties.m_color_2 = change.m_color_2;}
if (change.m_dash_size >= 2) {new_properties.m_dash_size = change.m_dash_size;}
new_properties.singleLineProperties = change.singleLineProperties;
2020-09-24 22:39:11 +02:00
return new_properties;
}
/**
2020-08-16 11:19:36 +02:00
@brief SearchAndReplaceWorker::applyChange
@param original : the original string
@param change : the changed string:
@return the string to be use in the properties
*/
2020-08-18 20:08:32 +02:00
QString SearchAndReplaceWorker::applyChange(const QString &original,
const QString &change)
{
if (change.isEmpty()) {return original;}
else if (change == eraseText()) {return QString();}
else {return change;}
}
/**
2020-08-16 11:19:36 +02:00
@brief SearchAndReplaceWorker::replaceAdvanced
@param diagram
@return the titleblock properties with the change applied,
2020-08-18 20:07:55 +02:00
according to the state of m_advanced_struct
2020-08-16 11:19:36 +02:00
*/
TitleBlockProperties SearchAndReplaceWorker::replaceAdvanced(Diagram *diagram)
{
TitleBlockProperties p = diagram->border_and_titleblock.exportTitleBlock();
2020-09-24 22:39:11 +02:00
if (m_advanced_struct.who == 0)
{
QRegularExpression rx(m_advanced_struct.search);
2020-09-24 22:39:11 +02:00
if (!rx.isValid())
{
qWarning() <<QObject::tr("this is an error in the code")
<< rx.errorString()
<< rx.patternErrorOffset();
}
QString replace = m_advanced_struct.replace;
QString what = m_advanced_struct.what;
if (what == "title") {p.title = p.title.replace(rx, replace);}
else if (what == "author") {p.author = p.author.replace(rx, replace);}
else if (what == "filename") {p.filename = p.filename.replace(rx, replace);}
else if (what == "folio") {p.folio = p.folio.replace(rx, replace);}
else if (what == "plant") {p.plant = p.plant.replace(rx, replace);}
else if (what == "locmach") {p.locmach = p.locmach.replace(rx, replace);}
else if (what == "indexrev") {p.indexrev = p.indexrev.replace(rx, replace);}
}
return p;
}
/**
2020-08-16 11:19:36 +02:00
@brief SearchAndReplaceWorker::replaceAdvanced
@param element
@return The diagram context with the change applied,
2020-08-18 20:07:55 +02:00
according to the state of m_advanced_struct
2020-08-16 11:19:36 +02:00
*/
DiagramContext SearchAndReplaceWorker::replaceAdvanced(Element *element)
{
DiagramContext context = element->elementInformations();
2020-09-24 22:39:11 +02:00
if (m_advanced_struct.who == 1)
{
QString what = m_advanced_struct.what;
if (context.contains(what))
{
QRegularExpression rx(m_advanced_struct.search);
2020-09-24 22:39:11 +02:00
if (!rx.isValid())
{
qWarning() <<QObject::tr("this is an error in the code")
<< rx.errorString()
<< rx.patternErrorOffset();
}
QString replace = m_advanced_struct.replace;
QString value = context[what].toString();
context.addValue(what, value.replace(rx, replace));
}
}
2020-09-24 22:39:11 +02:00
return context;
}
/**
2020-08-16 11:19:36 +02:00
@brief SearchAndReplaceWorker::replaceAdvanced
@param conductor
@return the conductor properties with the change applied,
2020-08-18 20:07:55 +02:00
according to the state of m_advanced_struct
2020-08-16 11:19:36 +02:00
*/
2020-08-18 20:08:32 +02:00
ConductorProperties SearchAndReplaceWorker::replaceAdvanced(
Conductor *conductor)
{
ConductorProperties properties = conductor->properties();
2020-09-24 22:39:11 +02:00
if (m_advanced_struct.who == 2)
{
QRegularExpression rx(m_advanced_struct.search);
2020-09-24 22:39:11 +02:00
if (!rx.isValid())
{
qWarning() <<QObject::tr("this is an error in the code")
<< rx.errorString()
<< rx.patternErrorOffset();
}
QString what = m_advanced_struct.what;
QString replace = m_advanced_struct.replace;
2020-09-24 22:39:11 +02:00
if (what == "formula") {properties.m_formula.replace(rx, replace);}
else if (what == "text") {properties.text.replace(rx, replace);}
else if (what == "function") {properties.m_function.replace(rx, replace);}
else if (what == "tension/protocol") {properties.m_tension_protocol.replace(rx, replace);}
2020-07-04 20:43:40 +02:00
else if (what == "conductor_color") {properties.m_wire_color.replace(rx, replace);}
else if (what == "conductor_section") {properties.m_wire_section.replace(rx, replace);}
}
2020-09-24 22:39:11 +02:00
return properties;
}