• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

parley

  • sources
  • kde-4.14
  • kdeedu
  • parley
  • src
  • settings
  • kgametheme
kgamethemeselector.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 Mauricio Piacentini <mauricio@tabuleiro.com>
3  Copyright (C) 2007 Matt Williams <matt@milliams.com>
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 <KLocale>
23 #include <KStandardDirs>
24 #include <KConfigSkeleton>
25 #include <KComponentData>
26 #include <KNS3/DownloadDialog>
27 
28 #include "ui_kgamethemeselector.h"
29 #include "kgametheme.h"
30 
31 class KGameThemeSelector::KGameThemeSelectorPrivate
32 {
33 public:
34  KGameThemeSelectorPrivate(KGameThemeSelector* parent) : q(parent) {}
35  ~KGameThemeSelectorPrivate() {
36  qDeleteAll(themeMap);
37  }
38 
39  KGameThemeSelector* q;
40 
41  QMap<QString, KGameTheme*> themeMap;
42  Ui::KGameThemeSelectorBase ui;
43  QString lookupDirectory;
44  QString groupName;
45 
46  void setupData(KConfigSkeleton* config, KGameThemeSelector::NewStuffState knsflags);
47  void findThemes(const QString &initialSelection);
48 
49  // private slots
50  void _k_updatePreview();
51  void _k_updateThemeList(const QString& strTheme);
52  void _k_openKNewStuffDialog();
53 };
54 
55 KGameThemeSelector::KGameThemeSelector(QWidget* parent, KConfigSkeleton * aconfig, KGameThemeSelector::NewStuffState knsflags, const QString &groupName, const QString &directory)
56  : QWidget(parent), d(new KGameThemeSelectorPrivate(this))
57 {
58  d->lookupDirectory = directory;
59  d->groupName = groupName;
60  d->setupData(aconfig, knsflags);
61 }
62 
63 KGameThemeSelector::~KGameThemeSelector()
64 {
65  delete d;
66 }
67 
68 void KGameThemeSelector::KGameThemeSelectorPrivate::setupData(KConfigSkeleton * aconfig, KGameThemeSelector::NewStuffState knsflags)
69 {
70  ui.setupUi(q);
71  ui.getNewButton->setIcon(KIcon("get-hot-new-stuff"));
72 
73  //The lineEdit widget holds our theme path for automatic connection via KConfigXT.
74  //But the user should not manipulate it directly, so we hide it.
75  ui.kcfg_Theme->hide();
76  connect(ui.kcfg_Theme, SIGNAL(textChanged(const QString&)), q, SLOT(_k_updateThemeList(const QString&)));
77 
78  //Disable KNS button?
79  if (knsflags == KGameThemeSelector::NewStuffDisableDownload) {
80  ui.getNewButton->hide();
81  }
82 
83  //Get the last used theme path from the KConfigSkeleton
84  KConfigSkeletonItem * configItem = aconfig->findItem("Theme");
85  QString lastUsedTheme = configItem->property().toString();
86 
87  //Now get our themes into the list widget
88  KGlobal::dirs()->addResourceType("gamethemeselector", "data", KGlobal::mainComponent().componentName() + '/' + lookupDirectory + '/');
89  findThemes(lastUsedTheme);
90 
91  connect(ui.getNewButton, SIGNAL(clicked()), q, SLOT(_k_openKNewStuffDialog()));
92 }
93 
94 void KGameThemeSelector::KGameThemeSelectorPrivate::findThemes(const QString &initialSelection)
95 {
96  qDeleteAll(themeMap.values());
97  themeMap.clear();
98 
99  //Disconnect the themeList as we are going to clear it and do not want previews generated
100  ui.themeList->disconnect();
101  ui.themeList->clear();
102  ui.themeList->setSortingEnabled(true);
103 
104  QStringList themesAvailable;
105  KGlobal::dirs()->findAllResources("gamethemeselector", "*.desktop", KStandardDirs::Recursive, themesAvailable);
106 
107  bool initialFound = false;
108  foreach(const QString & file, themesAvailable) {
109  QString themePath = lookupDirectory + '/' + file;
110  KGameTheme* atheme = new KGameTheme(groupName);
111 
112  if (atheme->load(themePath)) {
113  QString themeName = atheme->themeProperty("Name");
114  //Add underscores to avoid duplicate names.
115  while (themeMap.contains(themeName))
116  themeName += '_';
117  themeMap.insert(themeName, atheme);
118  QListWidgetItem * item = new QListWidgetItem(themeName, ui.themeList);
119 
120  //Find if this is our currently configured theme
121  if (themePath == initialSelection) {
122  initialFound = true;
123  ui.themeList->setCurrentItem(item);
124  _k_updatePreview();
125  }
126  } else {
127  delete atheme;
128  }
129  }
130 
131  if (!initialFound) {
132  // TODO change this if we ever change KGameTheme::loadDefault
133  QString defaultPath = "themes/default.desktop";
134  foreach(KGameTheme * theme, themeMap) {
135  if (theme->path().endsWith(defaultPath)) {
136  const QList<QListWidgetItem *> itemList = ui.themeList->findItems(theme->themeProperty("Name"), Qt::MatchExactly);
137  // never can be != 1 but better safe than sorry
138  if (itemList.count() == 1) {
139  ui.themeList->setCurrentItem(itemList.first());
140  _k_updatePreview();
141  }
142  }
143  }
144  }
145 
146  //Reconnect the themeList
147  connect(ui.themeList, SIGNAL(currentItemChanged(QListWidgetItem * , QListWidgetItem *)), q, SLOT(_k_updatePreview()));
148 }
149 
150 void KGameThemeSelector::KGameThemeSelectorPrivate::_k_updatePreview()
151 {
152  KGameTheme * seltheme = themeMap.value(ui.themeList->currentItem()->text());
153  //Sanity checkings. Should not happen.
154  if (!seltheme) return;
155  if (seltheme->path() == ui.kcfg_Theme->text()) {
156  return;
157  }
158  ui.kcfg_Theme->setText(seltheme->fileName());
159 
160  QString authstr("Author");
161  QString contactstr("AuthorEmail");
162  QString descstr("Description");
163  QString emailstr;
164  if (!seltheme->themeProperty(contactstr).isEmpty()) {
165  emailstr = QString("<a href=\"mailto:%1\">%1</a>").arg(seltheme->themeProperty(contactstr));
166  }
167 
168  ui.themeAuthor->setText(seltheme->themeProperty(authstr));
169  ui.themeContact->setText(emailstr);
170  ui.themeDescription->setText(seltheme->themeProperty(descstr));
171 
172  //Draw the preview
173  QPixmap pix(seltheme->preview());
174  ui.themePreview->setPixmap(pix.scaled(ui.themePreview->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
175 }
176 
177 void KGameThemeSelector::KGameThemeSelectorPrivate::_k_updateThemeList(const QString& strTheme)
178 {
179  //find theme and set selection to the current theme; happens when pressing "Default"
180  QListWidgetItem * currentItem = ui.themeList->currentItem();
181  if (!currentItem || themeMap.value(currentItem->text())->fileName() != strTheme) {
182  for (int i = 0; i < ui.themeList->count(); i++) {
183  if (themeMap.value(ui.themeList->item(i)->text())->fileName() == strTheme) {
184  ui.themeList->setCurrentItem(ui.themeList->item(i));
185  break;
186  }
187  }
188  }
189 }
190 
191 void KGameThemeSelector::KGameThemeSelectorPrivate::_k_openKNewStuffDialog()
192 {
193  KNS3::DownloadDialog dialog("parley-themes.knsrc", q);
194  dialog.exec();
195  if (!dialog.changedEntries().isEmpty())
196  findThemes(ui.kcfg_Theme->text());
197 }
198 
199 #include "kgamethemeselector.moc"
QWidget
kgamethemeselector.h
QMap< QString, KGameTheme * >
KGameTheme::path
QString path() const
Definition: kgametheme.cpp:139
QListWidgetItem
KGameTheme::themeProperty
virtual QString themeProperty(const QString &key) const
Possible keys:
Definition: kgametheme.cpp:175
KGameTheme
Class for loading theme files.
Definition: kgametheme.h:42
KConfigSkeleton
QList::count
int count(const T &value) const
QString::insert
QString & insert(int position, QChar ch)
QString::isEmpty
bool isEmpty() const
KGameTheme::fileName
QString fileName() const
Definition: kgametheme.cpp:148
QString::endsWith
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
QList::first
T & first()
QString
QList
QStringList
QPixmap
KGameThemeSelector::KGameThemeSelector
KGameThemeSelector(QWidget *parent, KConfigSkeleton *config, KGameThemeSelector::NewStuffState knsflags=KGameThemeSelector::NewStuffEnableDownload, const QString &groupName=QLatin1String("KGameTheme"), const QString &directory=QLatin1String("themes"))
Load a specific theme file.
Definition: kgamethemeselector.cpp:55
KGameTheme::load
virtual bool load(const QString &file)
Load a specific theme file.
Definition: kgametheme.cpp:67
KGameThemeSelector::~KGameThemeSelector
virtual ~KGameThemeSelector()
Definition: kgamethemeselector.cpp:63
KGameThemeSelector::NewStuffDisableDownload
Definition: kgamethemeselector.h:53
kgametheme.h
KGameTheme::preview
QPixmap preview() const
Definition: kgametheme.cpp:166
KGameThemeSelector
A widget used to select the game's theme.
Definition: kgamethemeselector.h:48
QObject::parent
QObject * parent() const
KGameThemeSelector::NewStuffState
NewStuffState
Definition: kgamethemeselector.h:52
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QListWidgetItem::text
QString text() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:15:56 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

parley

Skip menu "parley"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal