| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
#include "arthurwidgets.h" |
| 5 |
#include "hoverpoints.h" |
| 6 |
|
| 7 |
#include <algorithm> |
| 8 |
|
| 9 |
HoverPoints::HoverPoints(QWidget *widget, PointShape shape) |
| 10 |
: QObject(widget), |
| 11 |
m_widget(widget), |
| 12 |
m_shape(shape) |
| 13 |
{ |
| 14 |
widget->installEventFilter(this); |
| 15 |
widget->setAttribute(Qt::WA_AcceptTouchEvents); |
| 16 |
|
| 17 |
connect(this, &HoverPoints::pointsChanged, |
| 18 |
m_widget, QOverload<>::of(&QWidget::update)); |
| 19 |
} |
| 20 |
|
| 21 |
void HoverPoints::setEnabled(bool enabled) |
| 22 |
{ |
| 23 |
if (m_enabled != enabled) { |
| 24 |
m_enabled = enabled; |
| 25 |
m_widget->update(); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
bool HoverPoints::eventFilter(QObject *object, QEvent *event) |
| 30 |
{ |
| 31 |
if (object != m_widget || !m_enabled) |
| 32 |
return false; |
| 33 |
|
| 34 |
switch (event->type()) { |
| 35 |
case QEvent::MouseButtonPress: |
| 36 |
{ |
| 37 |
if (!m_fingerPointMapping.isEmpty()) |
| 38 |
return true; |
| 39 |
auto *me = static_cast<const QMouseEvent *>(event); |
| 40 |
QPointF clickPos = me->position().toPoint(); |
| 41 |
qsizetype index = -1; |
| 42 |
for (qsizetype i = 0; i < m_points.size(); ++i) { |
| 43 |
QPainterPath path; |
| 44 |
const QRectF rect = pointBoundingRect(m_points.at(i)); |
| 45 |
if (m_shape == CircleShape) |
| 46 |
path.addEllipse(rect); |
| 47 |
else |
| 48 |
path.addRect(rect); |
| 49 |
|
| 50 |
if (path.contains(clickPos)) { |
| 51 |
index = i; |
| 52 |
break; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
if (me->button() == Qt::LeftButton) { |
| 57 |
if (index == -1) { |
| 58 |
if (!m_editable) |
| 59 |
return false; |
| 60 |
qsizetype pos = 0; |
| 61 |
// Insert sort for x or y |
| 62 |
switch (m_sortType) { |
| 63 |
case XSort: |
| 64 |
for (qsizetype i = 0; i < m_points.size(); ++i) { |
| 65 |
if (m_points.at(i).x() > clickPos.x()) { |
| 66 |
pos = i; |
| 67 |
break; |
| 68 |
} |
| 69 |
} |
| 70 |
break; |
| 71 |
case YSort: |
| 72 |
for (qsizetype i = 0; i < m_points.size(); ++i) { |
| 73 |
if (m_points.at(i).y() > clickPos.y()) { |
| 74 |
pos = i; |
| 75 |
break; |
| 76 |
} |
| 77 |
} |
| 78 |
break; |
| 79 |
default: |
| 80 |
break; |
| 81 |
} |
| 82 |
|
| 83 |
m_points.insert(pos, clickPos); |
| 84 |
m_locks.insert(pos, 0); |
| 85 |
m_currentIndex = pos; |
| 86 |
firePointChange(); |
| 87 |
} else { |
| 88 |
m_currentIndex = index; |
| 89 |
} |
| 90 |
return true; |
| 91 |
|
| 92 |
} else if (me->button() == Qt::RightButton) { |
| 93 |
if (index >= 0 && m_editable) { |
| 94 |
if (m_locks[index] == 0) { |
| 95 |
m_locks.remove(index); |
| 96 |
m_points.remove(index); |
| 97 |
} |
| 98 |
firePointChange(); |
| 99 |
return true; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
break; |
| 104 |
|
| 105 |
case QEvent::MouseButtonRelease: |
| 106 |
if (!m_fingerPointMapping.isEmpty()) |
| 107 |
return true; |
| 108 |
m_currentIndex = -1; |
| 109 |
break; |
| 110 |
|
| 111 |
case QEvent::MouseMove: |
| 112 |
if (!m_fingerPointMapping.isEmpty()) |
| 113 |
return true; |
| 114 |
if (m_currentIndex >= 0) { |
| 115 |
auto *me = static_cast<const QMouseEvent *>(event); |
| 116 |
movePoint(m_currentIndex, me->position().toPoint()); |
| 117 |
} |
| 118 |
break; |
| 119 |
|
| 120 |
case QEvent::TouchBegin: |
| 121 |
case QEvent::TouchUpdate: |
| 122 |
{ |
| 123 |
auto *touchEvent = static_cast<const QTouchEvent*>(event); |
| 124 |
const auto points = touchEvent->points(); |
| 125 |
const qreal pointSize = qMax(m_pointSize.width(), m_pointSize.height()); |
| 126 |
for (const auto &point : points) { |
| 127 |
const int id = point.id(); |
| 128 |
switch (point.state()) { |
| 129 |
case QEventPoint::Pressed: |
| 130 |
case QEventPoint::Stationary: |
| 131 |
{ |
| 132 |
// find the point, move it |
| 133 |
const auto mappedPoints = m_fingerPointMapping.values(); |
| 134 |
QSet<qsizetype> activePoints(mappedPoints.begin(), mappedPoints.end()); |
| 135 |
qsizetype activePoint = -1; |
| 136 |
qreal distance = -1; |
| 137 |
const qsizetype pointsCount = m_points.size(); |
| 138 |
const qsizetype activePointCount = activePoints.size(); |
| 139 |
if (pointsCount == 2 && activePointCount == 1) { // only two points |
| 140 |
activePoint = activePoints.contains(0) ? 1 : 0; |
| 141 |
} else { |
| 142 |
for (qsizetype i = 0; i < pointsCount; ++i) { |
| 143 |
if (activePoints.contains(i)) |
| 144 |
continue; |
| 145 |
|
| 146 |
qreal d = QLineF(point.position(), m_points.at(i)).length(); |
| 147 |
if ((distance < 0 && d < 12 * pointSize) || d < distance) { |
| 148 |
distance = d; |
| 149 |
activePoint = i; |
| 150 |
} |
| 151 |
|
| 152 |
} |
| 153 |
} |
| 154 |
if (activePoint != -1) { |
| 155 |
m_fingerPointMapping.insert(id, activePoint); |
| 156 |
movePoint(activePoint, point.position()); |
| 157 |
} |
| 158 |
} |
| 159 |
break; |
| 160 |
case QEventPoint::Released: |
| 161 |
{ |
| 162 |
// move the point and release |
| 163 |
const auto it = m_fingerPointMapping.constFind(id); |
| 164 |
if (it != m_fingerPointMapping.constEnd()) { |
| 165 |
movePoint(it.value(), point.position()); |
| 166 |
m_fingerPointMapping.erase(it); |
| 167 |
} |
| 168 |
} |
| 169 |
break; |
| 170 |
case QEventPoint::Updated: |
| 171 |
{ |
| 172 |
// move the point |
| 173 |
const qsizetype pointIdx = m_fingerPointMapping.value(id, -1); |
| 174 |
if (pointIdx >= 0) // do we track this point? |
| 175 |
movePoint(pointIdx, point.position()); |
| 176 |
} |
| 177 |
break; |
| 178 |
default: |
| 179 |
break; |
| 180 |
} |
| 181 |
} |
| 182 |
if (m_fingerPointMapping.isEmpty()) { |
| 183 |
event->ignore(); |
| 184 |
return false; |
| 185 |
} |
| 186 |
return true; |
| 187 |
} |
| 188 |
case QEvent::TouchEnd: |
| 189 |
if (m_fingerPointMapping.isEmpty()) { |
| 190 |
event->ignore(); |
| 191 |
return false; |
| 192 |
} |
| 193 |
return true; |
| 194 |
|
| 195 |
case QEvent::Resize: |
| 196 |
{ |
| 197 |
auto *e = static_cast<const QResizeEvent *>(event); |
| 198 |
if (e->oldSize().width() <= 0 || e->oldSize().height() <= 0) |
| 199 |
break; |
| 200 |
qreal stretch_x = e->size().width() / qreal(e->oldSize().width()); |
| 201 |
qreal stretch_y = e->size().height() / qreal(e->oldSize().height()); |
| 202 |
for (qsizetype i = 0; i < m_points.size(); ++i) { |
| 203 |
QPointF p = m_points.at(i); |
| 204 |
movePoint(i, QPointF(p.x() * stretch_x, p.y() * stretch_y), false); |
| 205 |
} |
| 206 |
|
| 207 |
firePointChange(); |
| 208 |
break; |
| 209 |
} |
| 210 |
|
| 211 |
case QEvent::Paint: |
| 212 |
{ |
| 213 |
QWidget *that_widget = m_widget; |
| 214 |
m_widget = nullptr; |
| 215 |
QCoreApplication::sendEvent(object, event); |
| 216 |
m_widget = that_widget; |
| 217 |
paintPoints(); |
| 218 |
return true; |
| 219 |
} |
| 220 |
|
| 221 |
default: |
| 222 |
break; |
| 223 |
} |
| 224 |
|
| 225 |
return false; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
void HoverPoints::paintPoints() |
| 230 |
{ |
| 231 |
QPainter p; |
| 232 |
p.begin(m_widget); |
| 233 |
|
| 234 |
p.setRenderHint(QPainter::Antialiasing); |
| 235 |
|
| 236 |
if (m_connectionPen.style() != Qt::NoPen && m_connectionType != NoConnection) { |
| 237 |
p.setPen(m_connectionPen); |
| 238 |
|
| 239 |
if (m_connectionType == CurveConnection) { |
| 240 |
QPainterPath path; |
| 241 |
path.moveTo(m_points.at(0)); |
| 242 |
for (qsizetype i = 1; i < m_points.size(); ++i) { |
| 243 |
QPointF p1 = m_points.at(i - 1); |
| 244 |
QPointF p2 = m_points.at(i); |
| 245 |
qreal distance = p2.x() - p1.x(); |
| 246 |
|
| 247 |
path.cubicTo(p1.x() + distance / 2, p1.y(), |
| 248 |
p1.x() + distance / 2, p2.y(), |
| 249 |
p2.x(), p2.y()); |
| 250 |
} |
| 251 |
p.drawPath(path); |
| 252 |
} else { |
| 253 |
p.drawPolyline(m_points); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
p.setPen(m_pointPen); |
| 258 |
p.setBrush(m_pointBrush); |
| 259 |
|
| 260 |
for (const auto &point : std::as_const(m_points)) { |
| 261 |
QRectF bounds = pointBoundingRect(point); |
| 262 |
if (m_shape == CircleShape) |
| 263 |
p.drawEllipse(bounds); |
| 264 |
else |
| 265 |
p.drawRect(bounds); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
static QPointF bound_point(const QPointF &point, const QRectF &bounds, int lock) |
| 270 |
{ |
| 271 |
QPointF p = point; |
| 272 |
|
| 273 |
qreal left = bounds.left(); |
| 274 |
qreal right = bounds.right(); |
| 275 |
qreal top = bounds.top(); |
| 276 |
qreal bottom = bounds.bottom(); |
| 277 |
|
| 278 |
if (p.x() < left || (lock & HoverPoints::LockToLeft)) p.setX(left); |
| 279 |
else if (p.x() > right || (lock & HoverPoints::LockToRight)) p.setX(right); |
| 280 |
|
| 281 |
if (p.y() < top || (lock & HoverPoints::LockToTop)) p.setY(top); |
| 282 |
else if (p.y() > bottom || (lock & HoverPoints::LockToBottom)) p.setY(bottom); |
| 283 |
|
| 284 |
return p; |
| 285 |
} |
| 286 |
|
| 287 |
void HoverPoints::setPoints(const QPolygonF &points) |
| 288 |
{ |
| 289 |
if (points.size() != m_points.size()) |
| 290 |
m_fingerPointMapping.clear(); |
| 291 |
m_points.clear(); |
| 292 |
for (qsizetype i = 0; i < points.size(); ++i) |
| 293 |
m_points << bound_point(points.at(i), boundingRect(), 0); |
| 294 |
|
| 295 |
m_locks.clear(); |
| 296 |
if (m_points.size() > 0) { |
| 297 |
m_locks.resize(m_points.size()); |
| 298 |
m_locks.fill(0); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
void HoverPoints::movePoint(qsizetype index, const QPointF &point, bool emitUpdate) |
| 303 |
{ |
| 304 |
m_points[index] = bound_point(point, boundingRect(), m_locks.at(index)); |
| 305 |
if (emitUpdate) |
| 306 |
firePointChange(); |
| 307 |
} |
| 308 |
|
| 309 |
inline static bool x_less_than(const QPointF &p1, const QPointF &p2) |
| 310 |
{ |
| 311 |
return p1.x() < p2.x(); |
| 312 |
} |
| 313 |
|
| 314 |
inline static bool y_less_than(const QPointF &p1, const QPointF &p2) |
| 315 |
{ |
| 316 |
return p1.y() < p2.y(); |
| 317 |
} |
| 318 |
|
| 319 |
void HoverPoints::firePointChange() |
| 320 |
{ |
| 321 |
if (m_sortType != NoSort) { |
| 322 |
|
| 323 |
QPointF oldCurrent; |
| 324 |
if (m_currentIndex != -1) |
| 325 |
oldCurrent = m_points[m_currentIndex]; |
| 326 |
|
| 327 |
if (m_sortType == XSort) |
| 328 |
std::sort(m_points.begin(), m_points.end(), x_less_than); |
| 329 |
else if (m_sortType == YSort) |
| 330 |
std::sort(m_points.begin(), m_points.end(), y_less_than); |
| 331 |
|
| 332 |
// Compensate for changed order... |
| 333 |
if (m_currentIndex != -1) { |
| 334 |
for (qsizetype i = 0; i < m_points.size(); ++i) { |
| 335 |
if (m_points[i] == oldCurrent) { |
| 336 |
m_currentIndex = i; |
| 337 |
break; |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
emit pointsChanged(m_points); |
| 344 |
} |