attendeecomboboxdelegate.cpp 5.51 KB
Newer Older
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
/*
 * Copyright (c) 2014 Sandro Knauß <knauss@kolabsys.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * As a special exception, permission is given to link this program
 * with any edition of Qt, and distribute the resulting executable,
 * without including the source code for Qt in the source distribution.
 */

#include "attendeecomboboxdelegate.h"

#include "attendeeline.h"

Laurent Montel's avatar
Laurent Montel committed
27
#include <QMenu>
28 29 30 31 32 33 34 35 36 37 38 39
#include <QAbstractItemView>
#include <QApplication>
#include <QHelpEvent>
#include <QToolTip>
#include <QWhatsThis>

using namespace IncidenceEditorNG;

AttendeeComboBoxDelegate::AttendeeComboBoxDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
    , mStandardIndex(0)
{
Laurent Montel's avatar
Laurent Montel committed
40 41
    connect(this, &AttendeeComboBoxDelegate::closeEditor, this,
            &AttendeeComboBoxDelegate::doCloseEditor);
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
}

void AttendeeComboBoxDelegate::addItem(const QIcon &icon, const QString &text)
{
    QPair<QIcon, QString> pair;
    pair.first = icon;
    pair.second = text;
    mEntries << pair;
}

void AttendeeComboBoxDelegate::clear()
{
    mEntries.clear();
}

void AttendeeComboBoxDelegate::setToolTip(const QString &tT)
{
    mToolTip = tT;
}

void AttendeeComboBoxDelegate::setWhatsThis(const QString &wT)
{
    mWhatsThis = wT;
}

void AttendeeComboBoxDelegate::setStandardIndex(int index)
{
    mStandardIndex = index;
}

Laurent Montel's avatar
Laurent Montel committed
72
QWidget *AttendeeComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */, const QModelIndex & /* index */) const
73 74 75
{
    AttendeeComboBox *editor = new AttendeeComboBox(parent);

Laurent Montel's avatar
Laurent Montel committed
76
    for (const QPair<QIcon, QString> &pair : qAsConst(mEntries)) {
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        editor->addItem(pair.first, pair.second);
    }

    connect(editor, &AttendeeComboBox::leftPressed, this, &AttendeeComboBoxDelegate::leftPressed);
    connect(editor, &AttendeeComboBox::rightPressed, this, &AttendeeComboBoxDelegate::rightPressed);

    editor->setPopupMode(QToolButton::MenuButtonPopup);
    editor->setToolTip(mToolTip);
    editor->setWhatsThis(mWhatsThis);
    return editor;
}

void AttendeeComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    AttendeeComboBox *comboBox = static_cast<AttendeeComboBox *>(editor);
    int value = index.model()->data(index, Qt::EditRole).toUInt();
    if (value >= mEntries.count()) {
        value = mStandardIndex;
    }
    comboBox->setCurrentIndex(value);
}

Laurent Montel's avatar
Laurent Montel committed
99
void AttendeeComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
100 101 102 103 104 105
{
    AttendeeComboBox *comboBox = static_cast<AttendeeComboBox *>(editor);
    model->setData(index, comboBox->currentIndex(), Qt::EditRole);
    comboBox->menu()->close();
}

Laurent Montel's avatar
Laurent Montel committed
106
void AttendeeComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & /* index */) const
107 108 109 110
{
    editor->setGeometry(option.rect);
}

Laurent Montel's avatar
Laurent Montel committed
111
void AttendeeComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
112
{
Laurent Montel's avatar
Laurent Montel committed
113
    QStyleOptionButton myOption;
114 115 116 117 118 119 120 121 122

    int value = index.model()->data(index).toUInt();
    if (value >= mEntries.count()) {
        value = mStandardIndex;
    }

    myOption.rect = option.rect;
    myOption.state = option.state;
    myOption.icon = mEntries[value].first;
123
    myOption.iconSize = myOption.icon.actualSize(option.rect.size());
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

    QApplication::style()->drawControl(QStyle::CE_PushButton, &myOption, painter);
}

bool AttendeeComboBoxDelegate::eventFilter(QObject *editor, QEvent *event)
{
    if (event->type() == QEvent::Enter) {
        AttendeeComboBox *comboBox = static_cast<AttendeeComboBox *>(editor);
        comboBox->showMenu();
        return editor->eventFilter(editor, event);
    }

    return QStyledItemDelegate::eventFilter(editor, event);
}

void AttendeeComboBoxDelegate::doCloseEditor(QWidget *editor)
{
    AttendeeComboBox *comboBox = static_cast<AttendeeComboBox *>(editor);
    comboBox->menu()->close();
}

void AttendeeComboBoxDelegate::leftPressed()
{
Laurent Montel's avatar
Laurent Montel committed
147 148
    Q_EMIT closeEditor(
        static_cast<QWidget *>(QObject::sender()), QAbstractItemDelegate::EditPreviousItem);
149 150 151 152
}

void AttendeeComboBoxDelegate::rightPressed()
{
Laurent Montel's avatar
Laurent Montel committed
153 154
    Q_EMIT closeEditor(
        static_cast<QWidget *>(QObject::sender()), QAbstractItemDelegate::EditNextItem);
155 156
}

Laurent Montel's avatar
Laurent Montel committed
157
bool AttendeeComboBoxDelegate::helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index)
158 159 160 161 162 163
{
    if (!event || !view) {
        return false;
    }
    switch (event->type()) {
#ifndef QT_NO_TOOLTIP
Laurent Montel's avatar
Laurent Montel committed
164 165
    case QEvent::ToolTip:
    {
Laurent Montel's avatar
Laurent Montel committed
166
        QToolTip::showText(event->globalPos(), mToolTip, view);
167 168 169 170 171 172
        return true;
    }
#endif
#ifndef QT_NO_WHATSTHIS
    case QEvent::QueryWhatsThis:
        return true;
Laurent Montel's avatar
Laurent Montel committed
173 174
    case QEvent::WhatsThis:
    {
Laurent Montel's avatar
Laurent Montel committed
175
        QWhatsThis::showText(event->globalPos(), mWhatsThis, view);
176 177 178 179 180 181 182 183
        return true;
    }
#endif
    default:
        break;
    }
    return QStyledItemDelegate::helpEvent(event, view, option, index);
}