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