File indexing completed on 2024-12-22 04:13:03

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Alvin Wong <alvinhochun-at-gmail-com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef KIS_STABILIZER_DELAYED_PAINT_HELPER_H
0008 #define KIS_STABILIZER_DELAYED_PAINT_HELPER_H
0009 
0010 #include <QElapsedTimer>
0011 #include <QQueue>
0012 #include <QTimer>
0013 #include <QVector>
0014 
0015 #include <functional>
0016 
0017 #include "kis_paint_information.h"
0018 #include "kritaui_export.h"
0019 
0020 class KRITAUI_EXPORT KisStabilizerDelayedPaintHelper : public QObject
0021 {
0022     Q_OBJECT
0023 
0024     struct TimedPaintInfo
0025     {
0026         int elapsedTime;
0027         KisPaintInformation paintInfo;
0028         TimedPaintInfo(int elapsedTime, KisPaintInformation paintInfo);
0029     };
0030 
0031     QTimer m_paintTimer;
0032     QQueue<TimedPaintInfo> m_paintQueue;
0033     int m_lastPendingTime {0};
0034     int m_lastPaintTime {0};
0035     QElapsedTimer m_elapsedTimer;
0036 
0037     // Callbacks
0038     std::function<void(const KisPaintInformation &, const KisPaintInformation &)> m_paintLine;
0039     std::function<void()> m_requestUpdateOutline;
0040 
0041 public:
0042     KisStabilizerDelayedPaintHelper();
0043     ~KisStabilizerDelayedPaintHelper() override {}
0044 
0045     bool running() const {
0046         return m_paintTimer.isActive();
0047     }
0048 
0049     bool hasLastPaintInformation() const {
0050         return !m_paintQueue.isEmpty();
0051     }
0052 
0053     KisPaintInformation lastPaintInformation() const {
0054         // Please call hasLastPaintInformation before this
0055         return m_paintQueue.head().paintInfo;
0056     }
0057 
0058     void setPaintLineCallback(std::function<void(const KisPaintInformation &, const KisPaintInformation &)> paintLine) {
0059         m_paintLine = paintLine;
0060     }
0061 
0062     void setUpdateOutlineCallback(std::function<void()> requestUpdateOutline) {
0063         m_requestUpdateOutline = requestUpdateOutline;
0064     }
0065 
0066     void start(const KisPaintInformation &firstPaintInfo);
0067     void update(const QVector<KisPaintInformation> &newPaintInfos);
0068     void paintSome();
0069     void end();
0070     void cancel();
0071 
0072 private Q_SLOTS:
0073     void stabilizerDelayedPaintTimer();
0074 };
0075 
0076 #endif // KIS_STABILIZER_DELAYED_PAINT_HELPER_H