File indexing completed on 2024-12-22 04:15:04

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Mathias Wein <lynx.mw+kde@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "WGShadeLineEditor.h"
0008 #include "WGShadeSlider.h"
0009 
0010 #include "ui_WdgWGShadeLineEditor.h"
0011 
0012 #include <QCoreApplication>
0013 #include <QResizeEvent>
0014 
0015 WGShadeLineEditor::WGShadeLineEditor(QWidget *parent)
0016     : QFrame(parent, Qt::Popup)
0017     , m_model(new KisVisualColorModel)
0018     , m_ui(new Ui_WGShadeLineEditor)
0019     , m_previewLine(new WGShadeSlider(WGSelectorDisplayConfigSP(new WGSelectorDisplayConfig()), this, m_model))
0020     , m_iconSlider(new WGShadeSlider(WGSelectorDisplayConfigSP(new WGSelectorDisplayConfig()), this, m_model))
0021 {
0022     setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
0023 
0024     m_model->slotSetColorSpace(KoColorSpaceRegistry::instance()->rgb8());
0025     m_model->slotSetColor(KoColor(QColor(190, 50, 50), m_model->colorSpace()));
0026 
0027     m_ui->setupUi(this);
0028 
0029     m_previewLine->setObjectName(QString::fromUtf8("previewLine"));
0030     m_previewLine->setFixedHeight(24);
0031 
0032     m_ui->verticalLayout->addWidget(m_previewLine);
0033 
0034     m_ui->verticalLayout->setSizeConstraint(QLayout::SetFixedSize);
0035     m_previewLine->setModel(m_model);
0036     m_previewLine->slotSetChannelValues(m_model->channelValues());
0037     // since this widget is not shown by Qt, a resize event needs to be sent manually
0038     QResizeEvent event(QSize(128, 10), m_iconSlider->size());
0039     m_iconSlider->resize(128, 10);
0040     QCoreApplication::sendEvent(m_iconSlider, &event);
0041     m_iconSlider->hide();
0042     m_iconSlider->slotSetChannelValues(m_model->channelValues());
0043     connect(m_ui->sbRangeHue, SIGNAL(valueChanged(double)), SLOT(slotValueChanged()));
0044     connect(m_ui->sbRangeSaturation, SIGNAL(valueChanged(double)), SLOT(slotValueChanged()));
0045     connect(m_ui->sbRangeValue, SIGNAL(valueChanged(double)), SLOT(slotValueChanged()));
0046     connect(m_ui->sbOffsetHue, SIGNAL(valueChanged(double)), SLOT(slotValueChanged()));
0047     connect(m_ui->sbOffsetSaturation, SIGNAL(valueChanged(double)), SLOT(slotValueChanged()));
0048     connect(m_ui->sbOffsetValue, SIGNAL(valueChanged(double)), SLOT(slotValueChanged()));
0049     connect(m_ui->sbPatchCount, SIGNAL(valueChanged(int)), SLOT(slotPatchCountChanged(int)));
0050     connect(m_ui->rbSlider, SIGNAL(toggled(bool)), SLOT(slotSliderModeChanged(bool)));
0051     // disable spinBox when slider mode set
0052     connect(m_ui->rbColorPatches, SIGNAL(toggled(bool)), m_ui->sbPatchCount, SLOT(setEnabled(bool)));
0053 }
0054 
0055 WGShadeLineEditor::~WGShadeLineEditor()
0056 {
0057 
0058 }
0059 
0060 WGConfig::ShadeLine WGShadeLineEditor::configuration() const
0061 {
0062     WGConfig::ShadeLine cfg;
0063     cfg.gradient = QVector4D(m_ui->sbRangeHue->value(),
0064                              m_ui->sbRangeSaturation->value(),
0065                              m_ui->sbRangeValue->value(),
0066                              0);
0067     cfg.offset = QVector4D(m_ui->sbOffsetHue->value(),
0068                            m_ui->sbOffsetSaturation->value(),
0069                            m_ui->sbOffsetValue->value(),
0070                            0);
0071     cfg.patchCount = m_ui->rbSlider->isChecked() ? -1 : m_ui->sbPatchCount->value();
0072     return cfg;
0073 }
0074 
0075 void WGShadeLineEditor::setConfiguration(const WGConfig::ShadeLine &cfg, int lineIndex)
0076 {
0077     m_ui->sbRangeHue->setValue(cfg.gradient.x());
0078     m_ui->sbRangeSaturation->setValue(cfg.gradient.y());
0079     m_ui->sbRangeValue->setValue(cfg.gradient.z());
0080     m_ui->sbOffsetHue->setValue(cfg.offset.x());
0081     m_ui->sbOffsetSaturation->setValue(cfg.offset.y());
0082     m_ui->sbOffsetValue->setValue(cfg.offset.z());
0083     if (cfg.patchCount > 0) {
0084         m_ui->rbColorPatches->setChecked(true);
0085         m_ui->sbPatchCount->setValue(cfg.patchCount);
0086     }
0087     else {
0088         m_ui->rbSlider->setChecked(true);
0089     }
0090     m_lineIndex = lineIndex;
0091 }
0092 
0093 QIcon WGShadeLineEditor::generateIcon(const WGConfig::ShadeLine &cfg)
0094 {
0095     m_iconSlider->setGradient(cfg.gradient, cfg.offset);
0096     m_iconSlider->setDisplayMode(cfg.patchCount < 0, cfg.patchCount);
0097     return QIcon(QPixmap::fromImage(*m_iconSlider->background()));
0098 }
0099 
0100 void WGShadeLineEditor::hideEvent(QHideEvent *event)
0101 {
0102     Q_EMIT sigEditorClosed(m_lineIndex);
0103     QWidget::hideEvent(event);
0104 }
0105 
0106 void WGShadeLineEditor::slotValueChanged()
0107 {
0108     WGConfig::ShadeLine cfg = configuration();
0109     m_previewLine->setGradient(cfg.gradient, cfg.offset);
0110 }
0111 
0112 void WGShadeLineEditor::slotPatchCountChanged(int value)
0113 {
0114     m_previewLine->setDisplayMode(false, value);
0115 }
0116 
0117 void WGShadeLineEditor::slotSliderModeChanged(bool enabled)
0118 {
0119     m_previewLine->setDisplayMode(enabled, m_ui->sbPatchCount->value());
0120 }