LibKmahjongg

kmahjonggbackgroundselector.cpp
1 /*
2  SPDX-FileCopyrightText: 2006 Mauricio Piacentini <[email protected]>
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 <KLocalizedString>
16 
17 // LibKMahjongg
18 #include "kmahjonggbackground.h"
19 
20 KMahjonggBackgroundSelector::KMahjonggBackgroundSelector(QWidget * parent, KConfigSkeleton * aconfig)
21  : QWidget(parent)
22 {
23  setupUi(this);
24  setupData(aconfig);
25 }
26 
27 KMahjonggBackgroundSelector::~KMahjonggBackgroundSelector()
28 {
29  qDeleteAll(backgroundMap);
30 }
31 
32 void KMahjonggBackgroundSelector::setupData(KConfigSkeleton * aconfig)
33 {
34  //Get our currently configured background entry
35  KConfig * config = aconfig->config();
36  KConfigGroup group = config->group("General");
37  QString initialGroup = group.readEntry("Background_file");
38 
39  //The lineEdit widget holds our bg path, but the user does not manipulate it directly
40  kcfg_Background->hide();
41 
43 
44  //Now get our backgrounds into a list
45  QStringList bgsAvailable;
47  for (const QString & dir : dirs) {
48  const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.desktop"));
49  for (const QString & file : fileNames) {
50  bgsAvailable.append(dir + QLatin1Char('/') + file);
51  }
52  }
53 
54  QLatin1String namestr("Name");
55  int numvalidentries = 0;
56  for (int i = 0; i < bgsAvailable.size(); ++i) {
58  QString bgpath = bgsAvailable.at(i);
59  if (abg->load(bgpath, backgroundPreview->width(), backgroundPreview->height())) {
60  backgroundMap.insert(abg->authorProperty(namestr), abg);
61  backgroundList->addItem(abg->authorProperty(namestr));
62  //Find if this is our currently configured background
63  if (bgpath == initialGroup) {
64  //Select current entry
65  backgroundList->setCurrentRow(numvalidentries);
66  backgroundChanged();
67  }
68  ++numvalidentries;
69  } else {
70  delete abg;
71  }
72  }
73 
74  connect(backgroundList, &QListWidget::currentItemChanged, this, &KMahjonggBackgroundSelector::backgroundChanged);
75 }
76 
77 void KMahjonggBackgroundSelector::backgroundChanged()
78 {
79  KMahjonggBackground * selBG = backgroundMap.value(backgroundList->currentItem()->text());
80  //Sanity checkings. Should not happen.
81  if (selBG == nullptr) {
82  return;
83  }
84  if (selBG->path() == kcfg_Background->text()) {
85  return;
86  }
87  QLatin1String authstr("Author");
88  QLatin1String contactstr("AuthorEmail");
89  QLatin1String descstr("Description");
90  kcfg_Background->setText(selBG->path());
91  backgroundAuthor->setText(selBG->authorProperty(authstr));
92  backgroundContact->setText(selBG->authorProperty(contactstr));
93  backgroundDescription->setText(selBG->authorProperty(descstr));
94 
95  if (selBG->authorProperty(QStringLiteral("Plain")) == QLatin1String("1")) {
96  backgroundPreview->setPixmap(QPixmap());
97  return;
98  }
99 
100  //Make sure SVG is loaded when graphics is selected
101  if (!selBG->loadGraphics()) {
102  return;
103  }
104 
105  //Draw the preview
106  //TODO here: add code to load and keep proportions for non-tiled content?
107  QImage qiRend(backgroundPreview->size(), QImage::Format_ARGB32_Premultiplied);
108  qiRend.fill(0);
109  QPainter p(&qiRend);
110  p.fillRect(p.viewport(), selBG->getBackground());
111  p.end();
112  backgroundPreview->setPixmap(QPixmap::fromImage(qiRend));
113 }
void append(const T &value)
QString readEntry(const char *key, const char *aDefault=nullptr) const
QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags)
Format_ARGB32_Premultiplied
void currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
int size() const const
const T & at(int i) const const
KSharedConfigPtr config()
KStandardDirs * dirs()
KConfig * config()
QStringList locateAll(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
QStringList entryList(QDir::Filters filters, QDir::SortFlags sort) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 04:09:42 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.