2
* Copyright 2015 Canonical Ltd.
2
* Copyright 2016 Canonical Ltd.
4
4
* This program is free software; you can redistribute it and/or modify
5
5
* it under the terms of the GNU Lesser General Public License as published by
346
346
UCTheme::UCTheme(QObject *parent)
347
347
: QObject(parent)
348
, m_palette(UCTheme::defaultTheme().m_palette)
349
, m_engine(UCTheme::defaultTheme().m_engine)
350
, m_defaultStyle(false)
355
UCTheme::UCTheme(bool defaultStyle, QObject *parent)
359
, m_defaultStyle(defaultStyle)
348
, m_parentTheme(Q_NULLPTR)
349
, m_palette(Q_NULLPTR)
355
UCTheme *UCTheme::defaultTheme(QQmlEngine *engine)
357
if (!engine || !engine->rootContext()) {
360
UCTheme *theme = Q_NULLPTR;
361
for (int tryCount = 0; !theme && tryCount < 2; tryCount++) {
362
theme = engine->rootContext()->contextProperty(CONTEXT_THEME).value<UCTheme*>();
364
createDefaultTheme(engine);
370
void UCTheme::setupDefault()
372
// FIXME: move this into QPA
362
373
// set the default font
363
374
QFont defaultFont = QGuiApplication::font();
364
375
defaultFont.setFamily("Ubuntu");
379
390
void UCTheme::classBegin()
381
m_engine = qmlEngine(this);
392
QQmlEngine *engine = qmlEngine(this);
393
updateEnginePaths(engine);
394
m_palette = UCTheme::defaultTheme(engine)->m_palette;
386
void UCTheme::updateEnginePaths()
400
void UCTheme::updateEnginePaths(QQmlEngine *engine)
392
406
QStringList paths = themeSearchPath();
393
407
Q_FOREACH(const QString &path, paths) {
394
if (QDir(path).exists() && !m_engine->importPathList().contains(path)) {
395
m_engine->addImportPath(path);
408
if (QDir(path).exists() && !engine->importPathList().contains(path)) {
409
engine->addImportPath(path);
613
627
// registers the default theme property to the root context
614
void UCTheme::registerToContext(QQmlContext* context)
628
void UCTheme::createDefaultTheme(QQmlEngine* engine)
616
UCTheme *defaultTheme = &UCTheme::defaultTheme();
617
defaultTheme->m_engine = context->engine();
618
defaultTheme->updateEnginePaths();
620
context->setContextProperty("theme", defaultTheme);
630
QQmlContext *context = engine->rootContext();
632
UCTheme *theme = new UCTheme(engine);
633
QQmlEngine::setContextForObject(theme, context);
634
context->setContextProperty(CONTEXT_THEME, theme);
636
theme->setupDefault();
637
theme->updateEnginePaths(engine);
621
639
ContextPropertyChangeListener *listener =
622
new ContextPropertyChangeListener(context, "theme");
623
QObject::connect(defaultTheme, &UCTheme::nameChanged,
640
new ContextPropertyChangeListener(context, CONTEXT_THEME);
641
QObject::connect(theme, &UCTheme::nameChanged,
624
642
listener, &ContextPropertyChangeListener::updateContextProperty);
675
693
if (parent != NULL) {
676
694
QQmlEngine* engine = qmlEngine(parent);
677
if (engine != m_engine && !m_engine) {
696
Q_ASSERT(engine == qmlEngine(this));
681
697
// make sure we have the paths
682
if (engine != NULL) {
683
bool fallback = false;
684
QUrl url = styleUrl(styleName, version, &fallback);
687
qmlInfo(parent) << QStringLiteral("Theme '%1' has no '%2' style for version %3.%4, fall back to version %5.%6.")
688
.arg(name()).arg(styleName).arg(MAJOR_VERSION(version)).arg(MINOR_VERSION(version))
689
.arg(MAJOR_VERSION(LATEST_UITK_VERSION)).arg(MINOR_VERSION(LATEST_UITK_VERSION));
691
component = new QQmlComponent(engine, url, QQmlComponent::PreferSynchronous, parent);
692
if (component->isError()) {
693
qmlInfo(parent) << component->errorString();
697
// set context for the component
698
QQmlEngine::setContextForObject(component, qmlContext(parent));
698
bool fallback = false;
699
QUrl url = styleUrl(styleName, version, &fallback);
702
qmlInfo(parent) << QStringLiteral("Theme '%1' has no '%2' style for version %3.%4, fall back to version %5.%6.")
703
.arg(name()).arg(styleName).arg(MAJOR_VERSION(version)).arg(MINOR_VERSION(version))
704
.arg(MAJOR_VERSION(LATEST_UITK_VERSION)).arg(MINOR_VERSION(LATEST_UITK_VERSION));
706
component = new QQmlComponent(engine, url, QQmlComponent::PreferSynchronous, parent);
707
if (component->isError()) {
708
qmlInfo(parent) << component->errorString();
702
UbuntuI18n::instance().tr(QString("Warning: Style %1 not found in theme %2").arg(styleName).arg(name()));
712
// set context for the component
713
QQmlEngine::setContextForObject(component, qmlContext(parent));
717
UbuntuI18n::instance().tr(QString("Warning: Style %1 not found in theme %2").arg(styleName).arg(name()));
707
721
return component;
710
void UCTheme::loadPalette(bool notify)
724
void UCTheme::loadPalette(QQmlEngine *engine, bool notify)
721
735
// theme may not have palette defined
722
736
QUrl paletteUrl = styleUrl("Palette.qml", previousVersion ? previousVersion : LATEST_UITK_VERSION);
723
737
if (paletteUrl.isValid()) {
724
m_palette = QuickUtils::instance().createQmlObject(paletteUrl, m_engine);
738
m_palette = QuickUtils::instance().createQmlObject(paletteUrl, engine);
726
740
m_palette->setParent(this);