File indexing completed on 2024-04-21 15:24:03

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 "sketchmodel.h"
0006 
0007 void SketchModel::addStroke(const Stroke &stroke)
0008 {
0009     strokesRef(stroke.type()).append(stroke);
0010 }
0011 
0012 QVector<Stroke> SketchModel::strokes(Stroke::Type type) const
0013 {
0014     return strokesRef(type);
0015 }
0016 
0017 QVector<Stroke> SketchModel::strokes() const
0018 {
0019     return m_fillStrokes + m_outlineStrokes;
0020 }
0021 
0022 void SketchModel::eraseArea(Stroke::Type type, const QVector2D &center, float radius)
0023 {
0024     auto &strokes = strokesRef(type);
0025 
0026     auto it = std::begin(strokes);
0027     auto end = std::end(strokes);
0028 
0029     while (it != end) {
0030         const auto newStrokes = it->eraseArea(center, radius);
0031 
0032         if (newStrokes.size() == 0) {
0033             // Remove
0034             it = strokes.erase(it);
0035             end = std::end(strokes);
0036 
0037         } else if (newStrokes.size() == 1) {
0038             const auto newStroke = newStrokes.first();
0039             if (newStroke.samples() != it->samples()) {
0040                 // Change
0041                 *it = newStroke;
0042             }
0043             it++;
0044 
0045         } else {
0046             // Change + Inserts
0047             *it = newStrokes.first();
0048 
0049             auto newIt = std::cbegin(newStrokes);
0050             const auto newEnd = std::cend(newStrokes);
0051 
0052             newIt++; // Already in the model due to dataChanged above
0053 
0054             it++;
0055 
0056             while (newIt != newEnd) {
0057                 it = strokes.insert(it, *newIt);
0058                 end = std::end(strokes);
0059 
0060                 newIt++;
0061                 it++;
0062             }
0063         }
0064     }
0065 }
0066 
0067 const QVector<Stroke> &SketchModel::strokesRef(Stroke::Type type) const
0068 {
0069     switch (type) {
0070     case Stroke::Type::Fill:
0071         return m_fillStrokes;
0072     case Stroke::Type::Outline:
0073         return m_outlineStrokes;
0074     }
0075     Q_UNREACHABLE();
0076 }
0077 
0078 QVector<Stroke> &SketchModel::strokesRef(Stroke::Type type)
0079 {
0080     switch (type) {
0081     case Stroke::Type::Fill:
0082         return m_fillStrokes;
0083     case Stroke::Type::Outline:
0084         return m_outlineStrokes;
0085     }
0086     Q_UNREACHABLE();
0087 }