Advertisement
JoshDreamland

IconManager.cpp/hpp

Jun 11th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.38 KB | None | 0 0
  1. /* Copyright (C) 2013 Daniel Hrabovcak, Josh Ventura <JoshV10@gmail.com>
  2.  * This file is part of NaturalGM.
  3.  *
  4.  * NaturalGM is free software: you can redistribute it and/or modify it under
  5.  * the terms of the GNU General Public License as published by the Free Software
  6.  * Foundation, version 3 of the License, or (at your option) any later version.
  7.  *
  8.  * NaturalGM is distributed in the hope that it will be useful, but WITHOUT ANY
  9.  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  10.  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11.  *
  12.  * You should have received a copy of the GNU General Public License along with
  13.  * NaturalGM. If not, see <http://www.gnu.org/licenses/>.
  14. **/
  15.  
  16. #ifndef _NGM_ICONMANAGER__HPP
  17. #define _NGM_ICONMANAGER__HPP
  18.  
  19. #include <map>
  20. #include <vector>
  21. #include <memory>
  22.  
  23. #include <QObject>
  24. #include <QString>
  25. #include <QIcon>
  26.  
  27. class IconManager: public QObject
  28. {
  29.   Q_OBJECT
  30.   private:
  31.     std::vector<QString> searchPaths; ///< List of paths to search for theme icons
  32.     std::map<QString, int> extensionPriorities; ///< Mapping of known and preferred (or unpreferred) extensions
  33.     std::map<QString, std::pair<QString, int>> pathCache; ///< Map intended to cache fully qualified paths and their respective priorities by unqualified icon names.
  34.     std::map<QString, std::shared_ptr<QIcon>> iconCache; ///< Map intended to prevent loading the same icon twice.
  35.     QString currentTheme; ///< The name of the current icon theme.
  36.  
  37.     void cachePathnames(); ///< Called to crawl the filesystem for icons, caching the most favorable name.
  38.     void dumpCaches(); ///< Drop the caches; clear them and move their data to be collected.
  39.  
  40.   public:
  41.     // Methods the main initializer and plugin initializers will use
  42.  
  43.     /// Default constructor.
  44.     IconManager();
  45.  
  46.     /// Add a path from which to check for icons. The most obvious is "icons/".
  47.     void addSearchPath(const QString& path);
  48.    
  49.     /// Set the priority of a given extension. A smaller number indicates a higher priority;
  50.     /// the default priority is zero. Thus, a file having an extension with a priority of
  51.     /// zero is chosen over a file of the same name with an extension having a priority of
  52.     /// one, but is swept aside in favor of such a file with a priority of -1.
  53.     /// @param extension  The extension whose priority is to be set.
  54.     /// @param priority   The priority; -10 is a higher priority than 0, which is higher than 10.
  55.     void setExtensionPriority(const QString &extension, int priority);
  56.  
  57.     /// Set the current icon theme, by name.
  58.     void setIconTheme(const QString& iconTheme);
  59.  
  60.     /// Grab a list of available icon theme names.
  61.     const std::vector<QString> getAvailableIconThemes();
  62.  
  63.     // Methods everyone will use
  64.    
  65.     /// Retrieve a QIcon for the current theme from its most basic name.
  66.     /// @param iconName The unqualified name of this icon, eg, "LoadFile".
  67.     std::shared_ptr<QIcon> getIcon(const QString &iconName);
  68.    
  69.    
  70.   signals:
  71.     /// Signaled when the current theme changes
  72.     void iconThemeChanged(IconManager *manager);
  73.    
  74. };
  75.  
  76. #endif // _NGM_ICONMANAGER__HPP
  77.  
  78.  
  79. /* Copyright (C) 2013 Josh Ventura <JoshV10@gmail.com>
  80.  * This file is part of NaturalGM.
  81.  *
  82.  * NaturalGM is free software: you can redistribute it and/or modify it under
  83.  * the terms of the GNU General Public License as published by the Free Software
  84.  * Foundation, version 3 of the License, or (at your option) any later version.
  85.  *
  86.  * NaturalGM is distributed in the hope that it will be useful, but WITHOUT ANY
  87.  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  88.  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
  89.  *
  90.  * You should have received a copy of the GNU General Public License along with
  91.  * NaturalGM. If not, see <http://www.gnu.org/licenses/>.
  92. **/
  93.  
  94. #include "IconManager.hpp"
  95. #include <QDirIterator>
  96. #include <set>
  97.  
  98. using std::map;
  99. using std::set;
  100. using std::pair;
  101. using std::vector;
  102. using std::shared_ptr;
  103.  
  104. IconManager::IconManager() {}
  105.  
  106. void IconManager::addSearchPath(const QString& path) {
  107.     QString nixPath = path;
  108.     nixPath.replace('\\', '/');
  109.     if (!nixPath.endsWith('/'))
  110.         nixPath.append('/');
  111.     searchPaths.push_back(nixPath);
  112. }
  113.  
  114. void IconManager::setExtensionPriority(const QString &extension, int priority) {
  115.     extensionPriorities[extension] = priority;
  116. }
  117.  
  118. void IconManager::setIconTheme(const QString& iconTheme) {
  119.     currentTheme = iconTheme;
  120.     cachePathnames();
  121. }
  122.  
  123. const vector<QString> IconManager::getAvailableIconThemes() {
  124.     set<QString> foundThemes;
  125.     vector<QString> result;
  126.     for (auto path = searchPaths.begin(); path != searchPaths.end(); ++path) {
  127.         QDir dir(*path);
  128.         if (dir.exists()) {
  129.             QDirIterator dirit(dir);
  130.             while (dirit.hasNext()) {
  131.                 QString theme = dirit.next();
  132.                 if (foundThemes.find(theme) == foundThemes.end()) {
  133.                     foundThemes.insert(theme);
  134.                     result.push_back(theme);
  135.                 }
  136.             }
  137.         }
  138.     }
  139.     return result;
  140. }
  141.  
  142. std::shared_ptr<QIcon> IconManager::getIcon(const QString &iconName) {
  143.     auto iconIt = iconCache.find(iconName);
  144.     if (iconIt != iconCache.end())
  145.         return iconIt->second;
  146.     auto iconPathIt = pathCache.find(iconName);
  147.     QIcon *allocIcon = (iconPathIt == pathCache.end()) ? new QIcon() : new QIcon(iconPathIt->first);
  148.     std::shared_ptr<QIcon> result(allocIcon);
  149.     iconCache[iconName] = result;
  150.     return result;
  151. }
  152.  
  153. void IconManager::cachePathnames() {
  154.     pathCache.clear();
  155.     iconCache.clear();
  156.     for (auto path = searchPaths.begin(); path != searchPaths.end(); ++path) {
  157.         QDir dir(*path);
  158.         if (dir.exists()) {
  159.             QDirIterator dirit(dir);
  160.             while (dirit.hasNext()) {
  161.                 QString fn = dirit.next();
  162.                 QString nixname = fn.replace('\\', '/');
  163.                 QString filename;
  164.                 int fileNameStart = fn.lastIndexOf('/');
  165.                 int extensionStart = fn.lastIndexOf('.', fileNameStart);
  166.                 int priority = 0;
  167.                 if (extensionStart != -1) {
  168.                     auto extit = extensionPriorities.find(nixname.mid(extensionStart + 1));
  169.                     if (extit != extensionPriorities.end())
  170.                         priority = extit->second;
  171.                     filename = nixname.mid(fileNameStart + 1, extensionStart - fileNameStart - 1);
  172.                 }
  173.                 else filename = nixname.mid(fileNameStart + 1);
  174.  
  175.                 auto mapFile = pathCache.find(filename);
  176.                 if (mapFile == pathCache.end() || priority < mapFile->second.second)
  177.                     pathCache[filename] = pair<QString, int>(fn, priority);
  178.             }
  179.         }
  180.     }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement