Commit a010ab4f authored by Stefan Majewsky's avatar Stefan Majewsky

Copy mouseinputbutton.{h,cpp} from kdebase/workspace/libs/plasmagenericshell...

Copy mouseinputbutton.{h,cpp} from kdebase/workspace/libs/plasmagenericshell and port it to my coding style and Palapeli::InteractorTrigger.

svn path=/trunk/KDE/kdegames/palapeli/; revision=1112609
parent 7947c3d9
......@@ -2,6 +2,7 @@
set_directory_properties(PROPERTIES COMPILE_DEFINITIONS USE_LOCAL_LIBPALA)
set(palapeli_SRCS
config/mouseinputbutton.cpp
creator/propertywidget.cpp
creator/puzzlecreator.cpp
creator/slicerconfwidget.cpp
......
/***************************************************************************
* Copyright 2009 Chani Armitage <chani@kde.org>
* 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.
***************************************************************************/
#include "mouseinputbutton.h"
#include <QApplication>
#include <QKeyEvent>
#include <KLocalizedString>
Palapeli::MouseInputButton::MouseInputButton(QWidget* parent)
: QPushButton(parent)
, m_noButtonAllowed(true)
{
qRegisterMetaType<Palapeli::InteractorTrigger>();
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);
}
}
bool Palapeli::MouseInputButton::isNoButtonAllowed() const
{
return m_noButtonAllowed;
}
void Palapeli::MouseInputButton::setNoButtonAllowed(bool noButtonAllowed)
{
m_noButtonAllowed = noButtonAllowed;
}
Palapeli::InteractorTrigger Palapeli::MouseInputButton::trigger() const
{
return m_trigger;
}
void Palapeli::MouseInputButton::getTrigger()
{
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()
{
setTrigger(Palapeli::InteractorTrigger());
}
bool Palapeli::MouseInputButton::event(QEvent* event)
{
const QWheelEvent* wEvent = static_cast<QWheelEvent*>(event);
const QMouseEvent* mEvent = static_cast<QMouseEvent*>(event);
const QKeyEvent* kEvent = static_cast<QKeyEvent*>(event);
if (isChecked())
{
//got a trigger or cancel
switch ((int) event->type())
{
case QEvent::Wheel: {
Palapeli::InteractorTrigger newTrigger;
newTrigger.setModifiers(wEvent->modifiers());
newTrigger.setWheelDirection(wEvent->orientation());
setTrigger(newTrigger);
event->accept();
return true;
}
case QEvent::MouseButtonRelease: {
Palapeli::InteractorTrigger newTrigger;
newTrigger.setModifiers(mEvent->modifiers());
newTrigger.setButton(mEvent->button());
setTrigger(newTrigger);
event->accept();
return true;
}
case QEvent::MouseButtonPress:
event->accept();
return true;
case QEvent::KeyPress: {
if (kEvent->key() == Qt::Key_Escape)
{
//cancel
setTrigger(m_trigger);
event->accept();
return true;
}
if (kEvent->key() == Qt::Key_Space && m_noButtonAllowed)
{
//create trigger with NoButton (TODO: make this functionality more user-visible)
Palapeli::InteractorTrigger newTrigger;
newTrigger.setModifiers(kEvent->modifiers());
newTrigger.setButton(Qt::NoButton);
setTrigger(newTrigger);
event->accept();
return true;
}
} //fall through
case QEvent::KeyRelease:
showModifiers(kEvent->modifiers());
break;
}
}
bool ret = QPushButton::event(event);
if (event->type() == QEvent::MouseButtonRelease)
{
//fake a tooltip event
//because otherwise they go away when you click and don't come back until you move the mouse
QHelpEvent tip(QEvent::ToolTip, mEvent->pos(), mEvent->globalPos());
QApplication::sendEvent(this, &tip);
}
return ret;
}
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;
//apply new trigger
setChecked(false);
setToolTip(m_defaultToolTip);
if (m_trigger.isValid())
setText(m_trigger.toString());
else
setText(m_defaultText);
//announce change
if (needAnnouncement)
emit triggerChanged(oldTrigger, trigger);
}
void Palapeli::MouseInputButton::showModifiers(Qt::KeyboardModifiers modifiers)
{
Palapeli::InteractorTrigger dummyTrigger;
dummyTrigger.setModifiers(modifiers);
setText(dummyTrigger.toString().arg(i18n("Input here...")));
}
#include "mouseinputbutton.moc"
/***************************************************************************
* Copyright 2009 Chani Armitage <chani@kde.org>
* 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_MOUSEINPUTBUTTON_H
#define PALAPELI_MOUSEINPUTBUTTON_H
#include "../engine/interactorutils.h"
#include <QPushButton>
namespace Palapeli
{
class MouseInputButton : public QPushButton
{
Q_OBJECT
public:
MouseInputButton(QWidget* parent = 0);
void setDefaultText(const QString& text, const QString& toolTip);
bool isNoButtonAllowed() const;
void setNoButtonAllowed(bool noButtonAllowed);
Palapeli::InteractorTrigger trigger() const;
void setTrigger(const Palapeli::InteractorTrigger& trigger);
Q_SIGNALS:
void triggerChanged(const Palapeli::InteractorTrigger& oldTrigger, const Palapeli::InteractorTrigger& newTrigger);
public Q_SLOTS:
void reset();
protected:
bool event(QEvent* event);
private Q_SLOTS:
void getTrigger();
private:
void showModifiers(Qt::KeyboardModifiers modifiers);
Palapeli::InteractorTrigger m_trigger;
bool m_noButtonAllowed;
QString m_defaultText;
QString m_defaultToolTip;
};
}
#endif // PALAPELI_MOUSEINPUTBUTTON_H
......@@ -74,6 +74,7 @@ struct Palapeli::InteractorTrigger::Data
m_modifierStrings[Qt::GroupSwitchModifier] = i18nc("a special keyboard modifier", "GroupSwitch");
m_buttonStrings[Qt::NoButton] = i18nc("refers to no mouse buttons being pressed", "No-Button");
//FIXME: Left/right may be wrong if mouse buttons are swapped.
m_buttonStrings[(Qt::MouseButton) -1] = QLatin1String("%1"); //allow external users to insert their custom strings into here
m_buttonStrings[Qt::LeftButton] = i18nc("a mouse button", "Left-Button");
m_buttonStrings[Qt::RightButton] = i18nc("a mouse button", "Right-Button");
m_buttonStrings[Qt::MidButton] = i18nc("a mouse button", "Middle-Button");
......@@ -91,6 +92,7 @@ K_GLOBAL_STATIC_WITH_ARGS(Palapeli::InteractorTrigger::Data, itPrettyData, (fals
//BEGIN Palapeli::InteractorTrigger
Palapeli::InteractorTrigger::InteractorTrigger()
: m_button((Qt::MouseButton) -1)
{
}
......@@ -138,8 +140,18 @@ QPair<QString, QStringList> Palapeli::InteractorTrigger::toStringGeneric(const P
return qMakePair(actionString, modifierStrings);
}
bool Palapeli::InteractorTrigger::isValid() const
{
if (m_wheelDirection > 0 && m_button > 0)
return false; //do not allow wheel and mouse triggers at the same time
else
return m_button >= 0; //do not allow negative m_button values
}
QString Palapeli::InteractorTrigger::serialized() const
{
if (!isValid())
return QString();
QPair<QString, QStringList> bits = toStringGeneric(*itParserData);
if (bits.second.isEmpty())
bits.second << QLatin1String("NoModifier");
......
......@@ -20,6 +20,7 @@
#define PALAPELI_INTERACTORUTILS_H
class QGraphicsView;
#include <QMetaType>
#include <QPair>
#include <QPointF>
......@@ -47,12 +48,14 @@ namespace Palapeli
struct InteractorTrigger
{
public:
///Constructs a trigger which corresponds to no modifier + no button.
///Constructs an invalid trigger. (The mouse button is set to -1.)
InteractorTrigger();
///Constructs a trigger from the given serialization. If the parsing fails, this constructor returns the same as the default constructor.
///Constructs a trigger from the given serialization. If the parsing fails, this constructor returns an invalid trigger (just like the default constructor).
///Possible serializations include "MidButton;NoModifier", "RightButton;ShiftModifier" and "wheel:Horizontal;ShiftModifier|ControlModifier". (A formal specification of the format is left as an exercise to the reader.)
InteractorTrigger(const QString& serialization); //krazy:exclude=explicit (I want implicit conversions)
///Returns whether this triger is valid.
bool isValid() const;
///Returns the serialization for this trigger, or an empty string if this trigger is invalid.
///\see isValid()
QString serialized() const;
......@@ -80,4 +83,6 @@ namespace Palapeli
};
}
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