Skip to content

Commit b777380

Browse files
Increment/decrement circle counter on mouse wheel (flameshot-org#4698)
* Increment/decrement circle counter on mouse wheel * Cap circle counter tool to 999 --------- Co-authored-by: Maxim Inozemtsev <minozemtsev@ics.com>
1 parent cb607c1 commit b777380

4 files changed

Lines changed: 58 additions & 19 deletions

File tree

src/tools/capturetool.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ class CaptureTool : public QObject
105105
return {};
106106
};
107107
virtual QRect boundingRect() const = 0;
108+
virtual bool handleMouseWheelEvent(int delta,
109+
bool adjustmentButtonPressed,
110+
CaptureContext& context)
111+
{
112+
return false;
113+
}
108114

109115
// The icon of the tool.
110116
// inEditor is true when the icon is requested inside the editor

src/tools/circlecount/circlecounttool.cpp

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
namespace {
1111
#define PADDING_VALUE 2
1212
#define THICKNESS_OFFSET 15
13+
int const MIN_COUNTER = 1;
14+
int const MAX_COUNTER = 999;
1315
}
1416

1517
CircleCountTool::CircleCountTool(QObject* parent)
@@ -180,21 +182,50 @@ void CircleCountTool::paintMousePreview(QPainter& painter,
180182

181183
// Thickness for pen is *2 to range from radius to diameter to match the
182184
// ellipse draw function
183-
auto orig_pen = painter.pen();
184-
auto orig_opacity = painter.opacity();
185+
auto const previewWidth = (size() + THICKNESS_OFFSET) * 2;
186+
painter.save();
185187
painter.setOpacity(0.35);
186-
painter.setPen(QPen(context.color,
187-
(size() + THICKNESS_OFFSET) * 2,
188-
Qt::SolidLine,
189-
Qt::RoundCap));
188+
painter.setPen(
189+
QPen(context.color, previewWidth, Qt::SolidLine, Qt::RoundCap));
190190
painter.drawLine(context.mousePos,
191191
{ context.mousePos.x() + 1, context.mousePos.y() + 1 });
192-
painter.setOpacity(orig_opacity);
193-
painter.setPen(orig_pen);
192+
193+
auto font = painter.font();
194+
font.setPixelSize(previewWidth / 2);
195+
font.setBold(true);
196+
painter.setFont(font);
197+
198+
painter.setPen(QPen("white"));
199+
painter.drawText(QRect{ context.mousePos.x() - previewWidth / 2,
200+
context.mousePos.y() - previewWidth / 2,
201+
previewWidth,
202+
previewWidth },
203+
Qt::AlignCenter,
204+
QString::number(context.circleCount));
205+
206+
painter.restore();
207+
}
208+
209+
bool CircleCountTool::handleMouseWheelEvent(int delta,
210+
bool adjustmentButtonPressed,
211+
CaptureContext& ctx)
212+
{
213+
if (adjustmentButtonPressed) {
214+
ctx.circleCount =
215+
qBound(MIN_COUNTER, ctx.circleCount + delta, MAX_COUNTER);
216+
}
217+
return adjustmentButtonPressed;
194218
}
195219

196220
void CircleCountTool::drawStart(const CaptureContext& context)
197221
{
222+
setCount(context.circleCount);
223+
// ------------------------------------------------------------------
224+
// Temporary solution until the circle count variable removed from
225+
// the context altogether and made part of Circle count tool
226+
CaptureContext& ctx = const_cast<CaptureContext&>(context);
227+
ctx.circleCount = qBound(MIN_COUNTER, ctx.circleCount + 1, MAX_COUNTER);
228+
// ------------------------------------------------------------------
198229
AbstractTwoPointTool::drawStart(context);
199230
m_valid = true;
200231
}

src/tools/circlecount/circlecounttool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class CircleCountTool : public AbstractTwoPointTool
2424
void process(QPainter& painter, const QPixmap& pixmap) override;
2525
void paintMousePreview(QPainter& painter,
2626
const CaptureContext& context) override;
27+
bool handleMouseWheelEvent(int, bool, CaptureContext& ctx) override;
2728

2829
protected:
2930
CaptureTool::Type type() const override;

src/widgets/capture/capturewidget.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545

4646
#define MOUSE_DISTANCE_TO_START_MOVING 3
4747

48+
auto const MOUSE_WHEEL_TRESHOLD = 60;
49+
4850
// CaptureWidget is the main component used to capture the screen. It contains
4951
// an area of selection with its respective buttons.
5052

@@ -834,11 +836,6 @@ bool CaptureWidget::startDrawObjectTool(const QPoint& pos)
834836

835837
m_context.mousePos = m_displayGrid ? snapToGrid(pos) : pos;
836838
m_activeTool->drawStart(m_context);
837-
// TODO this is the wrong place to do this
838-
839-
if (m_activeTool->type() == CaptureTool::TYPE_CIRCLECOUNT) {
840-
m_activeTool->setCount(m_context.circleCount++);
841-
}
842839

843840
return true;
844841
}
@@ -1145,12 +1142,16 @@ void CaptureWidget::wheelEvent(QWheelEvent* e)
11451142
* not accept events faster that one in 200ms.
11461143
* */
11471144
int toolSizeOffset = 0;
1148-
if (e->angleDelta().y() >= 60) {
1149-
// mouse scroll (wheel) increment
1150-
toolSizeOffset = 1;
1151-
} else if (e->angleDelta().y() <= -60) {
1152-
// mouse scroll (wheel) decrement
1153-
toolSizeOffset = -1;
1145+
if (qAbs(e->angleDelta().y()) >= MOUSE_WHEEL_TRESHOLD) {
1146+
auto const delta =
1147+
qMax(qMin(e->angleDelta().y() / MOUSE_WHEEL_TRESHOLD, 1), -1);
1148+
if (activeButtonTool() &&
1149+
activeButtonTool()->handleMouseWheelEvent(
1150+
delta, m_adjustmentButtonPressed, m_context)) {
1151+
this->repaint();
1152+
return;
1153+
}
1154+
toolSizeOffset = delta;
11541155
} else {
11551156
// touchpad scroll
11561157
qint64 current = QDateTime::currentMSecsSinceEpoch();

0 commit comments

Comments
 (0)