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

0001 /*
0002  *  SPDX-FileCopyrightText: 2019 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "KisAsynchronousStrokeUpdateHelper.h"
0008 #include "kis_image_interfaces.h"
0009 
0010 KisAsynchronousStrokeUpdateHelper::KisAsynchronousStrokeUpdateHelper()
0011     : m_strokesFacade(0)
0012 {
0013     m_updateThresholdTimer.setSingleShot(false);
0014     m_updateThresholdTimer.setInterval(80 /* ms */);
0015     connect(&m_updateThresholdTimer, SIGNAL(timeout()), SLOT(slotAsyncUpdateCame()));
0016 }
0017 
0018 KisAsynchronousStrokeUpdateHelper::~KisAsynchronousStrokeUpdateHelper()
0019 {
0020 
0021 }
0022 
0023 void KisAsynchronousStrokeUpdateHelper::initUpdateStreamLowLevel(KisStrokesFacade *strokesFacade, KisStrokeId strokeId)
0024 {
0025     m_strokesFacade = strokesFacade;
0026     m_strokeId = strokeId;
0027 }
0028 
0029 void KisAsynchronousStrokeUpdateHelper::startUpdateStreamLowLevel()
0030 {
0031     KIS_SAFE_ASSERT_RECOVER_RETURN(m_strokesFacade);
0032     KIS_SAFE_ASSERT_RECOVER_RETURN(m_strokeId);
0033 
0034     m_updateThresholdTimer.start();
0035 }
0036 
0037 void KisAsynchronousStrokeUpdateHelper::startUpdateStream(KisStrokesFacade *strokesFacade, KisStrokeId strokeId)
0038 {
0039     initUpdateStreamLowLevel(strokesFacade, strokeId);
0040     startUpdateStreamLowLevel();
0041 }
0042 
0043 void KisAsynchronousStrokeUpdateHelper::endUpdateStream()
0044 {
0045     KIS_SAFE_ASSERT_RECOVER_RETURN(isActive());
0046 
0047     slotAsyncUpdateCame(true);
0048     cancelUpdateStream();
0049 }
0050 
0051 void KisAsynchronousStrokeUpdateHelper::cancelUpdateStream()
0052 {
0053     KIS_SAFE_ASSERT_RECOVER_RETURN(isActive());
0054 
0055     m_updateThresholdTimer.stop();
0056     m_strokeId.clear();
0057     m_strokesFacade = 0;
0058 }
0059 
0060 bool KisAsynchronousStrokeUpdateHelper::isActive() const
0061 {
0062     return m_strokeId;
0063 }
0064 
0065 void KisAsynchronousStrokeUpdateHelper::setCustomUpdateDataFactory(KisAsynchronousStrokeUpdateHelper::UpdateDataFactory factory)
0066 {
0067     m_customUpdateFactory = factory;
0068 }
0069 
0070 void KisAsynchronousStrokeUpdateHelper::slotAsyncUpdateCame(bool forceUpdate)
0071 {
0072     if (!m_strokeId || !m_strokesFacade) return;
0073     m_strokesFacade->addJob(m_strokeId,
0074                             m_customUpdateFactory ?
0075                                 m_customUpdateFactory(forceUpdate) :
0076                                 new UpdateData(forceUpdate));
0077 }