summaryrefslogtreecommitdiffstats
blob: 03af455b18706580a5cb9fe0a1020bba76fc9d93 (plain)
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
/*
 * Copyright (c) 2017 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swift/QtUI/QtSoundSelectionStyledItemDelegate.h>

#include <QApplication>
#include <QComboBox>
#include <QEvent>
#include <QFileDialog>
#include <QMenu>
#include <QMouseEvent>
#include <QPainter>
#include <QStyle>
#include <QStyleOptionComboBox>

#include <Swiften/Base/Log.h>

#include <Swift/QtUI/QtSwiftUtil.h>

namespace Swift {

QtSoundSelectionStyledItemDelegate::QtSoundSelectionStyledItemDelegate(QObject* parent) : QStyledItemDelegate(parent) {

}

void QtSoundSelectionStyledItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
    // draw item selected background
    painter->fillRect(option.rect, option.state & QStyle::State_Selected ? option.palette.highlight() : option.palette.base());

    auto editRoleString = index.data(Qt::EditRole).toString();

    // draw combo box
    QStyleOptionComboBox opt;
    opt.rect = option.rect;
    opt.rect.setHeight(opt.rect.height() + 2);
    opt.state = QStyle::State_Active | QStyle::State_Enabled;
    if (editRoleString.isEmpty()) {
        opt.currentText = tr("No sound");
    }
    else if (editRoleString == "defaultSound") {
        opt.currentText = tr("Default sound");
    }
    else {
        opt.currentText = editRoleString;
    }

    painter->save();
    QFont smallFont;
    smallFont.setPointSize(smallFont.pointSize() - 3);
    painter->setFont(smallFont);

    QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &opt, painter);
    QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &opt, painter);
    painter->restore();
}

bool QtSoundSelectionStyledItemDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& /*option*/, const QModelIndex& index) {
    if (event->type() == QEvent::MouseButtonRelease) {
        auto mouseEvent = dynamic_cast<QMouseEvent*>(event);
        assert(mouseEvent);
        auto editRoleString = index.data(Qt::EditRole).toString();

        auto popUpMenu = new QMenu();

        auto noSound = popUpMenu->addAction(tr("No sound"));
        auto defaultSound = popUpMenu->addAction(tr("Default sound"));
        QAction* customSoundFile = nullptr;
        QAction* selectedAction = nullptr;
        if (editRoleString.isEmpty()) {
            selectedAction = noSound;
        }
        else if (editRoleString == "defaultSound") {
            selectedAction = defaultSound;
        }
        else {
            customSoundFile = popUpMenu->addAction(editRoleString);
            selectedAction = customSoundFile;
        }
        if (selectedAction) {
            selectedAction->setCheckable(true);
            selectedAction->setChecked(true);
        }

        selectedAction = popUpMenu->exec(mouseEvent->globalPos(), selectedAction);

        if (selectedAction == defaultSound) {
            model->setData(index, "defaultSound", Qt::EditRole);
        }
        else if (selectedAction == noSound) {
            model->setData(index, "", Qt::EditRole);
        }

        delete popUpMenu;
    }
    return true;
}

};