Add modifier setting.

pull/29/head
Aesylwinn 9 years ago
parent b204758be1
commit 12db6d2114

@ -113,7 +113,7 @@ opencs_units (view/prefs
opencs_units (model/prefs
state setting intsetting doublesetting boolsetting enumsetting coloursetting shortcut
shortcuteventhandler shortcutmanager shortcutsetting
shortcuteventhandler shortcutmanager shortcutsetting modifiersetting
)
opencs_units_noqt (model/prefs

@ -0,0 +1,125 @@
#include "modifiersetting.hpp"
#include <QEvent>
#include <QKeyEvent>
#include <QLabel>
#include <QLineEdit>
#include <QMouseEvent>
#include <QPushButton>
#include <QWidget>
#include "state.hpp"
#include "shortcutmanager.hpp"
namespace CSMPrefs
{
ModifierSetting::ModifierSetting(Category* parent, Settings::Manager* values, QMutex* mutex, const std::string& key,
const std::string& label)
: Setting(parent, values, mutex, key, label)
, mEditorActive(false)
{
}
std::pair<QWidget*, QWidget*> ModifierSetting::makeWidgets(QWidget* parent)
{
QKeySequence sequence;
int modifier = 0;
State::get().getShortcutManager().getSequence(getKey(), sequence, modifier);
QString text = QString::fromUtf8(State::get().getShortcutManager().convertToString(modifier).c_str());
QLabel* label = new QLabel(QString::fromUtf8(getLabel().c_str()), parent);
QPushButton* widget = new QPushButton(text, parent);
widget->setCheckable(true);
widget->installEventFilter(this);
mButton = widget;
connect(widget, SIGNAL(toggled(bool)), this, SLOT(buttonToggled(bool)));
return std::make_pair(label, widget);
}
bool ModifierSetting::eventFilter(QObject* target, QEvent* event)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->isAutoRepeat())
return true;
int mod = keyEvent->modifiers();
int key = keyEvent->key();
return handleEvent(target, mod, key);
}
else if (event->type() == QEvent::MouseButtonPress)
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
int mod = mouseEvent->modifiers();
int button = mouseEvent->button();
return handleEvent(target, mod, button);
}
return false;
}
bool ModifierSetting::handleEvent(QObject* target, int mod, int value)
{
// For potential future exceptions
const int Blacklist[] =
{
0
};
const size_t BlacklistSize = sizeof(Blacklist) / sizeof(int);
if (!mEditorActive)
return false;
// Handle blacklist
for (size_t i = 0; i < BlacklistSize; ++i)
{
if (value == Blacklist[i])
return true;
}
// Update modifier
QKeySequence sequence;
int modifier = 0;
State::get().getShortcutManager().getSequence(getKey(), sequence, modifier);
modifier = value;
State::get().getShortcutManager().setSequence(getKey(), sequence, modifier);
// Store
{
std::string value = State::get().getShortcutManager().convertToString(sequence, modifier);
QMutexLocker lock(getMutex());
getValues().setString(getKey(), getParent()->getKey(), value);
}
getParent()->getState()->update(*this);
// Update button
QString text = QString::fromUtf8(State::get().getShortcutManager().convertToString(modifier).c_str());
mButton->setText(text);
mButton->setChecked(false);
mEditorActive = false;
return true;
}
void ModifierSetting::buttonToggled(bool checked)
{
if (checked)
mButton->setText("Press keys or click here...");
mEditorActive = checked;
}
}

@ -0,0 +1,41 @@
#ifndef CSM_PREFS_MODIFIERSETTING_H
#define CSM_PREFS_MODIFIERSETTING_H
#include <QKeySequence>
#include "setting.hpp"
class QEvent;
class QPushButton;
namespace CSMPrefs
{
class ModifierSetting : public Setting
{
Q_OBJECT
public:
ModifierSetting(Category* parent, Settings::Manager* values, QMutex* mutex, const std::string& key,
const std::string& label);
virtual std::pair<QWidget*, QWidget*> makeWidgets(QWidget* parent);
protected:
bool eventFilter(QObject* target, QEvent* event);
private:
bool handleEvent(QObject* target, int mod, int value);
QPushButton* mButton;
bool mEditorActive;
private slots:
void buttonToggled(bool checked);
};
}
#endif

