| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2021 Mathias Wein <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "WGCommonColorSet.h" |
| 8 |
|
| 9 |
#include "WGCommonColorsCalculationRunner.h" |
| 10 |
#include <QThreadPool> |
| 11 |
|
| 12 |
WGCommonColorSet::WGCommonColorSet(QObject *parent) |
| 13 |
: KisUniqueColorSet(parent) |
| 14 |
, m_commonColors(new QVector<KoColor>) |
| 15 |
{ |
| 16 |
m_updateTimer.setInterval(2000); |
| 17 |
m_updateTimer.setSingleShot(true); |
| 18 |
connect(&m_updateTimer, SIGNAL(timeout()), this, SLOT(slotUpdateColors())); |
| 19 |
} |
| 20 |
|
| 21 |
void WGCommonColorSet::setImage(KisImageSP image) |
| 22 |
{ |
| 23 |
if (m_autoUpdate) { |
| 24 |
if (m_image) { |
| 25 |
disconnect(m_image, SIGNAL(sigImageUpdated(QRect)), &m_updateTimer, SLOT(start())); |
| 26 |
} |
| 27 |
if (image) { |
| 28 |
connect(image, SIGNAL(sigImageUpdated(QRect)), &m_updateTimer, SLOT(start()), Qt::UniqueConnection); |
| 29 |
m_updateTimer.start(); |
| 30 |
} |
| 31 |
} |
| 32 |
m_image = image; |
| 33 |
} |
| 34 |
|
| 35 |
void WGCommonColorSet::setAutoUpdate(bool enabled) |
| 36 |
{ |
| 37 |
if (enabled == m_autoUpdate) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
m_autoUpdate = enabled; |
| 42 |
|
| 43 |
if (m_image) { |
| 44 |
if (enabled) { |
| 45 |
connect(m_image, SIGNAL(sigImageUpdated(QRect)), &m_updateTimer, SLOT(start()), Qt::UniqueConnection); |
| 46 |
} |
| 47 |
else { |
| 48 |
disconnect(m_image, SIGNAL(sigImageUpdated(QRect)), &m_updateTimer, SLOT(start())); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
void WGCommonColorSet::slotUpdateColors() |
| 54 |
{ |
| 55 |
if (!m_image) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
if(!m_idle) { |
| 60 |
// Previous computation is still running, try again later |
| 61 |
m_updateTimer.start(); |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
m_idle = false; |
| 66 |
Q_EMIT sigIdle(false); |
| 67 |
|
| 68 |
m_commonColors->clear(); |
| 69 |
WGCommonColorsCalculationRunner* runner = new WGCommonColorsCalculationRunner(m_image, m_numColors, m_commonColors); |
| 70 |
connect(runner, SIGNAL(sigDone()), this, SLOT(slotCalculationDone())); |
| 71 |
QThreadPool::globalInstance()->start(runner); |
| 72 |
} |
| 73 |
|
| 74 |
void WGCommonColorSet::slotCalculationDone() |
| 75 |
{ |
| 76 |
blockSignals(true); |
| 77 |
|
| 78 |
clear(); |
| 79 |
for (const KoColor &color : std::as_const(*m_commonColors)) { |
| 80 |
addColor(color); |
| 81 |
} |
| 82 |
|
| 83 |
blockSignals(false); |
| 84 |
Q_EMIT sigReset(); |
| 85 |
m_idle = true; |
| 86 |
Q_EMIT sigIdle(true); |
| 87 |
} |