forked from KDE/okular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magnifierview.cpp
175 lines (136 loc) · 4.44 KB
/
magnifierview.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "magnifierview.h"
#include <QPainter>
#include "core/document.h"
#include "core/generator.h"
#include "gui/pagepainter.h"
#include "gui/priorities.h"
static const int SCALE = 10;
MagnifierView::MagnifierView(Okular::Document *document, QWidget *parent)
: QWidget(parent)
, m_document(document)
, m_page(nullptr)
{
document->addObserver(this);
}
MagnifierView::~MagnifierView()
{
m_document->removeObserver(this);
}
void MagnifierView::notifySetup(const QVector<Okular::Page *> &pages, int setupFlags)
{
if (!(setupFlags & Okular::DocumentObserver::DocumentChanged)) {
return;
}
m_pages = pages;
m_page = nullptr;
m_current = -1;
}
void MagnifierView::notifyPageChanged(int page, int flags)
{
Q_UNUSED(page);
Q_UNUSED(flags);
if (isVisible()) {
update();
}
}
bool MagnifierView::canUnloadPixmap(int page) const
{
return (page != m_current);
}
void MagnifierView::notifyCurrentPageChanged(int previous, int current)
{
Q_UNUSED(previous);
if (current != m_current) {
m_current = current;
m_page = m_pages[current];
if (isVisible()) {
requestPixmap();
update();
}
}
}
void MagnifierView::updateView(const Okular::NormalizedPoint &p, const Okular::Page *page)
{
m_viewpoint = p;
if (page != m_page) // ok, we are screwed
{
m_page = page;
m_current = page->number();
}
if (isVisible()) {
requestPixmap();
update();
}
}
void MagnifierView::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e);
QPainter p(this);
if (m_page) {
QRect where = QRect(0, 0, width(), height());
PagePainter::paintCroppedPageOnPainter(&p, m_page, this, 0, m_page->width() * SCALE, m_page->height() * SCALE, where, normalizedView(), nullptr);
}
drawTicks(&p);
}
void MagnifierView::move(int x, int y)
{
QWidget::move(x, y);
requestPixmap();
}
void MagnifierView::requestPixmap()
{
if (!m_page) {
return;
}
const int full_width = m_page->width() * SCALE;
const int full_height = m_page->height() * SCALE;
Okular::NormalizedRect nrect = normalizedView();
if (!m_page->hasPixmap(this, full_width, full_height, nrect)) {
Okular::PixmapRequest *p = new Okular::PixmapRequest(this, m_current, full_width, full_height, devicePixelRatioF(), PAGEVIEW_PRIO, Okular::PixmapRequest::Asynchronous);
if (m_page->hasTilesManager(this)) {
p->setTile(true);
}
// request a little bit bigger rectangle then currently viewed, but not the full scale page
const double rect_width = (nrect.right - nrect.left) * 0.5, rect_height = (nrect.bottom - nrect.top) * 0.5;
const double top = qMax(nrect.top - rect_height, 0.0);
const double bottom = qMin(nrect.bottom + rect_height, 1.0);
const double left = qMax(nrect.left - rect_width, 0.0);
const double right = qMin(nrect.right + rect_width, 1.0);
p->setNormalizedRect(Okular::NormalizedRect(left, top, right, bottom));
m_document->requestPixmaps({p});
}
}
Okular::NormalizedRect MagnifierView::normalizedView() const
{
double h = (double)height() / (SCALE * m_page->height() * 2);
double w = (double)width() / (SCALE * m_page->width() * 2);
return Okular::NormalizedRect(m_viewpoint.x - w, m_viewpoint.y - h, m_viewpoint.x + w, m_viewpoint.y + h);
}
void MagnifierView::drawTicks(QPainter *p)
{
p->save();
p->setPen(QPen(Qt::black, 0));
// the cross
p->drawLine(width() / 2, 0, width() / 2, height() - 1);
p->drawLine(0, height() / 2, width() - 1, height() / 2);
// the borders
p->drawLine(0, 0, width() - 1, 0);
p->drawLine(width() - 1, 0, width() - 1, height() - 1);
p->drawLine(0, height() - 1, width() - 1, height() - 1);
p->drawLine(0, height() - 1, 0, 0);
// ticks
// TODO possibility to switch units (pt, mm, cc, in, printing dots)
float ps = (float)SCALE * 5; // how much pixels in widget is one pixel in document * how often
int tw = 10; // tick size in pixels
for (float x = 0; x < width(); x += ps) {
p->drawLine(x, 1, x, tw);
p->drawLine(x, height() - 1, x, height() - tw - 1);
p->drawLine(1, x, tw, x);
p->drawLine(width() - 1, x, width() - tw - 1, x);
}
p->restore();
}
#include "moc_magnifierview.cpp"