File indexing completed on 2024-04-28 15:52:32

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 "strokelistitem.h"
0006 
0007 #include <QPainter>
0008 
0009 #include "sketchmodel.h"
0010 #include "strokepainter.h"
0011 
0012 StrokeListItem::StrokeListItem(QQuickItem *parent)
0013     : QQuickPaintedItem(parent)
0014 {
0015     setAntialiasing(true);
0016     setRenderTarget(FramebufferObject);
0017 }
0018 
0019 void StrokeListItem::addStroke(const Stroke &stroke)
0020 {
0021     Q_ASSERT(m_model);
0022     Q_ASSERT(stroke.type() == m_type);
0023     m_model->addStroke(stroke);
0024     update();
0025 }
0026 
0027 void StrokeListItem::eraseArea(const QVector2D &center, float radius)
0028 {
0029     Q_ASSERT(m_model);
0030     m_model->eraseArea(m_type, center, radius);
0031     update();
0032 }
0033 
0034 void StrokeListItem::paint(QPainter *painter)
0035 {
0036     const auto strokes = m_model->strokes(m_type);
0037     for (const auto &stroke : qAsConst(strokes))
0038         StrokePainter::render(stroke, painter);
0039 }
0040 
0041 Stroke::Type StrokeListItem::type() const
0042 {
0043     return m_type;
0044 }
0045 
0046 SketchModel *StrokeListItem::model() const
0047 {
0048     return m_model;
0049 }
0050 
0051 void StrokeListItem::setType(Stroke::Type type)
0052 {
0053     if (m_type == type)
0054         return;
0055 
0056     m_type = type;
0057     emit typeChanged(type);
0058     update();
0059 }
0060 
0061 void StrokeListItem::setModel(SketchModel *model)
0062 {
0063     if (m_model == model)
0064         return;
0065 
0066     m_model = model;
0067     emit modelChanged(model);
0068     update();
0069 }