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

0001 /*
0002  *  SPDX-FileCopyrightText: 2022 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #include "KisCompositeOpListConnectionHelper.h"
0007 
0008 #include <QMetaObject>
0009 #include <QMetaProperty>
0010 #include <KoCompositeOpRegistry.h>
0011 #include <kis_assert.h>
0012 #include "kis_cmb_composite.h"
0013 
0014 namespace KisWidgetConnectionUtils
0015 {
0016 class ConnectCompositeOpListWidgetHelper : public QObject
0017 {
0018     Q_OBJECT
0019 public:
0020 
0021     ConnectCompositeOpListWidgetHelper(KisCompositeOpListWidget *parent)
0022         : QObject(parent),
0023           m_listWidget(parent)
0024     {
0025         connect(parent, &KisCompositeOpListWidget::clicked,
0026                 this, &ConnectCompositeOpListWidgetHelper::slotWidgetChanged);
0027     }
0028 public Q_SLOTS:
0029     void slotWidgetChanged() {
0030         Q_EMIT sigWidgetChanged(m_listWidget->selectedCompositeOp().id());
0031     }
0032 
0033     void slotPropertyChanged(const QString &id) {
0034         m_listWidget->setCompositeOp(KoCompositeOpRegistry::instance().getKoID(id));
0035     }
0036 
0037 Q_SIGNALS:
0038     void sigWidgetChanged(const QString &id);
0039 
0040 private:
0041     KisCompositeOpListWidget *m_listWidget;
0042 };
0043 
0044 void connectControl(KisCompositeOpListWidget *widget, QObject *source, const char *property)
0045 {
0046     const QMetaObject* meta = source->metaObject();
0047     QMetaProperty prop = meta->property(meta->indexOfProperty(property));
0048 
0049     KIS_SAFE_ASSERT_RECOVER_RETURN(prop.hasNotifySignal());
0050 
0051     QMetaMethod signal = prop.notifySignal();
0052 
0053     KIS_SAFE_ASSERT_RECOVER_RETURN(signal.parameterCount() >= 1);
0054     KIS_SAFE_ASSERT_RECOVER_RETURN(signal.parameterType(0) == QMetaType::type("QString"));
0055 
0056     ConnectCompositeOpListWidgetHelper *helper = new ConnectCompositeOpListWidgetHelper(widget);
0057 
0058     const QMetaObject* dstMeta = helper->metaObject();
0059 
0060     QMetaMethod updateSlot = dstMeta->method(
0061                 dstMeta->indexOfSlot("slotPropertyChanged(QString)"));
0062     QObject::connect(source, signal, helper, updateSlot);
0063 
0064     helper->slotPropertyChanged(prop.read(source).value<QString>());
0065 
0066     if (prop.isWritable()) {
0067         QObject::connect(helper, &ConnectCompositeOpListWidgetHelper::sigWidgetChanged, [prop, source] (const QString &value) { prop.write(source, QVariant::fromValue(value)); });
0068     }
0069 }
0070 
0071 }
0072 
0073 #include "KisCompositeOpListConnectionHelper.moc"