mirror of
https://github.com/fergalmoran/flameshot.git
synced 2025-12-22 09:51:06 +00:00
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:
File diff suppressed because it is too large
Load Diff
@@ -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());
|
||||
|
||||
@@ -87,6 +87,8 @@ private slots:
|
||||
void onMoveCaptureToolDown(int captureToolIndex);
|
||||
void selectAll();
|
||||
void xywhTick();
|
||||
void onDisplayGridChanged(bool display);
|
||||
void onGridSizeChanged(int size);
|
||||
|
||||
public:
|
||||
void removeToolObject(int index = -1);
|
||||
@@ -147,6 +149,8 @@ private:
|
||||
CaptureTool* activeButtonTool() const;
|
||||
CaptureTool::Type activeButtonToolType() const;
|
||||
|
||||
QPoint snapToGrid(const QPoint& point) const;
|
||||
|
||||
////////////////////////////////////////
|
||||
// Class members
|
||||
|
||||
@@ -214,4 +218,8 @@ private:
|
||||
// For start moving after more than X offset
|
||||
QPoint m_startMovePos;
|
||||
bool m_startMove;
|
||||
|
||||
// Grid
|
||||
bool m_displayGrid{ false };
|
||||
int m_gridSize{ 10 };
|
||||
};
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "src/utils/pathinfo.h"
|
||||
#include "utilitypanel.h"
|
||||
#include <QApplication>
|
||||
#include <QCheckBox>
|
||||
#include <QKeyEvent>
|
||||
#include <QLabel>
|
||||
#include <QLineEdit>
|
||||
@@ -80,6 +81,18 @@ SidePanelWidget::SidePanelWidget(QPixmap* p, QWidget* parent)
|
||||
m_layout->addWidget(m_colorWheel);
|
||||
m_layout->addWidget(m_colorHex);
|
||||
|
||||
QHBoxLayout* gridHBoxLayout = new QHBoxLayout(this);
|
||||
m_gridCheck = new QCheckBox(tr("Display grid"), this);
|
||||
m_gridSizeSpin = new QSpinBox(this);
|
||||
m_gridSizeSpin->setRange(5, 50);
|
||||
m_gridSizeSpin->setSingleStep(5);
|
||||
m_gridSizeSpin->setValue(10);
|
||||
m_gridSizeSpin->setDisabled(true);
|
||||
m_gridSizeSpin->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
||||
gridHBoxLayout->addWidget(m_gridCheck);
|
||||
gridHBoxLayout->addWidget(m_gridSizeSpin);
|
||||
m_layout->addLayout(gridHBoxLayout);
|
||||
|
||||
// tool size sigslots
|
||||
connect(m_toolSizeSpin,
|
||||
static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
|
||||
@@ -112,6 +125,15 @@ SidePanelWidget::SidePanelWidget(QPixmap* p, QWidget* parent)
|
||||
&color_widgets::ColorWheel::colorSelected,
|
||||
this,
|
||||
&SidePanelWidget::colorChanged);
|
||||
// Grid feature
|
||||
connect(m_gridCheck, &QCheckBox::clicked, this, [=](bool b) {
|
||||
this->m_gridSizeSpin->setEnabled(b);
|
||||
emit this->displayGridChanged(b);
|
||||
});
|
||||
connect(m_gridSizeSpin,
|
||||
qOverload<int>(&QSpinBox::valueChanged),
|
||||
this,
|
||||
&SidePanelWidget::gridSizeChanged);
|
||||
}
|
||||
|
||||
void SidePanelWidget::onColorChanged(const QColor& color)
|
||||
|
||||
@@ -14,6 +14,7 @@ class QLineEdit;
|
||||
class ColorGrabWidget;
|
||||
class QColorPickingEventFilter;
|
||||
class QSlider;
|
||||
class QCheckBox;
|
||||
|
||||
constexpr int maxToolSize = 50;
|
||||
constexpr int minSliderWidth = 100;
|
||||
@@ -31,6 +32,8 @@ signals:
|
||||
void colorChanged(const QColor& color);
|
||||
void toolSizeChanged(int size);
|
||||
void togglePanel();
|
||||
void displayGridChanged(bool display);
|
||||
void gridSizeChanged(int size);
|
||||
|
||||
public slots:
|
||||
void onToolSizeChanged(int tool);
|
||||
@@ -61,4 +64,6 @@ private:
|
||||
QSpinBox* m_toolSizeSpin;
|
||||
QSlider* m_toolSizeSlider;
|
||||
int m_toolSize{};
|
||||
QCheckBox* m_gridCheck{ nullptr };
|
||||
QSpinBox* m_gridSizeSpin{ nullptr };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user