File indexing completed on 2024-05-19 04:07:51
0001 /* 0002 SPDX-FileCopyrightText: 2009, 2010 Stefan Majewsky <majewsky@gmx.net> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "texturehelper.h" 0008 #include "settings.h" 0009 0010 #include <QDir> 0011 #include <QDirIterator> 0012 #include <QGraphicsScene> 0013 #include <QPainter> 0014 #include <KLocalizedString> 0015 #include <QSvgRenderer> 0016 0017 const QSize Palapeli::TextureHelper::DefaultThumbnailSize(32, 32); 0018 const QSize Palapeli::TextureHelper::DefaultPixmapSize(128, 128); 0019 0020 Palapeli::TextureHelper* Palapeli::TextureHelper::instance() 0021 { 0022 static Palapeli::TextureHelper instance; 0023 return &instance; 0024 } 0025 0026 QPixmap Palapeli::TextureHelper::render(const QString& filePath) 0027 { 0028 QPixmap pixmap; 0029 if (filePath.endsWith(QLatin1String(".svg"))) 0030 { 0031 QSvgRenderer renderer(filePath); 0032 pixmap = QPixmap(DefaultPixmapSize); 0033 pixmap.fill(Qt::transparent); 0034 QPainter painter(&pixmap); 0035 renderer.render(&painter); 0036 painter.end(); 0037 } 0038 else 0039 pixmap.load(filePath); 0040 return pixmap; 0041 } 0042 0043 Palapeli::TextureHelper::TextureHelper() 0044 : m_currentIndex(-1) 0045 { 0046 //create menu item for solid color 0047 QPixmap colorThumbnail(DefaultThumbnailSize); 0048 colorThumbnail.fill(Qt::transparent); 0049 QStandardItem* colorItem = new QStandardItem; 0050 colorItem->setData(QLatin1String("__color__"), IdentifierRole); 0051 colorItem->setData(colorThumbnail, Qt::DecorationRole); 0052 colorItem->setData(i18nc("@item:inlistbox", "Single color"), Qt::DisplayRole); 0053 appendRow(colorItem); 0054 //fetch backgrounds, and create menu items 0055 const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::AppLocalDataLocation, 0056 QStringLiteral("backgrounds"), 0057 QStandardPaths::LocateDirectory); 0058 for (const QString& dir : dirs) 0059 { 0060 QDirIterator dirIt(dir, QDir::Files); 0061 while (dirIt.hasNext()) 0062 { 0063 const QString filePath = dirIt.next(); 0064 const QString fileName = dirIt.fileName(); 0065 //create item for this brush 0066 const QPixmap pixmap = render(filePath); 0067 if (pixmap.isNull()) 0068 continue; 0069 QStandardItem* item = new QStandardItem; 0070 item->setData(pixmap, BrushRole); 0071 item->setData(fileName, IdentifierRole); 0072 item->setData(pixmap.scaled(DefaultThumbnailSize, Qt::KeepAspectRatio), Qt::DecorationRole); 0073 item->setData(fileName, Qt::DisplayRole); 0074 appendRow(item); 0075 } 0076 } 0077 //select initial brush 0078 readSettings(); 0079 } 0080 0081 int Palapeli::TextureHelper::currentIndex() const 0082 { 0083 return m_currentIndex; 0084 } 0085 0086 void Palapeli::TextureHelper::readSettings() 0087 { 0088 //read config 0089 const QString selectedBackground = Settings::viewBackground(); 0090 const QColor selectedColor = Settings::viewBackgroundColor(); 0091 for (int i = 0; i < rowCount(); ++i) 0092 { 0093 QStandardItem* item = this->item(i); 0094 if (item->data(IdentifierRole) != selectedBackground) 0095 continue; 0096 //use this brush 0097 m_currentIndex = i; 0098 if (selectedBackground == QLatin1String("__color__")) 0099 m_currentBrush = selectedColor; 0100 else 0101 m_currentBrush = item->data(BrushRole).value<QPixmap>(); 0102 for (QObject* scene : std::as_const(m_scenes)) 0103 static_cast<QGraphicsScene*>(scene)->setBackgroundBrush(m_currentBrush); 0104 } 0105 } 0106 0107 void Palapeli::TextureHelper::addScene(QGraphicsScene* scene) 0108 { 0109 if (!scene || m_scenes.contains(scene)) 0110 return; 0111 m_scenes << scene; 0112 scene->setBackgroundBrush(m_currentBrush); 0113 connect(scene, &QObject::destroyed, this, &TextureHelper::removeScene); 0114 } 0115 0116 void Palapeli::TextureHelper::removeScene(QObject* scene) 0117 { 0118 m_scenes.removeAll(scene); 0119 } 0120 0121 #include "moc_texturehelper.cpp"