d03a449d1e11cac710eb9fa00027ec9aad63e91d
[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 <QMaemo5ValueButton>
31 #include <QMaemo5TimePickSelector>
32 #include "themeschedulersettings.h"
33 #include "themescheduler.h"
34 #include "themepicker.h"
35 #include "buttonbox.h"
36
37 namespace
38 {
39     QString const TIME_FORMAT = "hh:mm";
40 }
41
42 ThemeSchedulerSettings::ThemeSchedulerSettings(QWidget* parent):
43 QDialog(parent), currentWidget_(0), addDialog_(0), itemList_(0)
44 {
45     setWindowTitle(tr("Theme scheduler"));
46
47     QHBoxLayout* layout = new QHBoxLayout;
48     layout_ = new QVBoxLayout;
49
50     enabled_ = new QCheckBox(tr("Enabled"));
51     enabled_->setChecked(ThemeScheduler::instance().isEnabled());
52
53     QLabel* info = new QLabel(tr("Scheduled themes:"));
54
55     layout_->addWidget(enabled_);
56     layout_->addWidget(info);
57
58     loadItems();
59
60     ButtonBox* buttons = new ButtonBox;
61     connect(buttons->addButton(tr("Save"), QDialogButtonBox::AcceptRole), SIGNAL(clicked(bool)), this, SLOT(saveSettings()));
62     connect(buttons->addButton(tr("Add new theme"), QDialogButtonBox::ActionRole), SIGNAL(clicked(bool)), this, SLOT(openAddDialog()));
63     connect(buttons->addButton(tr("Clear scheduler"), QDialogButtonBox::ActionRole), SIGNAL(clicked(bool)), this, SLOT(clearScheduler()));
64
65     layout->addLayout(layout_, Qt::AlignLeft);
66     layout->addWidget(buttons);
67
68     setLayout(layout);
69
70 }
71
72 void ThemeSchedulerSettings::openAddDialog()
73 {
74     if(!addDialog_)
75     {
76         addDialog_ = new QDialog(this);
77         addDialog_->setWindowTitle("Add scheduled theme");
78         QHBoxLayout* layout = new QHBoxLayout;
79         QVBoxLayout* left = new QVBoxLayout;
80         timeButton_ = new QMaemo5ValueButton(tr("Start time"));
81         timeButton_->setValueLayout(QMaemo5ValueButton::ValueBesideText);
82         pickSelector_ = new QMaemo5TimePickSelector;
83         timeButton_->setPickSelector(pickSelector_);
84         themePicker_ = new ThemePicker(tr("Theme"));
85
86         ButtonBox* buttons = new ButtonBox;
87         connect(buttons->addButton(tr("Add"), QDialogButtonBox::AcceptRole), SIGNAL(clicked(bool)), this, SLOT(addScheduledTheme()));
88
89         left->addWidget(timeButton_);
90         left->addWidget(themePicker_);
91
92         layout->addLayout(left);
93         layout->addWidget(buttons);
94
95         addDialog_->setLayout(layout);
96
97     }
98
99     pickSelector_->setCurrentTime(QTime::currentTime());
100     addDialog_->show();
101 }
102
103 void ThemeSchedulerSettings::addScheduledTheme()
104 {
105     QTime time = QTime::fromString(timeButton_->valueText(), TIME_FORMAT);
106     QString theme = themePicker_->value().toString();
107     ThemeScheduler::instance().addItem(time, theme);
108     loadItems();
109     addDialog_->hide();
110 }
111
112 void ThemeSchedulerSettings::clearScheduler()
113 {
114     ThemeScheduler::instance().clear();
115     loadItems();
116 }
117
118 void ThemeSchedulerSettings::loadItems()
119 {
120     ThemeScheduler::instance().getItems(items_);
121     itemList_ = 0;
122
123     if(currentWidget_)
124     {
125         layout_->removeWidget(currentWidget_);
126         delete currentWidget_;
127         currentWidget_ = 0;
128     }
129
130     if(items_.isEmpty())
131     {
132         QLabel* label = new QLabel(tr("There are currently no scheduled themes."));
133         label->setWordWrap(true);
134         label->setAlignment(Qt::AlignLeft | Qt::AlignTop);
135         currentWidget_ = label;
136         layout_->addWidget(label);
137     }
138     else
139     {
140         itemList_ = new QListWidget;
141         itemList_->setContextMenuPolicy(Qt::CustomContextMenu);
142         connect(itemList_, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showContextMenu(const QPoint&)));
143
144         for(int i = 0; i < items_.size(); i++)
145         {
146             const QTime* endTime = 0;
147
148             if(i < items_.size() - 1)
149             {
150                 endTime = &(items_.at(i + 1).time);
151             }
152             else
153             {
154                 endTime = &(items_.at(0).time);
155             }
156
157             QString theme = items_.at(i).theme;
158             QString text = items_.at(i).time.toString(TIME_FORMAT) + " - " +
159                               endTime->toString(TIME_FORMAT) + ": " +
160                               theme.at(0).toUpper() + theme.mid(1);
161             QListWidgetItem* item = new QListWidgetItem(text);
162             item->setData(Qt::UserRole, items_.at(i).time.toString(TIME_FORMAT));
163             itemList_->addItem(item);
164         }
165
166         currentWidget_ = itemList_;
167         layout_->addWidget(itemList_);
168     }
169
170     QApplication::processEvents();
171     adjustSize();
172     QApplication::processEvents();
173
174 }
175
176 void ThemeSchedulerSettings::saveSettings()
177 {
178     ThemeScheduler::instance().setEnabled(enabled_->isChecked());
179     hide();
180 }
181
182 void ThemeSchedulerSettings::showContextMenu(QPoint const& point)
183 {
184     if(!itemList_)
185     {
186         return;
187     }
188
189     QMenu* menu = new QMenu(itemList_);
190     menu->addAction(tr("Remove"), this, SLOT(removeCurrent()));
191     connect(menu, SIGNAL(aboutToHide()), this, SLOT(removeSelection()));
192     menu->popup(itemList_->mapToGlobal(point));
193 }
194
195 void ThemeSchedulerSettings::removeCurrent()
196 {
197     if(itemList_)
198     {
199         QListWidgetItem* item = itemList_->currentItem();
200
201         if(item)
202         {
203             ThemeScheduler::instance().removeItem(QTime::fromString(item->data(Qt::UserRole).toString(), TIME_FORMAT));
204             loadItems();
205         }
206     }
207 }
208
209 void ThemeSchedulerSettings::removeSelection()
210 {
211     if(itemList_)
212     {
213         itemList_->clearSelection();
214     }
215 }