KDEGames

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

KDE's Doxygen guidelines are available online.