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 if(!ThemeScheduler::instance().isEmpty())
117 ThemeScheduler::instance().clear();
122 void ThemeSchedulerSettings::loadItems()
124 ThemeScheduler::instance().getItems(items_);
129 layout_->removeWidget(currentWidget_);
130 delete currentWidget_;
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);
144 itemList_ = new QListWidget;
145 itemList_->setContextMenuPolicy(Qt::CustomContextMenu);
146 connect(itemList_, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showContextMenu(const QPoint&)));
148 for(int i = 0; i < items_.size(); i++)
150 const QTime* endTime = 0;
152 if(i < items_.size() - 1)
154 endTime = &(items_.at(i + 1).time);
158 endTime = &(items_.at(0).time);
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);
170 currentWidget_ = itemList_;
171 layout_->addWidget(itemList_);
175 void ThemeSchedulerSettings::saveSettings()
177 bool enabled = enabled_->isChecked();
179 if(enabled && ThemeScheduler::instance().isEmpty())
181 QMaemo5InformationBox::information(this, tr("Theme scheduler is empty."));
185 ThemeScheduler::instance().setEnabled(enabled);
194 void ThemeSchedulerSettings::showContextMenu(QPoint const& point)
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));
207 void ThemeSchedulerSettings::removeCurrent()
211 QListWidgetItem* item = itemList_->currentItem();
215 ThemeScheduler::instance().removeItem(QTime::fromString(item->data(Qt::UserRole).toString(), TIME_FORMAT));
221 void ThemeSchedulerSettings::removeSelection()
225 itemList_->clearSelection();
229 void ThemeSchedulerSettings::setVisible(bool visible)
233 enabled_->setChecked(ThemeScheduler::instance().isEnabled());
236 QDialog::setVisible(visible);