File indexing completed on 2024-12-15 03:45:02

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include "schemaentryitemeditorfactory.h"
0008 
0009 #include <core/aggregation.h>
0010 #include <core/schemaentry.h>
0011 #include <core/schemaentryelement.h>
0012 #include <model/aggregationelementmodel.h>
0013 #include <widgets/metaenumcombobox.h>
0014 
0015 #include <QDebug>
0016 
0017 using namespace KUserFeedback::Console;
0018 
0019 namespace KUserFeedback {
0020 namespace Console {
0021 
0022 class AggregationElementComboBox : public QComboBox
0023 {
0024     Q_OBJECT
0025     Q_PROPERTY(QVariant value READ value WRITE setValue USER true)
0026 public:
0027     explicit AggregationElementComboBox(QWidget *parent = nullptr) :
0028         QComboBox(parent)
0029     {}
0030 
0031     QVariant value() const
0032     {
0033         return currentData(Qt::EditRole);
0034     }
0035 
0036     void setValue(const QVariant &value)
0037     {
0038         const auto idxs = model()->match(model()->index(0, 0), Qt::EditRole, value, 1, Qt::MatchExactly);
0039         if (idxs.isEmpty())
0040             return;
0041         setCurrentIndex(idxs.at(0).row());
0042     }
0043 };
0044 
0045 class AggregationElementEditorCreator : public QStandardItemEditorCreator<AggregationElementComboBox>
0046 {
0047 public:
0048     explicit AggregationElementEditorCreator(AggregationElementModel *model) :
0049         m_model(model)
0050     {}
0051 
0052     QWidget* createWidget(QWidget *parent) const override
0053     {
0054         auto w = QStandardItemEditorCreator<AggregationElementComboBox>::createWidget(parent);
0055         qobject_cast<QComboBox*>(w)->setModel(m_model);
0056         return w;
0057     }
0058 
0059 private:
0060     Q_DISABLE_COPY(AggregationElementEditorCreator)
0061     AggregationElementModel *m_model;
0062 };
0063 
0064 }
0065 }
0066 
0067 SchemaEntryItemEditorFactory::SchemaEntryItemEditorFactory() :
0068     m_elementModel(new AggregationElementModel)
0069 {
0070    registerEditor(qMetaTypeId<SchemaEntry::DataType>(), new QStandardItemEditorCreator<MetaEnumComboBox>());
0071    registerEditor(qMetaTypeId<SchemaEntryElement::Type>(), new QStandardItemEditorCreator<MetaEnumComboBox>());
0072    registerEditor(qMetaTypeId<Aggregation::Type>(), new QStandardItemEditorCreator<MetaEnumComboBox>());
0073    registerEditor(qMetaTypeId<AggregationElement>(), new AggregationElementEditorCreator(m_elementModel.get()));
0074 }
0075 
0076 SchemaEntryItemEditorFactory::~SchemaEntryItemEditorFactory() = default;
0077 
0078 void SchemaEntryItemEditorFactory::setProduct(const Product& product)
0079 {
0080     m_elementModel->setProduct(product);
0081 }
0082 
0083 #include "schemaentryitemeditorfactory.moc"