1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
/*
Copyright (C) 2007 Mauricio Piacentini <mauricio@tabuleiro.com>
Copyright (C) 2007 Matt Williams <matt@milliams.com>
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "kgamethemeselector.h"
#include <QIcon>
#include <KConfigSkeleton>
#include <KNS3/DownloadDialog>
#include <QDirIterator>
#include <QDir>
#include <QStandardPaths>
#include <QCoreApplication>
#include "ui_kgamethemeselector.h"
#include "kgametheme.h"
class KGameThemeSelector::KGameThemeSelectorPrivate
{
public:
KGameThemeSelectorPrivate(KGameThemeSelector* parent) : q(parent) {}
~KGameThemeSelectorPrivate() { qDeleteAll(themeMap); }
KGameThemeSelector* q;
QMap<QString, KGameTheme*> themeMap;
Ui::KGameThemeSelectorBase ui;
QString lookupDirectory;
QString groupName;
void setupData(KConfigSkeleton* config, KGameThemeSelector::NewStuffState knsflags);
void findThemes(const QString &initialSelection);
// private slots
void _k_updatePreview();
void _k_updateThemeList(const QString& strTheme);
void _k_openKNewStuffDialog();
};
KGameThemeSelector::KGameThemeSelector(QWidget* parent, KConfigSkeleton * aconfig, KGameThemeSelector::NewStuffState knsflags, const QString &groupName, const QString &directory)
: QWidget(parent), d(new KGameThemeSelectorPrivate(this))
{
d->lookupDirectory = directory;
d->groupName = groupName;
d->setupData(aconfig, knsflags);
}
KGameThemeSelector::~KGameThemeSelector()
{
delete d;
}
void KGameThemeSelector::KGameThemeSelectorPrivate::setupData(KConfigSkeleton * aconfig, KGameThemeSelector::NewStuffState knsflags)
{
ui.setupUi(q);
ui.getNewButton->setIcon(QIcon::fromTheme( QStringLiteral( "get-hot-new-stuff" )));
//The lineEdit widget holds our theme path for automatic connection via KConfigXT.
//But the user should not manipulate it directly, so we hide it.
ui.kcfg_Theme->hide();
connect(ui.kcfg_Theme, SIGNAL(textChanged(QString)), q, SLOT(_k_updateThemeList(QString)));
//Disable KNS button?
if (knsflags==KGameThemeSelector::NewStuffDisableDownload) {
ui.getNewButton->hide();
}
//Get the last used theme path from the KConfigSkeleton
KConfigSkeletonItem * configItem = aconfig->findItem(QStringLiteral( "Theme" ));
QString lastUsedTheme = configItem->property().toString();
//Now get our themes into the list widget
findThemes(lastUsedTheme);
connect(ui.getNewButton, SIGNAL(clicked()), q, SLOT(_k_openKNewStuffDialog()));
}
void KGameThemeSelector::KGameThemeSelectorPrivate::findThemes(const QString &initialSelection)
{
qDeleteAll(themeMap.values());
themeMap.clear();
//Disconnect the themeList as we are going to clear it and do not want previews generated
ui.themeList->disconnect();
ui.themeList->clear();
ui.themeList->setSortingEnabled(true);
QStringList themesAvailable;
const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QCoreApplication::applicationName() + QLatin1Char( '/' ) + lookupDirectory, QStandardPaths::LocateDirectory); //Added subdirectory for finding gamethemeselector resources
for (const QString& dir : dirs) {
QDirIterator it(dir, QStringList() << QStringLiteral("*.desktop"), QDir::NoFilter, QDirIterator::Subdirectories);
while (it.hasNext()) {
QFileInfo fileInfo(it.next());
const QString filePath = QDir(dir).relativeFilePath(fileInfo.filePath());
themesAvailable.append(filePath);
}
}
bool initialFound = false;
for (const QString &file : qAsConst(themesAvailable))
{
const QString themePath = lookupDirectory + QLatin1Char( '/' ) + file;
KGameTheme* atheme = new KGameTheme(groupName);
if (atheme->load(themePath)) {
QString themeName = atheme->themeProperty(QStringLiteral( "Name" ));
//Add underscores to avoid duplicate names.
while (themeMap.contains(themeName))
themeName += QLatin1Char( '_' );
themeMap.insert(themeName, atheme);
QListWidgetItem * item = new QListWidgetItem(themeName, ui.themeList);
//Find if this is our currently configured theme
if (themePath==initialSelection) {
initialFound = true;
ui.themeList->setCurrentItem(item);
_k_updatePreview();
}
} else {
delete atheme;
}
}
if (!initialFound)
{
// TODO change this if we ever change KGameTheme::loadDefault
QString defaultPath = QStringLiteral( "themes/default.desktop" );
for (KGameTheme* theme : qAsConst(themeMap)) {
if (theme->path().endsWith(defaultPath))
{
const QList<QListWidgetItem *> itemList = ui.themeList->findItems(theme->themeProperty(QStringLiteral( "Name" )), Qt::MatchExactly);
// never can be != 1 but better safe than sorry
if (itemList.count() == 1)
{
ui.themeList->setCurrentItem(itemList.first());
_k_updatePreview();
}
}
}
}
//Reconnect the themeList
connect(ui.themeList, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), q, SLOT(_k_updatePreview()));
}
void KGameThemeSelector::KGameThemeSelectorPrivate::_k_updatePreview()
{
KGameTheme * seltheme = themeMap.value(ui.themeList->currentItem()->text());
//Sanity checkings. Should not happen.
if (!seltheme) return;
if (seltheme->path() == ui.kcfg_Theme->text()) {
return;
}
ui.kcfg_Theme->setText(seltheme->fileName());
QString authstr(QStringLiteral( "Author" ));
QString contactstr(QStringLiteral( "AuthorEmail" ));
QString descstr(QStringLiteral( "Description" ));
QString emailstr;
if (!seltheme->themeProperty(contactstr).isEmpty() ) {
emailstr = QString::fromLatin1( "<a href=\"mailto:%1\">%1</a>").arg(seltheme->themeProperty(contactstr));
}
ui.themeAuthor->setText(seltheme->themeProperty(authstr));
ui.themeContact->setText(emailstr);
ui.themeDescription->setText(seltheme->themeProperty(descstr));
//Draw the preview
QPixmap pix(seltheme->preview());
ui.themePreview->setPixmap(pix.scaled(ui.themePreview->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation));
}
void KGameThemeSelector::KGameThemeSelectorPrivate::_k_updateThemeList(const QString& strTheme)
{
//find theme and set selection to the current theme; happens when pressing "Default"
QListWidgetItem * currentItem = ui.themeList->currentItem();
if(!currentItem || themeMap.value(currentItem->text())->fileName() != strTheme)
{
for(int i = 0; i < ui.themeList->count(); i++)
{
if(themeMap.value(ui.themeList->item(i)->text())->fileName() == strTheme)
{
ui.themeList->setCurrentItem(ui.themeList->item(i));
break;
}
}
}
}
void KGameThemeSelector::KGameThemeSelectorPrivate::_k_openKNewStuffDialog()
{
QPointer<KNS3::DownloadDialog> dialog(new KNS3::DownloadDialog( q ));
dialog->exec();
if ( dialog && !dialog->changedEntries().isEmpty() )
findThemes( ui.kcfg_Theme->text() );
delete dialog;
}
#include "moc_kgamethemeselector.cpp"
|