From b34ec0d18989ad187ffa1cffe0245bd933b06b25 Mon Sep 17 00:00:00 2001 From: asv Date: Thu, 26 Jan 2006 09:19:56 +0000 Subject: [PATCH] Caching of pixmaps is added. If a pixmap was loaded once - it is added to an internal map, and returned from this map (whithout re-loading) on futher requests. --- src/Qtx/QtxResourceMgr.cxx | 56 +++++++++++++++++++++++++++++++++----- src/Qtx/QtxResourceMgr.h | 11 +++++++- 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/src/Qtx/QtxResourceMgr.cxx b/src/Qtx/QtxResourceMgr.cxx index 29e178a19..1e4e6223f 100644 --- a/src/Qtx/QtxResourceMgr.cxx +++ b/src/Qtx/QtxResourceMgr.cxx @@ -36,8 +36,9 @@ Level: Internal */ -QtxResourceMgr::Resources::Resources( const QString& fileName ) -: myFileName( fileName ) +QtxResourceMgr::Resources::Resources( const QtxResourceMgr* mgr, const QString& fileName ) +: myFileName( fileName ), + myMgr( const_cast( mgr ) ) { } @@ -130,6 +131,11 @@ QString QtxResourceMgr::Resources::path( const QString& sec, const QString& pref return filePath; } +QtxResourceMgr* QtxResourceMgr::Resources::resMgr() const +{ + return myMgr; +} + QtxResourceMgr::Section& QtxResourceMgr::Resources::section( const QString& sn ) { if ( !mySections.contains( sn ) ) @@ -157,12 +163,30 @@ QString QtxResourceMgr::Resources::fileName( const QString& sect, const QString& path = Qtx::addSlash( path ) + name; } } - return QDir::convertSeparators( path ); + if( !path.isEmpty() ) + { + QString fname = QDir::convertSeparators( path ); + QFileInfo inf( fname ); + fname = inf.absFilePath(); + return fname; + } + return QString(); } QPixmap QtxResourceMgr::Resources::loadPixmap( const QString& sect, const QString& prefix, const QString& name ) const { - return QPixmap( fileName( sect, prefix, name ) ); + QString fname = fileName( sect, prefix, name ); + bool toCache = resMgr() ? resMgr()->isPixmapCached() : false; + QPixmap p; + if( toCache && myPixmapCache.contains( fname ) ) + p = myPixmapCache[fname]; + else + { + p.load( fname ); + if( toCache ) + ( ( QMap& )myPixmapCache ).insert( fname, p ); + } + return p; } QTranslator* QtxResourceMgr::Resources::loadTranslator( const QString& sect, const QString& prefix, const QString& name ) const @@ -642,7 +666,8 @@ bool QtxResourceMgr::Format::save( Resources* res ) */ QtxResourceMgr::QtxResourceMgr( const QString& appName, const QString& resVarTemplate ) : myAppName( appName ), -myCheckExist( true ) + myCheckExist( true ), + myIsPixmapCached( true ) { QString envVar = !resVarTemplate.isEmpty() ? resVarTemplate : QString( "%1Resources" ); if ( envVar.contains( "%1" ) ) @@ -721,18 +746,35 @@ void QtxResourceMgr::initialize( const bool autoLoad ) const QtxResourceMgr* that = (QtxResourceMgr*)this; if ( !userFileName( appName() ).isEmpty() ) - that->myResources.append( new Resources( userFileName( appName() ) ) ); + that->myResources.append( new Resources( this, userFileName( appName() ) ) ); for ( QStringList::const_iterator it = myDirList.begin(); it != myDirList.end(); ++it ) { QString path = Qtx::addSlash( *it ) + globalFileName( appName() ); - that->myResources.append( new Resources( path ) ); + that->myResources.append( new Resources( this, path ) ); } if ( autoLoad ) that->load(); } +/*! + \brief Return true if all loaded pixmaps are stored in internal map; by default: true +*/ +bool QtxResourceMgr::isPixmapCached() const +{ + return myIsPixmapCached; +} + +/*! + \brief Set true, if it is necessary to store all loaded pixmap in internal map + (it accelerates following calls of loadPixmap) +*/ +void QtxResourceMgr::setIsPixmapCached( const bool on ) +{ + myIsPixmapCached = on; +} + /*! \brief Removes all resources from the manager. */ diff --git a/src/Qtx/QtxResourceMgr.h b/src/Qtx/QtxResourceMgr.h index 868dc9fde..9d7b8b26c 100644 --- a/src/Qtx/QtxResourceMgr.h +++ b/src/Qtx/QtxResourceMgr.h @@ -68,6 +68,9 @@ public: bool checkExisting() const; virtual void setCheckExisting( const bool ); + bool isPixmapCached() const; + void setIsPixmapCached( const bool ); + void clear(); bool value( const QString&, const QString&, int& ) const; @@ -165,6 +168,7 @@ private: bool myCheckExist; TransListMap myTranslator; QPixmap myDefaultPix; + bool myIsPixmapCached; }; /*! @@ -202,7 +206,7 @@ private: class QtxResourceMgr::Resources { public: - Resources( const QString& ); + Resources( const QtxResourceMgr*, const QString& ); virtual ~Resources(); QString file() const; @@ -230,6 +234,9 @@ public: QString path( const QString&, const QString&, const QString& ) const; +protected: + QtxResourceMgr* resMgr() const; + private: Section& section( const QString& ); const Section& section( const QString& ) const; @@ -242,6 +249,8 @@ private: private: SectionMap mySections; QString myFileName; + QMap myPixmapCache; + QtxResourceMgr* myMgr; friend class QtxResourceMgr::Format; }; -- 2.39.2