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

parley

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