aboutsummaryrefslogtreecommitdiffstats
path: root/src/virtualkeyboard/declarativesettings.cpp
blob: 49418861f05231ab2f546e8105ab09370d872437 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/******************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd
** All rights reserved.
** For any questions to The Qt Company, please use contact form at http://qt.io
**
** This file is part of the Qt Virtual Keyboard module.
**
** Licensees holding valid commercial license for Qt may use this file in
** accordance with the Qt License Agreement provided with the Software
** or, alternatively, in accordance with the terms contained in a written
** agreement between you and The Qt Company.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.io
**
******************************************************************************/

#include "declarativesettings.h"
#include "settings.h"
#include "virtualkeyboarddebug.h"
#include <QQmlEngine>
#include <QFileInfo>
#include <QDir>
#include <QtCore/private/qobject_p.h>

class DeclarativeSettingsPrivate : public QObjectPrivate
{
public:
    DeclarativeSettingsPrivate() :
        QObjectPrivate(),
        engine() {}

    QString buildStyleImportPath(const QString &path, const QString &name) const
    {
        QString importPath(path + name + "/style.qml");
        if (!importPath.startsWith("qrc:")) {
            QUrl url = QUrl::fromLocalFile(importPath);
            importPath = url.toString();
        }
        return importPath;
    }

    QString buildStyleFilePath(const QString &path, const QString &name) const
    {
        QString filePath(path);
        if (filePath.startsWith("qrc:"))
            filePath.remove(0, 3);
        return filePath + name + "/style.qml";
    }

    QString styleImportPath(const QString &name) const
    {
        QStringList styleImportPathList;
        styleImportPathList << "qrc:/content/styles/";
        QStringList importPathList = engine->importPathList();
        // Add QML import path (Note: the QML base dir is always the last entry in the list)
        if (!importPathList.isEmpty())
            styleImportPathList << importPathList.last() + "/QtQuick/Enterprise/VirtualKeyboard/Styles/";
        foreach (const QString &styleImportPath, styleImportPathList) {
            QString filePath = buildStyleFilePath(styleImportPath, name);
            bool pathExist = false;
#ifdef COMPILING_QML
            // qtquickcompiler removes *.qml file paths from qrc file, but keeps directories - QTRD-3268
            pathExist = QFileInfo(filePath).dir().exists();
#else
            pathExist = QFileInfo(filePath).exists();
#endif
            if (pathExist)
                return buildStyleImportPath(styleImportPath, name);
        }
        return QString();
    }

    QPointer<QQmlEngine> engine;
};

/*!
    \qmlmodule QtQuick.Enterprise.VirtualKeyboard.Settings 1.2

    This module provides settings components for Qt Virtual Keyboard.
*/

/*!
    \qmltype VirtualKeyboardSettings
    \inqmlmodule QtQuick.Enterprise.VirtualKeyboard.Settings
    \ingroup qtvirtualkeyboard-settings-qml
    \since QtQuick.Enterprise.VirtualKeyboard 1.2
    \internal
    \brief Provides settings for virtual keyboard.

    This module provides VirtualKeyboarSettings instance,
    which can be used for configuring virtual keyboard settings.

    Note that settings have only effect during current application
    lifetime, i.e. the configuration changes are not persistent.

    For example, to change the keyboard style in application:

    \code
        Component.onCompleted: VirtualKeyboardSettings.styleName = "retro"
    \endcode
*/

/*!
    \internal
*/
QObject *DeclarativeSettings::registerSettingsModule(QQmlEngine *engine, QJSEngine *jsEngine)
{
    Q_UNUSED(jsEngine);
    return new DeclarativeSettings(engine);
}

/*!
    \internal
*/
DeclarativeSettings::DeclarativeSettings(QQmlEngine *engine) :
    QObject(*new DeclarativeSettingsPrivate())
{
    Q_D(DeclarativeSettings);
    d->engine = engine;
    Settings *settings = Settings::instance();
    if (settings->styleName().isEmpty())
        resetStyle();
    connect(settings, SIGNAL(styleChanged()), SIGNAL(styleChanged()));
    connect(settings, SIGNAL(styleNameChanged()), SIGNAL(styleNameChanged()));
}

/*!
    \internal
*/
QString DeclarativeSettings::style() const
{
    return Settings::instance()->style();
}

/*!
    \internal
*/
QString DeclarativeSettings::styleName() const
{
    return Settings::instance()->styleName();
}

/*!
    \internal
*/
void DeclarativeSettings::setStyleName(const QString &styleName)
{
    Q_D(DeclarativeSettings);
    Settings *settings = Settings::instance();
    QString style = d->styleImportPath(styleName);
    if (style.isEmpty()) {
        qWarning() << "WARNING: Cannot find style" << styleName << "- fallback:" << settings->styleName();
        return;
    }
    settings->setStyleName(styleName);
    settings->setStyle(style);
}

void DeclarativeSettings::resetStyle()
{
    Q_D(DeclarativeSettings);
    Settings *settings = Settings::instance();
    QString styleName = QT_VIRTUALKEYBOARD_DEFAULT_STYLE;
    QString style = d->styleImportPath(styleName);
    QString customStyleName = qgetenv("QT_VIRTUALKEYBOARD_STYLE");
    if (!customStyleName.isEmpty()) {
        bool found = false;
        QRegExp styleNameValidator("\\w+");
        if (styleNameValidator.exactMatch(customStyleName)) {
            QString customStyle = d->styleImportPath(customStyleName);
            if (!customStyle.isEmpty()) {
                styleName = customStyleName;
                style = customStyle;
                found = true;
            }
        }
        if (!found) {
            qWarning() << "WARNING: Cannot find style" << customStyleName << "- fallback:" << styleName;
        }
    }
    if (!style.isEmpty()) {
        settings->setStyleName(styleName);
        settings->setStyle(style);
    }
}

/*!
    \qmlproperty string VirtualKeyboardSettings::style
    \internal
*/

/*!
    \qmlproperty string VirtualKeyboardSettings::styleName

    This property provides the current style. Application can change
    the keyboard style by setting the styleName to different value.

    The system wide keyboard style can be affected by setting
    the QT_VIRTUALKEYBOARD_STYLE environment variable.
*/