-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpageview.cpp
194 lines (151 loc) · 5.05 KB
/
pageview.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
License: GPL-2
An electronic filing cabinet: scan, print, stack, arrange
Copyright (C) 2009 Simon Glass, [email protected]
.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
X-Comment: On Debian GNU/Linux systems, the complete text of the GNU General
Public License can be found in the /usr/share/common-licenses/GPL file.
*/
#include <QDebug>
#include <QScrollBar>
#include <QWheelEvent>
#include "desktopmodel.h"
#include "pagedelegate.h"
#include "pageview.h"
Pageview::Pageview (QWidget *parent)
: QListView (parent)
{
setViewMode (IconMode);
setWrapping (true);
setFlow (LeftToRight);
// the user cannot have free movement, but this makes the destination cursor look nice
setMovement (Free);
setResizeMode (Adjust);
setLayoutMode (SinglePass);
setUniformItemSizes (true);
setSelectionRectVisible (true);
setHorizontalScrollMode (QAbstractItemView::ScrollPerPixel);
setVerticalScrollMode (QAbstractItemView::ScrollPerPixel);
setEditTriggers (QAbstractItemView::EditKeyPressed);
setStyleSheet ("QListView { background : lightgray }");
_autoscroll = true; // the user has not scrolled yet
_ignore_scroll = false;
}
Pageview::~Pageview ()
{
}
void Pageview::currentChanged (const QModelIndex &index, const QModelIndex &previous)
{
UNUSED (previous);
emit showInfo (index);
}
void Pageview::allowMove (bool allow)
{
setSelectionMode (ExtendedSelection);
setDropIndicatorShown (false);
setDragDropMode (allow ? InternalMove : NoDragDrop);
setDragEnabled (allow);
}
void Pageview::wheelEvent (QWheelEvent *e)
{
int new_scale = _scale_down;
int step = 1, jump = new_scale;
while (jump >= 12)
{
step *= 2;
jump /= 2;
}
if (e->modifiers () & Qt::ControlModifier)
{
//printf ("delta %d\n", e->delta ());
if (e->angleDelta ().y() < 0)
new_scale += step;
else if (e->angleDelta ().y() > 0 && new_scale > step)
new_scale -= step;
else
e->ignore ();
if (new_scale > 224)
new_scale = 224;
if (new_scale != _scale_down)
emit signalNewScale (new_scale);
}
else
QListView::wheelEvent (e);
}
void Pageview::setScale (int scale_down)
{
_scale_down = scale_down;
}
void Pageview::selectPage (QModelIndex ind)
{
QItemSelectionModel *sel = selectionModel ();
sel->select (QItemSelection (ind, ind),
QItemSelectionModel::Clear | QItemSelectionModel::Select);
scrollTo (ind);
}
void Pageview::scrollToEnd (bool ifAtEnd)
{
// QScrollBar *vs = verticalScrollBar ();
// qDebug () << "scrollToEnd" << _autoscroll;
if (!ifAtEnd || _autoscroll)
{
_ignore_scroll = true;
// doesn't seem to work, perhaps because the window extent hasn't been updated yet
// vs->setValue (vs->maximum ());
// so use this instead
scrollTo (model ()->index (model ()->rowCount (QModelIndex ()) - 1, 0, QModelIndex ()));
_ignore_scroll = false;
}
}
void Pageview::scrollContentsBy (int dx, int dy)
{
/* if we caused the scroll, then don't worry. Otherwise the user is trying
to adjust the scrollbars, so if they are not at the bottom, we turn off
autoscroll */
if (!_ignore_scroll)
{
QScrollBar *vs = verticalScrollBar ();
_autoscroll = vs->value () == vs->maximum ();
// qDebug () << "autoscroll" << _autoscroll;
}
QListView::scrollContentsBy (dx, dy);
}
void Pageview::slotPagePartChanged (const QModelIndex &index,
const QImage &image, int scaled_linenum)
{
const Pagedelegate *del = (Pagedelegate *)itemDelegate ();
QStyleOptionViewItem option = getViewOptions ();
int hvalue = horizontalScrollBar()->value();
int vvalue = verticalScrollBar()->value();
QRect rect = rectForIndex (index);
QRect part_rect;
option.rect = rect;
del->getPagePart (option, index, scaled_linenum, image.height (), part_rect);
part_rect.translate (-hvalue, -vvalue);
QWidget *vp = viewport ();
// qDebug () << "repaint" << part_rect;
// part_rect.setHeight (part_rect.height () - 1); // leave blank line (for testing!)
// vp->repaint (part_rect);
vp->update (part_rect);
}
void Pageview::updateGeometries (void)
{
QScrollBar *vs = verticalScrollBar ();
QScrollBar *hs = horizontalScrollBar ();
QListView::updateGeometries ();
vs->setSingleStep (30);
hs->setSingleStep (30);
}