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 <QMaemo5ValueButton>
31 #include <QMaemo5TimePickSelector>
32 #include "themeschedulersettings.h"
33 #include "themescheduler.h"
34 #include "themepicker.h"
35 #include "buttonbox.h"
39 QString const TIME_FORMAT = "hh:mm";
42 ThemeSchedulerSettings::ThemeSchedulerSettings(QWidget* parent):
43 QDialog(parent), currentWidget_(0), addDialog_(0), itemList_(0)
45 setWindowTitle(tr("Theme scheduler"));
47 QHBoxLayout* layout = new QHBoxLayout;
48 layout_ = new QVBoxLayout;
50 enabled_ = new QCheckBox(tr("Enabled"));
51 enabled_->setChecked(ThemeScheduler::instance().isEnabled());
53 QLabel* info = new QLabel(tr("Scheduled themes:"));
55 layout_->addWidget(enabled_);
56 layout_->addWidget(info);
60 ButtonBox* buttons = new ButtonBox;
61 buttons->addButton(tr("Save"), this, SLOT(saveSettings()), QDialogButtonBox::AcceptRole);
62 buttons->addButton(tr("Add new theme"), this, SLOT(openAddDialog()), QDialogButtonBox::ActionRole);
63 buttons->addButton(tr("Clear scheduler"), this, SLOT(clearScheduler()), QDialogButtonBox::ActionRole);
65 layout->addLayout(layout_, Qt::AlignLeft);
66 layout->addWidget(buttons);
72 void ThemeSchedulerSettings::openAddDialog()
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"));
86 ButtonBox* buttons = new ButtonBox;
87 buttons->addButton(tr("Add"), this, SLOT(addScheduledTheme()));
89 left->addWidget(timeButton_);
90 left->addWidget(themePicker_);
92 layout->addLayout(left);
93 layout->addWidget(buttons);
95 addDialog_->setLayout(layout);
99 pickSelector_->setCurrentTime(QTime::currentTime());
103 void ThemeSchedulerSettings::addScheduledTheme()
105 QTime time = QTime::fromString(timeButton_->valueText(), TIME_FORMAT);
106 QString theme = themePicker_->value().toString();
107 ThemeScheduler::instance().addItem(time, theme);
112 void ThemeSchedulerSettings::clearScheduler()
114 ThemeScheduler::instance().clear();
118 void ThemeSchedulerSettings::loadItems()
120 ThemeScheduler::instance().getItems(items_);
125 layout_->removeWidget(currentWidget_);
126 delete currentWidget_;
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);
140 itemList_ = new QListWidget;
141 itemList_->setContextMenuPolicy(Qt::CustomContextMenu);
142 connect(itemList_, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showContextMenu(const QPoint&)));
144 for(int i = 0; i < items_.size(); i++)
146 const QTime* endTime = 0;
148 if(i < items_.size() - 1)
150 endTime = &(items_.at(i + 1).time);
154 endTime = &(items_.at(0).time);
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);
166 currentWidget_ = itemList_;
167 layout_->addWidget(itemList_);
170 QApplication::processEvents();
172 QApplication::processEvents();
176 void ThemeSchedulerSettings::saveSettings()
178 ThemeScheduler::instance().setEnabled(enabled_->isChecked());
182 void ThemeSchedulerSettings::showContextMenu(QPoint const& point)
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));
195 void ThemeSchedulerSettings::removeCurrent()
199 QListWidgetItem* item = itemList_->currentItem();
203 ThemeScheduler::instance().removeItem(QTime::fromString(item->data(Qt::UserRole).toString(), TIME_FORMAT));
209 void ThemeSchedulerSettings::removeSelection()
213 itemList_->clearSelection();