Merge pull request #1235 from ispyisail/fix/bugtracker-112-editor-snap-zoomed-out

Fix bugtracker #112: no snap in the symbol editor below 100 % zoom
This commit is contained in:
Laurent Trinques
2026-10-02 12:42:52 +02:00
committed by GitHub
5 changed files with 138 additions and 27 deletions
+7 -1
View File
@@ -206,7 +206,13 @@ void ElementPrimitiveDecorator::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
// When moving the selection, consider the position of the first selected item
QPointF current_position = scene_pos - mouse_offset_;
QPointF rounded_current_position = snapConstPointToGrid(current_position);
// Snap to the editor's grid, as a single part does (bugtracker
// #112); Ctrl keeps the fine 1-unit step.
ElementScene *element_scene = qobject_cast<ElementScene *>(scene());
QPointF rounded_current_position =
(element_scene && !(event->modifiers() & Qt::ControlModifier))
? element_scene->snapToGrid(current_position)
: snapConstPointToGrid(current_position);
movement = rounded_current_position - decorated_items_.at(0) -> toItem() -> scenePos();
QRectF bounding_rect = modified_bounding_rect_;
+11 -26
View File
@@ -17,6 +17,8 @@
*/
#include "elementview.h"
#include "elementviewgrid.h"
#include "../qetapp.h"
#include "UndoCommand/pastepartscommand.h"
#include "ui/qetelementeditor.h"
@@ -535,29 +537,12 @@ void ElementView::drawBackground(QPainter *p, const QRectF &r) {
// choisit la granularite de la grille en fonction du zoom en cours
// selects the grid granularity according to the current zoom level
int drawn_x_grid = 1;//scene_ -> xGrid();
int drawn_y_grid = 1;//scene_ -> yGrid();
bool draw_grid = true;
bool draw_cross = false;
const ElementViewGrid grid = ElementViewGrid::forZoom(zoom_factor);
const int grid_step = grid.step;
const bool draw_grid = grid.draw_grid;
const bool draw_cross = grid.draw_cross;
if (zoom_factor < 1.0) { //< no grid
draw_grid = false;
} else if (zoom_factor < 4.0) { //< grid 10*10
drawn_x_grid *= 10;
drawn_y_grid *= 10;
}else if (zoom_factor < 8.0) { //< grid 5*5
drawn_x_grid *= 5;
drawn_y_grid *= 5;
draw_cross = true;
} else if (zoom_factor < 10.0) { //< grid 2*2
drawn_x_grid *= 2;
drawn_y_grid *= 2;
draw_cross = true;
} else { //< grid 1*1
draw_cross = true;
}
m_scene->setGrid(drawn_x_grid, drawn_y_grid);
m_scene->setGrid(grid_step, grid_step);
if (draw_grid) {
// draw the dots of the grid
@@ -587,12 +572,12 @@ void ElementView::drawBackground(QPainter *p, const QRectF &r) {
qreal limit_y = r.y() + r.height();
int g_x = (int)ceil(r.x());
while (g_x % drawn_x_grid) ++ g_x;
while (g_x % grid_step) ++ g_x;
int g_y = (int)ceil(r.y());
while (g_y % drawn_y_grid) ++ g_y;
while (g_y % grid_step) ++ g_y;
for (int gx = g_x ; gx < limit_x ; gx += drawn_x_grid) {
for (int gy = g_y ; gy < limit_y ; gy += drawn_y_grid) {
for (int gx = g_x ; gx < limit_x ; gx += grid_step) {
for (int gy = g_y ; gy < limit_y ; gy += grid_step) {
if (draw_cross) {
if (!(gx % 10) && !(gy % 10)) {
p -> drawLine(QLineF(gx - (pen.width()/4.0), gy, gx + (pen.width()/4.0), gy));
+60
View File
@@ -0,0 +1,60 @@
/*
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/>.
*/
#ifndef ELEMENTVIEWGRID_H
#define ELEMENTVIEWGRID_H
#include <QtGlobal>
/**
The symbol editor's grid at a given zoom: the step parts snap to, and
how the grid is drawn. The step gets finer as the view zooms in, so a
small detail can be placed precisely.
Below 100 % the grid is not drawn (its dots would merge into a grey
wash), but parts still snap every 10 units, as at 100 %. Snapping to 1
unit there left everything dragged while zoomed out off the grid, with
nothing on screen to show it (bugtracker #112).
*/
struct ElementViewGrid
{
int step = 10;
bool draw_grid = true;
bool draw_cross = false;
static ElementViewGrid forZoom(qreal zoom_factor)
{
ElementViewGrid grid;
if (zoom_factor < 1.0) {
grid.draw_grid = false;
} else if (zoom_factor < 4.0) {
grid.step = 10;
} else if (zoom_factor < 8.0) {
grid.step = 5;
grid.draw_cross = true;
} else if (zoom_factor < 10.0) {
grid.step = 2;
grid.draw_cross = true;
} else {
grid.step = 1;
grid.draw_cross = true;
}
return grid;
}
};
#endif // ELEMENTVIEWGRID_H