File indexing completed on 2024-04-28 05:11:30

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0005  */
0006 
0007 #include "attendeecomboboxdelegate.h"
0008 
0009 #include "attendeeline.h"
0010 
0011 #include <QAbstractItemView>
0012 #include <QApplication>
0013 #include <QHelpEvent>
0014 #include <QMenu>
0015 #include <QToolTip>
0016 #include <QWhatsThis>
0017 
0018 using namespace IncidenceEditorNG;
0019 
0020 AttendeeComboBoxDelegate::AttendeeComboBoxDelegate(QObject *parent)
0021     : QStyledItemDelegate(parent)
0022 {
0023     connect(this, &AttendeeComboBoxDelegate::closeEditor, this, &AttendeeComboBoxDelegate::doCloseEditor);
0024 }
0025 
0026 void AttendeeComboBoxDelegate::addItem(const QIcon &icon, const QString &text)
0027 {
0028     QPair<QIcon, QString> pair;
0029     pair.first = icon;
0030     pair.second = text;
0031     mEntries << pair;
0032 }
0033 
0034 void AttendeeComboBoxDelegate::clear()
0035 {
0036     mEntries.clear();
0037 }
0038 
0039 void AttendeeComboBoxDelegate::setToolTip(const QString &tT)
0040 {
0041     mToolTip = tT;
0042 }
0043 
0044 void AttendeeComboBoxDelegate::setWhatsThis(const QString &wT)
0045 {
0046     mWhatsThis = wT;
0047 }
0048 
0049 void AttendeeComboBoxDelegate::setStandardIndex(int index)
0050 {
0051     mStandardIndex = index;
0052 }
0053 
0054 QWidget *AttendeeComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */, const QModelIndex & /* index */) const
0055 {
0056     auto editor = new AttendeeComboBox(parent);
0057 
0058     for (const QPair<QIcon, QString> &pair : std::as_const(mEntries)) {
0059         editor->addItem(pair.first, pair.second);
0060     }
0061 
0062     connect(editor, &AttendeeComboBox::leftPressed, this, &AttendeeComboBoxDelegate::leftPressed);
0063     connect(editor, &AttendeeComboBox::rightPressed, this, &AttendeeComboBoxDelegate::rightPressed);
0064 
0065     editor->setPopupMode(QToolButton::MenuButtonPopup);
0066     editor->setToolTip(mToolTip);
0067     editor->setWhatsThis(mWhatsThis);
0068     return editor;
0069 }
0070 
0071 void AttendeeComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
0072 {
0073     auto comboBox = static_cast<AttendeeComboBox *>(editor);
0074     int value = index.model()->data(index, Qt::EditRole).toUInt();
0075     if (value >= mEntries.count()) {
0076         value = mStandardIndex;
0077     }
0078     comboBox->setCurrentIndex(value);
0079 }
0080 
0081 void AttendeeComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
0082 {
0083     auto comboBox = static_cast<AttendeeComboBox *>(editor);
0084     model->setData(index, comboBox->currentIndex(), Qt::EditRole);
0085     comboBox->menu()->close();
0086 }
0087 
0088 void AttendeeComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & /* index */) const
0089 {
0090     editor->setGeometry(option.rect);
0091 }
0092 
0093 void AttendeeComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0094 {
0095     QStyleOptionButton myOption;
0096 
0097     int value = index.model()->data(index).toUInt();
0098     if (value >= mEntries.count()) {
0099         value = mStandardIndex;
0100     }
0101 
0102     myOption.rect = option.rect;
0103     myOption.state = option.state;
0104     myOption.icon = mEntries[value].first;
0105     myOption.iconSize = myOption.icon.actualSize(option.rect.size());
0106 
0107     QApplication::style()->drawControl(QStyle::CE_PushButton, &myOption, painter);
0108 }
0109 
0110 bool AttendeeComboBoxDelegate::eventFilter(QObject *editor, QEvent *event)
0111 {
0112     if (event->type() == QEvent::Enter) {
0113         auto comboBox = static_cast<AttendeeComboBox *>(editor);
0114         comboBox->showMenu();
0115         return editor->eventFilter(editor, event);
0116     }
0117 
0118     return QStyledItemDelegate::eventFilter(editor, event);
0119 }
0120 
0121 void AttendeeComboBoxDelegate::doCloseEditor(QWidget *editor)
0122 {
0123     auto comboBox = static_cast<AttendeeComboBox *>(editor);
0124     comboBox->menu()->close();
0125 }
0126 
0127 void AttendeeComboBoxDelegate::leftPressed()
0128 {
0129     Q_EMIT closeEditor(static_cast<QWidget *>(QObject::sender()), QAbstractItemDelegate::EditPreviousItem);
0130 }
0131 
0132 void AttendeeComboBoxDelegate::rightPressed()
0133 {
0134     Q_EMIT closeEditor(static_cast<QWidget *>(QObject::sender()), QAbstractItemDelegate::EditNextItem);
0135 }
0136 
0137 bool AttendeeComboBoxDelegate::helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index)
0138 {
0139     if (!event || !view) {
0140         return false;
0141     }
0142     switch (event->type()) {
0143 #ifndef QT_NO_TOOLTIP
0144     case QEvent::ToolTip: {
0145         QToolTip::showText(event->globalPos(), mToolTip, view);
0146         return true;
0147     }
0148 #endif
0149 #ifndef QT_NO_WHATSTHIS
0150     case QEvent::QueryWhatsThis:
0151         return true;
0152     case QEvent::WhatsThis: {
0153         QWhatsThis::showText(event->globalPos(), mWhatsThis, view);
0154         return true;
0155     }
0156 #endif
0157     default:
0158         break;
0159     }
0160     return QStyledItemDelegate::helpEvent(event, view, option, index);
0161 }
0162 
0163 #include "moc_attendeecomboboxdelegate.cpp"