Initial source commit
[apps/homescreen.git] / homescreensimulator / src / mainwindow.cpp
1 /*
2  * Copyright (C) 2016, 2017 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_dBusDayNightModeAdaptor(0),
24     mp_dBusStatusBarProxy(0),
25     mp_dBusPopupProxy(0),
26     mp_dBusProximityProxy(0),
27     mp_dBusHomeScreenProxy(0)
28 {
29     mp_ui->setupUi(this);
30
31     // dbus setup
32     QDBusConnection dbus = QDBusConnection::sessionBus();
33
34     // publish dbus day night mode interface
35     mp_dBusDayNightModeAdaptor = new DaynightmodeAdaptor((QObject*)this);
36
37     dbus.registerService("org.agl.homescreen.simulator");
38     dbus.registerObject("/", this);
39
40     mp_dBusStatusBarProxy = new org::agl::statusbar("org.agl.homescreen",
41                                               "/StatusBar",
42                                               QDBusConnection::sessionBus(),
43                                               0);
44     mp_dBusPopupProxy = new org::agl::popup("org.agl.homescreen",
45                                               "/Popup",
46                                               QDBusConnection::sessionBus(),
47                                               0);
48     mp_dBusProximityProxy = new org::agl::proximity("org.agl.homescreen",
49                                               "/Proximity",
50                                               QDBusConnection::sessionBus(),
51                                               0);
52     mp_dBusHomeScreenProxy = new org::agl::homescreen("org.agl.homescreen",
53                                                      "/HomeScreen",
54                                                      QDBusConnection::sessionBus(),
55                                                      0);
56
57     QSettings settings;
58     this->move(settings.value("homescreensimulator/pos").toPoint());
59     mp_ui->radioButton_DayMode->setChecked(settings.value("homescreensimulator/daymode", true).toBool()); // if nothing is stored, use "true"
60     mp_ui->radioButton_NightMode->setChecked(settings.value("homescreensimulator/nightmode", false).toBool()); // if nothing is stored, use "false"
61 }
62
63 MainWindow::~MainWindow()
64 {
65     QSettings settings;
66     settings.setValue("homescreensimulator/pos", this->pos());
67     settings.setValue("homescreensimulator/daymode", mp_ui->radioButton_DayMode->isChecked());
68     settings.setValue("homescreensimulator/nightmode", mp_ui->radioButton_NightMode->isChecked());
69
70     delete mp_dBusHomeScreenProxy;
71     delete mp_dBusProximityProxy;
72     delete mp_dBusPopupProxy;
73     delete mp_dBusStatusBarProxy;
74
75     if (0 == mp_dBusDayNightModeAdaptor)
76     {
77         delete mp_dBusDayNightModeAdaptor;
78     }
79     delete mp_ui;
80 }
81
82 void MainWindow::on_radioButton_DayMode_toggled(bool checked)
83 {
84     if (checked)
85     {
86         emit(dayNightMode(SystemDayNight::DAYNIGHTMODE_DAY));
87     }
88 }
89
90 void MainWindow::on_radioButton_NightMode_toggled(bool checked)
91 {
92     if (checked)
93     {
94         emit(dayNightMode(SystemDayNight::DAYNIGHTMODE_NIGHT));
95     }
96 }
97
98 void MainWindow::on_pushButton_Update_clicked()
99 {
100     QList<int> availablePlaceholders = mp_dBusStatusBarProxy->getAvailablePlaceholders();
101     mp_ui->tableWidget_Status->verticalHeader()->setVisible(false);
102     mp_ui->tableWidget_Status->clear();
103     mp_ui->tableWidget_Status->setRowCount(availablePlaceholders.size());
104     mp_ui->tableWidget_Status->setColumnCount(3);
105
106     QList<QString> horizontalHeaderLabels;
107     horizontalHeaderLabels.append("Placeholder #");
108     horizontalHeaderLabels.append("Text");
109     horizontalHeaderLabels.append("iconURI");
110     mp_ui->tableWidget_Status->setHorizontalHeaderLabels(horizontalHeaderLabels);
111
112     // disable trigger for the update
113     mp_ui->tableWidget_Status->blockSignals(true);
114
115     for (int i = 0; i < availablePlaceholders.size(); ++i)
116     {
117         mp_ui->tableWidget_Status->setItem(i, 0, new QTableWidgetItem(QString::number(availablePlaceholders[i])));
118         mp_ui->tableWidget_Status->item(i, 0)->setFlags(Qt::ItemIsSelectable);
119         mp_ui->tableWidget_Status->setItem(i, 1, new QTableWidgetItem(mp_dBusStatusBarProxy->getStatusText(availablePlaceholders[i])));
120         mp_ui->tableWidget_Status->setItem(i, 2, new QTableWidgetItem(mp_dBusStatusBarProxy->getStatusIcon(availablePlaceholders[i])));
121     }
122
123     // enable trigger again
124     mp_ui->tableWidget_Status->blockSignals(false);
125 }
126
127 void MainWindow::on_pushButton_Exit_clicked()
128 {
129     QApplication::exit();
130 }
131
132 void MainWindow::on_tableWidget_Status_cellChanged(int row, int column)
133 {
134     // text
135     if (1 == column)
136     {
137         mp_dBusStatusBarProxy->setStatusText(mp_ui->tableWidget_Status->item(row, 0)->text().toInt(), mp_ui->tableWidget_Status->item(row, 1)->text());
138     }
139
140     // icon
141     if (2 == column)
142     {
143         mp_dBusStatusBarProxy->setStatusIcon(mp_ui->tableWidget_Status->item(row, 0)->text().toInt(), mp_ui->tableWidget_Status->item(row, 2)->text());
144     }
145 }
146
147 void MainWindow::on_pushButton_Send_clicked()
148 {
149     mp_dBusPopupProxy->showPopup(0, mp_ui->plainTextEdit_Popup->toPlainText());
150 }
151
152 void MainWindow::on_pushButton_IconExample_clicked()
153 {
154     on_pushButton_Update_clicked();
155
156     // THIS IS JUST AN EXAMPLE!!! It only works, because the alias for the pic is defined in the
157     // Home Screen app resources...
158     mp_ui->tableWidget_Status->setItem(0, 2, new QTableWidgetItem(":/icons/home_day.png"));
159 }
160
161 void MainWindow::on_pushButton_PopupExample_clicked()
162 {
163     mp_dBusPopupProxy->showPopup(0, "Hello Popup!\nNice to see you!");
164 }
165
166 void MainWindow::on_pushButton_comboBoxExample_clicked()
167 {
168     QStringList choices;
169     choices.append("Item1");
170     choices.append("Item2");
171     choices.append("Item3");
172     choices.append("Item4");
173     mp_dBusPopupProxy->showPopupComboBox("Make your choice!", choices);
174 }
175
176 void MainWindow::on_checkBox_ObjectDetected_clicked()
177 {
178     mp_dBusProximityProxy->setObjectDetected(Qt::Checked == mp_ui->checkBox_ObjectDetected->checkState());
179 }