-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagnifier.cpp
More file actions
54 lines (46 loc) · 1.76 KB
/
magnifier.cpp
File metadata and controls
54 lines (46 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// SPDX-License-Identifier: GPL-2.0
#include "magnifier.hpp"
#include "scene.hpp"
#include <QPainter>
Magnifier::Magnifier(QWidget *parent)
: QLabel(parent)
, dpr(parent->devicePixelRatio())
, total_size_fixed(static_cast<size_t>(round(total_size * dpr)))
, stamp(total_size_fixed, total_size_fixed)
{
stamp.setDevicePixelRatio(dpr);
stamp.fill(Qt::transparent);
QPainter painter(&stamp);
painter.setBrush(Qt::SolidPattern);
painter.drawEllipse(0, 0, total_size, total_size);
setAttribute(Qt::WA_TransparentForMouseEvents, true);
setFixedSize(QSize(total_size, total_size));
setPixmap(QPixmap(total_size, total_size));
}
void Magnifier::go(Scene *scene, const QPointF &scene_pos)
{
QGraphicsView *view = scene->get_view();
// Generate zoomed bitmap
QPoint relative_pos = view->mapFromScene(scene_pos);
QRect unzoomed_rect(relative_pos - QPoint(size / 2, size / 2), QSize(size, size));
QPixmap unzoomed = view->grab(unzoomed_rect);
QPixmap zoomed = unzoomed.scaled(QSize(total_size_fixed, total_size_fixed), Qt::IgnoreAspectRatio, Qt::FastTransformation);
// Stamp out disk
QPixmap stamped = stamp;
QPainter painter(&stamped);
painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
painter.drawPixmap(0, 0, zoomed, 0, 0, total_size_fixed, total_size_fixed);
// Paint circle around disk
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
painter.setBrush(Qt::NoBrush);
QPen pen(Qt::red);
pen.setWidth(border_width);
painter.setPen(pen);
painter.drawEllipse(border_width, border_width, total_size - border_width, total_size - border_width);
// Calculate position in parent widget
relative_pos -= QPoint(half_size, half_size);
QPoint window_pos = view->mapToParent(relative_pos);
setPixmap(stamped);
move(window_pos);
setVisible(true);
}