Run commands with right-drag mouse gestures

Hold the right button on a folio and drag: a ring appears around the
point where the button went down, showing up to eight commands, and the
one in the mouse's direction is highlighted with its name underneath.
Releasing runs it; releasing near the centre cancels. These are
SolidWorks' mouse gestures.

The commands are the shortcut bar's for the selection, placed clockwise
from the top, so customising the bar customises the ring. As with the
context menu, a right press selects what is under the mouse first.

A plain right click still opens the context menu, now on release on
every platform. The view tracks the right button and opens the menu
itself; the platform's own right-click event (sent on press on X11, on
release on Windows) is ignored while gestures are on, so the menu is
never opened twice. The keyboard menu is unchanged.

The view keeps out of the way while a tool or a placement is running,
since a right click cancels or finishes those, and while a text is
being edited. The new General option "Gestes de la souris avec le
bouton droit" (diagrameditor/mouse_gestures, on by default) turns it
off and restores the previous right button exactly.

Discussion #1033.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G2d2Zi8BfrYRPX88zhaoFG
(cherry picked from commit a625142f36e619bb4d4f3551b6086f2dd8eabc86)
This commit is contained in:
ispyisail
2026-09-26 13:49:27 +12:00
parent 329831d986
commit b57c37ca62
7 changed files with 373 additions and 8 deletions
+104 -8
View File
@@ -38,6 +38,7 @@
#include "undocommand/addgraphicsobjectcommand.h"
#include "diagram.h"
#include "diagramcontexttoolbar.h"
#include "diagramgestureoverlay.h"
#include "shortcutbarsettings.h"
#include "shortcutmanager.h"
#include "ElementsCollection/xmlelementcollection.h"
@@ -108,6 +109,7 @@ DiagramView::DiagramView(Diagram *diagram, QWidget *parent) :
}
m_context_toolbar = new DiagramContextToolbar(viewport());
m_gesture_overlay = new DiagramGestureOverlay(viewport());
connect(m_diagram, &QGraphicsScene::selectionChanged, this, [this]() {
if (m_diagram->selectedItems().isEmpty()) {
m_context_toolbar->hide();
@@ -635,6 +637,36 @@ void DiagramView::mousePressEvent(QMouseEvent *e)
m_press_pos = e->position().toPoint();
}
//Right button: a click opens the context menu on release, a drag is
//a gesture (DiagramGestureOverlay). Left alone while a tool runs --
//a right click cancels or finishes it -- and while a text is edited.
m_swallow_native_menu = false;
if (e->button() == Qt::RightButton
&& DiagramGestureOverlay::isEnabled()
&& !m_diagram->eventInterfaceIsRunning()
&& !m_diagram->focusItem())
{
m_gesture_tracking = true;
m_swallow_native_menu = true;
m_gesture_origin = e->position().toPoint();
m_context_toolbar->hide();
//Select what is under the mouse, as the context menu does, so
//a gesture acts on it
if (QGraphicsItem *item = m_diagram->itemAt(mapToScene(m_gesture_origin), transform())) {
if (!item->isSelected()) {
m_diagram->clearSelection();
//Clearing the selection can delete handler items, so
//look the item up again (see contextMenuEvent)
if (QGraphicsItem *again = m_diagram->itemAt(mapToScene(m_gesture_origin), transform())) {
again->setSelected(true);
}
}
}
e->accept();
return;
}
//Start drag view when hold the middle button
if (e->button() == Qt::MiddleButton)
{
@@ -686,6 +718,20 @@ void DiagramView::mouseMoveEvent(QMouseEvent *e)
setToolTip(tr("X: %1 Y: %2").arg(e->pos().x()).arg(e->pos().y()));
if (m_event_interface && m_event_interface->mouseMoveEvent(e)) return;
if (m_gesture_tracking)
{
const QPoint pos = e->position().toPoint();
if (!m_gesture_overlay->isVisible()
&& (pos - m_gesture_origin).manhattanLength() > 2 * QApplication::startDragDistance()) {
m_gesture_overlay->showAt(m_gesture_origin, selectionCommands());
}
if (m_gesture_overlay->isVisible()) {
m_gesture_overlay->setPointer(pos);
}
e->accept();
return;
}
// Drag the view
if (e->buttons() == Qt::MiddleButton)
{
@@ -747,6 +793,34 @@ void DiagramView::mouseReleaseEvent(QMouseEvent *e)
{
if (m_event_interface && m_event_interface->mouseReleaseEvent(e)) return;
if (m_gesture_tracking && e->button() == Qt::RightButton)
{
m_gesture_tracking = false;
const QPoint pos = e->position().toPoint();
if (m_gesture_overlay->isVisible())
{
//A gesture: run the command it points at, if any
QAction *action = m_gesture_overlay->actionAt(pos);
m_gesture_overlay->hide();
if (action) {
action->trigger();
}
}
else
{
//A plain right click: the context menu, opened here on
//release on every platform
QContextMenuEvent menu_event(QContextMenuEvent::Mouse, pos,
e->globalPosition().toPoint(),
e->modifiers());
m_menu_from_gesture = true;
contextMenuEvent(&menu_event);
m_menu_from_gesture = false;
}
e->accept();
return;
}
// Stop drag view
if (e->button() == Qt::MiddleButton)
{
@@ -804,6 +878,27 @@ void DiagramView::mouseReleaseEvent(QMouseEvent *e)
}
}
/**
@brief DiagramView::selectionCommands
@return the shortcut bar's commands for the current selection, as this
window's actions
*/
QList<QAction *> DiagramView::selectionCommands() const
{
QList<QAction *> actions;
QETDiagramEditor *qde = diagramEditor();
if (!qde) {
return actions;
}
const auto context = ShortcutBarSettings::contextFor(m_diagram->selectedItems());
for (const QString &id : ShortcutBarSettings::ids(context)) {
if (QAction *action = ShortcutManager::instance().action(id, qde)) {
actions << action;
}
}
return actions;
}
/**
@brief DiagramView::showContextToolbar
After a click that leaves something selected, show the shortcut bar's
@@ -823,14 +918,7 @@ void DiagramView::showContextToolbar(const QPoint &viewport_pos)
return;
}
QList<QAction *> actions;
const auto context = ShortcutBarSettings::contextFor(selection);
for (const QString &id : ShortcutBarSettings::ids(context)) {
if (QAction *action = ShortcutManager::instance().action(id, qde)) {
actions << action;
}
}
m_context_toolbar->showAt(viewport_pos, actions);
m_context_toolbar->showAt(viewport_pos, selectionCommands());
}
/**
@@ -1599,6 +1687,14 @@ void DiagramView::contextMenuEvent(QContextMenuEvent *e)
//right-click gets.
const bool from_keyboard = e->reason() == QContextMenuEvent::Keyboard;
//With gestures on, a right press is tracked by mousePressEvent and
//the menu opened on release; the platform's own event (sent on press
//on X11, on release on Windows) would open it a second time.
if (!from_keyboard && m_swallow_native_menu && !m_menu_from_gesture) {
e->accept();
return;
}
if (from_keyboard)
{
//Aim at the selection when there is one, so the menu appears