File indexing completed on 2022-09-27 13:20:31

0001 /*
0002     SPDX-FileCopyrightText: 2006 Mauricio Piacentini <mauricio@tabuleiro.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 // own
0008 #include "kmahjonggtilesetselector.h"
0009 
0010 // Qt
0011 #include <QDir>
0012 #include <QPainter>
0013 #include <QStandardPaths>
0014 
0015 // KF
0016 #include <KLocalizedString>
0017 
0018 // LibKMahjongg
0019 #include "kmahjonggtileset.h"
0020 
0021 KMahjonggTilesetSelector::KMahjonggTilesetSelector(QWidget * parent, KConfigSkeleton * aconfig)
0022     : QWidget(parent)
0023 {
0024     setupUi(this);
0025     setupData(aconfig);
0026 }
0027 
0028 KMahjonggTilesetSelector::~KMahjonggTilesetSelector()
0029 {
0030     tilesetMap.clear();
0031 }
0032 
0033 void KMahjonggTilesetSelector::setupData(KConfigSkeleton * aconfig)
0034 {
0035     //Get our currently configured Tileset entry
0036     KConfig * config = aconfig->config();
0037     KConfigGroup group = config->group("General");
0038     QString initialGroup = group.readEntry("Tileset_file");
0039 
0040     //The lineEdit widget holds our tileset path, but the user does not manipulate it directly
0041     kcfg_TileSet->hide();
0042 
0043     //This will also load our resourcedir if it is not done already
0044     KMahjonggTileset tile;
0045 
0046     //Now get our tilesets into a list
0047     QStringList tilesAvailable;
0048     const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("kmahjongglib/tilesets"), QStandardPaths::LocateDirectory);
0049     for (const QString & dir : dirs) {
0050         const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.desktop"));
0051         for (const QString & file : fileNames) {
0052             tilesAvailable.append(dir + QLatin1Char('/') + file);
0053         }
0054     }
0055 
0056     QLatin1String namestr("Name");
0057     int numvalidentries = 0;
0058     for (int i = 0; i < tilesAvailable.size(); ++i) {
0059         KMahjonggTileset * aset = new KMahjonggTileset();
0060         QString atileset = tilesAvailable.at(i);
0061         if (aset->loadTileset(atileset)) {
0062             tilesetMap.insert(aset->authorProperty(namestr), aset);
0063             tilesetList->addItem(aset->authorProperty(namestr));
0064             //Find if this is our currently configured Tileset
0065             if (atileset == initialGroup) {
0066                 //Select current entry
0067                 tilesetList->setCurrentRow(numvalidentries);
0068                 tilesetChanged();
0069             }
0070             ++numvalidentries;
0071         } else {
0072             delete aset;
0073         }
0074     }
0075 
0076     connect(tilesetList, &QListWidget::currentItemChanged, this, &KMahjonggTilesetSelector::tilesetChanged);
0077 }
0078 
0079 void KMahjonggTilesetSelector::tilesetChanged()
0080 {
0081     KMahjonggTileset * selTileset = tilesetMap.value(tilesetList->currentItem()->text());
0082     //Sanity checkings. Should not happen.
0083     if (selTileset == nullptr) {
0084         return;
0085     }
0086     if (selTileset->path() == kcfg_TileSet->text()) {
0087         return;
0088     }
0089     QLatin1String authstr("Author");
0090     QLatin1String contactstr("AuthorEmail");
0091     QLatin1String descstr("Description");
0092     kcfg_TileSet->setText(selTileset->path());
0093     tilesetAuthor->setText(selTileset->authorProperty(authstr));
0094     tilesetContact->setText(selTileset->authorProperty(contactstr));
0095     tilesetDescription->setText(selTileset->authorProperty(descstr));
0096 
0097     //Make sure SVG is loaded when graphics is selected
0098     if (!selTileset->loadGraphics()) {
0099         return;
0100     }
0101     //Let the tileset calculate its ideal size for the preview area, but reduce the margins a bit (pass oversized drawing area)
0102     QSize tilesize = selTileset->preferredTileSize(tilesetPreview->size() * 1.3, 1, 1);
0103     selTileset->reloadTileset(tilesize);
0104     //Draw the preview
0105     QImage qiRend(tilesetPreview->size(), QImage::Format_ARGB32_Premultiplied);
0106     qiRend.fill(0);
0107     QPainter p(&qiRend);
0108     //Calculate the margins to center the tile
0109     QSize margin = tilesetPreview->size() - tilesize;
0110     //Draw unselected tile and first tileface
0111     p.drawPixmap(margin.width() / 2, margin.height() / 2, selTileset->unselectedTile(1));
0112     p.drawPixmap(margin.width() / 2, margin.height() / 2, selTileset->tileface(0));
0113     p.end();
0114     tilesetPreview->setPixmap(QPixmap::fromImage(qiRend));
0115 }