summaryrefslogtreecommitdiffstats
blob: 1c3ed7f594b6d08f4ea2a7fb09b3c4640d2138ba (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
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swift/QtUI/EventViewer/EventDelegate.h>

#include <QDebug>

#include <Swift/Controllers/XMPPEvents/ErrorEvent.h>
#include <Swift/Controllers/XMPPEvents/IncomingFileTransferEvent.h>
#include <Swift/Controllers/XMPPEvents/MUCInviteEvent.h>
#include <Swift/Controllers/XMPPEvents/MessageEvent.h>
#include <Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h>

namespace Swift {

EventDelegate::EventDelegate() : QStyledItemDelegate(),
    messageDelegate_(QtEvent::SenderRole, Qt::DisplayRole, false),
    subscriptionDelegate_(QtEvent::SenderRole, Qt::DisplayRole, true),
    errorDelegate_(QtEvent::SenderRole, Qt::DisplayRole, true),
    mucInviteDelegate_(QtEvent::SenderRole, Qt::DisplayRole, false),
    incomingFileTransferDelegate_(QtEvent::SenderRole, Qt::DisplayRole, false) {

}

QSize EventDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index ) const {
    QtEvent* item = static_cast<QtEvent*>(index.internalPointer());
    if (!item) {
        return QStyledItemDelegate::sizeHint(option, index);
    }
    switch (getEventType(item->getEvent())) {
        case MessageEventType: return messageDelegate_.sizeHint(option, item);
        case SubscriptionEventType: return subscriptionDelegate_.sizeHint(option, item);
        case ErrorEventType: return errorDelegate_.sizeHint(option, item);
        case MUCInviteEventType: return mucInviteDelegate_.sizeHint(option, item);
        case IncomingFileTransferEventType: return incomingFileTransferDelegate_.sizeHint(option, item);
    }
    assert(false);
    return QSize();
}

void EventDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
    QtEvent* item = static_cast<QtEvent*>(index.internalPointer());
    if (!item) {
        QStyledItemDelegate::paint(painter, option, index);
        return;
    }
    switch (getEventType(item->getEvent())) {
        case MessageEventType: messageDelegate_.paint(painter, option, item);break;
        case SubscriptionEventType: subscriptionDelegate_.paint(painter, option, item);break;
        case ErrorEventType: errorDelegate_.paint(painter, option, item);break;
        case MUCInviteEventType: mucInviteDelegate_.paint(painter, option, item);break;
        case IncomingFileTransferEventType: incomingFileTransferDelegate_.paint(painter, option, item);break;
    }
}

EventType EventDelegate::getEventType(std::shared_ptr<StanzaEvent> event) const {
    std::shared_ptr<MessageEvent> messageEvent = std::dynamic_pointer_cast<MessageEvent>(event);
    if (messageEvent) {
        return MessageEventType;
    }
    std::shared_ptr<SubscriptionRequestEvent> subscriptionEvent = std::dynamic_pointer_cast<SubscriptionRequestEvent>(event);
    if (subscriptionEvent) {
        return SubscriptionEventType;
    }
    std::shared_ptr<ErrorEvent> errorEvent = std::dynamic_pointer_cast<ErrorEvent>(event);
    if (errorEvent) {
        return ErrorEventType;
    }
    std::shared_ptr<MUCInviteEvent> mucInviteEvent = std::dynamic_pointer_cast<MUCInviteEvent>(event);
    if (mucInviteEvent) {
        return MUCInviteEventType;
    }
    std::shared_ptr<IncomingFileTransferEvent> incomingFileTransferEvent = std::dynamic_pointer_cast<IncomingFileTransferEvent>(event);
    if (incomingFileTransferEvent) {
        return IncomingFileTransferEventType;
    }
    //I don't know what this is.
    assert(false);
    return MessageEventType;
}

}