Warning, file /graphics/washipad/src/sketchview.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of Washi Pad
0002 // SPDX-FileCopyrightText: 2018 Kevin Ottens <ervin@kde.org>
0003 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 
0005 #include "sketchview.h"
0006 
0007 Q_GLOBAL_STATIC(SketchView*, s_instance);
0008 
0009 SketchViewHandler::SketchViewHandler(QObject *parent)
0010     : QObject(parent)
0011 {
0012     connect(SketchView::instance(), &SketchView::tabletEventReceived,
0013             this, &SketchViewHandler::onTabletEventReceived);
0014 }
0015 
0016 Stroke SketchViewHandler::createStroke(Stroke::Type type, const QColor &color) const
0017 {
0018     auto result = Stroke{};
0019     result.setType(type);
0020     result.setColor(color);
0021     return result;
0022 }
0023 
0024 StrokeSample SketchViewHandler::createSample(const QVector2D &position, float width) const
0025 {
0026     return {position, width};
0027 }
0028 
0029 Event SketchViewHandler::point() const
0030 {
0031     return m_point;
0032 }
0033 
0034 bool SketchViewHandler::isPressed() const
0035 {
0036     return m_pressed;
0037 }
0038 
0039 void SketchViewHandler::onTabletEventReceived(QTabletEvent *event)
0040 {
0041     m_point = TabletEvent::create(event);
0042     emit pointChanged(m_point);
0043 
0044     if (event->type() == QEvent::TabletPress && !m_pressed) {
0045         m_pressed = true;
0046         emit pressedChanged(m_pressed);
0047     } else if (event->type() == QEvent::TabletRelease && m_pressed) {
0048         m_pressed = false;
0049         emit pressedChanged(m_pressed);
0050     }
0051 }
0052 
0053 void SketchViewHandler::setPressed(const bool pressed)
0054 {
0055     if (m_pressed == pressed) {
0056         return;
0057     }
0058     m_pressed = pressed;
0059     emit pressedChanged(m_pressed);
0060 }
0061 
0062 void SketchViewHandler::mouseMoved(const float x, const float y, const int button)
0063 {
0064     m_point = MouseEvent::create(x, y, button);
0065     emit pointChanged(m_point);
0066 }
0067 
0068 
0069 SketchView *SketchView::instance()
0070 {
0071     return *s_instance;
0072 }
0073 
0074 SketchView::SketchView(QWindow *parent)
0075     : QQuickView(parent)
0076 {
0077     if (*s_instance)
0078         qFatal("There must be only one SketchView instance");
0079 
0080     *s_instance = this;
0081 }
0082 
0083 void SketchView::tabletEvent(QTabletEvent *event)
0084 {
0085     event->accept();
0086 
0087     if (m_lastType == event->type()) {
0088         const auto length = (event->globalPos() - m_lastGlobalPos).manhattanLength();
0089         constexpr auto lengthThreshold =  4.0;
0090 
0091         if (length < lengthThreshold)
0092             return;
0093     }
0094 
0095     m_lastType = event->type();
0096     m_lastGlobalPos = event->globalPos();
0097     emit tabletEventReceived(event);
0098 }