@ -14,9 +14,8 @@
namespace CSMPrefs
{
ShortcutSetting::ShortcutSetting(Category* parent, Settings::Manager* values, QMutex* mutex, const std::string& key,
const std::string& label, const QKeySequence& default_)
const std::string& label)
: Setting(parent, values, mutex, key, label)
, mDefault(default_)
, mEditorActive(false)
, mEditorPos(0)
{
@ -30,7 +29,11 @@ namespace CSMPrefs
std::pair<QWidget*, QWidget*> ShortcutSetting::makeWidgets(QWidget* parent)
{
QString text = QString::fromUtf8(State::get().getShortcutManager().convertToString(mDefault).c_str());
QKeySequence sequence;
int modifier = 0;
State::get().getShortcutManager().getSequence(getKey(), sequence, modifier);
QString text = QString::fromUtf8(State::get().getShortcutManager().convertToString(sequence).c_str());
QLabel* label = new QLabel(QString::fromUtf8(getLabel().c_str()), parent);
QPushButton* widget = new QPushButton(text, parent);

@ -17,7 +17,7 @@ namespace CSMPrefs
public:
ShortcutSetting(Category* parent, Settings::Manager* values, QMutex* mutex, const std::string& key,
const std::string& label, const QKeySequence& default_);
const std::string& label);
virtual std::pair<QWidget*, QWidget*> makeWidgets(QWidget* parent);
@ -29,8 +29,6 @@ namespace CSMPrefs
bool handleEvent(QObject* target, int mod, int value, bool active);
QKeySequence mDefault;
QPushButton* mButton;
bool mEditorActive;

@ -12,6 +12,7 @@
#include "boolsetting.hpp"
#include "coloursetting.hpp"
#include "shortcutsetting.hpp"
#include "modifiersetting.hpp"
CSMPrefs::State *CSMPrefs::State::sThis = 0;
@ -292,6 +293,7 @@ void CSMPrefs::State::declare()
declareShortcut ("free-backward", "Free camera backward", QKeySequence(Qt::Key_S));
declareShortcut ("free-left", "Free camera left", QKeySequence(Qt::Key_A));
declareShortcut ("free-right", "Free camera right", QKeySequence(Qt::Key_D));
declareModifier ("free-forward", "Free camera speed modifier");
declareShortcut ("free-roll-left", "Free camera roll left", QKeySequence(Qt::Key_Q));
declareShortcut ("free-roll-right", "Free camera roll right", QKeySequence(Qt::Key_E));
declareShortcut ("free-speed-mode", "Free camera speed mode toggle", QKeySequence(Qt::Key_F));
@ -301,6 +303,7 @@ void CSMPrefs::State::declare()
declareShortcut ("orbit-down", "Orbit camera down", QKeySequence(Qt::Key_S));
declareShortcut ("orbit-left", "Orbit camera left", QKeySequence(Qt::Key_A));
declareShortcut ("orbit-right", "Orbit camera right", QKeySequence(Qt::Key_D));
declareModifier ("orbit-up", "Orbit camera speed modifier");
declareShortcut ("orbit-roll-left", "Orbit camera roll left", QKeySequence(Qt::Key_Q));
declareShortcut ("orbit-roll-right", "Orbit camera roll right", QKeySequence(Qt::Key_E));
declareShortcut ("orbit-speed-mode", "Orbit camera speed mode toggle", QKeySequence(Qt::Key_F));
@ -452,7 +455,19 @@ CSMPrefs::ShortcutSetting& CSMPrefs::State::declareShortcut (const std::string&
getShortcutManager().setSequence(key, sequence, mod);
CSMPrefs::ShortcutSetting *setting = new CSMPrefs::ShortcutSetting (&mCurrentCategory->second, &mSettings, &mMutex,
key, label, sequence);
key, label);
mCurrentCategory->second.addSetting (setting);
return *setting;
}
CSMPrefs::ModifierSetting& CSMPrefs::State::declareModifier(const std::string& key, const std::string& label)
{
if (mCurrentCategory==mCategories.end())
throw std::logic_error ("no category for setting");
CSMPrefs::ModifierSetting *setting = new CSMPrefs::ModifierSetting (&mCurrentCategory->second, &mSettings, &mMutex,
key, label);
mCurrentCategory->second.addSetting (setting);
return *setting;

@ -27,6 +27,7 @@ namespace CSMPrefs
class BoolSetting;
class ColourSetting;
class ShortcutSetting;
class ModifierSetting;
/// \brief User settings state
///
@ -77,6 +78,8 @@ namespace CSMPrefs
ShortcutSetting& declareShortcut (const std::string& key, const std::string& label,
const QKeySequence& default_, int modifier_=0);
ModifierSetting& declareModifier(const std::string& key, const std::string& label);
void declareSeparator();
void setDefault (const std::string& key, const std::string& default_);

Loading…
Cancel
Save