Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef THEMEHELPER_H_
- #define THEMEHELPER_H_
- #include <QObject>
- #include <QSettings>
- #include <bb/cascades/Application>
- #include <bb/cascades/ColorTheme>
- #include <bb/cascades/Theme>
- #include <bb/cascades/ThemeSupport>
- #include <bb/cascades/VisualStyle>
- using namespace bb::cascades;
- class ThemeHelper : public QObject {
- Q_OBJECT
- Q_PROPERTY(Themes currentTheme READ currentTheme NOTIFY currentThemeChanged);
- Q_ENUMS(Themes);
- public:
- enum Themes {
- Bright = 0, Dark, Unknown
- };
- static ThemeHelper & getInstance()
- {
- static ThemeHelper instance;
- return instance;
- }
- ThemeHelper(QObject *parent = 0);
- virtual ~ThemeHelper();
- static ThemeHelper::Themes currentTheme();
- Q_INVOKABLE static void setLastTheme();
- Q_INVOKABLE static void changeTheme(ThemeHelper::Themes type);
- private:
- static QSettings m_settings;
- Q_SIGNALS:
- void currentThemeChanged(ThemeHelper::Themes type);
- };
- Q_DECLARE_METATYPE(ThemeHelper::Themes);
- #endif /* THEMEHELPER_H_ */
- ---------------------------------------------------
- ---------------------------------------------------
- ---------------------------------------------------
- #include "ThemeHelper.h"
- /* Public */
- ThemeHelper::ThemeHelper(QObject *parent) : QObject(parent) {}
- ThemeHelper::~ThemeHelper() {}
- ThemeHelper::Themes ThemeHelper::currentTheme() {
- VisualStyle::Type style = Application::instance()->themeSupport()->theme()->colorTheme()->style();
- switch (style) {
- case Bright: return Bright;
- case Dark: return Dark;
- default: return Unknown;
- }
- }
- /* Q_INVOKABLE */
- void ThemeHelper::setLastTheme() {
- if(m_settings.contains("theme")) {
- Application::instance()->themeSupport()->setVisualStyle(m_settings.value("theme").toInt());
- }
- }
- void ThemeHelper::changeTheme(ThemeHelper::Themes type) {
- qDebug() << "ChangeTheme" << type;
- switch (type) {
- case Bright: Application::instance()->themeSupport()->setVisualStyle(VisualStyle::Bright); break;
- case Dark: Application::instance()->themeSupport()->setVisualStyle(VisualStyle::Dark); break;
- default:
- qWarning() << "Unknown theme";
- break;
- }
- m_settings.setValue("theme", QVariant(type));
- emit ThemeHelper::getInstance().currentThemeChanged(type);
- }
- /* Private */
- QSettings ThemeHelper::m_settings(QSettings::UserScope, QApplication::organizationName());
- ---------------------------------------------------
- ---------------------------------------------------
- ---------------------------------------------------
- nel constructor di ApplicationUI:
- ApplicationUI::ApplicationUI(bb::cascades::Application *app) :
- QObject(app)
- {
- qRegisterMetaType<ThemeHelper::Themes>("themes");
- [...]
- // Create scene document from main.qml asset, the parent is set
- // to ensure the document gets destroyed properly at shut down.
- QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
- ThemeHelper &x = ThemeHelper::getInstance();
- qml->setContextProperty("_themeHelper", &x);
- [...]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement