File indexing completed on 2024-04-28 04:39:08
0001 /* 0002 SPDX-FileCopyrightText: 2013 Milian Wolff <mail@milianw.de> 0003 0004 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "comboboxdelegate.h" 0008 0009 #include <QComboBox> 0010 0011 using namespace KDevelop; 0012 0013 ComboBoxDelegate::ComboBoxDelegate(const QVector<Item>& items, QObject* parent) 0014 : QStyledItemDelegate(parent) 0015 , m_items(items) 0016 { 0017 0018 } 0019 0020 ComboBoxDelegate::~ComboBoxDelegate() 0021 { 0022 0023 } 0024 0025 QWidget* ComboBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/) const 0026 { 0027 auto* ret = new QComboBox(parent); 0028 ret->setEditable(false); 0029 return ret; 0030 } 0031 0032 void ComboBoxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const 0033 { 0034 Q_ASSERT(qobject_cast<QComboBox*>(editor)); 0035 auto* box = static_cast<QComboBox*>(editor); 0036 box->clear(); 0037 const QString& current = index.data().toString(); 0038 int currentIndex = -1; 0039 int i = 0; 0040 for (const Item& item : m_items) { 0041 if (item.text == current) { 0042 currentIndex = i; 0043 } 0044 box->addItem(item.text); 0045 i++; 0046 } 0047 if (currentIndex != -1) { 0048 box->setCurrentIndex(currentIndex); 0049 } 0050 } 0051 0052 void ComboBoxDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const 0053 { 0054 Q_ASSERT(qobject_cast<QComboBox*>(editor)); 0055 auto* box = static_cast<QComboBox*>(editor); 0056 model->setData(index, m_items.at(box->currentIndex()).data); 0057 } 0058 0059 #include "moc_comboboxdelegate.cpp"