diff --git a/sources/SearchAndReplace/searchandreplaceworker.cpp b/sources/SearchAndReplace/searchandreplaceworker.cpp index 46ea7b772..3b12b0d02 100644 --- a/sources/SearchAndReplace/searchandreplaceworker.cpp +++ b/sources/SearchAndReplace/searchandreplaceworker.cpp @@ -18,6 +18,9 @@ #include "searchandreplaceworker.h" #include "diagram.h" #include "changetitleblockcommand.h" +#include "changeelementinformationcommand.h" +#include "element.h" +#include "qetapp.h" SearchAndReplaceWorker::SearchAndReplaceWorker() {} @@ -138,3 +141,67 @@ void SearchAndReplaceWorker::replaceDiagram(Diagram *diagram) list.append(diagram); replaceDiagram(list); } + +/** + * @brief SearchAndReplaceWorker::replaceElement + * Replace all properties of each elements in @list + * 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 list) +{ + if (list.isEmpty() || !list.first()->diagram()) { + return; + } + + QETProject *project_ = list.first()->diagram()->project(); + for (Element *elmt : list) + { + if (elmt->diagram()) { + if (elmt->diagram()->project() != project_) { + return; + } + } + } + + 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 || + elmt->linkType() == Element::Terminale) + { + DiagramContext old_context; + DiagramContext new_context = old_context = elmt->elementInformations(); + for (QString key : QETApp::elementInfoKeys()) + { + QString value = m_element_context.value(key).toString(); + if (value.isEmpty()) { + continue; + } + + if (value == eraseText()) { + new_context.addValue(key, QString()); + } else { + new_context.addValue(key, value); + } + } + + 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) +{ + QListlist; + list.append(element); + replaceElement(list); +} diff --git a/sources/SearchAndReplace/searchandreplaceworker.h b/sources/SearchAndReplace/searchandreplaceworker.h index fa2bb89ff..f009a67a4 100644 --- a/sources/SearchAndReplace/searchandreplaceworker.h +++ b/sources/SearchAndReplace/searchandreplaceworker.h @@ -23,6 +23,7 @@ #include "titleblockproperties.h" class Diagram; +class Element; /** * @brief The SearchAndReplaceWorker class @@ -36,12 +37,15 @@ class SearchAndReplaceWorker void clear(); void replaceDiagram(QList diagram_list); void replaceDiagram(Diagram *diagram); + void replaceElement(QList list); + void replaceElement(Element *element); static QString eraseText() {return QString("XXXXXXXXXXXXXXXXXXX");} static QDate eraseDate() {return QDate(1900, 1, 1);} private: TitleBlockProperties m_titleblock_properties; + DiagramContext m_element_context; friend class SearchAndReplaceWidget; }; diff --git a/sources/SearchAndReplace/ui/replaceelementdialog.cpp b/sources/SearchAndReplace/ui/replaceelementdialog.cpp new file mode 100644 index 000000000..d237e720a --- /dev/null +++ b/sources/SearchAndReplace/ui/replaceelementdialog.cpp @@ -0,0 +1,86 @@ +/* + Copyright 2006-2018 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 . +*/ +#include "replaceelementdialog.h" +#include "ui_replaceelementdialog.h" +#include "replaceelementdialog.h" +#include "elementinfopartwidget.h" +#include "qetapp.h" +#include "searchandreplaceworker.h" + +#include + +ReplaceElementDialog::ReplaceElementDialog(DiagramContext context, QWidget *parent) : + QDialog(parent), + ui(new Ui::ReplaceElementDialog) +{ + ui->setupUi(this); + buildWidget(); + setContext(context); +} + +ReplaceElementDialog::~ReplaceElementDialog() +{ + delete ui; +} + +/** + * @brief ReplaceElementDialog::setContext + * Set the current diagram context to be edited + * @param context + */ +void ReplaceElementDialog::setContext(DiagramContext context) +{ + m_context = context; + + for (ElementInfoPartWidget *eipw : m_eipw_list) + { + eipw->setText(m_context[eipw->key()].toString()); + eipw->setEraseTextChecked(false); + } +} + +/** + * @brief ReplaceElementDialog::context + * @return The edited diagram context + */ +DiagramContext ReplaceElementDialog::context() const +{ + DiagramContext context; + for (ElementInfoPartWidget *eipw : m_eipw_list) { + context.addValue(eipw->key(), eipw->text()); + } + + return context; +} + +void ReplaceElementDialog::buildWidget() +{ + ui->m_button_box->disconnect(); + connect(ui->m_button_box, &QDialogButtonBox::clicked, [this](QAbstractButton *button_) + { + this->done(ui->m_button_box->buttonRole(button_)); + }); + + for (QString str : QETApp::elementInfoKeys()) + { + ElementInfoPartWidget *eipw = new ElementInfoPartWidget(str, QETApp::elementTranslatedInfoKey(str), this); + eipw->setEraseTextVisible(true); + ui->m_scroll_layout->addWidget(eipw); + m_eipw_list << eipw; + } +} diff --git a/sources/SearchAndReplace/ui/replaceelementdialog.h b/sources/SearchAndReplace/ui/replaceelementdialog.h new file mode 100644 index 000000000..93caab7a4 --- /dev/null +++ b/sources/SearchAndReplace/ui/replaceelementdialog.h @@ -0,0 +1,50 @@ +/* + Copyright 2006-2018 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 . +*/ +#ifndef REPLACEELEMENTDIALOG_H +#define REPLACEELEMENTDIALOG_H + +#include +#include "diagramcontext.h" + +class ElementInfoPartWidget; + +namespace Ui { + class ReplaceElementDialog; +} + +class ReplaceElementDialog : public QDialog +{ + Q_OBJECT + + public: + explicit ReplaceElementDialog(DiagramContext context, QWidget *parent = nullptr); + ~ReplaceElementDialog(); + + void setContext(DiagramContext context); + DiagramContext context() const; + + private: + void buildWidget(); + + + Ui::ReplaceElementDialog *ui; + QList m_eipw_list; + DiagramContext m_context; +}; + +#endif // REPLACEELEMENTDIALOG_H diff --git a/sources/SearchAndReplace/ui/replaceelementdialog.ui b/sources/SearchAndReplace/ui/replaceelementdialog.ui new file mode 100644 index 000000000..9ff7eb47b --- /dev/null +++ b/sources/SearchAndReplace/ui/replaceelementdialog.ui @@ -0,0 +1,85 @@ + + + ReplaceElementDialog + + + + 0 + 0 + 300 + 400 + + + + + 300 + 400 + + + + + + + true + + + + + 0 + 0 + 280 + 351 + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::Reset + + + + + + + + + m_button_box + accepted() + ReplaceElementDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + m_button_box + rejected() + ReplaceElementDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/sources/SearchAndReplace/ui/searchandreplacewidget.cpp b/sources/SearchAndReplace/ui/searchandreplacewidget.cpp index aa0b84291..d81cc0426 100644 --- a/sources/SearchAndReplace/ui/searchandreplacewidget.cpp +++ b/sources/SearchAndReplace/ui/searchandreplacewidget.cpp @@ -25,6 +25,8 @@ #include "independenttextitem.h" #include "conductor.h" #include "replacefoliowidget.h" +#include "replaceelementdialog.h" +#include "qetapp.h" #include @@ -331,6 +333,7 @@ void SearchAndReplaceWidget::addElement(Element *element) str = tr("Inconnue"); qtwi->setText(0, str); qtwi->setCheckState(0, Qt::Checked); + qtwi->setData(0, Qt::UserRole, searchTerms(element)); } /** @@ -367,7 +370,10 @@ void SearchAndReplaceWidget::search() qtwi->setHidden(false); setVisibleAllParents(qtwi); } - for (QTreeWidgetItem *qtwi : m_diagram_hash.keys()) //Search for diagrams items + + QList qtwi_list = m_diagram_hash.keys(); + qtwi_list.append(m_element_hash.keys()); + for (QTreeWidgetItem *qtwi : qtwi_list) { QStringList list = qtwi->data(0, Qt::UserRole).toStringList(); if (!list.filter(str, Qt::CaseInsensitive).isEmpty()) @@ -631,6 +637,26 @@ QStringList SearchAndReplaceWidget::searchTerms(Diagram *diagram) const return list; } +/** + * @brief SearchAndReplaceWidget::searchTerms + * @param element + * @return All QString use as terms for search + */ +QStringList SearchAndReplaceWidget::searchTerms(Element *element) const +{ + QStringList list; + DiagramContext context = element->elementInformations(); + for (QString key : QETApp::elementInfoKeys()) + { + QString str = context.value(key).toString(); + if (!str.isEmpty()) { + list.append(str); + } + } + + return list; +} + void SearchAndReplaceWidget::on_m_quit_button_clicked() { this->setHidden(true); } @@ -813,6 +839,10 @@ void SearchAndReplaceWidget::on_m_folio_pb_clicked() } } +/** + * @brief SearchAndReplaceWidget::on_m_replace_pb_clicked + * Replace the current selection + */ void SearchAndReplaceWidget::on_m_replace_pb_clicked() { QTreeWidgetItem *qtwi = ui->m_tree_widget->currentItem(); @@ -829,26 +859,86 @@ void SearchAndReplaceWidget::on_m_replace_pb_clicked() m_worker.replaceDiagram(d.data()); } } + else if (ui->m_element_pb->text().endsWith(tr(" [Édité]")) && + m_element_hash.keys().contains(qtwi)) + { + QPointer e = m_element_hash.value(qtwi); + if (e) { + m_worker.replaceElement(e.data()); + } + } } activateNextChecked(); ui->m_replace_pb->setEnabled(ui->m_next_pb->isEnabled()); } +/** + * @brief SearchAndReplaceWidget::on_m_replace_all_pb_clicked + * Replace all checked item + */ void SearchAndReplaceWidget::on_m_replace_all_pb_clicked() { if (ui->m_folio_pb->text().endsWith(tr(" [Édité]"))) { - QList d; + QList diagram_list; for (QTreeWidgetItem *qtwi : m_diagram_hash.keys()) { if (!qtwi->isHidden() && qtwi->checkState(0) == Qt::Checked) { QPointer p = m_diagram_hash.value(qtwi); if (p) { - d.append(p.data()); + diagram_list.append(p.data()); } } } - m_worker.replaceDiagram(d); + m_worker.replaceDiagram(diagram_list); + } + if (ui->m_element_pb->text().endsWith(tr(" [Édité]"))) + { + QList element_list; + for (QTreeWidgetItem *qtwi : m_element_hash.keys()) + { + if (!qtwi->isHidden() && qtwi->checkState(0) == Qt::Checked) + { + QPointer p = m_element_hash.value(qtwi); + if (p) { + element_list.append(p.data()); + } + } + } + m_worker.replaceElement(element_list); + } + + //Change was made, we reload the panel + //and search again to keep up to date the tree widget + //and the match item of search + QString txt = ui->m_search_le->text(); + on_m_reload_pb_clicked(); + ui->m_search_le->setText(txt); + search(); +} + +void SearchAndReplaceWidget::on_m_element_pb_clicked() +{ + ReplaceElementDialog *dialog = new ReplaceElementDialog(m_worker.m_element_context, this); + + int result = dialog->exec(); + if (result == QDialogButtonBox::AcceptRole) + { + QString text = ui->m_element_pb->text(); + if (!text.endsWith(tr(" [Édité]"))) { + text.append(tr(" [Édité]")); + } + ui->m_element_pb->setText(text); + m_worker.m_element_context = dialog->context(); + } + else if (result == QDialogButtonBox::ResetRole) + { + QString text = ui->m_element_pb->text(); + if (text.endsWith(tr(" [Édité]"))) { + text.remove(tr(" [Édité]")); + } + ui->m_element_pb->setText(text); + m_worker.m_element_context = DiagramContext(); } } diff --git a/sources/SearchAndReplace/ui/searchandreplacewidget.h b/sources/SearchAndReplace/ui/searchandreplacewidget.h index a2cdea33a..a600a2541 100644 --- a/sources/SearchAndReplace/ui/searchandreplacewidget.h +++ b/sources/SearchAndReplace/ui/searchandreplacewidget.h @@ -61,6 +61,7 @@ class SearchAndReplaceWidget : public QWidget void updateParentCheckState(QTreeWidgetItem *item, bool all_parents = true); void activateNextChecked(); QStringList searchTerms(Diagram *diagram) const; + QStringList searchTerms(Element *element) const; private slots: void on_m_quit_button_clicked(); @@ -73,6 +74,7 @@ class SearchAndReplaceWidget : public QWidget void on_m_folio_pb_clicked(); void on_m_replace_pb_clicked(); void on_m_replace_all_pb_clicked(); + void on_m_element_pb_clicked(); private: Ui::SearchAndReplaceWidget *ui; diff --git a/sources/SearchAndReplace/ui/searchandreplacewidget.ui b/sources/SearchAndReplace/ui/searchandreplacewidget.ui index e4507f295..8a4d4c042 100644 --- a/sources/SearchAndReplace/ui/searchandreplacewidget.ui +++ b/sources/SearchAndReplace/ui/searchandreplacewidget.ui @@ -184,7 +184,7 @@ - false + true Élément diff --git a/sources/ui/elementinfopartwidget.cpp b/sources/ui/elementinfopartwidget.cpp index 149595886..457e0aacc 100644 --- a/sources/ui/elementinfopartwidget.cpp +++ b/sources/ui/elementinfopartwidget.cpp @@ -1,5 +1,5 @@ /* - Copyright 2006-2017 The QElectroTech Team + Copyright 2006-2018 The QElectroTech Team This file is part of QElectroTech. QElectroTech is free software: you can redistribute it and/or modify @@ -19,6 +19,7 @@ #include #include "ui_elementinfopartwidget.h" +#include "searchandreplaceworker.h" /** @@ -35,6 +36,7 @@ ElementInfoPartWidget::ElementInfoPartWidget(QString key, const QString& transla { ui->setupUi(this); ui->label_->setText(translated_key); + ui->m_erase_text->setVisible(false); connect(ui->line_edit, &QLineEdit::textEdited, this, &ElementInfoPartWidget::textEdited); connect(ui->line_edit, &QLineEdit::textChanged, this, &ElementInfoPartWidget::textChanged); @@ -54,8 +56,13 @@ ElementInfoPartWidget::~ElementInfoPartWidget() * Set text to line edit * @param txt */ -void ElementInfoPartWidget::setText(const QString &txt) { - ui->line_edit->setText(txt); +void ElementInfoPartWidget::setText(const QString &txt) +{ + if (txt == SearchAndReplaceWorker::eraseText()) { + ui->m_erase_text->setChecked(true); + } else { + ui->line_edit->setText(txt); + } } /** @@ -79,8 +86,7 @@ void ElementInfoPartWidget::setFocusTolineEdit() { * enable the line edit * @param e */ -void ElementInfoPartWidget::setEnabled(bool e) -{ +void ElementInfoPartWidget::setEnabled(bool e) { ui->line_edit->setEnabled(e); } @@ -89,7 +95,36 @@ void ElementInfoPartWidget::setEnabled(bool e) * disable the line edit * @param d */ -void ElementInfoPartWidget::setDisabled(bool d) -{ +void ElementInfoPartWidget::setDisabled(bool d) { ui->line_edit->setDisabled(d); } + +/** + * @brief ElementInfoPartWidget::setEraseTextVisible + * @param visible + */ +void ElementInfoPartWidget::setEraseTextVisible(bool visible) { + ui->m_erase_text->setVisible(visible); +} + +/** + * @brief ElementInfoPartWidget::setEraseTextChecked + * @param check + */ +void ElementInfoPartWidget::setEraseTextChecked(bool check) { + ui->m_erase_text->setChecked(check); +} + +/** + * @brief ElementInfoPartWidget::EraseTextCheckState + * @return + */ +Qt::CheckState ElementInfoPartWidget::EraseTextCheckState() const { + return ui->m_erase_text->checkState(); +} + +void ElementInfoPartWidget::on_m_erase_text_clicked() +{ + ui->line_edit->setText(ui->m_erase_text->isChecked() ? SearchAndReplaceWorker::eraseText() : QString()); + ui->line_edit->setDisabled(ui->m_erase_text->isChecked()); +} diff --git a/sources/ui/elementinfopartwidget.h b/sources/ui/elementinfopartwidget.h index 8e607d3b7..b79c788cd 100644 --- a/sources/ui/elementinfopartwidget.h +++ b/sources/ui/elementinfopartwidget.h @@ -1,5 +1,5 @@ /* - Copyright 2006-2017 The QElectroTech Team + Copyright 2006-2018 The QElectroTech Team This file is part of QElectroTech. QElectroTech is free software: you can redistribute it and/or modify @@ -39,13 +39,19 @@ class ElementInfoPartWidget : public QWidget void setFocusTolineEdit(); void setEnabled(bool e); void setDisabled(bool d); + void setEraseTextVisible (bool visible); + void setEraseTextChecked (bool check); + Qt::CheckState EraseTextCheckState ()const; signals: void textEdited (const QString & text); void textChanged (const QString & text); //ATTRIBUTES - private: + private slots: + void on_m_erase_text_clicked(); + + private: Ui::ElementInfoPartWidget *ui; QString key_; }; diff --git a/sources/ui/elementinfopartwidget.ui b/sources/ui/elementinfopartwidget.ui index e73175bc1..d99ff9141 100644 --- a/sources/ui/elementinfopartwidget.ui +++ b/sources/ui/elementinfopartwidget.ui @@ -6,14 +6,14 @@ 0 0 - 300 - 400 + 65 + 44 - 300 - 400 + 0 + 0 @@ -41,6 +41,13 @@ 2 + + + + true + + + @@ -48,10 +55,13 @@ - - - - true + + + + Supprimer ce texte + + +