From 3208d08f6210c68ad8c48f2e3515ac1a7672a48b Mon Sep 17 00:00:00 2001 From: Liang Qi Date: Thu, 26 Nov 2015 14:00:17 +0100 Subject: Rename QQuickFileSelector to QQuickStyleSelector Also moved to QtLabsControls lib. Change-Id: I8ed1256da2b9aa63db4ca3b51a0ace3ba730e542 Reviewed-by: J-P Nurmi --- src/controls/controls.pri | 3 + src/controls/qquickstyleselector.cpp | 235 +++++++++++++++++++++++++++++++++ src/controls/qquickstyleselector_p.h | 80 +++++++++++ src/controls/qquickstyleselector_p_p.h | 80 +++++++++++ 4 files changed, 398 insertions(+) create mode 100644 src/controls/qquickstyleselector.cpp create mode 100644 src/controls/qquickstyleselector_p.h create mode 100644 src/controls/qquickstyleselector_p_p.h (limited to 'src/controls') diff --git a/src/controls/controls.pri b/src/controls/controls.pri index ae85b4a7..63cba222 100644 --- a/src/controls/controls.pri +++ b/src/controls/controls.pri @@ -1,5 +1,8 @@ HEADERS += \ + $$PWD/qquickstyleselector_p.h \ + $$PWD/qquickstyleselector_p_p.h \ $$PWD/qquickpaddedrectangle_p.h SOURCES += \ + $$PWD/qquickstyleselector.cpp \ $$PWD/qquickpaddedrectangle.cpp diff --git a/src/controls/qquickstyleselector.cpp b/src/controls/qquickstyleselector.cpp new file mode 100644 index 00000000..2cf297a9 --- /dev/null +++ b/src/controls/qquickstyleselector.cpp @@ -0,0 +1,235 @@ +/*************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Copyright (C) 2013 BlackBerry Limited. All rights reserved. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Labs Controls module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickstyleselector_p.h" +#include "qquickstyleselector_p_p.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +//Environment variable to allow tooling full control of file selectors +static const char env_override[] = "QT_LABS_CONTROLS_NO_STYLE"; + +Q_GLOBAL_STATIC(QQuickStyleSelectorSharedData, sharedData); +static QBasicMutex sharedDataMutex; + +QQuickStyleSelectorPrivate::QQuickStyleSelectorPrivate() + : QObjectPrivate() +{ +} + +QQuickStyleSelector::QQuickStyleSelector(QObject *parent) + : QObject(*(new QQuickStyleSelectorPrivate()), parent) +{ +} + +QQuickStyleSelector::~QQuickStyleSelector() +{ +} + +QString QQuickStyleSelector::select(const QString &filePath) const +{ + Q_D(const QQuickStyleSelector); + return select(QUrl(d->baseUrl.toString() + filePath)).toString(); +} + +static bool isLocalScheme(const QString &file) +{ + bool local = file == QStringLiteral("qrc"); +#ifdef Q_OS_ANDROID + local |= file == QStringLiteral("assets"); +#endif + return local; +} + +QUrl QQuickStyleSelector::select(const QUrl &filePath) const +{ + Q_D(const QQuickStyleSelector); + if (!isLocalScheme(filePath.scheme()) && !filePath.isLocalFile()) + return filePath; + QUrl ret(filePath); + if (isLocalScheme(filePath.scheme())) { + QString equivalentPath = QLatin1Char(':') + filePath.path(); + QString selectedPath = d->select(equivalentPath); + ret.setPath(selectedPath.remove(0, 1)); + } else { + ret = QUrl::fromLocalFile(d->select(ret.toLocalFile())); + } + return ret; +} + +static QString selectionHelper(const QString &path, const QString &fileName, const QStringList &selectors) +{ + /* selectionHelper does a depth-first search of possible selected files. Because there is strict + selector ordering in the API, we can stop checking as soon as we find the file in a directory + which does not contain any other valid selector directories. + */ + Q_ASSERT(path.isEmpty() || path.endsWith(QLatin1Char('/'))); + + foreach (const QString &s, selectors) { + QString prospectiveBase = path + s + QLatin1Char('/'); + QStringList remainingSelectors = selectors; + remainingSelectors.removeAll(s); + if (!QDir(prospectiveBase).exists()) + continue; + QString prospectiveFile = selectionHelper(prospectiveBase, fileName, remainingSelectors); + if (!prospectiveFile.isEmpty()) + return prospectiveFile; + } + + // If we reach here there were no successful files found at a lower level in this branch, so we + // should check this level as a potential result. + if (!QFile::exists(path + fileName)) + return QString(); + return path + fileName; +} + +QString QQuickStyleSelectorPrivate::select(const QString &filePath) const +{ + Q_Q(const QQuickStyleSelector); + QFileInfo fi(filePath); + // If file doesn't exist, don't select + if (!fi.exists()) + return filePath; + + QString ret = selectionHelper(fi.path().isEmpty() ? QString() : fi.path() + QLatin1Char('/'), + fi.fileName(), q->allSelectors()); + + if (!ret.isEmpty()) + return ret; + return filePath; +} + +QString QQuickStyleSelector::style() const +{ + Q_D(const QQuickStyleSelector); + return d->style; +} + +void QQuickStyleSelector::setStyle(const QString &s) +{ + Q_D(QQuickStyleSelector); + d->style = s; +} + +QStringList QQuickStyleSelector::allSelectors() const +{ + Q_D(const QQuickStyleSelector); + QMutexLocker locker(&sharedDataMutex); + QQuickStyleSelectorPrivate::updateSelectors(); + return QStringList(d->style) + sharedData->staticSelectors; +} + +void QQuickStyleSelector::setBaseUrl(const QUrl &base) +{ + Q_D(QQuickStyleSelector); + if (d->baseUrl != base) + d->baseUrl = base; +} + +QUrl QQuickStyleSelector::baseUrl() const +{ + Q_D(const QQuickStyleSelector); + return d->baseUrl; +} + +void QQuickStyleSelectorPrivate::updateSelectors() +{ + if (!sharedData->staticSelectors.isEmpty()) + return; //Already loaded + + QLatin1Char pathSep(','); + QStringList envSelectors = QString::fromLatin1(qgetenv("QT_LABS_CONTROLS_STYLE")) + .split(pathSep, QString::SkipEmptyParts); + if (envSelectors.count()) + sharedData->staticSelectors << envSelectors; + + if (!qEnvironmentVariableIsEmpty(env_override)) + return; + + sharedData->staticSelectors << sharedData->preloadedStatics; //Potential for static selectors from other modules + + // TODO: Update on locale changed? + sharedData->staticSelectors << QLocale().name(); + + sharedData->staticSelectors << platformSelectors(); +} + +QStringList QQuickStyleSelectorPrivate::platformSelectors() +{ + // similar, but not identical to QSysInfo::osType + QStringList ret; +#if defined(Q_OS_WIN) + // can't fall back to QSysInfo because we need both "winphone" and "winrt" for the Windows Phone case + ret << QStringLiteral("windows"); + ret << QSysInfo::kernelType(); // "wince" and "winnt" +# if defined(Q_OS_WINRT) + ret << QStringLiteral("winrt"); +# if defined(Q_OS_WINPHONE) + ret << QStringLiteral("winphone"); +# endif +# endif +#elif defined(Q_OS_UNIX) + ret << QStringLiteral("unix"); +# if !defined(Q_OS_ANDROID) && !defined(Q_OS_BLACKBERRY) + // we don't want "linux" for Android or "qnx" for Blackberry here + ret << QSysInfo::kernelType(); +# ifdef Q_OS_MAC + ret << QStringLiteral("mac"); // compatibility, since kernelType() is "darwin" +# endif +# endif + QString productName = QSysInfo::productType(); + if (productName != QLatin1String("unknown")) + ret << productName; // "opensuse", "fedora", "osx", "ios", "blackberry", "android" +#endif + return ret; +} + +void QQuickStyleSelectorPrivate::addStatics(const QStringList &statics) +{ + QMutexLocker locker(&sharedDataMutex); + sharedData->preloadedStatics << statics; +} + +QT_END_NAMESPACE + +#include "moc_qquickstyleselector_p.cpp" diff --git a/src/controls/qquickstyleselector_p.h b/src/controls/qquickstyleselector_p.h new file mode 100644 index 00000000..89cfed71 --- /dev/null +++ b/src/controls/qquickstyleselector_p.h @@ -0,0 +1,80 @@ +/*************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Copyright (C) 2013 BlackBerry Limited. All rights reserved. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Labs Controls module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKSTYLESELECTOR_P_H +#define QQUICKSTYLESELECTOR_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include + +QT_BEGIN_NAMESPACE + +class QQuickStyleSelectorPrivate; +class QQuickStyleSelector : public QObject +{ + Q_OBJECT +public: + explicit QQuickStyleSelector(QObject *parent = Q_NULLPTR); + ~QQuickStyleSelector(); + + QString select(const QString &filePath) const; + + QString style() const; + void setStyle(const QString &s); + + QStringList allSelectors() const; + + void setBaseUrl(const QUrl &base); + QUrl baseUrl() const; + +private: + QUrl select(const QUrl &filePath) const; + + Q_DECLARE_PRIVATE(QQuickStyleSelector) +}; + +QT_END_NAMESPACE + +#endif // QQUICKSTYLESELECTOR_P_H diff --git a/src/controls/qquickstyleselector_p_p.h b/src/controls/qquickstyleselector_p_p.h new file mode 100644 index 00000000..bf11fd1c --- /dev/null +++ b/src/controls/qquickstyleselector_p_p.h @@ -0,0 +1,80 @@ +/*************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Copyright (C) 2013 BlackBerry Limited. All rights reserved. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Labs Controls module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKSTYLESELECTOR_P_P_H +#define QQUICKSTYLESELECTOR_P_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include +#include + +#include "qquickstyleselector_p.h" + +QT_BEGIN_NAMESPACE + +struct QQuickStyleSelectorSharedData //Not QSharedData because currently is just a global store +{ + QStringList staticSelectors; + QStringList preloadedStatics; +}; + +class QQuickStyleSelectorPrivate : QObjectPrivate +{ + Q_DECLARE_PUBLIC(QQuickStyleSelector) +public: + static void updateSelectors(); + static QStringList platformSelectors(); + static void addStatics(const QStringList &); //For loading GUI statics from other Qt modules + QQuickStyleSelectorPrivate(); + QString select(const QString &filePath) const; + + QString style; + QUrl baseUrl; +}; + +QT_END_NAMESPACE + +#endif // QQUICKSTYLESELECTOR_P_P_H + -- cgit v1.2.3