LibKmahjongg

kmahjonggbackgroundselector.cpp
1/*
2 SPDX-FileCopyrightText: 2006 Mauricio Piacentini <mauricio@tabuleiro.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7// own
8#include "kmahjonggbackgroundselector.h"
9
10// Qt
11#include <QDir>
12#include <QPainter>
13
14// KF
15#include <KAboutLicense>
16#include <KLocalizedString>
17
18// LibKMahjongg
19#include "kmahjonggbackground.h"
20
21KMahjonggBackgroundSelector::KMahjonggBackgroundSelector(QWidget *parent, KConfigSkeleton *aconfig)
22 : QWidget(parent)
23{
24 setupUi(this);
25 setupData(aconfig);
26}
27
28KMahjonggBackgroundSelector::~KMahjonggBackgroundSelector()
29{
30 qDeleteAll(backgroundMap);
31}
32
33void KMahjonggBackgroundSelector::setupData(KConfigSkeleton *aconfig)
34{
35 // Get our currently configured background entry
36 KConfig *config = aconfig->config();
37 KConfigGroup group = config->group(QStringLiteral("General"));
38 QString initialGroup = group.readEntry("Background_file");
39
40 // The lineEdit widget holds our bg path, but the user does not manipulate it directly
41 kcfg_Background->hide();
42
44
45 // Now get our backgrounds into a list
46 QStringList bgsAvailable;
47 const QStringList dirs =
49 for (const QString &dir : dirs) {
50 const QStringList fileNames = QDir(dir).entryList({QStringLiteral("*.desktop")});
51 bgsAvailable.reserve(bgsAvailable.size() + fileNames.size());
52 for (const QString &file : fileNames) {
53 bgsAvailable.append(dir + QLatin1Char('/') + file);
54 }
55 }
56
57 const qreal dpr = qApp->devicePixelRatio();
58 const QSize previewSize = backgroundPreview->size() * dpr;
59
60 int numvalidentries = 0;
61 for (const QString &bgpath : std::as_const(bgsAvailable)) {
62 auto *abg = new KMahjonggBackground();
63 if (abg->load(bgpath, previewSize.width(), previewSize.height())) {
64 const QString name = abg->name();
65 backgroundMap.insert(name, abg);
66 backgroundList->addItem(name);
67 // Find if this is our currently configured background
68 if (bgpath == initialGroup) {
69 // Select current entry
70 backgroundList->setCurrentRow(numvalidentries);
71 backgroundChanged();
72 }
73 ++numvalidentries;
74 } else {
75 delete abg;
76 }
77 }
78
79 connect(backgroundList, &QListWidget::currentItemChanged, this, &KMahjonggBackgroundSelector::backgroundChanged);
80}
81
82void KMahjonggBackgroundSelector::backgroundChanged()
83{
84 KMahjonggBackground *selBG = backgroundMap.value(backgroundList->currentItem()->text());
85 // Sanity checkings. Should not happen.
86 if (selBG == nullptr) {
87 return;
88 }
89 if (selBG->path() == kcfg_Background->text()) {
90 return;
91 }
92
93 kcfg_Background->setText(selBG->path());
94 backgroundAuthor->setText(selBG->authorName());
95 backgroundContact->setText(selBG->authorEmailAddress());
96 backgroundDescription->setText(selBG->description());
97 backgroundDescription->setText(selBG->description());
98 backgroundVersion->setText(selBG->version());
99 QString website = selBG->website();
100 if (!website.isEmpty()) {
101 website = QLatin1String("<a href=\"") + website + QLatin1String("\">") + website + QLatin1String("</a>");
102 }
103 backgroundWebsite->setText(website);
104 backgroundCopyright->setText(selBG->copyrightText());
105 const QString licenseName = KAboutLicense::byKeyword(selBG->license()).name(KAboutLicense::FullName);
106 backgroundLicense->setText(licenseName);
107
108 if (selBG->isPlain()) {
109 backgroundPreview->setPixmap(QPixmap());
110 return;
111 }
112
113 // Make sure SVG is loaded when graphics is selected
114 if (!selBG->loadGraphics()) {
115 return;
116 }
117
118 // Draw the preview
119 // TODO here: add code to load and keep proportions for non-tiled content?
120 const qreal dpr = qApp->devicePixelRatio();
121 QPixmap qiRend(backgroundPreview->size() * dpr);
122 qiRend.fill(Qt::transparent);
123 QPainter p(&qiRend);
124 p.fillRect(p.viewport(), selBG->getBackground());
125 p.end();
126 qiRend.setDevicePixelRatio(dpr);
127 backgroundPreview->setPixmap(qiRend);
128}
129
130#include "moc_kmahjonggbackgroundselector.cpp"
static KAboutLicense byKeyword(const QString &keyword)
QString name(KAboutLicense::NameFormat formatName=ShortName) const
KConfigGroup group(const QString &group)
QString readEntry(const char *key, const char *aDefault=nullptr) const
KConfig * config()
QString name(StandardAction id)
KEDUVOCDOCUMENT_EXPORT QStringList fileNames(const QString &language=QString())
QStringList entryList(Filters filters, SortFlags sort) const const
iterator insert(const Key &key, const T &value)
T value(const Key &key) const const
void append(QList< T > &&value)
void reserve(qsizetype size)
qsizetype size() const const
void currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
int height() const const
int width() const const
QStringList locateAll(StandardLocation type, const QString &fileName, LocateOptions options)
bool isEmpty() const const
transparent
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:55:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.