~zsombi/ubuntu-ui-toolkit/03-styleditem-styleset

« back to all changes in this revision

Viewing changes to modules/Ubuntu/Components/plugin/uctheme.cpp

  • Committer: Zsombor Egri
  • Date: 2015-03-12 20:52:56 UTC
  • mfrom: (1434.1.17 02-styleset)
  • Revision ID: zsombor.egri@canonical.com-20150312205256-j40z8x4ahlju4rtn
prereq sync, StyleSet renamed to theme

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *          Florian Boucault <florian.boucault@canonical.com>
18
18
 */
19
19
 
20
 
#include "ucstyleset.h"
 
20
#include "uctheme.h"
21
21
#include "listener.h"
22
22
#include "quickutils.h"
23
23
#include "i18n.h"
37
37
#include <QtGui/QFont>
38
38
 
39
39
/*!
40
 
    \qmltype StyleSet
41
 
    \instantiates UCStyleSet
 
40
    \qmltype ThemeSettings
 
41
    \instantiates UCTheme
42
42
    \inqmlmodule Ubuntu.Components 1.3
43
43
    \since Ubuntu.Components 1.3
44
44
    \ingroup theming
45
 
    \brief The StyleSet class provides facilities to define the styleset of a
 
45
    \brief The ThemeSettings class provides facilities to define the theme of a
46
46
    StyledItem.
47
47
 
48
 
    A global instance, which is the default styleset, is exposed as the \b styleSet
49
 
    context property.
 
48
    A global instance is exposed as the \b theme context property.
50
49
 
51
 
    The styleset or theme defines the visual aspect of the Ubuntu components. An
52
 
    application can use one or more styleset the same time. The StyleSet component
53
 
    provides abilities to change thye styleset used by the component and all its
 
50
    The theme or theme defines the visual aspect of the Ubuntu components. An
 
51
    application can use one or more theme the same time. The ThemeSettings component
 
52
    provides abilities to change thye theme used by the component and all its
54
53
    child components.
55
54
 
56
 
    Changing the styleset of the entire application can be achieved by changing
57
 
    the name of the root StyledItem's, i.e. MainView's current styleset.
 
55
    Changing the theme of the entire application can be achieved by changing
 
56
    the name of the root StyledItem's, i.e. MainView's current theme.
58
57
 
59
58
    \qml
60
59
    import QtQuick 2.4
64
63
        width: units.gu(40)
65
64
        height: units.gu(71)
66
65
 
67
 
        styleSet.name: "Ubuntu.Components.Themes.Ambiance"
 
66
        theme.name: "Ubuntu.Components.Themes.Ambiance"
68
67
    }
69
68
    \endqml
70
69
    \note Changing the style set name in this way will result in a change of the
71
70
    inherited style set. In case a different style set is desired, a new instance
72
 
    of the StyleSet must be created.
 
71
    of the ThemeSettings must be created.
73
72
 
74
73
    The \l createStyleComponent function can be used to create the style for a
75
74
    component. The following example will create the style with the inherited
79
78
    import Ubuntu.Components 1.3
80
79
    StyledItem {
81
80
        id: myItem
82
 
        style: styleSet.createStyleComponent("MyItemStyle.qml", myItem)
 
81
        style: theme.createStyleComponent("MyItemStyle.qml", myItem)
83
82
    }
84
83
    \endqml
85
84
 
156
155
    return QUrl();
157
156
}
158
157
 
159
 
UCStyleSet::UCStyleSet(QObject *parent)
 
158
UCTheme::UCTheme(QObject *parent)
160
159
    : QObject(parent)
161
 
    , m_palette(UCStyleSet::defaultSet().m_palette)
162
 
    , m_engine(UCStyleSet::defaultSet().m_engine)
 
160
    , m_palette(UCTheme::defaultTheme().m_palette)
 
161
    , m_engine(UCTheme::defaultTheme().m_engine)
163
162
    , m_defaultStyle(false)
164
163
{
165
164
    init();
166
165
}
167
166
 
168
 
UCStyleSet::UCStyleSet(bool defaultStyle)
169
 
    : QObject(0)
 
167
UCTheme::UCTheme(bool defaultStyle, QObject *parent)
 
168
    : QObject(parent)
170
169
    , m_palette(NULL)
171
170
    , m_engine(NULL)
172
171
    , m_defaultStyle(defaultStyle)
180
179
    QGuiApplication::setFont(defaultFont);
181
180
}
182
181
 
183
 
void UCStyleSet::init()
 
182
void UCTheme::init()
184
183
{
185
184
    m_completed = false;
186
185
    QObject::connect(&m_themeSettings, &UCThemeSettings::themeNameChanged,
187
 
                     this, &UCStyleSet::onThemeNameChanged);
 
186
                     this, &UCTheme::onThemeNameChanged);
188
187
    updateThemePaths();
189
188
}
190
189
 
191
 
void UCStyleSet::classBegin()
 
190
void UCTheme::classBegin()
192
191
{
193
192
    m_engine = qmlEngine(this);
194
193
    updateEnginePaths();
195
194
}
196
195
 
197
 
void UCStyleSet::updateEnginePaths()
 
196
void UCTheme::updateEnginePaths()
198
197
{
199
198
    if (!m_engine) {
200
199
        return;
209
208
}
210
209
 
211
210
// slot called when the styleset uses the system theme
212
 
void UCStyleSet::onThemeNameChanged()
 
211
void UCTheme::onThemeNameChanged()
213
212
{
214
213
    updateThemePaths();
215
214
    Q_EMIT nameChanged();
216
215
}
217
216
 
218
 
void UCStyleSet::updateThemePaths()
 
217
void UCTheme::updateThemePaths()
219
218
{
220
219
    m_themePaths.clear();
221
220
 
230
229
}
231
230
 
232
231
/*!
233
 
 * \qmlproperty StyleSet StyleSet::parent
234
 
 * The property specifies the parent StyleSet. The property only has a valid value
235
 
 * when assigned to \l StyledItem::styleSet property.
 
232
 * \qmlproperty ThemeSettings ThemeSettings::parentTheme
 
233
 * The property specifies the parent ThemeSettings instance. The property only
 
234
 * has a valid value when assigned to \l StyledItem::theme property.
236
235
 */
