diff --git a/sources/elementtextpattern.cpp b/sources/elementtextpattern.cpp index e4b3a6ef0..30d1f6f0b 100644 --- a/sources/elementtextpattern.cpp +++ b/sources/elementtextpattern.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 @@ -23,6 +23,8 @@ #include "elementtextitemgroup.h" #include "addelementtextcommand.h" #include "diagram.h" +#include "importelementtextpatterndialog.h" +#include "deleteqgraphicsitemcommand.h" #include #include @@ -122,7 +124,6 @@ QDomDocument ExportElementTextPattern::xmlConf() const //*******************// //******IMPORT*******// //*******************// - ImportElementTextPattern::ImportElementTextPattern(Element *elmt): m_element(elmt) { @@ -146,14 +147,15 @@ ImportElementTextPattern::ImportElementTextPattern(Element *elmt): } bool ok=false; + bool erase = false; //Remove the .xml extention of the files result.replaceInStrings(".xml", ""); - QString name = getName(result, ok); + QString name = getName(result, &ok, &erase); if(!ok || name.isEmpty()) return; else - apply(name); + apply(name, erase); } /** @@ -162,15 +164,14 @@ ImportElementTextPattern::ImportElementTextPattern(Element *elmt): * @param ok * @return */ -QString ImportElementTextPattern::getName(const QStringList& list, bool &ok) const +QString ImportElementTextPattern::getName(const QStringList& list, bool *ok, bool *erase) const { - return QInputDialog::getItem(parentWidget(), - QObject::tr("Séléctionner une configuration de textes"), - QObject::tr("Séléctionner la configuration de textes à ajouter à l'élément"), - list, - 0, - false, - &ok); + return ImportElementTextPatternDialog::getItem(parentWidget(), + QObject::tr("Séléctionner une configuration de textes"), + QObject::tr("Séléctionner la configuration de textes à ajouter à l'élément"), + list, + ok, + erase); } QWidget *ImportElementTextPattern::parentWidget() const @@ -182,12 +183,14 @@ QWidget *ImportElementTextPattern::parentWidget() const return parent; } + /** * @brief ImportElementTextPattern::apply * Apply the user choice * @param name : the name of the selected pattern + * @param erase : erase the existing texts and groups of element. */ -void ImportElementTextPattern::apply(QString name) const +void ImportElementTextPattern::apply(QString name, bool erase) const { if(!name.endsWith(".xml")) name.append(".xml"); @@ -235,6 +238,20 @@ void ImportElementTextPattern::apply(QString name) const QUndoStack &undo_stack = m_element->diagram()->undoStack(); undo_stack.beginMacro(QObject::tr("Importer la configuration de texte : %1").arg(name.remove(".xml"))); + //erase existing texts and groups + if (erase) + { + for (ElementTextItemGroup *group : m_element->textGroups()) { + undo_stack.push(new RemoveTextsGroupCommand(m_element, group)); + } + for (DynamicElementTextItem *deti : m_element->dynamicTextItems()) + { + DiagramContent dc; + dc.m_element_texts << deti; + undo_stack.push(new DeleteQGraphicsItemCommand(m_element->diagram(), dc)); + } + } + //Add the texts to element for(const QDomElement& text : texts) { diff --git a/sources/elementtextpattern.h b/sources/elementtextpattern.h index fb780412b..a3b8aa758 100644 --- a/sources/elementtextpattern.h +++ b/sources/elementtextpattern.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 @@ -45,9 +45,9 @@ class ImportElementTextPattern ImportElementTextPattern(Element *elmt); private: - QString getName(const QStringList& list, bool &ok) const; + QString getName(const QStringList& list, bool *ok, bool *erase) const; QWidget *parentWidget() const; - void apply(QString name) const; + void apply(QString name, bool erase = false) const; private: Element *m_element = nullptr; diff --git a/sources/ui/importelementtextpatterndialog.cpp b/sources/ui/importelementtextpatterndialog.cpp new file mode 100644 index 000000000..ce97c912c --- /dev/null +++ b/sources/ui/importelementtextpatterndialog.cpp @@ -0,0 +1,74 @@ +/* + 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 "importelementtextpatterndialog.h" +#include "ui_importelementtextpatterndialog.h" + +ImportElementTextPatternDialog::ImportElementTextPatternDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::ImportElementTextPatternDialog) { + ui->setupUi(this); +} + +ImportElementTextPatternDialog::~ImportElementTextPatternDialog() +{ + delete ui; +} + +/** + * @brief ImportElementTextPatternDialog::getItem + * For all arguments see QInputDialog::getItem, except for erase, they store the state of the check box. + * @param parent + * @param title + * @param label + * @param items + * @param ok + * @param erase + * @return + */ +QString ImportElementTextPatternDialog::getItem(QWidget *parent, const QString &title, const QString &label, const QStringList &items, bool *ok, bool *erase) +{ + QString text(items.value(0)); + + QScopedPointer dialog(new ImportElementTextPatternDialog(parent)); + dialog->setWindowTitle(title); + dialog->setLabelText(label); + dialog->setComboBoxItems(items); + dialog->setInputMethodHints(Qt::ImhNone); + const int ret = dialog->exec(); + if (ok) + *ok = !!ret; + if(erase) + *erase = dialog->ui->m_erase_existing_text->isChecked(); + if (ret) { + return dialog->textValue(); + } else { + return text; + } +} + +void ImportElementTextPatternDialog::setLabelText(const QString &label) { + ui->m_label->setText(label); +} + +void ImportElementTextPatternDialog::setComboBoxItems(const QStringList &items) { + ui->m_combo_box->addItems(items); +} + +QString ImportElementTextPatternDialog::textValue() const { + return ui->m_combo_box->currentText(); +} diff --git a/sources/ui/importelementtextpatterndialog.h b/sources/ui/importelementtextpatterndialog.h new file mode 100644 index 000000000..f6453bff9 --- /dev/null +++ b/sources/ui/importelementtextpatterndialog.h @@ -0,0 +1,52 @@ +/* + 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 IMPORTELEMENTTEXTPATTERNDIALOG_H +#define IMPORTELEMENTTEXTPATTERNDIALOG_H + +#include + +namespace Ui { + class ImportElementTextPatternDialog; +} + +/** + * @brief The ImportElementTextPatternDialog class + * A dialog use for ask user to select a element text pattern. + * This dialog is highly inspired from QInputDialog::getItem. + * In fact this the same + a check box. + */ +class ImportElementTextPatternDialog : public QDialog +{ + Q_OBJECT + + public: + explicit ImportElementTextPatternDialog (QWidget *parent = nullptr); + ~ImportElementTextPatternDialog(); + + static QString getItem (QWidget *parent, const QString &title, const QString &label, + const QStringList &items, bool *ok = nullptr, bool *erase = nullptr); + + void setLabelText (const QString &label); + void setComboBoxItems (const QStringList &items); + QString textValue() const; + + private: + Ui::ImportElementTextPatternDialog *ui; +}; + +#endif // IMPORTELEMENTTEXTPATTERNDIALOG_H diff --git a/sources/ui/importelementtextpatterndialog.ui b/sources/ui/importelementtextpatterndialog.ui new file mode 100644 index 000000000..6feec4cc7 --- /dev/null +++ b/sources/ui/importelementtextpatterndialog.ui @@ -0,0 +1,81 @@ + + + ImportElementTextPatternDialog + + + + 0 + 0 + 187 + 116 + + + + Dialog + + + + + + TextLabel + + + + + + + + + + Écraser les textes existants + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + ImportElementTextPatternDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + ImportElementTextPatternDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + +