Commit 8111364e authored by Thomas BOUTROUE's avatar Thomas BOUTROUE

Import a lot of helpers from UI Elements 2.0

Also import the SVG symbols collection
parent a6be1a3f
Pipeline #988 passed with stage
in 5 seconds
......@@ -12,10 +12,16 @@
#include "QQuickGridContainer.h"
#include "QQuickRowContainer.h"
#include "QQuickStackContainer.h"
#include "QQuickSvgIconHelper.h"
#include "QQmlMimeIconsHelper.h"
#include "QQuickWindowIconHelper.h"
#include "QQmlIntrospector.h"
#include "QQmlFsSingleton.h"
#include <qqml.h>
void QtQmlTricks::registerComponents (void) {
void QtQmlTricks::registerComponents (QQmlEngine * engine) {
Q_UNUSED (engine)
static const QString ERR_ENUM_CLASS { QStringLiteral ("Enum-class !") };
static const QString ERR_ATTACHED_OBJ { QStringLiteral ("Attached-object class !") };
static const QString ERR_ABSTRACT_BASE { QStringLiteral ("Abstract base class !") };
......@@ -32,4 +38,19 @@ void QtQmlTricks::registerComponents (void) {
qmlRegisterUncreatableType<QQuickContainerAttachedObject> ("QtQmlTricks", 3, 0, "Container", ERR_ATTACHED_OBJ);
qmlRegisterUncreatableType<QQmlObjectListModelBase> ("QtQmlTricks", 3, 0, "ObjectListModel", ERR_ABSTRACT_BASE);
qmlRegisterUncreatableType<QQmlFastObjectListModelBase> ("QtQmlTricks", 3, 0, "FastObjectListModel", ERR_ABSTRACT_BASE);
qmlRegisterType<QQuickSvgIconHelper> ("QtQmlTricks", 3, 0, "SvgIconHelper");
qmlRegisterType<QQmlMimeIconsHelper> ("QtQmlTricks", 3, 0, "MimeIconsHelper");
qmlRegisterType<QQuickGroupItem> ("QtQmlTricks", 3, 0, "Group");
qmlRegisterType<QQuickWindowIconHelper> ("QtQmlTricks", 3, 0, "WindowIconHelper");
qmlRegisterType<QQmlFileSystemModelEntry> ("QtQmlTricks", 3, 0, "FileSystemModelEntry");
qmlRegisterSingletonType<QQmlFileSystemSingleton> ("QtQmlTricks", 3, 0, "FileSystem", &QQmlFileSystemSingleton::qmlSingletonProvider);
qmlRegisterSingletonType<QQmlIntrospector> ("QtQmlTricks", 3, 0, "Introspector", &QQmlIntrospector::qmlSingletonProvider);
QQuickSvgIconHelper::setBasePath (":/QtQmlTricks/icons");
if (engine != Q_NULLPTR) {
engine->addImportPath ("qrc:///imports");
}
else {
qWarning () << "You didn't pass a QML engine to the register function,"
<< "some features (mostly plain QML components, and icon theme provider) won't work !";
}
}
#ifndef QTQMLTRICKS_H
#define QTQMLTRICKS_H
#include <QQmlEngine>
class QtQmlTricks {
public:
static void registerComponents (void);
static void registerComponents (QQmlEngine * engine = Q_NULLPTR);
};
#endif // QTQMLTRICKS_H
This diff is collapsed.
#ifndef QQMLFSSINGLETON_H
#define QQMLFSSINGLETON_H
#include <QObject>
#include <QString>
#include <QQmlEngine>
#include <QJSEngine>
#include <QFileInfo>
#include <QMimeDatabase>
template<> inline bool qLess<QFileInfo>::operator() (const QFileInfo & left, const QFileInfo & right) const {
return (left.baseName ().toLower () < right.baseName ().toLower ());
}
class QQmlFileSystemModelEntry : public QObject {
Q_OBJECT
Q_PROPERTY (QString url READ getUrl CONSTANT)
Q_PROPERTY (QString name READ getName CONSTANT)
Q_PROPERTY (QString path READ getPath CONSTANT)
Q_PROPERTY (QString mimeType READ getMimeType CONSTANT)
Q_PROPERTY (QString extension READ getExtension CONSTANT)
Q_PROPERTY (QString lastModified READ getLastModified CONSTANT)
Q_PROPERTY (bool isDir READ getIsDir CONSTANT)
Q_PROPERTY (bool isFile READ getIsFile CONSTANT)
Q_PROPERTY (bool isLink READ getIsLink CONSTANT)
Q_PROPERTY (int size READ getSize CONSTANT)
Q_PROPERTY (int permissions READ getPermission CONSTANT)
public:
explicit QQmlFileSystemModelEntry (const QFileInfo & info = QFileInfo (), QObject * parent = Q_NULLPTR);
static QMimeDatabase MIME_DATABASE;
static const QString DATETIME_FORMAT;
const QString & getUrl (void) const;
const QString & getName (void) const;
const QString & getPath (void) const;
const QString & getMimeType (void) const;
const QString & getExtension (void) const;
const QString & getLastModified (void) const;
bool getIsDir (void) const;
bool getIsFile (void) const;
bool getIsLink (void) const;
int getSize (void) const;
int getPermission (void) const;
private:
const QString m_url;
const QString m_name;
const QString m_path;
const QString m_mimeType;
const QString m_extension;
const QString m_lastModified;
const bool m_isDir;
const bool m_isFile;
const bool m_isLink;
const int m_size;
const int m_permissions;
};
class QQmlFileSystemSingleton : public QObject {
Q_OBJECT
Q_ENUMS (Permission)
Q_PROPERTY (QString homePath READ getHomePath CONSTANT)
Q_PROPERTY (QString rootPath READ getRootPath CONSTANT)
Q_PROPERTY (QString tempPath READ getTempPath CONSTANT)
Q_PROPERTY (QString appDirPath READ getAppDirPath CONSTANT)
Q_PROPERTY (QString appCachePath READ getAppCachePath CONSTANT)
Q_PROPERTY (QString appConfigPath READ getAppConfigPath CONSTANT)
Q_PROPERTY (QString documentsPath READ getDocumentsPath CONSTANT)
Q_PROPERTY (QString imagesPath READ getImagesPath CONSTANT)
Q_PROPERTY (QString musicPath READ getMusicPath CONSTANT)
Q_PROPERTY (QString videosPath READ getVideosPath CONSTANT)
Q_PROPERTY (QString downloadsPath READ getDownloadsPath CONSTANT)
Q_PROPERTY (QString workingDirectoryPath READ getWorkingDirectoryPath CONSTANT)
Q_PROPERTY (QStringList drivesList READ getDrivesList NOTIFY drivesListChanged)
public:
static QObject * qmlSingletonProvider (QQmlEngine * qmlEngine, QJSEngine * jsEngine);
enum Permission {
ReadOwner = 0x4000, WriteOwner = 0x2000, ExeOwner = 0x1000,
ReadUser = 0x0400, WriteUser = 0x0200, ExeUser = 0x0100,
ReadGroup = 0x0040, WriteGroup = 0x0020, ExeGroup = 0x0010,
ReadOther = 0x0004, WriteOther = 0x0002, ExeOther = 0x0001,
};
const QString & getHomePath (void) const;
const QString & getRootPath (void) const;
const QString & getTempPath (void) const;
const QString & getAppDirPath (void) const;
const QString & getAppCachePath (void) const;
const QString & getAppConfigPath (void) const;
const QString & getDocumentsPath (void) const;
const QString & getImagesPath (void) const;
const QString & getMusicPath (void) const;
const QString & getVideosPath (void) const;
const QString & getDownloadsPath (void) const;
const QString & getWorkingDirectoryPath (void) const;
const QStringList & getDrivesList (void) const;
public slots:
bool isDir (const QString & path) const;
bool isFile (const QString & path) const;
bool isLink (const QString & path) const;
bool hasParent (const QString & path) const;
bool exists (const QString & path) const;
bool copy (const QString & sourcePath, const QString & destPath) const;
bool move (const QString & sourcePath, const QString & destPath) const;
bool link (const QString & sourcePath, const QString & destPath) const;
bool remove (const QString & path) const;
bool mkpath (const QString & path) const;
int size (const QString & path) const;
QString parentDir (const QString & path) const;
QString baseName (const QString & path) const;
QString readTextFile (const QString & path) const;
bool writeTextFile (const QString & path, const QString & text) const;
QString pathFromUrl (const QUrl & url) const;
QUrl urlFromPath (const QString & path) const;
QVariantList list (const QString & dirPath,
const QStringList & nameFilters = QStringList (),
const bool showHidden = false,
const bool showFiles = true,
const bool showDirs = true,
const bool recursive = false) const;
signals:
void drivesListChanged (const QStringList & drivesList);
protected slots:
void doRefreshDrives (void);
private:
const QString m_homePath;
const QString m_rootPath;
const QString m_tempPath;
const QString m_appDirPath;
const QString m_appCachePath;
const QString m_appConfigPath;
const QString m_documentsPath;
const QString m_imagesPath;
const QString m_musicPath;
const QString m_videosPath;
const QString m_downloadsPath;
const QString m_workingDirectoryPath;
QStringList m_drivesList;
explicit QQmlFileSystemSingleton (QObject * parent = Q_NULLPTR);
};
#endif // QQMLFSSINGLETON_H
#include "QQmlIntrospector.h"
QQuickGroupItem::QQuickGroupItem (QQuickItem * parent)
: QQuickItem { parent }
{ }
QQuickGroupItem::~QQuickGroupItem (void) { }
QQmlIntrospector::QQmlIntrospector (QObject * parent) : QObject (parent) { }
QObject * QQmlIntrospector::qmlSingletonProvider (QQmlEngine * qmlEngine, QJSEngine * jsEngine) {
Q_UNUSED (qmlEngine)
Q_UNUSED (jsEngine)
return new QQmlIntrospector;
}
bool QQmlIntrospector::isGroupItem (QObject * object) {
return (qobject_cast<QQuickGroupItem *> (object) != Q_NULLPTR);
}
QQuickWindow * QQmlIntrospector::window (QQuickItem * item) {
return (item != Q_NULLPTR ? item->window () : Q_NULLPTR);
}
#ifndef QQMLINTROSPECTOR_H
#define QQMLINTROSPECTOR_H
#include <QObject>
#include <QQuickItem>
#include <QQuickWindow>
#include <QQmlComponent>
#include <QQmlEngine>
#include "QmlPropertyHelpers.h"
class QQuickGroupItem : public QQuickItem {
Q_OBJECT
QML_WRITABLE_CSTREF_PROPERTY (title, QString)
QML_WRITABLE_PTR_PROPERTY (icon, QQmlComponent)
public:
explicit QQuickGroupItem (QQuickItem * parent = Q_NULLPTR);
virtual ~QQuickGroupItem (void);
};
class QQmlIntrospector : public QObject {
Q_OBJECT
public:
explicit QQmlIntrospector (QObject * parent = Q_NULLPTR);
static QObject * qmlSingletonProvider (QQmlEngine * qmlEngine, QJSEngine * jsEngine);
Q_INVOKABLE bool isGroupItem (QObject * object);
Q_INVOKABLE QQuickWindow * window (QQuickItem * item);
};
#endif // QQMLINTROSPECTOR_H
#include "QQmlMimeIconsHelper.h"
#include <QIcon>
#include <QFile>
#include <QFileInfo>
#include <QStandardPaths>
#include <QUrl>
QQmlMimeIconsHelper::MetaDataCache QQmlMimeIconsHelper::s_cache;
QQmlMimeIconsHelper::QQmlMimeIconsHelper (QObject * parent) : QObject (parent) { }
QString QQmlMimeIconsHelper::getSvgIconPathForUrl (const QString & url) {
QString ret;
const QString path = QUrl (url).toLocalFile ();
const QFileInfo info (path);
if (info.exists ()) {
if (info.isDir ()) {
ret = s_cache.getSpecialFolderIconForPath (path);
if (ret.isEmpty ()) {
ret = "folder-closed";
}
}
else {
const QMimeType type = s_cache.getMimeTypeForFile (path);
const QString tmp = type.name ();
ret = s_cache.getSvgIconForMimeType (tmp);
if (ret.isEmpty ()) {
ret = "file";
//qWarning () << "No icon for this MIME-type" << tmp;
}
}
}
return QStringLiteral ("qrc:///QtQmlTricks/icons/filetypes/%1.svg").arg (ret);
}
QString QQmlMimeIconsHelper::getThemeIconNameForUrl (const QString & url) {
QString ret;
const QString path = QUrl (url).toLocalFile ();
const QFileInfo info (path);
if (info.exists ()) {
if (info.isDir ()) {
ret = s_cache.getSpecialFolderIconForPath (path);
if (ret.isEmpty ()) {
ret = "folder";
}
}
else {
const QMimeType type = s_cache.getMimeTypeForFile (path);
const QString tmp = type.name ();
ret = s_cache.getThemeIconForMimeType (tmp);
if (ret.isEmpty ()) {
if (QIcon::hasThemeIcon (type.iconName ())) {
s_cache.registerThemeIconForMimeType (tmp, type.iconName ());
}
else if (QIcon::hasThemeIcon (type.genericIconName ())) {
s_cache.registerThemeIconForMimeType (tmp, type.genericIconName ());
}
else {
s_cache.registerThemeIconForMimeType (tmp, "empty");
}
}
ret = s_cache.getThemeIconForMimeType (tmp);
}
}
return ret;
}
void QQmlMimeIconsHelper::MetaDataCache::registerSpecialFolderIconForPath (const QString & path, const QString & icon) {
specialFoldersIconNames.insert (path, icon);
}
void QQmlMimeIconsHelper::MetaDataCache::registerSvgIconForMimeType (const QString & type, const QString & icon) {
svgIconForMimetype.insert (type, icon);
}
void QQmlMimeIconsHelper::MetaDataCache::registerThemeIconForMimeType (const QString & type, const QString & icon) {
themeIconNameForMimeType.insert (type, icon);
}
QMimeType QQmlMimeIconsHelper::MetaDataCache::getMimeTypeForFile (const QString & path) {
return mimeDb.mimeTypeForFile (path);
}
QString QQmlMimeIconsHelper::MetaDataCache::getSpecialFolderIconForPath (const QString & path) {
return specialFoldersIconNames.value (path);
}
QString QQmlMimeIconsHelper::MetaDataCache::getSvgIconForMimeType (const QString & type) {
return svgIconForMimetype.value (type);
}
QString QQmlMimeIconsHelper::MetaDataCache::getThemeIconForMimeType (const QString & type) {
return themeIconNameForMimeType.value (type);
}
QQmlMimeIconsHelper::MetaDataCache::MetaDataCache (void) {
svgIconForMimetype.reserve (100);
specialFoldersIconNames.reserve (20);
themeIconNameForMimeType.reserve (100);
/// register special folders
foreach (const QString & path, QStandardPaths::standardLocations (QStandardPaths::HomeLocation)) {
registerSpecialFolderIconForPath (path, "folder-home");
}
foreach (const QString & path, QStandardPaths::standardLocations (QStandardPaths::DocumentsLocation)) {
registerSpecialFolderIconForPath (path, "folder-documents");
}
foreach (const QString & path, QStandardPaths::standardLocations (QStandardPaths::MusicLocation)) {
registerSpecialFolderIconForPath (path, "folder-music");
}
foreach (const QString & path, QStandardPaths::standardLocations (QStandardPaths::PicturesLocation)) {
registerSpecialFolderIconForPath (path, "folder-images");
}
foreach (const QString & path, QStandardPaths::standardLocations (QStandardPaths::MoviesLocation)) {
registerSpecialFolderIconForPath (path, "folder-videos");
}
foreach (const QString & path, QStandardPaths::standardLocations (QStandardPaths::DownloadLocation)) {
registerSpecialFolderIconForPath (path, "folder-downloads");
}
/// register SVG icons for MIME-types
registerSvgIconForMimeType ("image/png", "image");
registerSvgIconForMimeType ("image/jpeg", "image");
registerSvgIconForMimeType ("image/gif", "image");
registerSvgIconForMimeType ("image/svg", "drawing");
registerSvgIconForMimeType ("image/svg+xml", "drawing");
registerSvgIconForMimeType ("application/vnd.oasis.opendocument.graphics", "drawing");
registerSvgIconForMimeType ("audio/mpeg", "sound");
registerSvgIconForMimeType ("audio/x-wav", "sound");
registerSvgIconForMimeType ("audio/midi", "sound");
registerSvgIconForMimeType ("video/mp4", "video");
registerSvgIconForMimeType ("text/x-csrc", "code");
registerSvgIconForMimeType ("text/x-chdr", "code");
registerSvgIconForMimeType ("text/x-c++src", "code");
registerSvgIconForMimeType ("text/x-c++hdr", "code");
registerSvgIconForMimeType ("text/x-qml", "code");
registerSvgIconForMimeType ("text/x-java", "code");
registerSvgIconForMimeType ("text/css", "code");
registerSvgIconForMimeType ("application/javascript", "code");
registerSvgIconForMimeType ("application/xml", "xml");
registerSvgIconForMimeType ("application/x-shellscript", "script");
registerSvgIconForMimeType ("application/x-perl", "script");
registerSvgIconForMimeType ("application/x-object", "binary");
registerSvgIconForMimeType ("application/octet-stream", "binary");
registerSvgIconForMimeType ("application/x-cd-image", "disk-image");
registerSvgIconForMimeType ("application/zip", "archive");
registerSvgIconForMimeType ("application/x-xz-compressed-tar", "archive");
registerSvgIconForMimeType ("application/x-compressed-tar", "archive");
registerSvgIconForMimeType ("application/x-rar", "archive");
registerSvgIconForMimeType ("application/x-rpm", "archive");
registerSvgIconForMimeType ("application/gzip", "archive");
registerSvgIconForMimeType ("application/vnd.debian.binary-package", "archive");
registerSvgIconForMimeType ("application/vnd.android.package-archive", "archive");
registerSvgIconForMimeType ("application/x-7z-compressed", "archive");
registerSvgIconForMimeType ("application/x-bzip-compressed-tar", "archive");
registerSvgIconForMimeType ("text/x-makefile", "text");
registerSvgIconForMimeType ("text/x-log", "text");
registerSvgIconForMimeType ("text/x-theme", "text");
registerSvgIconForMimeType ("text/csv", "text");
registerSvgIconForMimeType ("text/plain", "text");
registerSvgIconForMimeType ("text/vcard", "text");
registerSvgIconForMimeType ("text/markdown", "text");
registerSvgIconForMimeType ("application/json", "text");
registerSvgIconForMimeType ("application/pdf", "pdf");
registerSvgIconForMimeType ("application/vnd.oasis.opendocument.text", "document");
registerSvgIconForMimeType ("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "document");
registerSvgIconForMimeType ("application/msword", "document");
registerSvgIconForMimeType ("application/vnd.oasis.opendocument.spreadsheet", "spreadsheet");
registerSvgIconForMimeType ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "spreadsheet");
registerSvgIconForMimeType ("application/vnd.ms-excel", "spreadsheet");
registerSvgIconForMimeType ("application/ms-excel", "spreadsheet");
registerSvgIconForMimeType ("application/vnd.oasis.opendocument.presentation", "slideshow");
registerSvgIconForMimeType ("application/vnd.openxmlformats-officedocument.presentationml.presentation", "slideshow");
registerSvgIconForMimeType ("application/vnd.ms-powerpoint", "slideshow");
registerSvgIconForMimeType ("text/html", "webpage");
registerSvgIconForMimeType ("application/sql", "database");
registerSvgIconForMimeType ("application/x-sqlite3", "database");
registerSvgIconForMimeType ("application/x-executable", "executable");
registerSvgIconForMimeType ("application/x-ms-dos-executable", "executable");
}
#ifndef QQMLMIMEICONSHELPER_H
#define QQMLMIMEICONSHELPER_H
#include <QObject>
#include <QString>
#include <QHash>
#include <QMimeDatabase>
#include <QMimeType>
class QQmlMimeIconsHelper : public QObject {
Q_OBJECT
public:
explicit QQmlMimeIconsHelper (QObject * parent = Q_NULLPTR);
public slots:
QString getSvgIconPathForUrl (const QString & url);
QString getThemeIconNameForUrl (const QString & url);
private:
class MetaDataCache {
public:
explicit MetaDataCache (void);
void registerSpecialFolderIconForPath (const QString & path, const QString & icon);
void registerSvgIconForMimeType (const QString & type, const QString & icon);
void registerThemeIconForMimeType (const QString & type, const QString & icon);
QString getSpecialFolderIconForPath (const QString & path);
QString getSvgIconForMimeType (const QString & type);
QString getThemeIconForMimeType (const QString & type);
QMimeType getMimeTypeForFile (const QString & path);
private:
QMimeDatabase mimeDb;
QHash<QString, QString> svgIconForMimetype;
QHash<QString, QString> themeIconNameForMimeType;
QHash<QString, QString> specialFoldersIconNames;
};
static MetaDataCache s_cache;
};
#endif // QQMLMIMEICONSHELPER_H
This diff is collapsed.
#ifndef QQMLSVGICONHELPER_H
#define QQMLSVGICONHELPER_H
#include <QObject>
#include <QString>
#include <QColor>
#include <QTimer>
#include <QQmlProperty>
#include <QQmlParserStatus>
#include <QQmlPropertyValueSource>
class SvgMetaDataCache;
class QQuickSvgIconHelper : public QObject, public QQmlParserStatus, public QQmlPropertyValueSource {
Q_OBJECT
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)
Q_PROPERTY (QString icon READ getIcon WRITE setIcon NOTIFY iconChanged)
public:
explicit QQuickSvgIconHelper (QObject * parent = Q_NULLPTR);
~QQuickSvgIconHelper (void);
static void setBasePath (const QString & basePath);
static void setCachePath (const QString & cachePath);
void classBegin (void);
void componentComplete (void);
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;
const QString & getIcon (void) const;
Q_INVOKABLE void scheduleRefresh (void);
public slots:
void setSize (const int size);
void setVerticalRatio (const qreal ratio);
void setHorizontalRatio (const qreal ratio);
void setColor (const QColor & color);
void setIcon (const QString & icon);
signals:
void sizeChanged (void);
void liveChanged (void);
void verticalRatioChanged (void);
void horizontalRatioChanged (void);
void colorChanged (void);
void iconChanged (void);
protected:
void doProcessIcon (void);
void doForceRegen (void);
private:
int m_size;
bool m_live;
bool m_ready;
qreal m_verticalRatio;
qreal m_horizontalRatio;
QColor m_color;
QString m_icon;
QString m_hash;
QString m_sourcePath;
QString m_cachedPath;
QTimer m_inhibitTimer;
QQmlProperty m_property;
friend class SvgMetaDataCache;
static SvgMetaDataCache & cache (void);
};
#endif // QQMLSVGICONHELPER_H
#include "QQuickWindowIconHelper.h"
#include <QUrl>
#include <QIcon>
#include <QWindow>
#include <QQuickWindow>
QQuickWindowIconHelper::QQuickWindowIconHelper (QQuickItem * parent)
: QQuickItem (parent)
{
connect (this, &QQuickWindowIconHelper::windowChanged, this, &QQuickWindowIconHelper::refreshWindowIcon);
connect (this, &QQuickWindowIconHelper::iconPathChanged, this, &QQuickWindowIconHelper::refreshWindowIcon);
}
void QQuickWindowIconHelper::componentComplete (void) {
QQuickItem::componentComplete ();
refreshWindowIcon ();
}
const QString & QQuickWindowIconHelper::getIconPath (void) const {
return m_iconPath;
}
void QQuickWindowIconHelper::setIconPath (const QString & iconPath) {
if (m_iconPath != iconPath) {
m_iconPath = iconPath;
emit iconPathChanged (iconPath);
}