Skip to content

Commit 5f2df51

Browse files
committed
started theme change feature, bug not working
1 parent 7bfef2b commit 5f2df51

File tree

8 files changed

+146
-1
lines changed

8 files changed

+146
-1
lines changed

assets/settings.qml

+26
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,33 @@ Sheet {
4444
topPadding: 40
4545
leftPadding: 10
4646

47+
Container {
48+
//Todo: fill me with QML
49+
DropDown {
50+
topPadding: 20
51+
id: themeDropDown
52+
title: qsTr("Theme Selection:") + Retranslate.onLocaleOrLanguageChanged
53+
54+
Option {
55+
text: qsTr("Bright") + Retranslate.onLocaleOrLanguageChanged
56+
value: VisualStyle.Bright
57+
}
58+
59+
Option {
60+
text: qsTr("Dark") + Retranslate.onLocaleOrLanguageChanged
61+
value: VisualStyle.Dark
62+
}
63+
onSelectedOptionChanged: {
64+
Settings.setSettings("theme", VisualStyle.Bright == themeDropDown.selectedValue ? "bright" : "dark");
65+
}
66+
}
67+
onCreationCompleted: {
68+
var theme = Settings.getSettings("theme", VisualStyle.Bright == themeDropDown.selectedValue ? "bright" : "dark");
69+
themeDropDown.setSelectedIndex("bright" == theme ? 0 : 1);
70+
}
71+
}
4772
}
73+
4874
}
4975

5076
}

bar-descriptor.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
<!-- Copyright information. Optional. -->
5252
<!-- <copyright></copyright> -->
5353

54-
<env var="CASCADES_THEME" value="dark"/>
54+
<!-- This is to set an unique theme whatever the device. -->
55+
<!-- <env var="CASCADES_THEME" value="dark"/> -->
5556

5657
<!-- Name of author which is used for signing. Must match the developer name of your development certificate. -->
5758
<configuration name="Device-Debug">

