BuildmLearn Toolkit  2.0.4
BuildmLearn Toolkit is an easy-to-use program that helps users make mobile apps without any knowledge of application development.
 All Classes Functions Enumerations Groups Pages
iconfactory.cpp
1 /*
2  Copyright (c) 2012, BuildmLearn Contributors listed at http://buildmlearn.org/people/
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 
11  * Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15  * Neither the name of the BuildmLearn nor the names of its
16  contributors may be used to endorse or promote products derived from
17  this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 
31 #include "miscellaneous/iconfactory.h"
32 
33 #include "miscellaneous/application.h"
34 #include "miscellaneous/settings.h"
35 
36 #include <QBuffer>
37 
38 
39 QPointer<IconFactory> IconFactory::s_instance;
40 
41 IconFactory::IconFactory(QObject *parent) : QObject(parent) {
42 }
43 
44 IconFactory::~IconFactory() {
45  qDebug("Destroying IconFactory instance.");
46 }
47 
49  if (s_instance.isNull()) {
50  s_instance = new IconFactory(qApp);
51  }
52 
53  return s_instance;
54 }
55 
57  QIcon::setThemeSearchPaths(QStringList() << APP_THEME_PATH);
58  qDebug("Available icon theme paths: %s.",
59  qPrintable(QIcon::themeSearchPaths().replaceInStrings(QRegExp("^|$"),
60  "\'").join(", ")));
61 }
62 
63 
64 
65 void IconFactory::setCurrentIconTheme(const QString &theme_name) {
66  qApp->settings()->setValue(APP_CFG_GUI, "icon_theme", theme_name);
67 }
68 
70  QStringList installed_themes = installedIconThemes();
71  QString theme_name_from_settings = qApp->settings()->value(APP_CFG_GUI,
72  "icon_theme",
73  APP_THEME_DEFAULT).toString();
74 
75  if (m_currentIconTheme == theme_name_from_settings) {
76  qDebug("Icon theme '%s' already loaded.",
77  qPrintable(theme_name_from_settings));
78  return;
79  }
80 
81  // Display list of installed themes.
82  qDebug("Installed icon themes are: %s.",
83  qPrintable(QStringList(installed_themes).replaceInStrings(QRegExp("^|$"),
84  "\'").join(", ")));
85 
86  if (installed_themes.contains(theme_name_from_settings)) {
87  // Desired icon theme is installed and can be loaded.
88  qDebug("Loading icon theme '%s'.", qPrintable(theme_name_from_settings));
89  m_currentIconTheme = theme_name_from_settings;
90  }
91  else {
92  // Desired icon theme is not currently available.
93  // Install "default" icon theme instead.
94  qDebug("Icon theme '%s' cannot be loaded because it is not installed. "
95  "No icon theme is loaded now.",
96  qPrintable(theme_name_from_settings));
97  m_currentIconTheme = APP_NO_THEME;
98  }
99 }
100 
101 QStringList IconFactory::installedIconThemes() const {
102  QStringList icon_theme_names;
103  icon_theme_names << APP_NO_THEME;
104 
105  // Iterate all directories with icon themes.
106  QStringList icon_themes_paths = QIcon::themeSearchPaths();
107  icon_themes_paths.removeDuplicates();
108 
109  foreach (const QString &icon_path, icon_themes_paths) {
110  QDir icon_dir(icon_path);
111 
112  // Iterate all icon themes in this directory.
113  foreach (const QString &icon_theme_path, icon_dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot |
114  QDir::Readable | QDir::CaseSensitive |
115  QDir::NoSymLinks,
116  QDir::Time)) {
117  icon_theme_names << icon_theme_path;
118  }
119  }
120 
121  icon_theme_names.removeDuplicates();
122  return icon_theme_names;
123 }
void setCurrentIconTheme(const QString &theme_name)
Sets icon theme with given name as the active one and loads it.
Definition: iconfactory.cpp:65
void setupSearchPaths()
Adds custom application path to be search for icons.
Definition: iconfactory.cpp:56
void loadCurrentIconTheme()
Loads name of selected icon theme (from settings) for the application and activates it...
Definition: iconfactory.cpp:69
Icon theme manipulator and provider.
Definition: iconfactory.h:47
QStringList installedIconThemes() const
Access to list of icon themes.
static IconFactory * instance()
Singleton getter.
Definition: iconfactory.cpp:48