Commit cd9213dc authored by Frederik Schwarzer's avatar Frederik Schwarzer

Format + Include cleanup.

parent 91b087af
......@@ -17,30 +17,40 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// own
#include "kmahjonggbackground.h"
#include "libkmahjongg_debug.h"
#include <KLocalizedString>
#include <kconfig.h>
#include <kconfiggroup.h>
#include <qsvgrenderer.h>
#include <QImage>
// Qt
#include <QFile>
#include <QImage>
#include <QMap>
#include <QPainter>
#include <QPixmap>
#include <QPixmapCache>
#include <QPainter>
#include <QSvgRenderer>
// KDE
#include <KConfig>
#include <KConfigGroup>
#include <KLocalizedString>
// LibKMahjongg
#include "libkmahjongg_debug.h"
class KMahjonggBackgroundPrivate
{
public:
public:
KMahjonggBackgroundPrivate()
: w(1), h(1), graphicsLoaded(false), isTiled(true), isSVG(false)
: w(1)
, h(1)
, graphicsLoaded(false)
, isTiled(true)
, isSVG(false)
{
}
QMap<QString, QString> authorproperties;
QString pixmapCacheNameFromElementId(const QString &elementid);
QString pixmapCacheNameFromElementId(const QString & elementid);
QPixmap renderBG(short width, short height);
QPixmap backgroundPixmap;
......@@ -62,19 +72,21 @@ KMahjonggBackground::KMahjonggBackground()
: d(new KMahjonggBackgroundPrivate)
{
static bool _inited = false;
if (_inited)
if (_inited) {
return;
}
_inited = true;
}
KMahjonggBackground::~KMahjonggBackground() {
KMahjonggBackground::~KMahjonggBackground()
{
delete d;
}
bool KMahjonggBackground::loadDefault()
{
// Set default background here.
QLatin1String idx( "egyptian.desktop" );
QLatin1String idx("egyptian.desktop");
QString bgPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kmahjongglib/backgrounds/" + idx);
qCDebug(LIBKMAHJONGG_LOG) << "Inside LoadDefault(), located background at" << bgPath;
......@@ -86,7 +98,8 @@ bool KMahjonggBackground::loadDefault()
#define kBGVersionFormat 1
bool KMahjonggBackground::load(const QString &file, short width, short height) {
bool KMahjonggBackground::load(const QString & file, short width, short height)
{
//qCDebug(LIBKMAHJONGG_LOG) << "Background loading";
d->isSVG = false;
......@@ -102,16 +115,16 @@ bool KMahjonggBackground::load(const QString &file, short width, short height) {
KConfig bgconfig(file, KConfig::SimpleConfig);
KConfigGroup group = bgconfig.group("KMahjonggBackground");
d->authorproperties.insert(QLatin1String( "Name" ), group.readEntry("Name"));// Returns translated data
d->authorproperties.insert(QLatin1String( "Author" ), group.readEntry("Author"));
d->authorproperties.insert(QLatin1String( "Description" ), group.readEntry("Description"));
d->authorproperties.insert(QLatin1String( "AuthorEmail" ), group.readEntry("AuthorEmail"));
d->authorproperties.insert(QLatin1String("Name"), group.readEntry("Name")); // Returns translated data
d->authorproperties.insert(QLatin1String("Author"), group.readEntry("Author"));
d->authorproperties.insert(QLatin1String("Description"), group.readEntry("Description"));
d->authorproperties.insert(QLatin1String("AuthorEmail"), group.readEntry("AuthorEmail"));
//The "Plain" key is set to 1 by the color_plain background.
d->isPlain = group.readEntry("Plain", 0) != 0;
d->authorproperties.insert(QLatin1String( "Plain" ), d->isPlain ? QLatin1String("1") : QLatin1String("0"));
d->authorproperties.insert(QLatin1String("Plain"), d->isPlain ? QLatin1String("1") : QLatin1String("0"));
//Version control
int bgversion = group.readEntry("VersionFormat",0);
int bgversion = group.readEntry("VersionFormat", 0);
//Format is increased when we have incompatible changes, meaning that older clients are not able to use the remaining information safely
if (bgversion > kBGVersionFormat) {
return false;
......@@ -130,11 +143,13 @@ bool KMahjonggBackground::load(const QString &file, short width, short height) {
qCDebug(LIBKMAHJONGG_LOG) << "Using background at" << d->graphicspath;
if (d->graphicspath.isEmpty()) return (false);
if (d->graphicspath.isEmpty()) {
return (false);
}
if (group.readEntry("Tiled",0)) {
d->w = group.readEntry("Width",0);
d->h = group.readEntry("Height",0);
if (group.readEntry("Tiled", 0)) {
d->w = group.readEntry("Width", 0);
d->h = group.readEntry("Height", 0);
d->isTiled = true;
} else {
d->w = width;
......@@ -146,35 +161,44 @@ bool KMahjonggBackground::load(const QString &file, short width, short height) {
return true;
}
bool KMahjonggBackground::loadGraphics() {
if (d->graphicsLoaded || d->isPlain) return (true) ;
bool KMahjonggBackground::loadGraphics()
{
if (d->graphicsLoaded || d->isPlain) {
return (true);
}
d->svg.load(d->graphicspath);
if (d->svg.isValid()) {
d->isSVG = true;
} else {
//qCDebug(LIBKMAHJONGG_LOG) << "could not load svg";
return( false );
return (false);
}
return (true);
}
void KMahjonggBackground::sizeChanged(int newW, int newH) {
void KMahjonggBackground::sizeChanged(int newW, int newH)
{
//in tiled mode we do not care about the whole field size
if (d->isTiled || d->isPlain) return;
if (d->isTiled || d->isPlain) {
return;
}
if (newW == d->w && newH == d->h)
if (newW == d->w && newH == d->h) {
return;
}
d->w = newW;
d->h = newH;
}
QString KMahjonggBackgroundPrivate::pixmapCacheNameFromElementId(const QString &elementid) {
return authorproperties[QLatin1String( "Name" )]+ elementid+QString::fromLatin1( "W%1H%2").arg(w).arg(h);
QString KMahjonggBackgroundPrivate::pixmapCacheNameFromElementId(const QString & elementid)
{
return authorproperties[QLatin1String("Name")] + elementid + QString::fromLatin1("W%1H%2").arg(w).arg(h);
}
QPixmap KMahjonggBackgroundPrivate::renderBG(short width, short height) {
QImage qiRend(QSize(width, height),QImage::Format_ARGB32_Premultiplied);
QPixmap KMahjonggBackgroundPrivate::renderBG(short width, short height)
{
QImage qiRend(QSize(width, height), QImage::Format_ARGB32_Premultiplied);
qiRend.fill(0);
if (svg.isValid()) {
......@@ -184,7 +208,8 @@ QPixmap KMahjonggBackgroundPrivate::renderBG(short width, short height) {
return QPixmap::fromImage(qiRend);
}
QBrush & KMahjonggBackground::getBackground() {
QBrush & KMahjonggBackground::getBackground()
{
if (d->isPlain) {
d->backgroundBrush = QBrush(QPixmap());
} else {
......@@ -197,11 +222,12 @@ QBrush & KMahjonggBackground::getBackground() {
return d->backgroundBrush;
}
QString KMahjonggBackground::path() const {
QString KMahjonggBackground::path() const
{
return d->filename;
}
QString KMahjonggBackground::authorProperty(const QString &key) const {
QString KMahjonggBackground::authorProperty(const QString & key) const
{
return d->authorproperties[key];
}
......@@ -20,8 +20,10 @@
#ifndef KMAHJONGGBACKGROUND_H
#define KMAHJONGGBACKGROUND_H
// Qt
#include <QBrush>
// LibKMahjongg
#include "libkmahjongg_export.h"
class KMahjonggBackgroundPrivate;
......
......@@ -16,15 +16,21 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// own
#include "kmahjonggbackgroundselector.h"
#include <KLocalizedString>
// Qt
#include <QDir>
#include <QPainter>
// KDE
#include <KLocalizedString>
// LibKMahjongg
#include "kmahjonggbackground.h"
#include <QDir>
KMahjonggBackgroundSelector::KMahjonggBackgroundSelector( QWidget* parent, KConfigSkeleton * aconfig )
: QWidget( parent )
KMahjonggBackgroundSelector::KMahjonggBackgroundSelector(QWidget * parent, KConfigSkeleton * aconfig)
: QWidget(parent)
{
setupUi(this);
setupData(aconfig);
......@@ -50,24 +56,23 @@ void KMahjonggBackgroundSelector::setupData(KConfigSkeleton * aconfig)
//Now get our backgrounds into a list
QStringList bgsAvailable;
const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, "kmahjongglib/backgrounds", QStandardPaths::LocateDirectory);
Q_FOREACH (const QString& dir, dirs) {
Q_FOREACH (const QString & dir, dirs) {
const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.desktop"));
Q_FOREACH (const QString& file, fileNames) {
Q_FOREACH (const QString & file, fileNames) {
bgsAvailable.append(dir + '/' + file);
}
}
}
QLatin1String namestr("Name");
int numvalidentries = 0;
for (int i = 0; i < bgsAvailable.size(); ++i)
{
for (int i = 0; i < bgsAvailable.size(); ++i) {
KMahjonggBackground * abg = new KMahjonggBackground();
QString bgpath = bgsAvailable.at(i);
if (abg->load(bgpath,backgroundPreview->width(),backgroundPreview->height())) {
if (abg->load(bgpath, backgroundPreview->width(), backgroundPreview->height())) {
backgroundMap.insert(abg->authorProperty(namestr), abg);
backgroundList->addItem(abg->authorProperty(namestr));
//Find if this is our currently configured background
if (bgpath==initialGroup) {
if (bgpath == initialGroup) {
//Select current entry
backgroundList->setCurrentRow(numvalidentries);
backgroundChanged();
......@@ -85,8 +90,10 @@ void KMahjonggBackgroundSelector::backgroundChanged()
{
KMahjonggBackground * selBG = backgroundMap.value(backgroundList->currentItem()->text());
//Sanity checkings. Should not happen.
if (!selBG) return;
if (selBG->path()==kcfg_Background->text()) {
if (!selBG) {
return;
}
if (selBG->path() == kcfg_Background->text()) {
return;
}
QLatin1String authstr("Author");
......@@ -97,23 +104,22 @@ void KMahjonggBackgroundSelector::backgroundChanged()
backgroundContact->setText(selBG->authorProperty(contactstr));
backgroundDescription->setText(selBG->authorProperty(descstr));
if (selBG->authorProperty(QLatin1String( "Plain" )) == QLatin1String("1")) {
if (selBG->authorProperty(QLatin1String("Plain")) == QLatin1String("1")) {
backgroundPreview->setPixmap(QPixmap());
return;
}
//Make sure SVG is loaded when graphics is selected
if (!selBG->loadGraphics()) return;
if (!selBG->loadGraphics()) {
return;
}
//Draw the preview
//TODO here: add code to load and keep proportions for non-tiled content?
QImage qiRend(backgroundPreview->size(),QImage::Format_ARGB32_Premultiplied);
QImage qiRend(backgroundPreview->size(), QImage::Format_ARGB32_Premultiplied);
qiRend.fill(0);
QPainter p(&qiRend);
p.fillRect(p.viewport(), selBG->getBackground() );
p.fillRect(p.viewport(), selBG->getBackground());
p.end();
backgroundPreview->setPixmap(QPixmap::fromImage(qiRend));
}
......@@ -19,8 +19,13 @@
#ifndef KMAHJONGGBACKGROUNDSELECTOR_H
#define KMAHJONGGBACKGROUNDSELECTOR_H
// Qt
#include <QMap>
// KDE
#include <KConfigSkeleton>
// LibKMahjongg
#include "ui_kmahjonggbackgroundselector.h"
class KMahjonggBackground;
......
......@@ -15,40 +15,45 @@
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "kmahjonggconfigdialog.h"
#include "kmahjonggtilesetselector.h"
#include "kmahjonggbackgroundselector.h"
#include "libkmahjongg_debug.h"
#include <kconfigdialogmanager.h>
#include <kstandardguiitem.h>
#include <KLocalizedString>
#include <krandom.h>
#include <kconfig.h>
#include <KConfigGroup>
// own
#include "kmahjonggconfigdialog.h"
// Qt
#include <QDialog>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QVBoxLayout>
// KDE
#include <KConfigGroup>
#include <KLocalizedString>
#include <KConfig>
#include <KConfigDialogManager>
#include <KRandom>
#include <KStandardGuiItem>
// LibKMahjongg
#include "kmahjonggbackgroundselector.h"
#include "kmahjonggtilesetselector.h"
#include "libkmahjongg_debug.h"
class KMahjonggConfigDialogPrivate
{
public:
KConfigSkeleton * m_config;
};
KMahjonggConfigDialog::KMahjonggConfigDialog( QWidget *parent, const QString& name,
KConfigSkeleton *config)
: KConfigDialog(parent, name, config),
d(new KMahjonggConfigDialogPrivate)
KMahjonggConfigDialog::KMahjonggConfigDialog(QWidget * parent, const QString & name,
KConfigSkeleton * config)
: KConfigDialog(parent, name, config)
, d(new KMahjonggConfigDialogPrivate)
{
setFaceType(List);
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Apply);
QVBoxLayout *mainLayout = new QVBoxLayout;
QDialogButtonBox * buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Apply);
QVBoxLayout * mainLayout = new QVBoxLayout;
setLayout(mainLayout);
QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
QPushButton * okButton = buttonBox->button(QDialogButtonBox::Ok);
okButton->setDefault(true);
okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
connect(buttonBox, &QDialogButtonBox::accepted, this, &KMahjonggConfigDialog::accept);
......@@ -68,14 +73,14 @@ void KMahjonggConfigDialog::addTilesetPage()
{
KMahjonggTilesetSelector * ts = new KMahjonggTilesetSelector(this, d->m_config);
//TODO: Use the cards icon for our page for now, need to get one for tilesets made
addPage(ts, i18n("Tiles"), QLatin1String( "games-config-tiles" ));
addPage(ts, i18n("Tiles"), QLatin1String("games-config-tiles"));
}
void KMahjonggConfigDialog::addBackgroundPage()
{
KMahjonggBackgroundSelector * ts = new KMahjonggBackgroundSelector(this, d->m_config);
//TODO: need icon
addPage(ts, i18n("Background"), QLatin1String( "games-config-background" ));
addPage(ts, i18n("Background"), QLatin1String("games-config-background"));
}
void KMahjonggConfigDialog::updateWidgetsDefault()
......@@ -90,5 +95,3 @@ void KMahjonggConfigDialog::updateSettings()
{
//qCDebug(LIBKMAHJONGG_LOG) << "updateSettings";
}*/
......@@ -19,11 +19,13 @@
#ifndef KMAHJONGGCONFIGDIALOG_H
#define KMAHJONGGCONFIGDIALOG_H
#include <kconfigdialog.h>
#include <libkmahjongg_export.h>
// KDE
#include <KConfigDialog>
#include <KConfigSkeleton>
// LibKMahjongg
#include <libkmahjongg_export.h>
class KMahjonggConfigDialogPrivate;
class LIBKMAHJONGG_EXPORT KMahjonggConfigDialog : public KConfigDialog
......
This diff is collapsed.
......@@ -20,9 +20,11 @@
#ifndef KMAHJONGGTILESET_H
#define KMAHJONGGTILESET_H
// Qt
#include <QString>
#include <QPixmap>
// LibKMahjongg
#include <libkmahjongg_export.h>
class KMahjonggTilesetPrivate;
......
......@@ -16,17 +16,22 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// own
#include "kmahjonggtilesetselector.h"
#include <KLocalizedString>
#include <QPainter>
#include <qstandardpaths.h>
// Qt
#include <QDir>
#include <QPainter>
#include <QStandardPaths>
// KDE
#include <KLocalizedString>
// LibKMahjongg
#include "kmahjonggtileset.h"
KMahjonggTilesetSelector::KMahjonggTilesetSelector( QWidget* parent, KConfigSkeleton * aconfig )
: QWidget( parent )
KMahjonggTilesetSelector::KMahjonggTilesetSelector(QWidget * parent, KConfigSkeleton * aconfig)
: QWidget(parent)
{
setupUi(this);
setupData(aconfig);
......@@ -34,7 +39,7 @@ KMahjonggTilesetSelector::KMahjonggTilesetSelector( QWidget* parent, KConfigSkel
KMahjonggTilesetSelector::~KMahjonggTilesetSelector()
{
tilesetMap.clear();
tilesetMap.clear();
}
void KMahjonggTilesetSelector::setupData(KConfigSkeleton * aconfig)
......@@ -53,24 +58,23 @@ void KMahjonggTilesetSelector::setupData(KConfigSkeleton * aconfig)
//Now get our tilesets into a list
QStringList tilesAvailable;
const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, "kmahjongglib/tilesets", QStandardPaths::LocateDirectory);
Q_FOREACH (const QString& dir, dirs) {
Q_FOREACH (const QString & dir, dirs) {
const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.desktop"));
Q_FOREACH (const QString& file, fileNames) {
Q_FOREACH (const QString & file, fileNames) {
tilesAvailable.append(dir + '/' + file);
}
}
QLatin1String namestr("Name");
int numvalidentries = 0;
for (int i = 0; i < tilesAvailable.size(); ++i)
{
for (int i = 0; i < tilesAvailable.size(); ++i) {
KMahjonggTileset * aset = new KMahjonggTileset();
QString atileset = tilesAvailable.at(i);
if (aset->loadTileset(atileset)) {
tilesetMap.insert(aset->authorProperty(namestr), aset);
tilesetList->addItem(aset->authorProperty(namestr));
//Find if this is our currently configured Tileset
if (atileset==initialGroup) {
if (atileset == initialGroup) {
//Select current entry
tilesetList->setCurrentRow(numvalidentries);
tilesetChanged();
......@@ -88,8 +92,10 @@ void KMahjonggTilesetSelector::tilesetChanged()
{
KMahjonggTileset * selTileset = tilesetMap.value(tilesetList->currentItem()->text());
//Sanity checkings. Should not happen.
if (!selTileset) return;
if (selTileset->path()==kcfg_TileSet->text()) {
if (!selTileset) {
return;
}
if (selTileset->path() == kcfg_TileSet->text()) {
return;
}
QLatin1String authstr("Author");
......@@ -101,22 +107,21 @@ void KMahjonggTilesetSelector::tilesetChanged()
tilesetDescription->setText(selTileset->authorProperty(descstr));
//Make sure SVG is loaded when graphics is selected
if (!selTileset->loadGraphics()) return;
if (!selTileset->loadGraphics()) {
return;
}
//Let the tileset calculate its ideal size for the preview area, but reduce the margins a bit (pass oversized drawing area)
QSize tilesize = selTileset->preferredTileSize(tilesetPreview->size()*1.3, 1, 1);
QSize tilesize = selTileset->preferredTileSize(tilesetPreview->size() * 1.3, 1, 1);
selTileset->reloadTileset(tilesize);
//Draw the preview
QImage qiRend(tilesetPreview->size(),QImage::Format_ARGB32_Premultiplied);
QImage qiRend(tilesetPreview->size(), QImage::Format_ARGB32_Premultiplied);
qiRend.fill(0);
QPainter p(&qiRend);
//Calculate the margins to center the tile
QSize margin = tilesetPreview->size() - tilesize;
//Draw unselected tile and first tileface
p.drawPixmap(margin.width()/2, margin.height()/2, selTileset->unselectedTile(1));
p.drawPixmap(margin.width()/2, margin.height()/2, selTileset->tileface(0));
p.drawPixmap(margin.width() / 2, margin.height() / 2, selTileset->unselectedTile(1));
p.drawPixmap(margin.width() / 2, margin.height() / 2, selTileset->tileface(0));
p.end();
tilesetPreview->setPixmap(QPixmap::fromImage(qiRend));
}
......@@ -19,8 +19,13 @@
#ifndef KMAHJONGGTILESETSELECTOR_H
#define KMAHJONGGTILESETSELECTOR_H
// Qt
#include <QMap>
// KDE
#include <KConfigSkeleton>
// LibKMahjongg
#include "ui_kmahjonggtilesetselector.h"
class KMahjonggTileset;
......
......@@ -17,7 +17,7 @@
Boston, MA 02110-1301, USA.
*/
// own
#include "libkmahjongg_debug.h"
Q_LOGGING_CATEGORY(LIBKMAHJONGG_LOG, "log_libkmahjongg")
Q_LOGGING_CATEGORY(LIBKMAHJONGG_LOG, "log_libkmahjongg")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment