]> git.cworth.org Git - apitrace/blob - gui/apicalldelegate.cpp
221462e66b9996fdcff6b0c1f21a2d2b96753df5
[apitrace] / gui / apicalldelegate.cpp
1 #include "apicalldelegate.h"
2
3 #include "apitracecall.h"
4 #include "apitracemodel.h"
5
6 #include <QApplication>
7 #include <QDebug>
8 #include <QPainter>
9 #include <QStaticText>
10 #include <QStyle>
11
12 ApiCallDelegate::ApiCallDelegate(QWidget *parent)
13     : QStyledItemDelegate(parent),
14       m_stateEmblem(":/resources/dialog-information.png"),
15       m_editEmblem(":/resources/document-edit.png"),
16       m_errorEmblem(":/resources/script-error.png")
17 {
18 }
19
20 void ApiCallDelegate::paint(QPainter *painter,
21                             const QStyleOptionViewItem &option,
22                             const QModelIndex &index) const
23 {
24     QVariant var = index.data(ApiTraceModel::EventRole);
25     ApiTraceEvent *event = var.value<ApiTraceEvent*>();
26
27     Q_ASSERT(index.column() == 0);
28
29     if (event) {
30         QPoint offset = QPoint(0, 0);
31         QStaticText text = event->staticText();
32         //text.setTextWidth(option.rect.width());
33         //QStyledItemDelegate::paint(painter, option, index);
34         QStyle *style = QApplication::style();
35         style->drawControl(QStyle::CE_ItemViewItem, &option, painter, 0);
36
37         // draw thumbnail of frame
38         if(event->type() == ApiTraceEvent::Frame) {
39             ApiTraceFrame *frame = static_cast<ApiTraceFrame*>(event);
40             QImage thumbnail = frame->thumbnail();
41             if (!thumbnail.isNull()) {
42                 painter->drawImage(option.rect.topLeft() + offset, thumbnail);
43                 offset += QPoint(option.rect.height() + 16, 0);
44             }
45         }
46
47         if (event->hasState()) {
48             QPixmap px = m_stateEmblem.pixmap(option.rect.height(),
49                                               option.rect.height());
50             painter->drawPixmap(option.rect.topLeft(), px);
51             offset = QPoint(option.rect.height() + 5, 0);
52         }
53         if (event->type() == ApiTraceEvent::Call) {
54             ApiTraceCall *call = static_cast<ApiTraceCall*>(event);
55             if (call->hasError()) {
56                 QPixmap px = m_errorEmblem.pixmap(option.rect.height(),
57                                                   option.rect.height());
58                 painter->drawPixmap(option.rect.topLeft() + offset, px);
59                 offset += QPoint(option.rect.height() + 5, 0);
60             }
61             if (call->edited()) {
62                 QPixmap px = m_editEmblem.pixmap(option.rect.height(),
63                                                  option.rect.height());
64                 painter->drawPixmap(option.rect.topLeft() + offset, px);
65                 offset += QPoint(option.rect.height() + 5, 0);
66             }
67         }
68
69         painter->drawStaticText(option.rect.topLeft() + offset, text);
70     } else {
71         QStyledItemDelegate::paint(painter, option, index);
72     }
73 }
74
75 QSize ApiCallDelegate::sizeHint(const QStyleOptionViewItem &option,
76                                 const QModelIndex &index) const
77 {
78     ApiTraceEvent *event =
79         index.data(ApiTraceModel::EventRole).value<ApiTraceEvent*>();
80
81 #ifndef __APPLE__
82     /* XXX: This fails on MacOSX, but seems safe to ignore */
83     Q_ASSERT(index.column() == 0);
84 #endif
85
86     if (event) {
87         QStaticText text = event->staticText();
88         //text.setTextWidth(option.rect.width());
89         //qDebug()<<"text size = "<<text.size();
90         return text.size().toSize();
91     }
92     return QStyledItemDelegate::sizeHint(option, index);
93 }
94
95
96 #include "apicalldelegate.moc"