Advertisement
deimos

Untitled

May 24th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. #ifndef THEMEHELPER_H_
  2. #define THEMEHELPER_H_
  3.  
  4. #include <QObject>
  5. #include <QSettings>
  6. #include <bb/cascades/Application>
  7. #include <bb/cascades/ColorTheme>
  8. #include <bb/cascades/Theme>
  9. #include <bb/cascades/ThemeSupport>
  10. #include <bb/cascades/VisualStyle>
  11.  
  12. using namespace bb::cascades;
  13.  
  14.  
  15. class ThemeHelper : public QObject {
  16.  
  17. Q_OBJECT
  18.  
  19.     Q_PROPERTY(Themes currentTheme READ currentTheme NOTIFY currentThemeChanged);
  20.     Q_ENUMS(Themes);
  21.  
  22. public:
  23.     enum Themes {
  24.         Bright = 0, Dark, Unknown
  25.     };
  26.     static ThemeHelper & getInstance()
  27.     {
  28.         static ThemeHelper instance;
  29.         return instance;
  30.     }
  31.  
  32.     ThemeHelper(QObject *parent = 0);
  33.     virtual ~ThemeHelper();
  34.     static ThemeHelper::Themes currentTheme();
  35.  
  36.     Q_INVOKABLE static void setLastTheme();
  37.     Q_INVOKABLE static void changeTheme(ThemeHelper::Themes type);
  38.  
  39. private:
  40.     static QSettings m_settings;
  41.  
  42. Q_SIGNALS:
  43.     void currentThemeChanged(ThemeHelper::Themes type);
  44. };
  45. Q_DECLARE_METATYPE(ThemeHelper::Themes);
  46.  
  47. #endif /* THEMEHELPER_H_ */
  48.  
  49. ---------------------------------------------------
  50. ---------------------------------------------------
  51. ---------------------------------------------------
  52.  
  53. #include "ThemeHelper.h"
  54.  
  55. /* Public */
  56. ThemeHelper::ThemeHelper(QObject *parent) : QObject(parent) {}
  57.  
  58. ThemeHelper::~ThemeHelper() {}
  59.  
  60. ThemeHelper::Themes ThemeHelper::currentTheme() {
  61.     VisualStyle::Type style = Application::instance()->themeSupport()->theme()->colorTheme()->style();
  62.     switch (style) {
  63.         case Bright: return Bright;
  64.         case Dark: return Dark;
  65.         default: return Unknown;
  66.     }
  67. }
  68.  
  69. /* Q_INVOKABLE */
  70. void ThemeHelper::setLastTheme() {
  71.     if(m_settings.contains("theme")) {
  72.         Application::instance()->themeSupport()->setVisualStyle(m_settings.value("theme").toInt());
  73.    }
  74. }
  75.  
  76. void ThemeHelper::changeTheme(ThemeHelper::Themes type) {
  77.     qDebug() << "ChangeTheme" << type;
  78.     switch (type) {
  79.         case Bright: Application::instance()->themeSupport()->setVisualStyle(VisualStyle::Bright); break;
  80.         case Dark: Application::instance()->themeSupport()->setVisualStyle(VisualStyle::Dark); break;
  81.         default:
  82.             qWarning() << "Unknown theme";
  83.             break;
  84.     }
  85.  
  86.     m_settings.setValue("theme", QVariant(type));
  87.     emit ThemeHelper::getInstance().currentThemeChanged(type);
  88. }
  89.  
  90. /* Private */
  91. QSettings ThemeHelper::m_settings(QSettings::UserScope, QApplication::organizationName());
  92.  
  93.  
  94. ---------------------------------------------------
  95. ---------------------------------------------------
  96. ---------------------------------------------------
  97.  
  98. nel constructor di ApplicationUI:
  99.  
  100. ApplicationUI::ApplicationUI(bb::cascades::Application *app) :
  101.         QObject(app)
  102. {
  103.     qRegisterMetaType<ThemeHelper::Themes>("themes");
  104.  
  105.     [...]
  106.  
  107.     // Create scene document from main.qml asset, the parent is set
  108.     // to ensure the document gets destroyed properly at shut down.
  109.     QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
  110.  
  111.  
  112.     ThemeHelper &x = ThemeHelper::getInstance();
  113.     qml->setContextProperty("_themeHelper", &x);
  114.  
  115.     [...]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement