Some refactoring in SettingsDlg
[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(this);
20     m_remoteNameLayout = new QHBoxLayout(this);
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     m_layout->addLayout(m_remoteNameLayout);
61     m_layout->addLayout(m_btnLayout);
62     this->setLayout(m_layout);
63
64     QString selectedRemote = QSettings(this).value("remoteName", "").toString();
65     if (selectedRemote == "") {
66         m_remoteNameLabel->setText("No remote selected");
67         enableRateBtns(false);
68     } else {
69         setRemote(selectedRemote);
70         m_remote.updateInfo();
71     }
72 }
73
74 SettingsDlg::~SettingsDlg()
75 {
76     delete m_advSettingsBtn;
77     delete m_selectRemoteBtn;
78     delete m_rateUpBtn;
79     delete m_rateDownBtn;
80     delete m_aboutBtn;
81     delete m_remoteNameLabel;
82     delete m_ratingLabel;
83     delete m_btnLayout;
84     delete m_remoteNameLayout;
85     delete m_layout;
86 }
87
88 void SettingsDlg::showAdvSettingsDlg()
89 {
90     AdvSettingsDlg dlg(this);
91     dlg.exec();
92 }
93
94 void SettingsDlg::showSelectRemoteDlg()
95 {
96     SelectRemoteDlg dlg(this);
97     connect(&dlg, SIGNAL(remoteChanged(Remote)),
98             this, SLOT(setRemote(Remote)));
99     dlg.exec();
100 }
101
102 void SettingsDlg::showAboutDlg()
103 {
104     AboutDlg dlg(this);
105     dlg.exec();
106 }
107
108 void SettingsDlg::setRemote(Remote r)
109 {
110     m_remote = r;
111     processRemoteChange();
112     updateRemoteInfo();
113 }
114
115 void SettingsDlg::setRemote(const QString &name)
116 {
117     m_remote = Remote(name);
118     processRemoteChange();
119     m_remote.updateInfo();  // request update from server
120 }
121
122 void SettingsDlg::processRemoteChange()
123 {
124     connect(&m_remote, SIGNAL(infoUpdated()),
125             this, SLOT(updateRemoteInfo()));
126     enableRateBtns();
127 }
128
129 void SettingsDlg::updateRemoteInfo()
130 {
131     m_remoteNameLabel->setText(m_remote.mfg() + " " + m_remote.name());
132     m_ratingLabel->setText(tr("Rating") + ": "
133             + QString::number(m_remote.rating()));
134 }
135
136 void SettingsDlg::rateUpClicked()
137 {
138     m_remote.sendRating(Rating::Up);
139     processRatingSent();
140 }
141
142 void SettingsDlg::rateDownClicked()
143 {
144     m_remote.sendRating(Rating::Down);
145     processRatingSent();
146 }
147
148 void SettingsDlg::processRatingSent()
149 {
150     enableRateBtns(false);
151     m_remote.updateInfo();
152 }
153
154 void SettingsDlg::enableRateBtns(bool enable)
155 {
156     m_rateUpBtn->setEnabled(enable);
157     m_rateDownBtn->setEnabled(enable);
158 }
159
160