The application now avoids reading and keeping in memory every element file in the collection.

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/branches/0.3@1366 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
xavier
2011-10-08 21:54:53 +00:00
parent 883da2a9c0
commit d431139d57
7 changed files with 59 additions and 16 deletions

View File

@@ -25,7 +25,8 @@
@param parent Item parent @param parent Item parent
*/ */
ElementsCollection::ElementsCollection(ElementsCollectionItem *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(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;
}

View File

@@ -21,6 +21,7 @@
#include "elementscollectionitem.h" #include "elementscollectionitem.h"
class QETProject; class QETProject;
class ElementsCategory; class ElementsCategory;
class ElementsCollectionCache;
class ElementDefinition; class ElementDefinition;
class MoveElementsHandler; class MoveElementsHandler;
/** /**
@@ -74,7 +75,6 @@ class ElementsCollection : public ElementsCollectionItem {
virtual ElementDefinition *createElement(const QString &); virtual ElementDefinition *createElement(const QString &);
virtual bool isEmpty(); virtual bool isEmpty();
virtual int count(); virtual int count();
virtual bool isCacheable() const = 0;
// Methodes propres a la classe ElementsCollection // Methodes propres a la classe ElementsCollection
public: public:
@@ -83,6 +83,9 @@ class ElementsCollection : public ElementsCollectionItem {
*/ */
virtual ElementsCategory *rootCategory() = 0; virtual ElementsCategory *rootCategory() = 0;
virtual ElementsCollectionItem *item(const QString &, bool = true); virtual ElementsCollectionItem *item(const QString &, bool = true);
virtual bool isCacheable() const = 0;
virtual ElementsCollectionCache *cache() const;
virtual void setCache(ElementsCollectionCache *);
// attributs // attributs
protected: protected:
@@ -90,5 +93,7 @@ class ElementsCollection : public ElementsCollectionItem {
QString protocol_; QString protocol_;
/// Projet auquel appartient cette collection /// Projet auquel appartient cette collection
QETProject *project_; QETProject *project_;
/// Optional cache used to improve performance
ElementsCollectionCache *cache_;
}; };
#endif #endif

View File

