Files
qelectrotech-source-mirror/sources/shortcutmanager.cpp
T
ispyisail 5275fb44fe Add configurable shortcuts: ShortcutManager registry + Shortcuts config page (#574)
Implements the first pillar of #574: a "Shortcuts" preferences page letting
users rebind, search and reset every keyboard shortcut in the app.

What it does
- New ShortcutManager singleton: every one of the ~95 setShortcut()/
  setShortcuts() call sites across qet.cpp, qetmainwindow.cpp,
  elementspanelwidget.cpp, autonumberingdockwidget.cpp, richtexteditor.cpp,
  qetdiagrameditor.cpp, qettemplateeditor.cpp and qetelementeditor.cpp now
  calls registerAction(target, id, category, default_sequence) instead,
  which applies the user's saved override (or the default) and remembers
  the target for later editing.
- New ShortcutsConfigPage, added to the existing "Configurer QElectroTech"
  dialog: a filterable table of every registered shortcut, grouped by
  category, each with a QKeySequenceEdit and a per-row reset button, plus a
  "reset all" button. Bindings are only persisted (via
  ShortcutManager::setSequence()) when the dialog is accepted.
- Conflict detection: rows whose currently-edited sequence collides with
  another row are highlighted with a tooltip naming the conflicting action.
- Overrides are stored under a "shortcuts/" QSettings group, one key per
  id, keyed to match the id (not persisted at all when equal to the
  hardcoded default), so a future QET version can safely raise a default
  for anyone who never customized it.

Design notes
- Targets are handled generically via QObject rather than QAction, since one
  call site (autonumberingdockwidget's "Configurer" button) is a
  QPushButton, not a QAction. Both declare an identical "shortcut"
  QKeySequence Q_PROPERTY, so registerAction() reads/writes it through the
  property system instead of needing a separate code path.
- Several live targets can share one id at once -- QET allows multiple
  windows of the same kind (diagram editor, element editor...) open
  simultaneously, each constructing its own QAction with the same id.
  setSequence() updates every live target for that id in one call, so a
  rebind takes effect in all open windows immediately, without restart.
- A shortcut's description is captured from its target's text() the first
  time that id is registered, then cached -- so the config page stays
  correct even after the owning window is closed. One consequence: a
  shortcut belonging to an on-demand window (element editor, title block
  editor, rich text editor) only appears in the list once that window has
  been opened at least once in the current session, since nothing has
  registered its id yet otherwise.

Testing
Full CMake build (qmake CONFIG+=no_kf5, Qt 5.15) compiles clean with zero
errors and zero new warnings. Verified end-to-end in a real running session
(Xvfb + xdotool):
- The Shortcuts page appears in Configure QElectroTech with the right icon,
  lists every always-registered shortcut with correct category/action name/
  current binding.
- The filter box correctly narrows the list, and correctly returns nothing
  for an action whose owning window hasn't been constructed yet this
  session (confirming the on-demand-registration behavior above is working
  as designed, not silently broken).
- Conflict detection correctly flagged a real pre-existing same-key overlap
  between "Supprimer" (delete selection, Del) and "Supprimer ce folio"
  (delete diagram from panel, Del) -- both highlighted with explanatory
  tooltips.
- Rebound "Manuel en ligne" to Ctrl+Shift+M, clicked OK: persisted under
  [shortcuts] in QElectroTech.conf, and the Aide menu's entry showed the new
  binding immediately, no restart needed.
- Reopened the dialog: the rebind was still shown. Clicked its per-row
  reset button, then OK: the settings key was removed entirely (not stored
  as "F1"), correctly falling back to the hardcoded default.

Retrofitting the Tab/Shift+Tab, select-all (#585) and Ctrl+G jump-to-element
(#586) shortcuts through this registry is left for a follow-up once those
PRs land, to avoid re-merging still-open branches into this one.

Developed with assistance from Claude (Anthropic).
2026-08-01 01:35:51 +12:00

168 lines
5.0 KiB
C++

/*
Copyright 2006-2026 The QElectroTech Team
This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QElectroTech 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "shortcutmanager.h"
#include <QObject>
#include <QSettings>
#include <QVariant>
#include <algorithm>
namespace {
const QString SETTINGS_GROUP = QStringLiteral("shortcuts/");
}
ShortcutManager &ShortcutManager::instance()
{
static ShortcutManager manager;
return manager;
}
/**
@brief ShortcutManager::savedSequence
@param id
@param default_sequence
@return the user-overridden sequence stored for \a id, or \a default_sequence
if the user never overrode it. A stored-but-empty sequence (the user
cleared the shortcut) is returned as an empty QKeySequence, not the default.
*/
QKeySequence ShortcutManager::savedSequence(const QString &id, const QKeySequence &default_sequence) const
{
QSettings settings;
const QString key = SETTINGS_GROUP + id;
if (!settings.contains(key)) {
return default_sequence;
}
return QKeySequence::fromString(settings.value(key).toString());
}
/**
@brief ShortcutManager::registerAction
Register \a target under \a id, applying the user's saved override (if
any) or \a default_sequence otherwise. \a target must be a QAction or a
QAbstractButton -- both declare a "shortcut" QKeySequence property, which
is what's actually read/written here, so this works for either without a
separate code path. Safe to call once per instance that shares this id
(e.g. once per open window of the same kind) -- the first call to reach a
given id fixes its category, description (taken from the target's current
"text" property) and default sequence for the lifetime of the
application; later calls just register another live target to update.
*/
void ShortcutManager::registerAction(QObject *target, const QString &id,
const QString &category,
const QKeySequence &default_sequence)
{
if (!target) {
return;
}
auto it = m_entries.find(id);
if (it == m_entries.end()) {
Entry entry;
entry.category = category;
entry.description = target->property("text").toString();
entry.description.remove(QLatin1Char('&'));
entry.default_sequence = default_sequence;
it = m_entries.insert(id, entry);
m_order << id;
}
QList<QPointer<QObject>> &targets = it->targets;
targets.erase(std::remove_if(targets.begin(), targets.end(),
[](const QPointer<QObject> &t) { return t.isNull(); }),
targets.end());
if (!targets.contains(target)) {
targets << target;
}
target->setProperty("shortcut", QVariant::fromValue(savedSequence(id, it->default_sequence)));
}
/**
@return every registered shortcut, in registration order, along with its
currently effective sequence -- for display in the Shortcuts config page.
*/
QList<ShortcutManager::ShortcutInfo> ShortcutManager::allShortcuts() const
{
QList<ShortcutInfo> list;
for (const QString &id : m_order) {
const Entry &entry = m_entries.value(id);
ShortcutInfo info;
info.id = id;
info.category = entry.category;
info.description = entry.description;
info.default_sequence = entry.default_sequence;
info.current_sequence = savedSequence(id, entry.default_sequence);
list << info;
}
return list;
}
QKeySequence ShortcutManager::currentSequence(const QString &id) const
{
auto it = m_entries.find(id);
if (it == m_entries.end()) {
return QKeySequence();
}
return savedSequence(id, it->default_sequence);
}
/**
@brief ShortcutManager::setSequence
Persist \a sequence as the binding for \a id and apply it immediately to
every currently live QAction registered under that id. Persisted as "no
override" when \a sequence matches the id's default, so a future QET
version raising that default takes effect for users who never customized it.
*/
void ShortcutManager::setSequence(const QString &id, const QKeySequence &sequence)
{
auto it = m_entries.find(id);
if (it == m_entries.end()) {
return;
}
QSettings settings;
const QString key = SETTINGS_GROUP + id;
if (sequence == it->default_sequence) {
settings.remove(key);
} else {
settings.setValue(key, sequence.toString());
}
for (const QPointer<QObject> &target : qAsConst(it->targets)) {
if (target) {
target->setProperty("shortcut", QVariant::fromValue(sequence));
}
}
}
void ShortcutManager::resetToDefault(const QString &id)
{
auto it = m_entries.find(id);
if (it == m_entries.end()) {
return;
}
setSequence(id, it->default_sequence);
}
void ShortcutManager::resetAllToDefaults()
{
for (const QString &id : m_order) {
resetToDefault(id);
}
}