File: histogramview.cpp

package info (click to toggle)
apitrace 7.1%2Bgit20170623.d38a69d6%2Brepack-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 15,992 kB
  • sloc: cpp: 179,347; ansic: 62,439; python: 33,058; java: 377; makefile: 105; sh: 26; xml: 26
file content (258 lines) | stat: -rw-r--r-- 6,652 bytes parent folder | download | duplicates (6)
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
#include "histogramview.h"

#include <QPen>
#include <QBrush>
#include <qmath.h>
#include <QPainter>
#include <QToolTip>
#include <QMouseEvent>

HistogramView::HistogramView(QWidget* parent) :
    GraphView(parent),
    m_data(NULL)
{
    setMouseTracking(true);

    m_gradientUnselected.setColorAt(0.9, QColor(200, 200, 200));
    m_gradientUnselected.setColorAt(0.0, QColor(220, 220, 220));

    m_gradientSelected.setColorAt(0.9, QColor(0, 0, 210));
    m_gradientSelected.setColorAt(0.0, QColor(130, 130, 255));
}


void HistogramView::setDataProvider(GraphDataProvider* data)
{
    delete m_data;
    m_data = data;

    if (m_data) {
        m_data->setSelectionState(m_selectionState);
        setDefaultView(0, m_data->size());
        m_viewWidthMin = 10;
    } else {
        setDefaultView(0, 0);
    }
}


void HistogramView::setSelectionState(SelectionState* state)
{
    if (m_data) {
        m_data->setSelectionState(state);
    }

    GraphView::setSelectionState(state);
}


void HistogramView::setSelectedGradient(const QLinearGradient& gradient)
{
    m_gradientSelected = gradient;
}


void HistogramView::setUnelectedGradient(const QLinearGradient& gradient)
{
    m_gradientUnselected = gradient;
}


void HistogramView::mouseMoveEvent(QMouseEvent *e)
{
    GraphView::mouseMoveEvent(e);

    if (e->buttons() || !m_data) {
        QToolTip::hideText();
        return;
    }

    qint64 index = itemAtPosition(e->pos());
    qint64 time = valueAtPosition(e->pos());

    if (m_data->value(index) >= time) {
        QToolTip::showText(e->globalPos(), m_data->itemTooltip(index));
    } else {
        QToolTip::hideText();
    }
}


void HistogramView::mouseDoubleClickEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        qint64 index = itemAtPosition(e->pos());
        qint64 time = valueAtPosition(e->pos());

        if (m_data->value(index) >= time) {
            m_data->itemDoubleClicked(index);
            return;
        }
    }

    GraphView::mouseDoubleClickEvent(e);
}


void HistogramView::update()
{
    m_graphBottom = 0;
    m_graphTop = 0;

    if (m_data) {
        for (qint64 i = m_viewLeft; i < m_viewRight; ++i) {
            qint64 value = m_data->value(i);

            if (value > m_graphTop) {
                m_graphTop = value;
            }
        }
    }

    GraphView::update();
}


void HistogramView::resizeEvent(QResizeEvent *)
{
    m_gradientSelected.setStart(0, height());
    m_gradientUnselected.setStart(0, height());
}


/* Draw the histogram
 *
 * When the view is zoomed such that there is more than one item occupying a single pixel
 * the one with the highest value will be displayed.
 */
void HistogramView::paintEvent(QPaintEvent *)
{
    if (!m_data) {
        return;
    }

    QBrush selectedBrush = QBrush(m_gradientSelected);
    QPen selectedPen = QPen(selectedBrush, 1);

    QBrush unselectedBrush = QBrush(m_gradientUnselected);
    QPen unselectedPen = QPen(unselectedBrush, 1);

    QPainter painter(this);
    painter.fillRect(0, 0, width(), height(), Qt::white);

    double dydv = height() / (double)m_graphTop;
    double dxdv = width() / (double)(m_viewRight - m_viewLeft);
    bool selection = m_selectionState && m_selectionState->type != SelectionState::None;

    if (dxdv < 1.0) {
        /* Less than one pixel per item */
        qint64 longestValue = m_graphBottom;
        qint64 longestSelected = m_graphBottom;
        int lastX = 0;
        double x = 0;

        if (selection) {
            painter.setPen(unselectedPen);
        } else {
            painter.setPen(selectedPen);
        }

        for (qint64 i = m_viewLeft; i < m_viewRight; ++i) {
            qint64 value = m_data->value(i);
            int ix;

            if (value > longestValue) {
                longestValue = value;
            }

            if (selection && m_data->selected(i) && value > longestSelected) {
                longestSelected = value;
            }

            x += dxdv;
            ix = (int)x;

            if (lastX != ix) {
                painter.drawLine(lastX, height(), lastX, height() - (longestValue * dydv));

                if (selection && longestSelected > m_graphBottom) {
                    painter.setPen(selectedPen);
                    painter.drawLine(lastX, height(), lastX, height() - (longestSelected * dydv));
                    painter.setPen(unselectedPen);
                    longestSelected = m_graphBottom;
                }

                longestValue = m_graphBottom;
                lastX = ix;
            }
        }
    } else {
        /* Draw rectangles for graph */
        double x = 0;

        for (qint64 i = m_viewLeft; i < m_viewRight; ++i, x += dxdv) {
            qint64 value = m_data->value(i);
            int y = qMax<int>(1, value * dydv);

            if (!selection || m_data->selected(i)) {
                painter.fillRect(x, height() - y, dxdv, y, selectedBrush);
            } else {
                painter.fillRect(x, height() - y, dxdv, y, unselectedBrush);
            }
        }
    }

    /* Draw the borders for the selection */
    if (m_selectionState && m_selectionState->type == SelectionState::Horizontal) {
        double dxdt = width() / (double)m_viewWidth;
        double scroll = m_viewLeft * dxdt;
        double left = (m_selectionState->start * dxdt) - scroll;
        double right = (m_selectionState->end * dxdt) - scroll;

        painter.setPen(Qt::green);

        if (left >= 0 && left <= width()) {
            painter.drawLine(left, 0, left, height());
        }

        if (right >= 0 && right <= width()) {
            painter.drawLine(right, 0, right, height());
        }
    }
}


/* Find the item with the highest value at pos.x() +/- 1,
 * the mouse must be within the bar height-wise.
 */
qint64 HistogramView::itemAtPosition(QPoint pos) {
    double dvdx = m_viewWidth / (double)width();

    qint64 left = qFloor(dvdx * (pos.x() - 1)) + m_viewLeft;
    qint64 right = qCeil(dvdx * (pos.x() + 1)) + m_viewLeft;

    qint64 longestIndex = 0;
    qint64 longestValue = 0;

    left = qBound<qint64>(0, left, m_data->size() - 1);
    right = qBound<qint64>(0, right, m_data->size() - 1);

    for (qint64 i = left; i <= right; ++i) {
        if (m_data->value(i) > longestValue) {
            longestValue = m_data->value(i);
            longestIndex = i;
        }
    }

    return longestIndex;
}


/* Return the value at position */
qint64 HistogramView::valueAtPosition(QPoint pos) {
    double value = m_graphTop / (double)height();
    value *= height() - pos.y();
    value += m_graphBottom;
    return (qint64)value;
}