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