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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
#include "canvasframerate.h"
#include <QtGui/qwidget.h>
#include <QtGui/qpainter.h>
#include <QtGui/qscrollbar.h>
#include <QtDeclarative/qmldebugclient.h>
#include <QtCore/qdebug.h>
#include <QtCore/qstringlist.h>
#include <QtCore/qdatastream.h>
#include <QtGui/qboxlayout.h>
#include <QResizeEvent>
#include <QShowEvent>
#include <QTabWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QCheckBox>
QT_BEGIN_NAMESPACE
class QLineGraph : public QWidget
{
Q_OBJECT
public:
QLineGraph(QWidget * = 0);
void setPosition(int);
public slots:
void addSample(int, int, int, int, bool);
protected:
virtual void paintEvent(QPaintEvent *);
virtual QSize sizeHint() const;
private slots:
void scrollbarChanged(int);
private:
void updateScrollbar();
void drawSample(QPainter *, int, const QRect &);
void drawTime(QPainter *, const QRect &);
struct Sample {
int sample[4];
bool isBreak;
};
QList<Sample> _samples;
QScrollBar sb;
int position;
int samplesPerWidth;
int resolutionForHeight;
bool ignoreScroll;
};
QLineGraph::QLineGraph(QWidget *parent)
: QWidget(parent), sb(Qt::Horizontal, this), position(-1), samplesPerWidth(99), resolutionForHeight(50), ignoreScroll(false)
{
sb.setMaximum(0);
sb.setMinimum(0);
sb.setSingleStep(1);
QVBoxLayout *layout = new QVBoxLayout;
setLayout(layout);
layout->addStretch(2);
layout->addWidget(&sb);
QObject::connect(&sb, SIGNAL(valueChanged(int)), this, SLOT(scrollbarChanged(int)));
}
QSize QLineGraph::sizeHint() const
{
return QSize(800, 600);
}
void QLineGraph::scrollbarChanged(int v)
{
if(ignoreScroll)
return;
if (v == sb.maximum())
position = -1;
else
position = v;
update();
}
void QLineGraph::updateScrollbar()
{
ignoreScroll = true;
sb.setMaximum(qMax(0, _samples.count() - samplesPerWidth - 1));
if(position == -1) {
sb.setValue(sb.maximum());
} else {
sb.setValue(position);
}
ignoreScroll = false;
}
void QLineGraph::addSample(int a, int b, int c, int d, bool isBreak)
{
Sample s;
s.isBreak = isBreak;
s.sample[0] = a;
s.sample[1] = b;
s.sample[2] = c;
s.sample[3] = d;
_samples << s;
updateScrollbar();
update();
}
void QLineGraph::setPosition(int p)
{
scrollbarChanged(p);
}
void QLineGraph::drawTime(QPainter *p, const QRect &rect)
{
if(_samples.isEmpty())
return;
int first = position;
if(first == -1)
first = qMax(0, _samples.count() - samplesPerWidth - 1);
int last = qMin(_samples.count() - 1, first + samplesPerWidth);
qreal scaleX = qreal(rect.width()) / qreal(samplesPerWidth);
int t = 0;
for(int ii = first; ii <= last; ++ii) {
int sampleTime = _samples.at(ii).sample[3] / 1000;
if(sampleTime != t) {
int xEnd = rect.left() + scaleX * (ii - first);
p->drawLine(xEnd, rect.bottom(), xEnd, rect.bottom() + 7);
QRect text(xEnd - 30, rect.bottom() + 10, 60, 30);
p->drawText(text, Qt::AlignHCenter | Qt::AlignTop, QString::number(_samples.at(ii).sample[3]));
t = sampleTime;
}
}
}
void QLineGraph::drawSample(QPainter *p, int s, const QRect &rect)
{
if(_samples.isEmpty())
return;
int first = position;
if(first == -1)
first = qMax(0, _samples.count() - samplesPerWidth - 1);
int last = qMin(_samples.count() - 1, first + samplesPerWidth);
qreal scaleY = rect.height() / resolutionForHeight;
qreal scaleX = qreal(rect.width()) / qreal(samplesPerWidth);
int xEnd;
int lastXEnd = rect.left();
p->save();
p->setPen(Qt::NoPen);
for(int ii = first + 1; ii <= last; ++ii) {
xEnd = rect.left() + scaleX * (ii - first);
int yEnd = rect.bottom() - _samples.at(ii).sample[s] * scaleY;
if (!(s == 0 && _samples.at(ii).isBreak))
p->drawRect(QRect(lastXEnd, yEnd, scaleX, _samples.at(ii).sample[s] * scaleY));
lastXEnd = xEnd;
}
p->restore();
}
void QLineGraph::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
QRect r(50, 10, width() - 60, height() - 60);
p.setBrush(QColor("lightsteelblue"));
drawSample(&p, 0, r);
p.setBrush(QColor("pink"));
drawSample(&p, 1, r);
p.setBrush(QColor("green"));
drawSample(&p, 2, r);
p.setBrush(Qt::NoBrush);
p.drawRect(r);
for(int ii = 0; ii <= resolutionForHeight; ++ii) {
int y = 1 + r.bottom() - ii * r.height() / resolutionForHeight;
if((ii % 10) == 0) {
p.drawLine(r.left() - 20, y, r.left(), y);
QRect text(r.left() - 20 - 53, y - 10, 50, 20);
p.drawText(text, Qt::AlignRight | Qt::AlignVCenter, QString::number(ii));
} else {
p.drawLine(r.left() - 7, y, r.left(), y);
}
}
drawTime(&p, r);
}
class CanvasFrameRatePlugin : public QmlDebugClient
{
Q_OBJECT
public:
CanvasFrameRatePlugin(QmlDebugConnection *client);
signals:
void sample(int, int, int, int, bool);
protected:
virtual void messageReceived(const QByteArray &);
private:
int la;
int lb;
int ld;
};
CanvasFrameRatePlugin::CanvasFrameRatePlugin(QmlDebugConnection *client)
: QmlDebugClient(QLatin1String("CanvasFrameRate"), client), la(-1)
{
}
void CanvasFrameRatePlugin::messageReceived(const QByteArray &data)
{
QByteArray rwData = data;
QDataStream stream(&rwData, QIODevice::ReadOnly);
int a; int b; int c; int d; bool isBreak;
stream >> a >> b >> c >> d >> isBreak;
if (la != -1)
emit sample(c, lb, la, ld, isBreak);
la = a;
lb = b;
ld = d;
}
CanvasFrameRate::CanvasFrameRate(QmlDebugConnection *client, QWidget *parent)
: QWidget(parent)
{
m_plugin = new CanvasFrameRatePlugin(client);
QVBoxLayout *layout = new QVBoxLayout;
layout->setContentsMargins(0,0,0,0);
layout->setSpacing(0);
setLayout(layout);
m_tabs = new QTabWidget(this);
layout->addWidget(m_tabs);
QHBoxLayout *bottom = new QHBoxLayout;
layout->addLayout(bottom);
bottom->addStretch(2);
QCheckBox *check = new QCheckBox("Enable", this);
bottom->addWidget(check);
QObject::connect(check, SIGNAL(stateChanged(int)),
this, SLOT(stateChanged(int)));
QPushButton *pb = new QPushButton(tr("New Tab"), this);
QObject::connect(pb, SIGNAL(clicked()), this, SLOT(newTab()));
bottom->addWidget(pb);
newTab();
}
void CanvasFrameRate::newTab()
{
if (m_tabs->count()) {
QWidget *w = m_tabs->widget(m_tabs->count() - 1);
QObject::disconnect(m_plugin, SIGNAL(sample(int,int,int,int,bool)),
w, SLOT(addSample(int,int,int,int,bool)));
}
int id = m_tabs->count();
QLineGraph *graph = new QLineGraph(this);
QObject::connect(m_plugin, SIGNAL(sample(int,int,int,int,bool)),
graph, SLOT(addSample(int,int,int,int,bool)));
QString name = QLatin1String("Graph ") + QString::number(id);
m_tabs->addTab(graph, name);
m_tabs->setCurrentIndex(id);
}
void CanvasFrameRate::stateChanged(int s)
{
bool checked = s != 0;
static_cast<QmlDebugClient *>(m_plugin)->setEnabled(checked);
}
QT_END_NAMESPACE
#include "canvasframerate.moc"
|