Place an element from the collection without dragging it

DiagramEventAddElement is already a good placement mode: the element
follows the cursor on the grid, a left click drops it, Space rotates it,
and it stays loaded for a run of the same symbol. Its only caller was
DiagramView::handleElementDrop(), so it could be reached only by
finishing a drag. Double-clicking a symbol in the Collections dock
opened the element editor instead.

- DiagramView::startElementPlacement() is split out of
  handleElementDrop(). defaultPlacementPos() uses the cursor when it is
  over the view and the centre of the visible area otherwise.

- ElementsCollectionWidget emits insertElementRequested() on double
  click, or Enter on the highlighted item. The host decides which view
  receives it, so the widget can later be reused outside the editor.

- When there is nowhere to place it (no folio open, read-only project),
  the element editor opens, as a double click did before.

- "Insérer le dernier élément" (Édition menu, default key A) places the
  last element again. DiagramView reports every placement it starts, so
  an element dropped by drag counts too. Macros are not remembered.

A rather than Space: Space rotates the pending element inside placement
mode and is bound three more times in this editor. The key is a
ShortcutManager default and can be changed in the Shortcuts page.

Double click placing is a behaviour change, so it has a preference,
"elementscollection/double-click-inserts" (default true), shown in
Configuration as an opt-out: "Double-cliquer dans la collection ouvre
l'éditeur d'élément au lieu de l'insérer".

Discussions #676 and #1033.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G2d2Zi8BfrYRPX88zhaoFG
This commit is contained in:
ispyisail
2026-08-08 17:18:30 +12:00
parent 4cffdf93b6
commit e6291eb8e8
8 changed files with 259 additions and 23 deletions
+60 -2
View File
@@ -223,14 +223,72 @@ void DiagramView::handleElementDrop(QDropEvent *event)
QPointF drop_pos;
drop_pos = mapToScene(event->position().toPoint());
startElementPlacement(location, drop_pos);
}
/**
@brief DiagramView::startElementPlacement
Enter the interactive placement mode for @a location, with the pending
element starting at @a scene_pos.
This is the mode where the element follows the cursor on the grid, a left
click drops a copy, Space rotates it and the element stays loaded so a run
of identical symbols can be placed with successive clicks.
Split out of handleElementDrop() so that placement is reachable without a
drag: the mode itself was always general, it simply had no caller other
than the end of a drop.
@param location : the element or macro to place
@param scene_pos : where the pending element first appears, in scene
coordinates
@return true if the placement mode was entered
*/
bool DiagramView::startElementPlacement(const ElementsLocation &location,
const QPointF &scene_pos)
{
if (!diagram() || !(location.isElement() && location.exist())) {
return false;
}
if (diagram()->isReadOnly()) {
return false;
}
if (location.path().endsWith(".qetmak")) {
diagram()->setEventInterface(new DiagramEventAddMacro(location, diagram(), drop_pos));
diagram()->setEventInterface(
new DiagramEventAddMacro(location, diagram(), scene_pos));
} else {
diagram()->setEventInterface(new DiagramEventAddElement(location, diagram(), drop_pos));
//DiagramEventAddElement takes a non-const reference, so it needs
//an lvalue it may modify. Copying keeps the caller's location
//untouched -- QETDiagramEditor stores the same one for
//"insert last element".
ElementsLocation loc(location);
diagram()->setEventInterface(
new DiagramEventAddElement(loc, diagram(), scene_pos));
emit elementPlacementStarted(location);
}
//Set focus to the view to get event
this->setFocus();
return true;
}
/**
@brief DiagramView::defaultPlacementPos
@return where a pending element should appear when placement was not
started by a drop, so there is no cursor position to use.
The cursor is used when it is over the view -- picking up a placement where
the user is already looking -- and the centre of the visible area
otherwise.
*/
QPointF DiagramView::defaultPlacementPos() const
{
const QPoint local = mapFromGlobal(QCursor::pos());
if (viewport() && viewport()->rect().contains(local)) {
return mapToScene(local);
}
return mapToScene(viewport() ? viewport()->rect().center()
: rect().center());
}
/**