@@ -46,8 +46,6 @@ class ElementsCollectionCache : public QObject {
bool fetchElement(ElementDefinition *); bool fetchElement(ElementDefinition *);
QString name() const; QString name() const;
QPixmap pixmap() const; QPixmap pixmap() const;
private:
bool fetchData(const ElementsLocation &); bool fetchData(const ElementsLocation &);
bool fetchNameFromCache(const QString &, const QDateTime &); bool fetchNameFromCache(const QString &, const QDateTime &);
bool fetchPixmapFromCache(const QString &, const QDateTime &); bool fetchPixmapFromCache(const QString &, const QDateTime &);

View File

@@ -15,6 +15,7 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>. along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "elementscollectioncache.h"
#include "fileelementdefinition.h" #include "fileelementdefinition.h"
#include "fileelementscategory.h" #include "fileelementscategory.h"
#include "fileelementscollection.h" #include "fileelementscollection.h"
@@ -43,7 +44,17 @@ FileElementDefinition::~FileElementDefinition() {
@return la definition XML de l'element @return la definition XML de l'element
*/ */
QDomElement FileElementDefinition::xml() { 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(); file_path = file_info.canonicalFilePath();
// ouvre le fichier if (parentCollection()) {
QFile file(file_path); ElementsCollectionCache *cache = parentCollection() -> cache();
if (cache && cache -> fetchNameFromCache(location().toString(), file_info.lastModified())) {
// charge le contenu du fichier en s'attendant a du XML // the element file has not been modified since the last time
bool read_xml = xml_element_.setContent(&file); // we put its name in cache: we do not need to load it.
if (!read_xml) { is_null = false;
is_null = true; return;
return; }
} }
// l'ouverture de la definition a reussi // we need to ensure this is a valid XML document
is_null = false; QFile file(file_path);
QDomDocument xml_document;
is_null = !xml_document.setContent(&file);
xml_document.clear();
} }
/** /**

View File

@@ -30,7 +30,6 @@ FileElementsCollection::FileElementsCollection(const QString &path, ElementsColl
protocol_ = "unknown"; protocol_ = "unknown";
project_ = 0; project_ = 0;
root = 0; root = 0;
reload();
} }
/** /**

View File

@@ -21,6 +21,7 @@
#include "qetdiagrameditor.h" #include "qetdiagrameditor.h"
#include "qetelementeditor.h" #include "qetelementeditor.h"
#include "elementscollectionitem.h" #include "elementscollectionitem.h"
#include "elementscollectioncache.h"
#include "fileelementscollection.h" #include "fileelementscollection.h"
#include "titleblocktemplate.h" #include "titleblocktemplate.h"
#include "templateeditor.h" #include "templateeditor.h"
@@ -42,6 +43,7 @@ QString QETApp::config_dir = QString();
QString QETApp::lang_dir = QString(); QString QETApp::lang_dir = QString();
FileElementsCollection *QETApp::common_collection = 0; FileElementsCollection *QETApp::common_collection = 0;
FileElementsCollection *QETApp::custom_collection = 0; FileElementsCollection *QETApp::custom_collection = 0;
ElementsCollectionCache *QETApp::collections_cache_ = 0;
QMap<uint, QETProject *> QETApp::registered_projects_ = QMap<uint, QETProject *>(); QMap<uint, QETProject *> QETApp::registered_projects_ = QMap<uint, QETProject *>();
uint QETApp::next_project_id = 0; uint QETApp::next_project_id = 0;
RecentFiles *QETApp::projects_recent_files_ = 0; RecentFiles *QETApp::projects_recent_files_ = 0;
@@ -92,6 +94,13 @@ QETApp::QETApp(int &argc, char **argv) :
setQuitOnLastWindowClosed(false); setQuitOnLastWindowClosed(false);
connect(this, SIGNAL(lastWindowClosed()), this, SLOT(checkRemainingWindows())); 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) // 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")); setSplashScreenStep(tr("Chargement... Lecture des collections d'\351l\351ments", "splash screen caption"));
foreach(ElementsCollection *collection, availableCollections()) { foreach(ElementsCollection *collection, availableCollections()) {
@@ -228,6 +237,7 @@ ElementsCollection *QETApp::commonElementsCollection() {
if (!common_collection) { if (!common_collection) {
common_collection = new FileElementsCollection(QETApp::commonElementsDir()); common_collection = new FileElementsCollection(QETApp::commonElementsDir());
common_collection -> setProtocol("common"); common_collection -> setProtocol("common");
common_collection -> setCache(collections_cache_);
} }
return(common_collection); return(common_collection);
} }
@@ -239,6 +249,7 @@ ElementsCollection *QETApp::customElementsCollection() {
if (!custom_collection) { if (!custom_collection) {
custom_collection = new FileElementsCollection(QETApp::customElementsDir()); custom_collection = new FileElementsCollection(QETApp::customElementsDir());
custom_collection -> setProtocol("custom"); custom_collection -> setProtocol("custom");
custom_collection -> setCache(collections_cache_);
} }
return(custom_collection); return(custom_collection);
} }

View File

@@ -26,6 +26,7 @@ class AboutQET;
class QETDiagramEditor; class QETDiagramEditor;
class QETElementEditor; class QETElementEditor;
class ElementsCollection; class ElementsCollection;
class ElementsCollectionCache;
class ElementsCollectionItem; class ElementsCollectionItem;
class FileElementsCollection; class FileElementsCollection;
class ElementsCategory; class ElementsCategory;
@@ -137,6 +138,7 @@ class QETApp : public QETSingleApplication {
static FileElementsCollection *common_collection; static FileElementsCollection *common_collection;
static FileElementsCollection *custom_collection; static FileElementsCollection *custom_collection;
static ElementsCollectionCache *collections_cache_;
static QMap<uint, QETProject *> registered_projects_; static QMap<uint, QETProject *> registered_projects_;
static uint next_project_id; static uint next_project_id;
static RecentFiles *projects_recent_files_; static RecentFiles *projects_recent_files_;