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

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 "attendeelineeditdelegate.h"
0008 
0009 #include "attendeeline.h"
0010 
0011 #include <KLocalizedString>
0012 
0013 #include <QAbstractItemView>
0014 #include <QHelpEvent>
0015 #include <QToolTip>
0016 #include <QWhatsThis>
0017 
0018 using namespace IncidenceEditorNG;
0019 
0020 AttendeeLineEditDelegate::AttendeeLineEditDelegate(QObject *parent)
0021     : QStyledItemDelegate(parent)
0022     , mToolTip(i18nc("@info:tooltip", "Enter the name or email address of the attendee."))
0023     , mWhatsThis(i18nc("@info:whatsthis",
0024                        "The email address or name of the attendee. An invitation "
0025                        "can be sent to the user if an email address is provided."))
0026 {
0027 }
0028 
0029 QWidget *AttendeeLineEditDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
0030 {
0031     Q_UNUSED(option)
0032     Q_UNUSED(index)
0033     auto editor = new AttendeeLineEdit(parent);
0034     connect(editor, &AttendeeLineEdit::leftPressed, this, &AttendeeLineEditDelegate::leftPressed);
0035     connect(editor, &AttendeeLineEdit::rightPressed, this, &AttendeeLineEditDelegate::rightPressed);
0036     editor->setToolTip(mToolTip);
0037     editor->setWhatsThis(mWhatsThis);
0038     editor->setCompletionMode(mCompletionMode);
0039     editor->setClearButtonEnabled(true);
0040 
0041     return editor;
0042 }
0043 
0044 void AttendeeLineEditDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
0045 {
0046     auto lineedit = static_cast<AttendeeLineEdit *>(editor);
0047     lineedit->setText(index.model()->data(index, Qt::EditRole).toString());
0048 }
0049 
0050 void AttendeeLineEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
0051 {
0052     auto lineedit = static_cast<AttendeeLineEdit *>(editor);
0053     model->setData(index, lineedit->text(), Qt::EditRole);
0054 }
0055 
0056 void AttendeeLineEditDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
0057 {
0058     Q_UNUSED(index)
0059     editor->setGeometry(option.rect);
0060 }
0061 
0062 void AttendeeLineEditDelegate::leftPressed()
0063 {
0064     Q_EMIT closeEditor(static_cast<QWidget *>(QObject::sender()), QAbstractItemDelegate::EditPreviousItem);
0065 }
0066 
0067 void AttendeeLineEditDelegate::rightPressed()
0068 {
0069     Q_EMIT closeEditor(static_cast<QWidget *>(QObject::sender()), QAbstractItemDelegate::EditNextItem);
0070 }
0071 
0072 void AttendeeLineEditDelegate::setCompletionMode(KCompletion::CompletionMode mode)
0073 {
0074     mCompletionMode = mode;
0075 }
0076 
0077 bool AttendeeLineEditDelegate::helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index)
0078 {
0079     if (!event || !view) {
0080         return false;
0081     }
0082     switch (event->type()) {
0083 #ifndef QT_NO_TOOLTIP
0084     case QEvent::ToolTip:
0085         QToolTip::showText(event->globalPos(), mToolTip, view);
0086         return true;
0087 #endif
0088 #ifndef QT_NO_WHATSTHIS
0089     case QEvent::QueryWhatsThis:
0090         return true;
0091     case QEvent::WhatsThis:
0092         QWhatsThis::showText(event->globalPos(), mWhatsThis, view);
0093         return true;
0094 #endif
0095     default:
0096         break;
0097     }
0098     return QStyledItemDelegate::helpEvent(event, view, option, index);
0099 }
0100 
0101 #include "moc_attendeelineeditdelegate.cpp"