Commit 4f304f9b authored by Stefan Majewsky's avatar Stefan Majewsky

Make API of MouseInputButton more like KKeySequenceWidget.

The key difference between these two API is that KKeySequenceWidget has
complex built-in validation methods, while MouseInputButton completely
delegates the validation to higher authorities.

svn path=/trunk/KDE/kdegames/palapeli/; revision=1112610
parent a010ab4f
......@@ -23,25 +23,20 @@
#include <QKeyEvent>
#include <KLocalizedString>
const QString Palapeli::MouseInputButton::DefaultText = i18n("Set Trigger...");
const QString Palapeli::MouseInputButton::DefaultToolTip = i18n("Click to change how an action is triggered");
Palapeli::MouseInputButton::MouseInputButton(QWidget* parent)
: QPushButton(parent)
, m_noButtonAllowed(true)
: QPushButton(parent)
, m_noButtonAllowed(true)
, m_requiresValidation(true)
{
qRegisterMetaType<Palapeli::InteractorTrigger>();
connect(this, SIGNAL(clicked()), SLOT(captureTrigger()));
setCheckable(true);
setDefaultText(i18n("Set Trigger..."), i18n("Click to change how an action is triggered"));
connect(this, SIGNAL(clicked()), SLOT(getTrigger()));
}
void Palapeli::MouseInputButton::setDefaultText(const QString& text, const QString& toolTip)
{
m_defaultText = text;
m_defaultToolTip = toolTip;
if (!isChecked())
{
setText(text);
setToolTip(toolTip);
}
setChecked(false);
setText(DefaultText);
setToolTip(DefaultToolTip);
}
bool Palapeli::MouseInputButton::isNoButtonAllowed() const
......@@ -54,18 +49,29 @@ void Palapeli::MouseInputButton::setNoButtonAllowed(bool noButtonAllowed)
m_noButtonAllowed = noButtonAllowed;
}
bool Palapeli::MouseInputButton::requiresValidation() const
{
return m_requiresValidation;
}
void Palapeli::MouseInputButton::setRequiresValidation(bool requiresValidation)
{
m_requiresValidation = requiresValidation;
}
Palapeli::InteractorTrigger Palapeli::MouseInputButton::trigger() const
{
return m_trigger;
}
void Palapeli::MouseInputButton::getTrigger()
void Palapeli::MouseInputButton::captureTrigger()
{
setChecked(true);
setText(i18n("Input here..."));
setToolTip(i18n("Hold down the modifier keys you want, then click a mouse button or scroll a mouse wheel here"));
}
void Palapeli::MouseInputButton::reset()
void Palapeli::MouseInputButton::clearTrigger()
{
setTrigger(Palapeli::InteractorTrigger());
}
......@@ -136,19 +142,34 @@ bool Palapeli::MouseInputButton::event(QEvent* event)
void Palapeli::MouseInputButton::setTrigger(const Palapeli::InteractorTrigger& trigger)
{
Palapeli::InteractorTrigger oldTrigger = m_trigger; //needed for triggerChanged signal
const bool needAnnouncement = !(m_trigger == trigger);
m_trigger = trigger;
//NOTE: Invalid triggers need not be confirmed (esp. calls to clearTrigger()).
if (m_requiresValidation && trigger.isValid() && trigger != m_trigger)
{
m_stagedTrigger = trigger;
emit triggerRequest(trigger);
}
else
applyTrigger(trigger);
}
void Palapeli::MouseInputButton::confirmTrigger(const Palapeli::InteractorTrigger& trigger)
{
if (m_stagedTrigger == trigger)
applyTrigger(m_stagedTrigger);
}
void Palapeli::MouseInputButton::applyTrigger(const Palapeli::InteractorTrigger& trigger)
{
const bool announceChange = m_trigger != trigger;
//apply new trigger
m_trigger = trigger;
m_stagedTrigger = Palapeli::InteractorTrigger();
setChecked(false);
setToolTip(m_defaultToolTip);
if (m_trigger.isValid())
setText(m_trigger.toString());
else
setText(m_defaultText);
setToolTip(DefaultToolTip);
setText(m_trigger.isValid() ? m_trigger.toString() : DefaultText);
//announce change
if (needAnnouncement)
emit triggerChanged(oldTrigger, trigger);
if (announceChange)
emit triggerChanged(trigger);
}
void Palapeli::MouseInputButton::showModifiers(Qt::KeyboardModifiers modifiers)
......
......@@ -32,26 +32,31 @@ namespace Palapeli
public:
MouseInputButton(QWidget* parent = 0);
void setDefaultText(const QString& text, const QString& toolTip);
bool isNoButtonAllowed() const;
void setNoButtonAllowed(bool noButtonAllowed);
///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;
void setTrigger(const Palapeli::InteractorTrigger& trigger);
Q_SIGNALS:
void triggerChanged(const Palapeli::InteractorTrigger& oldTrigger, const Palapeli::InteractorTrigger& newTrigger);
void triggerChanged(const Palapeli::InteractorTrigger& newTrigger);
void triggerRequest(const Palapeli::InteractorTrigger& newTrigger);
public Q_SLOTS:
void reset();
void captureTrigger();
void clearTrigger();
void confirmTrigger(const Palapeli::InteractorTrigger& trigger);
void setTrigger(const Palapeli::InteractorTrigger& trigger);
protected:
bool event(QEvent* event);
private Q_SLOTS:
void getTrigger();
private:
void applyTrigger(const Palapeli::InteractorTrigger& newTrigger);
void showModifiers(Qt::KeyboardModifiers modifiers);
Palapeli::InteractorTrigger m_trigger;
bool m_noButtonAllowed;
QString m_defaultText;
QString m_defaultToolTip;
static const QString DefaultText;
static const QString DefaultToolTip;
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().
bool m_noButtonAllowed, m_requiresValidation;
};
}
......
......@@ -76,6 +76,7 @@ namespace Palapeli
void setWheelDirection(Qt::Orientation orientation); ///< Setting this will reset button().
bool operator==(const Palapeli::InteractorTrigger& other) const;
inline bool operator!=(const Palapeli::InteractorTrigger& other) const;
private:
Qt::KeyboardModifiers m_modifiers;
Qt::MouseButton m_button;
......@@ -83,6 +84,11 @@ namespace Palapeli
};
}
bool Palapeli::InteractorTrigger::operator!=(const Palapeli::InteractorTrigger& other) const
{
return !(*this == other);
}
Q_DECLARE_METATYPE(Palapeli::InteractorTrigger)
#endif // PALAPELI_INTERACTORUTILS_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