File: comboboxdelegate.cpp

package info (click to toggle)
kf6-ktexttemplate 6.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,276 kB
  • sloc: cpp: 19,546; javascript: 6,043; python: 297; ruby: 24; makefile: 5
file content (114 lines) | stat: -rw-r--r-- 3,020 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
  This file is part of the KTextTemplate library

  SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com>

  SPDX-License-Identifier: LGPL-2.1-or-later

*/

#include "comboboxdelegate.h"
#include "comboboxdelegate_p.h"

#include <QApplication>
#include <QDebug>

ComboBoxEditorCreator::ComboBoxEditorCreator(const QStringList &data, ComboBoxDelegate::Type type)
    : QItemEditorCreatorBase()
    , m_data(data)
    , m_type(type)
{
}

ComboBoxEditorCreator::~ComboBoxEditorCreator()
{
}

QWidget *ComboBoxEditorCreator::createWidget(QWidget *parent) const
{
    ViewComboBox *vcb = new ViewComboBox(parent);
    vcb->addItems(m_data);

    if (m_type == ComboBoxDelegate::Editable)
        vcb->setEditable(true);

    return vcb;
}

QByteArray ComboBoxEditorCreator::valuePropertyName() const
{
    return QByteArray("choice");
}

ViewComboBox::ViewComboBox(QWidget *parent)
    : QComboBox(parent)
{
}

QString ViewComboBox::choice() const
{
    return currentText();
}

void ViewComboBox::setChoice(const QString &choice)
{
    const int index = findData(choice, Qt::DisplayRole, Qt::MatchFixedString);
    if (index >= 0)
        setCurrentIndex(index);
    else
        setEditText(choice);
}

ComboBoxDelegate::ComboBoxDelegate(const QStringList &data, Type type, QObject *parent)
    : QItemDelegate(parent)
{
    QItemEditorFactory *factory = new QItemEditorFactory;
    QItemEditorCreatorBase *creator = new ComboBoxEditorCreator(data, type);
    factory->registerEditor(QMetaType::QString, creator);

    setItemEditorFactory(factory);
}

QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QWidget *w = QItemDelegate::createEditor(parent, option, index);
    ViewComboBox *viewComboBox = qobject_cast<ViewComboBox *>(w);
    Q_ASSERT(viewComboBox);
    return viewComboBox;
}

static QSize textSize(const QFont &font, const QString &text)
{
    QFontMetrics fm(font);
    QSize size = fm.size(Qt::TextSingleLine, text);
    const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
    return QSize(size.width() + 2 * textMargin, size.height());
}

QSize ComboBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if (m_sizes.contains(index.row())) {
        return m_sizes.value(index.row());
    }
    QSize sz;
    QVariant fontData = index.data(Qt::FontRole);
    QFont fnt = qvariant_cast<QFont>(fontData).resolve(option.font);
    for (int i = 0; i < sizeof sTypes / sizeof *sTypes; ++i) {
        QString text = *(sTypes + i);
        QSize s = textSize(fnt, text);
        sz = sz.expandedTo(s);
    }

    QStyleOptionComboBox opt;
    opt.editable = true;
    opt.frame = true;
    opt.currentText = index.data().toString();

    sz = qApp->style()->sizeFromContents(QStyle::CT_ComboBox, &opt, sz);
    m_sizes.insert(index.row(), sz);

    return sz;
}

#include "moc_comboboxdelegate.cpp"
#include "moc_comboboxdelegate_p.cpp"