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