f559d636ac5c3e7398e50cfbb62e02600fd6a5c5
[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), buttonClicked_(false), 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(clicked(bool)), this, SLOT(enableDisableScheduler()));
53     connect(selector_, SIGNAL(selected()), this, SLOT(disableScheduler()));
54
55     QPushButton* loadButton = new QPushButton(tr("Import"));
56     connect(loadButton, SIGNAL(clicked(bool)), this, SLOT(loadFromFile()));
57
58     QPushButton* scheduler = new QPushButton(tr("Theme scheduler"));
59     connect(scheduler, SIGNAL(clicked(bool)), this, SLOT(openScheduler()));
60
61     first->addWidget(selector_, Qt::AlignLeft);
62     first->addWidget(loadButton);
63
64     left->addLayout(first);
65     left->addWidget(scheduler);
66
67     layout->addLayout(left, Qt::AlignLeft);
68     layout->addWidget(buttons);
69
70     setLayout(layout);
71
72 }
73
74 void ThemeSelector::saveTheme()
75 {
76     QString theme = selector_->value().toString();
77     Settings::instance().setValue("theme", theme);
78     hide();
79     emit themeChanged();
80 }
81
82 void ThemeSelector::loadFromFile()
83 {
84     QString file;
85
86     if(selector_->importFile(ThemeLoader::getThemeDir(),
87                           "Theme files",
88                           "*" + ThemeLoader::getThemeSuffix(),
89                           false,
90                           &file))
91     {
92         selector_->loadThemes();
93         selector_->selectTheme(file);
94     }
95 }
96
97 void ThemeSelector::openScheduler()
98 {
99     if(!themeScheduler_)
100     {
101         themeScheduler_ = new ThemeSchedulerSettings(this);
102         connect(themeScheduler_, SIGNAL(themeChanged()), this, SIGNAL(themeChanged()));
103     }
104
105     themeScheduler_->show();
106 }
107
108 void ThemeSelector::enableDisableScheduler()
109 {
110     buttonClicked_ = true;
111 }
112
113 void ThemeSelector::disableScheduler()
114 {
115     if(buttonClicked_ && ThemeScheduler::instance().isEnabled())
116     {
117         QMaemo5InformationBox::information(this, tr("Disabling theme scheduler..."), 1000);
118         ThemeScheduler::instance().setEnabled(false);
119     }
120 }
121
122 void ThemeSelector::setVisible(bool visible)
123 {
124     buttonClicked_ = false;
125
126     QDialog::setVisible(visible);
127 }