039b99fee5ca6a1cfb593c9a36c251a2dc9f0045
[staging/HomeScreen.git] / HomeScreen / src / settingswidget.cpp
1 /*
2  * Copyright (C) 2016 Mentor Graphics Development (Deutschland) GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "settingswidget.h"
18 #include "ui_settingswidget.h"
19 #include <QSettings>
20
21 SettingsWidget::SettingsWidget(QWidget *parent) :
22     QWidget(parent),
23     mp_ui(new Ui::SettingsWidget),
24     m_dayNightMode(SystemDayNight::DAYNIGHTMODE_DAY), // TODO: read from system
25     mp_dayNightModeProxy(0),
26     mp_translator(0)
27 {
28     // this has to be adopted to the system setup
29     mp_dayNightModeProxy = new org::agl::daynightmode("org.agl.homescreen.simulator", //"org.agl.systeminfoprovider"
30                                                       "/",
31                                                       QDBusConnection::sessionBus(),
32                                                       0);
33     QObject::connect(mp_dayNightModeProxy, SIGNAL(dayNightMode(int)), this, SLOT(dayNightModeSlot(int)));
34
35     // multi-language support
36     mp_translator = new QTranslator();
37     mp_translator->load("homescreen_en_US.qm", ":/translations"); // TODO: read from system
38     QApplication::instance()->installTranslator(mp_translator);
39
40     mp_ui->setupUi(this);
41
42     mp_ui->comboBoxLanguage->addItem(QString("English"), QVariant("homescreen_en_US.qm")); // TODO: make this configurable?
43     mp_ui->comboBoxLanguage->addItem(QString("Deutsch"), QVariant("homescreen_de_DE.qm"));
44     mp_ui->comboBoxLanguage->addItem(QString("日本語"), QVariant("homescreen_ja_JP.qm"));
45
46     QSettings settings;
47     mp_ui->comboBoxLanguage->setCurrentIndex(settings.value("systemsettings/language", 0).toInt());
48 }
49
50 SettingsWidget::~SettingsWidget()
51 {
52     delete mp_translator;
53     delete mp_dayNightModeProxy;
54
55     QSettings settings;
56     settings.setValue("systemsettings/language", mp_ui->comboBoxLanguage->currentIndex());
57     delete mp_ui;
58 }
59
60 void SettingsWidget::dayNightModeSlot(int mode)
61 {
62     switch (mode)
63     {
64     case SystemDayNight::DAYNIGHTMODE_DAY:
65         m_dayNightMode = SystemDayNight::DAYNIGHTMODE_DAY;
66         mp_ui->widget_Background->setStyleSheet(QString("background-image: url(:/images/backgrounds/bg_blue_day.png);"));
67         mp_ui->comboBoxLanguage->setStyleSheet(QString("QComboBox { \
68             border: 1px solid #D3D3D3; \
69             border-radius: 8px; \
70             background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(242, 242, 249, 255), stop:1 rgba(255, 255, 255, 255)); \
71             color: #333; \
72             padding: 0px; \
73             } \
74             QComboBox:on { \
75             background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #D5D5D5, stop: 1 #EEEEEE); \
76             } \
77             QComboBox::drop-down { \
78             border: 0px solid; \
79             border-radius: 0px; \
80             } \
81             QComboBox::down-arrow:on { \
82             }"));
83         // settings icon
84         mp_ui->widget_Settings_Icon->setStyleSheet(QString("border-image: url(:/icons/settings_day.png) 0 0 0 0 stretch stretch;"));
85         break;
86     case SystemDayNight::DAYNIGHTMODE_NIGHT:
87         m_dayNightMode = SystemDayNight::DAYNIGHTMODE_NIGHT;
88         mp_ui->widget_Background->setStyleSheet(QString("background-image: url(:/images/backgrounds/bg_blue_night.png);"));
89         mp_ui->comboBoxLanguage->setStyleSheet(QString("QComboBox { \
90             border: 1px solid #D3D3D3; \
91             border-radius: 8px; \
92             background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(147, 147, 151, 255), stop:1 rgba(255, 255, 255, 255)); \
93             color: #333; \
94             padding: 0px; \
95             } \
96             QComboBox:on { \
97             background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #D5D5D5, stop: 1 #EEEEEE); \
98             } \
99             QComboBox::drop-down { \
100             border: 0px solid; \
101             border-radius: 0px; \
102             } \
103             QComboBox::down-arrow:on { \
104             }"));
105         // settings icon
106         mp_ui->widget_Settings_Icon->setStyleSheet(QString("border-image: url(:/icons/settings_night.png) 0 0 0 0 stretch stretch;"));
107         break;
108     default:
109         m_dayNightMode = SystemDayNight::DAYNIGHTMODE_UNDEFINED;
110     }
111 }
112
113 void SettingsWidget::changeEvent(QEvent* event)
114 {
115     if (QEvent::LanguageChange == event->type())
116     {
117         mp_ui->retranslateUi(this);
118     }
119
120     QWidget::changeEvent(event);
121 }
122
123 void SettingsWidget::on_comboBoxLanguage_currentIndexChanged(const QString &)
124 {
125     if (0 != mp_translator)
126         mp_translator->load(mp_ui->comboBoxLanguage->currentData().toString(), ":/translations");
127 }