LibKmahjongg

kmahjonggtilesetselector.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 "kmahjonggtilesetselector.h"
9
10// Qt
11#include <QDir>
12#include <QPainter>
13#include <QStandardPaths>
14
15// KF
16#include <KAboutLicense>
17#include <KLocalizedString>
18
19// LibKMahjongg
20#include "kmahjonggtileset.h"
21
22KMahjonggTilesetSelector::KMahjonggTilesetSelector(QWidget *parent, KConfigSkeleton *aconfig)
23 : QWidget(parent)
24{
25 setupUi(this);
26 setupData(aconfig);
27}
28
29KMahjonggTilesetSelector::~KMahjonggTilesetSelector()
30{
31 qDeleteAll(tilesetMap);
32}
33
34void KMahjonggTilesetSelector::setupData(KConfigSkeleton *aconfig)
35{
36 // Get our currently configured Tileset entry
37 KConfig *config = aconfig->config();
38 KConfigGroup group = config->group(QStringLiteral("General"));
39 QString initialGroup = group.readEntry("Tileset_file");
40
41 // The lineEdit widget holds our tileset path, but the user does not manipulate it directly
42 kcfg_TileSet->hide();
43
44 // This will also load our resourcedir if it is not done already
46
47 // Now get our tilesets into a list
48 QStringList tilesAvailable;
49 const QStringList dirs =
51 for (const QString &dir : dirs) {
52 const QStringList fileNames = QDir(dir).entryList({QStringLiteral("*.desktop")});
53 tilesAvailable.reserve(tilesAvailable.size() + fileNames.size());
54 for (const QString &file : fileNames) {
55 tilesAvailable.append(dir + QLatin1Char('/') + file);
56 }
57 }
58
59 int numvalidentries = 0;
60 for (const QString &atileset : std::as_const(tilesAvailable)) {
61 auto *aset = new KMahjonggTileset();
62 if (aset->loadTileset(atileset)) {
63 const QString name = aset->name();
64 tilesetMap.insert(name, aset);
65 tilesetList->addItem(name);
66 // Find if this is our currently configured Tileset
67 if (atileset == initialGroup) {
68 // Select current entry
69 tilesetList->setCurrentRow(numvalidentries);
70 tilesetChanged();
71 }
72 ++numvalidentries;
73 } else {
74 delete aset;
75 }
76 }
77
78 connect(tilesetList, &QListWidget::currentItemChanged, this, &KMahjonggTilesetSelector::tilesetChanged);
79}
80
81void KMahjonggTilesetSelector::tilesetChanged()
82{
83 KMahjonggTileset *selTileset = tilesetMap.value(tilesetList->currentItem()->text());
84 // Sanity checkings. Should not happen.
85 if (selTileset == nullptr) {
86 return;
87 }
88 if (selTileset->path() == kcfg_TileSet->text()) {
89 return;
90 }
91
92 kcfg_TileSet->setText(selTileset->path());
93 tilesetAuthor->setText(selTileset->authorName());
94 tilesetContact->setText(selTileset->authorEmailAddress());
95 tilesetDescription->setText(selTileset->description());
96 tilesetVersion->setText(selTileset->version());
97 QString website = selTileset->website();
98 if (!website.isEmpty()) {
99 website = QLatin1String("<a href=\"") + website + QLatin1String("\">") + website + QLatin1String("</a>");
100 }
101 tilesetWebsite->setText(website);
102 tilesetCopyright->setText(selTileset->copyrightText());
103 const QString licenseName = KAboutLicense::byKeyword(selTileset->license()).name(KAboutLicense::FullName);
104 tilesetLicense->setText(licenseName);
105
106 // Make sure SVG is loaded when graphics is selected
107 if (!selTileset->loadGraphics()) {
108 return;
109 }
110 const qreal dpr = qApp->devicePixelRatio();
111 // Let the tileset calculate its ideal size for the preview area, but reduce the margins a bit (pass oversized drawing area)
112 const QSize previewSize = tilesetPreview->size() * dpr;
113 const QSize tilesize = selTileset->preferredTileSize(previewSize * 1.3, 1, 1);
114 selTileset->reloadTileset(tilesize);
115 // Draw the preview
116 QPixmap qiRend(previewSize);
117 qiRend.fill(Qt::transparent);
118 QPainter p(&qiRend);
119 // Calculate the margins to center the tile
120 const QSize margin = (previewSize - tilesize) / 2;
121 // Draw unselected tile and first tileface
122 p.drawPixmap(margin.width(), margin.height(), selTileset->unselectedTile(1));
123 p.drawPixmap(margin.width(), margin.height(), selTileset->tileface(0));
124 p.end();
125 qiRend.setDevicePixelRatio(dpr);
126 tilesetPreview->setPixmap(qiRend);
127}
128
129#include "moc_kmahjonggtilesetselector.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.