libkdegames
kgamethemeselector.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
00070 KConfig * config = aconfig->config();
00071 KConfigGroup group = config->group("General");
00072 QString initialGroup = group.readEntry("Theme");
00073
00074
00075 ui.kcfg_Theme->hide();
00076
00077
00078 if (knsflags==KGameThemeSelector::NewStuffDisableDownload) {
00079 ui.getNewButton->hide();
00080 }
00081
00082
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
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
00109 if (themepath==initialGroup) {
00110
00111 ui.themeList->setCurrentRow(numvalidentries);
00112 _k_updatePreview();
00113 }
00114 ++numvalidentries;
00115 } else {
00116 delete atheme;
00117 }
00118 }
00119 ui.themeList->sortItems();
00120
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
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
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
00156 QString currentthemepath = ui.kcfg_Theme->text();
00157 if (entries.size()>0) findThemes(currentthemepath);
00158
00159 qDeleteAll(entries);
00160 }
00161
00162 #include "kgamethemeselector.moc"