More tests for Remote class
[irwi] / src / settingsdlg.cpp
1 #include "settingsdlg.h"
2 #include "advsettingsdlg.h"
3 #include "selectremotedlg.h"
4 #include "aboutdlg.h"
5
6 #include <QHBoxLayout>
7 #include <QVBoxLayout>
8 #include <QWidget>
9 #include <QDialog>
10 #include <QPushButton>
11 #include <QLabel>
12 #include <QDebug>
13
14 SettingsDlg::SettingsDlg(QWidget *parent)
15     : QDialog(parent)
16 {
17     QSettings settings(this);
18     m_layout = new QVBoxLayout(this);
19     m_btnLayout = new QHBoxLayout(m_layout);
20     m_remoteNameLayout = new QHBoxLayout(m_layout);
21     
22     m_advSettingsBtn = new QPushButton(tr("Advanced"), this);
23     m_selectRemoteBtn = new QPushButton(tr("Select remote"), this);
24     m_aboutBtn = new QPushButton(tr("About"), this);
25     m_rateUpBtn = new QPushButton(
26             QIcon(settings.value("rateUpIcon",
27                 "/usr/share/icons/hicolor/48x48/hildon/chat_smiley_happy.png").
28                 toString()),
29             "", this);
30     m_rateDownBtn = new QPushButton(
31             QIcon(settings.value("rateDownIcon",
32                 "/usr/share/icons/hicolor/48x48/hildon/chat_smiley_sad.png").
33                 toString()),
34             "", this);
35     m_rateUpBtn->setMaximumSize(72, 72);
36     m_rateDownBtn->setMaximumSize(72, 72);
37
38     m_btnLayout->addWidget(m_advSettingsBtn);
39     m_btnLayout->addWidget(m_selectRemoteBtn);
40     m_btnLayout->addWidget(m_aboutBtn);
41
42     m_remoteNameLabel = new QLabel(this);
43     m_ratingLabel = new QLabel(this);
44     m_remoteNameLayout->addWidget(m_remoteNameLabel);
45     m_remoteNameLayout->addWidget(m_ratingLabel);
46     m_remoteNameLayout->addWidget(m_rateUpBtn);
47     m_remoteNameLayout->addWidget(m_rateDownBtn);
48
49     connect(m_advSettingsBtn, SIGNAL(clicked()),
50             this, SLOT(showAdvSettingsDlg()));
51     connect(m_selectRemoteBtn, SIGNAL(clicked()),
52             this, SLOT(showSelectRemoteDlg()));
53     connect(m_aboutBtn, SIGNAL(clicked()),
54             this, SLOT(showAboutDlg()));
55     connect(m_rateUpBtn, SIGNAL(clicked()),
56             this, SLOT(rateUpClicked()));
57     connect(m_rateDownBtn, SIGNAL(clicked()),
58             this, SLOT(rateDownClicked()));
59  
60     this->setLayout(m_layout);
61
62     QString selectedRemote = QSettings(this).value("remoteName", "").toString();
63     if (selectedRemote == "") {
64         m_remoteNameLabel->setText("No remote selected");
65         enableRateBtns(false);
66     } else {
67         setRemote(selectedRemote);
68         m_remote.updateInfo();
69         setBusy();
70     }
71 }
72
73 SettingsDlg::~SettingsDlg()
74 {
75     delete m_advSettingsBtn;
76     delete m_selectRemoteBtn;
77     delete m_rateUpBtn;
78     delete m_rateDownBtn;
79     delete m_aboutBtn;
80     delete m_remoteNameLabel;
81     delete m_ratingLabel;
82     delete m_btnLayout;
83     delete m_remoteNameLayout;
84     delete m_layout;
85 }
86
87 void SettingsDlg::setBusy(bool busy)
88 {
89     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, busy);
90     setEnabled(!busy);
91 }
92
93 void SettingsDlg::showAdvSettingsDlg()
94 {
95     AdvSettingsDlg dlg(this);
96     dlg.exec();
97 }
98
99 void SettingsDlg::showSelectRemoteDlg()
100 {
101     SelectRemoteDlg dlg(this);
102     connect(&dlg, SIGNAL(remoteChanged(Remote)),
103             this, SLOT(setRemote(Remote)));
104     dlg.exec();
105 }
106
107 void SettingsDlg::showAboutDlg()
108 {
109     AboutDlg dlg(this);
110     dlg.exec();
111 }
112
113 void SettingsDlg::setRemote(Remote r)
114 {
115     m_remote = r;
116     processRemoteChange();
117     updateRemoteInfo();
118 }
119
120 void SettingsDlg::setRemote(const QString &name)
121 {
122     setBusy();
123     m_remote = Remote(name);
124     processRemoteChange();
125     m_remote.updateInfo();  // request update from server
126 }
127
128 void SettingsDlg::processRemoteChange()
129 {
130     connect(&m_remote, SIGNAL(infoUpdated()),
131             this, SLOT(updateRemoteInfo()));
132     enableRateBtns();
133 }
134
135 void SettingsDlg::updateRemoteInfo()
136 {
137     setBusy(false);
138     m_remoteNameLabel->setText(m_remote.mfg() + " " + m_remote.name());
139     m_ratingLabel->setText(tr("Rating") + ": "
140             + QString::number(m_remote.rating()));
141 }
142
143 void SettingsDlg::rateUpClicked()
144 {
145     m_remote.sendRating(Rating::Up);
146     processRatingSent();
147 }
148
149 void SettingsDlg::rateDownClicked()
150 {
151     m_remote.sendRating(Rating::Down);
152     processRatingSent();
153 }
154
155 void SettingsDlg::processRatingSent()
156 {
157     enableRateBtns(false);
158     m_remote.updateInfo();
159 }
160
161 void SettingsDlg::enableRateBtns(bool enable)
162 {
163     m_rateUpBtn->setEnabled(enable);
164     m_rateDownBtn->setEnabled(enable);
165 }
166
167