diff --git a/src/blackmisc/icon.cpp b/src/blackmisc/icon.cpp index e6bf8f23e..05527c82e 100644 --- a/src/blackmisc/icon.cpp +++ b/src/blackmisc/icon.cpp @@ -15,6 +15,7 @@ #include #include +#include namespace BlackMisc { @@ -26,8 +27,12 @@ namespace BlackMisc { } CIcon::CIcon(const QString &resourceFilePath, const QString &descriptiveText) : - m_index(CIcons::IconIsFile), m_descriptiveText(descriptiveText), m_pixmap(CIcons::pixmapByResourceFileName(resourceFilePath)) - {} + m_index(CIcons::IconIsFile), m_descriptiveText(descriptiveText) + { + QString fullPath; + m_pixmap = CIcons::pixmapByResourceFileName(QDir::cleanPath(resourceFilePath), fullPath); + m_fileResourcePath = fullPath; + } CIcons::IconIndex CIcon::getIndex() const { diff --git a/src/blackmisc/icon.h b/src/blackmisc/icon.h index 08dcf1ecc..c014d9571 100644 --- a/src/blackmisc/icon.h +++ b/src/blackmisc/icon.h @@ -89,7 +89,13 @@ namespace BlackMisc CIcon(const QString &resourceFilePath, const QString &descriptiveText); //! Get descriptive text - const QString &getDescriptiveText() const { return this->m_descriptiveText; } + const QString &getDescriptiveText() const { return m_descriptiveText; } + + //! Resource path if any + const QString &getFileResourcePath() const { return m_fileResourcePath; } + + //! Resource path available? + bool hasFileResourcePath() const { return !m_fileResourcePath.isEmpty(); } //! Index CIcons::IconIndex getIndex() const; @@ -113,13 +119,13 @@ namespace BlackMisc QIcon toQIcon() const; //! Rotate by n degrees - void setRotation(int degrees) { this->m_rotateDegrees = degrees; } + void setRotation(int degrees) { m_rotateDegrees = degrees; } //! Rotate by given degrees void setRotation(const BlackMisc::PhysicalQuantities::CAngle &rotate); //! Set descriptive text - void setDescriptiveText(const QString &text) { this->m_descriptiveText = text; } + void setDescriptiveText(const QString &text) { m_descriptiveText = text; } //! Implicit conversion operator QPixmap() const { return this->toPixmap(); } @@ -137,13 +143,15 @@ namespace BlackMisc CIcons::IconIndex m_index = CIcons::NotSet; int m_rotateDegrees = 0; //!< Rotation QString m_descriptiveText; //!< what does it represent? + QString m_fileResourcePath; //!< file resource path QPixmap m_pixmap; //!< Used with generated pixmaps, when not used with index BLACK_METACLASS( CIcon, BLACK_METAMEMBER(index), BLACK_METAMEMBER(rotateDegrees, 0, DisabledForComparison | DisabledForHashing), - BLACK_METAMEMBER(descriptiveText) + BLACK_METAMEMBER(descriptiveText), + BLACK_METAMEMBER(fileResourcePath) ); }; diff --git a/src/blackmisc/icons.cpp b/src/blackmisc/icons.cpp index 6af7732f7..d3f26fb3b 100644 --- a/src/blackmisc/icons.cpp +++ b/src/blackmisc/icons.cpp @@ -1222,20 +1222,20 @@ namespace BlackMisc return rotate(rotateDegrees, pixmapByIndex(index)); } - const QPixmap &CIcons::pixmapByResourceFileName(const QString &fileName) + const QPixmap &CIcons::pixmapByResourceFileName(const QString &relativeFileName, QString &fullFilePath) { //! \fixme KB 20170701 noticed the "cache" is not threadsafe. However, there has never be an issue so far. Added thread assert. - Q_ASSERT_X(!fileName.isEmpty(), Q_FUNC_INFO, "missing filename"); + Q_ASSERT_X(!relativeFileName.isEmpty(), Q_FUNC_INFO, "missing filename"); Q_ASSERT_X(CThreadUtils::isCurrentThreadApplicationThread(), Q_FUNC_INFO, "not thread safe"); - if (!getResourceFileCache().contains(fileName)) + fullFilePath = CFileUtils::appendFilePaths(CDirectoryUtils::imagesDirectory(), relativeFileName); + if (!getResourceFileCache().contains(relativeFileName)) { - const QString path = CFileUtils::appendFilePaths(CDirectoryUtils::imagesDirectory(), fileName); QPixmap pm; - const bool s = pm.load(path); - getResourceFileCache().insert(fileName, s ? pm : CIcons::empty()); + const bool s = pm.load(fullFilePath); + CIcons::getResourceFileCache().insert(relativeFileName, s ? pm : CIcons::empty()); } - return getResourceFileCache()[fileName]; + return CIcons::getResourceFileCache()[relativeFileName]; } QImage CIcons::changeImageBackgroundColor(const QImage &imgSource, Qt::GlobalColor backgroundColor) diff --git a/src/blackmisc/icons.h b/src/blackmisc/icons.h index 24933d0b9..114113cf9 100644 --- a/src/blackmisc/icons.h +++ b/src/blackmisc/icons.h @@ -718,7 +718,7 @@ namespace BlackMisc // ------------------------------------------------------------- //! Pixmap by given index - static const QPixmap &pixmapByResourceFileName(const QString &fileName); + static const QPixmap &pixmapByResourceFileName(const QString &relativeFileName, QString &fullFilePath); // ------------------------------------------------------------- // Utility functions