]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - apps/thermostat-demo.git/blob - settingscreen.cpp
thermostat-demo: Add license information
[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);
125     QLabel *m_remoteListenPort = new QLabel("Remote Listen Port: ", this);
126     m_remoteListenPortLineEdit = new QKeyboardLineEdit;
128     QFormLayout* remoteListenLayout = new QFormLayout;
129     remoteListenLayout->addRow(m_remoteListenPort, m_remoteListenPortLineEdit);
131     QGroupBox* remoteSettingsGroupBox = new QGroupBox("Remote Settings");
132     remoteSettingsGroupBox->setLayout(remoteListenLayout);
134     QVBoxLayout *networkSettingsLayout = new QVBoxLayout(this);
135     networkSettingsLayout->addWidget(proxySettingsGroupBox);
136     //networkSettingsLayout->addWidget(remoteSettingsGroupBox);
137     networkSettingsLayout->addStretch();
139     networkSettingsWidget->setLayout(networkSettingsLayout);
141     return networkSettingsWidget;
144 QWidget* SettingScreen::buildGeneralSettingsWidget()
146     // create box to contain all possible cities
147     cityBox = new QComboBox;
148     cityBox->setFocusPolicy(Qt::NoFocus);
149     cityBox->addItem("Dallas,TX");
150     cityBox->addItem("Tokyo,Japan");
151     cityBox->addItem("Chicago,IL");
152     cityBox->addItem("Hong Kong,China");
153     cityBox->addItem("Munich,Germany");
154     cityBox->addItem("Taipei,Taiwan");
155     cityBox->addItem("San Jose,CA");
156     cityBox->addItem("Shanghai,China");
157     cityBox->addItem("New York,NY");
158     cityBox->addItem("Bangalore,India");
159     cityBox->addItem("Sao Paulo,Brazil");
160     cityBox->addItem("Seoul,South Korea");
161     cityBox->addItem("Moscow,Russia");
162     cityBox->addItem("Mexico City,Mexico");
163     cityBox->addItem("Johannesburg,South Africa");
166     QVBoxLayout *cityLayout = new QVBoxLayout;
167     cityLayout->addStretch();
168     cityLayout->addWidget(cityBox);
169     cityLayout->addStretch();
171     QWidget* generalSettingsWidget = new QWidget(this);
173     QGroupBox* generalSettingsGroupBox = new QGroupBox("City");
174     licenseStringLabel = new QLabel("");
175     licenseStringLabel->setWordWrap(true);
176     licenseStringLabel->setAlignment(Qt::AlignCenter);
178     generalSettingsGroupBox->setLayout(cityLayout);
180     QVBoxLayout *generalSettingsLayout = new QVBoxLayout(this);
181     generalSettingsLayout->addWidget(generalSettingsGroupBox);
182     generalSettingsLayout->addStretch();
183     generalSettingsLayout->addWidget(licenseStringLabel, 0, Qt::AlignCenter);
184     generalSettingsLayout->addStretch();
186     generalSettingsWidget->setLayout(generalSettingsLayout);
188     return generalSettingsWidget;
191 void SettingScreen::setLicenseString(QString licenseString)
193     licenseStringLabel->setText(licenseString);
196 QWidget* SettingScreen::buildFormatSettingsWidget()
198     // create button to select Fahrenheit
199     FButton = new QPushButton("°F", (this));
200     //FButton->setMaximumWidth(150);
201     FButton->setObjectName("FButton");
202     FButton->setCheckable(true);
203     FButton->setFocusPolicy(Qt::NoFocus);
204     connect(FButton, SIGNAL(clicked()), this, SLOT(setTempFormatF()));
206     // create button to select Celsius
207     CButton = new QPushButton("°C", (this));
208     //CButton->setMaximumWidth(150);
209     CButton->setObjectName("CButton");
210     CButton->setCheckable(true);
211     CButton->setFocusPolicy(Qt::NoFocus);
212     connect(CButton, SIGNAL(clicked()), this, SLOT(setTempFormatC()));
214     // create button to select 12h time format
215     Button12h = new QPushButton("12h", (this));
216     Button12h->setObjectName("Button12h");
217     Button12h->setCheckable(true);
218     Button12h->setFocusPolicy(Qt::NoFocus);
219     connect(Button12h, SIGNAL(clicked()), this, SLOT(setTimeFormat12h()));
221     // create button to select 24h time format
222     Button24h = new QPushButton("24h", (this));
223     Button24h->setObjectName("Button24h");
224     Button24h->setCheckable(true);
225     Button24h->setFocusPolicy(Qt::NoFocus);
226     connect(Button24h, SIGNAL(clicked()), this, SLOT(setTimeFormat24h()));
228     // create layout
229     QHBoxLayout *unitLayout = new QHBoxLayout;
230     unitLayout->addSpacing(100);
231     unitLayout->addWidget(FButton);
232     unitLayout->addWidget(CButton);
233     unitLayout->addSpacing(100);
234     unitLayout->setSpacing(0);
235     unitLayout->setContentsMargins(0,0,0,0);
237     // create layout
238     QHBoxLayout *timeFormatLayout = new QHBoxLayout;
239     timeFormatLayout->addSpacing(100);
240     timeFormatLayout->addWidget(Button12h);
241     timeFormatLayout->addWidget(Button24h);
242     timeFormatLayout->addSpacing(100);
243     timeFormatLayout->setSpacing(0);
244     timeFormatLayout->setContentsMargins(0,0,0,0);
246     QGroupBox *tempFormatGroupBox = new QGroupBox("Temperature", this);
247     QGroupBox *timeFormatGroupBox = new QGroupBox("Time Format", this);
249     tempFormatGroupBox->setLayout(unitLayout);
250     timeFormatGroupBox->setLayout(timeFormatLayout);
252     QVBoxLayout *formatSettingsLayout = new QVBoxLayout(this);
253     formatSettingsLayout->addWidget(tempFormatGroupBox);
254     formatSettingsLayout->addWidget(timeFormatGroupBox);
256     QWidget *formatSettingsWidget = new QWidget(this);
257     formatSettingsWidget->setLayout(formatSettingsLayout);
259     return formatSettingsWidget;
264 void SettingScreen::setTempFormatF()
266     CButton->setDisabled(false);
267     m_globalSettings->setTemperatureFormat(GlobalSettings::TempFormatF);
268     CButton->setChecked(false);
269     FButton->setChecked(true);
270     FButton->setDisabled(true);
273 void SettingScreen::setTempFormatC()
275     FButton->setDisabled(false);
276     m_globalSettings->setTemperatureFormat(GlobalSettings::TempFormatC);
277     FButton->setChecked(false);
278     CButton->setChecked(true);
279     CButton->setDisabled(true);
282 void SettingScreen::setTimeFormat12h()
284     Button24h->setDisabled(false);
285     m_globalSettings->setTimeFormat(GlobalSettings::TimeFormat12h);
286     Button24h->setChecked(false);
287     Button12h->setChecked(true);
288     Button12h->setDisabled(true);
291 void SettingScreen::setTimeFormat24h()
293     Button12h->setDisabled(false);
294     m_globalSettings->setTimeFormat(GlobalSettings::TimeFormat24h);
295     Button12h->setChecked(false);
296     Button24h->setChecked(true);
297     Button24h->setDisabled(true);
300 void SettingScreen::commitChanges()
302     m_globalSettings->setProxyInfo(m_proxyHostLineEdit->text(),m_proxyPortLineEdit->text().toInt());
303     emit(valueChanged());
305     QString city = cityBox->currentText();
306     m_globalSettings->setCurrentCity(city);
307     emit cityChanged();
309     close();