First version
[staging/HomeScreen.git] / 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
20 MainWindow::MainWindow(QWidget *parent) :
21     QMainWindow(parent),
22     mp_ui(new Ui::MainWindow),
23     mp_statusBarWidget(0),
24     mp_dBusDayNightMode_StatusBarWidget(0),
25     mp_controlBarWidget(0),
26     mp_dBusDayNightMode_ControlBarWidget(0),
27     m_dayNightMode(SystemDayNight::DAYNIGHTMODE_DAY), // TODO: read from system
28     mp_daynightmodeAdaptor(0),
29     mp_popupAdaptor(0),
30     mp_dBusPopup(0),
31     mp_popupWidget(0)
32 {
33     // dbus setup
34     QDBusConnection dbus = QDBusConnection::sessionBus();
35
36     // publish dbus day night mode interface
37     mp_daynightmodeAdaptor = new DaynightmodeAdaptor((QObject*)this);
38     // publish dbus popup interface
39     mp_popupAdaptor = new PopupAdaptor((QObject*)this);
40
41     dbus.registerObject("/MainWindow", this);
42     dbus.registerService("org.agl.mainwindow");
43
44     // no window decoration
45     setWindowFlags(Qt::FramelessWindowHint);
46     mp_ui->setupUi(this);
47
48     mp_statusBarWidget = new StatusBarWidget(this);
49     mp_statusBarWidget->raise();
50     // apply layout
51     mp_statusBarWidget->setGeometry(0, 0, 800, 60);
52     // connect to the dBus interface provided by the status bar widget
53     mp_dBusDayNightMode_StatusBarWidget = new org::agl::daynightmode("org.agl.mainwindow",
54                                               "/StatusBarWidget",
55                                               QDBusConnection::sessionBus(),
56                                               0);
57
58     mp_controlBarWidget = new ControlBarWidget(this);
59     mp_controlBarWidget->raise();
60     // apply layout
61     mp_controlBarWidget->setGeometry(0, 540, 800, 60);
62     // connect to the dBus interface provided by the control bar widget
63     mp_dBusDayNightMode_ControlBarWidget = new org::agl::daynightmode("org.agl.mainwindow",
64                                               "/ControlBarWidget",
65                                               QDBusConnection::sessionBus(),
66                                               0);
67 }
68
69 MainWindow::~MainWindow()
70 {
71     delete mp_dBusDayNightMode_ControlBarWidget;
72     delete mp_dBusDayNightMode_StatusBarWidget;
73
74     if (0 == mp_dBusPopup)
75     {
76         delete mp_dBusPopup;
77     }
78     if (0 != mp_popupWidget)
79     {
80         delete mp_popupWidget;
81     }
82
83
84
85     delete mp_popupAdaptor;
86     delete mp_daynightmodeAdaptor;
87     delete mp_statusBarWidget;
88     delete mp_ui;
89 }
90
91 void MainWindow::setDayNightMode(int mode)
92 {
93     switch (mode)
94     {
95     case SystemDayNight::DAYNIGHTMODE_DAY:
96         m_dayNightMode = SystemDayNight::DAYNIGHTMODE_DAY;
97         mp_ui->widget_Background->setStyleSheet(QString("background-image: url(:/images/backgrounds/bg_blue_day.png)"));
98         // home icon
99         mp_ui->widget_Home_Icon->setStyleSheet(QString("border-image: url(:/icons/home_day.png) 0 0 0 0 stretch stretch;"));
100
101         break;
102     case SystemDayNight::DAYNIGHTMODE_NIGHT:
103         m_dayNightMode = SystemDayNight::DAYNIGHTMODE_NIGHT;
104         mp_ui->widget_Background->setStyleSheet(QString("background-image: url(:/images/backgrounds/bg_blue_night.png)"));
105         // home icon
106         mp_ui->widget_Home_Icon->setStyleSheet(QString("border-image: url(:/icons/home_night.png) 0 0 0 0 stretch stretch;"));
107
108         break;
109     default:
110         m_dayNightMode = SystemDayNight::DAYNIGHTMODE_UNDEFINED;
111     }
112
113     mp_dBusDayNightMode_StatusBarWidget->setDayNightMode(m_dayNightMode);
114     mp_dBusDayNightMode_ControlBarWidget->setDayNightMode(m_dayNightMode);
115 }
116
117 void MainWindow::showPopup(int type, const QString &text)
118 {
119     if (0 == mp_popupWidget)
120     {
121         qDebug("0 == mp_popupWidget");
122         mp_popupWidget = new PopupWidget(this);
123     }
124
125     mp_popupWidget->move(0, 0);
126     mp_popupWidget->show();
127
128     if (0 == mp_dBusPopup)
129     {
130         qDebug("0 == mp_dBusPopup");
131         // connect to the dBus interface provided by the popup widget
132         mp_dBusPopup = new org::agl::popup("org.agl.mainwindow",
133                                                   "/PopupWidget",
134                                                   QDBusConnection::sessionBus(),
135                                                   0);
136     }
137     mp_dBusPopup->showPopup(type, text);
138 }
139
140 void MainWindow::changeEvent(QEvent* event)
141 {
142     if (QEvent::LanguageChange == event->type())
143     {
144         mp_ui->retranslateUi(this);
145     }
146
147     QMainWindow::changeEvent(event);
148 }
149