Implemented proximity interface.
[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 <include/daynightmode.hpp>
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_applauncherwidget(0),
28     mp_popupWidget(0),
29     mp_layoutHandler(new LayoutHandler()),
30     mp_dBusDayNightModeProxy(0),
31     mp_proximityAdaptor(0),
32     mp_homeScreenControlInterface(0)
33 {
34     // this has to be adopted to the system setup
35     mp_dBusDayNightModeProxy = new org::agl::daynightmode("org.agl.homescreen.simulator", //"org.agl.systeminfoprovider"
36                                                       "/",
37                                                       QDBusConnection::sessionBus(),
38                                                       0);
39     QObject::connect(mp_dBusDayNightModeProxy, SIGNAL(dayNightMode(int)), this, SLOT(dayNightModeSlot(int)));
40
41     mp_proximityAdaptor = new ProximityAdaptor((QObject*)this);
42
43     // dbus setup
44     QDBusConnection dbus = QDBusConnection::sessionBus();
45     dbus.registerObject("/Proximity", this);
46     dbus.registerService("org.agl.homescreen");
47
48     // no window decoration
49     setWindowFlags(Qt::FramelessWindowHint);
50
51     mp_ui->setupUi(this);
52
53     mp_statusBarWidget = new StatusBarWidget(this);
54     mp_statusBarWidget->raise();
55     // apply layout
56     mp_statusBarWidget->move(0, 0);
57
58     mp_controlBarWidget = new ControlBarWidget(this);
59     mp_controlBarWidget->raise();
60     // apply layout
61     mp_controlBarWidget->move(0, 1920-60);
62
63     mp_settingsWidget = new SettingsWidget(this);
64     mp_settingsWidget->raise();
65     // apply layout
66     mp_settingsWidget->move(0, 60);
67     //mp_settingsWidget->hide();
68
69     mp_applauncherwidget = new AppLauncherWidget(this);
70     mp_applauncherwidget->raise();
71     // apply layout
72     mp_applauncherwidget->move(0, 60);
73
74
75     mp_popupWidget = new PopupWidget();
76     mp_controlBarWidget->raise();
77     // apply layout
78     mp_popupWidget->move(0, 0);
79
80
81     QObject::connect(mp_settingsWidget, SIGNAL(colorSchemeChanged()), this, SLOT(updateColorScheme()));
82     QObject::connect(mp_settingsWidget, SIGNAL(colorSchemeChanged()), mp_statusBarWidget, SLOT(updateColorScheme()));
83     QObject::connect(mp_settingsWidget, SIGNAL(colorSchemeChanged()), mp_controlBarWidget, SLOT(updateColorScheme()));
84     QObject::connect(mp_settingsWidget, SIGNAL(colorSchemeChanged()), mp_settingsWidget, SLOT(updateColorScheme()));
85     QObject::connect(mp_settingsWidget, SIGNAL(colorSchemeChanged()), mp_popupWidget, SLOT(updateColorScheme()));
86
87     QObject::connect(mp_controlBarWidget, SIGNAL(settingsButtonPressed()), mp_settingsWidget, SLOT(raise()));
88     QObject::connect(mp_controlBarWidget, SIGNAL(homeButtonPressed()), mp_applauncherwidget, SLOT(raise()));
89
90     QObject::connect(mp_applauncherwidget, SIGNAL(newRequestsToBeVisibleApp(int)), mp_layoutHandler, SLOT(makeMeVisible(int)));
91
92     // apply color scheme
93     updateColorScheme();
94
95     // this is only useful during development and will be removed later
96     setWindowIcon(QIcon(":/icons/home_day.png"));
97
98     mp_applauncherwidget->populateAppList();
99     mp_layoutHandler->setUpLayouts();
100
101     mp_homeScreenControlInterface = new HomeScreenControlInterface(this);
102     QObject::connect(mp_homeScreenControlInterface, SIGNAL(newRequestsToBeVisibleApp(int)), mp_layoutHandler, SLOT(makeMeVisible(int)));
103
104     QObject::connect(mp_popupWidget, SIGNAL(comboBoxResult(QString)), mp_layoutHandler, SLOT(setLayoutByName(QString)));
105 }
106
107 MainWindow::~MainWindow()
108 {
109     delete mp_homeScreenControlInterface;
110
111     delete mp_dBusDayNightModeProxy;
112
113     delete mp_layoutHandler;
114
115     delete mp_popupWidget;
116     delete mp_applauncherwidget;
117     delete mp_settingsWidget;
118     delete mp_controlBarWidget;
119     delete mp_statusBarWidget;
120     delete mp_proximityAdaptor;
121     delete mp_ui;
122 }
123
124 void MainWindow::dayNightModeSlot(int mode)
125 {
126     QSettings settings;
127     settings.setValue("systemsettings/daynightmode", mode);
128     // make sure that everything is written to the settings file before continuing
129     settings.sync();
130
131     updateColorScheme();
132 }
133
134 void MainWindow::setObjectDetected(bool detected)
135 {
136     qDebug("setObjectDetected %s", detected ? "true" : "false");
137     QSettings settings;
138     settings.setValue("systemsettings/proximityobjectdetected", detected);
139     // make sure that everything is written to the settings file before continuing
140     settings.sync();
141
142     updateColorScheme();
143 }
144
145 void MainWindow::updateColorScheme()
146 {
147     QSettings settings;
148     QSettings settings_cs(QApplication::applicationDirPath() +
149                           "/colorschemes/" +
150                           settings.value("systemsettings/colorscheme", "default").toString() +
151                           "/" +
152                           QString::number(settings.value("systemsettings/proximityobjectdetected", false).toBool()) +
153                           "/" +
154                           QString::number(settings.value("systemsettings/daynightmode", SystemDayNight::DAYNIGHTMODE_DAY).toInt()) +
155                           ".ini",
156                           QSettings::IniFormat);
157
158     mp_ui->widget_background->setStyleSheet(settings_cs.value("MainWindow/widget_background_css").toString());
159     mp_ui->widget_homeIcon->setStyleSheet(settings_cs.value("MainWindow/widget_homeIcon_css").toString());
160
161     // update children
162     mp_statusBarWidget->updateColorScheme();
163     mp_controlBarWidget->updateColorScheme();
164     mp_settingsWidget->updateColorScheme();
165     mp_applauncherwidget->updateColorScheme();
166     mp_popupWidget->updateColorScheme();
167 }
168
169 void MainWindow::changeEvent(QEvent* event)
170 {
171     if (QEvent::LanguageChange == event->type())
172     {
173         mp_ui->retranslateUi(this);
174     }
175
176     QMainWindow::changeEvent(event);
177 }
178