CFugue
qvumeter.cpp
1 /*
2  This is part of CFugue, a C++ Runtime for MIDI Score Programming
3  Copyright (C) 2011 Gopalakrishna Palem
4 
5  For links to further information, or to contact the author,
6  see <http://musicnote.sourceforge.net/>.
7 */
8 /* */
9 /************ Modified from original code by Gopalakrishna Palem *********/
10 /* */
11 /***************************************************************************
12  * Copyright (C) 2008 - Giuseppe Cigala *
13  * g_cigala@virgilio.it *
14  * *
15  * This program is free software; you can redistribute it and/or modify *
16  * it under the terms of the GNU General Public License as published by *
17  * the Free Software Foundation; either version 2 of the License, or *
18  * (at your option) any later version. *
19  * *
20  * This program is distributed in the hope that it will be useful, *
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23  * GNU General Public License for more details. *
24  * *
25  * You should have received a copy of the GNU General Public License *
26  * along with this program; if not, write to the *
27  * Free Software Foundation, Inc., *
28  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
29  ***************************************************************************/
30 
31 
32 #include "qvumeter.h"
33 
34 const int nBorderWidth = 10;
35 const int nInterBarGap = 10;
36 
37 const int nTopLableStartX = nBorderWidth + nInterBarGap/2;
38 const int nTopLabelStartY = 15;
39 const int nTopLabelWidth = 15;
40 const int nTopLabelHeight = 25;
41 
42 const int nBarStartX = nTopLableStartX;
43 const int nBarStartY = nTopLabelStartY + nTopLabelHeight + 10;
44 const int nBarWidth = nTopLabelWidth; // we need the text and the bar to start on same X with same width
45 const int nBarHeight = 450;
46 
47 const int nDownLabelStartX = nBarStartX;
48 const int nDownLabelStartY = nBarStartY + nBarHeight + 5;
49 const int nDownLabelWidth = nBarWidth;
50 const int nDownLabelHeight = nTopLabelHeight;
51 
52 const int nTotalWindowHeight = nDownLabelStartY + nDownLabelHeight + nTopLabelStartY;
53 const int nUnitBarWidth = nInterBarGap + nTopLabelWidth;
54 
55 
56 QVUMeter::QVUMeter(QStringList strBarLables, QWidget *parent) : QWidget(parent)
57 {
58  colBack = QColor(60, 60, 60);
59 
60  m_nBars = strBarLables.length();
61 
62  for(int i=0 ; i < m_nBars; ++i)
63  m_Bars.push_back(new QVUBar(this, strBarLables[i]));
64 
65  m_nTotalWindowWidth = nUnitBarWidth * m_nBars + 2 * nBorderWidth;
66 }
67 
68 
69 QVUMeter::QVUMeter(QWidget *parent) : QWidget(parent)
70 {
71  colBack = QColor(60, 60, 60);
72 
73  m_nBars = 1;
74 
75  for(int i=0 ; i < m_nBars; ++i)
76  m_Bars.push_back(new QVUBar(this));
77 
78  m_nTotalWindowWidth = nUnitBarWidth * m_nBars + 2 * nBorderWidth;
79 }
80 
81 
82 void QVUMeter::SetBars(QStringList strBarLables, double min, double max)
83 {
84  m_Bars.clear();
85 
86  m_nBars = strBarLables.length();
87 
88  for(int i=0 ; i < m_nBars; ++i)
89  m_Bars.push_back(new QVUBar(this, strBarLables[i], min, max));
90 
91  m_nTotalWindowWidth = nUnitBarWidth * m_nBars + 2 * nBorderWidth;
92 
93  update();
94 }
95 
96 void QVUMeter::SetBarCount(int nCount, double min, double max)
97 {
98  while(m_Bars.size() > nCount) // remove extra bars if required
99  m_Bars.removeLast();
100 
101  for(int i=0, nMax = m_Bars.size(); i < nMax; ++i) // adjust the min, max values for existing bars
102  m_Bars.at(i)->setMinMaxValues(min, max);
103 
104  while(m_Bars.size() < nCount) // add additional bars if required
105  m_Bars.push_back(new QVUBar(this, "", min, max));
106 
107  m_nTotalWindowWidth = nUnitBarWidth * m_nBars + 2 * nBorderWidth;
108 
109  update();
110 }
111 
112 void QVUMeter::paintEvent(QPaintEvent *)
113 {
114  paintBorder();
115  paintBar();
116  paintValue();
117 
118 }
119 
120 void QVUMeter::paintBorder()
121 {
122  int nTotalWidth = m_nTotalWindowWidth;
123  int nTotalHeight = nTotalWindowHeight;
124 
125  QPainter painter(this);
126  painter.setWindow(0, 0, nTotalWidth, nTotalHeight);
127  painter.setRenderHint(QPainter::Antialiasing);
128  QColor light = Qt::white;
129  QColor dark = colBack.darker(140);
130 
131  painter.setPen(QPen(colBack, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
132  QLinearGradient linGrad(5, nTotalHeight/2, 10, nTotalHeight/2);
133  linGrad.setColorAt(0, light);
134  linGrad.setColorAt(1, colBack);
135  linGrad.setSpread(QGradient::PadSpread);
136  painter.setBrush(linGrad);
137  QRectF border(5, 5, nTotalWidth - 10, nTotalHeight - 10);
138  painter.drawRoundRect(border, 30, 5);
139  QLinearGradient linGrad1(nTotalWidth - nBorderWidth, nTotalHeight/2, nTotalWidth - 5, nTotalHeight/2);
140  linGrad1.setColorAt(0, colBack);
141  linGrad1.setColorAt(1, dark);
142  linGrad1.setSpread(QGradient::PadSpread);
143  painter.setBrush(linGrad1);
144  QRectF border1(nBorderWidth, 5, nTotalWidth - nBorderWidth - 5, nTotalHeight - 10);
145  painter.drawRoundRect(border1, 30, 5);
146 
147  // paint Down label
148  QFont valFont("Arial", 9, QFont::Bold);//valFont("Arial", 12, QFont::Bold);
149  painter.setFont(valFont);
150 
151  for(int i=0; i < m_nBars; ++i)
152  {
153  painter.setPen(QPen(m_Bars[i]->colText, 2));
154 
155  int nStartX = nDownLabelStartX + i * nUnitBarWidth;
156  int nStartY = nDownLabelStartY;
157  QRectF Left(nStartX, nStartY, nDownLabelWidth, nDownLabelHeight);//(20, 505, 25, 20);
158  painter.drawText(Left, Qt::AlignCenter, m_Bars[i]->lable());
159  }
160 }
161 
162 void QVUMeter::paintBar()
163 {
164  int nTotalWidth = m_nTotalWindowWidth;
165  int nTotalHeight = nTotalWindowHeight;
166 
167  QPainter painter(this);
168  painter.setWindow(0, 0, nTotalWidth, nTotalHeight);
169  painter.setRenderHint(QPainter::Antialiasing);
170 
171 
172  // draw color bars
173 
174  for(int i=0; i < m_nBars; ++i)
175  {
176  const QVUBar& bar = *m_Bars[i];
177 
178  int nStartX = nBarStartX + i * nUnitBarWidth;
179  int nStartY = nBarStartY;
180 
181  QLinearGradient linGrad(nStartX, 0, nStartX, 500);//(50, 0, 50, 500)
182  linGrad.setColorAt(0, bar.colHigh);
183  linGrad.setColorAt(1, bar.colLow);
184  linGrad.setSpread(QGradient::PadSpread);
185  painter.setBrush(linGrad);
186 
187  painter.drawRect(nStartX, nStartY, nBarWidth, nBarHeight);
188 
189  // draw background bar
190  painter.setBrush(QColor(40, 40, 40));
191 
192  double length = nBarHeight;
193  double leftBar = abs(length * (bar.Value - bar.min)/(bar.max-bar.min));
194 
195  painter.drawRect(nStartX, nStartY, nBarWidth, nBarHeight-leftBar);
196 
197  painter.setPen(QPen(Qt::black, 2));
198 
199  for (int i = 0; i <=60; i++)
200  {
201  painter.drawLine(nStartX + 1, 500-nBarHeight*i/60, nStartX + nBarWidth - 1, 500-nBarHeight*i/60);//(21, 500-450*i/60, 44, 500-450*i/60);
202  }
203  }
204 
205 }
206 
207 void QVUMeter::paintValue()
208 {
209  int nTotalWidth = m_nTotalWindowWidth;
210  int nTotalHeight = nTotalWindowHeight;
211 
212  QPainter painter(this);
213  painter.setWindow(0, 0, nTotalWidth, nTotalHeight);
214  painter.setRenderHint(QPainter::Antialiasing);
215 
216  QFont valFont("Arial", 9, QFont::Bold);
217  painter.setFont(valFont);
218 
219  for(int i=0; i < m_nBars; ++i)
220  {
221  const QVUBar& bar = *m_Bars[i];
222 
223  int nStartX = nTopLableStartX + i * nUnitBarWidth;
224  int nStartY = nTopLabelStartY;
225 
226  painter.setPen(QPen(Qt::black, 2));
227  painter.setBrush(Qt::black);
228  painter.drawRect(nStartX, nStartY, nTopLabelWidth, nTopLabelHeight);//(20, 15, 25, 25);
229 
230  painter.setPen(Qt::gray);
231  painter.drawLine(nStartX, nStartY + nTopLabelHeight, nStartX + nTopLabelWidth, nStartY + nTopLabelHeight);//(20, 40, 45, 40);
232 
233  painter.setPen(QPen(bar.colText, 1));
234 
235  QRectF leftR(nStartX, nStartY, nTopLabelWidth, nTopLabelHeight);//(20, 15, 25, 25);
236  QString lVal = QString("%1").arg(bar.Value, 0,'f', 0);
237  painter.drawText(leftR, Qt::AlignCenter, lVal);
238 
239  //emit valueChanged(Value);
240  }
241 
242 }
243 
244 QVUBar::QVUBar(QObject* pParent, QString strLable, double _min, double _max) : QObject(pParent)
245 {
246  colText = Qt::white;
247  colHigh = Qt::red;
248  colLow = Qt::green;
249  dimVal = 9;
250  min = _min;
251  max = _max;
252  Value = 0;
253  _lable = strLable;
254 }
255 
256 void QVUBar::setValueDim(int dim)
257 {
258  dimVal = dim;
259  ((QWidget *)parent())->update();
260 }
261 
262 void QVUMeter::setColorBg(QColor color)
263 {
264  colBack = color;
265  ((QWidget *)parent())->update();
266 }
267 
268 void QVUBar::setColorOfText(QColor color)
269 {
270  colText = color;
271  ((QWidget *)parent())->update();
272 }
273 
274 void QVUBar::setColorHigh(QColor color)
275 {
276  colHigh = color;
277  ((QWidget *)parent())->update();
278 }
279 
280 
281 void QVUBar::setColorLow(QColor color)
282 {
283  colLow = color;
284  ((QWidget *)parent())->update();
285 }
286 
287 void QVUBar::setValue(double leftValue)
288 {
289  if (leftValue > max)
290  {
291  Value = max;
292  }
293  else if (leftValue < min)
294  {
295  Value = min;
296  }
297  else
298  {
299  Value = leftValue;
300  }
301  ((QWidget *)parent())->update();
302 }
303 
304 
305 void QVUBar::setMinValue(double minValue)
306 {
307  if (minValue > max)
308  {
309  min = max;
310  max = minValue;
311  }
312  else
313  {
314  min = minValue;
315  }
316  ((QWidget *)parent())->update();
317 }
318 
319 void QVUBar::setMaxValue(double maxValue)
320 {
321  if (maxValue < min)
322  {
323  max = min;
324  min = maxValue;
325  }
326  else
327  {
328  max = maxValue;
329  }
330  ((QWidget *)parent())->update();
331 }
332 
333 void QVUBar::setMinMaxValues(double minValue, double maxValue)
334 {
335  if(minValue > maxValue)
336  {
337  min = maxValue;
338  max = minValue;
339  }
340  else
341  {
342  min = minValue;
343  max = maxValue;
344  }
345  ((QWidget *)parent())->update();
346 }
347 
348 void QVUBar::setLable(QString strLable)
349 {
350  _lable = strLable;
351  ((QWidget *)parent())->update();
352 }
353 
354 QSize QVUMeter::minimumSizeHint() const
355 {
356  return QSize(m_nTotalWindowWidth / 5, nTotalWindowHeight / 10);
357 }
358 
359 QSize QVUMeter::sizeHint() const
360 {
361  return QSize(m_nTotalWindowWidth, nTotalWindowHeight);
362 }
363 
364 
365 QVUBar* QVUMeter::GetBar(int nIndex) const
366 {
367  if(nIndex >= 0 && nIndex < m_Bars.size())
368  return m_Bars.at(nIndex);
369 
370  return NULL;
371 }
372 
373 void QVUMeter::SetMinMaxValues(double nMin, double nMax)
374 {
375  for(int i=0, nMax = m_Bars.size(); i < nMax; ++i)
376  m_Bars.at(i)->setMinMaxValues(nMin, nMax);
377 }
378 
379 int QVUMeter::GetBarCount() const
380 {
381  return m_Bars.size();
382 }
void SetBars(QStringList strBarLables, double min=0, double max=100)
Definition: qvumeter.cpp:82
void SetBarCount(int nCount, double min=0.0, double max=100.0)
Definition: qvumeter.cpp:96

CFugue, the C++ Music Programming Library © Copyright 2009 Cenacle Research India Private Limited Gopalakrishna Palem