...
[jspeed] / src / themeschedulersettings.cpp
1 /*
2  * This file is part of jSpeed.
3  *
4  * jSpeed is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * jSpeed is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with jSpeed.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtCore/QDebug>
20 #include <QtGui/QMenu>
21 #include <QtGui/QApplication>
22 #include <QtGui/QHBoxLayout>
23 #include <QtGui/QVBoxLayout>
24 #include <QtGui/QCheckBox>
25 #include <QtGui/QLabel>
26 #include <QtGui/QScrollArea>
27 #include <QtGui/QDialogButtonBox>
28 #include <QtGui/QPushButton>
29 #include <QtGui/QListWidget>
30 #include <QMaemo5InformationBox>
31 #include <QMaemo5ValueButton>
32 #include <QMaemo5TimePickSelector>
33 #include "themeschedulersettings.h"
34 #include "themescheduler.h"
35 #include "themepicker.h"
36 #include "buttonbox.h"
37
38 namespace
39 {
40     QString const TIME_FORMAT = "hh:mm";
41 }
42
43 ThemeSchedulerSettings::ThemeSchedulerSettings(QWidget* parent):
44 QDialog(parent), currentWidget_(0), addDialog_(0), itemList_(0)
45 {
46     setWindowTitle(tr("Theme scheduler"));
47
48     QHBoxLayout* layout = new QHBoxLayout;
49     layout_ = new QVBoxLayout;
50
51     enabled_ = new QCheckBox(tr("Enabled"));
52     enabled_->setChecked(ThemeScheduler::instance().isEnabled());
53
54     QLabel* info = new QLabel(tr("Scheduled themes:"));
55
56     layout_->addWidget(enabled_);
57     layout_->addWidget(info);
58
59     loadItems();
60
61     ButtonBox* buttons = new ButtonBox;
62     connect(buttons->addButton(tr("Save"), QDialogButtonBox::AcceptRole), SIGNAL(clicked(bool)), this, SLOT(saveSettings()));
63     connect(buttons->addButton(tr("Add new theme"), QDialogButtonBox::ActionRole), SIGNAL(clicked(bool)), this, SLOT(openAddDialog()));
64     connect(buttons->addButton(tr("Clear scheduler"), QDialogButtonBox::ActionRole), SIGNAL(clicked(bool)), this, SLOT(clearScheduler()));
65
66     layout->addLayout(layout_, Qt::AlignLeft);
67     layout->addWidget(buttons);
68
69     setLayout(layout);
70
71 }
72
73 void ThemeSchedulerSettings::openAddDialog()
74 {
75     if(!addDialog_)
76     {
77         addDialog_ = new QDialog(this);
78         addDialog_->setWindowTitle("Add scheduled theme");
79         QHBoxLayout* layout = new QHBoxLayout;
80         QVBoxLayout* left = new QVBoxLayout;
81         timeButton_ = new QMaemo5ValueButton(tr("Start time"));
82         timeButton_->setValueLayout(QMaemo5ValueButton::ValueBesideText);
83         pickSelector_ = new QMaemo5TimePickSelector;
84         timeButton_->setPickSelector(pickSelector_);
85         themePicker_ = new ThemePicker(tr("Theme"));
86
87         ButtonBox* buttons = new ButtonBox;
88         connect(buttons->addButton(tr("Add"), QDialogButtonBox::AcceptRole), SIGNAL(clicked(bool)), this, SLOT(addScheduledTheme()));
89
90         left->addWidget(timeButton_);
91         left->addWidget(themePicker_);
92
93         layout->addLayout(left);
94         layout->addWidget(buttons);
95
96         addDialog_->setLayout(layout);
97
98     }
99
100     pickSelector_->setCurrentTime(QTime::currentTime());
101     addDialog_->show();
102 }
103
104 void ThemeSchedulerSettings::addScheduledTheme()
105 {
106     QTime time = QTime::fromString(timeButton_->valueText(), TIME_FORMAT);
107     QString theme = themePicker_->value().toString();
108     ThemeScheduler::instance().addItem(time, theme);
109     loadItems();
110     addDialog_->hide();
111 }
112
113 void ThemeSchedulerSettings::clearScheduler()
114 {
115     if(!ThemeScheduler::instance().isEmpty())
116     {
117         ThemeScheduler::instance().clear();
118         loadItems();
119     }
120 }
121
122 void ThemeSchedulerSettings::loadItems()
123 {
124     ThemeScheduler::instance().getItems(items_);
125     itemList_ = 0;
126
127     if(currentWidget_)
128     {
129         layout_->removeWidget(currentWidget_);
130         delete currentWidget_;
131         currentWidget_ = 0;
132     }
133
134     if(items_.isEmpty())
135     {
136         QLabel* label = new QLabel(tr("There are currently no scheduled themes."));
137         label->setWordWrap(true);
138         label->setAlignment(Qt::AlignLeft | Qt::AlignTop);
139         currentWidget_ = label;
140         layout_->addWidget(label);
141     }
142     else
143     {
144         itemList_ = new QListWidget;
145         itemList_->setContextMenuPolicy(Qt::CustomContextMenu);
146         connect(itemList_, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showContextMenu(const QPoint&)));
147
148         for(int i = 0; i < items_.size(); i++)
149         {
150             const QTime* endTime = 0;
151
152             if(i < items_.size() - 1)
153             {
154                 endTime = &(items_.at(i + 1).time);
155             }
156             else
157             {
158                 endTime = &(items_.at(0).time);
159             }
160
161             QString theme = items_.at(i).theme;
162             QString text = items_.at(i).time.toString(TIME_FORMAT) + " - " +
163                               endTime->toString(TIME_FORMAT) + ": " +
164                               theme.at(0).toUpper() + theme.mid(1);
165             QListWidgetItem* item = new QListWidgetItem(text);
166             item->setData(Qt::UserRole, items_.at(i).time.toString(TIME_FORMAT));
167             itemList_->addItem(item);
168         }
169
170         currentWidget_ = itemList_;
171         layout_->addWidget(itemList_);
172     }
173 }
174
175 void ThemeSchedulerSettings::saveSettings()
176 {
177     bool enabled = enabled_->isChecked();
178
179     if(enabled && ThemeScheduler::instance().isEmpty())
180     {
181         QMaemo5InformationBox::information(this, tr("Theme scheduler is empty."));
182         return;
183     }
184
185     ThemeScheduler::instance().setEnabled(enabled);
186     hide();
187
188     if(enabled)
189     {
190         emit themeChanged();
191     }
192 }
193
194 void ThemeSchedulerSettings::showContextMenu(QPoint const& point)
195 {
196     if(!itemList_)
197     {
198         return;
199     }
200
201     QMenu* menu = new QMenu(itemList_);
202     menu->addAction(tr("Remove"), this, SLOT(removeCurrent()));
203     connect(menu, SIGNAL(aboutToHide()), this, SLOT(removeSelection()));
204     menu->popup(itemList_->mapToGlobal(point));
205 }
206
207 void ThemeSchedulerSettings::removeCurrent()
208 {
209     if(itemList_)
210     {
211         QListWidgetItem* item = itemList_->currentItem();
212
213         if(item)
214         {
215             ThemeScheduler::instance().removeItem(QTime::fromString(item->data(Qt::UserRole).toString(), TIME_FORMAT));
216             loadItems();
217         }
218     }
219 }
220
221 void ThemeSchedulerSettings::removeSelection()
222 {
223     if(itemList_)
224     {
225         itemList_->clearSelection();
226     }
227 }
228
229 void ThemeSchedulerSettings::setVisible(bool visible)
230 {
231     if(visible)
232     {
233         enabled_->setChecked(ThemeScheduler::instance().isEnabled());
234     }
235
236     QDialog::setVisible(visible);
237 }