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