File indexing completed on 2025-02-02 04:54:38
0001 // SPDX-License-Identifier: GPL-2.0-or-later 0002 // SPDX-FileCopyrightText: 2018 Kevin Ottens <ervin@kde.org> 0003 0004 /* 0005 * Adapted by Louis Schul <schul9louis@gmail.com> 0006 * in 2023 for Klevernotes 0007 */ 0008 0009 #include "sketchview.h" 0010 0011 #include <QPointingDevice> 0012 0013 Q_GLOBAL_STATIC(SketchView *, s_instance); 0014 0015 SketchViewHandler::SketchViewHandler(QObject *parent) 0016 : QObject(parent) 0017 { 0018 connect(SketchView::instance(), &SketchView::tabletEventReceived, this, &SketchViewHandler::onTabletEventReceived); 0019 } 0020 0021 Stroke SketchViewHandler::createStroke(Stroke::Type type, const QColor &color) const 0022 { 0023 auto result = Stroke{}; 0024 result.setType(type); 0025 result.setColor(color); 0026 return result; 0027 } 0028 0029 StrokeSample SketchViewHandler::createSample(const QVector2D &position, float width) const 0030 { 0031 StrokeSample sample; 0032 sample.position = position; 0033 sample.width = width; 0034 return sample; 0035 } 0036 0037 Event SketchViewHandler::point() const 0038 { 0039 return m_point; 0040 } 0041 0042 bool SketchViewHandler::isPressed() const 0043 { 0044 return m_pressed; 0045 } 0046 0047 void SketchViewHandler::onTabletEventReceived(QTabletEvent *event) 0048 { 0049 m_point = TabletEvent::create(event); 0050 Q_EMIT pointChanged(m_point); 0051 0052 int pointerIndex = (event->pointerType() == QPointingDevice::PointerType::Eraser) ? 1 : 0; 0053 changePointer(pointerIndex); 0054 0055 if (event->type() == QEvent::TabletPress && !m_pressed) { 0056 m_pressed = true; 0057 Q_EMIT pressedChanged(m_pressed); 0058 } else if (event->type() == QEvent::TabletRelease && m_pressed) { 0059 m_pressed = false; 0060 Q_EMIT pressedChanged(m_pressed); 0061 } 0062 } 0063 0064 void SketchViewHandler::mouseMoved(const float x, const float y) 0065 { 0066 m_point = MouseEvent::create(x, y); 0067 Q_EMIT pointChanged(m_point); 0068 return; 0069 } 0070 0071 void SketchViewHandler::changeMousePress(bool pressed) 0072 { 0073 m_pressed = pressed; 0074 Q_EMIT pressedChanged(pressed); 0075 } 0076 0077 void SketchViewHandler::changePointer(const int pointerIndex) 0078 { 0079 m_pointerType = Pointer(pointerIndex); 0080 Q_EMIT isEraserChanged(m_pointerType == Pointer::Eraser); 0081 } 0082 0083 SketchView *SketchView::instance() 0084 { 0085 return *s_instance; 0086 } 0087 0088 SketchView::SketchView(QWindow *parent) 0089 : QQuickView(parent) 0090 { 0091 if (*s_instance) 0092 qFatal("There must be only one SketchView instance"); 0093 0094 *s_instance = this; 0095 } 0096 0097 void SketchView::tabletEvent(QTabletEvent *event) 0098 { 0099 event->accept(); 0100 0101 if (m_lastType == event->type()) { 0102 const QPointF eventPos = event->globalPosition(); 0103 const auto length = (eventPos - m_lastGlobalPos).manhattanLength(); 0104 constexpr auto lengthThreshold = 4.0; 0105 0106 if (length < lengthThreshold) 0107 return; 0108 } 0109 0110 m_lastType = event->type(); 0111 m_lastGlobalPos = event->globalPosition(); 0112 Q_EMIT tabletEventReceived(event); 0113 }