1 /*
2  * This file is part of the KDE project
3  *
4  * Copyright (C) 2013 Arjen Hiemstra <ahiemstra@heimr.nl>
5  * Copyright (C) 2013 Dan Leinir Turthra Jensen <admin@leinir.dk>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  */
23 
24 #include "LinkArea.h"
25 
26 using namespace Calligra::Components;
27 
28 class LinkArea::Private
29 {
30 public:
Private()31     Private()
32         : document{ nullptr }
33         , controllerZoom(1.f)
34         , clickInProgress(false)
35         , wiggleFactor(2)
36     { }
37 
38     Calligra::Components::Document* document;
39     float controllerZoom;
40 
41     bool clickInProgress;
42     QPoint clickLocation;
43     int wiggleFactor;
44 };
45 
LinkArea(QQuickItem * parent)46 LinkArea::LinkArea(QQuickItem* parent)
47     : QQuickItem(parent)
48     , d(new Private)
49 {
50     setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton | Qt::MidButton);
51 }
52 
~LinkArea()53 LinkArea::~LinkArea()
54 {
55     delete d;
56 }
57 
document() const58 Calligra::Components::Document* LinkArea::document() const
59 {
60     return d->document;
61 }
62 
setDocument(Calligra::Components::Document * newDocument)63 void LinkArea::setDocument(Calligra::Components::Document* newDocument)
64 {
65     if( newDocument != d->document )
66     {
67         d->document = newDocument;
68         emit documentChanged();
69     }
70 }
71 
mousePressEvent(QMouseEvent * event)72 void LinkArea::mousePressEvent(QMouseEvent* event)
73 {
74     d->clickInProgress = true;
75     d->clickLocation = event->pos();
76 }
77 
mouseReleaseEvent(QMouseEvent * event)78 void LinkArea::mouseReleaseEvent(QMouseEvent* event)
79 {
80     if(!d->clickInProgress)
81         return;
82     d->clickInProgress = false;
83 
84     // Don't activate anything if the finger has moved too far
85     QRect rect((d->clickLocation - QPointF(d->wiggleFactor, d->wiggleFactor)).toPoint(), QSize(d->wiggleFactor * 2, d->wiggleFactor * 2));
86     if(!rect.contains(event->pos())) {
87         return;
88     }
89 
90     QPoint pos = event->pos() / d->controllerZoom;
91     QUrl url;
92     if( d->document )
93         url = d->document->urlAtPoint( pos );
94 
95     if(url.isEmpty()) {
96         emit clicked();
97     }
98     else {
99         emit linkClicked(url);
100     }
101     event->accept();
102 }
103 
mouseDoubleClickEvent(QMouseEvent * event)104 void LinkArea::mouseDoubleClickEvent(QMouseEvent* event)
105 {
106     Q_UNUSED(event);
107     emit doubleClicked();
108 }
109 
controllerZoom() const110 float LinkArea::controllerZoom() const
111 {
112     return d->controllerZoom;
113 }
114 
setControllerZoom(float newZoom)115 void LinkArea::setControllerZoom(float newZoom)
116 {
117     if(d->controllerZoom != newZoom)
118     {
119         d->controllerZoom = newZoom;
120         emit controllerZoomChanged();
121     }
122 }
123