File indexing completed on 2024-05-12 05:17:28
0001 /* 0002 SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> 0003 SPDX-License-Identifier: LGPL-2.0-or-later 0004 */ 0005 0006 #include "metaenumcombobox.h" 0007 0008 #include <QDebug> 0009 #include <QMetaEnum> 0010 #include <QMetaType> 0011 0012 MetaEnumComboBox::MetaEnumComboBox(QWidget* parent) : 0013 QComboBox(parent) 0014 { 0015 } 0016 0017 MetaEnumComboBox::~MetaEnumComboBox() = default; 0018 0019 QVariant MetaEnumComboBox::value() const 0020 { 0021 auto value = m_value; 0022 *(static_cast<int*>(value.data())) = currentData().toInt(); 0023 return value; 0024 } 0025 0026 void MetaEnumComboBox::setValue(const QVariant& value) 0027 { 0028 clear(); 0029 m_value = value; 0030 0031 const auto mo = QMetaType(value.typeId()).metaObject(); 0032 if (!mo) 0033 return; 0034 0035 const QByteArray typeName = value.typeName(); 0036 const auto idx = typeName.lastIndexOf("::"); 0037 if (idx <= 0) 0038 return; 0039 0040 const auto enumName = typeName.mid(idx + 2); 0041 const auto enumIdx = mo->indexOfEnumerator(enumName.constData()); 0042 if (enumIdx < 0) 0043 return; 0044 0045 const auto me = mo->enumerator(enumIdx); 0046 for (int i = 0; i < me.keyCount(); ++i) { 0047 addItem(QLatin1String(me.key(i)), me.value(i)); 0048 if (me.value(i) == value.toInt()) 0049 setCurrentIndex(i); 0050 } 0051 }