Blame view
SpeakersMonitor.cpp
2.63 KB
3f2992b2c
|
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 |
#include <QPainter> #include <QDebug> #include "SpeakersMonitor.h" SpeakersMonitor::SpeakersMonitor(int labelHeight, QWidget *parent) : QWidget(parent), m_maxDensity(0.0), m_labelHeight(labelHeight) { } /////////// // slots // /////////// void SpeakersMonitor::speakersRetrieved(QList<QString> speakers) { const int n(speakers.size()); const qreal lambda(201.0/200.0); QString currSpeaker; qreal currDensity; QList<QString> speakerLabels; // retrieving speaker labels for (int i(0); i < speakers.size(); i++) if (!(currSpeaker = speakers[i]).isEmpty() && currSpeaker != "S" && !speakerLabels.contains(currSpeaker)) speakerLabels.push_back(currSpeaker); // computing speaker density in each time position QVector<qreal> left(n); QVector<qreal> right(n); QList<qreal> densityValues; for (int i(0); i < speakerLabels.size(); i++) { currSpeaker = speakerLabels[i]; densityValues.clear(); left[0] = right[n-1] = 0; for (int i(1); i < n; i++) { left[i] = (left[i-1] + (speakers[i-1] == currSpeaker)) / lambda; right[n-i-1] = (right[n-i] + (speakers[n-i] == currSpeaker)) / lambda; } for (int i(0); i < n; i++) { currDensity = left[i] + (speakers[i] == currSpeaker) + right[i]; densityValues.push_back(currDensity); if (currDensity > m_maxDensity) m_maxDensity = currDensity; } m_density.insert(currSpeaker, densityValues); } setFixedSize(480, m_density.keys().size() * m_labelHeight); } void SpeakersMonitor::positionChanged(qint64 position) { m_position = position / 40 - 1; update(); } void SpeakersMonitor::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QString currSpeaker; QList<qreal> densityValues; QPainter painter(this); QFont font; font.setPointSize(m_labelHeight-2); painter.setFont(font); int x1, x2, y; m_it = m_density.constBegin(); int i(1); int maxWidth(0); while (m_it != m_density.constEnd()) { currSpeaker = m_it.key(); QRect rect = painter.boundingRect(QRect(0, i * m_labelHeight - 1, 10, m_labelHeight), Qt::AlignHCenter, currSpeaker); if (rect.width() > maxWidth) maxWidth = rect.width(); painter.drawText(QPoint(0, i * m_labelHeight - 1), currSpeaker); i++; m_it++; } m_it = m_density.constBegin(); i = 0; qreal ratio = (width() - maxWidth) / m_maxDensity; while (m_it != m_density.constEnd()) { currSpeaker = m_it.key(); densityValues = m_it.value(); x1 = maxWidth; x2 = maxWidth + densityValues[m_position] * ratio; y = i * m_labelHeight + m_labelHeight / 2; painter.drawLine(x1, y, x2, y); i++; m_it++; } } |