Icons created from a file keep the file name of that file so it can be also used to display the file in HTML code

This commit is contained in:
Klaus Basan
2018-07-24 16:52:50 +02:00
parent 75ee43c0e1
commit 9dd9faa2d6
4 changed files with 27 additions and 14 deletions

View File

@@ -15,6 +15,7 @@
#include <QStringBuilder>
#include <QtGlobal>
#include <QDir>
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
{

View File

@@ -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)
);
};

View File

@@ -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)

View File

@@ -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