Commit 2e2b2a4f authored by Stefan Majewsky's avatar Stefan Majewsky

Straight port of TriggerConfigWidget and TriggerListView to the recent API...

Straight port of TriggerConfigWidget and TriggerListView to the recent API changes in the mouse interaction stack.

svn path=/trunk/KDE/kdegames/palapeli/; revision=1118491
parent 42fc7502
......@@ -4,9 +4,9 @@ set_directory_properties(PROPERTIES COMPILE_DEFINITIONS USE_LOCAL_LIBPALA)
set(palapeli_SRCS
config/configdialog.cpp
config/elidinglabel.cpp
#config/mouseinputbutton.cpp
#config/triggerconfigwidget.cpp
#config/triggerlistview.cpp
config/mouseinputbutton.cpp
config/triggerconfigwidget.cpp
config/triggerlistview.cpp
creator/propertywidget.cpp
creator/puzzlecreator.cpp
creator/slicerconfwidget.cpp
......
......@@ -18,6 +18,7 @@
#include "configdialog.h"
#include "configdialog_p.h"
#include "triggerconfigwidget.h"
#include "../engine/texturehelper.h"
#include "settings.h"
......@@ -56,13 +57,15 @@ void Palapeli::TriggerComboBox::handleCurrentIndexChanged(int index)
Palapeli::ConfigDialog::ConfigDialog(QWidget* parent)
: KConfigDialog(parent, QString(), Settings::self())
, m_triggerPage(new Palapeli::TriggerConfigWidget)
{
//setup page "General settings"
QWidget* generalPage = new QWidget;
m_generalUi.setupUi(generalPage);
m_generalUi.kcfg_ViewBackground->setModel(Palapeli::TextureHelper::instance());
addPage(generalPage, i18n("General settings"))->setIcon(KIcon("configure"));
//TODO: add TriggerConfigWidget
//setup page "Mouse interaction"
addPage(m_triggerPage, i18n("Mouse interaction"))->setIcon(KIcon("input-mouse"));
}
void Palapeli::ConfigDialog::updateSettings()
......
......@@ -25,6 +25,8 @@
namespace Palapeli
{
class TriggerConfigWidget;
class ConfigDialog : public KConfigDialog
{
Q_OBJECT
......@@ -36,6 +38,7 @@ namespace Palapeli
virtual void updateWidgetsDefault();
private:
Ui::Settings m_generalUi;
Palapeli::TriggerConfigWidget* m_triggerPage;
};
}
......
......@@ -44,7 +44,7 @@ Palapeli::MouseInputButton::MouseInputButton(QWidget* parent)
, m_noButtonAllowed(true)
, m_requiresValidation(false)
{
qRegisterMetaType<Palapeli::InteractorTrigger>();
qRegisterMetaType<Palapeli::Trigger>();
connect(this, SIGNAL(clicked()), SLOT(captureTrigger()));
connect(m_clearButton, SIGNAL(clicked()), SLOT(clearTrigger()));
setCheckable(true);
......@@ -99,7 +99,7 @@ void Palapeli::MouseInputButton::setRequiresValidation(bool requiresValidation)
m_requiresValidation = requiresValidation;
}
Palapeli::InteractorTrigger Palapeli::MouseInputButton::trigger() const
Palapeli::Trigger Palapeli::MouseInputButton::trigger() const
{
return m_trigger;
}
......@@ -115,7 +115,7 @@ void Palapeli::MouseInputButton::captureTrigger()
void Palapeli::MouseInputButton::clearTrigger()
{
setTrigger(Palapeli::InteractorTrigger());
setTrigger(Palapeli::Trigger());
}
bool Palapeli::MouseInputButton::event(QEvent* event)
......@@ -129,7 +129,7 @@ bool Palapeli::MouseInputButton::event(QEvent* event)
switch ((int) event->type())
{
case QEvent::Wheel: {
Palapeli::InteractorTrigger newTrigger;
Palapeli::Trigger newTrigger;
newTrigger.setModifiers(wEvent->modifiers());
newTrigger.setWheelDirection(wEvent->orientation());
setTrigger(newTrigger);
......@@ -137,7 +137,7 @@ bool Palapeli::MouseInputButton::event(QEvent* event)
return true;
}
case QEvent::MouseButtonRelease: {
Palapeli::InteractorTrigger newTrigger;
Palapeli::Trigger newTrigger;
newTrigger.setModifiers(mEvent->modifiers());
newTrigger.setButton(mEvent->button());
setTrigger(newTrigger);
......@@ -158,7 +158,7 @@ bool Palapeli::MouseInputButton::event(QEvent* event)
if (kEvent->key() == Qt::Key_Space && m_noButtonAllowed)
{
//create trigger with NoButton (TODO: make this functionality more user-visible)
Palapeli::InteractorTrigger newTrigger;
Palapeli::Trigger newTrigger;
newTrigger.setModifiers(kEvent->modifiers());
newTrigger.setButton(Qt::NoButton);
setTrigger(newTrigger);
......@@ -182,7 +182,7 @@ bool Palapeli::MouseInputButton::event(QEvent* event)
return ret;
}
void Palapeli::MouseInputButton::setTrigger(const Palapeli::InteractorTrigger& trigger)
void Palapeli::MouseInputButton::setTrigger(const Palapeli::Trigger& trigger)
{
//NOTE: Invalid triggers need not be confirmed (esp. calls to clearTrigger()).
if (m_requiresValidation && trigger.isValid() && m_trigger != trigger)
......@@ -194,7 +194,7 @@ void Palapeli::MouseInputButton::setTrigger(const Palapeli::InteractorTrigger& t
applyTrigger(trigger);
}
void Palapeli::MouseInputButton::confirmTrigger(const Palapeli::InteractorTrigger& trigger)
void Palapeli::MouseInputButton::confirmTrigger(const Palapeli::Trigger& trigger)
{
if (m_stagedTrigger == trigger)
applyTrigger(m_stagedTrigger);
......@@ -214,12 +214,12 @@ void Palapeli::MouseInputButton::updateAppearance()
m_clearButton->setVisible(m_trigger.isValid());
}
void Palapeli::MouseInputButton::applyTrigger(const Palapeli::InteractorTrigger& trigger)
void Palapeli::MouseInputButton::applyTrigger(const Palapeli::Trigger& trigger)
{
const bool announceChange = m_trigger != trigger;
//apply new trigger
m_trigger = trigger;
m_stagedTrigger = Palapeli::InteractorTrigger();
m_stagedTrigger = Palapeli::Trigger();
updateAppearance();
//announce change
if (announceChange)
......@@ -228,7 +228,7 @@ void Palapeli::MouseInputButton::applyTrigger(const Palapeli::InteractorTrigger&
void Palapeli::MouseInputButton::showModifiers(Qt::KeyboardModifiers modifiers)
{
Palapeli::InteractorTrigger dummyTrigger;
Palapeli::Trigger dummyTrigger;
dummyTrigger.setModifiers(modifiers);
m_mainLabel->setText(dummyTrigger.toString().arg(i18n("Input here...")));
}
......
......@@ -20,7 +20,7 @@
#ifndef PALAPELI_MOUSEINPUTBUTTON_H
#define PALAPELI_MOUSEINPUTBUTTON_H
#include "../engine/interactorutils.h"
#include "../engine/trigger.h"
class QLabel;
#include <QPushButton>
......@@ -42,20 +42,20 @@ namespace Palapeli
///If set, a call to setTrigger() will not immediately change the trigger. Instead, the triggerRequest() signal will be fired, and the new trigger will be set only after confirmTrigger() has been called.
bool requiresValidation() const;
void setRequiresValidation(bool requiresValidation);
Palapeli::InteractorTrigger trigger() const;
Palapeli::Trigger trigger() const;
Q_SIGNALS:
void triggerChanged(const Palapeli::InteractorTrigger& newTrigger);
void triggerRequest(const Palapeli::InteractorTrigger& newTrigger);
void triggerChanged(const Palapeli::Trigger& newTrigger);
void triggerRequest(const Palapeli::Trigger& newTrigger);
public Q_SLOTS:
void captureTrigger();
void clearTrigger();
void confirmTrigger(const Palapeli::InteractorTrigger& trigger);
void setTrigger(const Palapeli::InteractorTrigger& trigger);
void confirmTrigger(const Palapeli::Trigger& trigger);
void setTrigger(const Palapeli::Trigger& trigger);
protected:
bool event(QEvent* event);
private:
void updateAppearance();
void applyTrigger(const Palapeli::InteractorTrigger& newTrigger);
void applyTrigger(const Palapeli::Trigger& newTrigger);
void showModifiers(Qt::KeyboardModifiers modifiers);
static const QString DefaultToolTip;
......@@ -64,7 +64,7 @@ namespace Palapeli
QLabel* m_mainLabel;
QLabel* m_clearButton;
Palapeli::InteractorTrigger m_trigger, m_stagedTrigger; ///< m_stagedTrigger is the trigger which has been set with setTrigger(), but which is still waiting to be confirmed with confirmTrigger().
Palapeli::Trigger m_trigger, m_stagedTrigger; ///< m_stagedTrigger is the trigger which has been set with setTrigger(), but which is still waiting to be confirmed with confirmTrigger().
bool m_noButtonAllowed, m_requiresValidation;
};
}
......
......@@ -18,13 +18,14 @@
#include "triggerconfigwidget.h"
#include "triggerlistview.h"
#include "../engine/interactormanager.h"
#include "../engine/interactor.h"
#include "../engine/triggermapper.h"
#include <KLocalizedString>
Palapeli::TriggerConfigWidget::TriggerConfigWidget(Palapeli::InteractorManager* manager, QWidget* parent)
Palapeli::TriggerConfigWidget::TriggerConfigWidget(QWidget* parent)
: QTabWidget(parent)
, m_manager(manager)
, m_interactors(Palapeli::TriggerMapper::createInteractors(0)) //these interactors are just for reading metadata
{
createTriggerListView(m_mouseView, Palapeli::MouseInteractor);
createTriggerListView(m_wheelView, Palapeli::WheelInteractor);
......@@ -32,22 +33,27 @@ Palapeli::TriggerConfigWidget::TriggerConfigWidget(Palapeli::InteractorManager*
addTab(m_wheelView, i18n("Mouse wheel"));
}
void Palapeli::TriggerConfigWidget::createTriggerListView(Palapeli::TriggerListView*& view, Palapeli::InteractorTypes types)
Palapeli::TriggerConfigWidget::~TriggerConfigWidget()
{
qDeleteAll(m_interactors);
}
void Palapeli::TriggerConfigWidget::createTriggerListView(Palapeli::TriggerListView*& view, int interactorType)
{
//filter interactors
QList<Palapeli::Interactor*> interactors = m_manager->interactors();
QMutableListIterator<Palapeli::Interactor*> it1(interactors);
while (it1.hasNext())
if ((it1.next()->interactorTypes() & types) != types)
it1.remove();
//filter triggers
QList<Palapeli::AssociatedInteractorTrigger> triggers = m_manager->triggers();
QMutableListIterator<Palapeli::AssociatedInteractorTrigger> it2(triggers);
while (it2.hasNext())
if (!interactors.contains(it2.next().second))
it2.remove();
QMap<QByteArray, Palapeli::Interactor*> interactors(m_interactors);
QMutableMapIterator<QByteArray, Palapeli::Interactor*> iter1(interactors);
while (iter1.hasNext())
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, triggers, this);
view = new Palapeli::TriggerListView(interactors, associations, this);
}
void Palapeli::TriggerConfigWidget::writeConfig()
......
......@@ -19,25 +19,26 @@
#ifndef PALAPELI_TRIGGERCONFIGWIDGET_H
#define PALAPELI_TRIGGERCONFIGWIDGET_H
#include "../engine/interactor.h"
#include <QMap>
#include <QTabWidget>
namespace Palapeli
{
class InteractorManager;
class Interactor;
class TriggerListView;
class TriggerConfigWidget : public QTabWidget
{
public:
//TODO: Provide signal interface for changes (to enable "Apply" button in config dialog.)
TriggerConfigWidget(Palapeli::InteractorManager* manager, QWidget* parent = 0);
TriggerConfigWidget(QWidget* parent = 0);
virtual ~TriggerConfigWidget();
void writeConfig();
private:
void createTriggerListView(Palapeli::TriggerListView*& view, Palapeli::InteractorTypes types);
void createTriggerListView(Palapeli::TriggerListView*& view, int type);
Palapeli::InteractorManager* m_manager;
QMap<QByteArray, Palapeli::Interactor*> m_interactors;
Palapeli::TriggerListView* m_mouseView;
Palapeli::TriggerListView* m_wheelView;
};
......
......@@ -31,7 +31,6 @@
#include <KStringHandler>
#include <KWidgetItemDelegate>
#include <KDebug>
namespace Palapeli
{
class TriggerListProxyModel : public KCategorizedSortFilterProxyModel
......@@ -45,14 +44,14 @@ namespace Palapeli
protected:
virtual int compareCategories(const QModelIndex& left, const QModelIndex& right) const
{
const int categoryLeft = left.data(Palapeli::InteractorRole).value<Palapeli::Interactor*>()->category();
const int categoryRight = right.data(Palapeli::InteractorRole).value<Palapeli::Interactor*>()->category();
const int categoryLeft = left.data(KCategorizedSortFilterProxyModel::CategorySortRole).value<int>();
const int categoryRight = right.data(KCategorizedSortFilterProxyModel::CategorySortRole).value<int>();
return categoryRight - categoryLeft;
}
virtual bool subSortLessThan(const QModelIndex& left, const QModelIndex& right) const
{
const QString textLeft = left.data(Palapeli::InteractorRole).value<Palapeli::Interactor*>()->description();
const QString textRight = right.data(Palapeli::InteractorRole).value<Palapeli::Interactor*>()->description();
const QString textLeft = left.data(Qt::DisplayRole).toString();
const QString textRight = right.data(Qt::DisplayRole).toString();
return KStringHandler::naturalCompare(textLeft, textRight) < 0;
}
};
......@@ -66,7 +65,7 @@ namespace Palapeli
m_iconLabel = new QLabel(this);
m_nameLabel = new Palapeli::ElidingLabel(this);
m_inputButton = new Palapeli::MouseInputButton(this);
connect(m_inputButton, SIGNAL(triggerChanged(const Palapeli::InteractorTrigger&)), SIGNAL(triggerChanged(const Palapeli::InteractorTrigger&)));
connect(m_inputButton, SIGNAL(triggerChanged(const Palapeli::Trigger&)), SIGNAL(triggerChanged(const Palapeli::Trigger&)));
//construct layout
QHBoxLayout* layout = new QHBoxLayout;
setLayout(layout);
......@@ -85,12 +84,12 @@ namespace Palapeli
{
m_nameLabel->setFullText(text);
}
void setTrigger(const Palapeli::InteractorTrigger& trigger)
void setTrigger(const Palapeli::Trigger& trigger)
{
m_inputButton->setTrigger(trigger);
}
Q_SIGNALS:
void triggerChanged(const Palapeli::InteractorTrigger& newTrigger);
void triggerChanged(const Palapeli::Trigger& newTrigger);
private:
QLabel* m_iconLabel;
Palapeli::ElidingLabel* m_nameLabel;
......@@ -128,15 +127,15 @@ namespace Palapeli
widget->setIcon(index.data(Qt::DecorationRole).value<QIcon>());
widget->setText(index.data(Qt::DisplayRole).value<QString>());
disconnect(widget, 0, this, 0);
widget->setTrigger(index.data(TriggerRole).value<Palapeli::InteractorTrigger>());
connect(widget, SIGNAL(triggerChanged(const Palapeli::InteractorTrigger&)), SLOT(triggerChanged(const Palapeli::InteractorTrigger&)));
widget->setTrigger(index.data(TriggerRole).value<Palapeli::Trigger>());
connect(widget, SIGNAL(triggerChanged(const Palapeli::Trigger&)), SLOT(triggerChanged(const Palapeli::Trigger&)));
//adjust widget geometry
QRect rect = option.rect;
rect.moveTop(0);
widget->setGeometry(rect);
}
private Q_SLOTS:
void triggerChanged(const Palapeli::InteractorTrigger& newTrigger)
void triggerChanged(const Palapeli::Trigger& newTrigger)
{
const QModelIndex index = focusedIndex();
QAbstractItemModel* model = const_cast<QAbstractItemModel*>(index.model());
......@@ -162,7 +161,7 @@ static QString categoryToString(Palapeli::Interactor::Category category)
}
}
Palapeli::TriggerListView::TriggerListView(const QList<Palapeli::Interactor*> interactors, const QList<Palapeli::AssociatedInteractorTrigger>& triggers, QWidget* parent)
Palapeli::TriggerListView::TriggerListView(const QMap<QByteArray, Palapeli::Interactor*>& interactors, const QMap<QByteArray, Palapeli::Trigger>& associations, QWidget* parent)
: KCategorizedView(parent)
, m_categoryDrawer(new KCategoryDrawer)
, m_baseModel(new QStandardItemModel(this))
......@@ -170,30 +169,19 @@ Palapeli::TriggerListView::TriggerListView(const QList<Palapeli::Interactor*> in
, m_delegate(new Palapeli::TriggerListDelegate(this))
{
//fill base model with interactors
foreach (Palapeli::Interactor* interactor, interactors)
QMap<QByteArray, Palapeli::Interactor*>::const_iterator it1 = interactors.begin(), it2 = interactors.end();
for (; it1 != it2; ++it1)
{
Palapeli::Interactor* interactor = it1.value();
QStandardItem* item = new QStandardItem;
item->setData(interactor->description(), Qt::DisplayRole);
item->setData(interactor->icon(), Qt::DecorationRole);
item->setData(qVariantFromValue(interactor), Palapeli::InteractorRole);
item->setData(qVariantFromValue(Palapeli::InteractorTrigger()), Palapeli::TriggerRole);
item->setData(it1.key(), Palapeli::InteractorRole);
item->setData(qVariantFromValue(associations.value(it1.key())), Palapeli::TriggerRole);
item->setData(categoryToString(interactor->category()), KCategorizedSortFilterProxyModel::CategoryDisplayRole);
item->setData(interactor->category(), KCategorizedSortFilterProxyModel::CategorySortRole);
m_baseModel->appendRow(item);
}
//fill base model with triggers
foreach (const Palapeli::AssociatedInteractorTrigger& trigger, triggers)
{
for (int i = 0; i < m_baseModel->rowCount(); ++i)
{
QStandardItem* item = m_baseModel->item(i);
if (item->data(Palapeli::InteractorRole).value<Palapeli::Interactor*>() == trigger.second)
{
item->setData(qVariantFromValue(trigger.first), Palapeli::TriggerRole);
break;
}
}
}
//setup model/view
m_proxyModel->setSourceModel(m_baseModel);
setModel(m_proxyModel);
......@@ -206,16 +194,16 @@ Palapeli::TriggerListView::~TriggerListView()
delete m_categoryDrawer;
}
QList<Palapeli::AssociatedInteractorTrigger> Palapeli::TriggerListView::triggers() const
QMap<QByteArray, Palapeli::Trigger> Palapeli::TriggerListView::triggers() const
{
//read triggers from base model
QList<Palapeli::AssociatedInteractorTrigger> result;
QMap<QByteArray, Palapeli::Trigger> result;
for (int i = 0; i < m_baseModel->rowCount(); ++i)
{
QStandardItem* item = m_baseModel->item(i);
Palapeli::Interactor* interactor = item->data(Palapeli::InteractorRole).value<Palapeli::Interactor*>();
Palapeli::InteractorTrigger trigger = item->data(Palapeli::TriggerRole).value<Palapeli::InteractorTrigger>();
result << qMakePair(trigger, interactor);
const QByteArray interactor = item->data(Palapeli::InteractorRole).value<QByteArray>();
const Palapeli::Trigger trigger = item->data(Palapeli::TriggerRole).value<Palapeli::Trigger>();
result.insert(interactor, trigger);
}
return result;
}
......
......@@ -19,7 +19,7 @@
#ifndef PALAPELI_TRIGGERLISTVIEW_H
#define PALAPELI_TRIGGERLISTVIEW_H
#include "../engine/interactorutils.h"
#include "../engine/trigger.h"
class QStandardItemModel;
class KCategorizedSortFilterProxyModel;
......@@ -28,16 +28,17 @@ class KCategoryDrawer;
namespace Palapeli
{
class Interactor;
class TriggerListDelegate;
enum TriggerListRoles { TriggerRole = Qt::UserRole + 42, InteractorRole };
class TriggerListView : public KCategorizedView
{
public:
TriggerListView(const QList<Palapeli::Interactor*> interactors, const QList<Palapeli::AssociatedInteractorTrigger>& triggers, QWidget* parent = 0);
TriggerListView(const QMap<QByteArray, Palapeli::Interactor*>& interactors, const QMap<QByteArray, Palapeli::Trigger>& associations, QWidget* parent = 0);
virtual ~TriggerListView();
QList<Palapeli::AssociatedInteractorTrigger> triggers() const;
QMap<QByteArray, Palapeli::Trigger> triggers() const;
private:
KCategoryDrawer* m_categoryDrawer;
QStandardItemModel* m_baseModel;
......
......@@ -31,6 +31,11 @@ Palapeli::InteractorManager::InteractorManager(QGraphicsView* view)
connect(Palapeli::TriggerMapper::instance(), SIGNAL(associationsChanged()), SLOT(resetActiveTriggers()));
}
Palapeli::InteractorManager::~InteractorManager()
{
qDeleteAll(m_interactors);
}
void Palapeli::InteractorManager::updateScene()
{
foreach (Palapeli::Interactor* interactor, m_interactors)
......
......@@ -33,6 +33,7 @@ namespace Palapeli
Q_OBJECT
public:
explicit InteractorManager(QGraphicsView* view);
virtual ~InteractorManager();
void handleEvent(QWheelEvent* event);
void handleEvent(QMouseEvent* event);
......
......@@ -19,6 +19,7 @@
#ifndef PALAPELI_TRIGGER_H
#define PALAPELI_TRIGGER_H
#include <QMetaType>
#include <QString>
namespace Palapeli
......@@ -61,4 +62,6 @@ bool Palapeli::Trigger::operator!=(const Palapeli::Trigger& other) const
return !(*this == other);
}
Q_DECLARE_METATYPE(Palapeli::Trigger)
#endif // PALAPELI_TRIGGER_H
......@@ -56,6 +56,11 @@ Palapeli::TriggerMapper::TriggerMapper()
readSettings();
}
QMap<QByteArray, Palapeli::Trigger> Palapeli::TriggerMapper::associations() const
{
return m_associations;
}
void Palapeli::TriggerMapper::readDefaultSettings()
{
m_associations.clear();
......
......@@ -40,6 +40,9 @@ namespace Palapeli
Q_OBJECT
public:
static Palapeli::TriggerMapper* instance();
static QMap<QByteArray, Palapeli::Interactor*> createInteractors(QGraphicsView* view);
QMap<QByteArray, Palapeli::Trigger> associations() const;
Q_SIGNALS:
void associationsChanged();
public Q_SLOTS:
......@@ -48,7 +51,6 @@ namespace Palapeli
void writeSettings();
protected:
friend class InteractorManager;
static QMap<QByteArray, Palapeli::Interactor*> createInteractors(QGraphicsView* view);
Palapeli::EventProcessingFlags testTrigger(const QByteArray& interactor, QWheelEvent* event) const;
Palapeli::EventContext testTrigger(const QByteArray& interactor, QMouseEvent* event) const;
......
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