12#include <QJsonDocument>
28ShortcutManager::~ShortcutManager() =
default;
36 mainWidget_ = mainWidget;
42 for (
const auto& [
id, action] : actions_)
54 if (action.
id.isEmpty())
58 if (actions_.count(action.
id) > 0)
63 if (!conflict.isEmpty())
68 actions_[action.
id] = modifiedAction;
72 actions_[action.
id] = action;
78 createQShortcut(action.
id);
86 removeQShortcut(actionId);
87 actions_.erase(actionId);
92 return actions_.count(actionId) > 0;
97 auto it = actions_.find(actionId);
98 if (it != actions_.end())
105 std::vector<ShortcutAction> result;
106 result.reserve(actions_.size());
107 for (
const auto& [
id, action] : actions_)
109 result.push_back(action);
116 std::vector<ShortcutAction> result;
117 for (
const auto& [
id, action] : actions_)
119 if (action.category == category)
120 result.push_back(action);
127 QStringList categories;
128 for (
const auto& [
id, action] : actions_)
130 if (!categories.contains(action.category))
131 categories.append(action.category);
143 auto it = actions_.find(actionId);
144 if (it == actions_.end())
148 if (!shortcut.isEmpty())
151 if (!conflict.isEmpty())
156 updateQShortcut(actionId);
164 auto it = actions_.find(actionId);
165 if (it == actions_.end())
168 it->second.resetToDefault();
169 updateQShortcut(actionId);
176 for (
auto& [
id, action] : actions_)
178 action.resetToDefault();
186 const QString& excludeActionId)
const
188 if (shortcut.isEmpty())
191 for (
const auto& [
id, action] : actions_)
193 if (
id == excludeActionId)
196 if (action.currentShortcut == shortcut)
209 auto it = actions_.find(actionId);
210 if (it == actions_.end())
213 it->second.isEnabled = enabled;
215 auto shortcutIt = shortcuts_.find(actionId);
216 if (shortcutIt != shortcuts_.end())
218 shortcutIt->second->setEnabled(enabled && allEnabled_);
224 for (
auto& [
id, action] : actions_)
226 if (action.category == category)
228 action.isEnabled = enabled;
230 auto shortcutIt = shortcuts_.find(
id);
231 if (shortcutIt != shortcuts_.end())
233 shortcutIt->second->setEnabled(enabled && allEnabled_);
241 allEnabled_ = enabled;
243 for (
auto& [
id, shortcut] : shortcuts_)
245 auto it = actions_.find(
id);
246 if (it != actions_.end())
248 shortcut->setEnabled(enabled && it->second.isEnabled);
260 settings.beginGroup(
"Shortcuts");
262 for (
const auto& [
id, action] : actions_)
264 if (action.isCustomized())
266 settings.setValue(
id, action.currentShortcut.toString());
280 settings.beginGroup(
"Shortcuts");
282 for (
auto& [
id, action] : actions_)
284 if (settings.contains(
id))
286 QString seqStr = settings.value(
id).toString();
287 QKeySequence seq(seqStr);
290 action.currentShortcut = seq;
301 QJsonArray actionsArray;
303 for (
const auto& [
id, action] : actions_)
306 obj[
"id"] = action.id;
307 obj[
"label"] = action.label;
308 obj[
"category"] = action.category;
309 obj[
"defaultShortcut"] = action.defaultShortcut.toString();
310 obj[
"currentShortcut"] = action.currentShortcut.toString();
311 obj[
"isCustomized"] = action.isCustomized();
312 actionsArray.append(obj);
315 root[
"shortcuts"] = actionsArray;
318 QFile file(filePath);
319 if (!file.open(QIODevice::WriteOnly))
322 file.write(QJsonDocument(root).toJson(QJsonDocument::Indented));
328 QFile file(filePath);
329 if (!file.open(QIODevice::ReadOnly))
332 QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
336 QJsonObject root = doc.object();
337 QJsonArray actionsArray = root[
"shortcuts"].toArray();
344 for (
const auto& val : actionsArray)
346 QJsonObject obj = val.toObject();
347 QString
id = obj[
"id"].toString();
348 QString shortcutStr = obj[
"currentShortcut"].toString();
352 QKeySequence seq(shortcutStr);
377 return seq.toString(QKeySequence::NativeText);
382 auto it = actions_.find(actionId);
383 if (it == actions_.end())
407void ShortcutManager::createQShortcut(
const QString& actionId)
412 auto it = actions_.find(actionId);
413 if (it == actions_.end())
425 action.
isGlobal ? Qt::ApplicationShortcut : Qt::WindowShortcut);
427 shortcut->setEnabled(action.
isEnabled && allEnabled_);
430 connect(shortcut.get(), &QShortcut::activated,
this,
431 [
this, actionId]() { triggerAction(actionId); });
433 shortcuts_[actionId] = std::move(shortcut);
436void ShortcutManager::updateQShortcut(
const QString& actionId)
438 auto it = actions_.find(actionId);
439 if (it == actions_.end())
442 const ShortcutAction& action = it->second;
445 removeQShortcut(actionId);
450 createQShortcut(actionId);
454void ShortcutManager::removeQShortcut(
const QString& actionId)
456 shortcuts_.erase(actionId);
bool triggerAction(const QString &actionId)
Trigger an action by ID.
void saveCustomizations()
Save custom shortcuts to settings.
QString checkConflict(const QKeySequence &shortcut, const QString &excludeActionId=QString()) const
Check if a shortcut conflicts with existing ones.
bool registerAction(const ShortcutAction &action)
Register a new shortcut action.
bool setShortcut(const QString &actionId, const QKeySequence &shortcut)
Set a custom shortcut for an action.
ShortcutManager(const ShortcutManager &)=delete
std::vector< ShortcutAction > getAllActions() const
Get all registered actions.
void resetAllToDefaults()
Reset all shortcuts to defaults.
void shortcutsReloaded()
Emitted when shortcuts are reloaded.
const ShortcutAction * getAction(const QString &actionId) const
Get an action by ID.
void setCategoryEnabled(const QString &category, bool enabled)
Enable or disable all actions in a category.
static ShortcutManager & instance()
Get singleton instance.
static QString keySequenceToString(const QKeySequence &seq)
Get display string for a key sequence.
void setAllEnabled(bool enabled)
Enable or disable all shortcuts (useful when dialogs are open).
void resetToDefault(const QString &actionId)
Reset an action to its default shortcut.
QStringList getCategories() const
Get all categories.
bool hasAction(const QString &actionId) const
Check if an action is registered.
std::vector< ShortcutAction > getActionsByCategory(const QString &category) const
Get actions by category.
void setActionEnabled(const QString &actionId, bool enabled)
Enable or disable an action.
void loadCustomizations()
Load custom shortcuts from settings.
void unregisterAction(const QString &actionId)
Unregister a shortcut action.
void initialize(QWidget *mainWidget)
Initialize manager with main widget for shortcut context.
int importFromFile(const QString &filePath)
Import shortcuts from JSON file.
bool exportToFile(const QString &filePath) const
Export shortcuts to JSON file.
void shortcutChanged(const QString &actionId, const QKeySequence &newShortcut)
Emitted when a shortcut is changed.
Represents a single keyboard shortcut action.
QKeySequence currentShortcut
Current (possibly customized) key sequence.
bool isGlobal
Whether this is a global (application-wide) shortcut.
QString id
Unique identifier (e.g., "game.launch").
std::function< bool()> canExecute
Optional: returns false to disable temporarily.
bool isEnabled
Whether this shortcut is currently enabled.
std::function< void()> action
Action to execute.