Commit cf82437b authored by Stefan Majewsky's avatar Stefan Majewsky

Add a clear button to the mouse input button, in anticipation of a better configuration UI.

svn path=/trunk/KDE/kdegames/palapeli/; revision=1113449
parent 12ff5ca7
......@@ -18,27 +18,59 @@
***************************************************************************/
#include "mouseinputbutton.h"
#include "mouseinputbutton_p.h"
#include <QApplication>
#include <QKeyEvent>
#include <QHBoxLayout>
#include <QStyleOptionButton>
#include <KIcon>
#include <KLocalizedString>
const QString Palapeli::MouseInputButton::DefaultText = i18n("Set Trigger...");
const QString Palapeli::MouseInputButton::DefaultToolTip = i18n("Click to change how an action is triggered");
static QIcon clearIcon()
{
if (QApplication::isLeftToRight())
return KIcon("edit-clear-locationbar-rtl");
else
return KIcon("edit-clear-locationbar-ltr");
}
Palapeli::MouseInputButton::MouseInputButton(QWidget* parent)
: QPushButton(parent)
, m_iconLabel(new QLabel)
, m_mainLabel(new QLabel)
, m_clearButton(new Palapeli::FlatButton(clearIcon()))
, m_noButtonAllowed(true)
, m_requiresValidation(false)
{
qRegisterMetaType<Palapeli::InteractorTrigger>();
connect(this, SIGNAL(clicked()), SLOT(captureTrigger()));
connect(m_clearButton, SIGNAL(clicked()), SLOT(clearTrigger()));
setCheckable(true);
setChecked(false);
setIcon(KIcon("input-mouse"));
setText(DefaultText);
setToolTip(DefaultToolTip);
m_iconLabel->setPixmap(KIcon("input-mouse").pixmap(22)); //TODO: respect global icon size configuration
//setup child widgets
m_iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_mainLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
m_mainLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
m_clearButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_clearButton->setToolTip(i18n("Remove this trigger"));
//setup layout
QHBoxLayout* layout = new QHBoxLayout;
setLayout(layout);
layout->setMargin(0);
layout->addWidget(m_iconLabel);
layout->addWidget(m_mainLabel);
layout->addWidget(m_clearButton);
updateAppearance();
//calculate margin for layout from QStyle::sizeFromContents
QStyleOptionButton opt;
QPushButton::initStyleOption(&opt);
const QSize bareSizeHint = layout->sizeHint();
const QSize fullSizeHint = style()->sizeFromContents(QStyle::CT_PushButton, &opt, bareSizeHint, this).expandedTo(QApplication::globalStrut());
const int marginX = (fullSizeHint.width() - bareSizeHint.width()) / 2;
const int marginY = (fullSizeHint.height() - bareSizeHint.height()) / 2;
layout->setContentsMargins(marginX, marginY, marginX, marginY);
}
bool Palapeli::MouseInputButton::isNoButtonAllowed() const
......@@ -69,7 +101,8 @@ Palapeli::InteractorTrigger Palapeli::MouseInputButton::trigger() const
void Palapeli::MouseInputButton::captureTrigger()
{
setChecked(true);
setText(i18n("Input here..."));
m_clearButton->setVisible(false); //while capture is in progress
m_mainLabel->setText(i18n("Input here..."));
setToolTip(i18n("Hold down the modifier keys you want, then click a mouse button or scroll a mouse wheel here"));
setFocus(Qt::MouseFocusReason);
}
......@@ -161,15 +194,27 @@ void Palapeli::MouseInputButton::confirmTrigger(const Palapeli::InteractorTrigge
applyTrigger(m_stagedTrigger);
}
void Palapeli::MouseInputButton::updateAppearance()
{
//find caption
static const QString noTriggerString = i18nc("This is used for describing that no mouse action has been assigned to this interaction plugin.", "None");
QString text = m_trigger.toString();
if (!m_trigger.isValid())
text = text.arg(noTriggerString);
//apply properties
setChecked(false);
setToolTip(DefaultToolTip);
m_mainLabel->setText(text);
m_clearButton->setVisible(m_trigger.isValid());
}
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(DefaultToolTip);
setText(m_trigger.isValid() ? m_trigger.toString() : DefaultText);
updateAppearance();
//announce change
if (announceChange)
emit triggerChanged(trigger);
......@@ -179,7 +224,8 @@ void Palapeli::MouseInputButton::showModifiers(Qt::KeyboardModifiers modifiers)
{
Palapeli::InteractorTrigger dummyTrigger;
dummyTrigger.setModifiers(modifiers);
setText(dummyTrigger.toString().arg(i18n("Input here...")));
m_mainLabel->setText(dummyTrigger.toString().arg(i18n("Input here...")));
}
#include "mouseinputbutton.moc"
#include "mouseinputbutton_p.moc"
......@@ -22,7 +22,9 @@
#include "../engine/interactorutils.h"
class QLabel;
#include <QPushButton>
class QToolButton;
namespace Palapeli
{
......@@ -34,6 +36,8 @@ namespace Palapeli
bool isNoButtonAllowed() const;
void setNoButtonAllowed(bool noButtonAllowed);
bool showClearButton() const;
void setShowClearButton(bool showClearButton);
///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);
......@@ -49,12 +53,16 @@ namespace Palapeli
protected:
bool event(QEvent* event);
private:
void updateAppearance();
void applyTrigger(const Palapeli::InteractorTrigger& newTrigger);
void showModifiers(Qt::KeyboardModifiers modifiers);
static const QString DefaultText;
static const QString DefaultToolTip;
QLabel* m_iconLabel;
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().
bool m_noButtonAllowed, m_requiresValidation;
};
......
/***************************************************************************
* 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_P_H
#define PALAPELI_MOUSEINPUTBUTTON_P_H
#include <QLabel>
#include <QMouseEvent>
namespace Palapeli
{
class FlatButton : public QLabel
{
Q_OBJECT
public:
FlatButton(const QIcon& icon, QWidget* parent = 0) : QLabel(parent), m_icon(icon)
{
leaveEvent(0); //apply icon
setMouseTracking(true);
}
Q_SIGNALS:
void clicked();
protected:
virtual void enterEvent(QEvent*)
{
//TODO: respect global icon size configuration
setPixmap(m_icon.pixmap(22, QIcon::Active));
}
virtual void leaveEvent(QEvent*)
{
setPixmap(m_icon.pixmap(22, QIcon::Normal));
}
virtual void mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton)
event->accept();
else
QLabel::mousePressEvent(event);
}
virtual void mouseReleaseEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton)
{
event->accept();
if (rect().contains(event->pos()))
emit clicked();
}
else
QLabel::mouseReleaseEvent(event);
}
private:
QIcon m_icon;
};
}
#endif // PALAPELI_MOUSEINPUTBUTTON_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