KDEGames

kgamethemeselector.cpp
1 /*
2  SPDX-FileCopyrightText: 2007 Mauricio Piacentini <[email protected]>
3  SPDX-FileCopyrightText: 2007 Matt Williams <[email protected]>
4 
5  SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #include "kgamethemeselector.h"
9 
10 // own
11 #include "ui_kgamethemeselector.h"
12 #include "kgametheme.h"
13 // KF
14 #include <KConfigSkeleton>
15 #include <KNS3/DownloadDialog>
16 // Qt
17 #include <QIcon>
18 #include <QDirIterator>
19 #include <QDir>
20 #include <QStandardPaths>
21 #include <QCoreApplication>
22 
23 #if KDEGAMESPRIVATE_BUILD_DEPRECATED_SINCE(4, 9)
24 
25 class KGameThemeSelectorPrivate
26 {
27  public:
28  KGameThemeSelectorPrivate(KGameThemeSelector* parent) : q(parent) {}
29  ~KGameThemeSelectorPrivate() { qDeleteAll(themeMap); }
30 
32 
34  Ui::KGameThemeSelectorBase ui;
35  QString lookupDirectory;
36  QString groupName;
37 
38  void setupData(KConfigSkeleton* config, KGameThemeSelector::NewStuffState knsflags);
39  void findThemes(const QString &initialSelection);
40 
41  // private slots
42  void _k_updatePreview();
43  void _k_updateThemeList(const QString& strTheme);
44  void _k_openKNewStuffDialog();
45 };
46 
47 KGameThemeSelector::KGameThemeSelector(QWidget* parent, KConfigSkeleton * aconfig, KGameThemeSelector::NewStuffState knsflags, const QString &groupName, const QString &directory)
48  : QWidget(parent), d(new KGameThemeSelectorPrivate(this))
49 {
50  d->lookupDirectory = directory;
51  d->groupName = groupName;
52  d->setupData(aconfig, knsflags);
53 }
54 
55 KGameThemeSelector::~KGameThemeSelector() = default;
56 
57 void KGameThemeSelectorPrivate::setupData(KConfigSkeleton * aconfig, KGameThemeSelector::NewStuffState knsflags)
58 {
59  ui.setupUi(q);
60  ui.getNewButton->setIcon(QIcon::fromTheme( QStringLiteral( "get-hot-new-stuff" )));
61 
62  //The lineEdit widget holds our theme path for automatic connection via KConfigXT.
63  //But the user should not manipulate it directly, so we hide it.
64  ui.kcfg_Theme->hide();
65  QObject::connect(ui.kcfg_Theme, &QLineEdit::textChanged, q, [this](const QString &text) { _k_updateThemeList(text); });
66 
67  //Disable KNS button?
68  if (knsflags==KGameThemeSelector::NewStuffDisableDownload) {
69  ui.getNewButton->hide();
70  }
71 
72  //Get the last used theme path from the KConfigSkeleton
73  KConfigSkeletonItem * configItem = aconfig->findItem(QStringLiteral( "Theme" ));
74  QString lastUsedTheme = configItem->property().toString();
75 
76  //Now get our themes into the list widget
77 
78  findThemes(lastUsedTheme);
79 
80  QObject::connect(ui.getNewButton, &QAbstractButton::clicked, q, [this]() { _k_openKNewStuffDialog(); });
81 }
82 
83 void KGameThemeSelectorPrivate::findThemes(const QString &initialSelection)
84 {
85  qDeleteAll(themeMap);
86  themeMap.clear();
87 
88  //Disconnect the themeList as we are going to clear it and do not want previews generated
89  ui.themeList->disconnect();
90  ui.themeList->clear();
91  ui.themeList->setSortingEnabled(true);
92 
93  QStringList themesAvailable;
94 
95  const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QCoreApplication::applicationName() + QLatin1Char( '/' ) + lookupDirectory, QStandardPaths::LocateDirectory); //Added subdirectory for finding gamethemeselector resources
96  for (const QString& dir : dirs) {
97  QDirIterator it(dir, QStringList() << QStringLiteral("*.desktop"), QDir::NoFilter, QDirIterator::Subdirectories);
98  while (it.hasNext()) {
99  QFileInfo fileInfo(it.next());
100  const QString filePath = QDir(dir).relativeFilePath(fileInfo.filePath());
101  themesAvailable.append(filePath);
102  }
103  }
104 
105  bool initialFound = false;
106  for (const QString &file : qAsConst(themesAvailable))
107  {
108  const QString themePath = lookupDirectory + QLatin1Char( '/' ) + file;
109  KGameTheme* atheme = new KGameTheme(groupName);
110 
111  if (atheme->load(themePath)) {
112  QString themeName = atheme->themeProperty(QStringLiteral( "Name" ));
113  //Add underscores to avoid duplicate names.
114  while (themeMap.contains(themeName))
115  themeName += QLatin1Char( '_' );
116  themeMap.insert(themeName, atheme);
117  QListWidgetItem * item = new QListWidgetItem(themeName, ui.themeList);
118 
119  //Find if this is our currently configured theme
120  if (themePath==initialSelection) {
121  initialFound = true;
122  ui.themeList->setCurrentItem(item);
123  _k_updatePreview();
124  }
125  } else {
126  delete atheme;
127  }
128  }
129 
130  if (!initialFound)
131  {
132  // TODO change this if we ever change KGameTheme::loadDefault
133  QString defaultPath = QStringLiteral( "themes/default.desktop" );
134  for (KGameTheme* theme : qAsConst(themeMap)) {
135  if (theme->path().endsWith(defaultPath))
136  {
137  const QList<QListWidgetItem *> itemList = ui.themeList->findItems(theme->themeProperty(QStringLiteral( "Name" )), Qt::MatchExactly);
138  // never can be != 1 but better safe than sorry
139  if (itemList.count() == 1)
140  {
141  ui.themeList->setCurrentItem(itemList.first());
142  _k_updatePreview();
143  }
144  }
145  }
146  }
147 
148  //Reconnect the themeList
149  QObject::connect(ui.themeList, &QListWidget::currentItemChanged, q, [this]() { _k_updatePreview(); });
150 }
151 
152 void KGameThemeSelectorPrivate::_k_updatePreview()
153 {
154  KGameTheme * seltheme = themeMap.value(ui.themeList->currentItem()->text());
155  //Sanity checkings. Should not happen.
156  if (!seltheme) return;
157  if (seltheme->path() == ui.kcfg_Theme->text()) {
158  return;
159  }
160  ui.kcfg_Theme->setText(seltheme->fileName());
161 
162  QString authstr(QStringLiteral( "Author" ));
163  QString contactstr(QStringLiteral( "AuthorEmail" ));
164  QString descstr(QStringLiteral( "Description" ));
165  QString emailstr;
166  if (!seltheme->themeProperty(contactstr).isEmpty() ) {
167  emailstr = QStringLiteral("<a href=\"mailto:%1\">%1</a>").arg(seltheme->themeProperty(contactstr));
168  }
169 
170  ui.themeAuthor->setText(seltheme->themeProperty(authstr));
171  ui.themeContact->setText(emailstr);
172  ui.themeDescription->setText(seltheme->themeProperty(descstr));
173 
174  //Draw the preview
175  QPixmap pix(seltheme->preview());
176  ui.themePreview->setPixmap(pix.scaled(ui.themePreview->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation));
177 }
178 
179 void KGameThemeSelectorPrivate::_k_updateThemeList(const QString& strTheme)
180 {
181  //find theme and set selection to the current theme; happens when pressing "Default"
182  QListWidgetItem * currentItem = ui.themeList->currentItem();
183  if(!currentItem || themeMap.value(currentItem->text())->fileName() != strTheme)
184  {
185  for(int i = 0; i < ui.themeList->count(); i++)
186  {
187  if(themeMap.value(ui.themeList->item(i)->text())->fileName() == strTheme)
188  {
189  ui.themeList->setCurrentItem(ui.themeList->item(i));
190  break;
191  }
192  }
193  }
194 }
195 
196 void KGameThemeSelectorPrivate::_k_openKNewStuffDialog()
197 {
199  dialog->exec();
200  if ( dialog && !dialog->changedEntries().isEmpty() )
201  findThemes( ui.kcfg_Theme->text() );
202  delete dialog;
203 }
204 
205 #include "moc_kgamethemeselector.cpp"
206 
207 #endif
QString next()
QString relativeFilePath(const QString &fileName) const const
MatchExactly
QStringList locateAll(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
QString path() const
Definition: kgametheme.cpp:131
void textChanged(const QString &text)
virtual QString themeProperty(const QString &key) const
Possible keys:
Definition: kgametheme.cpp:176
Class for loading theme files.
Definition: kgametheme.h:40
KConfigSkeletonItem * findItem(const QString &name) const
int count(const T &value) const const
void append(const T &value)
bool isEmpty() const const
void clicked(bool checked)
QString fileName() const
Definition: kgametheme.cpp:149
T & first()
virtual bool load(const QString &file)
Load a specific theme file.
Definition: kgametheme.cpp:57
KeepAspectRatio
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QIcon fromTheme(const QString &name)
QPixmap preview() const
Definition: kgametheme.cpp:167
A widget used to select the game&#39;s theme.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
bool hasNext() const const
virtual QVariant property() const =0
QString toString() const const
void currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
SmoothTransformation
QString text() const const
QString applicationName()
KGameThemeSelector(QWidget *parent, KConfigSkeleton *config, KGameThemeSelector::NewStuffState knsflags=KGameThemeSelector::NewStuffEnableDownload, const QString &groupName=QStringLiteral("KGameTheme"), const QString &directory=QStringLiteral("themes"))
Load a specific theme file.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Dec 7 2021 22:34:15 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.