[qet] [2665] add pattern factory/singletton to create different type of element |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/qet Archives
]
Revision: 2665
Author: blacksun
Date: 2013-12-20 21:30:55 +0100 (Fri, 20 Dec 2013)
Log Message:
-----------
add pattern factory/singletton to create different type of element
add two news class for element type : simple and folio report
add an empty tab for the element report in the element properties widget
Modified Paths:
--------------
trunk/elements/nomenclatures/bases/renvoi/next_folio.elmt
trunk/elements/nomenclatures/bases/renvoi/previous_folio.elmt
trunk/sources/diagram.cpp
trunk/sources/diagramview.cpp
trunk/sources/elementscollectioncache.cpp
trunk/sources/elementspanel.cpp
trunk/sources/qetapp.cpp
trunk/sources/qetgraphicsitem/customelement.cpp
trunk/sources/qetgraphicsitem/customelement.h
trunk/sources/qetgraphicsitem/element.h
trunk/sources/ui/elementpropertieswidget.cpp
trunk/sources/ui/elementpropertieswidget.h
Added Paths:
-----------
trunk/sources/factory/
trunk/sources/factory/elementfactory.cpp
trunk/sources/factory/elementfactory.h
trunk/sources/qetgraphicsitem/reportelement.cpp
trunk/sources/qetgraphicsitem/reportelement.h
trunk/sources/qetgraphicsitem/simpleelement.cpp
trunk/sources/qetgraphicsitem/simpleelement.h
Modified: trunk/elements/nomenclatures/bases/renvoi/next_folio.elmt
===================================================================
--- trunk/elements/nomenclatures/bases/renvoi/next_folio.elmt 2013-12-20 14:53:49 UTC (rev 2664)
+++ trunk/elements/nomenclatures/bases/renvoi/next_folio.elmt 2013-12-20 20:30:55 UTC (rev 2665)
@@ -1,4 +1,4 @@
-<definition width="40" version="0.3" hotspot_x="15" hotspot_y="20" height="40" type="element" orientation="dyyy">
+<definition width="40" version="0.3" hotspot_x="15" hotspot_y="20" height="40" type="element" orientation="dyyy" link_type="folio_report">
<names>
<name lang="ar">الصفحة التالية</name>
<name lang="de">Nächste Seite</name>
Modified: trunk/elements/nomenclatures/bases/renvoi/previous_folio.elmt
===================================================================
--- trunk/elements/nomenclatures/bases/renvoi/previous_folio.elmt 2013-12-20 14:53:49 UTC (rev 2664)
+++ trunk/elements/nomenclatures/bases/renvoi/previous_folio.elmt 2013-12-20 20:30:55 UTC (rev 2665)
@@ -1,4 +1,4 @@
-<definition width="50" version="0.3" hotspot_x="35" hotspot_y="20" height="40" type="element" orientation="dyyy">
+<definition width="50" version="0.3" hotspot_x="35" hotspot_y="20" height="40" type="element" orientation="dyyy" link_type="folio_report">
<names>
<name lang="ar">الصفحة السابقة</name>
<name lang="de">Vorherige Seite</name>
Modified: trunk/sources/diagram.cpp
===================================================================
--- trunk/sources/diagram.cpp 2013-12-20 14:53:49 UTC (rev 2664)
+++ trunk/sources/diagram.cpp 2013-12-20 20:30:55 UTC (rev 2665)
@@ -19,6 +19,7 @@
#include "qetgraphicsitem/conductor.h"
#include "qetgraphicsitem/conductortextitem.h"
#include "qetgraphicsitem/customelement.h"
+#include "factory/elementfactory.h"
#include "diagram.h"
#include "diagramcommands.h"
#include "diagramcontent.h"
@@ -523,9 +524,10 @@
ElementsLocation element_location = ElementsLocation(type_id);
if (type_id.startsWith("embed://")) element_location.setProject(project_);
- CustomElement *nvel_elmt = new CustomElement(element_location);
- if (nvel_elmt -> isNull()) {
- QString debug_message = QString("Diagram::fromXml() : Le chargement de la description de l'element %1 a echoue avec le code d'erreur %2").arg(element_location.path()).arg(nvel_elmt -> state());
+ int state = 0;
+ Element *nvel_elmt = ElementFactory::Instance()->createElement(element_location, 0, 0, &state);
+ if (state) {
+ QString debug_message = QString("Diagram::fromXml() : Le chargement de la description de l'element %1 a echoue avec le code d'erreur %2").arg(element_location.path()).arg(state);
qDebug() << qPrintable(debug_message);
delete nvel_elmt;
Modified: trunk/sources/diagramview.cpp
===================================================================
--- trunk/sources/diagramview.cpp 2013-12-20 14:53:49 UTC (rev 2664)
+++ trunk/sources/diagramview.cpp 2013-12-20 20:30:55 UTC (rev 2665)
@@ -42,6 +42,7 @@
#include <ui/elementpropertieswidget.h>
#include <QGraphicsPixmapItem>
#include <QGraphicsSceneMouseEvent>
+#include "factory/elementfactory.h"
/**
@@ -856,7 +857,7 @@
bool DiagramView::addElementAtPos(const ElementsLocation &location, const QPoint &pos) {
// construit une instance de l'element correspondant a l'emplacement
int state;
- Element *el = new CustomElement(location, 0, 0, &state);
+ Element *el = ElementFactory::Instance()->createElement(location, 0, 0, &state);
if (state) {
delete el;
return(false);
Modified: trunk/sources/elementscollectioncache.cpp
===================================================================
--- trunk/sources/elementscollectioncache.cpp 2013-12-20 14:53:49 UTC (rev 2664)
+++ trunk/sources/elementscollectioncache.cpp 2013-12-20 20:30:55 UTC (rev 2665)
@@ -2,7 +2,7 @@
#include "elementscollection.h"
#include "elementscategory.h"
#include "elementdefinition.h"
-#include "qetgraphicsitem/customelement.h"
+#include "factory/elementfactory.h"
/**
Construct a cache for elements collections.
@@ -171,7 +171,7 @@
*/
bool ElementsCollectionCache::fetchData(const ElementsLocation &location) {
int state;
- CustomElement *custom_elmt = new CustomElement(location, 0, 0, &state);
+ Element *custom_elmt = ElementFactory::Instance()->createElement(location, 0, 0, &state);
if (state) {
qDebug() << "ElementsCollectionCache::fetchData() : Le chargement du composant" << qPrintable(location.toString()) << "a echoue avec le code d'erreur" << state;
} else {
Modified: trunk/sources/elementspanel.cpp
===================================================================
--- trunk/sources/elementspanel.cpp 2013-12-20 14:53:49 UTC (rev 2664)
+++ trunk/sources/elementspanel.cpp 2013-12-20 20:30:55 UTC (rev 2665)
@@ -21,7 +21,7 @@
#include "diagram.h"
#include "elementscategory.h"
#include "elementscollectioncache.h"
-#include "qetgraphicsitem/customelement.h"
+#include "factory/elementfactory.h"
#include "fileelementscollection.h"
#include "fileelementdefinition.h"
#include "qeticons.h"
@@ -309,7 +309,7 @@
// element temporaire pour fournir un apercu
int elmt_creation_state;
- Element *temp_elmt = new CustomElement(location, 0, 0, &elmt_creation_state);
+ Element *temp_elmt = ElementFactory::Instance()->createElement(location, 0, 0, &elmt_creation_state);
if (elmt_creation_state) {
delete temp_elmt;
return;
Added: trunk/sources/factory/elementfactory.cpp
===================================================================
--- trunk/sources/factory/elementfactory.cpp (rev 0)
+++ trunk/sources/factory/elementfactory.cpp 2013-12-20 20:30:55 UTC (rev 2665)
@@ -0,0 +1,52 @@
+/*
+ Copyright 2006-2013 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 "elementfactory.h"
+#include "elementdefinition.h"
+#include "elementscollectionitem.h"
+#include "qetapp.h"
+#include "QDomElement"
+#include "qetgraphicsitem/simpleelement.h"
+#include "qetgraphicsitem/reportelement.h"
+
+ElementFactory* ElementFactory::factory_ = 0;
+/**
+ * @brief ElementFactory::createElement
+ * @param location create element at this location
+ * @param qgi parent item for this elemnt
+ * @param s diagram of the element
+ * @param state state of the creation
+ * @return the element or 0
+ */
+Element * ElementFactory::createElement(const ElementsLocation &location, QGraphicsItem *qgi, Diagram *s, int *state) {
+ // recupere la definition de l'element
+ ElementsCollectionItem *element_item = QETApp::collectionItem(location);
+ ElementDefinition *element_definition;
+ if (!element_item ||\
+ !element_item -> isElement() ||\
+ !(element_definition = qobject_cast<ElementDefinition *>(element_item))) {
+ if (state) *state = 1;
+ return 0;
+ }
+
+ if (element_definition->xml().hasAttribute("link_type")) {
+ QString link_type = element_definition->xml().attribute("link_type");
+ if (link_type == "folio_report") return (new ReportElement(location, qgi, s, state));
+ }
+ //default if nothing match for link_type
+ return (new SimpleElement(location, qgi, s, state));
+}
Added: trunk/sources/factory/elementfactory.h
===================================================================
--- trunk/sources/factory/elementfactory.h (rev 0)
+++ trunk/sources/factory/elementfactory.h 2013-12-20 20:30:55 UTC (rev 2665)
@@ -0,0 +1,70 @@
+/*
+ Copyright 2006-2013 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 ELEMENTFACTORY_H
+#define ELEMENTFACTORY_H
+
+#include <QMutex>
+#include "qetgraphicsitem/element.h"
+
+/**
+ * @brief The ElementFactory class
+ *this class is a pattern factory and also a singleton factory.
+ *this class create new instance of herited class element like
+ *simple element or report element.
+ *
+ */
+class ElementFactory
+{
+ //methods for singleton pattern
+ public:
+ // return instance of factory
+ static ElementFactory* Instance() {
+ static QMutex mutex;
+ if (!factory_) {
+ mutex.lock();
+ if (!factory_) factory_ = new ElementFactory();
+ mutex.unlock();
+ }
+ return factory_;
+ }
+ // delete the instance of factory
+ static void dropInstance () {
+ static QMutex mutex;
+ if (factory_) {
+ mutex.lock();
+ delete factory_;
+ factory_ = 0;
+ mutex.unlock();
+ }
+ }
+ //attributes
+ private:
+ static ElementFactory* factory_;
+
+ //methods for the class factory himself
+ private:
+ ElementFactory() {}
+ ElementFactory (const ElementFactory &);
+ ElementFactory operator= (const ElementFactory &);
+ ~ElementFactory() {}
+
+ public:
+ Element * createElement (const ElementsLocation &, QGraphicsItem * = 0, Diagram * = 0, int * = 0);
+};
+//ElementFactory ElementFactory::factory_ = 0;
+#endif // ELEMENTFACTORY_H
Modified: trunk/sources/qetapp.cpp
===================================================================
--- trunk/sources/qetapp.cpp 2013-12-20 14:53:49 UTC (rev 2664)
+++ trunk/sources/qetapp.cpp 2013-12-20 20:30:55 UTC (rev 2665)
@@ -36,6 +36,7 @@
#define QUOTE(x) STRINGIFY(x)
#define STRINGIFY(x) #x
#include <QProcessEnvironment>
+#include "factory/elementfactory.h"
#ifdef QET_ALLOW_OVERRIDE_CED_OPTION
QString QETApp::common_elements_dir = QString();
@@ -141,6 +142,7 @@
delete common_collection;
if (custom_tbt_collection_) delete custom_tbt_collection_;
if (common_tbt_collection_) delete common_tbt_collection_;
+ ElementFactory::dropInstance();
}
/**
Modified: trunk/sources/qetgraphicsitem/customelement.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/customelement.cpp 2013-12-20 14:53:49 UTC (rev 2664)
+++ trunk/sources/qetgraphicsitem/customelement.cpp 2013-12-20 20:30:55 UTC (rev 2665)
@@ -82,12 +82,6 @@
elmt_state = 0;
}
-CustomElement::CustomElement(const QDomElement &xml_def_elmt, QGraphicsItem *qgi, Diagram *s, int *state) : FixedElement(qgi, s) {
- int elmt_state = -1;
- buildFromXml(xml_def_elmt, &elmt_state);
- if (state) *state = elmt_state;
-}
-
/**
Construit l'element personnalise a partir d'un element XML representant sa
definition.
Modified: trunk/sources/qetgraphicsitem/customelement.h
===================================================================
--- trunk/sources/qetgraphicsitem/customelement.h 2013-12-20 14:53:49 UTC (rev 2664)
+++ trunk/sources/qetgraphicsitem/customelement.h 2013-12-20 20:30:55 UTC (rev 2665)
@@ -35,7 +35,6 @@
// constructors, destructor
public:
CustomElement(const ElementsLocation &, QGraphicsItem * = 0, Diagram * = 0, int * = 0);
- CustomElement(const QDomElement &, QGraphicsItem * = 0, Diagram * = 0, int * = 0);
virtual ~CustomElement();
private:
Modified: trunk/sources/qetgraphicsitem/element.h
===================================================================
--- trunk/sources/qetgraphicsitem/element.h 2013-12-20 14:53:49 UTC (rev 2664)
+++ trunk/sources/qetgraphicsitem/element.h 2013-12-20 20:30:55 UTC (rev 2665)
@@ -28,6 +28,15 @@
class Element : public QetGraphicsItem {
Q_OBJECT
+
+ public:
+ enum linkerType{
+ simple = 1,
+ report = 2,
+ master = 4,
+ slave = 8,
+ bornier = 16
+ };
// constructors, destructor
public:
@@ -78,6 +87,8 @@
virtual QString typeId() const = 0;
/// @return the human name for this element
virtual QString name() const = 0;
+ /// @return the linkable type
+ virtual int linkType() const = 0;
virtual bool isHighlighted() const;
virtual void setHighlighted(bool);
Added: trunk/sources/qetgraphicsitem/reportelement.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/reportelement.cpp (rev 0)
+++ trunk/sources/qetgraphicsitem/reportelement.cpp 2013-12-20 20:30:55 UTC (rev 2665)
@@ -0,0 +1,26 @@
+/*
+ Copyright 2006-2013 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 "reportelement.h"
+
+ReportElement::ReportElement(const ElementsLocation &location, QGraphicsItem *qgi, Diagram *s, int *state) :
+ CustomElement(location, qgi, s, state)
+{}
+
+int ReportElement::linkType() const {
+ return ReportElement::report;
+}
Added: trunk/sources/qetgraphicsitem/reportelement.h
===================================================================
--- trunk/sources/qetgraphicsitem/reportelement.h (rev 0)
+++ trunk/sources/qetgraphicsitem/reportelement.h 2013-12-20 20:30:55 UTC (rev 2665)
@@ -0,0 +1,42 @@
+/*
+ Copyright 2006-2013 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 REPORTELEMENT_H
+#define REPORTELEMENT_H
+
+#include "customelement.h"
+
+/**
+ * @brief The ReportElement class
+ *this class represent an element that can be linked to an other ReportElement
+ * a folio report in a diagram is a element that show a wire go on an other folio
+ */
+class ReportElement : public CustomElement {
+
+ Q_OBJECT
+
+ public :
+ explicit ReportElement(const ElementsLocation &, QGraphicsItem * = 0, Diagram * = 0, int * = 0);
+
+ virtual int linkType() const;
+
+ signals:
+
+ public slots:
+};
+
+#endif // REPORTELEMENT_H
Added: trunk/sources/qetgraphicsitem/simpleelement.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/simpleelement.cpp (rev 0)
+++ trunk/sources/qetgraphicsitem/simpleelement.cpp 2013-12-20 20:30:55 UTC (rev 2665)
@@ -0,0 +1,26 @@
+/*
+ Copyright 2006-2013 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 "simpleelement.h"
+
+SimpleElement::SimpleElement(const ElementsLocation &location, QGraphicsItem *qgi, Diagram *s, int *state) :
+ CustomElement(location, qgi, s, state)
+{}
+
+int SimpleElement::linkType() const {
+ return SimpleElement::simple;
+}
Added: trunk/sources/qetgraphicsitem/simpleelement.h
===================================================================
--- trunk/sources/qetgraphicsitem/simpleelement.h (rev 0)
+++ trunk/sources/qetgraphicsitem/simpleelement.h 2013-12-20 20:30:55 UTC (rev 2665)
@@ -0,0 +1,42 @@
+/*
+ Copyright 2006-2013 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 SIMPLEELEMENT_H
+#define SIMPLEELEMENT_H
+
+#include "customelement.h"
+
+/**
+ * @brief The SimpleElement class
+ *this class represente a simple element with no specific attribute
+ */
+class SimpleElement : public CustomElement {
+
+ Q_OBJECT
+
+ public :
+ explicit SimpleElement(const ElementsLocation &, QGraphicsItem * = 0, Diagram * = 0, int * = 0);
+
+ virtual int linkType() const;
+
+ signals:
+
+ public slots:
+
+};
+
+#endif // SIMPLEELEMENT_H
Modified: trunk/sources/ui/elementpropertieswidget.cpp
===================================================================
--- trunk/sources/ui/elementpropertieswidget.cpp 2013-12-20 14:53:49 UTC (rev 2664)
+++ trunk/sources/ui/elementpropertieswidget.cpp 2013-12-20 20:30:55 UTC (rev 2665)
@@ -14,19 +14,7 @@
element_ (elmt),
diagram_ (elmt->diagram())
{
- setWindowTitle(tr("Propri\351t\351s de l'\351l\351ment"));
-
- tab_ = new QTabWidget(this);
- tab_ -> addTab(generalWidget(), tr("G\351n\351ral"));
-
- 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(dbb);
- setLayout(main_layout);
+ buildInterface();
}
/**
@@ -90,6 +78,45 @@
}
/**
+ * @brief elementpropertieswidget::buildInterface
+ *build the interface of this dialog, the main tab can have
+ *different tab according to the edited element
+ */
+void elementpropertieswidget::buildInterface() {
+
+ setWindowTitle(tr("Propri\351t\351s de l'\351l\351ment"));
+ tab_ = new QTabWidget(this);
+ tab_ -> addTab(generalWidget(), tr("G\351n\351ral"));
+
+ //Add tab according to the element
+ switch (element_ -> linkType()) {
+ case Element::simple:
+ break;
+ case Element::report:
+ w = new QComboBox(this);
+ tab_ -> addTab(w, tr("Report de folio"));
+ break;
+ case Element::master:
+ break;
+ case Element::slave:
+ break;
+ case Element::bornier:
+ break;
+ default:
+ break;
+ }
+
+ 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(dbb);
+ setLayout(main_layout);
+}
+
+/**
* @brief elementpropertieswidget::standardButtonClicked
* apply action when click in the dialog standard button box
* @param button
Modified: trunk/sources/ui/elementpropertieswidget.h
===================================================================
--- trunk/sources/ui/elementpropertieswidget.h 2013-12-20 14:53:49 UTC (rev 2664)
+++ trunk/sources/ui/elementpropertieswidget.h 2013-12-20 20:30:55 UTC (rev 2665)
@@ -13,6 +13,7 @@
private:
QWidget* generalWidget();
+ void buildInterface();
signals:
/// Signal emitted when users wish to locate an element from the diagram within elements collection
@@ -26,6 +27,7 @@
void editElement ();
private:
+ QWidget *w; ///this widget is only for test
QDialogButtonBox *dbb;
Element *element_;
Diagram *diagram_;