Add a shortcut bar that opens at the cursor with S

Pressing S on a folio opens the element picker at the cursor with a row
of commands above it, chosen by what is selected, like the SolidWorks
shortcut bar:

  nothing selected     insert last element, element picker, text, line,
                       rectangle, terminal strip plan, paste, folio
                       properties
  elements selected    rotate, rotate texts, edit, copy, cut, delete
  only conductors      reset path, edit, delete

Each row is a list of ShortcutManager ids, so any registered command can
go on it and the bar carries no command list of its own. The lists are
in QSettings (diagrameditor/shortcut_bar/<context>); a context the user
has not changed follows the defaults. A disabled command keeps its
place, greyed, so a row looks the same each time. Clicking a button
closes the bar and triggers the action.

A new configuration page, "Barre de raccourcis", edits the three lists:
add, remove and reorder any diagram editor command.

To make that possible:
- ShortcutManager::action(id, owner) returns the action a given window
  registered under an id, since each editor window registers its own.
- The add-item actions (text, image, shapes, terminal strip plan) are
  registered as diagrameditor.add_<kind>, with no default key. They also
  appear in the Shortcuts page and can now be bound.

Discussion #1033.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G2d2Zi8BfrYRPX88zhaoFG
(cherry picked from commit 53c1e213282b9f7226ca1c09e81703113236bb88)
This commit is contained in:
ispyisail
2026-09-26 12:38:40 +12:00
parent 07501d604e
commit 2da101ca90
12 changed files with 659 additions and 13 deletions
@@ -19,7 +19,10 @@
#include "elementscollectionwidget.h"
#include <QAction>
#include <QGuiApplication>
#include <QHBoxLayout>
#include <QToolButton>
#include <QKeyEvent>
#include <QLabel>
#include <QLineEdit>
@@ -58,6 +61,13 @@ ElementPickerPopup::ElementPickerPopup(ElementsCollectionWidget *source,
layout->setContentsMargins(6, 6, 6, 6);
layout->setSpacing(4);
//Command row, shown when the picker is opened as the shortcut bar
m_commands = new QWidget(this);
m_commands_layout = new QHBoxLayout(m_commands);
m_commands_layout->setContentsMargins(0, 0, 0, 0);
m_commands_layout->setSpacing(2);
m_commands->hide();
m_search = new QLineEdit(this);
m_search->setPlaceholderText(tr("Rechercher un élément…"));
m_search->setClearButtonEnabled(true);
@@ -73,6 +83,7 @@ ElementPickerPopup::ElementPickerPopup(ElementsCollectionWidget *source,
m_hint = new QLabel(tr("Entrée pour insérer · Échap pour fermer"), this);
m_hint->setEnabled(false);
layout->addWidget(m_commands);
layout->addWidget(m_search);
layout->addWidget(m_view);
layout->addWidget(m_hint);
@@ -97,8 +108,10 @@ ElementPickerPopup::ElementPickerPopup(ElementsCollectionWidget *source,
focused and any previous query cleared.
@param global_pos
*/
void ElementPickerPopup::popUpAt(const QPoint &global_pos)
void ElementPickerPopup::popUpAt(const QPoint &global_pos,
const QList<QAction *> &commands)
{
setCommands(commands);
m_search->clear();
m_model->clear();
showPalette();
@@ -121,6 +134,53 @@ void ElementPickerPopup::popUpAt(const QPoint &global_pos)
m_search->setFocus();
}
/**
@brief ElementPickerPopup::setCommands
Show @a commands as a row of buttons above the search field, or hide the
row when there are none. A disabled command keeps its place, greyed out,
so the row looks the same every time for a given selection.
Clicking a button closes the picker first, then triggers the action: a
command such as "add a line" starts a mode on the folio, which needs the
focus the popup holds.
@param commands
*/
void ElementPickerPopup::setCommands(const QList<QAction *> &commands)
{
while (QLayoutItem *item = m_commands_layout->takeAt(0)) {
delete item->widget();
delete item;
}
for (QAction *action : commands)
{
auto *button = new QToolButton(m_commands);
button->setAutoRaise(true);
button->setIconSize(QSize(24, 24));
const QString text = action->text().remove(QLatin1Char('&'));
if (action->icon().isNull()) {
button->setText(text);
button->setToolButtonStyle(Qt::ToolButtonTextOnly);
} else {
button->setIcon(action->icon());
}
const QKeySequence key = action->shortcut();
button->setToolTip(key.isEmpty()
? text
: QStringLiteral("%1 (%2)").arg(
text, key.toString(QKeySequence::NativeText)));
button->setEnabled(action->isEnabled());
button->setFocusPolicy(Qt::NoFocus);
connect(button, &QToolButton::clicked, this, [this, action]() {
hide();
action->trigger();
});
m_commands_layout->addWidget(button);
}
m_commands_layout->addStretch();
m_commands->setVisible(!commands.isEmpty());
}
/**
@brief ElementPickerPopup::runSearch
*/