mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-18 05:00:33 +01:00
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 git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@2665 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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 @@ bool Diagram::fromXml(QDomElement &document, QPointF position, bool consider_inf
|
||||
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;
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@
|
||||
#include <ui/elementpropertieswidget.h>
|
||||
#include <QGraphicsPixmapItem>
|
||||
#include <QGraphicsSceneMouseEvent>
|
||||
#include "factory/elementfactory.h"
|
||||
|
||||
|
||||
/**
|
||||
@@ -856,7 +857,7 @@ bool DiagramView::mustIntegrateTitleBlockTemplate(const TitleBlockTemplateLocati
|
||||
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);
|
||||
|
||||
@@ -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 @@ QPixmap ElementsCollectionCache::pixmap() const {
|
||||
*/
|
||||
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 {
|
||||
|
||||
@@ -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 @@ void ElementsPanel::startElementDrag(const ElementsLocation &location) {
|
||||
|
||||
// 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;
|
||||
|
||||
52
sources/factory/elementfactory.cpp
Normal file
52
sources/factory/elementfactory.cpp
Normal file
@@ -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));
|
||||
}
|
||||
70
sources/factory/elementfactory.h
Normal file
70
sources/factory/elementfactory.h
Normal file
@@ -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
|
||||
@@ -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 @@ QETApp::~QETApp() {
|
||||
delete common_collection;
|
||||
if (custom_tbt_collection_) delete custom_tbt_collection_;
|
||||
if (common_tbt_collection_) delete common_tbt_collection_;
|
||||
ElementFactory::dropInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -82,12 +82,6 @@ CustomElement::CustomElement(const ElementsLocation &location, QGraphicsItem *qg
|
||||
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.
|
||||
|
||||
@@ -35,7 +35,6 @@ class CustomElement : public FixedElement {
|
||||
// constructors, destructor
|
||||
public:
|
||||
CustomElement(const ElementsLocation &, QGraphicsItem * = 0, Diagram * = 0, int * = 0);
|
||||
CustomElement(const QDomElement &, QGraphicsItem * = 0, Diagram * = 0, int * = 0);
|
||||
virtual ~CustomElement();
|
||||
|
||||
private:
|
||||
|
||||
@@ -29,6 +29,15 @@ class Element : public QetGraphicsItem {
|
||||
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum linkerType{
|
||||
simple = 1,
|
||||
report = 2,
|
||||
master = 4,
|
||||
slave = 8,
|
||||
bornier = 16
|
||||
};
|
||||
|
||||
// constructors, destructor
|
||||
public:
|
||||
Element(QGraphicsItem * = 0, Diagram * = 0);
|
||||
@@ -78,6 +87,8 @@ class Element : public QetGraphicsItem {
|
||||
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);
|
||||
|
||||
26
sources/qetgraphicsitem/reportelement.cpp
Normal file
26
sources/qetgraphicsitem/reportelement.cpp
Normal file
@@ -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;
|
||||
}
|
||||
42
sources/qetgraphicsitem/reportelement.h
Normal file
42
sources/qetgraphicsitem/reportelement.h
Normal file
@@ -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
|
||||
26
sources/qetgraphicsitem/simpleelement.cpp
Normal file
26
sources/qetgraphicsitem/simpleelement.cpp
Normal file
@@ -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;
|
||||
}
|
||||
42
sources/qetgraphicsitem/simpleelement.h
Normal file
42
sources/qetgraphicsitem/simpleelement.h
Normal file
@@ -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
|
||||
@@ -14,19 +14,7 @@ elementpropertieswidget::elementpropertieswidget(Element *elmt, QWidget *parent)
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,6 +77,45 @@ QWidget* elementpropertieswidget::generalWidget() {
|
||||
return general_widget;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
|
||||
@@ -13,6 +13,7 @@ class elementpropertieswidget : public QDialog
|
||||
|
||||
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 @@ class elementpropertieswidget : public QDialog
|
||||
void editElement ();
|
||||
|
||||
private:
|
||||
QWidget *w; ///this widget is only for test
|
||||
QDialogButtonBox *dbb;
|
||||
Element *element_;
|
||||
Diagram *diagram_;
|
||||
|
||||
Reference in New Issue
Block a user