git repos / SagoImageBrowser

blame: src/shortcutmanager.cpp

normal view · raw

83f0e0bc Poul Sander 2026-05-30 17:59 1
#include "shortcutmanager.h"
83f0e0bc Poul Sander 2026-05-30 17:59 2
83f0e0bc Poul Sander 2026-05-30 17:59 3
#include <QKeyEvent>
83f0e0bc Poul Sander 2026-05-30 17:59 4
#include <QSettings>
83f0e0bc Poul Sander 2026-05-30 17:59 5
83f0e0bc Poul Sander 2026-05-30 17:59 6
namespace {
83f0e0bc Poul Sander 2026-05-30 17:59 7
83f0e0bc Poul Sander 2026-05-30 17:59 8
struct ActionInfo {
83f0e0bc Poul Sander 2026-05-30 17:59 9
    const char *name;        // settings key
83f0e0bc Poul Sander 2026-05-30 17:59 10
    const char *description; // human-readable for help overlay / prefs table
83f0e0bc Poul Sander 2026-05-30 17:59 11
    QKeySequence defaultKey;
83f0e0bc Poul Sander 2026-05-30 17:59 12
    ShortcutManager::Category category;
83f0e0bc Poul Sander 2026-05-30 17:59 13
};
83f0e0bc Poul Sander 2026-05-30 17:59 14
83f0e0bc Poul Sander 2026-05-30 17:59 15
// Order must match ShortcutManager::Action enum
83f0e0bc Poul Sander 2026-05-30 17:59 16
static const ActionInfo kDefaults[ShortcutManager::ActionCount] = {
83f0e0bc Poul Sander 2026-05-30 17:59 17
    {"ToggleHelp",      "Show/hide keyboard shortcuts", QKeySequence(Qt::Key_F1),       ShortcutManager::CatNotConfigurable},
83f0e0bc Poul Sander 2026-05-30 17:59 18
    {"CloseViewer",     "Close viewer",                 QKeySequence(Qt::Key_Escape),   ShortcutManager::CatImageViewer},
83f0e0bc Poul Sander 2026-05-30 17:59 19
    {"CloseViewerAlt",  "Close viewer (alt)",           QKeySequence(Qt::Key_Return),   ShortcutManager::CatImageViewer},
83f0e0bc Poul Sander 2026-05-30 17:59 20
    {"NextImage",       "Next image",                   QKeySequence(Qt::Key_PageDown), ShortcutManager::CatImageViewer},
83f0e0bc Poul Sander 2026-05-30 17:59 21
    {"PrevImage",       "Previous image",               QKeySequence(Qt::Key_PageUp),   ShortcutManager::CatImageViewer},
83f0e0bc Poul Sander 2026-05-30 17:59 22
    {"PanLeft",         "Pan left",                     QKeySequence(Qt::Key_Left),     ShortcutManager::CatImageViewer},
83f0e0bc Poul Sander 2026-05-30 17:59 23
    {"PanRight",        "Pan right",                    QKeySequence(Qt::Key_Right),    ShortcutManager::CatImageViewer},
83f0e0bc Poul Sander 2026-05-30 17:59 24
    {"PanUp",           "Pan up",                       QKeySequence(Qt::Key_Up),       ShortcutManager::CatImageViewer},
83f0e0bc Poul Sander 2026-05-30 17:59 25
    {"PanDown",         "Pan down",                     QKeySequence(Qt::Key_Down),     ShortcutManager::CatImageViewer},
83f0e0bc Poul Sander 2026-05-30 17:59 26
    {"ZoomOriginal",    "Zoom to original size",        QKeySequence(Qt::Key_Slash),    ShortcutManager::CatImageViewer},
83f0e0bc Poul Sander 2026-05-30 17:59 27
    {"ZoomFit",         "Zoom to fit screen",           QKeySequence(Qt::Key_Asterisk), ShortcutManager::CatImageViewer},
83f0e0bc Poul Sander 2026-05-30 17:59 28
    {"ZoomIn",          "Zoom in",                      QKeySequence(Qt::Key_Plus),     ShortcutManager::CatImageViewer},
83f0e0bc Poul Sander 2026-05-30 17:59 29
    {"ZoomOut",         "Zoom out",                     QKeySequence(Qt::Key_Minus),    ShortcutManager::CatImageViewer},
83f0e0bc Poul Sander 2026-05-30 17:59 30
    {"ToggleExif",      "Toggle EXIF info",             QKeySequence(Qt::Key_I),        ShortcutManager::CatImageViewer},
83f0e0bc Poul Sander 2026-05-30 17:59 31
    {"EditCaption",     "Edit caption",                 QKeySequence(Qt::Key_E),        ShortcutManager::CatImageViewer},
83f0e0bc Poul Sander 2026-05-30 17:59 32
    {"ToggleFullscreen","Toggle fullscreen",            QKeySequence(Qt::Key_F11),      ShortcutManager::CatImageViewer},
83f0e0bc Poul Sander 2026-05-30 17:59 33
    {"ActivateEntry",   "Open folder / image",          QKeySequence(Qt::Key_Return),   ShortcutManager::CatBrowser},
bf525669 Poul Sander 2026-05-30 23:18 34
    {"PrevFolder",      "Previous folder",              QKeySequence(Qt::CTRL | Qt::Key_Left),  ShortcutManager::CatBrowser},
bf525669 Poul Sander 2026-05-30 23:18 35
    {"NextFolder",      "Next folder",                  QKeySequence(Qt::CTRL | Qt::Key_Right), ShortcutManager::CatBrowser},
83f0e0bc Poul Sander 2026-05-30 17:59 36
};
83f0e0bc Poul Sander 2026-05-30 17:59 37
83f0e0bc Poul Sander 2026-05-30 17:59 38
} // namespace
83f0e0bc Poul Sander 2026-05-30 17:59 39
83f0e0bc Poul Sander 2026-05-30 17:59 40
ShortcutManager::ShortcutManager()
83f0e0bc Poul Sander 2026-05-30 17:59 41
{
83f0e0bc Poul Sander 2026-05-30 17:59 42
    resetToDefaults();
83f0e0bc Poul Sander 2026-05-30 17:59 43
}
83f0e0bc Poul Sander 2026-05-30 17:59 44
83f0e0bc Poul Sander 2026-05-30 17:59 45
ShortcutManager &ShortcutManager::instance()
83f0e0bc Poul Sander 2026-05-30 17:59 46
{
83f0e0bc Poul Sander 2026-05-30 17:59 47
    static ShortcutManager mgr;
83f0e0bc Poul Sander 2026-05-30 17:59 48
    return mgr;
83f0e0bc Poul Sander 2026-05-30 17:59 49
}
83f0e0bc Poul Sander 2026-05-30 17:59 50
83f0e0bc Poul Sander 2026-05-30 17:59 51
QKeySequence ShortcutManager::shortcut(Action action) const
83f0e0bc Poul Sander 2026-05-30 17:59 52
{
83f0e0bc Poul Sander 2026-05-30 17:59 53
    if (action < 0 || action >= ActionCount)
83f0e0bc Poul Sander 2026-05-30 17:59 54
        return {};
83f0e0bc Poul Sander 2026-05-30 17:59 55
    return m_bindings[action].key;
83f0e0bc Poul Sander 2026-05-30 17:59 56
}
83f0e0bc Poul Sander 2026-05-30 17:59 57
83f0e0bc Poul Sander 2026-05-30 17:59 58
QString ShortcutManager::displayKey(Action action) const
83f0e0bc Poul Sander 2026-05-30 17:59 59
{
83f0e0bc Poul Sander 2026-05-30 17:59 60
    if (action < 0 || action >= ActionCount)
83f0e0bc Poul Sander 2026-05-30 17:59 61
        return {};
83f0e0bc Poul Sander 2026-05-30 17:59 62
    return m_bindings[action].key.toString(QKeySequence::NativeText);
83f0e0bc Poul Sander 2026-05-30 17:59 63
}
83f0e0bc Poul Sander 2026-05-30 17:59 64
83f0e0bc Poul Sander 2026-05-30 17:59 65
QString ShortcutManager::description(Action action) const
83f0e0bc Poul Sander 2026-05-30 17:59 66
{
83f0e0bc Poul Sander 2026-05-30 17:59 67
    return actionDescription(action);
83f0e0bc Poul Sander 2026-05-30 17:59 68
}
83f0e0bc Poul Sander 2026-05-30 17:59 69
83f0e0bc Poul Sander 2026-05-30 17:59 70
void ShortcutManager::setShortcut(Action action, const QKeySequence &seq)
83f0e0bc Poul Sander 2026-05-30 17:59 71
{
83f0e0bc Poul Sander 2026-05-30 17:59 72
    if (action >= 0 && action < ActionCount)
83f0e0bc Poul Sander 2026-05-30 17:59 73
        m_bindings[action].key = seq;
83f0e0bc Poul Sander 2026-05-30 17:59 74
}
83f0e0bc Poul Sander 2026-05-30 17:59 75
83f0e0bc Poul Sander 2026-05-30 17:59 76
void ShortcutManager::resetToDefaults()
83f0e0bc Poul Sander 2026-05-30 17:59 77
{
83f0e0bc Poul Sander 2026-05-30 17:59 78
    for (int i = 0; i < ActionCount; ++i)
83f0e0bc Poul Sander 2026-05-30 17:59 79
        m_bindings[i].key = kDefaults[i].defaultKey;
83f0e0bc Poul Sander 2026-05-30 17:59 80
}
83f0e0bc Poul Sander 2026-05-30 17:59 81
83f0e0bc Poul Sander 2026-05-30 17:59 82
void ShortcutManager::load(QSettings &settings)
83f0e0bc Poul Sander 2026-05-30 17:59 83
{
83f0e0bc Poul Sander 2026-05-30 17:59 84
    settings.beginGroup("shortcuts");
83f0e0bc Poul Sander 2026-05-30 17:59 85
    for (int i = 0; i < ActionCount; ++i) {
83f0e0bc Poul Sander 2026-05-30 17:59 86
        if (kDefaults[i].category == CatNotConfigurable) {
83f0e0bc Poul Sander 2026-05-30 17:59 87
            m_bindings[i].key = kDefaults[i].defaultKey;
83f0e0bc Poul Sander 2026-05-30 17:59 88
            continue;
83f0e0bc Poul Sander 2026-05-30 17:59 89
        }
83f0e0bc Poul Sander 2026-05-30 17:59 90
        const QString key = QString::fromLatin1(kDefaults[i].name);
83f0e0bc Poul Sander 2026-05-30 17:59 91
        if (settings.contains(key))
83f0e0bc Poul Sander 2026-05-30 17:59 92
            m_bindings[i].key = QKeySequence(settings.value(key).toString());
83f0e0bc Poul Sander 2026-05-30 17:59 93
        else
83f0e0bc Poul Sander 2026-05-30 17:59 94
            m_bindings[i].key = kDefaults[i].defaultKey;
83f0e0bc Poul Sander 2026-05-30 17:59 95
    }
83f0e0bc Poul Sander 2026-05-30 17:59 96
    settings.endGroup();
83f0e0bc Poul Sander 2026-05-30 17:59 97
}
83f0e0bc Poul Sander 2026-05-30 17:59 98
83f0e0bc Poul Sander 2026-05-30 17:59 99
void ShortcutManager::save(QSettings &settings) const
83f0e0bc Poul Sander 2026-05-30 17:59 100
{
83f0e0bc Poul Sander 2026-05-30 17:59 101
    settings.beginGroup("shortcuts");
83f0e0bc Poul Sander 2026-05-30 17:59 102
    for (int i = 0; i < ActionCount; ++i) {
83f0e0bc Poul Sander 2026-05-30 17:59 103
        if (kDefaults[i].category == CatNotConfigurable)
83f0e0bc Poul Sander 2026-05-30 17:59 104
            continue;
83f0e0bc Poul Sander 2026-05-30 17:59 105
        const QString key = QString::fromLatin1(kDefaults[i].name);
83f0e0bc Poul Sander 2026-05-30 17:59 106
        if (m_bindings[i].key == kDefaults[i].defaultKey)
83f0e0bc Poul Sander 2026-05-30 17:59 107
            settings.remove(key); // only store non-defaults
83f0e0bc Poul Sander 2026-05-30 17:59 108
        else
83f0e0bc Poul Sander 2026-05-30 17:59 109
            settings.setValue(key, m_bindings[i].key.toString());
83f0e0bc Poul Sander 2026-05-30 17:59 110
    }
83f0e0bc Poul Sander 2026-05-30 17:59 111
    settings.endGroup();
83f0e0bc Poul Sander 2026-05-30 17:59 112
}
83f0e0bc Poul Sander 2026-05-30 17:59 113
83f0e0bc Poul Sander 2026-05-30 17:59 114
QKeySequence ShortcutManager::defaultShortcut(Action action)
83f0e0bc Poul Sander 2026-05-30 17:59 115
{
83f0e0bc Poul Sander 2026-05-30 17:59 116
    if (action < 0 || action >= ActionCount)
83f0e0bc Poul Sander 2026-05-30 17:59 117
        return {};
83f0e0bc Poul Sander 2026-05-30 17:59 118
    return kDefaults[action].defaultKey;
83f0e0bc Poul Sander 2026-05-30 17:59 119
}
83f0e0bc Poul Sander 2026-05-30 17:59 120
83f0e0bc Poul Sander 2026-05-30 17:59 121
QString ShortcutManager::actionName(Action action)
83f0e0bc Poul Sander 2026-05-30 17:59 122
{
83f0e0bc Poul Sander 2026-05-30 17:59 123
    if (action < 0 || action >= ActionCount)
83f0e0bc Poul Sander 2026-05-30 17:59 124
        return {};
83f0e0bc Poul Sander 2026-05-30 17:59 125
    return QString::fromLatin1(kDefaults[action].name);
83f0e0bc Poul Sander 2026-05-30 17:59 126
}
83f0e0bc Poul Sander 2026-05-30 17:59 127
83f0e0bc Poul Sander 2026-05-30 17:59 128
QString ShortcutManager::actionDescription(Action action)
83f0e0bc Poul Sander 2026-05-30 17:59 129
{
83f0e0bc Poul Sander 2026-05-30 17:59 130
    if (action < 0 || action >= ActionCount)
83f0e0bc Poul Sander 2026-05-30 17:59 131
        return {};
83f0e0bc Poul Sander 2026-05-30 17:59 132
    return QString::fromLatin1(kDefaults[action].description);
83f0e0bc Poul Sander 2026-05-30 17:59 133
}
83f0e0bc Poul Sander 2026-05-30 17:59 134
83f0e0bc Poul Sander 2026-05-30 17:59 135
ShortcutManager::Category ShortcutManager::actionCategory(Action action)
83f0e0bc Poul Sander 2026-05-30 17:59 136
{
83f0e0bc Poul Sander 2026-05-30 17:59 137
    if (action < 0 || action >= ActionCount)
83f0e0bc Poul Sander 2026-05-30 17:59 138
        return CatNotConfigurable;
83f0e0bc Poul Sander 2026-05-30 17:59 139
    return kDefaults[action].category;
83f0e0bc Poul Sander 2026-05-30 17:59 140
}
83f0e0bc Poul Sander 2026-05-30 17:59 141
83f0e0bc Poul Sander 2026-05-30 17:59 142
bool ShortcutManager::matches(const QKeyEvent *event, Action action) const
83f0e0bc Poul Sander 2026-05-30 17:59 143
{
83f0e0bc Poul Sander 2026-05-30 17:59 144
    if (action < 0 || action >= ActionCount)
83f0e0bc Poul Sander 2026-05-30 17:59 145
        return false;
83f0e0bc Poul Sander 2026-05-30 17:59 146
    const QKeySequence &seq = m_bindings[action].key;
83f0e0bc Poul Sander 2026-05-30 17:59 147
    if (seq.isEmpty())
83f0e0bc Poul Sander 2026-05-30 17:59 148
        return false;
83f0e0bc Poul Sander 2026-05-30 17:59 149
    // Compare the first key in the sequence against the event's key + modifiers.
83f0e0bc Poul Sander 2026-05-30 17:59 150
    int eventKey = event->key();
83f0e0bc Poul Sander 2026-05-30 17:59 151
    Qt::KeyboardModifiers mods = event->modifiers() & ~Qt::KeypadModifier;
83f0e0bc Poul Sander 2026-05-30 17:59 152
    if (mods != Qt::NoModifier)
83f0e0bc Poul Sander 2026-05-30 17:59 153
        eventKey |= mods;
83f0e0bc Poul Sander 2026-05-30 17:59 154
    return seq[0] == QKeyCombination::fromCombined(eventKey);
83f0e0bc Poul Sander 2026-05-30 17:59 155
}
1970-01-01 00:00 156