Add hotkey settings

This commit is contained in:
Dan Paulat 2024-04-10 00:01:34 -05:00
parent 22e3101c22
commit 3da1d15470
7 changed files with 272 additions and 1 deletions

View file

@ -2,6 +2,7 @@
#include <scwx/qt/map/map_provider.hpp>
#include <scwx/qt/settings/audio_settings.hpp>
#include <scwx/qt/settings/general_settings.hpp>
#include <scwx/qt/settings/hotkey_settings.hpp>
#include <scwx/qt/settings/map_settings.hpp>
#include <scwx/qt/settings/palette_settings.hpp>
#include <scwx/qt/settings/product_settings.hpp>
@ -132,6 +133,7 @@ boost::json::value SettingsManager::Impl::ConvertSettingsToJson()
settings::GeneralSettings::Instance().WriteJson(settingsJson);
settings::AudioSettings::Instance().WriteJson(settingsJson);
settings::HotkeySettings::Instance().WriteJson(settingsJson);
settings::MapSettings::Instance().WriteJson(settingsJson);
settings::PaletteSettings::Instance().WriteJson(settingsJson);
settings::ProductSettings::Instance().WriteJson(settingsJson);
@ -147,6 +149,7 @@ void SettingsManager::Impl::GenerateDefaultSettings()
settings::GeneralSettings::Instance().SetDefaults();
settings::AudioSettings::Instance().SetDefaults();
settings::HotkeySettings::Instance().SetDefaults();
settings::MapSettings::Instance().SetDefaults();
settings::PaletteSettings::Instance().SetDefaults();
settings::ProductSettings::Instance().SetDefaults();
@ -163,6 +166,7 @@ bool SettingsManager::Impl::LoadSettings(
jsonDirty |= !settings::GeneralSettings::Instance().ReadJson(settingsJson);
jsonDirty |= !settings::AudioSettings::Instance().ReadJson(settingsJson);
jsonDirty |= !settings::HotkeySettings::Instance().ReadJson(settingsJson);
jsonDirty |= !settings::MapSettings::Instance().ReadJson(settingsJson);
jsonDirty |= !settings::PaletteSettings::Instance().ReadJson(settingsJson);
jsonDirty |= !settings::ProductSettings::Instance().ReadJson(settingsJson);

View file

@ -0,0 +1,117 @@
#include <scwx/qt/settings/hotkey_settings.hpp>
#include <QKeySequence>
namespace scwx
{
namespace qt
{
namespace settings
{
static const std::string logPrefix_ = "scwx::qt::settings::hotkey_settings";
static const std::unordered_map<types::Hotkey, QKeySequence> kDefaultHotkeys_ {
{types::Hotkey::ChangeMapStyle, QKeySequence {Qt::Key::Key_Z}},
{types::Hotkey::CopyCursorCoordinates,
QKeySequence {QKeyCombination {Qt::KeyboardModifier::ControlModifier,
Qt::Key::Key_C}}},
{types::Hotkey::CopyMapCoordinates,
QKeySequence {QKeyCombination {Qt::KeyboardModifier::ControlModifier |
Qt::KeyboardModifier::ShiftModifier,
Qt::Key::Key_C}}},
{types::Hotkey::MapPanUp, QKeySequence {Qt::Key::Key_W}},
{types::Hotkey::MapPanDown, QKeySequence {Qt::Key::Key_S}},
{types::Hotkey::MapPanLeft, QKeySequence {Qt::Key::Key_A}},
{types::Hotkey::MapPanRight, QKeySequence {Qt::Key::Key_D}},
{types::Hotkey::MapRotateClockwise, QKeySequence {Qt::Key::Key_E}},
{types::Hotkey::MapRotateCounterclockwise, QKeySequence {Qt::Key::Key_Q}},
{types::Hotkey::MapZoomIn, QKeySequence {Qt::Key::Key_Equal}},
{types::Hotkey::MapZoomOut, QKeySequence {Qt::Key::Key_Minus}},
{types::Hotkey::ProductTiltDecrease,
QKeySequence {Qt::Key::Key_BracketLeft}},
{types::Hotkey::ProductTiltIncrease,
QKeySequence {Qt::Key::Key_BracketRight}},
{types::Hotkey::Unknown, QKeySequence {}}};
static bool IsHotkeyValid(const std::string& value);
class HotkeySettings::Impl
{
public:
explicit Impl()
{
for (const auto& hotkey : types::HotkeyIterator())
{
const std::string& name = types::GetHotkeyShortName(hotkey);
const std::string defaultValue =
kDefaultHotkeys_.at(hotkey).toString().toStdString();
auto result =
hotkey_.emplace(hotkey, SettingsVariable<std::string> {name});
SettingsVariable<std::string>& settingsVariable = result.first->second;
settingsVariable.SetDefault(defaultValue);
settingsVariable.SetValidator(&IsHotkeyValid);
variables_.push_back(&settingsVariable);
}
// Add an empty hotkey (not part of registered variables) for error
// handling
hotkey_.emplace(types::Hotkey::Unknown,
SettingsVariable<std::string> {"?"});
}
~Impl() {}
std::unordered_map<types::Hotkey, SettingsVariable<std::string>> hotkey_ {};
std::vector<SettingsVariableBase*> variables_ {};
};
HotkeySettings::HotkeySettings() :
SettingsCategory("hotkeys"), p(std::make_unique<Impl>())
{
RegisterVariables(p->variables_);
SetDefaults();
p->variables_.clear();
}
HotkeySettings::~HotkeySettings() = default;
HotkeySettings::HotkeySettings(HotkeySettings&&) noexcept = default;
HotkeySettings& HotkeySettings::operator=(HotkeySettings&&) noexcept = default;
SettingsVariable<std::string>&
HotkeySettings::hotkey(scwx::qt::types::Hotkey hotkey) const
{
auto hotkeyVariable = p->hotkey_.find(hotkey);
if (hotkeyVariable == p->hotkey_.cend())
{
hotkeyVariable = p->hotkey_.find(types::Hotkey::Unknown);
}
return hotkeyVariable->second;
}
HotkeySettings& HotkeySettings::Instance()
{
static HotkeySettings hotkeySettings_;
return hotkeySettings_;
}
bool operator==(const HotkeySettings& lhs, const HotkeySettings& rhs)
{
return (lhs.p->hotkey_ == rhs.p->hotkey_);
}
static bool IsHotkeyValid(const std::string& value)
{
return QKeySequence {QString::fromStdString(value)}
.toString()
.toStdString() == value;
}
} // namespace settings
} // namespace qt
} // namespace scwx

View file

@ -0,0 +1,42 @@
#pragma once
#include <scwx/qt/settings/settings_category.hpp>
#include <scwx/qt/settings/settings_variable.hpp>
#include <scwx/qt/types/hotkey_types.hpp>
#include <memory>
#include <string>
namespace scwx
{
namespace qt
{
namespace settings
{
class HotkeySettings : public SettingsCategory
{
public:
explicit HotkeySettings();
~HotkeySettings();
HotkeySettings(const HotkeySettings&) = delete;
HotkeySettings& operator=(const HotkeySettings&) = delete;
HotkeySettings(HotkeySettings&&) noexcept;
HotkeySettings& operator=(HotkeySettings&&) noexcept;
SettingsVariable<std::string>& hotkey(scwx::qt::types::Hotkey hotkey) const;
static HotkeySettings& Instance();
friend bool operator==(const HotkeySettings& lhs, const HotkeySettings& rhs);
private:
class Impl;
std::unique_ptr<Impl> p;
};
} // namespace settings
} // namespace qt
} // namespace scwx

View file

@ -0,0 +1,62 @@
#include <scwx/qt/types/hotkey_types.hpp>
#include <scwx/util/enum.hpp>
#include <unordered_map>
#include <boost/algorithm/string.hpp>
namespace scwx
{
namespace qt
{
namespace types
{
static const std::unordered_map<Hotkey, std::string> hotkeyShortName_ {
{Hotkey::ChangeMapStyle, "change_map_style"},
{Hotkey::CopyCursorCoordinates, "copy_cursor_coordinates"},
{Hotkey::CopyMapCoordinates, "copy_map_coordinates"},
{Hotkey::MapPanUp, "map_pan_up"},
{Hotkey::MapPanDown, "map_pan_down"},
{Hotkey::MapPanLeft, "map_pan_left"},
{Hotkey::MapPanRight, "map_pan_right"},
{Hotkey::MapRotateClockwise, "map_rotate_clockwise"},
{Hotkey::MapRotateCounterclockwise, "map_rotate_counterclockwise"},
{Hotkey::MapZoomIn, "map_zoom_in"},
{Hotkey::MapZoomOut, "map_zoom_out"},
{Hotkey::ProductTiltDecrease, "product_tilt_decrease"},
{Hotkey::ProductTiltIncrease, "product_tilt_increase"},
{Hotkey::Unknown, "?"}};
static const std::unordered_map<Hotkey, std::string> hotkeyLongName_ {
{Hotkey::ChangeMapStyle, "Change Map Style"},
{Hotkey::CopyCursorCoordinates, "Copy Cursor Coordinates"},
{Hotkey::CopyMapCoordinates, "Copy Map Coordinates"},
{Hotkey::MapPanUp, "Map Pan Up"},
{Hotkey::MapPanDown, "Map Pan Down"},
{Hotkey::MapPanLeft, "Map Pan Left"},
{Hotkey::MapPanRight, "Map Pan Right"},
{Hotkey::MapRotateClockwise, "Map Rotate Clockwise"},
{Hotkey::MapRotateCounterclockwise, "Map Rotate Counterclockwise"},
{Hotkey::MapZoomIn, "Map Zoom In"},
{Hotkey::MapZoomOut, "Map Zoom Out"},
{Hotkey::ProductTiltDecrease, "Product Tilt Decrease"},
{Hotkey::ProductTiltIncrease, "Product Tilt Increase"},
{Hotkey::Unknown, "?"}};
SCWX_GET_ENUM(Hotkey, GetHotkeyFromShortName, hotkeyShortName_)
SCWX_GET_ENUM(Hotkey, GetHotkeyFromLongName, hotkeyLongName_)
const std::string& GetHotkeyShortName(Hotkey hotkey)
{
return hotkeyShortName_.at(hotkey);
}
const std::string& GetHotkeyLongName(Hotkey hotkey)
{
return hotkeyLongName_.at(hotkey);
}
} // namespace types
} // namespace qt
} // namespace scwx

View file

@ -0,0 +1,42 @@
#pragma once
#include <scwx/util/iterator.hpp>
#include <string>
namespace scwx
{
namespace qt
{
namespace types
{
enum class Hotkey
{
ChangeMapStyle,
CopyCursorCoordinates,
CopyMapCoordinates,
MapPanUp,
MapPanDown,
MapPanLeft,
MapPanRight,
MapRotateClockwise,
MapRotateCounterclockwise,
MapZoomIn,
MapZoomOut,
ProductTiltDecrease,
ProductTiltIncrease,
Unknown
};
typedef scwx::util::
Iterator<Hotkey, Hotkey::ChangeMapStyle, Hotkey::ProductTiltIncrease>
HotkeyIterator;
Hotkey GetHotkeyFromShortName(const std::string& name);
Hotkey GetHotkeyFromLongName(const std::string& name);
const std::string& GetHotkeyShortName(Hotkey hotkey);
const std::string& GetHotkeyLongName(Hotkey hotkey);
} // namespace types
} // namespace qt
} // namespace scwx