Snap to grid (#3016)

* Add widget to side panel

* Draw grid

* snap tool to grid

* Update french translation

* Format code with clang-format

Co-authored-by: joshua <joshua@qelectrotech.org>
This commit is contained in:
Joshua-cla
2022-12-20 18:54:31 +01:00
committed by GitHub
parent b4300d303d
commit 685d0ee84b
5 changed files with 455 additions and 386 deletions

View File

@@ -393,6 +393,18 @@ void CaptureWidget::xywhTick()
repaint();
}
void CaptureWidget::onDisplayGridChanged(bool display)
{
m_displayGrid = display;
repaint();
}
void CaptureWidget::onGridSizeChanged(int size)
{
m_gridSize = size;
repaint();
}
void CaptureWidget::showxywh(bool show)
{
int timeout =
@@ -605,6 +617,31 @@ void CaptureWidget::paintEvent(QPaintEvent* paintEvent)
xy);
}
if (m_displayGrid) {
painter.save();
QColor uicolor = ConfigHandler().uiColor();
uicolor.setAlpha(100);
painter.setPen(uicolor);
painter.setBrush(QBrush(uicolor));
auto topLeft = mapToGlobal(m_context.selection.topLeft());
topLeft.rx() -= topLeft.x() % m_gridSize;
topLeft.ry() -= topLeft.y() % m_gridSize;
topLeft = mapFromGlobal(topLeft);
const auto scale{ m_context.screenshot.devicePixelRatio() };
const auto step{ m_gridSize * scale };
const auto radius{ 1 * scale };
for (int y = topLeft.y(); y < m_context.selection.bottom(); y += step) {
for (int x = topLeft.x(); x < m_context.selection.right();
x += step) {
painter.drawEllipse(x, y, radius, radius);
}
}
painter.restore();
}
if (m_activeTool && m_mouseIsClicked) {
m_activeTool->process(painter, m_context.screenshot);
} else if (m_previewEnabled && activeButtonTool() &&
@@ -671,7 +708,8 @@ bool CaptureWidget::startDrawObjectTool(const QPoint& pos)
&CaptureTool::requestAction,
this,
&CaptureWidget::handleToolSignal);
m_context.mousePos = pos;
m_context.mousePos = m_displayGrid ? snapToGrid(pos) : pos;
m_activeTool->drawStart(m_context);
// TODO this is the wrong place to do this
@@ -847,7 +885,8 @@ void CaptureWidget::mouseMoveEvent(QMouseEvent* e)
if (m_adjustmentButtonPressed) {
m_activeTool->drawMoveWithAdjustment(e->pos());
} else {
m_activeTool->drawMove(e->pos());
m_activeTool->drawMove(m_displayGrid ? snapToGrid(e->pos())
: e->pos());
}
// update drawing object
updateTool(m_activeTool);
@@ -1126,6 +1165,14 @@ void CaptureWidget::initPanel()
&SidePanelWidget::togglePanel,
m_panel,
&UtilityPanel::toggle);
connect(m_sidePanel,
&SidePanelWidget::displayGridChanged,
this,
&CaptureWidget::onDisplayGridChanged);
connect(m_sidePanel,
&SidePanelWidget::gridSizeChanged,
this,
&CaptureWidget::onGridSizeChanged);
// TODO replace with a CaptureWidget signal
emit m_sidePanel->colorChanged(m_context.color);
emit toolSizeChanged(m_context.toolSize);
@@ -1721,6 +1768,20 @@ CaptureTool::Type CaptureWidget::activeButtonToolType() const
return activeTool->type();
}
QPoint CaptureWidget::snapToGrid(const QPoint& point) const
{
QPoint snapPoint = mapToGlobal(point);
const auto scale{ m_context.screenshot.devicePixelRatio() };
snapPoint.setX((qRound(snapPoint.x() / double(m_gridSize)) * m_gridSize) *
scale);
snapPoint.setY((qRound(snapPoint.y() / double(m_gridSize)) * m_gridSize) *
scale);
return mapFromGlobal(snapPoint);
}
QPointer<CaptureTool> CaptureWidget::activeToolObject()
{
return m_captureToolObjects.at(m_panel->activeLayerIndex());