File indexing completed on 2024-04-21 04:05:21

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 <KAboutLicense>
0016 #include <KLocalizedString>
0017 
0018 // LibKMahjongg
0019 #include "kmahjonggbackground.h"
0020 
0021 KMahjonggBackgroundSelector::KMahjonggBackgroundSelector(QWidget *parent, KConfigSkeleton *aconfig)
0022     : QWidget(parent)
0023 {
0024     setupUi(this);
0025     setupData(aconfig);
0026 }
0027 
0028 KMahjonggBackgroundSelector::~KMahjonggBackgroundSelector()
0029 {
0030     qDeleteAll(backgroundMap);
0031 }
0032 
0033 void KMahjonggBackgroundSelector::setupData(KConfigSkeleton *aconfig)
0034 {
0035     // Get our currently configured background entry
0036     KConfig *config = aconfig->config();
0037     KConfigGroup group = config->group(QStringLiteral("General"));
0038     QString initialGroup = group.readEntry("Background_file");
0039 
0040     // The lineEdit widget holds our bg path, but the user does not manipulate it directly
0041     kcfg_Background->hide();
0042 
0043     KMahjonggBackground bg;
0044 
0045     // Now get our backgrounds into a list
0046     QStringList bgsAvailable;
0047     const QStringList dirs =
0048         QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("kmahjongglib/backgrounds"), QStandardPaths::LocateDirectory);
0049     for (const QString &dir : dirs) {
0050         const QStringList fileNames = QDir(dir).entryList({QStringLiteral("*.desktop")});
0051         bgsAvailable.reserve(bgsAvailable.size() + fileNames.size());
0052         for (const QString &file : fileNames) {
0053             bgsAvailable.append(dir + QLatin1Char('/') + file);
0054         }
0055     }
0056 
0057     const qreal dpr = qApp->devicePixelRatio();
0058     const QSize previewSize = backgroundPreview->size() * dpr;
0059 
0060     int numvalidentries = 0;
0061     for (const QString &bgpath : std::as_const(bgsAvailable)) {
0062         auto *abg = new KMahjonggBackground();
0063         if (abg->load(bgpath, previewSize.width(), previewSize.height())) {
0064             const QString name = abg->name();
0065             backgroundMap.insert(name, abg);
0066             backgroundList->addItem(name);
0067             // Find if this is our currently configured background
0068             if (bgpath == initialGroup) {
0069                 // Select current entry
0070                 backgroundList->setCurrentRow(numvalidentries);
0071                 backgroundChanged();
0072             }
0073             ++numvalidentries;
0074         } else {
0075             delete abg;
0076         }
0077     }
0078 
0079     connect(backgroundList, &QListWidget::currentItemChanged, this, &KMahjonggBackgroundSelector::backgroundChanged);
0080 }
0081 
0082 void KMahjonggBackgroundSelector::backgroundChanged()
0083 {
0084     KMahjonggBackground *selBG = backgroundMap.value(backgroundList->currentItem()->text());
0085     // Sanity checkings. Should not happen.
0086     if (selBG == nullptr) {
0087         return;
0088     }
0089     if (selBG->path() == kcfg_Background->text()) {
0090         return;
0091     }
0092 
0093     kcfg_Background->setText(selBG->path());
0094     backgroundAuthor->setText(selBG->authorName());
0095     backgroundContact->setText(selBG->authorEmailAddress());
0096     backgroundDescription->setText(selBG->description());
0097     backgroundDescription->setText(selBG->description());
0098     backgroundVersion->setText(selBG->version());
0099     QString website = selBG->website();
0100     if (!website.isEmpty()) {
0101         website = QLatin1String("<a href=\"") + website + QLatin1String("\">") + website + QLatin1String("</a>");
0102     }
0103     backgroundWebsite->setText(website);
0104     backgroundCopyright->setText(selBG->copyrightText());
0105     const QString licenseName = KAboutLicense::byKeyword(selBG->license()).name(KAboutLicense::FullName);
0106     backgroundLicense->setText(licenseName);
0107 
0108     if (selBG->isPlain()) {
0109         backgroundPreview->setPixmap(QPixmap());
0110         return;
0111     }
0112 
0113     // Make sure SVG is loaded when graphics is selected
0114     if (!selBG->loadGraphics()) {
0115         return;
0116     }
0117 
0118     // Draw the preview
0119     // TODO here: add code to load and keep proportions for non-tiled content?
0120     const qreal dpr = qApp->devicePixelRatio();
0121     QPixmap qiRend(backgroundPreview->size() * dpr);
0122     qiRend.fill(Qt::transparent);
0123     QPainter p(&qiRend);
0124     p.fillRect(p.viewport(), selBG->getBackground());
0125     p.end();
0126     qiRend.setDevicePixelRatio(dpr);
0127     backgroundPreview->setPixmap(qiRend);
0128 }
0129 
0130 #include "moc_kmahjonggbackgroundselector.cpp"