Commit 20096a6d authored by Stefan Majewsky's avatar Stefan Majewsky

Make the trigger configuration work. Woohoo! One month of hard work pays off.

svn path=/trunk/KDE/kdegames/palapeli/; revision=1118492
parent 2e2b2a4f
...@@ -72,17 +72,17 @@ void Palapeli::ConfigDialog::updateSettings() ...@@ -72,17 +72,17 @@ void Palapeli::ConfigDialog::updateSettings()
{ {
//schedule update of TextureHelper (but only after KConfigDialog has written the settings, which might happen after this slot call) //schedule update of TextureHelper (but only after KConfigDialog has written the settings, which might happen after this slot call)
QTimer::singleShot(0, Palapeli::TextureHelper::instance(), SLOT(readSettings())); QTimer::singleShot(0, Palapeli::TextureHelper::instance(), SLOT(readSettings()));
//TODO: Here goes code for applying the settings from the TriggerConfigWidget. m_triggerPage->updateSettings();
} }
void Palapeli::ConfigDialog::updateWidgets() void Palapeli::ConfigDialog::updateWidgets()
{ {
//TODO: Here goes code for initializing the TriggerConfigWidget. m_triggerPage->updateWidgets();
} }
void Palapeli::ConfigDialog::updateWidgetsDefault() void Palapeli::ConfigDialog::updateWidgetsDefault()
{ {
//TODO: Here goes code for resetting the TriggerConfigWidget to default values. m_triggerPage->updateWidgetsDefault();
} }
//END Palapeli::ConfigDialog //END Palapeli::ConfigDialog
......
...@@ -26,9 +26,9 @@ ...@@ -26,9 +26,9 @@
Palapeli::TriggerConfigWidget::TriggerConfigWidget(QWidget* parent) Palapeli::TriggerConfigWidget::TriggerConfigWidget(QWidget* parent)
: QTabWidget(parent) : QTabWidget(parent)
, m_interactors(Palapeli::TriggerMapper::createInteractors(0)) //these interactors are just for reading metadata , m_interactors(Palapeli::TriggerMapper::createInteractors(0)) //these interactors are just for reading metadata
, m_mouseView(new Palapeli::TriggerListView(m_interactors, Palapeli::MouseInteractor, this))
, m_wheelView(new Palapeli::TriggerListView(m_interactors, Palapeli::WheelInteractor, this))
{ {
createTriggerListView(m_mouseView, Palapeli::MouseInteractor);
createTriggerListView(m_wheelView, Palapeli::WheelInteractor);
addTab(m_mouseView, i18n("Mouse buttons")); addTab(m_mouseView, i18n("Mouse buttons"));
addTab(m_wheelView, i18n("Mouse wheel")); addTab(m_wheelView, i18n("Mouse wheel"));
} }
...@@ -38,25 +38,24 @@ Palapeli::TriggerConfigWidget::~TriggerConfigWidget() ...@@ -38,25 +38,24 @@ Palapeli::TriggerConfigWidget::~TriggerConfigWidget()
qDeleteAll(m_interactors); qDeleteAll(m_interactors);
} }
void Palapeli::TriggerConfigWidget::createTriggerListView(Palapeli::TriggerListView*& view, int interactorType) void Palapeli::TriggerConfigWidget::updateSettings()
{ {
//filter interactors QMap<QByteArray, Palapeli::Trigger> associations;
QMap<QByteArray, Palapeli::Interactor*> interactors(m_interactors); m_mouseView->getAssociations(associations);
QMutableMapIterator<QByteArray, Palapeli::Interactor*> iter1(interactors); m_wheelView->getAssociations(associations);
while (iter1.hasNext()) Palapeli::TriggerMapper::instance()->setAssociations(associations);
if (iter1.next().value()->interactorType() != interactorType)
iter1.remove();
//filter associations
QMap<QByteArray, Palapeli::Trigger> associations = Palapeli::TriggerMapper::instance()->associations();
QMutableMapIterator<QByteArray, Palapeli::Trigger> iter2(associations);
while (iter2.hasNext())
if (!interactors.contains(iter2.next().key()))
iter2.remove();
//create view
view = new Palapeli::TriggerListView(interactors, associations, this);
} }
void Palapeli::TriggerConfigWidget::writeConfig() void Palapeli::TriggerConfigWidget::updateWidgets()
{ {
//TODO const QMap<QByteArray, Palapeli::Trigger> associations = Palapeli::TriggerMapper::instance()->associations();
m_mouseView->setAssociations(associations);
m_wheelView->setAssociations(associations);
}
void Palapeli::TriggerConfigWidget::updateWidgetsDefault()
{
const QMap<QByteArray, Palapeli::Trigger> associations = Palapeli::TriggerMapper::defaultAssociations();
m_mouseView->setAssociations(associations);
m_wheelView->setAssociations(associations);
} }
...@@ -34,10 +34,10 @@ namespace Palapeli ...@@ -34,10 +34,10 @@ namespace Palapeli
TriggerConfigWidget(QWidget* parent = 0); TriggerConfigWidget(QWidget* parent = 0);
virtual ~TriggerConfigWidget(); virtual ~TriggerConfigWidget();
void writeConfig(); void updateSettings();
void updateWidgets();
void updateWidgetsDefault();
private: private:
void createTriggerListView(Palapeli::TriggerListView*& view, int type);
QMap<QByteArray, Palapeli::Interactor*> m_interactors; QMap<QByteArray, Palapeli::Interactor*> m_interactors;
Palapeli::TriggerListView* m_mouseView; Palapeli::TriggerListView* m_mouseView;
Palapeli::TriggerListView* m_wheelView; Palapeli::TriggerListView* m_wheelView;
......
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include "triggerlistview.h" #include "triggerlistview.h"
#include "elidinglabel.h" #include "elidinglabel.h"
#include "mouseinputbutton.h" #include "mouseinputbutton.h"
#include "../engine/interactor.h"
#include <QApplication> #include <QApplication>
#include <QHBoxLayout> #include <QHBoxLayout>
...@@ -161,7 +160,7 @@ static QString categoryToString(Palapeli::Interactor::Category category) ...@@ -161,7 +160,7 @@ static QString categoryToString(Palapeli::Interactor::Category category)
} }
} }
Palapeli::TriggerListView::TriggerListView(const QMap<QByteArray, Palapeli::Interactor*>& interactors, const QMap<QByteArray, Palapeli::Trigger>& associations, QWidget* parent) Palapeli::TriggerListView::TriggerListView(const QMap<QByteArray, Palapeli::Interactor*>& interactors, Palapeli::InteractorType interactorType, QWidget* parent)
: KCategorizedView(parent) : KCategorizedView(parent)
, m_categoryDrawer(new KCategoryDrawer) , m_categoryDrawer(new KCategoryDrawer)
, m_baseModel(new QStandardItemModel(this)) , m_baseModel(new QStandardItemModel(this))
...@@ -172,12 +171,16 @@ Palapeli::TriggerListView::TriggerListView(const QMap<QByteArray, Palapeli::Inte ...@@ -172,12 +171,16 @@ Palapeli::TriggerListView::TriggerListView(const QMap<QByteArray, Palapeli::Inte
QMap<QByteArray, Palapeli::Interactor*>::const_iterator it1 = interactors.begin(), it2 = interactors.end(); QMap<QByteArray, Palapeli::Interactor*>::const_iterator it1 = interactors.begin(), it2 = interactors.end();
for (; it1 != it2; ++it1) for (; it1 != it2; ++it1)
{ {
//filter interactor
Palapeli::Interactor* interactor = it1.value(); Palapeli::Interactor* interactor = it1.value();
if (interactor->interactorType() != interactorType)
continue;
//create item for interactor
QStandardItem* item = new QStandardItem; QStandardItem* item = new QStandardItem;
item->setData(interactor->description(), Qt::DisplayRole); item->setData(interactor->description(), Qt::DisplayRole);
item->setData(interactor->icon(), Qt::DecorationRole); item->setData(interactor->icon(), Qt::DecorationRole);
item->setData(it1.key(), Palapeli::InteractorRole); item->setData(it1.key(), Palapeli::InteractorRole);
item->setData(qVariantFromValue(associations.value(it1.key())), Palapeli::TriggerRole); item->setData(qVariantFromValue(Palapeli::Trigger()), Palapeli::TriggerRole);
item->setData(categoryToString(interactor->category()), KCategorizedSortFilterProxyModel::CategoryDisplayRole); item->setData(categoryToString(interactor->category()), KCategorizedSortFilterProxyModel::CategoryDisplayRole);
item->setData(interactor->category(), KCategorizedSortFilterProxyModel::CategorySortRole); item->setData(interactor->category(), KCategorizedSortFilterProxyModel::CategorySortRole);
m_baseModel->appendRow(item); m_baseModel->appendRow(item);
...@@ -194,18 +197,25 @@ Palapeli::TriggerListView::~TriggerListView() ...@@ -194,18 +197,25 @@ Palapeli::TriggerListView::~TriggerListView()
delete m_categoryDrawer; delete m_categoryDrawer;
} }
QMap<QByteArray, Palapeli::Trigger> Palapeli::TriggerListView::triggers() const void Palapeli::TriggerListView::getAssociations(QMap<QByteArray, Palapeli::Trigger>& associations)
{ {
//read triggers from base model
QMap<QByteArray, Palapeli::Trigger> result;
for (int i = 0; i < m_baseModel->rowCount(); ++i) for (int i = 0; i < m_baseModel->rowCount(); ++i)
{ {
QStandardItem* item = m_baseModel->item(i); QStandardItem* item = m_baseModel->item(i);
const QByteArray interactor = item->data(Palapeli::InteractorRole).value<QByteArray>(); const QByteArray interactor = item->data(Palapeli::InteractorRole).value<QByteArray>();
const Palapeli::Trigger trigger = item->data(Palapeli::TriggerRole).value<Palapeli::Trigger>(); const Palapeli::Trigger trigger = item->data(Palapeli::TriggerRole).value<Palapeli::Trigger>();
result.insert(interactor, trigger); associations.insertMulti(interactor, trigger);
}
}
void Palapeli::TriggerListView::setAssociations(const QMap<QByteArray, Palapeli::Trigger>& associations)
{
for (int i = 0; i < m_baseModel->rowCount(); ++i)
{
QStandardItem* item = m_baseModel->item(i);
const QByteArray interactor = item->data(Palapeli::InteractorRole).value<QByteArray>();
item->setData(qVariantFromValue(associations.value(interactor)), Palapeli::TriggerRole);
} }
return result;
} }
#include "triggerlistview.moc" #include "triggerlistview.moc"
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#ifndef PALAPELI_TRIGGERLISTVIEW_H #ifndef PALAPELI_TRIGGERLISTVIEW_H
#define PALAPELI_TRIGGERLISTVIEW_H #define PALAPELI_TRIGGERLISTVIEW_H
#include "../engine/interactor.h"
#include "../engine/trigger.h" #include "../engine/trigger.h"
class QStandardItemModel; class QStandardItemModel;
...@@ -35,10 +36,11 @@ namespace Palapeli ...@@ -35,10 +36,11 @@ namespace Palapeli
class TriggerListView : public KCategorizedView class TriggerListView : public KCategorizedView
{ {
public: public:
TriggerListView(const QMap<QByteArray, Palapeli::Interactor*>& interactors, const QMap<QByteArray, Palapeli::Trigger>& associations, QWidget* parent = 0); TriggerListView(const QMap<QByteArray, Palapeli::Interactor*>& interactors, Palapeli::InteractorType interactorType, QWidget* parent = 0);
virtual ~TriggerListView(); virtual ~TriggerListView();
QMap<QByteArray, Palapeli::Trigger> triggers() const; void getAssociations(QMap<QByteArray, Palapeli::Trigger>& associations);
void setAssociations(const QMap<QByteArray, Palapeli::Trigger>& associations);
private: private:
KCategoryDrawer* m_categoryDrawer; KCategoryDrawer* m_categoryDrawer;
QStandardItemModel* m_baseModel; QStandardItemModel* m_baseModel;
......
...@@ -45,6 +45,18 @@ QMap<QByteArray, Palapeli::Interactor*> Palapeli::TriggerMapper::createInteracto ...@@ -45,6 +45,18 @@ QMap<QByteArray, Palapeli::Interactor*> Palapeli::TriggerMapper::createInteracto
return result; return result;
} }
QMap<QByteArray, Palapeli::Trigger> Palapeli::TriggerMapper::defaultAssociations()
{
QMap<QByteArray, Palapeli::Trigger> result;
result.insert("MovePiece", Palapeli::Trigger("LeftButton;NoModifier"));
result.insert("SelectPiece", Palapeli::Trigger("LeftButton;ControlModifier"));
result.insert("MoveViewport", Palapeli::Trigger("RightButton;NoModifier"));
result.insert("ZoomViewport", Palapeli::Trigger("wheel:Vertical;NoModifier"));
result.insert("RubberBand", Palapeli::Trigger("LeftButton;NoModifier"));
result.insert("Constraints", Palapeli::Trigger("LeftButton;NoModifier"));
return result;
}
Palapeli::TriggerMapper::TriggerMapper() Palapeli::TriggerMapper::TriggerMapper()
{ {
//initialize quasi-static data //initialize quasi-static data
...@@ -61,18 +73,6 @@ QMap<QByteArray, Palapeli::Trigger> Palapeli::TriggerMapper::associations() cons ...@@ -61,18 +73,6 @@ QMap<QByteArray, Palapeli::Trigger> Palapeli::TriggerMapper::associations() cons
return m_associations; return m_associations;
} }
void Palapeli::TriggerMapper::readDefaultSettings()
{
m_associations.clear();
m_associations.insert("MovePiece", Palapeli::Trigger("LeftButton;NoModifier"));
m_associations.insert("SelectPiece", Palapeli::Trigger("LeftButton;ControlModifier"));
m_associations.insert("MoveViewport", Palapeli::Trigger("RightButton;NoModifier"));
m_associations.insert("ZoomViewport", Palapeli::Trigger("wheel:Vertical;NoModifier"));
m_associations.insert("RubberBand", Palapeli::Trigger("LeftButton;NoModifier"));
m_associations.insert("Constraints", Palapeli::Trigger("LeftButton;NoModifier"));
emit associationsChanged();
}
void Palapeli::TriggerMapper::readSettings() void Palapeli::TriggerMapper::readSettings()
{ {
m_associations.clear(); m_associations.clear();
...@@ -87,15 +87,18 @@ void Palapeli::TriggerMapper::readSettings() ...@@ -87,15 +87,18 @@ void Palapeli::TriggerMapper::readSettings()
if (trigger.isValid()) if (trigger.isValid())
m_associations.insertMulti(interactorKey, trigger); m_associations.insertMulti(interactorKey, trigger);
} }
//fallback to default settings //fallback to default settings if necessary
if (m_associations.isEmpty()) if (m_associations.isEmpty())
readDefaultSettings(); m_associations = Palapeli::TriggerMapper::defaultAssociations();
else //announce update to InteractorManagers
emit associationsChanged(); emit associationsChanged();
} }
void Palapeli::TriggerMapper::writeSettings() void Palapeli::TriggerMapper::setAssociations(const QMap<QByteArray, Palapeli::Trigger>& associations)
{ {
m_associations = associations;
//announce update to InteractorManagers
emit associationsChanged();
//assemble trigger serializations //assemble trigger serializations
QMap<QByteArray, QList<QByteArray> > triggerSerializations; QMap<QByteArray, QList<QByteArray> > triggerSerializations;
{ {
......
...@@ -43,12 +43,12 @@ namespace Palapeli ...@@ -43,12 +43,12 @@ namespace Palapeli
static QMap<QByteArray, Palapeli::Interactor*> createInteractors(QGraphicsView* view); static QMap<QByteArray, Palapeli::Interactor*> createInteractors(QGraphicsView* view);
QMap<QByteArray, Palapeli::Trigger> associations() const; QMap<QByteArray, Palapeli::Trigger> associations() const;
static QMap<QByteArray, Palapeli::Trigger> defaultAssociations();
Q_SIGNALS: Q_SIGNALS:
void associationsChanged(); void associationsChanged();
public Q_SLOTS: public Q_SLOTS:
void readDefaultSettings();
void readSettings(); void readSettings();
void writeSettings(); void setAssociations(const QMap<QByteArray, Palapeli::Trigger>& associations);
protected: protected:
friend class InteractorManager; friend class InteractorManager;
......
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