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

0001 /*
0002  *  SPDX-FileCopyrightText: 2018 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 
0008 #include "KisSelectionUpdateCompressor.h"
0009 #include "kis_image.h"
0010 #include "kis_selection.h"
0011 #include "kis_layer_utils.h"
0012 #include "kis_update_selection_job.h"
0013 
0014 
0015 KisSelectionUpdateCompressor::KisSelectionUpdateCompressor(KisSelection *selection)
0016     : m_parentSelection(selection)
0017     , m_updateSignalCompressor(new KisThreadSafeSignalCompressor(100, KisSignalCompressor::POSTPONE))
0018 {
0019     connect(m_updateSignalCompressor, SIGNAL(timeout()), this, SLOT(startUpdateJob()));
0020 
0021     this->moveToThread(m_updateSignalCompressor->thread());
0022 }
0023 
0024 KisSelectionUpdateCompressor::~KisSelectionUpdateCompressor()
0025 {
0026     m_updateSignalCompressor->deleteLater();
0027 }
0028 
0029 void KisSelectionUpdateCompressor::requestUpdate(const QRect &updateRect)
0030 {
0031     m_fullUpdateRequested |= updateRect.isEmpty();
0032     m_updateRect = !m_fullUpdateRequested ? m_updateRect | updateRect : QRect();
0033     m_updateSignalCompressor->start();
0034 }
0035 
0036 void KisSelectionUpdateCompressor::tryProcessStalledUpdate()
0037 {
0038     if (m_hasStalledUpdate) {
0039         m_updateSignalCompressor->start();
0040     }
0041 }
0042 
0043 void KisSelectionUpdateCompressor::startUpdateJob()
0044 {
0045     KisNodeSP parentNode = m_parentSelection->parentNode();
0046     if (!parentNode) {
0047         m_hasStalledUpdate = true;
0048         return;
0049     }
0050 
0051     // FIXME: we cannot use parentNode->image() here because masks don't
0052     //        have the pointer initialized for some reason.
0053     KisImageSP image = KisLayerUtils::findImageByHierarchy(parentNode);
0054     if (!image) {
0055         m_hasStalledUpdate = true;
0056         return;
0057     }
0058 
0059     if (image) {
0060         image->addSpontaneousJob(new KisUpdateSelectionJob(m_parentSelection, m_updateRect));
0061     }
0062     m_updateRect = QRect();
0063     m_fullUpdateRequested = false;
0064     m_hasStalledUpdate = false;
0065 }