112a075ae44789f2b0580d58d597ff0074e0f796
[apps/thermostat-demo.git] / settingscreen.cpp
1 #include "settingscreen.h"
2 #include "mainwindow.h"
3 #include "globalsettings.h"
4 #include "qkeyboardlineedit.h"
5 #include "webdataengine/openweathermapdataengine.h"
7 #include <QtGui>
9 #define MAXIMUM_CONTENT_HEIGHT 450
10 #define MAXIMUM_CONTENT_WIDTH 800
12 SettingScreen::SettingScreen(QWidget *parent) :
13     QWidget(parent)
14 {
15     m_globalSettings = GlobalSettings::getInstance();
17     // create title
18     QLabel *title = new QLabel("Select your CURRENT SETTINGS");
20     // create return button
21     QPushButton *backButton = new QPushButton((this));
22     backButton->setIcon(QIcon(":/Images/glossy-blue-orb-icon-arrowback.png"));
23     backButton->setIconSize(QSize(40,40));
24     backButton->setFocusPolicy(Qt::NoFocus);
25     backButton->setObjectName("backButton");
26     connect(backButton,SIGNAL(clicked()),this,SLOT(commitChanges()));
27     connect(backButton, SIGNAL(clicked()), this, SIGNAL(valueChanged()));
29     QHBoxLayout *topLayout = new QHBoxLayout;
30     topLayout->addSpacing(40);
31     topLayout->addStretch();
32     topLayout->addWidget(title);
33     topLayout->addStretch();
34     topLayout->addWidget(backButton);
36     QTabWidget* mainTabWidget = new QTabWidget(this);
37     mainTabWidget->setFocusPolicy(Qt::NoFocus);
39     mainTabWidget->addTab(buildGeneralSettingsWidget(), "General");
40     mainTabWidget->addTab(buildFormatSettingsWidget(), "Format");
41     mainTabWidget->addTab(buildNetworkSettingsWidget(), "Network");
42 //    mainTabWidget->addTab(buildGeneralSettingsWidget(), QIcon(":/Images/glossy-blue-orb-icon-arrowback.png"),"General");
43 //    mainTabWidget->addTab(buildFormatSettingsWidget(), QIcon(":/Images/glossy-blue-orb-icon-arrowback.png"),"Format");
44 //    mainTabWidget->addTab(buildNetworkSettingsWidget(), QIcon(":/Images/glossy-blue-orb-icon-arrowback.png"),"Network");
45 //
46     mainTabWidget->setIconSize(QSize(40,40));
47     mainTabWidget->setTabPosition(QTabWidget::West);
48     mainTabWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
49     mainTabWidget->setMaximumSize(MAXIMUM_CONTENT_HEIGHT, MAXIMUM_CONTENT_WIDTH);
51     QHBoxLayout* contentsBox = new QHBoxLayout;
52     contentsBox->addWidget(mainTabWidget);
54     QVBoxLayout *mainLayout = new QVBoxLayout;
55     mainLayout->addLayout(topLayout);
56     mainLayout->addStretch();
57     mainLayout->addLayout(contentsBox);
58     mainLayout->setStretchFactor(contentsBox, 0);
59     mainLayout->addStretch(0);
60     mainLayout->setContentsMargins(0,0,0,0);
61     mainLayout->setAlignment(contentsBox,Qt::AlignCenter);
64     // show layout
65     setLayout(mainLayout);
67 }
69 void SettingScreen::showEvent(QShowEvent *)
70 {
71     fillSettingValues();
72 }
74 void SettingScreen::fillSettingValues()
75 {
76     //populate the current settings
78     int index = cityBox->findText(m_globalSettings->currentCity());
79     if(index>-1)
80         cityBox->setCurrentIndex(index);
82     if(m_globalSettings->temperatureFormat() == GlobalSettings::TempFormatC)
83         setTempFormatC();
84     else
85         setTempFormatF();
87     if(m_globalSettings->timeFormat() == GlobalSettings::TimeFormat24h)
88         setTimeFormat24h();
89     else
90         setTimeFormat12h();
92     m_proxyHostLineEdit->setText(m_globalSettings->proxyHost());
94     //to prevent a zero from showing up
95     if(QString::number(m_globalSettings->proxyPort()) != 0)
96         m_proxyPortLineEdit->setText(QString::number(m_globalSettings->proxyPort()));
97 }
99 QWidget* SettingScreen::buildNetworkSettingsWidget()
101     QWidget* networkSettingsWidget = new QWidget(this);
102     networkSettingsWidget->setStyle(this->style());
105     QLabel *m_proxyHostLabel = new QLabel("Proxy Host: ", this);
106     QLabel *m_proxyPortLabel = new QLabel("Proxy Port: ", this);
107     m_proxyHostLineEdit = new QKeyboardLineEdit;
108     m_proxyPortLineEdit = new QKeyboardLineEdit;
110     QFormLayout* proxyLayout = new QFormLayout;
111     proxyLayout->addRow(m_proxyHostLabel, m_proxyHostLineEdit);
112     proxyLayout->addRow(m_proxyPortLabel, m_proxyPortLineEdit);
113     m_proxyHostLineEdit->setMaximumWidth(300);
114     m_proxyPortLineEdit->setMaximumWidth(80);
115     proxyLayout->setFormAlignment(Qt::AlignCenter);
117     QVBoxLayout* proxyVBoxLayout = new QVBoxLayout(this);
118     proxyVBoxLayout->addStretch();
119     proxyVBoxLayout->addLayout(proxyLayout);
121     QGroupBox* proxySettingsGroupBox = new QGroupBox("Proxy Settings");
122     proxySettingsGroupBox->setLayout(proxyVBoxLayout);
124     configureWifiButton = new QPushButton("Configure Wifi");
126     connect(configureWifiButton, SIGNAL(clicked()), this, SLOT(launchWifiConfigure()));
127     configureWifiButton->setFocusPolicy(Qt::NoFocus);
129     QVBoxLayout *networkSettingsLayout = new QVBoxLayout(this);
130     networkSettingsLayout->addWidget(proxySettingsGroupBox);
131     networkSettingsLayout->addWidget(configureWifiButton);
132     networkSettingsLayout->addStretch();
134     networkSettingsWidget->setLayout(networkSettingsLayout);
136     connect(&m_wifiConfigureProcess, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(wifiConfigureFinished(int, QProcess::ExitStatus)));
138     return networkSettingsWidget;
141 QWidget* SettingScreen::buildGeneralSettingsWidget()
143     // create box to contain all possible cities
144     cityBox = new QComboBox;
145     cityBox->setFocusPolicy(Qt::NoFocus);
146     cityBox->addItem("Dallas,TX");
147     cityBox->addItem("Tokyo,Japan");
148     cityBox->addItem("Chicago,IL");
149     cityBox->addItem("Hong Kong,China");
150     cityBox->addItem("Munich,Germany");
151     cityBox->addItem("Taipei,Taiwan");
152     cityBox->addItem("San Jose,CA");
153     cityBox->addItem("Shanghai,China");
154     cityBox->addItem("New York,NY");
155     cityBox->addItem("Bangalore,India");
156     cityBox->addItem("Sao Paulo,Brazil");
157     cityBox->addItem("Seoul,South Korea");
158     cityBox->addItem("Moscow,Russia");
159     cityBox->addItem("Mexico City,Mexico");
160     cityBox->addItem("Johannesburg,South Africa");
163     QVBoxLayout *cityLayout = new QVBoxLayout;
164     cityLayout->addStretch();
165     cityLayout->addWidget(cityBox);
166     cityLayout->addStretch();
168     QWidget* generalSettingsWidget = new QWidget(this);
170     QGroupBox* generalSettingsGroupBox = new QGroupBox("City");
171     licenseStringLabel = new QLabel("");
172     licenseStringLabel->setWordWrap(true);
173     licenseStringLabel->setAlignment(Qt::AlignCenter);
175     generalSettingsGroupBox->setLayout(cityLayout);
177     QVBoxLayout *generalSettingsLayout = new QVBoxLayout(this);
178     generalSettingsLayout->addWidget(generalSettingsGroupBox);
179     generalSettingsLayout->addStretch();
180     generalSettingsLayout->addWidget(licenseStringLabel, 0, Qt::AlignCenter);
181     generalSettingsLayout->addStretch();
183     generalSettingsWidget->setLayout(generalSettingsLayout);
185     return generalSettingsWidget;
188 void SettingScreen::setLicenseString(QString licenseString)
190     licenseStringLabel->setText(licenseString);
193 QWidget* SettingScreen::buildFormatSettingsWidget()
195     // create button to select Fahrenheit
196     FButton = new QPushButton("°F", (this));
197     //FButton->setMaximumWidth(150);
198     FButton->setObjectName("FButton");
199     FButton->setCheckable(true);
200     FButton->setFocusPolicy(Qt::NoFocus);
201     connect(FButton, SIGNAL(clicked()), this, SLOT(setTempFormatF()));
203     // create button to select Celsius
204     CButton = new QPushButton("°C", (this));
205     //CButton->setMaximumWidth(150);
206     CButton->setObjectName("CButton");
207     CButton->setCheckable(true);
208     CButton->setFocusPolicy(Qt::NoFocus);
209     connect(CButton, SIGNAL(clicked()), this, SLOT(setTempFormatC()));
211     // create button to select 12h time format
212     Button12h = new QPushButton("12h", (this));
213     Button12h->setObjectName("Button12h");
214     Button12h->setCheckable(true);
215     Button12h->setFocusPolicy(Qt::NoFocus);
216     connect(Button12h, SIGNAL(clicked()), this, SLOT(setTimeFormat12h()));
218     // create button to select 24h time format
219     Button24h = new QPushButton("24h", (this));
220     Button24h->setObjectName("Button24h");
221     Button24h->setCheckable(true);
222     Button24h->setFocusPolicy(Qt::NoFocus);
223     connect(Button24h, SIGNAL(clicked()), this, SLOT(setTimeFormat24h()));
225     // create layout
226     QHBoxLayout *unitLayout = new QHBoxLayout;
227     unitLayout->addSpacing(100);
228     unitLayout->addWidget(FButton);
229     unitLayout->addWidget(CButton);
230     unitLayout->addSpacing(100);
231     unitLayout->setSpacing(0);
232     unitLayout->setContentsMargins(0,0,0,0);
234     // create layout
235     QHBoxLayout *timeFormatLayout = new QHBoxLayout;
236     timeFormatLayout->addSpacing(100);
237     timeFormatLayout->addWidget(Button12h);
238     timeFormatLayout->addWidget(Button24h);
239     timeFormatLayout->addSpacing(100);
240     timeFormatLayout->setSpacing(0);
241     timeFormatLayout->setContentsMargins(0,0,0,0);
243     QGroupBox *tempFormatGroupBox = new QGroupBox("Temperature", this);
244     QGroupBox *timeFormatGroupBox = new QGroupBox("Time Format", this);
246     tempFormatGroupBox->setLayout(unitLayout);
247     timeFormatGroupBox->setLayout(timeFormatLayout);
249     QVBoxLayout *formatSettingsLayout = new QVBoxLayout(this);
250     formatSettingsLayout->addWidget(tempFormatGroupBox);
251     formatSettingsLayout->addWidget(timeFormatGroupBox);
253     QWidget *formatSettingsWidget = new QWidget(this);
254     formatSettingsWidget->setLayout(formatSettingsLayout);
256     return formatSettingsWidget;
261 void SettingScreen::setTempFormatF()
263     CButton->setDisabled(false);
264     m_globalSettings->setTemperatureFormat(GlobalSettings::TempFormatF);
265     CButton->setChecked(false);
266     FButton->setChecked(true);
267     FButton->setDisabled(true);
270 void SettingScreen::setTempFormatC()
272     FButton->setDisabled(false);
273     m_globalSettings->setTemperatureFormat(GlobalSettings::TempFormatC);
274     FButton->setChecked(false);
275     CButton->setChecked(true);
276     CButton->setDisabled(true);
279 void SettingScreen::setTimeFormat12h()
281     Button24h->setDisabled(false);
282     m_globalSettings->setTimeFormat(GlobalSettings::TimeFormat12h);
283     Button24h->setChecked(false);
284     Button12h->setChecked(true);
285     Button12h->setDisabled(true);
288 void SettingScreen::setTimeFormat24h()
290     Button12h->setDisabled(false);
291     m_globalSettings->setTimeFormat(GlobalSettings::TimeFormat24h);
292     Button12h->setChecked(false);
293     Button24h->setChecked(true);
294     Button24h->setDisabled(true);
297 void SettingScreen::commitChanges()
299     m_globalSettings->setProxyInfo(m_proxyHostLineEdit->text(),m_proxyPortLineEdit->text().toInt());
300     emit(valueChanged());
302     QString city = cityBox->currentText();
303     m_globalSettings->setCurrentCity(city);
304     emit cityChanged();
306     close();
309 void SettingScreen::launchWifiConfigure()
311     configureWifiButton->setEnabled(false);
312     m_wifiConfigureProcess.start("wpatest.sh wpa_gui-e -geometry 480x240+0+0");
315 void SettingScreen::wifiConfigureFinished(int, QProcess::ExitStatus)
317     configureWifiButton->setEnabled(true);