kgamethemeselector.cpp

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

Generated on Sun Mar 16 08:02:53 2008 for Libkdegames by  doxygen 1.5.3