First version
[staging/HomeScreen.git] / src / controlbarwidget.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 "controlbarwidget.h"
18 #include "ui_controlbarwidget.h"
19
20 ControlBarWidget::ControlBarWidget(QWidget *parent) :
21     QWidget(parent),
22     mp_ui(new Ui::ControlBarWidget),
23     mp_settingsWidget(0),
24     mp_dBusDayNightMode_SettingsWidget(0),
25     m_dayNightMode(SystemDayNight::DAYNIGHTMODE_DAY), // TODO: read from system
26     mp_daynightmodeAdaptor(0)
27 {
28     // publish dbus day night mode interface
29     mp_daynightmodeAdaptor = new DaynightmodeAdaptor((QObject*)this);
30     QDBusConnection dbus = QDBusConnection::sessionBus();
31     dbus.registerObject("/ControlBarWidget", this);
32     dbus.registerService("org.agl.mainwindow");
33
34     mp_ui->setupUi(this);
35 }
36
37 ControlBarWidget::~ControlBarWidget()
38 {
39     delete mp_daynightmodeAdaptor;
40     if (0 != mp_settingsWidget)
41     {
42         delete mp_settingsWidget;
43     }
44     if (0 == mp_dBusDayNightMode_SettingsWidget)
45     {
46         delete mp_dBusDayNightMode_SettingsWidget;
47     }
48     delete mp_ui;
49 }
50
51 void ControlBarWidget::setDayNightMode(int mode)
52 {
53     QIcon icon;
54     switch (mode)
55     {
56     case SystemDayNight::DAYNIGHTMODE_DAY:
57         m_dayNightMode = SystemDayNight::DAYNIGHTMODE_DAY;
58         mp_ui->widget_Background->setStyleSheet(QString("background-image: url(:/images/backgrounds/bg_green_day.png)"));
59
60         icon.addFile(QStringLiteral(":/icons/home_day.png"), QSize(), QIcon::Normal, QIcon::Off);
61         mp_ui->pushButton_Home->setIcon(icon);
62         icon.addFile(QStringLiteral(":/icons/settings_day.png"), QSize(), QIcon::Normal, QIcon::Off);
63         mp_ui->pushButton_Settings->setIcon(icon);
64         break;
65     case SystemDayNight::DAYNIGHTMODE_NIGHT:
66         m_dayNightMode = SystemDayNight::DAYNIGHTMODE_NIGHT;
67         mp_ui->widget_Background->setStyleSheet(QString("background-image: url(:/images/backgrounds/bg_green_night.png)"));
68
69         icon.addFile(QStringLiteral(":/icons/home_night.png"), QSize(), QIcon::Normal, QIcon::Off);
70         mp_ui->pushButton_Home->setIcon(icon);
71         icon.addFile(QStringLiteral(":/icons/settings_night.png"), QSize(), QIcon::Normal, QIcon::Off);
72         mp_ui->pushButton_Settings->setIcon(icon);
73         break;
74     default:
75         m_dayNightMode = SystemDayNight::DAYNIGHTMODE_UNDEFINED;
76     }
77
78     // settings widget if present
79     if (0 != mp_dBusDayNightMode_SettingsWidget)
80     {
81         mp_dBusDayNightMode_SettingsWidget->setDayNightMode(m_dayNightMode);
82     }
83 }
84
85 void ControlBarWidget::on_pushButton_Settings_clicked()
86 {
87     if (0 == mp_settingsWidget)
88     {
89         mp_settingsWidget = new SettingsWidget((QWidget*)parent());
90     }
91
92     mp_settingsWidget->move(0, 60);
93     mp_settingsWidget->show();
94
95     if (0 == mp_dBusDayNightMode_SettingsWidget)
96     {
97         // connect to the dBus interface provided by the settings widget
98         mp_dBusDayNightMode_SettingsWidget = new org::agl::daynightmode("org.agl.mainwindow",
99                                                   "/SettingsWidget",
100                                                   QDBusConnection::sessionBus(),
101                                                   0);
102         mp_dBusDayNightMode_SettingsWidget->setDayNightMode(m_dayNightMode);
103     }
104 }
105
106 void ControlBarWidget::on_pushButton_Home_clicked()
107 {
108     if (0 != mp_settingsWidget)
109     {
110         mp_settingsWidget->hide();
111     }
112 }