1 /*
2 dspdfviewer - Dual Screen PDF Viewer for LaTeX-Beamer
3 Copyright (C) 2012 Danny Edel <mail@danny-edel.de>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20
21 #include "hyperlinkarea.h"
22 #include <stdexcept>
23 #include <cmath>
24 #include <boost/math/special_functions/round.hpp>
25 #include "debug.h"
26
27 using boost::math::iround;
28
HyperlinkArea(QLabel * imageLabel,const AdjustedLink & link)29 HyperlinkArea::HyperlinkArea(QLabel* imageLabel, const AdjustedLink& link): QLabel(), targetPage(link.targetPageNumber())
30 {
31 if ( link.linkType() != Poppler::Link::Goto )
32 throw WrongLinkType();
33 QRect mySize;
34 const QPixmap* pixmap = imageLabel->pixmap();
35 if ( pixmap == nullptr )
36 throw /** FIXME Exception **/ std::runtime_error("Tried to construct a HyperlinkArea from an image label without a pixmap");
37
38 QRectF sizeWithinImageLabel = link.linkArea();
39
40 mySize.setTop( iround(sizeWithinImageLabel.top() * pixmap->height()) );
41 mySize.setLeft( iround(sizeWithinImageLabel.left() * pixmap->width()) );
42
43 mySize.setHeight(std::abs( iround(sizeWithinImageLabel.height() * pixmap->height())) );
44 mySize.setWidth( iround(sizeWithinImageLabel.width() * pixmap->width()) );
45
46 setParent(imageLabel);
47 setGeometry(mySize);
48
49 /*
50 setAutoFillBackground(true);
51 QPalette pal = palette();
52 pal.setColor(QPalette::Window, QColor(Qt::black) );
53 setPalette(pal);
54
55 */
56
57 // setText( QString("%1 @ +%2,%3").arg(targetPage).arg( link.linkArea().right() ). arg(link.linkArea().top()) );
58
59 show();
60
61
62 setCursor( Qt::PointingHandCursor );
63
64
65 DEBUGOUT << "Added an hyperlink to" << link.targetPageNumber() << "at" << geometry();
66 }
67
68
mousePressEvent(QMouseEvent * ev)69 void HyperlinkArea::mousePressEvent(QMouseEvent* ev)
70 {
71 DEBUGOUT << "Hyperlink clicked" << ev << "Target page" << targetPage;
72
73 emit gotoPageRequested(targetPage);
74 }
75