bf7735c96195976054f7887a8f1a1dc5cbff6d82
[staging/HomeScreen.git] / HomeScreen / src / mainwindow.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 "mainwindow.h"
18 #include "ui_mainwindow.h"
19 #include "../interfaces/daynightmode.h"
20
21 MainWindow::MainWindow(QWidget *parent) :
22     QMainWindow(parent),
23     mp_ui(new Ui::MainWindow),
24     mp_statusBarWidget(0),
25     mp_controlBarWidget(0),
26     mp_settingsWidget(0),
27     mp_popupWidget(0),
28     mp_dayNightModeProxy(0)
29 {
30     // this has to be adopted to the system setup
31     mp_dayNightModeProxy = new org::agl::daynightmode("org.agl.homescreen.simulator", //"org.agl.systeminfoprovider"
32                                                       "/",
33                                                       QDBusConnection::sessionBus(),
34                                                       0);
35     QObject::connect(mp_dayNightModeProxy, SIGNAL(dayNightMode(int)), this, SLOT(dayNightModeSlot(int)));
36
37     // dbus setup
38     QDBusConnection dbus = QDBusConnection::sessionBus();
39
40     dbus.registerObject("/MainWindow", this);
41     dbus.registerService("org.agl.homescreen");
42
43     // no window decoration
44     setWindowFlags(Qt::FramelessWindowHint);
45     mp_ui->setupUi(this);
46
47     mp_statusBarWidget = new StatusBarWidget(this);
48     mp_statusBarWidget->raise();
49     // apply layout
50     mp_statusBarWidget->move(0, 0);
51
52     mp_controlBarWidget = new ControlBarWidget(this);
53     mp_controlBarWidget->raise();
54     // apply layout
55     mp_controlBarWidget->move(0, 1920-60);
56
57     mp_settingsWidget = new SettingsWidget(this);
58     mp_settingsWidget->raise();
59     // apply layout
60     mp_settingsWidget->move(0, 60);
61     mp_settingsWidget->hide();
62
63     mp_popupWidget = new PopupWidget(this);
64     mp_controlBarWidget->raise();
65     // apply layout
66     mp_popupWidget->move(0, 0);
67
68     QObject::connect(mp_settingsWidget, SIGNAL(colorSchemeChanged()), this, SLOT(updateColorScheme()));
69     QObject::connect(mp_settingsWidget, SIGNAL(colorSchemeChanged()), mp_statusBarWidget, SLOT(updateColorScheme()));
70     QObject::connect(mp_settingsWidget, SIGNAL(colorSchemeChanged()), mp_controlBarWidget, SLOT(updateColorScheme()));
71     QObject::connect(mp_settingsWidget, SIGNAL(colorSchemeChanged()), mp_settingsWidget, SLOT(updateColorScheme()));
72     QObject::connect(mp_settingsWidget, SIGNAL(colorSchemeChanged()), mp_popupWidget, SLOT(updateColorScheme()));
73
74     QObject::connect(mp_controlBarWidget, SIGNAL(settingsButtonPressed()), mp_settingsWidget, SLOT(show()));
75     QObject::connect(mp_controlBarWidget, SIGNAL(homeButtonPressed()), mp_settingsWidget, SLOT(hide()));
76
77     // apply color scheme
78     updateColorScheme();
79     mp_statusBarWidget->updateColorScheme();
80     mp_controlBarWidget->updateColorScheme();
81     mp_settingsWidget->updateColorScheme();
82     mp_popupWidget->updateColorScheme();
83
84     setWindowIcon(QIcon(":/icons/home_day.png"));
85 }
86
87 MainWindow::~MainWindow()
88 {
89     delete mp_dayNightModeProxy;
90     delete mp_popupWidget;
91     delete mp_settingsWidget;
92     delete mp_controlBarWidget;
93     delete mp_statusBarWidget;
94     delete mp_ui;
95 }
96
97 void MainWindow::dayNightModeSlot(int mode)
98 {
99     QSettings settings;
100     settings.setValue("systemsettings/daynightmode", mode);
101     // make sure that everything is written to the settings file before continuing
102     settings.sync();
103
104     updateColorScheme();
105
106     mp_statusBarWidget->updateColorScheme();
107     mp_controlBarWidget->updateColorScheme();
108     mp_settingsWidget->updateColorScheme();
109     mp_popupWidget->updateColorScheme();
110 }
111
112 void MainWindow::updateColorScheme()
113 {
114     QSettings settings;
115     QSettings settings_cs(QApplication::applicationDirPath() +
116                           "/colorschemes/" +
117                           settings.value("systemsettings/colorscheme", "default").toString() +
118                           "/" +
119                           QString::number(settings.value("systemsettings/daynightmode", SystemDayNight::DAYNIGHTMODE_DAY).toInt()) +
120                           ".ini",
121                           QSettings::IniFormat);
122
123     mp_ui->widget_Background->setStyleSheet(settings_cs.value("MainWindow/widget_Background").toString());
124     mp_ui->widget_Home_Icon->setStyleSheet(settings_cs.value("MainWindow/widget_Home_Icon").toString());
125 }
126
127 void MainWindow::changeEvent(QEvent* event)
128 {
129     if (QEvent::LanguageChange == event->type())
130     {
131         mp_ui->retranslateUi(this);
132     }
133
134     QMainWindow::changeEvent(event);
135 }