File indexing completed on 2024-12-22 03:46:48

0001 /*
0002   This file is part of the KTextTemplate library
0003 
0004   SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com>
0005 
0006   SPDX-License-Identifier: LGPL-2.1-or-later
0007 
0008 */
0009 
0010 #include "comboboxdelegate.h"
0011 #include "comboboxdelegate_p.h"
0012 
0013 #include <QApplication>
0014 #include <QDebug>
0015 
0016 ComboBoxEditorCreator::ComboBoxEditorCreator(const QStringList &data, ComboBoxDelegate::Type type)
0017     : QItemEditorCreatorBase()
0018     , m_data(data)
0019     , m_type(type)
0020 {
0021 }
0022 
0023 ComboBoxEditorCreator::~ComboBoxEditorCreator()
0024 {
0025 }
0026 
0027 QWidget *ComboBoxEditorCreator::createWidget(QWidget *parent) const
0028 {
0029     ViewComboBox *vcb = new ViewComboBox(parent);
0030     vcb->addItems(m_data);
0031 
0032     if (m_type == ComboBoxDelegate::Editable)
0033         vcb->setEditable(true);
0034 
0035     return vcb;
0036 }
0037 
0038 QByteArray ComboBoxEditorCreator::valuePropertyName() const
0039 {
0040     return QByteArray("choice");
0041 }
0042 
0043 ViewComboBox::ViewComboBox(QWidget *parent)
0044     : QComboBox(parent)
0045 {
0046 }
0047 
0048 QString ViewComboBox::choice() const
0049 {
0050     return currentText();
0051 }
0052 
0053 void ViewComboBox::setChoice(const QString &choice)
0054 {
0055     const int index = findData(choice, Qt::DisplayRole, Qt::MatchFixedString);
0056     if (index >= 0)
0057         setCurrentIndex(index);
0058     else
0059         setEditText(choice);
0060 }
0061 
0062 ComboBoxDelegate::ComboBoxDelegate(const QStringList &data, Type type, QObject *parent)
0063     : QItemDelegate(parent)
0064 {
0065     QItemEditorFactory *factory = new QItemEditorFactory;
0066     QItemEditorCreatorBase *creator = new ComboBoxEditorCreator(data, type);
0067     factory->registerEditor(QMetaType::QString, creator);
0068 
0069     setItemEditorFactory(factory);
0070 }
0071 
0072 QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
0073 {
0074     QWidget *w = QItemDelegate::createEditor(parent, option, index);
0075     ViewComboBox *viewComboBox = qobject_cast<ViewComboBox *>(w);
0076     Q_ASSERT(viewComboBox);
0077     return viewComboBox;
0078 }
0079 
0080 static QSize textSize(const QFont &font, const QString &text)
0081 {
0082     QFontMetrics fm(font);
0083     QSize size = fm.size(Qt::TextSingleLine, text);
0084     const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
0085     return QSize(size.width() + 2 * textMargin, size.height());
0086 }
0087 
0088 QSize ComboBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
0089 {
0090     if (m_sizes.contains(index.row())) {
0091         return m_sizes.value(index.row());
0092     }
0093     QSize sz;
0094     QVariant fontData = index.data(Qt::FontRole);
0095     QFont fnt = qvariant_cast<QFont>(fontData).resolve(option.font);
0096     for (int i = 0; i < sizeof sTypes / sizeof *sTypes; ++i) {
0097         QString text = *(sTypes + i);
0098         QSize s = textSize(fnt, text);
0099         sz = sz.expandedTo(s);
0100     }
0101 
0102     QStyleOptionComboBox opt;
0103     opt.editable = true;
0104     opt.frame = true;
0105     opt.currentText = index.data().toString();
0106 
0107     sz = qApp->style()->sizeFromContents(QStyle::CT_ComboBox, &opt, sz);
0108     m_sizes.insert(index.row(), sz);
0109 
0110     return sz;
0111 }
0112 
0113 #include "moc_comboboxdelegate.cpp"
0114 #include "moc_comboboxdelegate_p.cpp"