1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
27 #include "vogleditor_qtimelineview.h"
28 #include "vogleditor_frameitem.h"
30 vogleditor_QTimelineView::vogleditor_QTimelineView(QWidget *parent) :
33 m_curApiCallNumber(0),
37 QLinearGradient gradient(QPointF(50, -20), QPointF(80, 20));
38 gradient.setColorAt(0.0, Qt::white);
39 gradient.setColorAt(1.0, QColor(0xa6, 0xce, 0x39));
41 m_background = QBrush(QColor(200,200,200));//QBrush(parent->palette().brush(parent->backgroundRole()));
42 m_triangleBrush = QBrush(gradient);
43 m_trianglePen = QPen(Qt::black);
44 m_trianglePen.setWidth(1);
45 m_textPen = QPen(Qt::white);
46 m_textFont.setPixelSize(50);
48 m_horizontalScale = 1;
52 vogleditor_QTimelineView::~vogleditor_QTimelineView()
57 void vogleditor_QTimelineView::paintEvent(QPaintEvent *event)
61 paint(&painter, event);
65 void vogleditor_QTimelineView::drawBaseTimeline(QPainter* painter, const QRect& rect, int gap)
69 // fill entire background with background color
70 painter->fillRect(rect, m_background);
72 // translate drawing to vertical center of rect
73 painter->translate(0, rect.height()/2);
75 painter->setBrush(m_triangleBrush);
76 painter->setPen(m_trianglePen);
78 // everything will have a small gap on the left and right sides
79 painter->translate(gap, 0);
81 // draw the actual timeline
82 int lineLength = rect.width()-2*gap;
83 painter->drawLine(0,0, lineLength, 0);
88 void vogleditor_QTimelineView::paint(QPainter *painter, QPaintEvent *event)
92 int arrowTop = event->rect().height()/2-gap-arrowHeight;
93 int arrowHalfWidth = 3;
94 m_lineLength = event->rect().width()-2*gap;
97 triangle.setPoint(0, 0, arrowTop);
98 triangle.setPoint(1, -arrowHalfWidth, arrowTop+arrowHeight);
99 triangle.setPoint(2, arrowHalfWidth, arrowTop+arrowHeight);
101 drawBaseTimeline(painter, event->rect(), gap);
103 if (m_pModel == NULL)
108 if (m_pModel->get_root_item() == NULL)
113 if (m_pPixmap != NULL)
115 if (m_pPixmap->height() != event->rect().height() ||
116 m_pPixmap->width() != event->rect().width())
122 if (m_pPixmap == NULL)
124 m_pPixmap = new QPixmap(event->rect().width(), event->rect().height());
125 QPainter pixmapPainter(m_pPixmap);
126 drawBaseTimeline(&pixmapPainter, event->rect(), gap);
128 // translate drawing to vertical center of rect
129 // everything will have a small gap on the left and right sides
130 pixmapPainter.translate(gap, event->rect().height()/2);
132 if (m_pModel->get_root_item()->getBrush() == NULL)
134 m_pModel->get_root_item()->setBrush(&m_triangleBrush);
137 m_horizontalScale = (float)m_lineLength / (float)m_pModel->get_root_item()->getDuration();
139 // we don't want to draw the root item, but all of its children
140 int numChildren = m_pModel->get_root_item()->childCount();
141 int height = event->rect().height()/2-2*gap;
143 pixmapPainter.setBrush(m_triangleBrush);
144 pixmapPainter.setPen(m_trianglePen);
146 for (int c = 0; c < numChildren; c++)
148 vogleditor_timelineItem* pChild = m_pModel->get_root_item()->child(c);
149 drawTimelineItem(&pixmapPainter, pChild, height);
153 painter->drawPixmap(event->rect(), *m_pPixmap, event->rect());
155 // translate drawing to vertical center of rect
156 // everything will have a small gap on the left and right sides
157 painter->translate(gap, event->rect().height()/2);
159 painter->setBrush(m_triangleBrush);
160 painter->setPen(m_trianglePen);
162 int numChildren = m_pModel->get_root_item()->childCount();
163 for (int c = 0; c < numChildren; c++)
165 vogleditor_timelineItem* pChild = m_pModel->get_root_item()->child(c);
167 // draw current frame marker
168 if (pChild->getFrameItem() != NULL && pChild->getFrameItem()->frameNumber() == m_curFrame)
171 painter->translate(scalePositionHorizontally(pChild->getBeginTime()), 0);
172 painter->drawPolygon(triangle);
176 // draw current api call marker
177 if (pChild->getApiCallItem() != NULL && pChild->getApiCallItem()->globalCallIndex() == m_curApiCallNumber)
180 painter->translate(scalePositionHorizontally(pChild->getBeginTime()), 0);
181 painter->drawPolygon(triangle);
187 float vogleditor_QTimelineView::scaleDurationHorizontally(float value)
189 float scaled = value * m_horizontalScale;
198 float vogleditor_QTimelineView::scalePositionHorizontally(float value)
200 float horizontalShift = m_pModel->get_root_item()->getBeginTime();
201 float horizontalLength = m_pModel->get_root_item()->getDuration();
202 float offset = ((value - horizontalShift) / horizontalLength) * m_lineLength;
207 void vogleditor_QTimelineView::drawTimelineItem(QPainter* painter, vogleditor_timelineItem *pItem, int height)
209 float duration = pItem->getDuration();
216 if (pItem->isMarker())
218 painter->setBrush(m_triangleBrush);
219 painter->setPen(m_trianglePen);
221 float offset = scalePositionHorizontally(pItem->getBeginTime());
222 painter->drawLine(QLineF(offset, -height, offset, height));
226 float durationRatio = duration / m_maxItemDuration;
227 int intensity = std::min(255, (int)(durationRatio * 255.0f));
228 // painter->setBrush(*(pItem->getBrush()));
229 QColor color(intensity, 255-intensity, 0);
230 painter->setBrush(QBrush(color));
231 painter->setPen(color);
233 rect.setLeft(scalePositionHorizontally(pItem->getBeginTime()));
234 rect.setTop(-height/2);
235 rect.setWidth(scaleDurationHorizontally(duration));
236 rect.setHeight(height);
237 painter->drawRect(rect);
239 // now draw all children
240 int numChildren = pItem->childCount();
241 for (int c = 0; c < numChildren; c++)
243 drawTimelineItem(painter, pItem->child(c), height-1);