Search and replace : Element properties can be changed (and mass changed) through the search and replace widget.

Also improve the search for element


git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5576 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2018-11-09 18:50:37 +00:00
parent b276d6517a
commit 1ead2aa0bb
11 changed files with 457 additions and 22 deletions

View File

@@ -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<Element *> 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)
{
QList<Element *>list;
list.append(element);
replaceElement(list);
}

View File

@@ -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 *> diagram_list);
void replaceDiagram(Diagram *diagram);
void replaceElement(QList <Element *> 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;
};

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#include "replaceelementdialog.h"
#include "ui_replaceelementdialog.h"
#include "replaceelementdialog.h"
#include "elementinfopartwidget.h"
#include "qetapp.h"
#include "searchandreplaceworker.h"
#include <QAbstractButton>
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;
}
}

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef REPLACEELEMENTDIALOG_H
#define REPLACEELEMENTDIALOG_H
#include <QDialog>
#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 <ElementInfoPartWidget *> m_eipw_list;
DiagramContext m_context;
};
#endif // REPLACEELEMENTDIALOG_H

View File

@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ReplaceElementDialog</class>
<widget class="QDialog" name="ReplaceElementDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>300</width>
<height>400</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>300</width>
<height>400</height>
</size>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QScrollArea" name="scrollArea">
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>280</width>
<height>351</height>
</rect>
</property>
<layout class="QVBoxLayout" name="m_scroll_layout"/>
</widget>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="m_button_box">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::Reset</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>m_button_box</sender>
<signal>accepted()</signal>
<receiver>ReplaceElementDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>m_button_box</sender>
<signal>rejected()</signal>
<receiver>ReplaceElementDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@@ -25,6 +25,8 @@
#include "independenttextitem.h"
#include "conductor.h"
#include "replacefoliowidget.h"
#include "replaceelementdialog.h"
#include "qetapp.h"
#include <QSettings>
@@ -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<QTreeWidgetItem *> 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<Element> 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 <Diagram *> d;
QList <Diagram *> diagram_list;
for (QTreeWidgetItem *qtwi : m_diagram_hash.keys())
{
if (!qtwi->isHidden() && qtwi->checkState(0) == Qt::Checked)
{
QPointer <Diagram> 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 *> element_list;
for (QTreeWidgetItem *qtwi : m_element_hash.keys())
{
if (!qtwi->isHidden() && qtwi->checkState(0) == Qt::Checked)
{
QPointer <Element> 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();
}
}

View File

@@ -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;

View File

@@ -184,7 +184,7 @@
<item row="1" column="4">
<widget class="QPushButton" name="m_element_pb">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="text">
<string>Élément</string>

View File

@@ -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 <utility>
#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,9 +56,14 @@ ElementInfoPartWidget::~ElementInfoPartWidget()
* Set text to line edit
* @param txt
*/
void ElementInfoPartWidget::setText(const QString &txt) {
void ElementInfoPartWidget::setText(const QString &txt)
{
if (txt == SearchAndReplaceWorker::eraseText()) {
ui->m_erase_text->setChecked(true);
} else {
ui->line_edit->setText(txt);
}
}
/**
* @brief ElementInfoPartWidget::text
@@ -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());
}

View File

@@ -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,12 +39,18 @@ 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 slots:
void on_m_erase_text_clicked();
private:
Ui::ElementInfoPartWidget *ui;
QString key_;

View File

@@ -6,14 +6,14 @@
<rect>
<x>0</x>
<y>0</y>
<width>300</width>
<height>400</height>
<width>65</width>
<height>44</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>300</width>
<height>400</height>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="windowTitle">
@@ -41,6 +41,13 @@
<property name="verticalSpacing">
<number>2</number>
</property>
<item row="2" column="0">
<widget class="QLineEdit" name="line_edit">
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_">
<property name="text">
@@ -48,10 +55,13 @@
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLineEdit" name="line_edit">
<property name="clearButtonEnabled">
<bool>true</bool>
<item row="2" column="1">
<widget class="QCheckBox" name="m_erase_text">
<property name="toolTip">
<string>Supprimer ce texte</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>