Commit 0f3846ef authored by Stefan Majewsky's avatar Stefan Majewsky

Report state of TriggerConfigWidget to KConfigDialog.

svn path=/trunk/KDE/kdegames/palapeli/; revision=1118500
parent 2d9d78e4
...@@ -66,6 +66,17 @@ Palapeli::ConfigDialog::ConfigDialog(QWidget* parent) ...@@ -66,6 +66,17 @@ Palapeli::ConfigDialog::ConfigDialog(QWidget* parent)
addPage(generalPage, i18n("General settings"))->setIcon(KIcon("configure")); addPage(generalPage, i18n("General settings"))->setIcon(KIcon("configure"));
//setup page "Mouse interaction" //setup page "Mouse interaction"
addPage(m_triggerPage, i18n("Mouse interaction"))->setIcon(KIcon("input-mouse")); addPage(m_triggerPage, i18n("Mouse interaction"))->setIcon(KIcon("input-mouse"));
connect(m_triggerPage, SIGNAL(associationsChanged()), SLOT(updateButtons()));
}
bool Palapeli::ConfigDialog::hasChanged()
{
return m_triggerPage->hasChanged();
}
bool Palapeli::ConfigDialog::isDefault()
{
return m_triggerPage->isDefault();
} }
void Palapeli::ConfigDialog::updateSettings() void Palapeli::ConfigDialog::updateSettings()
......
...@@ -33,6 +33,8 @@ namespace Palapeli ...@@ -33,6 +33,8 @@ namespace Palapeli
public: public:
ConfigDialog(QWidget* parent = 0); ConfigDialog(QWidget* parent = 0);
protected: protected:
virtual bool hasChanged();
virtual bool isDefault();
virtual void updateSettings(); virtual void updateSettings();
virtual void updateWidgets(); virtual void updateWidgets();
virtual void updateWidgetsDefault(); virtual void updateWidgetsDefault();
......
...@@ -31,6 +31,8 @@ Palapeli::TriggerConfigWidget::TriggerConfigWidget(QWidget* parent) ...@@ -31,6 +31,8 @@ Palapeli::TriggerConfigWidget::TriggerConfigWidget(QWidget* parent)
{ {
addTab(m_mouseView, i18n("Mouse buttons")); addTab(m_mouseView, i18n("Mouse buttons"));
addTab(m_wheelView, i18n("Mouse wheel")); addTab(m_wheelView, i18n("Mouse wheel"));
connect(m_mouseView, SIGNAL(associationsChanged()), SIGNAL(associationsChanged()));
connect(m_wheelView, SIGNAL(associationsChanged()), SIGNAL(associationsChanged()));
} }
Palapeli::TriggerConfigWidget::~TriggerConfigWidget() Palapeli::TriggerConfigWidget::~TriggerConfigWidget()
...@@ -38,6 +40,22 @@ Palapeli::TriggerConfigWidget::~TriggerConfigWidget() ...@@ -38,6 +40,22 @@ Palapeli::TriggerConfigWidget::~TriggerConfigWidget()
qDeleteAll(m_interactors); qDeleteAll(m_interactors);
} }
bool Palapeli::TriggerConfigWidget::hasChanged() const
{
QMap<QByteArray, Palapeli::Trigger> associations;
m_mouseView->getAssociations(associations);
m_wheelView->getAssociations(associations);
return associations != Palapeli::TriggerMapper::instance()->associations();
}
bool Palapeli::TriggerConfigWidget::isDefault() const
{
QMap<QByteArray, Palapeli::Trigger> associations;
m_mouseView->getAssociations(associations);
m_wheelView->getAssociations(associations);
return associations == Palapeli::TriggerMapper::defaultAssociations();
}
void Palapeli::TriggerConfigWidget::updateSettings() void Palapeli::TriggerConfigWidget::updateSettings()
{ {
QMap<QByteArray, Palapeli::Trigger> associations; QMap<QByteArray, Palapeli::Trigger> associations;
...@@ -51,6 +69,7 @@ void Palapeli::TriggerConfigWidget::updateWidgets() ...@@ -51,6 +69,7 @@ void Palapeli::TriggerConfigWidget::updateWidgets()
const QMap<QByteArray, Palapeli::Trigger> associations = Palapeli::TriggerMapper::instance()->associations(); const QMap<QByteArray, Palapeli::Trigger> associations = Palapeli::TriggerMapper::instance()->associations();
m_mouseView->setAssociations(associations); m_mouseView->setAssociations(associations);
m_wheelView->setAssociations(associations); m_wheelView->setAssociations(associations);
emit associationsChanged();
} }
void Palapeli::TriggerConfigWidget::updateWidgetsDefault() void Palapeli::TriggerConfigWidget::updateWidgetsDefault()
...@@ -58,4 +77,7 @@ void Palapeli::TriggerConfigWidget::updateWidgetsDefault() ...@@ -58,4 +77,7 @@ void Palapeli::TriggerConfigWidget::updateWidgetsDefault()
const QMap<QByteArray, Palapeli::Trigger> associations = Palapeli::TriggerMapper::defaultAssociations(); const QMap<QByteArray, Palapeli::Trigger> associations = Palapeli::TriggerMapper::defaultAssociations();
m_mouseView->setAssociations(associations); m_mouseView->setAssociations(associations);
m_wheelView->setAssociations(associations); m_wheelView->setAssociations(associations);
emit associationsChanged();
} }
#include "triggerconfigwidget.moc"
...@@ -29,14 +29,19 @@ namespace Palapeli ...@@ -29,14 +29,19 @@ namespace Palapeli
class TriggerConfigWidget : public QTabWidget class TriggerConfigWidget : public QTabWidget
{ {
Q_OBJECT
public: public:
//TODO: Provide signal interface for changes (to enable "Apply" button in config dialog.) //TODO: Provide signal interface for changes (to enable "Apply" button in config dialog.)
TriggerConfigWidget(QWidget* parent = 0); TriggerConfigWidget(QWidget* parent = 0);
virtual ~TriggerConfigWidget(); virtual ~TriggerConfigWidget();
bool hasChanged() const;
bool isDefault() const;
void updateSettings(); void updateSettings();
void updateWidgets(); void updateWidgets();
void updateWidgetsDefault(); void updateWidgetsDefault();
Q_SIGNALS:
void associationsChanged();
private: private:
QMap<QByteArray, Palapeli::Interactor*> m_interactors; QMap<QByteArray, Palapeli::Interactor*> m_interactors;
Palapeli::TriggerListView* m_mouseView; Palapeli::TriggerListView* m_mouseView;
......
...@@ -17,140 +17,7 @@ ...@@ -17,140 +17,7 @@
***************************************************************************/ ***************************************************************************/
#include "triggerlistview.h" #include "triggerlistview.h"
#include "elidinglabel.h" #include "triggerlistview_p.h"
#include "mouseinputbutton.h"
#include <QApplication>
#include <QHBoxLayout>
#include <QLabel>
#include <QStandardItemModel>
#include <KCategorizedSortFilterProxyModel>
#include <KCategoryDrawer>
#include <KLocalizedString>
#include <KStringHandler>
#include <KWidgetItemDelegate>
namespace Palapeli
{
class TriggerListProxyModel : public KCategorizedSortFilterProxyModel
{
public:
TriggerListProxyModel(QObject* parent = 0)
: KCategorizedSortFilterProxyModel(parent)
{
setCategorizedModel(true);
}
protected:
virtual int compareCategories(const QModelIndex& left, const QModelIndex& right) const
{
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(Qt::DisplayRole).toString();
const QString textRight = right.data(Qt::DisplayRole).toString();
return KStringHandler::naturalCompare(textLeft, textRight) < 0;
}
};
class TriggerListDelegateWidget : public QWidget
{
Q_OBJECT
public:
TriggerListDelegateWidget(QWidget* parent = 0) : QWidget(parent)
{
m_iconLabel = new QLabel(this);
m_nameLabel = new Palapeli::ElidingLabel(this);
m_inputButton = new Palapeli::MouseInputButton(this);
connect(m_inputButton, SIGNAL(triggerChanged(const Palapeli::Trigger&)), SIGNAL(triggerChanged(const Palapeli::Trigger&)));
//construct layout
QHBoxLayout* layout = new QHBoxLayout;
setLayout(layout);
layout->addWidget(m_iconLabel);
m_iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
layout->addWidget(m_nameLabel);
layout->addWidget(m_inputButton);
m_inputButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
}
void setIcon(const QIcon& icon)
{
//TODO: respect global icon size configuration
m_iconLabel->setPixmap(icon.pixmap(22));
}
void setText(const QString& text)
{
m_nameLabel->setFullText(text);
}
void setTrigger(const Palapeli::Trigger& trigger)
{
m_inputButton->setTrigger(trigger);
}
void setInteractorType(Palapeli::InteractorType type)
{
m_inputButton->setMouseAllowed(type == Palapeli::MouseInteractor);
m_inputButton->setWheelAllowed(type == Palapeli::WheelInteractor);
}
Q_SIGNALS:
void triggerChanged(const Palapeli::Trigger& newTrigger);
private:
QLabel* m_iconLabel;
Palapeli::ElidingLabel* m_nameLabel;
Palapeli::MouseInputButton* m_inputButton;
};
class TriggerListDelegate : public KWidgetItemDelegate
{
Q_OBJECT
public:
TriggerListDelegate(QAbstractItemView* view, QObject* parent = 0) : KWidgetItemDelegate(view, parent)
{
m_calculator = new Palapeli::TriggerListDelegateWidget(view);
m_calculator->setVisible(false);
}
virtual void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
Q_UNUSED(index)
QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
}
virtual QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
updateItemWidgets(QList<QWidget*>() << m_calculator, option, index);
return m_calculator->minimumSizeHint();
}
protected:
virtual QList<QWidget*> createItemWidgets() const
{
return QList<QWidget*>() << new Palapeli::TriggerListDelegateWidget(itemView());
}
virtual void updateItemWidgets(QList<QWidget*> widgets, const QStyleOptionViewItem& option, const QPersistentModelIndex& index) const
{
Palapeli::TriggerListDelegateWidget* widget = qobject_cast<Palapeli::TriggerListDelegateWidget*>(widgets[0]);
//adjust widget contents
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::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);
//adjust widget behavior
widget->setInteractorType((Palapeli::InteractorType) index.data(Palapeli::InteractorTypeRole).toInt());
}
private Q_SLOTS:
void triggerChanged(const Palapeli::Trigger& newTrigger)
{
const QModelIndex index = focusedIndex();
QAbstractItemModel* model = const_cast<QAbstractItemModel*>(index.model());
model->setData(index, qVariantFromValue(newTrigger), Palapeli::TriggerRole);
}
private:
Palapeli::TriggerListDelegateWidget* m_calculator;
};
}
static QString categoryToString(Palapeli::Interactor::Category category) static QString categoryToString(Palapeli::Interactor::Category category)
{ {
...@@ -197,6 +64,7 @@ Palapeli::TriggerListView::TriggerListView(const QMap<QByteArray, Palapeli::Inte ...@@ -197,6 +64,7 @@ Palapeli::TriggerListView::TriggerListView(const QMap<QByteArray, Palapeli::Inte
m_proxyModel->setSourceModel(m_baseModel); m_proxyModel->setSourceModel(m_baseModel);
setModel(m_proxyModel); setModel(m_proxyModel);
setItemDelegate(m_delegate); setItemDelegate(m_delegate);
connect(m_delegate, SIGNAL(triggerChanged()), SIGNAL(associationsChanged()));
// setCategoryDrawer(m_categoryDrawer); //FIXME: Why do I crash? // setCategoryDrawer(m_categoryDrawer); //FIXME: Why do I crash?
} }
...@@ -227,3 +95,4 @@ void Palapeli::TriggerListView::setAssociations(const QMap<QByteArray, Palapeli: ...@@ -227,3 +95,4 @@ void Palapeli::TriggerListView::setAssociations(const QMap<QByteArray, Palapeli:
} }
#include "triggerlistview.moc" #include "triggerlistview.moc"
#include "triggerlistview_p.moc"
...@@ -35,12 +35,15 @@ namespace Palapeli ...@@ -35,12 +35,15 @@ namespace Palapeli
class TriggerListView : public KCategorizedView class TriggerListView : public KCategorizedView
{ {
Q_OBJECT
public: public:
TriggerListView(const QMap<QByteArray, Palapeli::Interactor*>& interactors, Palapeli::InteractorType interactorType, QWidget* parent = 0); TriggerListView(const QMap<QByteArray, Palapeli::Interactor*>& interactors, Palapeli::InteractorType interactorType, QWidget* parent = 0);
virtual ~TriggerListView(); virtual ~TriggerListView();
void getAssociations(QMap<QByteArray, Palapeli::Trigger>& associations); void getAssociations(QMap<QByteArray, Palapeli::Trigger>& associations);
void setAssociations(const QMap<QByteArray, Palapeli::Trigger>& associations); void setAssociations(const QMap<QByteArray, Palapeli::Trigger>& associations);
Q_SIGNALS:
void associationsChanged();
private: private:
KCategoryDrawer* m_categoryDrawer; KCategoryDrawer* m_categoryDrawer;
QStandardItemModel* m_baseModel; QStandardItemModel* m_baseModel;
......
/***************************************************************************
* 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 Library 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library 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_TRIGGERLISTVIEW_P_H
#define PALAPELI_TRIGGERLISTVIEW_P_H
#include "triggerlistview.h"
#include "elidinglabel.h"
#include "mouseinputbutton.h"
#include <QApplication>
#include <QHBoxLayout>
#include <QLabel>
#include <QStandardItemModel>
#include <KCategorizedSortFilterProxyModel>
#include <KCategoryDrawer>
#include <KLocalizedString>
#include <KStringHandler>
#include <KWidgetItemDelegate>
namespace Palapeli
{
class TriggerListProxyModel : public KCategorizedSortFilterProxyModel
{
public:
TriggerListProxyModel(QObject* parent = 0)
: KCategorizedSortFilterProxyModel(parent)
{
setCategorizedModel(true);
}
protected:
virtual int compareCategories(const QModelIndex& left, const QModelIndex& right) const
{
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(Qt::DisplayRole).toString();
const QString textRight = right.data(Qt::DisplayRole).toString();
return KStringHandler::naturalCompare(textLeft, textRight) < 0;
}
};
class TriggerListDelegateWidget : public QWidget
{
Q_OBJECT
public:
TriggerListDelegateWidget(QWidget* parent = 0) : QWidget(parent)
{
m_iconLabel = new QLabel(this);
m_nameLabel = new Palapeli::ElidingLabel(this);
m_inputButton = new Palapeli::MouseInputButton(this);
connect(m_inputButton, SIGNAL(triggerChanged(const Palapeli::Trigger&)), SIGNAL(triggerChanged(const Palapeli::Trigger&)));
//construct layout
QHBoxLayout* layout = new QHBoxLayout;
setLayout(layout);
layout->addWidget(m_iconLabel);
m_iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
layout->addWidget(m_nameLabel);
layout->addWidget(m_inputButton);
m_inputButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
}
void setIcon(const QIcon& icon)
{
//TODO: respect global icon size configuration
m_iconLabel->setPixmap(icon.pixmap(22));
}
void setText(const QString& text)
{
m_nameLabel->setFullText(text);
}
void setTrigger(const Palapeli::Trigger& trigger)
{
m_inputButton->setTrigger(trigger);
}
void setInteractorType(Palapeli::InteractorType type)
{
m_inputButton->setMouseAllowed(type == Palapeli::MouseInteractor);
m_inputButton->setWheelAllowed(type == Palapeli::WheelInteractor);
}
Q_SIGNALS:
void triggerChanged(const Palapeli::Trigger& newTrigger);
private:
QLabel* m_iconLabel;
Palapeli::ElidingLabel* m_nameLabel;
Palapeli::MouseInputButton* m_inputButton;
};
class TriggerListDelegate : public KWidgetItemDelegate
{
Q_OBJECT
public:
TriggerListDelegate(QAbstractItemView* view, QObject* parent = 0) : KWidgetItemDelegate(view, parent)
{
m_calculator = new Palapeli::TriggerListDelegateWidget(view);
m_calculator->setVisible(false);
}
virtual void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
Q_UNUSED(index)
QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
}
virtual QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
updateItemWidgets(QList<QWidget*>() << m_calculator, option, index);
return m_calculator->minimumSizeHint();
}
protected:
virtual QList<QWidget*> createItemWidgets() const
{
return QList<QWidget*>() << new Palapeli::TriggerListDelegateWidget(itemView());
}
virtual void updateItemWidgets(QList<QWidget*> widgets, const QStyleOptionViewItem& option, const QPersistentModelIndex& index) const
{
Palapeli::TriggerListDelegateWidget* widget = qobject_cast<Palapeli::TriggerListDelegateWidget*>(widgets[0]);
//adjust widget contents
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::Trigger>());
connect(widget, SIGNAL(triggerChanged(const Palapeli::Trigger&)), SLOT(slotTriggerChanged(const Palapeli::Trigger&)));
//adjust widget geometry
QRect rect = option.rect;
rect.moveTop(0);
widget->setGeometry(rect);
//adjust widget behavior
widget->setInteractorType((Palapeli::InteractorType) index.data(Palapeli::InteractorTypeRole).toInt());
}
Q_SIGNALS:
void triggerChanged();
private Q_SLOTS:
void slotTriggerChanged(const Palapeli::Trigger& newTrigger)
{
const QModelIndex index = focusedIndex();
QAbstractItemModel* model = const_cast<QAbstractItemModel*>(index.model());
model->setData(index, qVariantFromValue(newTrigger), Palapeli::TriggerRole);
emit triggerChanged();
}
private:
Palapeli::TriggerListDelegateWidget* m_calculator;
};
}
#endif // PALAPELI_TRIGGERLISTVIEW_P_H
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