Debian Codegraph @

src/imports/PerformanceMetrics/plugin/upmgraphmodel.cpp

raw
1 /*
2 * Copyright 2014 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Author: Florian Boucault <[email protected]>
17 */
18
19 #include "upmgraphmodel.h"
20
21 #include <QtCore/QtMath>
22
23 UPMGraphModel::UPMGraphModel(QObject *parent) :
24 QObject(parent),
25 m_shift(0),
26 m_samples(100),
27 m_currentValue(0)
28 {
29 m_image = QImage(m_samples, 1, QImage::Format_RGB32);
30 m_image.fill(0);
31 }
32
33 void UPMGraphModel::appendValue(int width, int value)
34 {
35 /* FIXME: modifying m_image here implicitly triggers a deep copy
36 of its data because UCTextureFromImage usually holds a reference
37 to that image
38 */
39 width = qMax(1, width);
40 QRgb* line = (QRgb*)m_image.scanLine(0);
41
42 if (width >= m_image.width()) {
43 memset(&line[0], value, m_image.width() * 4);
44 } else if (m_shift + width > m_image.width()) {
45 int after = m_image.width() - m_shift;
46 int before = width - after;
47 memset(&line[m_shift], value, after * 4);
48 memset(&line[0], value, before * 4);
49 } else {
50 memset(&line[m_shift], value, width * 4);
51 }
52 m_shift = (m_shift + width) % m_samples;
53 m_currentValue = value;
54
55 Q_EMIT imageChanged();
56 Q_EMIT shiftChanged();
57 Q_EMIT currentValueChanged();
58 }
59
60 QImage UPMGraphModel::image() const
61 {
62 return m_image;
63 }
64
65 int UPMGraphModel::shift() const
66 {
67 return m_shift;
68 }
69
70 int UPMGraphModel::samples() const
71 {
72 return m_samples;
73 }
74
75 void UPMGraphModel::setSamples(int samples)
76 {
77 if (samples != m_samples) {
78 m_samples = samples;
79 m_image = QImage(m_samples, 1, QImage::Format_RGB32);
80 m_image.fill(0);
81 Q_EMIT samplesChanged();
82 Q_EMIT imageChanged();
83 }
84 }
85
86 int UPMGraphModel::currentValue() const
87 {
88 return m_currentValue;
89 }