Theme download link fixed in web page.
[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),
37 buttonClicked_(false), themeSelected_(false), themeScheduler_(0)
38 {
39     setWindowTitle(tr("Select theme"));
40
41     QHBoxLayout* layout = new QHBoxLayout;
42     QVBoxLayout* left = new QVBoxLayout;
43     QHBoxLayout* first = new QHBoxLayout;
44
45     QPushButton* saveButton = new QPushButton(tr("Save"));
46     connect(saveButton, SIGNAL(clicked(bool)), this, SLOT(saveTheme()));
47     QDialogButtonBox* buttons = new QDialogButtonBox;
48     buttons->setCenterButtons(false);
49     buttons->setOrientation(Qt::Vertical);
50     buttons->addButton(saveButton, QDialogButtonBox::AcceptRole);
51
52     selector_ = new ThemePicker(tr("Theme"), this);
53     connect(selector_, SIGNAL(clicked(bool)), this, SLOT(setButtonClicked()));
54     connect(selector_, SIGNAL(selected()), this, SLOT(setThemeSelected()));
55
56     QPushButton* loadButton = new QPushButton(tr("Import"));
57     connect(loadButton, SIGNAL(clicked(bool)), this, SLOT(loadFromFile()));
58
59     QPushButton* scheduler = new QPushButton(tr("Theme scheduler"));
60     connect(scheduler, SIGNAL(clicked(bool)), this, SLOT(openScheduler()));
61
62     first->addWidget(selector_, Qt::AlignLeft);
63     first->addWidget(loadButton);
64
65     left->addLayout(first);
66     left->addWidget(scheduler);
67
68     layout->addLayout(left, Qt::AlignLeft);
69     layout->addWidget(buttons);
70
71     setLayout(layout);
72
73 }
74
75 void ThemeSelector::saveTheme()
76 {
77     QString theme = selector_->value().toString();
78     Settings::instance().setValue("theme", theme);
79     hide();
80
81     if(buttonClicked_ && themeSelected_ && ThemeScheduler::instance().isEnabled())
82     {
83         QMaemo5InformationBox::information(this, tr("Disabling theme scheduler..."), 1000);
84         ThemeScheduler::instance().setEnabled(false);
85         themeSelected_ = false;
86         buttonClicked_ = false;
87     }
88
89     emit themeChanged();
90 }
91
92 void ThemeSelector::loadFromFile()
93 {
94     QString file;
95
96     if(selector_->importFile(ThemeLoader::getThemeDir(),
97                           "Theme files",
98                           "*" + ThemeLoader::getThemeSuffix(),
99                           false,
100                           &file))
101     {
102         selector_->loadThemes();
103         selector_->selectTheme(file);
104         themeSelected_ = true;
105         buttonClicked_ = true;
106     }
107 }
108
109 void ThemeSelector::openScheduler()
110 {
111     if(!themeScheduler_)
112     {
113         themeScheduler_ = new ThemeSchedulerSettings(this);
114         connect(themeScheduler_, SIGNAL(themeChanged()), this, SLOT(onThemeChanged()));
115     }
116
117     themeScheduler_->show();
118 }
119
120 void ThemeSelector::setButtonClicked()
121 {
122     buttonClicked_ = true;
123 }
124
125 void ThemeSelector::setThemeSelected()
126 {
127     themeSelected_ = true;
128 }
129
130 void ThemeSelector::onThemeChanged()
131 {
132     buttonClicked_ = false;
133     themeSelected_ = false;
134     emit themeChanged();
135 }
136
137
138 void ThemeSelector::setVisible(bool visible)
139 {
140     QDialog::setVisible(visible);
141 }