[qet] [4407] Minor improvement and remove some use of ElementsCollectionItem and ElementsDefinition |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/qet Archives
]
Revision: 4407
Author: blacksun
Date: 2016-03-29 19:23:58 +0200 (Tue, 29 Mar 2016)
Log Message:
-----------
Minor improvement and remove some use of ElementsCollectionItem and ElementsDefinition
Modified Paths:
--------------
trunk/sources/ElementsCollection/elementslocation.cpp
trunk/sources/ElementsCollection/elementslocation.h
trunk/sources/ElementsCollection/xmlelementcollection.cpp
trunk/sources/diagramevent/diagrameventaddelement.cpp
trunk/sources/factory/elementfactory.cpp
trunk/sources/factory/elementfactory.h
trunk/sources/qetgraphicsitem/customelement.cpp
trunk/sources/qetgraphicsitem/customelement.h
Modified: trunk/sources/ElementsCollection/elementslocation.cpp
===================================================================
--- trunk/sources/ElementsCollection/elementslocation.cpp 2016-03-28 16:25:45 UTC (rev 4406)
+++ trunk/sources/ElementsCollection/elementslocation.cpp 2016-03-29 17:23:58 UTC (rev 4407)
@@ -57,10 +57,7 @@
ElementsLocation::ElementsLocation(const ElementsLocation &other) :
m_collection_path(other.m_collection_path),
m_file_system_path(other.m_file_system_path),
- m_project(other.m_project),
- m_xml(other.m_xml),
- m_uuid(other.m_uuid),
- m_icon(other.m_icon)
+ m_project(other.m_project)
{}
/**
@@ -84,9 +81,6 @@
m_collection_path = other.m_collection_path;
m_file_system_path = other.m_file_system_path;
m_project = other.m_project;
- m_xml = other.m_xml;
- m_uuid = other.m_uuid;
- m_icon = other.m_icon;
return(*this);
}
@@ -491,17 +485,14 @@
* @return The definition of this element.
* The definition can be null.
*/
-QDomElement ElementsLocation::xml()
+QDomElement ElementsLocation::xml() const
{
- if (!m_xml.isNull())
- return m_xml;
-
if (!m_project)
{
QFile file (m_file_system_path);
QDomDocument docu;
if (docu.setContent(&file))
- m_xml = docu.documentElement().cloneNode().toElement();
+ return docu.documentElement().cloneNode().toElement();
}
else
{
@@ -509,33 +500,32 @@
if (isElement())
{
QDomElement element = m_project->embeddedElementCollection()->element(str.remove("embed://"));
- m_xml = element.firstChildElement("definition");
+ return element.firstChildElement("definition");
}
else
{
QDomElement element = m_project->embeddedElementCollection()->directory(str.remove("embed://"));
- m_xml = element;
+ return element;
}
}
- return m_xml;
+ return QDomElement();
}
/**
* @brief ElementsLocation::uuid
* @return The uuid of the pointed element
+ * Uuid can be null
*/
-QUuid ElementsLocation::uuid()
+QUuid ElementsLocation::uuid() const
{
- if (!m_uuid.isNull()) return m_uuid;
-
//Get the uuid of element
QList<QDomElement> list_ = QET::findInDomElement(xml(), "uuid");
if (!list_.isEmpty())
- m_uuid = QUuid(list_.first().attribute("uuid"));
+ return QUuid(list_.first().attribute("uuid"));
- return m_uuid;
+ return QUuid();
}
/**
@@ -543,15 +533,14 @@
* @return The icon of the represented element.
* If icon can't be set, return a null QIcon
*/
-QIcon ElementsLocation::icon()
+QIcon ElementsLocation::icon() const
{
- if (!m_icon.isNull()) return m_icon;
-
if (!m_project)
{
ElementsCollectionCache *cache = QETApp::collectionCache();
- if (cache->fetchElement(*this))
- m_icon = QIcon(cache->pixmap());
+ ElementsLocation loc(*this); //Make a copy of this to keep this method const
+ if (cache->fetchElement(loc))
+ return QIcon(cache->pixmap());
}
else
{
@@ -560,22 +549,23 @@
Element *elmt = factory->createElement(*this, 0, &state);
if (state == 0)
- m_icon = QIcon(elmt->pixmap());
+ return QIcon(elmt->pixmap());
}
- return m_icon;
+ return QIcon();
}
/**
* @brief ElementLocation::name
* @return The name of the represented element in the current local
*/
-QString ElementsLocation::name()
+QString ElementsLocation::name() const
{
if (!m_project)
{
ElementsCollectionCache *cache = QETApp::collectionCache();
- if (cache->fetchElement(*this))
+ ElementsLocation loc(*this); //Make a copy of this to keep this method const
+ if (cache->fetchElement(loc))
return cache->name();
else
return QString();
Modified: trunk/sources/ElementsCollection/elementslocation.h
===================================================================
--- trunk/sources/ElementsCollection/elementslocation.h 2016-03-28 16:25:45 UTC (rev 4406)
+++ trunk/sources/ElementsCollection/elementslocation.h 2016-03-29 17:23:58 UTC (rev 4407)
@@ -67,19 +67,16 @@
XmlElementCollection *projectCollection() const;
NamesList nameList();
- QDomElement xml();
- QUuid uuid();
- QIcon icon();
- QString name();
+ QDomElement xml() const;
+ QUuid uuid() const;
+ QIcon icon() const;
+ QString name() const;
QString fileName() const;
private:
QString m_collection_path;
QString m_file_system_path;
QETProject *m_project = nullptr;
- QDomElement m_xml;
- QUuid m_uuid;
- QIcon m_icon;
public:
static int MetaTypeId; ///< Id of the corresponding Qt meta type
Modified: trunk/sources/ElementsCollection/xmlelementcollection.cpp
===================================================================
--- trunk/sources/ElementsCollection/xmlelementcollection.cpp 2016-03-28 16:25:45 UTC (rev 4406)
+++ trunk/sources/ElementsCollection/xmlelementcollection.cpp 2016-03-29 17:23:58 UTC (rev 4407)
@@ -285,31 +285,16 @@
QDomElement XmlElementCollection::directory(const QString &path)
{
QStringList path_list = path.split("/");
- QDomElement dom_element = m_dom_document.documentElement();
+ QDomElement parent_dom = m_dom_document.documentElement();
for (int i=0 ; i<path_list.size() ; i++)
{
- QDomNodeList node_list = dom_element.elementsByTagName("category");
- if (node_list.isEmpty()) return QDomElement();
-
- for (int j=0 ; j <node_list.size() ; j++)
- {
- QDomNode node = node_list.at(j);
- if (node.isElement())
- {
- QDomElement qde = node.toElement();
- if (qde.attribute("name") == path_list.at(i))
- {
- dom_element = node.toElement();
- j = node_list.size(); //Use to go out of the for loop
- }
- else if (j == node_list.size()-1)
- return QDomElement();
- }
- }
+ QDomElement child_dom = child(parent_dom, path_list.at(i));
+ if (child_dom.isNull()) return QDomElement();
+ else parent_dom = child_dom;
}
- return dom_element;
+ return parent_dom;
}
/**
Modified: trunk/sources/diagramevent/diagrameventaddelement.cpp
===================================================================
--- trunk/sources/diagramevent/diagrameventaddelement.cpp 2016-03-28 16:25:45 UTC (rev 4406)
+++ trunk/sources/diagramevent/diagrameventaddelement.cpp 2016-03-29 17:23:58 UTC (rev 4407)
@@ -16,8 +16,6 @@
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "diagrameventaddelement.h"
-#include "elementscollectionitem.h"
-#include "qetapp.h"
#include "integrationmoveelementshandler.h"
#include "elementfactory.h"
#include "diagram.h"
@@ -39,8 +37,7 @@
m_element(nullptr)
{
//Check if there is an element at this location
- ElementsCollectionItem *item = QETApp::collectionItem(location);
- if (item)
+ if (location.isElement() && location.exist())
{
//location is an element, we build it, if build fail,
//m_running stay to false (by default), so this interface will be deleted at next event
Modified: trunk/sources/factory/elementfactory.cpp
===================================================================
--- trunk/sources/factory/elementfactory.cpp 2016-03-28 16:25:45 UTC (rev 4406)
+++ trunk/sources/factory/elementfactory.cpp 2016-03-29 17:23:58 UTC (rev 4407)
@@ -16,9 +16,6 @@
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 "simpleelement.h"
#include "reportelement.h"
@@ -26,7 +23,7 @@
#include "slaveelement.h"
#include "terminalelement.h"
-ElementFactory* ElementFactory::factory_ = 0;
+ElementFactory* ElementFactory::factory_ = nullptr;
/**
* @brief ElementFactory::createElement
* @param location create element at this location
@@ -36,20 +33,16 @@
*/
Element * ElementFactory::createElement(const ElementsLocation &location, QGraphicsItem *qgi, 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 (Q_UNLIKELY( !(location.isElement() && location.exist()) ))
{
- if (state) *state = 1;
- return 0;
+ if (state)
+ *state = 1;
+ return nullptr;
}
- if (element_definition->xml().hasAttribute("link_type"))
+ if (location.xml().hasAttribute("link_type"))
{
- QString link_type = element_definition->xml().attribute("link_type");
+ QString link_type = location.xml().attribute("link_type");
if (link_type == "next_report" || link_type == "previous_report") return (new ReportElement(location, link_type, qgi, state));
if (link_type == "master") return (new MasterElement (location, qgi, state));
if (link_type == "slave") return (new SlaveElement (location, qgi, state));
Modified: trunk/sources/factory/elementfactory.h
===================================================================
--- trunk/sources/factory/elementfactory.h 2016-03-28 16:25:45 UTC (rev 4406)
+++ trunk/sources/factory/elementfactory.h 2016-03-29 17:23:58 UTC (rev 4407)
@@ -23,7 +23,6 @@
class Element;
class ElementsLocation;
class QGraphicsItem;
-class Diagram;
/**
* @brief The ElementFactory class
Modified: trunk/sources/qetgraphicsitem/customelement.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/customelement.cpp 2016-03-28 16:25:45 UTC (rev 4406)
+++ trunk/sources/qetgraphicsitem/customelement.cpp 2016-03-29 17:23:58 UTC (rev 4407)
@@ -46,48 +46,29 @@
*/
CustomElement::CustomElement(const ElementsLocation &location, QGraphicsItem *qgi, int *state) :
FixedElement(qgi),
- elmt_state(-1),
location_(location),
forbid_antialiasing(false)
{
- // 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(Q_UNLIKELY( !(location.isElement() && location.exist()) ))
+ {
if (state) *state = 1;
- elmt_state = 1;
return;
}
-
- if (!element_definition -> isReadable()) {
- if (state) *state = 2;
- elmt_state = 2;
- return;
- }
-
- if (element_definition -> isNull()) {
- if (state) *state = 3;
- elmt_state = 3;
- return;
- }
- //Start from empty lists.
+ //Start from empty lists.
list_lines_.clear();
list_rectangles_.clear();
list_circles_.clear();
list_polygons_.clear();
list_arcs_.clear();
- buildFromXml(element_definition -> xml(), &elmt_state);
+ int elmt_state;
+ buildFromXml(location.xml(), &elmt_state);
if (state) *state = elmt_state;
if (elmt_state) return;
if (state) *state = 0;
- elmt_state = 0;
}
/**
@@ -106,7 +87,8 @@
*/
bool CustomElement::buildFromXml(const QDomElement &xml_def_elmt, int *state) {
- if (xml_def_elmt.tagName() != "definition" || xml_def_elmt.attribute("type") != "element") {
+ if (xml_def_elmt.tagName() != "definition" || xml_def_elmt.attribute("type") != "element")
+ {
if (state) *state = 4;
return(false);
}
Modified: trunk/sources/qetgraphicsitem/customelement.h
===================================================================
--- trunk/sources/qetgraphicsitem/customelement.h 2016-03-28 16:25:45 UTC (rev 4406)
+++ trunk/sources/qetgraphicsitem/customelement.h 2016-03-29 17:23:58 UTC (rev 4407)
@@ -45,7 +45,6 @@
// attributes
protected:
- int elmt_state; // hold the error code in case the instanciation fails, or 0 if everything went well
NamesList names;
ElementsLocation location_;
QPicture drawing;
@@ -75,8 +74,6 @@
virtual void paint(QPainter *, const QStyleOptionGraphicsItem *);
QString typeId() const;
ElementsLocation location() const;
- bool isNull() const;
- int state() const;
QString name() const;
ElementTextItem* taggedText(const QString &tagg) const;
@@ -115,30 +112,6 @@
}
/**
- @return true if this element is null, i.e. if its XML description could not
- be loaded.
-*/
-inline bool CustomElement::isNull() const {
- return(elmt_state);
-}
-
-/**
- @return An integer representing the state of this element:
- - 0: instantiation succeeded
- - 1: the file does not exist
- - 2: the file could not be opened
- - 3: The file is not a valid XML document
- - 4: The XML document does not have a "definition" root element.
- - 5: The definition attributes are missing or invalid
- - 6: The definition is empty
- - 7: The parsing of an XML element describing an element drawing primitive failed
- - 8: No primitive could be loadedAucune partie du dessin n'a pu etre chargee
-*/
-inline int CustomElement::state() const {
- return(elmt_state);
-}
-
-/**
@return The name of this element.
*/
inline QString CustomElement::name() const {