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

0001 /*
0002  * SPDX-FileCopyrightText: 2018 Jouni Pentikäinen <joupent@gmail.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include <KisSpinBoxI18nHelper.h>
0008 #include <kis_signal_compressor.h>
0009 
0010 #include "KisSelectionPropertySlider.h"
0011 
0012 template class KisSelectionPropertySlider<KoShape *>;
0013 
0014 struct KisSelectionPropertySliderBase::Private
0015 {
0016     KisSignalCompressor *signalCompressor {nullptr};
0017     QString normalTemplate;
0018     QString mixedTemplate;
0019 
0020     explicit Private(KisSelectionPropertySliderBase *q)
0021         : signalCompressor(new KisSignalCompressor(100, KisSignalCompressor::FIRST_ACTIVE, q))
0022     {}
0023 };
0024 
0025 KisSelectionPropertySliderBase::KisSelectionPropertySliderBase(QWidget *parent)
0026     : KisDoubleSliderSpinBox(parent)
0027     , m_d(new Private(this))
0028 {
0029     connect(m_d->signalCompressor, SIGNAL(timeout()), SLOT(slotCompressedUpdate()));
0030 }
0031 
0032 KisSelectionPropertySliderBase::~KisSelectionPropertySliderBase()
0033 {}
0034 
0035 void KisSelectionPropertySliderBase::setTextTemplates(const QString &normalTemplate, const QString &mixedTemplate)
0036 {
0037     m_d->normalTemplate = normalTemplate;
0038     m_d->mixedTemplate = mixedTemplate;
0039     KisSpinBoxI18nHelper::setText(static_cast<QDoubleSpinBox *>(this), normalTemplate);
0040 }
0041 
0042 void KisSelectionPropertySliderBase::setInternalValue(qreal _value, bool blockUpdateSignal)
0043 {
0044     static const qreal eps = 1e-3;
0045 
0046     if (!hasSelection()) return;
0047 
0048     setPrivateValue(_value);
0049 
0050     const qreal newValue = value();
0051     const qreal commonValue = getCommonValue();
0052     if (qAbs(commonValue - newValue) < eps) {
0053         return;
0054     }
0055 
0056     if(!blockUpdateSignal) {
0057         m_d->signalCompressor->start();
0058     }
0059 }
0060 
0061 void KisSelectionPropertySliderBase::slotCompressedUpdate()
0062 {
0063     emit(valueChanged(value()));
0064 }
0065 
0066 void KisSelectionPropertySliderBase::setSelectionValue(qreal commonValue, bool mixed)
0067 {
0068     if (mixed) {
0069         setInternalValue(0.0, true); // BUG:409131
0070         KisSpinBoxI18nHelper::setText(static_cast<QDoubleSpinBox *>(this), m_d->mixedTemplate);
0071     } else {
0072         setValue(commonValue);
0073         KisSpinBoxI18nHelper::setText(static_cast<QDoubleSpinBox *>(this), m_d->normalTemplate);
0074     }
0075 }
0076 
0077 KisShapePropertySlider::KisShapePropertySlider(QWidget *parent)
0078     : KisSelectionPropertySlider<KoShape*>::KisSelectionPropertySlider(parent)
0079 {}