| 1 | /* |
| 2 | Copyright (C) 2005-2019 Mario Stephan <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published |
| 6 | by the Free Software Foundation; either version 2.1 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include "vumeter.h" |
| 19 | #include <QPainter> |
| 20 | #include <QStyleOption> |
| 21 | #include <QWidget> |
| 22 | |
| 23 | struct VUMeterPrivate { |
| 24 | double valueLeft; |
| 25 | double valueRight; |
| 26 | double peakLeft; |
| 27 | double peakRight; |
| 28 | Qt::Orientation orientation; |
| 29 | QTime peakTime; |
| 30 | int linesPerSegment; |
| 31 | int ledSize; |
| 32 | int spacesInSegments; |
| 33 | int segmentsPerPeak; |
| 34 | int margin; |
| 35 | int maxLevel; |
| 36 | int highLevel; |
| 37 | int step; |
| 38 | int spacesBetweenSegments; |
| 39 | QColor colBack; |
| 40 | QColor colValue; |
| 41 | }; |
| 42 | |
| 43 | VUMeter::VUMeter(QWidget* parent) |
| 44 | : QWidget(parent) |
| 45 | , p(new VUMeterPrivate) |
| 46 | { |
| 47 | setOrientation(Qt::Vertical); |
| 48 | |
| 49 | BackgroundColor.setRgb(40, 40, 40); |
| 50 | LevelColorNormal.setRgb(0, 200, 0); |
| 51 | LevelColorHigh.setRgb(200, 0, 0); |
| 52 | LevelColorOff.setRgb(80, 120, 120); |
| 53 | QPalette pal = palette(); |
| 54 | pal.setColor(QPalette::Window, BackgroundColor); |
| 55 | setPalette(pal); |
| 56 | p->valueLeft = p->valueRight = 0; |
| 57 | p->peakLeft = p->peakRight = 0; |
| 58 | p->margin = 1; |
| 59 | p->linesPerSegment = 1; |
| 60 | p->spacesBetweenSegments = 0; |
| 61 | p->step = p->linesPerSegment + p->spacesBetweenSegments; |
| 62 | p->segmentsPerPeak = 1; |
| 63 | p->colBack = QColor(60, 60, 60); |
| 64 | p->colValue = Qt::white; |
| 65 | } |
| 66 | |
| 67 | VUMeter::~VUMeter() |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | void VUMeter::setOrientation(Qt::Orientation o) |
| 72 | { |
| 73 | p->orientation = o; |
| 74 | } |
| 75 | |
| 76 | Qt::Orientation VUMeter::orientation() const |
| 77 | { |
| 78 | return p->orientation; |
| 79 | } |
| 80 | |
| 81 | void VUMeter::reset() |
| 82 | { |
| 83 | p->peakLeft = p->peakRight = 0; |
| 84 | p->valueLeft = p->valueRight = 0; |
| 85 | update(); |
| 86 | } |
| 87 | |
| 88 | void VUMeter::resizeEvent(QResizeEvent* e) |
| 89 | { |
| 90 | Q_UNUSED(e); |
| 91 | |
| 92 | int size = (orientation() == Qt::Vertical) ? width() : height(); |
| 93 | p->ledSize = ((size - p->margin) / 2); |
| 94 | |
| 95 | p->maxLevel = (((orientation() == Qt::Vertical) ? height() : width()) |
| 96 | / (p->linesPerSegment + p->step)) |
| 97 | * (p->linesPerSegment + p->step) |
| 98 | + p->margin; |
| 99 | p->highLevel = static_cast<int>(0.75 * p->maxLevel); |
| 100 | } |
| 101 | |
| 102 | void VUMeter::checkPeakTime() |
| 103 | { |
| 104 | if (p->peakTime.elapsed() >= 1000) { |
| 105 | p->peakLeft = 0; |
| 106 | p->peakRight = 0; |
| 107 | p->peakTime.restart(); |
| 108 | update(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void VUMeter::setValueLeft(double f) |
| 113 | { |
| 114 | double fl = f; |
| 115 | |
| 116 | if (fl < 0.0) |
| 117 | fl = 0.0; |
| 118 | |
| 119 | if (fl > p->peakLeft) { |
| 120 | p->peakLeft = fl; |
| 121 | p->peakTime.start(); |
| 122 | } |
| 123 | |
| 124 | if (p->valueLeft != fl) { |
| 125 | p->valueLeft = fl; |
| 126 | if (p->valueLeft == 0.0) |
| 127 | p->peakLeft = 0.0; |
| 128 | update(); |
| 129 | checkPeakTime(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void VUMeter::setPercentage(double f) |
| 134 | { |
| 135 | //qDebug() << "Level: "<< f; |
| 136 | p->valueRight = p->valueLeft = f; |
| 137 | update(); |
| 138 | } |
| 139 | |
| 140 | void VUMeter::setValueRight(double f) |
| 141 | { |
| 142 | double fr = f; |
| 143 | |
| 144 | if (fr < 0.0) |
| 145 | fr = 0.0; |
| 146 | if (fr > p->peakRight) { |
| 147 | p->peakRight = fr; |
| 148 | p->peakTime.start(); |
| 149 | } |
| 150 | |
| 151 | if (p->valueRight != fr) { |
| 152 | p->valueRight = fr; |
| 153 | if (p->valueRight == 0.0) |
| 154 | p->peakRight = 0.0; |
| 155 | update(); |
| 156 | checkPeakTime(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void VUMeter::setLinesPerSegment(int i) |
| 161 | { |
| 162 | p->linesPerSegment = i; |
| 163 | p->step = p->linesPerSegment + p->spacesBetweenSegments; |
| 164 | } |
| 165 | |
| 166 | void VUMeter::setSpacesBetweenSegments(int i) |
| 167 | { |
| 168 | p->spacesBetweenSegments = i; |
| 169 | p->step = p->linesPerSegment + p->spacesBetweenSegments; |
| 170 | } |
| 171 | |
| 172 | void VUMeter::setSegmentsPerPeak(int i) |
| 173 | { |
| 174 | p->segmentsPerPeak = i; |
| 175 | } |
| 176 | |
| 177 | void VUMeter::setMargin(int i) |
| 178 | { |
| 179 | p->margin = i; |
| 180 | } |
| 181 | |
| 182 | void VUMeter::paintEvent(QPaintEvent*) |
| 183 | { |
| 184 | drawMeter(); |
| 185 | QStyleOption opt; |
| 186 | opt.init(this); |
| 187 | QPainter p(this); |
| 188 | style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); |
| 189 | } |
| 190 | |
| 191 | void VUMeter::drawMeter() |
| 192 | { |
| 193 | |
| 194 | QPainter painter(this); |
| 195 | |
| 196 | int valueLeft = 0; |
| 197 | int valueRight = 0; |
| 198 | int peakLeft = 0; |
| 199 | int peakRight = 0; |
| 200 | QColor colorLeft; |
| 201 | QColor colorRight; |
| 202 | |
| 203 | valueLeft = static_cast<int>((p->maxLevel / p->step * p->valueLeft) * p->step); |
| 204 | peakLeft = static_cast<int>((p->maxLevel / p->step * p->peakLeft) * p->step); |
| 205 | |
| 206 | valueRight = static_cast<int>((p->maxLevel / p->step * p->valueRight) * p->step); |
| 207 | peakRight = static_cast<int>((p->maxLevel / p->step * p->peakRight) * p->step); |
| 208 | |
| 209 | // segments |
| 210 | for (int segment = 0; segment < p->maxLevel; segment += p->step) { |
| 211 | |
| 212 | // left value & peak colors |
| 213 | if ((segment < valueLeft && segment <= p->highLevel) || (peakLeft > 0 && peakLeft >= segment && peakLeft < segment + p->step * p->segmentsPerPeak && segment <= p->highLevel)) { |
| 214 | colorLeft = LevelColorNormal; |
| 215 | } else if ((segment < valueLeft && segment > p->highLevel) || (peakLeft > 0 && peakLeft >= segment && peakLeft < segment + p->step * p->segmentsPerPeak && segment > p->highLevel)) { |
| 216 | colorLeft = LevelColorHigh; |
| 217 | } else { |
| 218 | colorLeft = LevelColorOff; |
| 219 | } |
| 220 | |
| 221 | // right value & peak colors |
| 222 | if ((segment < valueRight && segment <= p->highLevel) || (peakRight > 0 && peakRight >= segment && peakRight < segment + p->step * p->segmentsPerPeak && segment <= p->highLevel)) { |
| 223 | colorRight = LevelColorNormal; |
| 224 | } else if ((segment < valueRight && segment > p->highLevel) || (peakRight > 0 && peakRight >= segment && peakRight < segment + p->step * p->segmentsPerPeak && segment > p->highLevel)) { |
| 225 | colorRight = LevelColorHigh; |
| 226 | } else { |
| 227 | colorRight = LevelColorOff; |
| 228 | } |
| 229 | |
| 230 | // LEDs |
| 231 | for (int led = 0; led < p->linesPerSegment; led++) { |
| 232 | |
| 233 | int level = segment + led; |
| 234 | |
| 235 | if (orientation() == Qt::Vertical) { |
| 236 | // draw left |
| 237 | painter.setPen(colorLeft); |
| 238 | painter.drawLine(0, p->maxLevel - level, |
| 239 | p->ledSize - 1, p->maxLevel - level); |
| 240 | // draw right |
| 241 | painter.setPen(colorRight); |
| 242 | painter.drawLine(p->margin + p->ledSize, p->maxLevel - level, |
| 243 | p->margin + p->ledSize * 2 - 1, p->maxLevel - level); |
| 244 | } else { |
| 245 | // draw left |
| 246 | painter.setPen(colorLeft); |
| 247 | painter.drawLine(level, 0, |
| 248 | level, p->ledSize - 1); |
| 249 | // draw right |
| 250 | painter.setPen(colorRight); |
| 251 | painter.drawLine(level, p->margin + p->ledSize, |
| 252 | level, p->margin + p->ledSize * 2 - 1); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |