forked from jonathanverner/comment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpageView.cpp
320 lines (282 loc) · 9.28 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/** This file is part of project comment
*
* File: pageView.cpp
* Created: 2008-10-12
* Author: Jonathan Verner <[email protected]>
* License: GPL v2 or later
*
* Copyright (C) 2010 Jonathan Verner <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "pageBeginItem.h"
#include "pageView.h"
#include "abstractTool.h"
#include "myToolTip.h"
#include "hiliteItem.h"
#include "pdfScene.h"
#include <QtCore/QDebug>
#include <QtGui/QApplication>
#include <QtGui/QClipboard>
#include <QtGui/QMouseEvent>
#include <QtGui/QKeyEvent>
#include <QtGui/QGraphicsItem>
#include <QtGui/QScrollBar>
#include <QtGui/QMenu>
bool viewEvent::isClick() {
return distance < 10;
};
QAction *pageView::newAction( QString shortCut, QObject *target, const char* slot ) {
return newAction( QKeySequence(shortCut), target, slot );
}
QAction *pageView::newAction( QKeySequence shortCut, QObject *target, const char* slot ) {
QAction *ret = new QAction( this );
ret->setShortcut( shortCut );
ret->setShortcutContext( Qt::WidgetShortcut );
addAction( ret );
connect( ret, SIGNAL( triggered() ), target, slot );
return ret;
}
void pageView::disableActions( bool b) {
qDebug() << "pageView: Disabling actions " << b;
QAction *act;
foreach( act, actions() ) {
act->setDisabled(b);
}
}
pageView::pageView( QGraphicsScene *scene, QWidget *parent ) :
QGraphicsView( scene, parent ), zoom(1), currentPage(1), currentTool(NULL),
movingItem(NULL), toolTipItem(NULL) {
setDragMode( QGraphicsView::ScrollHandDrag );
hi = new hiliteItem();
hi->setColor( QColor(0,0,0,100) );
hi->setZValue( 40 );
scene->addItem(hi);
}
viewEvent pageView::eventToVE( QMouseEvent *e, viewEvent::eventType tp ) {
viewEvent ret;
abstractAnnotation *annot;
ret.distance=0;
ret.viewPort = viewport();
ret.original_event = e;
if ( tp == viewEvent::VE_MOUSE_PRESS ) {
mousePressStartPos = e->pos();
} else if ( tp == viewEvent::VE_MOUSE_MOVE && e->buttons() ) {
QPoint delta = e->pos() - mousePressStartPos;
ret.distance = abs(delta.x())+abs(delta.y());
qDebug() << "Distance:" << ret.distance;
} else if ( tp == viewEvent::VE_MOUSE_RELEASE ) {
QPoint delta = e->pos() - mousePressStartPos;
ret.distance = abs(delta.x())+abs(delta.y());
mousePressStartPos.setX(-1);
mousePressStartPos.setY(-1);
qDebug() << "Total: Distance:" << ret.distance;
}
ret.mousePressPosition=mapToScene(mousePressStartPos);
ret.myType = tp;
ret.SC = scene();
ret.IT=NULL;
QList<QGraphicsItem*> itms = items(e->pos());
for( QList<QGraphicsItem*>::iterator it = itms.begin(); it != itms.end(); ++it ) {
ret.topMostAll = dynamic_cast<abstractAnnotation*>(*it);
if ( ret.topMostAll ) break;
}
ret.lastSP=mapToScene( lastMouseEvPos );
ret.SP=mapToScene( e->pos() );
ret.bt_caused = e->button();
ret.bt_state = e->buttons();
ret.evPos=e->pos();
ret.lastEvPos=lastMouseEvPos;
lastMouseEvPos=e->pos();
if ( currentTool ) {
foreach( QGraphicsItem *i, items( e->pos() ) ) {
if ( currentTool->acceptEventsFor( i ) ) {
ret.IT = i;
break;
}
}
}
return ret;
};
void pageView::setCurrentTool( abstractTool *tool ) {
currentTool = tool;
}
/*void pageView::keyPressEvent( QKeyEvent *e ) {
return;
};*/
void pageView::mouseMoveEvent( QMouseEvent *e ) {
viewEvent viewEv = eventToVE( e, viewEvent::VE_MOUSE_MOVE );
if ( e->pos().y() < 30 || e->pos().y() > (height()-30) ||
e->pos().x() < 30 || e->pos().x() > (width()-30) ) {
emit mouseNearBorder( e->globalPos() );
}
if ( movingItem ) { // moving an item
movingItem->setPos( mapToScene( e->pos() ) - movingItem->scenePos() + movingItem->pos()+moveDelta );
} else if ( currentTool && currentTool->handleEvent( &viewEv ) ) {
return;
} else if ( viewEv.bt_state & Qt::LeftButton ) { // panning the view
QScrollBar *hBar = horizontalScrollBar();
QScrollBar *vBar = verticalScrollBar();
QPoint delta = viewEv.eventDelta();
hBar->setValue(hBar->value() - (isRightToLeft() ? delta.x() : -delta.x()));
vBar->setValue(vBar->value() + delta.y());
/* pdfScene *sc = dynamic_cast<pdfScene*>(scene());
qDebug() << "Currently on Pos:" << hBar->value()<< vBar->value();*/
} else if ( viewEv.bt_state & Qt::RightButton ) {
hi->updateBBoxes( static_cast<pdfScene*>(scene())->selectText( viewEv.mousePressPosition, viewEv.scenePos() ) );
selectedText = static_cast<pdfScene*>(scene())->selectedText( viewEv.mousePressPos(), viewEv.scenePos( ) );
} else { // show/hide tooltips
abstractAnnotation *annot;
bool hide = true;
foreach( QGraphicsItem *i, items( viewEv.evPos ) ) {
if ( annot = dynamic_cast<abstractAnnotation*>( i ) ) {
if ( annot->hasToolTip() ) {
if ( toolTipItem && toolTipItem != annot ) {
qDebug() << "Hiding old tooltip to show new";
toolTipItem->hideToolTip();
} else if ( toolTipItem == annot ) {
qDebug() << "Tooltip Shown, no need to show more";
hide = false;
break;
}
toolTipItem = annot;
toolTipItem->showToolTip( e->globalPos()+QPoint(10,10) );
hide=false;
break;
}
}
}
if ( toolTipItem && hide ) {
myToolTip::hide();
if ( currentTool ) currentTool->hideEditor();
toolTipItem = NULL;
}
}
}
void pageView::mousePressEvent( QMouseEvent *e ) {
viewEvent viewEv = eventToVE( e, viewEvent::VE_MOUSE_PRESS );
viewport()->setCursor(Qt::ClosedHandCursor);
if ( viewEv.bt_state & Qt::LeftButton ) { // check whether we will be moving an item
abstractAnnotation *annot;
foreach( QGraphicsItem *i, items( viewEv.evPos ) ) {
if ( annot = dynamic_cast<abstractAnnotation*>( i ) ) {
if ( annot->isMovable() ) {
qDebug() << " Moving annotation ";
movingItem = i;
moveDelta = annot->scenePos() - viewEv.SP;
return;
}
}
}
if ( currentTool ) currentTool->handleEvent( &viewEv );
} else if ( (viewEv.bt_caused & Qt::RightButton) && viewEv.topMostAll ) { // popup-menu
viewEv.topMostAll->contextMenu()->popup( e->globalPos() );
} else if ( currentTool )
currentTool->handleEvent( &viewEv );
else if ( viewEv.bt_caused & Qt::RightButton ) {
hi->clear();
hi->setPos( static_cast<pdfScene*>(scene())->topLeftPage(static_cast<pdfScene*>(scene())->posToPage( viewEv.scenePos() ) ) );
hi->show();
selectedText = "";
}
}
void pageView::mouseReleaseEvent( QMouseEvent *e ) {
viewport()->setCursor(Qt::OpenHandCursor);
viewEvent viewEv = pageView::eventToVE( e, viewEvent::VE_MOUSE_RELEASE );
movingItem=NULL;
if ( currentTool ) {
if ( currentTool->handleEvent( &viewEv ) ) return;
};
if ( viewEv.isClick() && ( viewEv.bt_caused == Qt::LeftButton ) ) {
abstractAnnotation *annot;
foreach( QGraphicsItem *i, items( viewEv.evPos ) ) {
if ( annot = dynamic_cast<abstractAnnotation*>( i ) ) {
if ( annot->editSelf() ) return;
}
}
}
if ( viewEv.bt_caused == Qt::RightButton ) {
qDebug() << "Selected: " << selectedText;
QApplication::clipboard()->setText( selectedText, QClipboard::Selection );
}
}
void pageView::zoomIN() {
scale ( 1.5, 1.5 );
}
void pageView::zoomOUT() {
scale( 0.7, 0.7 );
}
int pageView::getLastPage() {
int lastPageNum = 0;
pageBeginItem *pg;
foreach( QGraphicsItem *i, items() ) {
if ( pg = dynamic_cast<pageBeginItem *>(i) )
if ( pg->getPageNum() > lastPageNum ) lastPageNum = pg->getPageNum();
}
return lastPageNum;
}
void pageView::gotoPage( int num ) {
pageBeginItem *pg;
foreach( QGraphicsItem *i, items() ) {
if ( pg = dynamic_cast<pageBeginItem *>(i) ) {
if ( pg->getPageNum() == num ) {
currentPage = num;
centerOn( i );
emit onPage( num );
return;
}
}
}
return;
}
void pageView::gotoPoint(const QPointF& point) {
centerOn( point );
//FIXME: Need to emit onPage
}
void pageView::firstPage() {
gotoPage( 1 );
}
void pageView::lastPage() {
gotoPage( getLastPage() );
}
void pageView::nextPage() {
gotoPage( currentPage + 1 );
}
void pageView::prevPage() {
gotoPage( currentPage - 1 );
}
void pageView::up() {
QScrollBar *hBar = horizontalScrollBar();
QScrollBar *vBar = verticalScrollBar();
int delta = 10;
vBar->setValue(vBar->value() - delta);
}
void pageView::down() {
QScrollBar *vBar = verticalScrollBar();
int delta = 10;
vBar->setValue(vBar->value() + delta);
}
void pageView::left() {
QScrollBar *hBar = horizontalScrollBar();
int delta = 10;
hBar->setValue(hBar->value() - delta);
}
void pageView::right() {
QScrollBar *hBar = horizontalScrollBar();
int delta = 10;
hBar->setValue(hBar->value() + delta);
}
#include "pageView.moc"