2 * This file is part of jSpeed.
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.
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.
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/>.
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"
40 QString const TIME_FORMAT = "hh:mm";
43 ThemeSchedulerSettings::ThemeSchedulerSettings(QWidget* parent):
44 QDialog(parent), currentWidget_(0), addDialog_(0), itemList_(0)
46 setWindowTitle(tr("Theme scheduler"));
48 QHBoxLayout* layout = new QHBoxLayout;
49 layout_ = new QVBoxLayout;
51 enabled_ = new QCheckBox(tr("Enabled"));
52 enabled_->setChecked(ThemeScheduler::instance().isEnabled());
54 QLabel* info = new QLabel(tr("Scheduled themes:"));
56 layout_->addWidget(enabled_);
57 layout_->addWidget(info);
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()));
66 layout->addLayout(layout_, Qt::AlignLeft);
67 layout->addWidget(buttons);
73 void ThemeSchedulerSettings::openAddDialog()
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"));
87 ButtonBox* buttons = new ButtonBox;
88 connect(buttons->addButton(tr("Add"), QDialogButtonBox::AcceptRole), SIGNAL(clicked(bool)), this, SLOT(addScheduledTheme()));
90 left->addWidget(timeButton_);
91 left->addWidget(themePicker_);
93 layout->addLayout(left);
94 layout->addWidget(buttons);
96 addDialog_->setLayout(layout);
100 pickSelector_->setCurrentTime(QTime::currentTime());
104 void ThemeSchedulerSettings::addScheduledTheme()
106 QTime time = QTime::fromString(timeButton_->valueText(), TIME_FORMAT);
107 QString theme = themePicker_->value().toString();
108 ThemeScheduler::instance().addItem(time, theme);
113 void ThemeSchedulerSettings::clearScheduler()
115 ThemeScheduler::instance().clear();
119 void ThemeSchedulerSettings::loadItems()
121 ThemeScheduler::instance().getItems(items_);
126 layout_->removeWidget(currentWidget_);
127 delete currentWidget_;
133 QLabel* label = new QLabel(tr("There are currently no scheduled themes."));
134 label->setWordWrap(true);
135 label->setAlignment(Qt::AlignLeft | Qt::AlignTop);
136 currentWidget_ = label;
137 layout_->addWidget(label);
141 itemList_ = new QListWidget;
142 itemList_->setContextMenuPolicy(Qt::CustomContextMenu);
143 connect(itemList_, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showContextMenu(const QPoint&)));
145 for(int i = 0; i < items_.size(); i++)
147 const QTime* endTime = 0;
149 if(i < items_.size() - 1)
151 endTime = &(items_.at(i + 1).time);
155 endTime = &(items_.at(0).time);
158 QString theme = items_.at(i).theme;
159 QString text = items_.at(i).time.toString(TIME_FORMAT) + " - " +
160 endTime->toString(TIME_FORMAT) + ": " +
161 theme.at(0).toUpper() + theme.mid(1);
162 QListWidgetItem* item = new QListWidgetItem(text);
163 item->setData(Qt::UserRole, items_.at(i).time.toString(TIME_FORMAT));
164 itemList_->addItem(item);
167 currentWidget_ = itemList_;
168 layout_->addWidget(itemList_);
171 QApplication::processEvents();
173 QApplication::processEvents();
177 void ThemeSchedulerSettings::saveSettings()
179 bool enabled = enabled_->isChecked();
181 if(enabled && ThemeScheduler::instance().isEmpty())
183 QMaemo5InformationBox::information(this, tr("Theme scheduler is empty."));
187 ThemeScheduler::instance().setEnabled(enabled);
196 void ThemeSchedulerSettings::showContextMenu(QPoint const& point)
203 QMenu* menu = new QMenu(itemList_);
204 menu->addAction(tr("Remove"), this, SLOT(removeCurrent()));
205 connect(menu, SIGNAL(aboutToHide()), this, SLOT(removeSelection()));
206 menu->popup(itemList_->mapToGlobal(point));
209 void ThemeSchedulerSettings::removeCurrent()
213 QListWidgetItem* item = itemList_->currentItem();
217 ThemeScheduler::instance().removeItem(QTime::fromString(item->data(Qt::UserRole).toString(), TIME_FORMAT));
223 void ThemeSchedulerSettings::removeSelection()
227 itemList_->clearSelection();
231 void ThemeSchedulerSettings::setVisible(bool visible)
235 enabled_->setChecked(ThemeScheduler::instance().isEnabled());
238 QDialog::setVisible(visible);