Commit 7e634d76 authored by Stefan Majewsky's avatar Stefan Majewsky

Do the config stuff the right way.

The TriggerConfigWidget is temporarily removed from the config dialog,
but the rest works as one would expect because everything's handled by
KConfigDialog now. This requires a change of the config format which is
backwards-compatible to Palapeli 1.0, but not to earlier trunk versions
of Palapeli 1.1.

svn path=/trunk/KDE/kdegames/palapeli/; revision=1117449
parent fbb97848
......@@ -2,6 +2,7 @@
set_directory_properties(PROPERTIES COMPILE_DEFINITIONS USE_LOCAL_LIBPALA)
set(palapeli_SRCS
config/configdialog.cpp
config/elidinglabel.cpp
config/mouseinputbutton.cpp
config/triggerconfigwidget.cpp
......
/***************************************************************************
* Copyright 2010 Stefan Majewsky <majewsky@gmx.net>
*
* This program 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 "configdialog.h"
#include "configdialog_p.h"
#include "../engine/texturehelper.h"
#include "../engine/view.h"
#include "settings.h"
#include <QTimer>
//BEGIN Palapeli::TriggerComboBox
Palapeli::TriggerComboBox::TriggerComboBox(QWidget* parent)
: KComboBox(parent)
{
connect(this, SIGNAL(currentIndexChanged(int)), SLOT(handleCurrentIndexChanged(int)));
}
QString Palapeli::TriggerComboBox::backgroundKey() const
{
return itemData(currentIndex(), Palapeli::TextureHelper::IdentifierRole).toString();
}
void Palapeli::TriggerComboBox::setBackgroundKey(const QString& backgroundKey)
{
int index = findData(backgroundKey, Palapeli::TextureHelper::IdentifierRole);
if (index > -1)
setCurrentIndex(index);
}
void Palapeli::TriggerComboBox::handleCurrentIndexChanged(int index)
{
Q_UNUSED(index)
const QString key = backgroundKey();
emit backgroundKeyChanged(key);
emit itemTypeChanged(key == QLatin1String("__color__"));
}
//END Palapeli::TriggerComboBox
//BEGIN Palapeli::ConfigDialog
Palapeli::ConfigDialog::ConfigDialog(Palapeli::View* view, QWidget* parent)
: KConfigDialog(parent, QString(), Settings::self())
, m_view(view)
{
//setup page "General settings"
QWidget* generalPage = new QWidget;
m_generalUi.setupUi(generalPage);
m_generalUi.kcfg_ViewBackground->setModel(view->textureHelper());
addPage(generalPage, i18n("General settings"))->setIcon(KIcon("configure"));
//TODO: add TriggerConfigWidget
}
void Palapeli::ConfigDialog::updateSettings()
{
//schedule update of TextureHelper (but only after KConfigDialog has written the settings, which might happen after this slot call)
QTimer::singleShot(0, m_generalUi.kcfg_ViewBackground->model(), SLOT(readSettings()));
//TODO: Here goes code for applying the settings from the TriggerConfigWidget.
}
void Palapeli::ConfigDialog::updateWidgets()
{
//TODO: Here goes code for initializing the TriggerConfigWidget.
}
void Palapeli::ConfigDialog::updateWidgetsDefault()
{
//TODO: Here goes code for resetting the TriggerConfigWidget to default values.
}
//END Palapeli::ConfigDialog
#include "configdialog.moc"
#include "configdialog_p.moc"
/***************************************************************************
* Copyright 2010 Stefan Majewsky <majewsky@gmx.net>
*
* This program 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.
***************************************************************************/
#ifndef PALAPELI_CONFIGDIALOG_H
#define PALAPELI_CONFIGDIALOG_H
#include "ui_settings.h"
#include <KConfigDialog>
namespace Palapeli
{
class ConfigDialog_PageGeneral;
class View;
class ConfigDialog : public KConfigDialog
{
Q_OBJECT
public:
ConfigDialog(Palapeli::View* view, QWidget* parent = 0);
protected:
virtual void updateSettings();
virtual void updateWidgets();
virtual void updateWidgetsDefault();
private:
Palapeli::View* m_view;
Ui::Settings m_generalUi;
};
}
#endif // PALAPELI_CONFIGDIALOG_H
/***************************************************************************
* Copyright 2010 Stefan Majewsky <majewsky@gmx.net>
*
* This program 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.
***************************************************************************/
#ifndef PALAPELI_CONFIGDIALOG_P_H
#define PALAPELI_CONFIGDIALOG_P_H
#include <KComboBox>
namespace Palapeli
{
class TriggerComboBox : public KComboBox
{
Q_OBJECT
Q_PROPERTY(QString backgroundKey READ backgroundKey WRITE setBackgroundKey NOTIFY backgroundKeyChanged)
Q_PROPERTY(QByteArray kcfg_property READ kcfgProperty)
public:
TriggerComboBox(QWidget* parent = 0);
QString backgroundKey() const;
QByteArray kcfgProperty() const { return "backgroundKey"; }
public Q_SLOTS:
void setBackgroundKey(const QString& backgroundKey);
Q_SIGNALS:
void backgroundKeyChanged(const QString& backgroundKey);
void itemTypeChanged(bool isColor);
private Q_SLOTS:
void handleCurrentIndexChanged(int index);
};
}
#endif // PALAPELI_CONFIGDIALOG_P_H
......@@ -53,16 +53,11 @@ Palapeli::TextureHelper::TextureHelper(QObject* parent)
, m_scene(0)
, m_currentIndex(-1)
{
const QString selectedStyle = Settings::viewBackgroundStyle();
const QColor selectedColor = Settings::viewBackgroundColor();
const QString selectedFileName = Settings::viewBackground();
int selectedIndex = 0, currentIndex = 0;
//create menu item for solid color
QPixmap colorThumbnail(DefaultThumbnailSize);
colorThumbnail.fill(selectedColor);
colorThumbnail.fill(Qt::transparent);
QStandardItem* colorItem = new QStandardItem;
colorItem->setData(selectedColor, BrushRole);
colorItem->setData("color", StyleRole);
colorItem->setData(QLatin1String("__color__"), IdentifierRole);
colorItem->setData(colorThumbnail, Qt::DecorationRole);
colorItem->setData(i18nc("@item:inlistbox", "Single color"), Qt::DisplayRole);
appendRow(colorItem);
......@@ -70,22 +65,19 @@ Palapeli::TextureHelper::TextureHelper(QObject* parent)
const QStringList backgroundFiles = KGlobal::dirs()->findAllResources("appdata", "backgrounds/*");
foreach (const QString& path, backgroundFiles)
{
++currentIndex;
//get file name and find selected or default backgrounds
const QString fileName = QFileInfo(path).fileName();
if (fileName == selectedFileName && selectedStyle == "texture")
selectedIndex = currentIndex;
//create item for this brush
const QPixmap pixmap = render(fileName);
QStandardItem* item = new QStandardItem;
item->setData(pixmap, BrushRole);
item->setData("texture", StyleRole);
item->setData(fileName, IdentifierRole);
item->setData(pixmap.scaled(DefaultThumbnailSize, Qt::KeepAspectRatio), Qt::DecorationRole);
item->setData(fileName, Qt::DisplayRole);
appendRow(item);
}
//select initial brush
setCurrentIndex(selectedIndex);
readSettings();
}
int Palapeli::TextureHelper::currentIndex() const
......@@ -93,35 +85,25 @@ int Palapeli::TextureHelper::currentIndex() const
return m_currentIndex;
}
void Palapeli::TextureHelper::setCurrentIndex(int index)
void Palapeli::TextureHelper::readSettings()
{
if (m_currentIndex == index)
return;
if (index < 0 || index >= rowCount())
return;
m_currentIndex = index;
QBrush brush;
if (index == 0)
{
const QColor color = item(index)->data(BrushRole).value<QColor>();
m_currentBrush = color;
if (m_scene)
m_scene->setBackgroundBrush(m_currentBrush);
//write config
Settings::setViewBackgroundColor(color);
Settings::setViewBackgroundStyle("color");
}
else
//read config
const QString selectedBackground = Settings::viewBackground();
const QColor selectedColor = Settings::viewBackgroundColor();
for (int i = 0; i < rowCount(); ++i)
{
m_currentBrush = item(index)->data(BrushRole).value<QPixmap>();
QStandardItem* item = this->item(i);
if (item->data(IdentifierRole) != selectedBackground)
continue;
//use this brush
m_currentIndex = i;
if (selectedBackground == QLatin1String("__color__"))
m_currentBrush = selectedColor;
else
m_currentBrush = item->data(BrushRole).value<QPixmap>();
if (m_scene)
m_scene->setBackgroundBrush(m_currentBrush);
//write config
const QString key = item(index)->data(Qt::DisplayRole).toString();
Settings::setViewBackground(key);
Settings::setViewBackgroundStyle("texture");
}
Settings::self()->writeConfig();
}
void Palapeli::TextureHelper::setScene(QGraphicsScene* scene)
......@@ -133,19 +115,4 @@ void Palapeli::TextureHelper::setScene(QGraphicsScene* scene)
m_scene->setBackgroundBrush(m_currentBrush);
}
void Palapeli::TextureHelper::setSolidColor(const QColor& color)
{
QStandardItem* item = this->item(0);
item->setData(color, BrushRole);
QPixmap colorThumbnail(DefaultThumbnailSize);
colorThumbnail.fill(color);
item->setData(colorThumbnail, Qt::DecorationRole);
//apply new color and write config if necessary
if (m_currentIndex == 0)
{
m_currentIndex = -1;
setCurrentIndex(0);
}
}
#include "texturehelper.moc"
......@@ -30,16 +30,15 @@ namespace Palapeli
public:
enum CustomRoles {
BrushRole = Qt::UserRole + 1,
StyleRole = Qt::UserRole + 2
IdentifierRole = Qt::UserRole + 2
};
TextureHelper(QObject* parent = 0);
int currentIndex() const;
public Q_SLOTS:
void setCurrentIndex(int index);
void readSettings();
void setScene(QGraphicsScene* scene);
void setSolidColor(const QColor& color);
private:
static QPixmap render(const QString& fileName);
......
......@@ -8,16 +8,12 @@
</entry>
</group>
<group name="Appearance">
<entry name="ViewBackgroundStyle" type="String">
<label>The type of background to use for the view. Possible values are "texture" (use image from "ViewBackground" key) or "color" (use color from "ViewBackgroundColor" key).</label>
<default>texture</default>
</entry>
<entry name="ViewBackground" type="String">
<label>The background image to use for the View.</label>
<label>The filename of the background image to use for the View, or "__color__" if a solid color should be used (see key "ViewBackgroundColor").</label>
<default>background.svg</default>
</entry>
<entry name="ViewBackgroundColor" type="Color">
<label>The background color to use for the View.</label>
<label>The background color to use for the View (if the key "ViewBackground" is set to "__color__").</label>
<default>white</default>
</entry>
<entry name="ShowStatusBar" type="Bool">
......
......@@ -25,7 +25,7 @@
</widget>
</item>
<item row="0" column="1">
<widget class="KComboBox" name="cfg_ViewBackground">
<widget class="Palapeli::TriggerComboBox" name="kcfg_ViewBackground">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
......@@ -106,7 +106,32 @@
<extends>QPushButton</extends>
<header>kcolorbutton.h</header>
</customwidget>
<customwidget>
<class>Palapeli::TriggerComboBox</class>
<extends>KComboBox</extends>
<header>config/configdialog_p.h</header>
<slots>
<signal>itemTypeChanged(bool)</signal>
</slots>
</customwidget>
</customwidgets>
<resources/>
<connections/>
<connections>
<connection>
<sender>kcfg_ViewBackground</sender>
<signal>itemTypeChanged(bool)</signal>
<receiver>kcfg_ViewBackgroundColor</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>270</x>
<y>51</y>
</hint>
<hint type="destinationlabel">
<x>440</x>
<y>50</y>
</hint>
</hints>
</connection>
</connections>
</ui>
......@@ -17,7 +17,7 @@
***************************************************************************/
#include "mainwindow.h"
#include "../config/triggerconfigwidget.h"
#include "../config/configdialog.h"
#include "../creator/puzzlecreator.h"
#include "../engine/interactormanager.h"
#include "../engine/scene.h"
......@@ -178,6 +178,9 @@ void Palapeli::MainWindow::configureShortcuts()
void Palapeli::MainWindow::configurePalapeli()
{
Palapeli::ConfigDialog dialog(m_puzzleTable->view());
dialog.exec();
#if 0
//setup "General settings" widget
QWidget* settingsWidget = new QWidget;
Ui::Settings settingsUi; settingsUi.setupUi(settingsWidget);
......@@ -204,6 +207,7 @@ void Palapeli::MainWindow::configure_TextureChanged(int index)
return;
const QString selectedStyle = backgroundBox->itemData(index, Palapeli::TextureHelper::StyleRole).toString();
emit configure_ColorEnabledChanged(selectedStyle == "color");
#endif
}
#include "mainwindow.moc"
......@@ -39,8 +39,6 @@ namespace Palapeli
void loadPuzzle(const QModelIndex& index);
void configureShortcuts();
void configurePalapeli();
protected Q_SLOTS:
void configure_TextureChanged(int index);
Q_SIGNALS:
void configure_ColorEnabledChanged(bool enabled);
protected:
......
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