237
 
UCStyleSet *UCStyleSet::parentSet()
 
236
UCTheme *UCTheme::parentTheme()
238
237
{
239
238
    UCStyledItemBase *owner = qobject_cast<UCStyledItemBase*>(parent());
240
239
    UCStyledItemBasePrivate *pOwner = owner ? UCStyledItemBasePrivate::get(owner) : NULL;
241
 
    if (pOwner && pOwner->styleSet == this && pOwner->parentStyledItem) {
242
 
        return UCStyledItemBasePrivate::get(pOwner->parentStyledItem)->getStyleSet();
 
240
    if (pOwner && pOwner->theme == this && pOwner->parentStyledItem) {
 
241
        return UCStyledItemBasePrivate::get(pOwner->parentStyledItem)->getTheme();
243
242
    }
244
243
    return NULL;
245
244
}
246
245
 
247
246
/*!
248
 
    \qmlproperty string StyleSet::name
 
247
    \qmlproperty string ThemeSettings::name
249
248
 
250
249
    The name of the current theme. The name can be set only at creation time, runtime
251
250
    changes will be omitted.
255
254
    import Ubuntu.Componenst 1.3
256
255
 
257
256
    StyledItem {
258
 
        style: StyleSet {
 
257
        style: ThemeSettings {
259
258
            // this is right
260
259
            name: "Ubuntu.Components.Themes.Ambiance"
261
260
        }
262
261
        // this is not allowed, and will be omitted
263
 
        Components.onCompleted: styleSet.name = "Ubuntu.Components.Themes.SuruDark"
 
262
        Components.onCompleted: theme.name = "Ubuntu.Components.Themes.SuruDark"
264
263
    }
265
264
    \endqml
266
265
*/
267
 
QString UCStyleSet::name() const
 
266
QString UCTheme::name() const
268
267
{
269
268
    return !m_name.isEmpty() ? m_name : m_themeSettings.themeName();
270
269
}
271
 
void UCStyleSet::setName(const QString& name)
 
270
void UCTheme::setName(const QString& name)
272
271
{
273
272
    if (name == m_name) {
274
273
        return;
278
277
        init();
279
278
    } else {
280
279
        QObject::disconnect(&m_themeSettings, &UCThemeSettings::themeNameChanged,
281
 
                            this, &UCStyleSet::onThemeNameChanged);
 
280
                            this, &UCTheme::onThemeNameChanged);
282
281
        updateThemePaths();
283
282
    }
284
283
    updateEnginePaths();
285
284
    loadPalette();
286
285
    Q_EMIT nameChanged();
287
286
}
288
 
