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.h
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 #ifndef ICONFACTORY_H
32 #define ICONFACTORY_H
33 
34 #include <QObject>
35 
36 #include "definitions/definitions.h"
37 #include "application.h"
38 
39 #include <QString>
40 #include <QIcon>
41 #include <QPointer>
42 #include <QHash>
43 #include <QDir>
44 
45 
46 /// \brief Icon theme manipulator and provider.
47 class IconFactory : public QObject {
48  Q_OBJECT
49 
50  public:
51  // Destructor.
52  virtual ~IconFactory();
53 
54  /// \brief Returns icon from active theme.
55  /// \param name Name of the icon.
56  /// \return Returns icon from active theme or invalid icon if
57  /// "no icon theme" is set.
58  inline QIcon fromTheme(const QString &name) {
59  if (m_currentIconTheme == APP_NO_THEME) {
60  return QIcon();
61  }
62 
63  if (!m_cachedIcons.contains(name)) {
64  // Icon is not cached yet.
65  m_cachedIcons.insert(name, QIcon(APP_THEME_PATH + QDir::separator() +
66  m_currentIconTheme + QDir::separator() +
67  name + APP_THEME_SUFFIX));
68  }
69 
70  return m_cachedIcons.value(name);
71  }
72 
73  /// \brief Adds custom application path to be search for icons.
74  void setupSearchPaths();
75 
76  /// \brief Access to list of icon themes.
77  /// \return Returns list of installed themes, including "default" theme.
78  QStringList installedIconThemes() const;
79 
80  /// \brief Loads name of selected icon theme (from settings) for the application and
81  /// activates it.
82  /// \note If that particular theme is not installed, then "default" theme is loaded.
83  void loadCurrentIconTheme();
84 
85  /// \brief Gets name of current theme.
86  /// \return Returns name of currently activated theme for the application.
87  inline QString currentIconTheme() const {
88  return m_currentIconTheme;
89  }
90 
91  ///
92  /// \brief Sets icon theme with given name as the active one and loads it.
93  /// \param theme_name New theme name.
94  void setCurrentIconTheme(const QString &theme_name);
95 
96  ///
97  /// \brief Singleton getter.
98  /// \return Returns pointer to singleton.
99  static IconFactory *instance();
100 
101  private:
102  // Constructor.
103  explicit IconFactory(QObject *parent = 0);
104 
105  QHash<QString, QIcon> m_cachedIcons;
106  QString m_currentIconTheme;
107 
108  // Singleton.
109  static QPointer<IconFactory> s_instance;
110 };
111 
112 #endif // ICONFACTORY_H
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.
QIcon fromTheme(const QString &name)
Returns icon from active theme.
Definition: iconfactory.h:58
static IconFactory * instance()
Singleton getter.
Definition: iconfactory.cpp:48
QString currentIconTheme() const
Gets name of current theme.
Definition: iconfactory.h:87