Incidenceeditor

attendeecomboboxdelegate.cpp
1/*
2 * SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
5 */
6
7#include "attendeecomboboxdelegate.h"
8
9#include "attendeeline.h"
10
11#include <QAbstractItemView>
12#include <QApplication>
13#include <QHelpEvent>
14#include <QMenu>
15#include <QToolTip>
16#include <QWhatsThis>
17
18using namespace IncidenceEditorNG;
19
20AttendeeComboBoxDelegate::AttendeeComboBoxDelegate(QObject *parent)
21 : QStyledItemDelegate(parent)
22{
23 connect(this, &AttendeeComboBoxDelegate::closeEditor, this, &AttendeeComboBoxDelegate::doCloseEditor);
24}
25
26void AttendeeComboBoxDelegate::addItem(const QIcon &icon, const QString &text)
27{
28 QPair<QIcon, QString> pair;
29 pair.first = icon;
30 pair.second = text;
31 mEntries << pair;
32}
33
34void AttendeeComboBoxDelegate::clear()
35{
36 mEntries.clear();
37}
38
39void AttendeeComboBoxDelegate::setToolTip(const QString &tT)
40{
41 mToolTip = tT;
42}
43
44void AttendeeComboBoxDelegate::setWhatsThis(const QString &wT)
45{
46 mWhatsThis = wT;
47}
48
50{
51 mStandardIndex = index;
52}
53
54QWidget *AttendeeComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */, const QModelIndex & /* index */) const
55{
56 auto editor = new AttendeeComboBox(parent);
57
58 for (const QPair<QIcon, QString> &pair : std::as_const(mEntries)) {
59 editor->addItem(pair.first, pair.second);
60 }
61
62 connect(editor, &AttendeeComboBox::leftPressed, this, &AttendeeComboBoxDelegate::leftPressed);
63 connect(editor, &AttendeeComboBox::rightPressed, this, &AttendeeComboBoxDelegate::rightPressed);
64
65 editor->setPopupMode(QToolButton::MenuButtonPopup);
66 editor->setToolTip(mToolTip);
67 editor->setWhatsThis(mWhatsThis);
68 return editor;
69}
70
71void AttendeeComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
72{
73 auto comboBox = static_cast<AttendeeComboBox *>(editor);
74 int value = index.model()->data(index, Qt::EditRole).toUInt();
75 if (value >= mEntries.count()) {
76 value = mStandardIndex;
77 }
78 comboBox->setCurrentIndex(value);
79}
80
81void AttendeeComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
82{
83 auto comboBox = static_cast<AttendeeComboBox *>(editor);
84 model->setData(index, comboBox->currentIndex(), Qt::EditRole);
85 comboBox->menu()->close();
86}
87
88void AttendeeComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & /* index */) const
89{
90 editor->setGeometry(option.rect);
91}
92
93void AttendeeComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
94{
95 QStyleOptionButton myOption;
96
97 int value = index.model()->data(index).toUInt();
98 if (value >= mEntries.count()) {
99 value = mStandardIndex;
100 }
101
102 myOption.rect = option.rect;
103 myOption.state = option.state;
104 myOption.icon = mEntries[value].first;
105 myOption.iconSize = myOption.icon.actualSize(option.rect.size());
106
108}
109
110bool AttendeeComboBoxDelegate::eventFilter(QObject *editor, QEvent *event)
111{
112 if (event->type() == QEvent::Enter) {
113 auto comboBox = static_cast<AttendeeComboBox *>(editor);
114 comboBox->showMenu();
115 return editor->eventFilter(editor, event);
116 }
117
119}
120
121void AttendeeComboBoxDelegate::doCloseEditor(QWidget *editor)
122{
123 auto comboBox = static_cast<AttendeeComboBox *>(editor);
124 comboBox->menu()->close();
125}
126
127void AttendeeComboBoxDelegate::leftPressed()
128{
130}
131
132void AttendeeComboBoxDelegate::rightPressed()
133{
135}
136
137bool AttendeeComboBoxDelegate::helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index)
138{
139 if (!event || !view) {
140 return false;
141 }
142 switch (event->type()) {
143#ifndef QT_NO_TOOLTIP
144 case QEvent::ToolTip: {
145 QToolTip::showText(event->globalPos(), mToolTip, view);
146 return true;
147 }
148#endif
149#ifndef QT_NO_WHATSTHIS
151 return true;
152 case QEvent::WhatsThis: {
153 QWhatsThis::showText(event->globalPos(), mWhatsThis, view);
154 return true;
155 }
156#endif
157 default:
158 break;
159 }
160 return QStyledItemDelegate::helpEvent(event, view, option, index);
161}
162
163#include "moc_attendeecomboboxdelegate.cpp"
void setStandardIndex(int)
choose this index, if the item in the model is unknown
void closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint)
virtual bool helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index)
virtual QVariant data(const QModelIndex &index, int role) const const=0
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
QStyle * style()
void clear()
qsizetype count() const const
T & first()
const QAbstractItemModel * model() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool event(QEvent *e)
virtual bool eventFilter(QObject *watched, QEvent *event)
QObject * parent() const const
QObject * sender() const const
virtual void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const const=0
virtual bool eventFilter(QObject *editor, QEvent *event) override
EditRole
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void showText(const QPoint &pos, const QString &text, QWidget *w, const QRect &rect, int msecDisplayTime)
uint toUInt(bool *ok) const const
void showText(const QPoint &pos, const QString &text, QWidget *w)
void setGeometry(const QRect &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.