Minor improvement and remove some use of ElementsCollectionItem and ElementsDefinition

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4407 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun 2016-03-29 17:23:58 +00:00
parent dd5d9c9504
commit 95e309ff18
8 changed files with 44 additions and 128 deletions

View File

@ -57,10 +57,7 @@ ElementsLocation::~ElementsLocation() {
ElementsLocation::ElementsLocation(const ElementsLocation &other) : ElementsLocation::ElementsLocation(const ElementsLocation &other) :
m_collection_path(other.m_collection_path), m_collection_path(other.m_collection_path),
m_file_system_path(other.m_file_system_path), m_file_system_path(other.m_file_system_path),
m_project(other.m_project), m_project(other.m_project)
m_xml(other.m_xml),
m_uuid(other.m_uuid),
m_icon(other.m_icon)
{} {}
/** /**
@ -84,9 +81,6 @@ ElementsLocation &ElementsLocation::operator=(const ElementsLocation &other) {
m_collection_path = other.m_collection_path; m_collection_path = other.m_collection_path;
m_file_system_path = other.m_file_system_path; m_file_system_path = other.m_file_system_path;
m_project = other.m_project; m_project = other.m_project;
m_xml = other.m_xml;
m_uuid = other.m_uuid;
m_icon = other.m_icon;
return(*this); return(*this);
} }
@ -491,17 +485,14 @@ NamesList ElementsLocation::nameList()
* @return The definition of this element. * @return The definition of this element.
* The definition can be null. * The definition can be null.
*/ */
QDomElement ElementsLocation::xml() QDomElement ElementsLocation::xml() const
{ {
if (!m_xml.isNull())
return m_xml;
if (!m_project) if (!m_project)
{ {
QFile file (m_file_system_path); QFile file (m_file_system_path);
QDomDocument docu; QDomDocument docu;
if (docu.setContent(&file)) if (docu.setContent(&file))
m_xml = docu.documentElement().cloneNode().toElement(); return docu.documentElement().cloneNode().toElement();
} }
else else
{ {
@ -509,33 +500,32 @@ QDomElement ElementsLocation::xml()
if (isElement()) if (isElement())
{ {
QDomElement element = m_project->embeddedElementCollection()->element(str.remove("embed://")); QDomElement element = m_project->embeddedElementCollection()->element(str.remove("embed://"));
m_xml = element.firstChildElement("definition"); return element.firstChildElement("definition");
} }
else else
{ {
QDomElement element = m_project->embeddedElementCollection()->directory(str.remove("embed://")); QDomElement element = m_project->embeddedElementCollection()->directory(str.remove("embed://"));
m_xml = element; return element;
} }
} }
return m_xml; return QDomElement();
} }
/** /**
* @brief ElementsLocation::uuid * @brief ElementsLocation::uuid
* @return The uuid of the pointed element * @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 //Get the uuid of element
QList<QDomElement> list_ = QET::findInDomElement(xml(), "uuid"); QList<QDomElement> list_ = QET::findInDomElement(xml(), "uuid");
if (!list_.isEmpty()) 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 @@ QUuid ElementsLocation::uuid()
* @return The icon of the represented element. * @return The icon of the represented element.
* If icon can't be set, return a null QIcon * 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) if (!m_project)
{ {
ElementsCollectionCache *cache = QETApp::collectionCache(); ElementsCollectionCache *cache = QETApp::collectionCache();
if (cache->fetchElement(*this)) ElementsLocation loc(*this); //Make a copy of this to keep this method const
m_icon = QIcon(cache->pixmap()); if (cache->fetchElement(loc))
return QIcon(cache->pixmap());
} }
else else
{ {
@ -560,22 +549,23 @@ QIcon ElementsLocation::icon()
Element *elmt = factory->createElement(*this, 0, &state); Element *elmt = factory->createElement(*this, 0, &state);
if (state == 0) if (state == 0)
m_icon = QIcon(elmt->pixmap()); return QIcon(elmt->pixmap());
} }
return m_icon; return QIcon();
} }
/** /**
* @brief ElementLocation::name * @brief ElementLocation::name
* @return The name of the represented element in the current local * @return The name of the represented element in the current local
*/ */
QString ElementsLocation::name() QString ElementsLocation::name() const
{ {
if (!m_project) if (!m_project)
{ {
ElementsCollectionCache *cache = QETApp::collectionCache(); 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(); return cache->name();
else else
return QString(); return QString();

View File

@ -67,19 +67,16 @@ class ElementsLocation
XmlElementCollection *projectCollection() const; XmlElementCollection *projectCollection() const;
NamesList nameList(); NamesList nameList();
QDomElement xml(); QDomElement xml() const;
QUuid uuid(); QUuid uuid() const;
QIcon icon(); QIcon icon() const;
QString name(); QString name() const;
QString fileName() const; QString fileName() const;
private: private:
QString m_collection_path; QString m_collection_path;
QString m_file_system_path; QString m_file_system_path;
QETProject *m_project = nullptr; QETProject *m_project = nullptr;
QDomElement m_xml;
QUuid m_uuid;
QIcon m_icon;
public: public:
static int MetaTypeId; ///< Id of the corresponding Qt meta type static int MetaTypeId; ///< Id of the corresponding Qt meta type

View File

@ -285,31 +285,16 @@ QDomElement XmlElementCollection::element(const QString &path)
QDomElement XmlElementCollection::directory(const QString &path) QDomElement XmlElementCollection::directory(const QString &path)
{ {
QStringList path_list = path.split("/"); 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++) for (int i=0 ; i<path_list.size() ; i++)
{ {
QDomNodeList node_list = dom_element.elementsByTagName("category"); QDomElement child_dom = child(parent_dom, path_list.at(i));
if (node_list.isEmpty()) return QDomElement(); if (child_dom.isNull()) return QDomElement();
else parent_dom = child_dom;
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();
}
}
} }
return dom_element; return parent_dom;
} }
/** /**

View File

@ -16,8 +16,6 @@
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>. along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "diagrameventaddelement.h" #include "diagrameventaddelement.h"
#include "elementscollectionitem.h"
#include "qetapp.h"
#include "integrationmoveelementshandler.h" #include "integrationmoveelementshandler.h"
#include "elementfactory.h" #include "elementfactory.h"
#include "diagram.h" #include "diagram.h"
@ -39,8 +37,7 @@ DiagramEventAddElement::DiagramEventAddElement(ElementsLocation &location, Diagr
m_element(nullptr) m_element(nullptr)
{ {
//Check if there is an element at this location //Check if there is an element at this location
ElementsCollectionItem *item = QETApp::collectionItem(location); if (location.isElement() && location.exist())
if (item)
{ {
//location is an element, we build it, if build fail, //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 //m_running stay to false (by default), so this interface will be deleted at next event

View File

@ -16,9 +16,6 @@
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>. along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "elementfactory.h" #include "elementfactory.h"
#include "elementdefinition.h"
#include "elementscollectionitem.h"
#include "qetapp.h"
#include "QDomElement" #include "QDomElement"
#include "simpleelement.h" #include "simpleelement.h"
#include "reportelement.h" #include "reportelement.h"
@ -26,7 +23,7 @@
#include "slaveelement.h" #include "slaveelement.h"
#include "terminalelement.h" #include "terminalelement.h"
ElementFactory* ElementFactory::factory_ = 0; ElementFactory* ElementFactory::factory_ = nullptr;
/** /**
* @brief ElementFactory::createElement * @brief ElementFactory::createElement
* @param location create element at this location * @param location create element at this location
@ -36,20 +33,16 @@ ElementFactory* ElementFactory::factory_ = 0;
*/ */
Element * ElementFactory::createElement(const ElementsLocation &location, QGraphicsItem *qgi, int *state) Element * ElementFactory::createElement(const ElementsLocation &location, QGraphicsItem *qgi, int *state)
{ {
// recupere la definition de l'element if (Q_UNLIKELY( !(location.isElement() && location.exist()) ))
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; if (state)
return 0; *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 == "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 == "master") return (new MasterElement (location, qgi, state));
if (link_type == "slave") return (new SlaveElement (location, qgi, state)); if (link_type == "slave") return (new SlaveElement (location, qgi, state));

View File

@ -23,7 +23,6 @@
class Element; class Element;
class ElementsLocation; class ElementsLocation;
class QGraphicsItem; class QGraphicsItem;
class Diagram;
/** /**
* @brief The ElementFactory class * @brief The ElementFactory class

View File

@ -46,32 +46,13 @@
*/ */
CustomElement::CustomElement(const ElementsLocation &location, QGraphicsItem *qgi, int *state) : CustomElement::CustomElement(const ElementsLocation &location, QGraphicsItem *qgi, int *state) :
FixedElement(qgi), FixedElement(qgi),
elmt_state(-1),
location_(location), location_(location),
forbid_antialiasing(false) forbid_antialiasing(false)
{ {
// recupere la definition de l'element
ElementsCollectionItem *element_item = QETApp::collectionItem(location); if(Q_UNLIKELY( !(location.isElement() && location.exist()) ))
ElementDefinition *element_definition; {
if (
!element_item ||\
!element_item -> isElement() ||\
!(element_definition = qobject_cast<ElementDefinition *>(element_item))
) {
if (state) *state = 1; 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; return;
} }
@ -82,12 +63,12 @@ CustomElement::CustomElement(const ElementsLocation &location, QGraphicsItem *qg
list_polygons_.clear(); list_polygons_.clear();
list_arcs_.clear(); list_arcs_.clear();
buildFromXml(element_definition -> xml(), &elmt_state); int elmt_state;
buildFromXml(location.xml(), &elmt_state);
if (state) *state = elmt_state; if (state) *state = elmt_state;
if (elmt_state) return; if (elmt_state) return;
if (state) *state = 0; if (state) *state = 0;
elmt_state = 0;
} }
/** /**
@ -106,7 +87,8 @@ CustomElement::CustomElement(const ElementsLocation &location, QGraphicsItem *qg
*/ */
bool CustomElement::buildFromXml(const QDomElement &xml_def_elmt, int *state) { 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; if (state) *state = 4;
return(false); return(false);
} }

View File

@ -45,7 +45,6 @@ class CustomElement : public FixedElement
// attributes // attributes
protected: protected:
int elmt_state; // hold the error code in case the instanciation fails, or 0 if everything went well
NamesList names; NamesList names;
ElementsLocation location_; ElementsLocation location_;
QPicture drawing; QPicture drawing;
@ -75,8 +74,6 @@ class CustomElement : public FixedElement
virtual void paint(QPainter *, const QStyleOptionGraphicsItem *); virtual void paint(QPainter *, const QStyleOptionGraphicsItem *);
QString typeId() const; QString typeId() const;
ElementsLocation location() const; ElementsLocation location() const;
bool isNull() const;
int state() const;
QString name() const; QString name() const;
ElementTextItem* taggedText(const QString &tagg) const; ElementTextItem* taggedText(const QString &tagg) const;
@ -114,30 +111,6 @@ inline ElementsLocation CustomElement::location() const {
return(location_); return(location_);
} }
/**
@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. @return The name of this element.
*/ */