File indexing completed on 2024-05-12 15:58:37

0001 /*
0002  *  SPDX-FileCopyrightText: 2013 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef __KIS_PROGRESS_UPDATE_HELPER_H
0008 #define __KIS_PROGRESS_UPDATE_HELPER_H
0009 
0010 #include <KoUpdater.h>
0011 #include <kis_types.h>
0012 
0013 class KisProgressUpdateHelper {
0014 public:
0015     KisProgressUpdateHelper(KoUpdaterPtr progressUpdater, int portion, int numSteps)
0016         : m_progressUpdater(progressUpdater),
0017           m_baseProgress(0),
0018           m_portion(portion),
0019           m_currentStep(0),
0020           m_numSteps(numSteps),
0021           m_lastReportedLocalProgress(-1)
0022      {
0023          if (m_progressUpdater) {
0024              m_baseProgress = m_progressUpdater->progress();
0025          }
0026      }
0027 
0028     ~KisProgressUpdateHelper() {
0029         if (m_progressUpdater) {
0030             m_progressUpdater->setProgress(m_baseProgress + m_portion);
0031         }
0032     }
0033 
0034     void step() {
0035         int localProgress = m_numSteps ?
0036             m_portion * (++m_currentStep) / m_numSteps : m_portion;
0037 
0038         if (m_progressUpdater && m_lastReportedLocalProgress != localProgress) {
0039             m_lastReportedLocalProgress = localProgress;
0040             m_progressUpdater->setProgress(m_baseProgress + localProgress);
0041         }
0042         // TODO: handle interrupted processing (connect to other layers, i.e. undo)
0043     }
0044 
0045 private:
0046     KoUpdaterPtr m_progressUpdater;
0047     int m_baseProgress;
0048     int m_portion;
0049     int m_currentStep;
0050     int m_numSteps;
0051     int m_lastReportedLocalProgress;
0052 };
0053 
0054 #endif /* __KIS_PROGRESS_UPDATE_HELPER_H */