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
......@@ -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
#include "QQmlFsSingleton.h"
#include <QTimer>
#include <QUrl>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QStandardPaths>
#include <QCoreApplication>
#include <QDateTime>
#include <QMimeDatabase>
#include <QMimeType>
#include <QStringBuilder>
#include <QDirIterator>
#include <QDebug>
QMimeDatabase QQmlFileSystemModelEntry::MIME_DATABASE;
const QString QQmlFileSystemModelEntry::DATETIME_FORMAT = QStringLiteral ("yyyy-MM-dd hh:mm:ss.zzz");
QQmlFileSystemSingleton::QQmlFileSystemSingleton (QObject * parent)
: QObject (parent)
, m_homePath (QDir::homePath ())
, m_rootPath (QDir::rootPath ())
, m_tempPath (QDir::tempPath ())
, m_appDirPath (QCoreApplication::applicationDirPath ())
, m_appCachePath (QStandardPaths::writableLocation (QStandardPaths::CacheLocation))
#if QT_VERSION >= 0x050500
, m_appConfigPath (QStandardPaths::writableLocation (QStandardPaths::AppConfigLocation))
#else
, m_appConfigPath (QStandardPaths::writableLocation (QStandardPaths::ConfigLocation) % '/' % QCoreApplication::applicationName ())
#endif
, m_documentsPath (QStandardPaths::writableLocation (QStandardPaths::DocumentsLocation))
, m_imagesPath (QStandardPaths::writableLocation (QStandardPaths::PicturesLocation))
, m_musicPath (QStandardPaths::writableLocation (QStandardPaths::MusicLocation))
, m_videosPath (QStandardPaths::writableLocation (QStandardPaths::MoviesLocation))
, m_downloadsPath (QStandardPaths::writableLocation (QStandardPaths::DownloadLocation))
, m_workingDirectoryPath (QDir::currentPath ())
{
doRefreshDrives ();
}
QObject * QQmlFileSystemSingleton::qmlSingletonProvider (QQmlEngine * qmlEngine, QJSEngine * jsEngine) {
Q_UNUSED (qmlEngine)
Q_UNUSED (jsEngine)
return new QQmlFileSystemSingleton;
}
void QQmlFileSystemSingleton::doRefreshDrives (void) {
QStringList tmp;
const QList<QFileInfo> infoList = QDir::drives ();
for (QList<QFileInfo>::const_iterator it = infoList.constBegin (); it != infoList.constEnd (); ++it) {
tmp.append ((* it).absolutePath ());
}
if (m_drivesList != tmp) {
m_drivesList = tmp;
emit drivesListChanged (m_drivesList);
}
#ifdef Q_OS_WIN
# if QT_VERSION >= 0x050400
QTimer::singleShot (5000, this, &QQmlFileSystemSingleton::doRefreshDrives);
# else
QTimer::singleShot (5000, this, SLOT(doRefreshDrives()));
# endif
#endif
}
const QString & QQmlFileSystemSingleton::getHomePath (void) const {
return m_homePath;
}
const QString & QQmlFileSystemSingleton::getRootPath (void) const {
return m_rootPath;
}
const QString & QQmlFileSystemSingleton::getTempPath (void) const {
return m_tempPath;
}
const QString & QQmlFileSystemSingleton::getAppDirPath (void) const {
return m_appDirPath;
}
const QString & QQmlFileSystemSingleton::getAppCachePath (void) const {
return m_appCachePath;
}
const QString & QQmlFileSystemSingleton::getAppConfigPath (void) const {
return m_appConfigPath;
}
const QString & QQmlFileSystemSingleton::getDocumentsPath (void) const {
return m_documentsPath;
}
const QString & QQmlFileSystemSingleton::getImagesPath (void) const {
return m_imagesPath;
}
const QString & QQmlFileSystemSingleton::getMusicPath (void) const {
return m_musicPath;
}
const QString & QQmlFileSystemSingleton::getVideosPath (void) const {
return m_videosPath;
}
const QString & QQmlFileSystemSingleton::getDownloadsPath (void) const {
return m_downloadsPath;
}
const QString & QQmlFileSystemSingleton::getWorkingDirectoryPath (void) const {
return m_workingDirectoryPath;
}
const QStringList & QQmlFileSystemSingleton::getDrivesList (void) const {
return m_drivesList;
}
bool QQmlFileSystemSingleton::isDir (const QString & path) const {
return QFileInfo (path).isDir ();
}
bool QQmlFileSystemSingleton::isFile (const QString & path) const {
return QFileInfo (path).isFile ();
}
bool QQmlFileSystemSingleton::isLink (const QString & path) const {
return QFileInfo (path).isSymLink ();
}
bool QQmlFileSystemSingleton::hasParent (const QString & path) const {
if (QDir::rootPath () == "/") {
return (path.length () > 1);
}
else {
return (path.length () > 3);
}
}
bool QQmlFileSystemSingleton::exists (const QString & path) const {
return QFile::exists (path);
}
bool QQmlFileSystemSingleton::copy (const QString & sourcePath, const QString & destPath) const {
return QFile::copy (sourcePath, destPath);
}
bool QQmlFileSystemSingleton::move (const QString & sourcePath, const QString & destPath) const {
return QFile::rename (sourcePath, destPath);
}
bool QQmlFileSystemSingleton::link (const QString & sourcePath, const QString & destPath) const {
return QFile::link (sourcePath, destPath);
}
bool QQmlFileSystemSingleton::remove (const QString & path) const {
if (QFileInfo (path).isDir ()) {
return QDir (path).removeRecursively ();
}
else {
return QFile::remove (path);
}
}
bool QQmlFileSystemSingleton::mkpath (const QString & path) const {
return QDir ().mkpath (path);
}
int QQmlFileSystemSingleton::size (const QString & path) const {
return static_cast<int> (QFileInfo (path).size ());
}
QString QQmlFileSystemSingleton::parentDir (const QString & path) const {
return QFileInfo (path).dir ().absolutePath ();
}
QString QQmlFileSystemSingleton::baseName (const QString & path) const {
return QFileInfo (path).fileName ();
}
QString QQmlFileSystemSingleton::readTextFile (const QString & path) const {
QString ret;
QFile file (path);
if (file.open (QFile::ReadOnly)) {
ret = QString::fromUtf8 (file.readAll ());
file.close ();
}
return ret;
}
bool QQmlFileSystemSingleton::writeTextFile (const QString & path, const QString & text) const {
bool ret = false;
QFile file (path);
if (file.open (QFile::WriteOnly)) {
file.write (text.toUtf8 ());
file.flush ();
file.close ();
ret = true;
}
return ret;
}
QString QQmlFileSystemSingleton::pathFromUrl (const QUrl & url) const {
return url.toLocalFile ();
}
QUrl QQmlFileSystemSingleton::urlFromPath (const QString & path) const {
return QUrl::fromLocalFile (path);
}
static void getDirsList (const QString & dirPath,
const bool includeHidden,
const bool recursive,
QFileInfoList & dirsList) {
static const QStringList EMPTY_STR_LIST;
QDirIterator lister (dirPath,
EMPTY_STR_LIST,
QDir::Filters (QDir::Dirs | QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Hidden),
QDirIterator::NoIteratorFlags);
while (lister.hasNext ()) {
const QFileInfo & info = lister.next ();
if (includeHidden || (!info.isHidden () && !info.fileName ().startsWith ('.'))) {
dirsList.append (info);
if (recursive) {
getDirsList (info.absoluteFilePath (), includeHidden, true, dirsList);
}
}
}
}
static void getFilesList (const QString & dirPath,
const QStringList & nameFilters,
const bool includeHidden,
QFileInfoList & filesList) {
QDirIterator lister (dirPath,
nameFilters,
QDir::Filters (QDir::Files | QDir::Hidden),
QDirIterator::NoIteratorFlags);
while (lister.hasNext ()) {
const QFileInfo & info = lister.next ();
if (includeHidden || (!info.isHidden () && !info.fileName ().startsWith ('.'))) {
filesList.append (info);
}
}
}
QVariantList QQmlFileSystemSingleton::list (const QString & dirPath, const QStringList & nameFilters, const bool showHidden, const bool showFiles, const bool showDirs, const bool recursive) const {
QVariantList ret;
const QDir dir (dirPath);
if (!dirPath.isEmpty () && dir.exists ()) {
QFileInfoList dirsList;
QFileInfoList filesList;
if (recursive) {
getDirsList (dirPath, showHidden, recursive, dirsList);
if (showFiles) {
getFilesList (dirPath, nameFilters, showHidden, filesList);
for (QFileInfoList::const_iterator it = dirsList.constBegin (); it != dirsList.constEnd (); ++it) {
getFilesList ((* it).absoluteFilePath (), nameFilters, showHidden, filesList);
}
}
if (!showDirs) {
dirsList.clear ();
}
}
else {
if (showDirs) {
getDirsList (dirPath, showHidden, false, dirsList);
}
if (showFiles) {
getFilesList (dirPath, nameFilters, showHidden, filesList);
}
}
qSort (dirsList);
qSort (filesList);
ret.reserve (dirsList.size () + filesList.size ());
for (QFileInfoList::const_iterator it = dirsList.constBegin (); it != dirsList.constEnd (); ++it) {
ret.append (QVariant::fromValue (new QQmlFileSystemModelEntry (* it)));
}
for (QFileInfoList::const_iterator it = filesList.constBegin (); it != filesList.constEnd (); ++it) {
ret.append (QVariant::fromValue (new QQmlFileSystemModelEntry (* it)));
}
}
return ret;
}
QQmlFileSystemModelEntry::QQmlFileSystemModelEntry (const QFileInfo & info, QObject * parent)
: QObject (parent)
, m_url (QUrl::fromLocalFile (info.absoluteFilePath ()).toString ())
, m_name (info.fileName ())
, m_path (info.absoluteFilePath ())
#ifndef Q_OS_LINUX
, m_mimeType (MIME_DATABASE.mimeTypeForFile (info.absoluteFilePath ()).name ())
#else
, m_mimeType ("undefined")
#endif
, m_extension (info.completeSuffix ())
, m_lastModified (info.lastModified ().toString (DATETIME_FORMAT))
, m_isDir (info.isDir ())
, m_isFile (info.isFile ())
, m_isLink (info.isSymLink ())
, m_size (static_cast<int> (info.size ()))
, m_permissions (static_cast<int> (info.permissions ()))
{
if (parent == Q_NULLPTR) {
QQmlEngine::setObjectOwnership (this, QQmlEngine::JavaScriptOwnership);
}
}
const QString & QQmlFileSystemModelEntry::getUrl (void) const {
return m_url;
}
const QString & QQmlFileSystemModelEntry::getName (void) const {
return m_name;
}
const QString & QQmlFileSystemModelEntry::getPath (void) const {
return m_path;
}
const QString & QQmlFileSystemModelEntry::getMimeType (void) const {
return m_mimeType;
}
const QString & QQmlFileSystemModelEntry::getExtension (void) const {
return m_extension;
}
const QString & QQmlFileSystemModelEntry::getLastModified (void) const {
return m_lastModified;
}
bool QQmlFileSystemModelEntry::getIsDir (void) const {
return m_isDir;
}
bool QQmlFileSystemModelEntry::getIsFile (void) const {
return m_isFile;
}
bool QQmlFileSystemModelEntry::getIsLink (void) const {
return m_isLink;
}
int QQmlFileSystemModelEntry::getSize (void) const {
return m_size;
}
int QQmlFileSystemModelEntry::getPermission (void) const {
return m_permissions;
}
#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