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

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 "kmahjonggbackgroundselector.h"
0009 
0010 // Qt
0011 #include <QDir>
0012 #include <QPainter>
0013 
0014 // KF
0015 #include <KLocalizedString>
0016 
0017 // LibKMahjongg
0018 #include "kmahjonggbackground.h"
0019 
0020 KMahjonggBackgroundSelector::KMahjonggBackgroundSelector(QWidget * parent, KConfigSkeleton * aconfig)
0021     : QWidget(parent)
0022 {
0023     setupUi(this);
0024     setupData(aconfig);
0025 }
0026 
0027 KMahjonggBackgroundSelector::~KMahjonggBackgroundSelector()
0028 {
0029     qDeleteAll(backgroundMap);
0030 }
0031 
0032 void KMahjonggBackgroundSelector::setupData(KConfigSkeleton * aconfig)
0033 {
0034     //Get our currently configured background entry
0035     KConfig * config = aconfig->config();
0036     KConfigGroup group = config->group("General");
0037     QString initialGroup = group.readEntry("Background_file");
0038 
0039     //The lineEdit widget holds our bg path, but the user does not manipulate it directly
0040     kcfg_Background->hide();
0041 
0042     KMahjonggBackground bg;
0043 
0044     //Now get our backgrounds into a list
0045     QStringList bgsAvailable;
0046     const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("kmahjongglib/backgrounds"), QStandardPaths::LocateDirectory);
0047     for (const QString & dir : dirs) {
0048         const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.desktop"));
0049         for (const QString & file : fileNames) {
0050             bgsAvailable.append(dir + QLatin1Char('/') + file);
0051         }
0052     }
0053 
0054     QLatin1String namestr("Name");
0055     int numvalidentries = 0;
0056     for (int i = 0; i < bgsAvailable.size(); ++i) {
0057         KMahjonggBackground * abg = new KMahjonggBackground();
0058         QString bgpath = bgsAvailable.at(i);
0059         if (abg->load(bgpath, backgroundPreview->width(), backgroundPreview->height())) {
0060             backgroundMap.insert(abg->authorProperty(namestr), abg);
0061             backgroundList->addItem(abg->authorProperty(namestr));
0062             //Find if this is our currently configured background
0063             if (bgpath == initialGroup) {
0064                 //Select current entry
0065                 backgroundList->setCurrentRow(numvalidentries);
0066                 backgroundChanged();
0067             }
0068             ++numvalidentries;
0069         } else {
0070             delete abg;
0071         }
0072     }
0073 
0074     connect(backgroundList, &QListWidget::currentItemChanged, this, &KMahjonggBackgroundSelector::backgroundChanged);
0075 }
0076 
0077 void KMahjonggBackgroundSelector::backgroundChanged()
0078 {
0079     KMahjonggBackground * selBG = backgroundMap.value(backgroundList->currentItem()->text());
0080     //Sanity checkings. Should not happen.
0081     if (selBG == nullptr) {
0082         return;
0083     }
0084     if (selBG->path() == kcfg_Background->text()) {
0085         return;
0086     }
0087     QLatin1String authstr("Author");
0088     QLatin1String contactstr("AuthorEmail");
0089     QLatin1String descstr("Description");
0090     kcfg_Background->setText(selBG->path());
0091     backgroundAuthor->setText(selBG->authorProperty(authstr));
0092     backgroundContact->setText(selBG->authorProperty(contactstr));
0093     backgroundDescription->setText(selBG->authorProperty(descstr));
0094 
0095     if (selBG->authorProperty(QStringLiteral("Plain")) == QLatin1String("1")) {
0096         backgroundPreview->setPixmap(QPixmap());
0097         return;
0098     }
0099 
0100     //Make sure SVG is loaded when graphics is selected
0101     if (!selBG->loadGraphics()) {
0102         return;
0103     }
0104 
0105     //Draw the preview
0106     //TODO here: add code to load and keep proportions for non-tiled content?
0107     QImage qiRend(backgroundPreview->size(), QImage::Format_ARGB32_Premultiplied);
0108     qiRend.fill(0);
0109     QPainter p(&qiRend);
0110     p.fillRect(p.viewport(), selBG->getBackground());
0111     p.end();
0112     backgroundPreview->setPixmap(QPixmap::fromImage(qiRend));
0113 }