diff --git a/sources/ui/elementinfowidget.h b/sources/ui/elementinfowidget.h index ff413983d..4f053cac6 100644 --- a/sources/ui/elementinfowidget.h +++ b/sources/ui/elementinfowidget.h @@ -45,7 +45,8 @@ class ElementInfoWidget : public PropertiesEditorWidget ~ElementInfoWidget(); void apply(); - QUndoCommand* associatedUndo () const; + QUndoCommand *associatedUndo () const; + QString title() const {return tr("Informations");} protected: virtual bool event(QEvent *event); diff --git a/sources/ui/elementpropertiesdialog.cpp b/sources/ui/elementpropertiesdialog.cpp index f380dc026..356c87731 100644 --- a/sources/ui/elementpropertiesdialog.cpp +++ b/sources/ui/elementpropertiesdialog.cpp @@ -16,13 +16,10 @@ along with QElectroTech. If not, see . */ #include "elementpropertiesdialog.h" -#include "ghostelement.h" -#include "qeticons.h" -#include "diagramposition.h" -#include "diagram.h" -#include "elementinfowidget.h" -#include "masterpropertieswidget.h" -#include "linksingleelementwidget.h" +#include "elementpropertieswidget.h" + +#include +#include /** * @brief ElementPropertiesDialog::ElementPropertiesDialog @@ -32,123 +29,18 @@ */ ElementPropertiesDialog::ElementPropertiesDialog(Element *elmt, QWidget *parent) : QDialog(parent), - element_ (elmt), - diagram_ (elmt->diagram()) + element_ (elmt) { - eiw_ = 0; - mpw_ = 0; - lsew_ = 0; - buildInterface(); -} + m_editor = new ElementPropertiesWidget(elmt, this); -/** - * @brief ElementPropertiesDialog::generalWidget - * build the widget for the tab général - * @return - */ -QWidget* ElementPropertiesDialog::generalWidget() { - CustomElement *custom_element = qobject_cast(element_); - GhostElement *ghost_element = qobject_cast(element_); + connect(m_editor, SIGNAL(editElementRequired(ElementsLocation)), this , SLOT(editElement(ElementsLocation))); + connect(m_editor, SIGNAL(findElementRequired(ElementsLocation)), this, SLOT(findInPanel(ElementsLocation))); - // type de l'element - QString description_string; - if (ghost_element) { - description_string += tr("Élément manquant"); - } else { - description_string += tr("Élément"); - } - description_string += "\n"; - - // some element characteristic - description_string += QString(tr("Nom : %1\n")).arg(element_ -> name()); - int folio_index = diagram_ -> folioIndex(); - if (folio_index != -1) { - description_string += QString(tr("Folio : %1\n")).arg(folio_index + 1); - } - description_string += QString(tr("Position : %1\n")).arg(diagram_ -> convertPosition(element_ -> scenePos()).toString()); - description_string += QString(tr("Dimensions : %1*%2\n")).arg(element_ -> size().width()).arg(element_ -> size().height()); - description_string += QString(tr("Bornes : %1\n")).arg(element_ -> terminals().count()); - description_string += QString(tr("Champs de texte : %1\n")).arg(element_ -> texts().count()); - - if (custom_element) { - description_string += QString(tr("Emplacement : %1\n")).arg(custom_element -> location().toString()); - } - - // widget himself - QWidget *general_widget = new QWidget (tab_); - QVBoxLayout *vlayout_ = new QVBoxLayout (general_widget); - general_widget -> setLayout(vlayout_); - - //widget for the text - vlayout_->addWidget(new QLabel (description_string, general_widget)); - - //widget for the pixmap - QLabel *pix = new QLabel(general_widget); - pix->setPixmap(element_->pixmap()); - vlayout_->addWidget(pix, 0, Qt::AlignHCenter); - vlayout_ -> addStretch(); - - //button widget - find_in_panel = new QPushButton(QET::Icons::ZoomDraw, tr("Retrouver dans le panel"), general_widget); - connect(find_in_panel, SIGNAL(clicked()), this, SLOT(findInPanel())); - edit_element = new QPushButton(QET::Icons::ElementEdit, tr("Éditer l'élément"), general_widget); - connect(edit_element, SIGNAL(clicked()), this, SLOT(editElement())); - QHBoxLayout *hlayout_ = new QHBoxLayout; - hlayout_->addWidget(find_in_panel); - hlayout_->addWidget(edit_element); - - vlayout_->addLayout(hlayout_); - return general_widget; -} - -/** - * @brief ElementPropertiesDialog::buildInterface - *build the interface of this dialog, the main tab can have - *different tab according to the edited element - */ -void ElementPropertiesDialog::buildInterface() { - - setWindowTitle(tr("Propriétés de l'élément")); - tab_ = new QTabWidget(this); - - //Add tab according to the element - switch (element_ -> linkType()) { - case Element::Simple: - eiw_ = new ElementInfoWidget(element_, this); - tab_ -> addTab(eiw_, tr("Information")); - break; - case Element::NextReport: - lsew_ = new LinkSingleElementWidget(element_, this); - tab_ -> addTab(lsew_, tr("Report de folio")); - break; - case Element::PreviousReport: - lsew_ = new LinkSingleElementWidget(element_, this); - tab_ -> addTab(lsew_, tr("Report de folio")); - break; - case Element::Master: - mpw_ = new MasterPropertiesWidget(element_, this); - tab_ -> addTab(mpw_, tr("Référence croisée (maitre)")); - eiw_ = new ElementInfoWidget(element_, this); - tab_ -> addTab(eiw_, tr("Informations")); - break; - case Element::Slave: - lsew_ = new LinkSingleElementWidget(element_, this); - tab_ -> addTab(lsew_, tr("Référence croisée (esclave)")); - break; - case Element::Terminale: - break; - default: - break; - } - - tab_ -> addTab(generalWidget(), tr("Général")); - - dbb = new QDialogButtonBox(QDialogButtonBox::Apply | QDialogButtonBox::Cancel | QDialogButtonBox::Reset, - Qt::Horizontal, this); + dbb = new QDialogButtonBox(QDialogButtonBox::Apply | QDialogButtonBox::Cancel | QDialogButtonBox::Reset, Qt::Horizontal, this); connect(dbb, SIGNAL(clicked(QAbstractButton*)), this, SLOT(standardButtonClicked(QAbstractButton*))); QVBoxLayout *main_layout = new QVBoxLayout(this); - main_layout -> addWidget(tab_); + main_layout -> addWidget(m_editor); main_layout -> addWidget(dbb); setLayout(main_layout); } @@ -161,56 +53,28 @@ void ElementPropertiesDialog::buildInterface() { */ void ElementPropertiesDialog::standardButtonClicked(QAbstractButton *button) { int answer = dbb -> buttonRole(button); - bool accept = false; switch (answer) { case QDialogButtonBox::ResetRole: - if (mpw_) mpw_->reset(); + m_editor->reset(); break; case QDialogButtonBox::ApplyRole: - accept = true; - break; - case QDialogButtonBox::RejectRole: - this -> reject(); + m_editor->apply(); + accept(); break; default: - this -> reject(); + reject(); break; } - - if (accept) { - QUndoCommand *a = nullptr; - QUndoCommand *b = nullptr; - - if (eiw_) a = eiw_ -> associatedUndo(); - if (mpw_) b = mpw_ -> associatedUndo(); - if (lsew_) lsew_ -> apply(); - - //If two undo, we push it in a macro - if (a && b) { - QUndoStack &stack = element_ -> diagram() -> undoStack(); - stack.beginMacro(a -> text() + " + " + b -> text()); - stack.push(a); - stack.push(b); - stack.endMacro(); - } - else { - if (a) element_ -> diagram() -> undoStack().push(a); - if (b) element_ -> diagram() -> undoStack().push(b); - } - - this -> accept(); - } } /** * @brief ElementPropertiesDialog::findInPanel * Slot */ -void ElementPropertiesDialog::findInPanel() { - if (CustomElement *custom_element = qobject_cast(element_)) { - emit findElementRequired(custom_element->location()); - } +void ElementPropertiesDialog::findInPanel(const ElementsLocation &location) +{ + emit findElementRequired(location); reject(); } @@ -218,10 +82,9 @@ void ElementPropertiesDialog::findInPanel() { * @brief ElementPropertiesDialog::editElement * Slot */ -void ElementPropertiesDialog::editElement() { - if (CustomElement *custom_element = qobject_cast(element_)) { - emit findElementRequired(custom_element->location()); - emit editElementRequired(custom_element->location()); - } +void ElementPropertiesDialog::editElement(const ElementsLocation &location) +{ + emit findElementRequired(location); + emit editElementRequired(location); reject(); } diff --git a/sources/ui/elementpropertiesdialog.h b/sources/ui/elementpropertiesdialog.h index 8e8848a2e..47da944e5 100644 --- a/sources/ui/elementpropertiesdialog.h +++ b/sources/ui/elementpropertiesdialog.h @@ -15,31 +15,28 @@ You should have received a copy of the GNU General Public License along with QElectroTech. If not, see . */ -#ifndef ELEMENTPROPERTIESWIDGET_H -#define ELEMENTPROPERTIESWIDGET_H +#ifndef ELEMENTPROPERTIESDIALOG_H +#define ELEMENTPROPERTIESDIALOG_H #include -class Diagram; class Element; class ElementsLocation; -class ElementInfoWidget; -class MasterPropertiesWidget; -class LinkSingleElementWidget; class QAbstractButton; class QDialogButtonBox; -class QTabWidget; +class ElementPropertiesWidget; -class ElementPropertiesDialog : public QDialog { +/** + * @brief The ElementPropertiesDialog class + * Display the element properties widget in a QDialog + */ +class ElementPropertiesDialog : public QDialog +{ Q_OBJECT public: explicit ElementPropertiesDialog(Element *elmt, QWidget *parent = 0); - private: - QWidget* generalWidget(); - void buildInterface(); - signals: /// Signal emitted when users wish to locate an element from the diagram within elements collection void findElementRequired(const ElementsLocation &); @@ -48,19 +45,13 @@ class ElementPropertiesDialog : public QDialog { public slots: void standardButtonClicked (QAbstractButton *); - void findInPanel (); - void editElement (); + void findInPanel (const ElementsLocation &); + void editElement (const ElementsLocation &); private: Element *element_; - Diagram *diagram_; - QTabWidget *tab_; - QPushButton *find_in_panel, - *edit_element; QDialogButtonBox *dbb; - ElementInfoWidget *eiw_; - MasterPropertiesWidget *mpw_; - LinkSingleElementWidget *lsew_; + ElementPropertiesWidget *m_editor; }; -#endif // ELEMENTPROPERTIESWIDGET_H +#endif // ELEMENTPROPERTIESDIALOG_H diff --git a/sources/ui/elementpropertieswidget.cpp b/sources/ui/elementpropertieswidget.cpp new file mode 100644 index 000000000..9526d5797 --- /dev/null +++ b/sources/ui/elementpropertieswidget.cpp @@ -0,0 +1,203 @@ +/* + Copyright 2006-2015 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 "elementpropertieswidget.h" +#include "element.h" +#include "elementinfowidget.h" +#include "masterpropertieswidget.h" +#include "linksingleelementwidget.h" +#include "ghostelement.h" +#include "diagram.h" +#include "diagramposition.h" +#include "qeticons.h" + +#include +#include +#include + +/** + * @brief ElementPropertiesWidget::ElementPropertiesWidget + * Default constructor + * @param elmt + * @param parent + */ +ElementPropertiesWidget::ElementPropertiesWidget(Element *elmt, QWidget *parent) : + PropertiesEditorWidget (parent), + m_element (elmt), + m_diagram (elmt->diagram()), + m_tab (nullptr) +{ + buildGui(); +} + +/** + * @brief ElementPropertiesWidget::apply + * Apply the new properties by pushing an undo command + * to the parent project's undo stack of element + */ +void ElementPropertiesWidget::apply() +{ + QList undo_list; + + foreach (PropertiesEditorWidget *pew, m_list_editor) + if (pew->associatedUndo()) + undo_list << pew->associatedUndo(); + + if (undo_list.isEmpty()) return; + + QString str; + foreach(QUndoCommand *uc, undo_list) str += uc->text() += " "; + + QUndoStack &stack = m_element -> diagram() -> undoStack(); + stack.beginMacro(str); + foreach(QUndoCommand *uc, undo_list) stack.push(uc); + stack.endMacro(); +} + +/** + * @brief ElementPropertiesWidget::reset + * Reset the edited properties + */ +void ElementPropertiesWidget::reset() { + foreach (PropertiesEditorWidget *pew, m_list_editor) pew->reset(); +} + +/** + * @brief ElementPropertiesWidget::findInPanel + * If m_element is a custom element, emit findElementRequired + */ +void ElementPropertiesWidget::findInPanel() +{ + if (CustomElement *custom_element = qobject_cast(m_element)) + emit findElementRequired(custom_element->location()); +} + +/** + * @brief ElementPropertiesWidget::editElement + * If m_element is a custom element, emit findElementRequired and editElementRequired + */ +void ElementPropertiesWidget::editElement() +{ + if (CustomElement *custom_element = qobject_cast(m_element)) + { + emit findElementRequired(custom_element->location()); + emit editElementRequired(custom_element->location()); + } +} + +/** + * @brief ElementPropertiesWidget::build + * Build the content of this widget + */ +void ElementPropertiesWidget::buildGui() +{ + m_tab = new QTabWidget(this); + + //Add tab according to the element + switch (m_element -> linkType()) + { + case Element::Simple: + m_list_editor << new ElementInfoWidget(m_element, this); + break; + case Element::NextReport: + m_list_editor << new LinkSingleElementWidget(m_element, this); + break; + case Element::PreviousReport: + m_list_editor << new LinkSingleElementWidget(m_element, this); + break; + case Element::Master: + m_list_editor << new MasterPropertiesWidget(m_element, this); + m_list_editor << new ElementInfoWidget(m_element, this); + break; + case Element::Slave: + m_list_editor << new LinkSingleElementWidget(m_element, this); + break; + case Element::Terminale: + break; + default: + break; + } + + foreach (PropertiesEditorWidget *pew, m_list_editor) m_tab->addTab(pew, pew->title()); + m_tab -> addTab(generalWidget(), tr("Général")); + + QVBoxLayout *main_layout = new QVBoxLayout(this); + main_layout -> addWidget(m_tab); + setLayout(main_layout); +} + +/** + * @brief ElementPropertiesWidget::generalWidget + * @return build and return the "general" widget + */ +QWidget *ElementPropertiesWidget::generalWidget() +{ + CustomElement *custom_element = qobject_cast(m_element); + GhostElement *ghost_element = qobject_cast(m_element); + + // type de l'element + QString description_string; + if (ghost_element) { + description_string += tr("Élément manquant"); + } else { + description_string += tr("Élément"); + } + description_string += "\n"; + + // some element characteristic + description_string += QString(tr("Nom : %1\n")).arg(m_element -> name()); + int folio_index = m_diagram -> folioIndex(); + if (folio_index != -1) { + description_string += QString(tr("Folio : %1\n")).arg(folio_index + 1); + } + description_string += QString(tr("Position : %1\n")).arg(m_diagram -> convertPosition(m_element -> scenePos()).toString()); + description_string += QString(tr("Dimensions : %1*%2\n")).arg(m_element -> size().width()).arg(m_element -> size().height()); + description_string += QString(tr("Bornes : %1\n")).arg(m_element -> terminals().count()); + description_string += QString(tr("Champs de texte : %1\n")).arg(m_element -> texts().count()); + + if (custom_element) { + description_string += QString(tr("Emplacement : %1\n")).arg(custom_element -> location().toString()); + } + + // widget himself + QWidget *general_widget = new QWidget (m_tab); + QVBoxLayout *vlayout_ = new QVBoxLayout (general_widget); + general_widget -> setLayout(vlayout_); + + //widget for the text + QLabel *label = new QLabel (description_string, general_widget); + label->setWordWrap(true); + vlayout_->addWidget(label); + + //widget for the pixmap + QLabel *pix = new QLabel(general_widget); + pix->setPixmap(m_element->pixmap()); + vlayout_->addWidget(pix, 0, Qt::AlignHCenter); + vlayout_ -> addStretch(); + + //button widget + QPushButton *find_in_panel = new QPushButton(QET::Icons::ZoomDraw, tr("Retrouver dans le panel"), general_widget); + connect(find_in_panel, SIGNAL(clicked()), this, SLOT(findInPanel())); + QPushButton *edit_element = new QPushButton(QET::Icons::ElementEdit, tr("Éditer l'élément"), general_widget); + connect(edit_element, SIGNAL(clicked()), this, SLOT(editElement())); + QHBoxLayout *hlayout_ = new QHBoxLayout; + hlayout_->addWidget(find_in_panel); + hlayout_->addWidget(edit_element); + + vlayout_->addLayout(hlayout_); + return general_widget; +} diff --git a/sources/ui/elementpropertieswidget.h b/sources/ui/elementpropertieswidget.h new file mode 100644 index 000000000..da50d78e2 --- /dev/null +++ b/sources/ui/elementpropertieswidget.h @@ -0,0 +1,59 @@ +/* + Copyright 2006-2015 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 ELEMENTPROPERTIESWIDGET_H +#define ELEMENTPROPERTIESWIDGET_H + +#include "propertieseditorwidget.h" + +class Element; +class Diagram; +class QTabWidget; +class ElementsLocation; + + +class ElementPropertiesWidget : public PropertiesEditorWidget +{ + Q_OBJECT + + public: + explicit ElementPropertiesWidget(Element *elmt, QWidget *parent = 0); + void apply(); + void reset(); + + public slots: + void findInPanel (); + void editElement (); + + private: + void buildGui(); + QWidget *generalWidget(); + + signals: + /// Signal emitted when users wish to locate an element from the diagram within elements collection + void findElementRequired(const ElementsLocation &); + /// Signal emitted when users wish to edit an element from the diagram + void editElementRequired(const ElementsLocation &); + + private: + Element *m_element; + Diagram *m_diagram; + QTabWidget *m_tab; + QList m_list_editor; +}; + +#endif // ELEMENTPROPERTIESWIDGET_H diff --git a/sources/ui/linksingleelementwidget.cpp b/sources/ui/linksingleelementwidget.cpp index e9e975738..f83917afd 100644 --- a/sources/ui/linksingleelementwidget.cpp +++ b/sources/ui/linksingleelementwidget.cpp @@ -31,7 +31,7 @@ * the parent widget */ LinkSingleElementWidget::LinkSingleElementWidget(Element *elmt, QWidget *parent) : - QWidget(parent), + PropertiesEditorWidget(parent), ui(new Ui::LinkSingleElementWidget), element_(elmt), esw_(0), @@ -67,10 +67,32 @@ LinkSingleElementWidget::~LinkSingleElementWidget() * Apply the new property of the edited element */ void LinkSingleElementWidget::apply() { + QUndoCommand *undo = associatedUndo(); + if (undo) + element_->diagram()->undoStack().push(undo); +} + +/** + * @brief LinkSingleElementWidget::associatedUndo + * @return the undo command associated to the current edition + * if there isn't change, return nulptr + */ +QUndoCommand *LinkSingleElementWidget::associatedUndo() const +{ if (esw_->selectedElement()) - element_->diagram()->undoStack().push(new LinkElementsCommand(element_, esw_->selectedElement())); + return new LinkElementsCommand(element_, esw_->selectedElement()); else if (unlink_) - element_->diagram()->undoStack().push(new unlinkElementsCommand(element_)); + return new unlinkElementsCommand(element_); + + return nullptr; +} + +QString LinkSingleElementWidget::title() const +{ + if (element_->linkType() == Element::AllReport) + return tr("Report de folio"); + else + return tr("Référence croisée (esclave)"); } /** diff --git a/sources/ui/linksingleelementwidget.h b/sources/ui/linksingleelementwidget.h index 813715c70..dd1f972e9 100644 --- a/sources/ui/linksingleelementwidget.h +++ b/sources/ui/linksingleelementwidget.h @@ -18,7 +18,7 @@ #ifndef LINKSINGLEELEMENTWIDGET_H #define LINKSINGLEELEMENTWIDGET_H -#include +#include "propertieseditorwidget.h" #include "element.h" class Diagram; @@ -39,15 +39,18 @@ namespace Ui { * If the element is already linked, the widget ask user to unlink. * This widget embedded the diagram command for undo/redo the action */ -class LinkSingleElementWidget : public QWidget +class LinkSingleElementWidget : public PropertiesEditorWidget { Q_OBJECT ///Methods public: - explicit LinkSingleElementWidget(Element *elmt, QWidget *parent = 0); - ~LinkSingleElementWidget(); - void apply(); + explicit LinkSingleElementWidget(Element *elmt, QWidget *parent = 0); + ~LinkSingleElementWidget(); + + void apply(); + QUndoCommand *associatedUndo() const; + QString title() const; private: void buildInterface(); diff --git a/sources/ui/masterpropertieswidget.h b/sources/ui/masterpropertieswidget.h index 4879cbda3..21c102c96 100644 --- a/sources/ui/masterpropertieswidget.h +++ b/sources/ui/masterpropertieswidget.h @@ -46,7 +46,8 @@ class MasterPropertiesWidget : public PropertiesEditorWidget void apply(); void reset(); - QUndoCommand* associatedUndo() const; + QUndoCommand *associatedUndo () const; + QString title() const {return tr("Référence croisée (maitre)");} private: void buildInterface(); diff --git a/sources/ui/propertieseditorwidget.cpp b/sources/ui/propertieseditorwidget.cpp index dc9550a65..9a0c8aebd 100644 --- a/sources/ui/propertieseditorwidget.cpp +++ b/sources/ui/propertieseditorwidget.cpp @@ -28,10 +28,19 @@ PropertiesEditorWidget::PropertiesEditorWidget(QWidget *parent) : {} /** - * @brief PropertiesEditorWidget::assosiatedUndo - * By default, return an empty undo command - * @return an empty undo command + * @brief PropertiesEditorWidget::associatedUndo + * By default, return a nullptr + * @return nullptr */ -QUndoCommand *PropertiesEditorWidget::assosiatedUndo() { - return new QUndoCommand(); +QUndoCommand *PropertiesEditorWidget::associatedUndo() const{ + return nullptr; +} + +/** + * @brief PropertiesEditorWidget::title + * @return the title of this editor + */ +QString PropertiesEditorWidget::title() const +{ + return QString(); } diff --git a/sources/ui/propertieseditorwidget.h b/sources/ui/propertieseditorwidget.h index 42d861fc4..055532e5d 100644 --- a/sources/ui/propertieseditorwidget.h +++ b/sources/ui/propertieseditorwidget.h @@ -33,9 +33,10 @@ class PropertiesEditorWidget : public QWidget public: explicit PropertiesEditorWidget(QWidget *parent = 0); - void apply() {} - void reset() {} - virtual QUndoCommand *assosiatedUndo (); + virtual void apply() {} + virtual void reset() {} + virtual QUndoCommand *associatedUndo () const; + virtual QString title() const; }; #endif // PROPERTIESEDITORWIDGET_H