From 3d051419a546c5892b5febcaee80352289c9d393 Mon Sep 17 00:00:00 2001 From: joshua Date: Thu, 9 Jan 2020 10:26:10 +0100 Subject: [PATCH] Improve file access on windows and mac OSX --- .../elementscollectionmodel.cpp | 4 +- .../ElementsCollection/elementslocation.cpp | 45 +++++++++++++++---- sources/ElementsCollection/elementslocation.h | 13 +++++- .../fileelementcollectionitem.cpp | 17 ++++--- .../fileelementcollectionitem.h | 2 + 5 files changed, 63 insertions(+), 18 deletions(-) diff --git a/sources/ElementsCollection/elementscollectionmodel.cpp b/sources/ElementsCollection/elementscollectionmodel.cpp index a2f537413..9b3301b00 100644 --- a/sources/ElementsCollection/elementscollectionmodel.cpp +++ b/sources/ElementsCollection/elementscollectionmodel.cpp @@ -243,6 +243,8 @@ void ElementsCollectionModel::loadCollections(bool common_collection, bool custo list.append(projectItems(project)); } + ElementsLocation::clearAcces(); + qDebug() << "acces count " << ElementsLocation::accesCount(); QTime t; t.start(); QFuture futur = QtConcurrent::map(list, setUpData); @@ -252,7 +254,7 @@ void ElementsCollectionModel::loadCollections(bool common_collection, bool custo } int ms = t.elapsed(); - QMessageBox::about(nullptr, tr("Chargement collection d'élément"), tr("Le chargement de la collection d'éléments à été éffectué en %1 ms").arg(ms)); + QMessageBox::about(nullptr, tr("Chargement collection d'élément"), tr("Le chargement de la collection d'éléments à été éffectué en %1 ms %2 acces").arg(ms).arg(ElementsLocation::accesCount())); } /** diff --git a/sources/ElementsCollection/elementslocation.cpp b/sources/ElementsCollection/elementslocation.cpp index 0b27c4e2b..d698a530e 100644 --- a/sources/ElementsCollection/elementslocation.cpp +++ b/sources/ElementsCollection/elementslocation.cpp @@ -1,4 +1,4 @@ -/* +/* Copyright 2006-2019 The QElectroTech Team This file is part of QElectroTech. @@ -25,6 +25,7 @@ #include "qetxml.h" #include +static int acces; // make this class usable with QVariant int ElementsLocation::MetaTypeId = qRegisterMetaType("ElementsLocation"); @@ -538,6 +539,7 @@ QDomElement ElementsLocation::xml() const { if (!m_project) { + ++acces; QFile file (m_file_system_path); QDomDocument docu; if (docu.setContent(&file)) @@ -568,11 +570,28 @@ QDomElement ElementsLocation::xml() const */ pugi::xml_document ElementsLocation::pugiXml() const { - if (!m_project) + //Except for linux OS (because linux keep in cache the file), we keep in memory the xml + //to avoid multiple access to file. + //keep in memory the XML, consumes a little more RAM, for this reason we don't use it for linux to minimize the RAM footprint. +#ifndef Q_OS_LINUX + if (!m_string_stream.str().empty()) { + pugi::xml_document docu; + docu.load_string(m_string_stream.str().c_str()); + return docu; + } +#endif + if (!m_project) + { + ++acces; pugi::xml_document docu; if (docu.load_file(m_file_system_path.toStdString().c_str())) + { +#ifndef Q_OS_LINUX + docu.save(m_string_stream); +#endif return docu; + } } else { @@ -778,14 +797,24 @@ DiagramContext ElementsLocation::elementInformations() const return context; } -/** - @param location A standard element location - @return a hash identifying this location -*/ -uint qHash(const ElementsLocation &location) { - return(qHash(location.toString())); +void ElementsLocation::clearAcces() +{ + acces =0; } +int ElementsLocation::accesCount() +{ + return acces; +} + +///** +// @param location A standard element location +// @return a hash identifying this location +//*/ +//uint qHash(const ElementsLocation &location) { +// return(qHash(location.toString())); +//} + QDebug operator<< (QDebug debug, const ElementsLocation &location) { QDebugStateSaver saver(debug); diff --git a/sources/ElementsCollection/elementslocation.h b/sources/ElementsCollection/elementslocation.h index e20da25da..5283f2699 100644 --- a/sources/ElementsCollection/elementslocation.h +++ b/sources/ElementsCollection/elementslocation.h @@ -1,4 +1,4 @@ -/* +/* Copyright 2006-2019 The QElectroTech Team This file is part of QElectroTech. @@ -24,6 +24,10 @@ #include #include +#ifndef Q_OS_LINUX +#include "sstream" +#endif + class QETProject; class XmlElementCollection; @@ -86,13 +90,18 @@ class ElementsLocation QString m_collection_path; QString m_file_system_path; QETProject *m_project = nullptr; +#ifndef Q_OS_LINUX + mutable std::stringstream m_string_stream; +#endif public: static int MetaTypeId; ///< Id of the corresponding Qt meta type + static void clearAcces(); + static int accesCount(); }; QDebug operator<<(QDebug debug, const ElementsLocation &location); Q_DECLARE_METATYPE(ElementsLocation) -uint qHash(const ElementsLocation &); +//uint qHash(const ElementsLocation &); #endif diff --git a/sources/ElementsCollection/fileelementcollectionitem.cpp b/sources/ElementsCollection/fileelementcollectionitem.cpp index a28bc8f4a..db6d14b53 100644 --- a/sources/ElementsCollection/fileelementcollectionitem.cpp +++ b/sources/ElementsCollection/fileelementcollectionitem.cpp @@ -40,8 +40,10 @@ FileElementCollectionItem::FileElementCollectionItem() bool FileElementCollectionItem::setRootPath(const QString& path, bool set_data, bool hide_element) { QDir dir(path); - if (dir.exists()) { + if (dir.exists()) + { m_path = path; + m_location.setPath(collectionPath()); populate(set_data, hide_element); return true; } @@ -159,8 +161,8 @@ QString FileElementCollectionItem::localName() } } else if (isElement()) { - ElementsLocation loc(collectionPath()); - setText(loc.name()); +// ElementsLocation loc(collectionPath()); + setText(m_location.name()); } return text(); @@ -264,8 +266,8 @@ void FileElementCollectionItem::setUpData() //Set the local name and all informations of the element //in the data Qt::UserRole+1, these data will be use for search. - ElementsLocation location(collectionPath()); - DiagramContext context = location.elementInformations(); +// ElementsLocation location(collectionPath()); + DiagramContext context = m_location.elementInformations(); QStringList search_list; for (QString key : context.keys()) { search_list.append(context.value(key).toString()); @@ -297,8 +299,8 @@ void FileElementCollectionItem::setUpIcon() if (isDir()) setIcon(QET::Icons::Folder); else { - ElementsLocation loc(collectionPath()); - setIcon(loc.icon()); +// ElementsLocation loc(collectionPath()); + setIcon(m_location.icon()); } } } @@ -313,6 +315,7 @@ void FileElementCollectionItem::setUpIcon() void FileElementCollectionItem::setPathName(const QString& path_name, bool set_data, bool hide_element) { m_path = path_name; + m_location.setPath(collectionPath()); //This isn't an element, we create the childs if (!path_name.endsWith(".elmt")) diff --git a/sources/ElementsCollection/fileelementcollectionitem.h b/sources/ElementsCollection/fileelementcollectionitem.h index bf7f172b4..313f1cbfd 100644 --- a/sources/ElementsCollection/fileelementcollectionitem.h +++ b/sources/ElementsCollection/fileelementcollectionitem.h @@ -19,6 +19,7 @@ #define FILEELEMENTCOLLECTIONITEM2_H #include "elementcollectionitem.h" +#include "elementslocation.h" /** * @brief The FileElementCollectionItem class @@ -59,6 +60,7 @@ class FileElementCollectionItem : public ElementCollectionItem private: QString m_path; + ElementsLocation m_location; }; #endif // FILEELEMENTCOLLECTIONITEM2_H