Commit af5d7f6e authored by Stefan Majewsky's avatar Stefan Majewsky

Turn TextureHelper into a singleton to support multiple views.

Also some code cleanup in MainWindow.

svn path=/trunk/KDE/kdegames/palapeli/; revision=1117715
parent 8018268c
......@@ -19,7 +19,6 @@
#include "configdialog.h"
#include "configdialog_p.h"
#include "../engine/texturehelper.h"
#include "../engine/view.h"
#include "settings.h"
#include <QTimer>
......@@ -55,14 +54,13 @@ void Palapeli::TriggerComboBox::handleCurrentIndexChanged(int index)
//END Palapeli::TriggerComboBox
//BEGIN Palapeli::ConfigDialog
Palapeli::ConfigDialog::ConfigDialog(Palapeli::View* view, QWidget* parent)
Palapeli::ConfigDialog::ConfigDialog(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());
m_generalUi.kcfg_ViewBackground->setModel(Palapeli::TextureHelper::instance());
addPage(generalPage, i18n("General settings"))->setIcon(KIcon("configure"));
//TODO: add TriggerConfigWidget
}
......@@ -70,7 +68,7 @@ Palapeli::ConfigDialog::ConfigDialog(Palapeli::View* view, QWidget* parent)
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()));
QTimer::singleShot(0, Palapeli::TextureHelper::instance(), SLOT(readSettings()));
//TODO: Here goes code for applying the settings from the TriggerConfigWidget.
}
......
......@@ -25,20 +25,16 @@
namespace Palapeli
{
class ConfigDialog_PageGeneral;
class View;
class ConfigDialog : public KConfigDialog
{
Q_OBJECT
public:
ConfigDialog(Palapeli::View* view, QWidget* parent = 0);
ConfigDialog(QWidget* parent = 0);
protected:
virtual void updateSettings();
virtual void updateWidgets();
virtual void updateWidgetsDefault();
private:
Palapeli::View* m_view;
Ui::Settings m_generalUi;
};
}
......
......@@ -30,6 +30,12 @@
const QSize Palapeli::TextureHelper::DefaultThumbnailSize(32, 32);
const QSize Palapeli::TextureHelper::DefaultPixmapSize(128, 128);
Palapeli::TextureHelper* Palapeli::TextureHelper::instance()
{
static Palapeli::TextureHelper instance;
return &instance;
}
QPixmap Palapeli::TextureHelper::render(const QString& fileName)
{
const QString path = KStandardDirs::locate("appdata", "backgrounds/" + fileName);
......@@ -48,10 +54,8 @@ QPixmap Palapeli::TextureHelper::render(const QString& fileName)
return pixmap;
}
Palapeli::TextureHelper::TextureHelper(QObject* parent)
: QStandardItemModel(parent)
, m_scene(0)
, m_currentIndex(-1)
Palapeli::TextureHelper::TextureHelper()
: m_currentIndex(-1)
{
//create menu item for solid color
QPixmap colorThumbnail(DefaultThumbnailSize);
......@@ -101,18 +105,23 @@ void Palapeli::TextureHelper::readSettings()
m_currentBrush = selectedColor;
else
m_currentBrush = item->data(BrushRole).value<QPixmap>();
if (m_scene)
m_scene->setBackgroundBrush(m_currentBrush);
foreach (QGraphicsScene* scene, m_scenes)
scene->setBackgroundBrush(m_currentBrush);
}
}
void Palapeli::TextureHelper::setScene(QGraphicsScene* scene)
void Palapeli::TextureHelper::addScene(QGraphicsScene* scene)
{
if (m_scene == scene)
if (!scene || m_scenes.contains(scene))
return;
m_scene = scene;
if (m_scene)
m_scene->setBackgroundBrush(m_currentBrush);
m_scenes << scene;
scene->setBackgroundBrush(m_currentBrush);
connect(scene, SIGNAL(destroyed(QObject*)), SLOT(removeScene(QObject*)));
}
void Palapeli::TextureHelper::removeScene(QObject* scene)
{
m_scenes.removeAll(reinterpret_cast<QGraphicsScene*>(scene));
}
#include "texturehelper.moc"
......@@ -24,6 +24,7 @@ class QGraphicsScene;
namespace Palapeli
{
//NOTE: Singleton, needs to be initialized in main().
class TextureHelper : public QStandardItemModel
{
Q_OBJECT
......@@ -33,16 +34,17 @@ namespace Palapeli
IdentifierRole = Qt::UserRole + 2
};
TextureHelper(QObject* parent = 0);
static Palapeli::TextureHelper* instance();
int currentIndex() const;
public Q_SLOTS:
void readSettings();
void setScene(QGraphicsScene* scene);
void addScene(QGraphicsScene* scene);
void removeScene(QObject* object);
private:
TextureHelper();
static QPixmap render(const QString& fileName);
QGraphicsScene* m_scene;
QList<QGraphicsScene*> m_scenes;
int m_currentIndex;
QBrush m_currentBrush;
......
......@@ -33,7 +33,6 @@ const int Palapeli::View::MaximumZoomLevel = 200;
Palapeli::View::View()
: m_interactorManager(new Palapeli::InteractorManager(this))
, m_scene(0)
, m_txHelper(new Palapeli::TextureHelper(this))
, m_zoomLevel(100)
{
setMouseTracking(true);
......@@ -61,16 +60,11 @@ void Palapeli::View::setScene(Palapeli::Scene* scene)
m_scene = scene;
this->QGraphicsView::setScene(m_scene);
m_interactorManager->updateScene();
m_txHelper->setScene(m_scene);
Palapeli::TextureHelper::instance()->addScene(m_scene);
//reset zoom level (TODO: store viewport geometry in Scene)
zoomTo(100);
}
Palapeli::TextureHelper* Palapeli::View::textureHelper() const
{
return m_txHelper;
}
QRectF Palapeli::View::viewportRect() const
{
return mapToScene(viewport()->rect()).boundingRect();
......
......@@ -26,7 +26,6 @@ namespace Palapeli
class ConstraintVisualizer;
class InteractorManager;
class Scene;
class TextureHelper;
class View : public QGraphicsView
{
......@@ -37,7 +36,6 @@ namespace Palapeli
Palapeli::InteractorManager* interactorManager() const;
Palapeli::Scene* scene() const;
Palapeli::TextureHelper* textureHelper() const;
QRectF viewportRect() const;
void setViewportRect(const QRectF& viewportRect);
......@@ -67,7 +65,6 @@ namespace Palapeli
private:
Palapeli::InteractorManager* m_interactorManager;
Palapeli::Scene* m_scene;
Palapeli::TextureHelper* m_txHelper;
QPointF m_dragPrevPos;
int m_zoomLevel;
};
......
......@@ -19,27 +19,23 @@
#include "mainwindow.h"
#include "../config/configdialog.h"
#include "../creator/puzzlecreator.h"
#include "../engine/interactormanager.h"
#include "../engine/scene.h"
#include "../engine/texturehelper.h"
#include "../engine/view.h"
#include "collectionwidget.h"
#include "puzzletablewidget.h"
#include "settings.h"
#include "tabwindow.h"
#include "ui_settings.h"
#include <QPointer>
#include <QTabBar> //krazy:exclude=qclasses
#include <KActionCollection>
#include <KCmdLineArgs>
#include <KConfigDialog>
#include <KLocalizedString>
#include <KMenuBar>
#include <KTabWidget>
#include <KToggleAction>
#include <KShortcutsDialog>
#include <KStandardAction>
#include <KTabWidget>
#include <KToggleAction>
namespace Palapeli
{
......@@ -178,36 +174,7 @@ 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);
//NOTE: It is intentional that the widget "cfg_ViewBackground" is _not_ called "kcfg_ViewBackground". KConfigDialog's config handling would mess things up if it was used in this case.
settingsUi.cfg_ViewBackground->setModel(m_puzzleTable->view()->textureHelper());
connect(settingsUi.cfg_ViewBackground, SIGNAL(currentIndexChanged(int)), this, SLOT(configure_TextureChanged(int)));
connect(this, SIGNAL(configure_ColorEnabledChanged(bool)), settingsUi.kcfg_ViewBackgroundColor, SLOT(setEnabled(bool)));
settingsUi.cfg_ViewBackground->setCurrentIndex(m_puzzleTable->view()->textureHelper()->currentIndex());
connect(settingsUi.cfg_ViewBackground, SIGNAL(currentIndexChanged(int)), m_puzzleTable->view()->textureHelper(), SLOT(setCurrentIndex(int)));
connect(settingsUi.kcfg_ViewBackgroundColor, SIGNAL(changed(QColor)), m_puzzleTable->view()->textureHelper(), SLOT(setSolidColor(QColor)));
//FIXME: Both TextureHelper and InteractorManager immediately apply changes made to their configuration through the UI, which is not what the user expects.
//setup dialog
KConfigDialog settingsDialog(this, QString(), Settings::self());
settingsDialog.addPage(settingsWidget, i18n("General settings"))->setIcon(KIcon("configure"));
settingsDialog.addPage(new Palapeli::TriggerConfigWidget(m_puzzleTable->view()->interactorManager()), i18n("Mouse interaction"))->setIcon(KIcon("input-mouse"));
// connect(&settingsDialog, SIGNAL(settingsChanged(const QString&)), this, SLOT(configureFinished())); //NOTE: unused ATM (settings are read on demand)
settingsDialog.exec();
}
void Palapeli::MainWindow::configure_TextureChanged(int index)
{
QComboBox* backgroundBox = qobject_cast<QComboBox*>(sender()); //krazy:exclude=qclasses
if (!backgroundBox)
return;
const QString selectedStyle = backgroundBox->itemData(index, Palapeli::TextureHelper::StyleRole).toString();
emit configure_ColorEnabledChanged(selectedStyle == "color");
#endif
Palapeli::ConfigDialog().exec();
}
#include "mainwindow.moc"
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