Changeset View
Changeset View
Standalone View
Standalone View
src/engine/texturehelper.cpp
Show All 13 Lines | |||||
14 | * You should have received a copy of the GNU General Public License | 14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the Free Software | 15 | * along with this program; if not, write to the Free Software | ||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
17 | ***************************************************************************/ | 17 | ***************************************************************************/ | ||
18 | 18 | | |||
19 | #include "texturehelper.h" | 19 | #include "texturehelper.h" | ||
20 | #include "settings.h" | 20 | #include "settings.h" | ||
21 | 21 | | |||
22 | #include <QDir> | ||||
22 | #include <QFileInfo> | 23 | #include <QFileInfo> | ||
23 | #include <QGraphicsScene> | 24 | #include <QGraphicsScene> | ||
24 | #include <QPainter> | 25 | #include <QPainter> | ||
25 | #include <KGlobal> | | |||
26 | #include <KLocalizedString> | 26 | #include <KLocalizedString> | ||
27 | #include <KStandardDirs> | | |||
28 | #include <QSvgRenderer> | 27 | #include <QSvgRenderer> | ||
29 | 28 | | |||
30 | const QSize Palapeli::TextureHelper::DefaultThumbnailSize(32, 32); | 29 | const QSize Palapeli::TextureHelper::DefaultThumbnailSize(32, 32); | ||
31 | const QSize Palapeli::TextureHelper::DefaultPixmapSize(128, 128); | 30 | const QSize Palapeli::TextureHelper::DefaultPixmapSize(128, 128); | ||
32 | 31 | | |||
33 | Palapeli::TextureHelper* Palapeli::TextureHelper::instance() | 32 | Palapeli::TextureHelper* Palapeli::TextureHelper::instance() | ||
34 | { | 33 | { | ||
35 | static Palapeli::TextureHelper instance; | 34 | static Palapeli::TextureHelper instance; | ||
36 | return &instance; | 35 | return &instance; | ||
37 | } | 36 | } | ||
38 | 37 | | |||
39 | QPixmap Palapeli::TextureHelper::render(const QString& fileName) | 38 | QPixmap Palapeli::TextureHelper::render(const QString& filePath) | ||
40 | { | 39 | { | ||
41 | const QString path = KStandardDirs::locate("appdata", "backgrounds/" + fileName); | | |||
42 | QPixmap pixmap; | 40 | QPixmap pixmap; | ||
43 | if (fileName.contains(".svg")) | 41 | if (filePath.endsWith(QLatin1String(".svg"))) | ||
44 | { | 42 | { | ||
45 | QSvgRenderer renderer(path); | 43 | QSvgRenderer renderer(filePath); | ||
46 | pixmap = QPixmap(DefaultPixmapSize); | 44 | pixmap = QPixmap(DefaultPixmapSize); | ||
47 | pixmap.fill(Qt::transparent); | 45 | pixmap.fill(Qt::transparent); | ||
48 | QPainter painter(&pixmap); | 46 | QPainter painter(&pixmap); | ||
49 | renderer.render(&painter); | 47 | renderer.render(&painter); | ||
50 | painter.end(); | 48 | painter.end(); | ||
51 | } | 49 | } | ||
52 | else | 50 | else | ||
53 | pixmap.load(path); | 51 | pixmap.load(filePath); | ||
54 | return pixmap; | 52 | return pixmap; | ||
55 | } | 53 | } | ||
56 | 54 | | |||
57 | Palapeli::TextureHelper::TextureHelper() | 55 | Palapeli::TextureHelper::TextureHelper() | ||
58 | : m_currentIndex(-1) | 56 | : m_currentIndex(-1) | ||
59 | { | 57 | { | ||
60 | //create menu item for solid color | 58 | //create menu item for solid color | ||
61 | QPixmap colorThumbnail(DefaultThumbnailSize); | 59 | QPixmap colorThumbnail(DefaultThumbnailSize); | ||
62 | colorThumbnail.fill(Qt::transparent); | 60 | colorThumbnail.fill(Qt::transparent); | ||
63 | QStandardItem* colorItem = new QStandardItem; | 61 | QStandardItem* colorItem = new QStandardItem; | ||
64 | colorItem->setData(QLatin1String("__color__"), IdentifierRole); | 62 | colorItem->setData(QLatin1String("__color__"), IdentifierRole); | ||
65 | colorItem->setData(colorThumbnail, Qt::DecorationRole); | 63 | colorItem->setData(colorThumbnail, Qt::DecorationRole); | ||
66 | colorItem->setData(i18nc("@item:inlistbox", "Single color"), Qt::DisplayRole); | 64 | colorItem->setData(i18nc("@item:inlistbox", "Single color"), Qt::DisplayRole); | ||
67 | appendRow(colorItem); | 65 | appendRow(colorItem); | ||
68 | //fetch backgrounds, and create menu items | 66 | //fetch backgrounds, and create menu items | ||
69 | const QStringList backgroundFiles = KGlobal::dirs()->findAllResources("appdata", "backgrounds/*"); | 67 | const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::AppLocalDataLocation, | ||
70 | foreach (const QString& path, backgroundFiles) | 68 | "backgrounds", | ||
69 | QStandardPaths::LocateDirectory); | ||||
70 | foreach (const QString& dir, dirs) | ||||
71 | { | 71 | { | ||
72 | //get file name and find selected or default backgrounds | 72 | const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*"), QDir::Files); | ||
73 | const QString fileName = QFileInfo(path).fileName(); | 73 | Q_FOREACH (const QString& fileName, fileNames) | ||
nhirsl: A bit cleaner solution to this might be QDirIterator
Something like:
QDirIterator it… | |||||
74 | { | ||||
75 | const QString filePath = dir + QLatin1Char('/') + fileName; | ||||
74 | //create item for this brush | 76 | //create item for this brush | ||
75 | const QPixmap pixmap = render(fileName); | 77 | const QPixmap pixmap = render(filePath); | ||
nhirsl: I would add here:
if (!pixmap.isNull())
{
...
} | |||||
76 | QStandardItem* item = new QStandardItem; | 78 | QStandardItem* item = new QStandardItem; | ||
77 | item->setData(pixmap, BrushRole); | 79 | item->setData(pixmap, BrushRole); | ||
78 | item->setData(fileName, IdentifierRole); | 80 | item->setData(fileName, IdentifierRole); | ||
79 | item->setData(pixmap.scaled(DefaultThumbnailSize, Qt::KeepAspectRatio), Qt::DecorationRole); | 81 | item->setData(pixmap.scaled(DefaultThumbnailSize, Qt::KeepAspectRatio), Qt::DecorationRole); | ||
80 | item->setData(fileName, Qt::DisplayRole); | 82 | item->setData(fileName, Qt::DisplayRole); | ||
81 | appendRow(item); | 83 | appendRow(item); | ||
82 | } | 84 | } | ||
85 | } | ||||
83 | //select initial brush | 86 | //select initial brush | ||
84 | readSettings(); | 87 | readSettings(); | ||
85 | } | 88 | } | ||
86 | 89 | | |||
87 | int Palapeli::TextureHelper::currentIndex() const | 90 | int Palapeli::TextureHelper::currentIndex() const | ||
88 | { | 91 | { | ||
89 | return m_currentIndex; | 92 | return m_currentIndex; | ||
90 | } | 93 | } | ||
Show All 38 Lines |
A bit cleaner solution to this might be QDirIterator
Something like:
QDirIterator it(backgroundsFolder);
while (it.hasNext())