diff --git a/sources/elementscollection.cpp b/sources/elementscollection.cpp index f6860bbda..90abd4d64 100644 --- a/sources/elementscollection.cpp +++ b/sources/elementscollection.cpp @@ -25,7 +25,8 @@ @param parent Item parent */ ElementsCollection::ElementsCollection(ElementsCollectionItem *parent) : - ElementsCollectionItem(parent) + ElementsCollectionItem(parent), + cache_(0) { } @@ -414,3 +415,16 @@ ElementsCollectionItem *ElementsCollection::item(const QString &item_path, bool return(result); } +/** + @return The cache used by this collection, or 0 if this collection does not have any +*/ +ElementsCollectionCache *ElementsCollection::cache() const { + return(cache_); +} + +/** + @param cache The cache to be used by this collection +*/ +void ElementsCollection::setCache(ElementsCollectionCache *cache) { + cache_ = cache; +} diff --git a/sources/elementscollection.h b/sources/elementscollection.h index 08ba89bee..72578b69c 100644 --- a/sources/elementscollection.h +++ b/sources/elementscollection.h @@ -21,6 +21,7 @@ #include "elementscollectionitem.h" class QETProject; class ElementsCategory; +class ElementsCollectionCache; class ElementDefinition; class MoveElementsHandler; /** @@ -74,7 +75,6 @@ class ElementsCollection : public ElementsCollectionItem { virtual ElementDefinition *createElement(const QString &); virtual bool isEmpty(); virtual int count(); - virtual bool isCacheable() const = 0; // Methodes propres a la classe ElementsCollection public: @@ -83,6 +83,9 @@ class ElementsCollection : public ElementsCollectionItem { */ virtual ElementsCategory *rootCategory() = 0; virtual ElementsCollectionItem *item(const QString &, bool = true); + virtual bool isCacheable() const = 0; + virtual ElementsCollectionCache *cache() const; + virtual void setCache(ElementsCollectionCache *); // attributs protected: @@ -90,5 +93,7 @@ class ElementsCollection : public ElementsCollectionItem { QString protocol_; /// Projet auquel appartient cette collection QETProject *project_; + /// Optional cache used to improve performance + ElementsCollectionCache *cache_; }; #endif diff --git a/sources/elementscollectioncache.h b/sources/elementscollectioncache.h index f75378420..662f1d9a9 100644 --- a/sources/elementscollectioncache.h +++ b/sources/elementscollectioncache.h @@ -46,8 +46,6 @@ class ElementsCollectionCache : public QObject { bool fetchElement(ElementDefinition *); QString name() const; QPixmap pixmap() const; - - private: bool fetchData(const ElementsLocation &); bool fetchNameFromCache(const QString &, const QDateTime &); bool fetchPixmapFromCache(const QString &, const QDateTime &); diff --git a/sources/fileelementdefinition.cpp b/sources/fileelementdefinition.cpp index 2a5512625..ac794712e 100644 --- a/sources/fileelementdefinition.cpp +++ b/sources/fileelementdefinition.cpp @@ -15,6 +15,7 @@ You should have received a copy of the GNU General Public License along with QElectroTech. If not, see . */ +#include "elementscollectioncache.h" #include "fileelementdefinition.h" #include "fileelementscategory.h" #include "fileelementscollection.h" @@ -43,7 +44,17 @@ FileElementDefinition::~FileElementDefinition() { @return la definition XML de l'element */ QDomElement FileElementDefinition::xml() { - return(xml_element_.documentElement()); + // ouvre le fichier + QFile file(file_path); + + // charge le contenu du fichier en s'attendant a du XML + is_null = !xml_element_.setContent(&file); + if (is_null) { + return(QDomElement()); + } else { + // l'ouverture de la definition a reussi + return(xml_element_.documentElement()); + } } /** @@ -122,18 +133,21 @@ void FileElementDefinition::reload() { } file_path = file_info.canonicalFilePath(); - // ouvre le fichier - QFile file(file_path); - - // charge le contenu du fichier en s'attendant a du XML - bool read_xml = xml_element_.setContent(&file); - if (!read_xml) { - is_null = true; - return; + if (parentCollection()) { + ElementsCollectionCache *cache = parentCollection() -> cache(); + if (cache && cache -> fetchNameFromCache(location().toString(), file_info.lastModified())) { + // the element file has not been modified since the last time + // we put its name in cache: we do not need to load it. + is_null = false; + return; + } } - // l'ouverture de la definition a reussi - is_null = false; + // we need to ensure this is a valid XML document + QFile file(file_path); + QDomDocument xml_document; + is_null = !xml_document.setContent(&file); + xml_document.clear(); } /** diff --git a/sources/fileelementscollection.cpp b/sources/fileelementscollection.cpp index 1243117e3..de5b5a8b7 100644 --- a/sources/fileelementscollection.cpp +++ b/sources/fileelementscollection.cpp @@ -30,7 +30,6 @@ FileElementsCollection::FileElementsCollection(const QString &path, ElementsColl protocol_ = "unknown"; project_ = 0; root = 0; - reload(); } /** diff --git a/sources/qetapp.cpp b/sources/qetapp.cpp index cc7777ad8..e69284f70 100644 --- a/sources/qetapp.cpp +++ b/sources/qetapp.cpp @@ -21,6 +21,7 @@ #include "qetdiagrameditor.h" #include "qetelementeditor.h" #include "elementscollectionitem.h" +#include "elementscollectioncache.h" #include "fileelementscollection.h" #include "titleblocktemplate.h" #include "templateeditor.h" @@ -42,6 +43,7 @@ QString QETApp::config_dir = QString(); QString QETApp::lang_dir = QString(); FileElementsCollection *QETApp::common_collection = 0; FileElementsCollection *QETApp::custom_collection = 0; +ElementsCollectionCache *QETApp::collections_cache_ = 0; QMap QETApp::registered_projects_ = QMap(); uint QETApp::next_project_id = 0; RecentFiles *QETApp::projects_recent_files_ = 0; @@ -92,6 +94,13 @@ QETApp::QETApp(int &argc, char **argv) : setQuitOnLastWindowClosed(false); connect(this, SIGNAL(lastWindowClosed()), this, SLOT(checkRemainingWindows())); + setSplashScreenStep(tr("Chargement... Initialisation du cache des collections d'\351l\351ments", "splash screen caption")); + if (!collections_cache_) { + QString cache_path = QETApp::configDir() + "/elements_cache.sqlite"; + collections_cache_ = new ElementsCollectionCache(cache_path, this); + collections_cache_ -> setLocale(QLocale::system().name().left(2)); // @todo we need a unique function to get the good language + } + // loads known collections into memory (this does not include items rendering made in elements panels) setSplashScreenStep(tr("Chargement... Lecture des collections d'\351l\351ments", "splash screen caption")); foreach(ElementsCollection *collection, availableCollections()) { @@ -228,6 +237,7 @@ ElementsCollection *QETApp::commonElementsCollection() { if (!common_collection) { common_collection = new FileElementsCollection(QETApp::commonElementsDir()); common_collection -> setProtocol("common"); + common_collection -> setCache(collections_cache_); } return(common_collection); } @@ -239,6 +249,7 @@ ElementsCollection *QETApp::customElementsCollection() { if (!custom_collection) { custom_collection = new FileElementsCollection(QETApp::customElementsDir()); custom_collection -> setProtocol("custom"); + custom_collection -> setCache(collections_cache_); } return(custom_collection); } diff --git a/sources/qetapp.h b/sources/qetapp.h index 1df86f4c4..f95102801 100644 --- a/sources/qetapp.h +++ b/sources/qetapp.h @@ -26,6 +26,7 @@ class AboutQET; class QETDiagramEditor; class QETElementEditor; class ElementsCollection; +class ElementsCollectionCache; class ElementsCollectionItem; class FileElementsCollection; class ElementsCategory; @@ -137,6 +138,7 @@ class QETApp : public QETSingleApplication { static FileElementsCollection *common_collection; static FileElementsCollection *custom_collection; + static ElementsCollectionCache *collections_cache_; static QMap registered_projects_; static uint next_project_id; static RecentFiles *projects_recent_files_;