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
|
/*
* This file is part of the KDE project
*
* SPDX-FileCopyrightText: 2013 Dan Leinir Turthra Jensen <admin@leinir.dk>
*
* SPDX-License-Identifier: LGPL-2.0-or-later
*
*/
#include "CQLinkArea.h"
#include <QMouseEvent>
#include <QPainter>
#include <qquickitem.h>
struct LinkLayerLink {
QRectF linkRect;
QUrl linkTarget;
};
class CQLinkArea::Private
{
public:
Private()
: clickInProgress(false)
, wiggleFactor(4)
{
}
QVariantList links;
QList<LinkLayerLink> realLinks;
bool clickInProgress;
QPointF clickLocation;
int wiggleFactor;
QSizeF sourceSize;
QColor linkColor;
};
CQLinkArea::CQLinkArea(QQuickItem *parent)
: QQuickPaintedItem(parent)
, d(new Private)
{
setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton | Qt::MiddleButton);
setAcceptTouchEvents(true);
}
CQLinkArea::~CQLinkArea() = default;
void CQLinkArea::paint(QPainter *painter)
{
painter->save();
painter->setPen(Qt::transparent);
painter->setBrush(QBrush(d->linkColor));
for (const LinkLayerLink &link : std::as_const(d->realLinks)) {
QRectF target((link.linkRect.y() / d->sourceSize.height()) * height(),
(link.linkRect.x() / d->sourceSize.width()) * width(),
(link.linkRect.height() / d->sourceSize.height()) * height(),
(link.linkRect.width() / d->sourceSize.width()) * width());
painter->drawRect(target);
}
painter->restore();
}
QVariantList CQLinkArea::links() const
{
return d->links;
}
void CQLinkArea::setLinks(const QVariantList &newLinks)
{
d->links = newLinks;
// run through the new data and cache a data list with the information
// so we don't have to interpret the QObjects all the time
d->realLinks.clear();
foreach (const QVariant &var, newLinks) {
QObject *obj = var.value<QObject *>();
if (!obj) {
continue;
}
LinkLayerLink link;
link.linkRect = obj->property("linkRect").toRectF().adjusted(-d->wiggleFactor, -d->wiggleFactor, d->wiggleFactor, d->wiggleFactor);
link.linkTarget = obj->property("linkTarget").toUrl();
d->realLinks.append(link);
}
Q_EMIT linksChanged();
}
QSizeF CQLinkArea::sourceSize() const
{
return d->sourceSize;
}
void CQLinkArea::setSourceSize(const QSizeF &size)
{
if (size != d->sourceSize) {
d->sourceSize = size;
Q_EMIT sourceSizeChanged();
update();
}
}
QColor CQLinkArea::linkColor() const
{
return d->linkColor;
}
void CQLinkArea::setLinkColor(const QColor &color)
{
if (color != d->linkColor) {
d->linkColor = color;
d->linkColor.setAlphaF(0.25);
Q_EMIT linkColorChanged();
update();
}
}
void CQLinkArea::mousePressEvent(QMouseEvent *event)
{
d->clickInProgress = true;
d->clickLocation = event->pos();
}
void CQLinkArea::mouseReleaseEvent(QMouseEvent *event)
{
d->clickInProgress = false;
// Don't activate anything if the finger has moved too far
QRect rect((d->clickLocation - QPointF(d->wiggleFactor, d->wiggleFactor)).toPoint(), QSize(d->wiggleFactor * 2, d->wiggleFactor * 2));
if (!rect.contains(event->pos())) {
return;
}
QUrl url;
QPointF inverted(event->pos().y(), event->pos().x());
foreach (const LinkLayerLink &link, d->realLinks) {
QRectF scaledTarget((link.linkRect.x() / d->sourceSize.width()) * width(),
(link.linkRect.y() / d->sourceSize.height()) * height(),
(link.linkRect.width() / d->sourceSize.width()) * width(),
(link.linkRect.height() / d->sourceSize.height()) * height());
if (scaledTarget.contains(inverted)) {
url = link.linkTarget;
break;
}
}
if (url.isEmpty()) {
Q_EMIT clicked();
} else {
Q_EMIT linkClicked(url);
}
}
void CQLinkArea::mouseDoubleClickEvent(QMouseEvent *event)
{
Q_UNUSED(event)
Q_EMIT doubleClicked();
}
|