config.pri

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ device {
2121
SOURCES += $$quote($$BASEDIR/src/ActiveFrameUpdater.cpp) \
2222
$$quote($$BASEDIR/src/GetterRequest.cpp) \
2323
$$quote($$BASEDIR/src/QmlBeam.cpp) \
24+
$$quote($$BASEDIR/src/Settings.cpp) \
2425
$$quote($$BASEDIR/src/WifiInfo.cpp) \
2526
$$quote($$BASEDIR/src/applicationui.cpp) \
2627
$$quote($$BASEDIR/src/main.cpp) \
@@ -29,6 +30,7 @@ device {
2930
HEADERS += $$quote($$BASEDIR/src/ActiveFrameUpdater.h) \
3031
$$quote($$BASEDIR/src/GetterRequest.h) \
3132
$$quote($$BASEDIR/src/QmlBeam.h) \
33+
$$quote($$BASEDIR/src/Settings.h) \
3234
$$quote($$BASEDIR/src/WifiInfo.h) \
3335
$$quote($$BASEDIR/src/applicationui.hpp) \
3436
$$quote($$BASEDIR/src/timer.h)
@@ -53,6 +55,7 @@ device {
5355
SOURCES += $$quote($$BASEDIR/src/ActiveFrameUpdater.cpp) \
5456
$$quote($$BASEDIR/src/GetterRequest.cpp) \
5557
$$quote($$BASEDIR/src/QmlBeam.cpp) \
58+
$$quote($$BASEDIR/src/Settings.cpp) \
5659
$$quote($$BASEDIR/src/WifiInfo.cpp) \
5760
$$quote($$BASEDIR/src/applicationui.cpp) \
5861
$$quote($$BASEDIR/src/main.cpp) \
@@ -61,6 +64,7 @@ device {
6164
HEADERS += $$quote($$BASEDIR/src/ActiveFrameUpdater.h) \
6265
$$quote($$BASEDIR/src/GetterRequest.h) \
6366
$$quote($$BASEDIR/src/QmlBeam.h) \
67+
$$quote($$BASEDIR/src/Settings.h) \
6468
$$quote($$BASEDIR/src/WifiInfo.h) \
6569
$$quote($$BASEDIR/src/applicationui.hpp) \
6670
$$quote($$BASEDIR/src/timer.h)
@@ -87,6 +91,7 @@ simulator {
8791
SOURCES += $$quote($$BASEDIR/src/ActiveFrameUpdater.cpp) \
8892
$$quote($$BASEDIR/src/GetterRequest.cpp) \
8993
$$quote($$BASEDIR/src/QmlBeam.cpp) \
94+
$$quote($$BASEDIR/src/Settings.cpp) \
9095
$$quote($$BASEDIR/src/WifiInfo.cpp) \
9196
$$quote($$BASEDIR/src/applicationui.cpp) \
9297
$$quote($$BASEDIR/src/main.cpp) \
@@ -95,6 +100,7 @@ simulator {
95100
HEADERS += $$quote($$BASEDIR/src/ActiveFrameUpdater.h) \
96101
$$quote($$BASEDIR/src/GetterRequest.h) \
97102
$$quote($$BASEDIR/src/QmlBeam.h) \
103+
$$quote($$BASEDIR/src/Settings.h) \
98104
$$quote($$BASEDIR/src/WifiInfo.h) \
99105
$$quote($$BASEDIR/src/applicationui.hpp) \
100106
$$quote($$BASEDIR/src/timer.h)

src/Settings.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "Settings.h"
2+
3+
#include <QSettings>
4+
5+
Settings::Settings() {
6+
}
7+
8+
QString Settings::getSettings(const QString &objectName, const QString &defaultValue) {
9+
QSettings settings;
10+
11+
// If no value has been saved, return the default value.
12+
if (settings.value(objectName).isNull()) {
13+
return defaultValue;
14+
}
15+
16+
// Otherwise, return the value stored in the settings object.
17+
return settings.value(objectName).toString();
18+
}
19+
20+
void Settings::setSettings(const QString &objectName, const QString &inputValue) {
21+
// A new value is saved to the application settings object.
22+
QSettings settings;
23+
settings.setValue(objectName, QVariant(inputValue));
24+
}
25+
26+
Settings::~Settings() {
27+
}

src/Settings.h

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef SETTINGS_HPP_
2+
#define SETTINGS_HPP_
3+
4+
#include <QObject>
5+
6+
class Settings: public QObject {
7+
Q_OBJECT
8+
9+
public:
10+
Settings();
11+
virtual ~Settings();
12+
13+
/**
14+
* This Invokable function gets a value from the QSettings,
15+
* if that value does not exist in the QSettings database, the default value is returned.
16+
*
17+
* @param objectName Index path to the item
18+
* @param defaultValue Used to create the data in the database when adding
19+
* @return If the objectName exists, the value of the QSettings object is returned.
20+
* If the objectName doesn't exist, the default value is returned.
21+
*/
22+
Q_INVOKABLE
23+
QString getSettings(const QString &objectName, const QString &defaultValue);
24+
25+
/**
26+
* This function sets a value in the QSettings database. This function should to be called
27+
* when a data value has been updated from QML
28+
*
29+
* @param objectName Index path to the item
30+
* @param inputValue new value to the QSettings database
31+
*/
32+
Q_INVOKABLE
33+
void setSettings(const QString &objectName, const QString &inputValue);
34+
};
35+
36+
#endif

src/applicationui.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#include <bb/cascades/LocaleHandler>
2323
#include <QSettings>
2424

25+
//For Theme setting
26+
#include "Settings.h"
27+
2528
//For Static ActiveFrame
2629
#include <bb/cascades/SceneCover>
2730
#include <bb/cascades/Container>
@@ -60,6 +63,10 @@ ApplicationUI::ApplicationUI(bb::cascades::Application *app) :
6063
//set context for using as QSettings
6164
qml->setContextProperty("doitsettings",this);
6265

66+
// Create settings object and export it to qml
67+
Settings *settings = new Settings();
68+
qml->setContextProperty("Settings", settings);
69+
6370
//Create the Dynamic ActiveFrame using ActiveFrameUpdater.qml
6471
ActiveFrameUpdater *activeFrame = new ActiveFrameUpdater();
6572
Application::instance()->setCover(activeFrame);

src/main.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,35 @@
2424

2525
#include "GetterRequest.h"
2626
#include "timer.h"
27+
#include "Settings.h"
2728

2829
using namespace bb::cascades;
30+
using ::bb::cascades::Application;
31+
32+
QString getValue() {
33+
Settings settings;
34+
// use "theme" key for property showing what theme to use on application start
35+
return settings.getSettings("theme", "");
36+
}
37+
38+
39+
void myMessageOutput(QtMsgType type, const char* msg) {
40+
Q_UNUSED(type);
41+
fprintf(stdout, "%s\n", msg);
42+
fflush(stdout);
43+
}
2944

3045
Q_DECL_EXPORT int main(int argc, char **argv)
3146
{
47+
// update env variable before an application instance created
48+
qputenv("CASCADES_THEME", getValue().toUtf8());
49+
3250
Application app(argc, argv);
3351

52+
#ifndef QT_NO_DEBUG
53+
qInstallMsgHandler(myMessageOutput);
54+
#endif
55+
3456
qmlRegisterType<GetterRequest>("Network.GetterRequest", 1, 0, "GetterRequest");
3557
qmlRegisterType<Timer>("CustomerTimer", 1, 0, "Timer");
3658
qmlRegisterType<QTimer>("QTimerLibrary", 1, 0, "QTimer");

translations/DoIt.ts

+20
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@
4141
<source>password</source>
4242
<translation type="unfinished"></translation>
4343
</message>
44+
<message>
45+
<location filename="../assets/Settings.qml" line="52"/>
46+
<source>Theme Selection:</source>
47+
<translation type="unfinished"></translation>
48+
</message>
49+
<message>
50+
<location filename="../assets/Settings.qml" line="55"/>
51+
<source>Bright</source>
52+
<translation type="unfinished"></translation>
53+
</message>
54+
<message>
55+
<location filename="../assets/Settings.qml" line="60"/>
56+
<source>Dark</source>
57+
<translation type="unfinished"></translation>
58+
</message>
59+
<message>
60+
<location filename="../assets/Settings.qml" line="69"/>
61+
<source>Hello Themes</source>
62+
<translation type="unfinished"></translation>
63+
</message>
4464
</context>
4565
<context>
4666
<name>Start</name>

0 commit comments

Comments
 (0)