b64548f078b77eb7fe02206d30e02c07ca7c3cfe
[jspeed] / src / themeselector.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/QDir>
20 #include <QtCore/QDebug>
21 #include <QtCore/QString>
22 #include <QtCore/QStringList>
23 #include <QtGui/QFileDialog>
24 #include <QtGui/QDialogButtonBox>
25 #include <QtGui/QPushButton>
26 #include <QtGui/QHBoxLayout>
27 #include <QtGui/QVBoxLayout>
28 #include <QtGui/QMessageBox>
29 #include <QMaemo5InformationBox>
30 #include "themeselector.h"
31 #include "themepicker.h"
32 #include "themeloader.h"
33 #include "settings.h"
34 #include "themeschedulersettings.h"
35
36 ThemeSelector::ThemeSelector(QWidget* parent): QDialog(parent), themeScheduler_(0)
37 {
38     setWindowTitle(tr("Select theme"));
39
40     QHBoxLayout* layout = new QHBoxLayout;
41     QVBoxLayout* left = new QVBoxLayout;
42     QHBoxLayout* first = new QHBoxLayout;
43
44     QPushButton* saveButton = new QPushButton(tr("Save"));
45     connect(saveButton, SIGNAL(clicked(bool)), this, SLOT(saveTheme()));
46     QDialogButtonBox* buttons = new QDialogButtonBox;
47     buttons->setCenterButtons(false);
48     buttons->setOrientation(Qt::Vertical);
49     buttons->addButton(saveButton, QDialogButtonBox::AcceptRole);
50
51     selector_ = new ThemePicker(tr("Theme"), this);
52     connect(selector_, SIGNAL(selected()), this, SLOT(disableScheduler()));
53
54     QPushButton* loadButton = new QPushButton(tr("Import"));
55     connect(loadButton, SIGNAL(clicked(bool)), this, SLOT(loadFromFile()));
56
57     QPushButton* scheduler = new QPushButton(tr("Theme scheduler"));
58     connect(scheduler, SIGNAL(clicked(bool)), this, SLOT(openScheduler()));
59
60     first->addWidget(selector_, Qt::AlignLeft);
61     first->addWidget(loadButton);
62
63     left->addLayout(first);
64     left->addWidget(scheduler);
65
66     layout->addLayout(left, Qt::AlignLeft);
67     layout->addWidget(buttons);
68
69     setLayout(layout);
70
71 }
72
73 void ThemeSelector::saveTheme()
74 {
75     QString theme = selector_->value().toString();
76     Settings::instance().setValue("theme", theme);
77     hide();
78     emit themeChanged();
79 }
80
81 void ThemeSelector::loadFromFile()
82 {
83     QString file;
84
85     if(selector_->importFile(ThemeLoader::getThemeDir(),
86                           "Theme files",
87                           "*" + ThemeLoader::getThemeSuffix(),
88                           false,
89                           &file))
90     {
91         selector_->loadThemes();
92         selector_->selectTheme(file);
93     }
94 }
95
96 void ThemeSelector::openScheduler()
97 {
98     if(!themeScheduler_)
99     {
100         themeScheduler_ = new ThemeSchedulerSettings(this);
101         connect(themeScheduler_, SIGNAL(themeChanged()), this, SIGNAL(themeChanged()));
102     }
103
104     themeScheduler_->show();
105 }
106
107 void ThemeSelector::disableScheduler()
108 {
109     if(ThemeScheduler::instance().isEnabled())
110     {
111         QMaemo5InformationBox::information(this, tr("Disabling theme scheduler..."), 1000);
112         ThemeScheduler::instance().setEnabled(false);
113     }
114 }