• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libkdegames

kgamethemeselector.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2007 Mauricio Piacentini  <mauricio@tabuleiro.com>
00003     Copyright (C) 2007 Matt Williams   <matt@milliams.com>
00004 
00005     This library is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018 */
00019 
00020 #include "kgamethemeselector.h"
00021 
00022 #include <KLocale>
00023 #include <KStandardDirs>
00024 #include <KConfigSkeleton>
00025 #include <knewstuff2/engine.h>
00026 #include <KComponentData>
00027 
00028 #include "ui_kgamethemeselector.h"
00029 #include "kgametheme.h"
00030 
00031 class KGameThemeSelector::KGameThemeSelectorPrivate
00032 {
00033     public:
00034         KGameThemeSelectorPrivate(KGameThemeSelector* parent) : q(parent) {}
00035         ~KGameThemeSelectorPrivate() { qDeleteAll(themeMap); }
00036 
00037         KGameThemeSelector* q;
00038 
00039         QMap<QString, KGameTheme*> themeMap;
00040         Ui::KGameThemeSelectorBase ui;
00041         QString lookupDirectory;
00042         QString groupName;
00043 
00044         void setupData(KConfigSkeleton* config, KGameThemeSelector::NewStuffState knsflags);
00045         void findThemes(const QString &initialGroup);
00046 
00047         // private slots
00048         void _k_updatePreview();
00049         void _k_openKNewStuffDialog();
00050 };
00051 
00052 KGameThemeSelector::KGameThemeSelector(QWidget* parent, KConfigSkeleton * aconfig, KGameThemeSelector::NewStuffState knsflags, const QString &groupName, const QString &directory)
00053     : QWidget(parent), d(new KGameThemeSelectorPrivate(this))
00054 {
00055     d->lookupDirectory = directory;
00056     d->groupName = groupName;
00057     d->setupData(aconfig, knsflags);
00058 }
00059 
00060 KGameThemeSelector::~KGameThemeSelector()
00061 {
00062     delete d;
00063 }
00064 
00065 void KGameThemeSelector::KGameThemeSelectorPrivate::setupData(KConfigSkeleton * aconfig, KGameThemeSelector::NewStuffState knsflags)
00066 {
00067     ui.setupUi(q);
00068     
00069     //Get our currently configured Tileset entry
00070     KConfig * config = aconfig->config();
00071     KConfigGroup group = config->group("General");
00072     QString initialGroup = group.readEntry("Theme"); //Should be someting like "themes/default.desktop"
00073 
00074     //The lineEdit widget holds our bg path, but the user does not manipulate it directly
00075     ui.kcfg_Theme->hide();
00076     
00077     //Disable KNS button?
00078     if (knsflags==KGameThemeSelector::NewStuffDisableDownload) {
00079       ui.getNewButton->hide();
00080     }
00081 
00082     //Now get our tilesets into a list
00083     KGlobal::dirs()->addResourceType("gamethemeselector", "data", KGlobal::mainComponent().componentName() + '/' + lookupDirectory + '/');
00084     findThemes(initialGroup);
00085     connect(ui.getNewButton, SIGNAL(clicked()), q, SLOT(_k_openKNewStuffDialog()));
00086 }
00087 
00088 void KGameThemeSelector::KGameThemeSelectorPrivate::findThemes(const QString &initialGroup)
00089 {
00090     
00091     QStringList themesAvailable;
00092     KGlobal::dirs()->findAllResources("gamethemeselector", QString("*.desktop"), KStandardDirs::Recursive, themesAvailable);
00093     QString namestr("Name");
00094     int numvalidentries = 0;
00095     qDeleteAll(themeMap.values());
00096     themeMap.clear();
00097     //Disconnect the themeList as we are going to clear it and do not want previews generated
00098     ui.themeList->disconnect();
00099     ui.themeList->clear();
00100     
00101     for (int i = 0; i < themesAvailable.size(); ++i)
00102     {
00103       KGameTheme* atheme = new KGameTheme(groupName);
00104       QString themepath = lookupDirectory + '/' + themesAvailable.at(i);
00105       if (atheme->load(themepath)) {
00106         themeMap.insert(atheme->themeProperty(namestr), atheme);
00107         ui.themeList->addItem(atheme->themeProperty(namestr));
00108         //Find if this is our currently configured Theme
00109         if (themepath==initialGroup) {
00110                     //Select current entry
00111           ui.themeList->setCurrentRow(numvalidentries);
00112           _k_updatePreview();
00113         }
00114         ++numvalidentries;
00115       } else {
00116         delete atheme;
00117       }
00118     }
00119     ui.themeList->sortItems();
00120     //reconnect the themeList
00121     connect(ui.themeList, SIGNAL(currentItemChanged ( QListWidgetItem * , QListWidgetItem * )), q, SLOT(_k_updatePreview()));
00122 }
00123 
00124 void KGameThemeSelector::KGameThemeSelectorPrivate::_k_updatePreview()
00125 {
00126     KGameTheme * seltheme = themeMap.value(ui.themeList->currentItem()->text());
00127         //Sanity checkings. Should not happen.
00128     if (!seltheme) return;
00129     if (seltheme->path() == ui.kcfg_Theme->text()) {
00130         return;
00131     }
00132     ui.kcfg_Theme->setText(seltheme->fileName());
00133 
00134     QString authstr("Author");
00135     QString contactstr("AuthorEmail");
00136     QString descstr("Description");
00137     QString emailstr;
00138     if (!seltheme->themeProperty(contactstr).isEmpty() ) {
00139         emailstr = QString("<a href=\"mailto:%1\">%1</a>").arg(seltheme->themeProperty(contactstr));
00140     }
00141 
00142     ui.themeAuthor->setText(seltheme->themeProperty(authstr));
00143     ui.themeContact->setText(emailstr);
00144     ui.themeDescription->setText(seltheme->themeProperty(descstr));
00145 
00146     //Draw the preview
00147     QPixmap pix(seltheme->preview());
00148     ui.themePreview->setPixmap(pix.scaled(ui.themePreview->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation));
00149 
00150 }
00151 
00152 void KGameThemeSelector::KGameThemeSelectorPrivate::_k_openKNewStuffDialog()
00153 {
00154     KNS::Entry::List entries = KNS::Engine::download();
00155     //rescan themes directory
00156     QString currentthemepath = ui.kcfg_Theme->text();
00157     if (entries.size()>0) findThemes(currentthemepath);
00158     //Needed as the static KNS constructor made copies
00159     qDeleteAll(entries);
00160 }
00161 
00162 #include "kgamethemeselector.moc"

libkdegames

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

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal