File indexing completed on 2024-04-28 16:13:35

0001 /*
0002  * This file is part of the KDE project
0003  *
0004  * Copyright (C) 2013 Arjen Hiemstra <ahiemstra@heimr.nl>
0005  * Copyright (C) 2013 Dan Leinir Turthra Jensen <admin@leinir.dk>
0006  *
0007  * This library is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU Library General Public
0009  * License as published by the Free Software Foundation; either
0010  * version 2 of the License, or (at your option) any later version.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Library General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Library General Public License
0018  * along with this library; see the file COPYING.LIB.  If not, write to
0019  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020  * Boston, MA 02110-1301, USA.
0021  *
0022  */
0023 
0024 #include "LinkArea.h"
0025 
0026 using namespace Calligra::Components;
0027 
0028 class LinkArea::Private
0029 {
0030 public:
0031     Private()
0032         : document{ nullptr }
0033         , controllerZoom(1.f)
0034         , clickInProgress(false)
0035         , wiggleFactor(2)
0036     { }
0037 
0038     Calligra::Components::Document* document;
0039     float controllerZoom;
0040 
0041     bool clickInProgress;
0042     QPoint clickLocation;
0043     int wiggleFactor;
0044 };
0045 
0046 LinkArea::LinkArea(QQuickItem* parent)
0047     : QQuickItem(parent)
0048     , d(new Private)
0049 {
0050     setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton | Qt::MidButton);
0051 }
0052 
0053 LinkArea::~LinkArea()
0054 {
0055     delete d;
0056 }
0057 
0058 Calligra::Components::Document* LinkArea::document() const
0059 {
0060     return d->document;
0061 }
0062 
0063 void LinkArea::setDocument(Calligra::Components::Document* newDocument)
0064 {
0065     if( newDocument != d->document )
0066     {
0067         d->document = newDocument;
0068         emit documentChanged();
0069     }
0070 }
0071 
0072 void LinkArea::mousePressEvent(QMouseEvent* event)
0073 {
0074     d->clickInProgress = true;
0075     d->clickLocation = event->pos();
0076 }
0077 
0078 void LinkArea::mouseReleaseEvent(QMouseEvent* event)
0079 {
0080     if(!d->clickInProgress)
0081         return;
0082     d->clickInProgress = false;
0083 
0084     // Don't activate anything if the finger has moved too far
0085     QRect rect((d->clickLocation - QPointF(d->wiggleFactor, d->wiggleFactor)).toPoint(), QSize(d->wiggleFactor * 2, d->wiggleFactor * 2));
0086     if(!rect.contains(event->pos())) {
0087         return;
0088     }
0089 
0090     QPoint pos = event->pos() / d->controllerZoom;
0091     QUrl url;
0092     if( d->document )
0093         url = d->document->urlAtPoint( pos );
0094 
0095     if(url.isEmpty()) {
0096         emit clicked();
0097     }
0098     else {
0099         emit linkClicked(url);
0100     }
0101     event->accept();
0102 }
0103 
0104 void LinkArea::mouseDoubleClickEvent(QMouseEvent* event)
0105 {
0106     Q_UNUSED(event);
0107     emit doubleClicked();
0108 }
0109 
0110 float LinkArea::controllerZoom() const
0111 {
0112     return d->controllerZoom;
0113 }
0114 
0115 void LinkArea::setControllerZoom(float newZoom)
0116 {
0117     if(d->controllerZoom != newZoom)
0118     {
0119         d->controllerZoom = newZoom;
0120         emit controllerZoomChanged();
0121     }
0122 }