File indexing completed on 2024-05-19 12:42:34

0001 /*
0002  * This file is part of the KDE project
0003  *
0004  * SPDX-FileCopyrightText: 2013 Arjen Hiemstra <ahiemstra@heimr.nl>
0005  * SPDX-FileCopyrightText: 2013 Dan Leinir Turthra Jensen <admin@leinir.dk>
0006  *
0007  * SPDX-License-Identifier: LGPL-2.0-or-later
0008  *
0009  */
0010 
0011 #include "LinkArea.h"
0012 
0013 using namespace Calligra::Components;
0014 
0015 class LinkArea::Private
0016 {
0017 public:
0018     Private()
0019         : document{ nullptr }
0020         , controllerZoom(1.f)
0021         , clickInProgress(false)
0022         , wiggleFactor(2)
0023     { }
0024 
0025     Calligra::Components::Document* document;
0026     float controllerZoom;
0027 
0028     bool clickInProgress;
0029     QPoint clickLocation;
0030     int wiggleFactor;
0031 };
0032 
0033 LinkArea::LinkArea(QQuickItem* parent)
0034     : QQuickItem(parent)
0035     , d(new Private)
0036 {
0037     setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton | Qt::MidButton);
0038 }
0039 
0040 LinkArea::~LinkArea()
0041 {
0042     delete d;
0043 }
0044 
0045 Calligra::Components::Document* LinkArea::document() const
0046 {
0047     return d->document;
0048 }
0049 
0050 void LinkArea::setDocument(Calligra::Components::Document* newDocument)
0051 {
0052     if( newDocument != d->document )
0053     {
0054         d->document = newDocument;
0055         emit documentChanged();
0056     }
0057 }
0058 
0059 void LinkArea::mousePressEvent(QMouseEvent* event)
0060 {
0061     d->clickInProgress = true;
0062     d->clickLocation = event->pos();
0063 }
0064 
0065 void LinkArea::mouseReleaseEvent(QMouseEvent* event)
0066 {
0067     if(!d->clickInProgress)
0068         return;
0069     d->clickInProgress = false;
0070 
0071     // Don't activate anything if the finger has moved too far
0072     QRect rect((d->clickLocation - QPointF(d->wiggleFactor, d->wiggleFactor)).toPoint(), QSize(d->wiggleFactor * 2, d->wiggleFactor * 2));
0073     if(!rect.contains(event->pos())) {
0074         return;
0075     }
0076 
0077     QPoint pos = event->pos() / d->controllerZoom;
0078     QUrl url;
0079     if( d->document )
0080         url = d->document->urlAtPoint( pos );
0081 
0082     if(url.isEmpty()) {
0083         emit clicked();
0084     }
0085     else {
0086         emit linkClicked(url);
0087     }
0088     event->accept();
0089 }
0090 
0091 void LinkArea::mouseDoubleClickEvent(QMouseEvent* event)
0092 {
0093     Q_UNUSED(event);
0094     emit doubleClicked();
0095 }
0096 
0097 float LinkArea::controllerZoom() const
0098 {
0099     return d->controllerZoom;
0100 }
0101 
0102 void LinkArea::setControllerZoom(float newZoom)
0103 {
0104     if(d->controllerZoom != newZoom)
0105     {
0106         d->controllerZoom = newZoom;
0107         emit controllerZoomChanged();
0108     }
0109 }