Added POI text field. Some tuning to detail screen item position.
[jspeed] / src / themepicker.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/QString>
20 #include <QtCore/QDebug>
21 #include <QtCore/QDir>
22 #include "themepicker.h"
23 #include "themescheduler.h"
24 #include "themeloader.h"
25 #include "settings.h"
26
27 ThemePicker::ThemePicker(QString const& text, QWidget* parent):
28 FileSelector(text, parent)
29 {
30 }
31
32 void ThemePicker::setVisible(bool visible)
33 {
34     if(visible)
35     {
36         loadThemes();
37     }
38
39     FileSelector::setVisible(visible);
40 }
41
42 void ThemePicker::loadThemes()
43 {
44     QString current = Settings::instance().value("theme", ThemeScheduler::getDefaultTheme()).toString();
45     clear();
46     addItem(tr("Default"), ThemeScheduler::getDefaultTheme());
47
48     QDir themeDir(ThemeLoader::getThemeDir());
49
50     if(!themeDir.exists() || !themeDir.isReadable())
51     {
52         qDebug() << "Warning: theme dir (" + ThemeLoader::getThemeDir() + ") doesn't exist or is read protected";
53         return;
54     }
55
56     QStringList filters;
57     filters << "*" + ThemeLoader::getThemeSuffix();
58     themeDir.setNameFilters(filters);
59     themeDir.setFilter(QDir::Files);
60     QStringList files = themeDir.entryList();
61
62     for(int i = 0; i < files.size(); i++)
63     {
64         QString name;
65         QString id;
66         getThemeDetails(files.at(i), name, id);
67         addItem(name, id);
68
69         if(id == current)
70         {
71             setCurrentIndex(i + 1);
72         }
73     }
74 }
75
76 bool ThemePicker::selectTheme(QString const& filename)
77 {
78     QString name;
79     QString id;
80     getThemeDetails(filename, name, id);
81     return selectByValue(id);
82 }
83
84 void ThemePicker::getThemeDetails(QString filename,
85                                   QString& name,
86                                   QString& id)
87 {
88     static QRegExp cleaner(QRegExp::escape(ThemeLoader::getThemeSuffix()) + "$");
89     filename = filename.replace(cleaner, "");
90
91     id = filename;
92     name = filename.at(0).toUpper() + filename.mid(1);
93 }