1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Rosegarden
5 A MIDI and audio sequencer and musical notation editor.
6 Copyright 2000-2021 the Rosegarden development team.
7
8 This file originally from Sonic Visualiser, copyright 2007 Queen
9 Mary, University of London.
10
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version. See the file
15 COPYING included with this distribution for more information.
16 */
17
18 #define RG_MODULE_STRING "[IconLoader]"
19
20 #include "IconLoader.h"
21
22 #include <QColor>
23 #include <QImage>
24 #include <QRgb>
25
26 #include <map>
27
28 namespace {
29
30 std::map<QString, QPixmap> pixmapCache;
31
32 QPixmap
loadPixmap2(QString dir,QString name)33 loadPixmap2(QString dir, QString name)
34 {
35 QPixmap pixmap;
36
37 QString pathName = QString("%1/%2").arg(dir).arg(name);
38
39 // Try to load assuming extension is included.
40 pixmap.load(pathName);
41 if (!pixmap.isNull())
42 return pixmap;
43
44 // Try .png.
45 pixmap.load(pathName + ".png");
46 if (!pixmap.isNull())
47 return pixmap;
48
49 // Try .jpg.
50 pixmap.load(pathName + ".jpg");
51 if (!pixmap.isNull())
52 return pixmap;
53
54 // Try .xpm.
55 pixmap.load(pathName + ".xpm");
56
57 return pixmap;
58 }
59
60 }
61
62 namespace Rosegarden
63 {
64
65
66 QIcon
load(QString name)67 IconLoader::load(QString name)
68 {
69 QPixmap pmap(loadPixmap(name));
70 if (pmap.isNull())
71 return QIcon();
72
73 return QIcon(pmap);
74 }
75
76 QPixmap
loadPixmap(QString name)77 IconLoader::loadPixmap(QString name)
78 {
79 // Check the cache.
80 std::map<QString, QPixmap>::const_iterator it = pixmapCache.find(name);
81 // If found in the cache, return it.
82 if (it != pixmapCache.end())
83 return it->second;
84
85 // Try the various possible directories in :pixmaps.
86 QPixmap pixmap = loadPixmap2(":pixmaps/toolbar", name);
87 if (pixmap.isNull())
88 pixmap = loadPixmap2(":pixmaps/transport", name);
89 if (pixmap.isNull())
90 pixmap = loadPixmap2(":pixmaps/misc", name);
91 if (pixmap.isNull())
92 pixmap = loadPixmap2(":pixmaps/stock", name);
93 if (pixmap.isNull())
94 pixmap = loadPixmap2(":pixmaps/icons", name);
95 if (pixmap.isNull())
96 pixmap = loadPixmap2(":pixmaps/style", name);
97 if (pixmap.isNull())
98 pixmap = loadPixmap2(":pixmaps", name);
99
100 // Store whatever we found, or didn't, in the cache.
101 pixmapCache[name] = pixmap;
102
103 return pixmap;
104 }
105
106
107 }
108