Commit c302d888 authored by Thomas BOUTROUE's avatar Thomas BOUTROUE

Simplify SvgIconHelper / SvgIconLoader

parent 73e64612
Pipeline #992 passed with stage
in 13 seconds
......@@ -17,44 +17,19 @@
#include <QSvgRenderer>
#include <QMultiHash>
#include <QByteArray>
#include <QSocketNotifier>
#include <QDebug>
#include <QVarLengthArray>
#ifdef Q_OS_LINUX
# include <stdio.h>
# include <errno.h>
# include <unistd.h>
# include <iostream>
# include <sys/ioctl.h>
# include <sys/inotify.h>
#endif
class SvgMetaDataCache : public QObject {
class SvgMetaDataCache {
public:
explicit SvgMetaDataCache (QObject * parent = Q_NULLPTR)
: QObject (parent)
, inotifyFd (-1)
, inotifyWatcher (Q_NULLPTR)
, hasher (QCryptographicHash::Md5)
explicit SvgMetaDataCache (void)
: hasher (QCryptographicHash::Md5)
{
changeBasePath (qApp->applicationDirPath ());
changeCachePath (QStandardPaths::writableLocation (QStandardPaths::CacheLocation) % "/SvgIconsCache");
#ifdef Q_OS_LINUX
inotifyFd = inotify_init ();
inotifyWatcher = new QSocketNotifier (inotifyFd, QSocketNotifier::Read);
connect (inotifyWatcher, &QSocketNotifier::activated, this, &SvgMetaDataCache::onInotifyEvent);
#endif
}
virtual ~SvgMetaDataCache (void) {
#ifdef Q_OS_LINUX
for (QHash<QString, int>::const_iterator it = descriptorsIndex.constBegin (); it != descriptorsIndex.constEnd (); ++it) {
inotify_rm_watch (inotifyFd, it.value ());
}
::close (inotifyFd);
#endif
}
virtual ~SvgMetaDataCache (void) { }
void changeBasePath (const QString & path) {
basePath = path;
......@@ -153,75 +128,11 @@ public:
return ret;
}
void addFileWatcher (const QString & path, QQuickSvgIconHelper * svgHelper) {
if (!path.isEmpty () && !path.startsWith (":/") && !path.startsWith ("qrc:/") && svgHelper != Q_NULLPTR) {
#ifdef Q_OS_LINUX
const QByteArray tmp = path.toLatin1 ();
if (!descriptorsIndex.contains (path)) {
const int desc = inotify_add_watch (inotifyFd, tmp.constData (), IN_MODIFY | IN_DELETE_SELF);
descriptorsIndex.insert (path, desc);
}
if (!svgHelpersIndex.contains (path, svgHelper)) {
svgHelpersIndex.insert (path, svgHelper);
}
#endif
}
}
void removeFileWatcher (const QString & path, QQuickSvgIconHelper * svgHelper) {
if (!path.isEmpty () && !path.startsWith (":/") && !path.startsWith ("qrc:/") && svgHelper != Q_NULLPTR) {
#ifdef Q_OS_LINUX
svgHelpersIndex.remove (path, svgHelper);
if (svgHelpersIndex.values (path).isEmpty ()) {
const int desc = descriptorsIndex.value (path, -1);
if (desc > -1) {
inotify_rm_watch (inotifyFd, desc);
descriptorsIndex.remove (path);
}
}
#endif
}
}
protected:
void onInotifyEvent (void) {
#ifdef Q_OS_LINUX
static const int SIZEOF_EVT = sizeof (inotify_event);
ssize_t buffSize = 0;
::ioctl (inotifyFd, FIONREAD, &buffSize);
QByteArray buf (static_cast<int> (buffSize), '\0');
buffSize = ::read (inotifyFd, buf.data (), static_cast<size_t> (buffSize));
inotifyBuffer.append (buf.left (static_cast<int> (buffSize)));
while (static_cast<int> (inotifyBuffer.size ()) >= SIZEOF_EVT) {
const inotify_event * evt = reinterpret_cast<const inotify_event *> (inotifyBuffer.constData ());
const QString path = descriptorsIndex.key (evt->wd);
if (!path.isEmpty ()) {
const QList<QQuickSvgIconHelper *> watchersList = svgHelpersIndex.values (path);
for (QList<QQuickSvgIconHelper *>::const_iterator it = watchersList.constBegin (); it != watchersList.constEnd (); ++it) {
QQuickSvgIconHelper * svgHelper = (* it);
if (svgHelper != Q_NULLPTR) {
if (evt->mask & IN_MODIFY) {
emit svgHelper->doForceRegen ();
}
else if (evt->mask & IN_DELETE_SELF) {
emit svgHelper->doForceRegen ();
}
else { }
}
}
}
inotifyBuffer.remove (0, static_cast<int> (SIZEOF_EVT + evt->len));
}
#endif
}
private:
int inotifyFd;
QString basePath;
QString cachePath;
QByteArray inotifyBuffer;
QSvgRenderer renderer;
QSocketNotifier * inotifyWatcher;
QCryptographicHash hasher;
QHash<QString, int> descriptorsIndex;
QHash<QString, QString> checksumsIndex;
......@@ -231,7 +142,6 @@ private:
QQuickSvgIconHelper::QQuickSvgIconHelper (QObject * parent)
: QObject (parent)
, m_size (0)
, m_live (false)
, m_ready (false)
, m_verticalRatio (1.0)
, m_horizontalRatio (1.0)
......@@ -244,9 +154,7 @@ QQuickSvgIconHelper::QQuickSvgIconHelper (QObject * parent)
connect (&m_inhibitTimer, &QTimer::timeout, this, &QQuickSvgIconHelper::doProcessIcon, Qt::UniqueConnection);
}
QQuickSvgIconHelper::~QQuickSvgIconHelper () {
cache ().removeFileWatcher (m_sourcePath, this);
}
QQuickSvgIconHelper::~QQuickSvgIconHelper (void) { }
SvgMetaDataCache & QQuickSvgIconHelper::cache (void) {
static SvgMetaDataCache ret;
......@@ -279,10 +187,6 @@ int QQuickSvgIconHelper::getSize (void) const {
return m_size;
}
bool QQuickSvgIconHelper::getLive (void) const {
return m_live;
}
qreal QQuickSvgIconHelper::getVerticalRatio (void) const {
return m_verticalRatio;
}
......@@ -364,12 +268,6 @@ void QQuickSvgIconHelper::doProcessIcon (void) {
: cache ().baseFile (m_icon % ".svg")));
if (m_sourcePath != sourcePath) {
m_sourcePath = sourcePath;
const bool live = !sourcePath.startsWith (':');
if (m_live != live) {
m_live = live;
emit liveChanged ();
}
cache ().addFileWatcher (m_sourcePath, this);
}
if (QFile::exists (m_sourcePath)) {
m_hash = cache ().hashData (m_sourcePath.toLatin1 ());
......
......@@ -16,7 +16,6 @@ class QQuickSvgIconHelper : public QObject, public QQmlParserStatus, public QQml
Q_INTERFACES (QQmlParserStatus)
Q_INTERFACES (QQmlPropertyValueSource)
Q_PROPERTY (int size READ getSize WRITE setSize NOTIFY sizeChanged)
Q_PROPERTY (bool live READ getLive NOTIFY liveChanged)
Q_PROPERTY (qreal verticalRatio READ getVerticalRatio WRITE setVerticalRatio NOTIFY verticalRatioChanged)
Q_PROPERTY (qreal horizontalRatio READ getHorizontalRatio WRITE setHorizontalRatio NOTIFY horizontalRatioChanged)
Q_PROPERTY (QColor color READ getColor WRITE setColor NOTIFY colorChanged)
......@@ -24,7 +23,7 @@ class QQuickSvgIconHelper : public QObject, public QQmlParserStatus, public QQml
public:
explicit QQuickSvgIconHelper (QObject * parent = Q_NULLPTR);
~QQuickSvgIconHelper (void);
virtual ~QQuickSvgIconHelper (void);
static void setBasePath (const QString & basePath);
static void setCachePath (const QString & cachePath);
......@@ -34,7 +33,6 @@ public:
void setTarget (const QQmlProperty & target);
int getSize (void) const;
bool getLive (void) const;
qreal getVerticalRatio (void) const;
qreal getHorizontalRatio (void) const;
const QColor & getColor (void) const;
......@@ -51,7 +49,6 @@ public slots:
signals:
void sizeChanged (void);
void liveChanged (void);
void verticalRatioChanged (void);
void horizontalRatioChanged (void);
void colorChanged (void);
......@@ -63,7 +60,6 @@ protected:
private:
int m_size;
bool m_live;
bool m_ready;
qreal m_verticalRatio;
qreal m_horizontalRatio;
......
......@@ -14,7 +14,7 @@ Item {
Image {
id: img;
cache: !helper.live;
cache: true;
smooth: false;
opacity: (dimmed ? 0.65 : 1.0);
fillMode: Image.Pad;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment