-
Notifications
You must be signed in to change notification settings - Fork 3
/
linkTool.cpp
165 lines (138 loc) · 5.75 KB
/
linkTool.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
/** This file is part of project comment
*
* File: linkTool.cpp
* Created: 02.05.2010
* 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 "linkTool.h"
#include "toolBox.h"
#include "linkTool.h"
#include "pdfScene.h"
#include "pdfUtil.h"
#include "linkLayer.h"
#include "pageView.h"
#include <QtGui/QTextEdit>
#include <QtGui/QStackedWidget>
#include <QtCore/QDebug>
#include <podofo/podofo.h>
QIcon linkTool::icon;
bool linkTool::acceptEventsFor( QGraphicsItem *item ) {
if ( dynamic_cast<linkAnnotation*>(item) ) {
return true;
}
return false;
}
linkTool::linkTool( pdfScene *Scene, toolBox *ToolBar, QStackedWidget *EditArea ):
abstractTool( Scene, ToolBar, EditArea )
{
setToolName( "Link Tool" );
icon = QIcon::fromTheme("link");
toolBar->addTool( QIcon(icon), this );
}
abstractAnnotation *linkTool::processAnnotation( PoDoFo::PdfDocument *doc, PoDoFo::PdfAnnotation *annotation, pdfCoords *transform ) {
if ( ! linkAnnotation::isA( annotation ) ) return NULL;
try {
return new linkAnnotation( this, annotation, doc, transform );
} catch (...) {
qDebug() << "Error adding annotation";
return NULL;
}
}
void linkTool::newActionEvent( const QPointF *ScenePos ) {
/* qDebug() << "Creating new Link Annotation at " << *ScenePos;
linkAnnotation *annot = new linkAnnotation( this );
annot->setZValue( 10 );
scene->placeAnnotation( annot, ScenePos );
editItem( annot );*/
}
linkAnnotation::linkAnnotation( linkTool* tool, PoDoFo::PdfAnnotation* Link, PoDoFo::PdfDocument *doc, pdfCoords* transform):
abstractAnnotation( tool, Link, transform )
{
movable = false;
PoDoFo::PdfDestination *dest = pdfUtil::getDestination( Link, doc );
if ( ! dest ) throw 5;
dest->GetObject()->SetOwner(Link->GetObject()->GetOwner());
QString tgtName="";
if ( dest->GetObject()->IsString() ) {
tgtName = pdfUtil::pdfStringToQ(dest->GetObject()->GetString());
} else if ( ! dest->GetObject()->IsArray() && dest->GetObject()->GetDictionary().HasKey(PoDoFo::PdfName("comment_target_name")) ) {
tgtName = pdfUtil::pdfStringToQ(dest->GetObject()->GetDictionary().GetKey(PoDoFo::PdfName("comment_target_name"))->GetString());
}
tgt = tool->scene->getLinkTargets()->addTarget( tgtName, dest, doc );
PoDoFo::PdfRect pdfRect = Link->GetRect();
QRectF pgRect = transform->pdfRectToScene( &pdfRect );
activeArea=QRectF(QPointF(0,0),pgRect.size());
setPos(pgRect.topLeft());
setZValue( 10 );
qDebug() << "Created new link from " << pos() << " ["<<activeArea<<"] to " << tgt->pos() << " ["<<tgt->boundingRect()<<"]";
}
bool linkAnnotation::isA( PoDoFo::PdfAnnotation *annotation ) {
if ( ! annotation ) return false;
return ( annotation->GetType() == PoDoFo::ePdfAnnotation_Link );
}
PoDoFo::PdfAnnotation* linkAnnotation::saveToPdfPage( PoDoFo::PdfDocument* document, PoDoFo::PdfPage* pg, pdfCoords* coords ) {
qDebug() << "Saving Link annotation for "<<getAuthor()<<" : " << pos();
PoDoFo::PdfAnnotation *annot = abstractAnnotation::saveToPdfPage( document, pg, coords );
annot->GetObject()->GetDictionary().AddKey( PoDoFo::PdfName("Dest"), pdfUtil::qStringToPdf(tgt->getName()) );
}
void linkAnnotation::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
painter->fillRect( activeArea, QColor( 155, 155, 0, 100 ) );
}
bool linkAnnotation::editSelf() {
linkTool *tool = dynamic_cast<linkTool*>(myTool);
tool->emit_gotoPos( tgt->scenePos() );
}
void linkTool::emit_gotoPos(const QPointF& pos) {
emit gotoPos( pos );
}
bool linkTool::handleEvent(viewEvent* ev) {
linkAnnotation *annot;
if ( ev->type() == viewEvent::VE_MOUSE_PRESS && ( ev->btnCaused() == Qt::LeftButton ) ) {
if ( annot = dynamic_cast<linkAnnotation*>(ev->item()) ) {
//editAnnotationExtent( annot );
} else {
QPointF pos = ev->scenePos();
newActionEvent( &pos );
}
return true;
} else if ( ev->type() == viewEvent::VE_MOUSE_RELEASE && ( ev->btnCaused() == Qt::LeftButton ) ) {
if ( ev->isClick() && (annot = dynamic_cast<linkAnnotation*>(ev->item())) ) {
qDebug() << "Going to " << annot->tgt->getName() << " at " << annot->tgt->scenePos();
emit gotoPos( annot->tgt->scenePos() );
return true;
}
if ( ! (annot=dynamic_cast<linkAnnotation*>(currentEditItem)) ) return false;
//updateCurrentAnnotation( ev->scenePos() );
//editAnnotationText();
return true;
} else if ( ev->type() == viewEvent::VE_MOUSE_MOVE && (annot=dynamic_cast<linkAnnotation*>(currentEditItem)) ) {
/*if ( editingHilight ) {
updateCurrentAnnotation( ev->scenePos() );
if ( ! (ev->btnState() & Qt::LeftButton) ) { // Missed a mouse release, end editing annotation
qDebug() << "WARNING MISSED MOUSE RELEASE EVENT!!!";
editAnnotationText();
}
} else finishEditing();*/
return true;
} else return abstractTool::handleEvent( ev );
return false;
}
#include "linkTool.moc"