void UCStyleSet::resetName()
 
287
void UCTheme::resetName()
289
288
{
290
289
    setName(QString());
291
290
}
292
291
 
293
292
/*!
294
 
    \qmlproperty Palette StyleSet::palette
 
293
    \qmlproperty Palette ThemeSettings::palette
295
294
 
296
295
    The palette of the current theme.
297
296
*/
298
 
QObject* UCStyleSet::palette()
 
297
QObject* UCTheme::palette()
299
298
{
300
299
    if (!m_palette) {
301
300
        loadPalette(false);
303
302
    return m_palette;
304
303
}
305
304
 
306
 
QUrl UCStyleSet::styleUrl(const QString& styleName)
 
305
QUrl UCTheme::styleUrl(const QString& styleName)
307
306
{
308
307
    Q_FOREACH (const QUrl& themePath, m_themePaths) {
309
308
        QUrl styleUrl = themePath.resolved(styleName);
315
314
    return QUrl();
316
315
}
317
316
 
318
 
QString UCStyleSet::parentThemeName(const QString& themeName)
 
317
QString UCTheme::parentThemeName(const QString& themeName)
319
318
{
320
319
    QString parentTheme;
321
320
    QUrl themePath = pathFromThemeName(themeName);
331
330
    return parentTheme;
332
331
}
333
332
 
334
 
// registers the default styleSet property to the root context
335
 
void UCStyleSet::registerToContext(QQmlContext* context)
 
333
// registers the default theme property to the root context
 
334
void UCTheme::registerToContext(QQmlContext* context)
336
335
{
337
 
    UCStyleSet *defaultSet = &UCStyleSet::defaultSet();
338
 
    defaultSet->m_engine = context->engine();
339
 
    defaultSet->updateEnginePaths();
 
336
    UCTheme *defaultTheme = &UCTheme::defaultTheme();
 
337
    defaultTheme->m_engine = context->engine();
 
338
    defaultTheme->updateEnginePaths();
340
339
 
341
 
    context->setContextProperty("styleSet", defaultSet);
 
340
    context->setContextProperty("theme", defaultTheme);
342
341
    ContextPropertyChangeListener *listener =
343
 
        new ContextPropertyChangeListener(context, "styleSet");
344
 
    QObject::connect(defaultSet, &UCStyleSet::nameChanged,
 
342
        new ContextPropertyChangeListener(context, "theme");
 
343
    QObject::connect(defaultTheme, &UCTheme::nameChanged,
345
344
                     listener, &ContextPropertyChangeListener::updateContextProperty);
346
345
}
347
346
 
348
347
/*!
349
 
    \qmlmethod Component StyleSet::createStyleComponent(string styleName, object parent)
 
348
    \qmlmethod Component ThemeSettings::createStyleComponent(string styleName, object parent)
350
349
 
351
350
    Returns an instance of the style component named \a styleName and parented
352
351
    to \a parent.
353
352
*/
354
 
QQmlComponent* UCStyleSet::createStyleComponent(const QString& styleName, QObject* parent)
 
353
QQmlComponent* UCTheme::createStyleComponent(const QString& styleName, QObject* parent)
355
354
{
356
355
    QQmlComponent *component = NULL;
357
356
 
381
380
    return component;
382
381
}
383
382
 
384
 
void UCStyleSet::loadPalette(bool notify)
 
383
void UCTheme::loadPalette(bool notify)
385
384
{
386
385
    if (!m_engine) {
387
386
        return;
398
397
        }
399
398
    } else {
400
399
        // use the default palette if none defined
401
 
        m_palette = defaultSet().m_palette;
 
400
        m_palette = defaultTheme().m_palette;
402
401
    }
403
402
}