int HorizontalSlider::convert(int p) const { if(m_max > m_min) return ceil((m_max - m_min) * p / (width() - sliderSize())) + m_min; else return 0; }
void HorizontalSlider::mousePressEvent(QMouseEvent *e) { m_press_pos = e->x(); if (m_slider_pos < e->x() && e->x() < m_slider_pos + sliderSize()) { m_press_pos = e->x() - m_slider_pos; } update(); }
void HorizontalSlider::mouseMoveEvent(QMouseEvent* e) { int po = e->x() - m_press_pos; bool rtl = (layoutDirection() == Qt::RightToLeft); if (0 <= po && po <= width() - sliderSize()) { if(rtl) po = width() - po - sliderSize(); m_value = convert(po); update(); if (m_value != m_old_value) { m_old_value = m_value; emit sliderMoved(m_value); } } }
void HorizontalSlider::paintEvent(QPaintEvent *) { if(m_max <= m_min) return; bool rtl = (layoutDirection() == Qt::RightToLeft); int p = ceil((m_value - m_min)*(width() - sliderSize())/(m_max - m_min)); if(rtl) p = width() - p - sliderSize(); QPainter paint(this); paint.fillRect(0, 0, width(), height(), m_normal_bg); paint.setPen(m_normal); paint.drawRect(0, 0, width() - 1, height() - 1); paint.fillRect(p, 0, sliderSize(), height() - 1, m_normal); m_slider_pos = p; }
void MeterSlider::resizeEvent(QResizeEvent* /*ev*/) { int h = height(); int kh = sliderSize().height(); int mh = h - kh; int mw = _meterWidth / _channel; onPm = QPixmap(mw, mh); offPm = QPixmap(mw, mh); double range = maxValue() - minValue(); int h1 = mh - lrint((maxValue() - redScale) * mh / range); int h2 = mh - lrint((maxValue() - yellowScale) * mh / range); QColor yellowRed; yellowRed.setHsv(QColor(Qt::yellow).hue()-8, QColor(Qt::yellow).saturation(), QColor(Qt::yellow).value()); QColor yellRedRed; yellRedRed.setHsv(QColor(Qt::yellow).hue()-16, QColor(Qt::yellow).saturation(), QColor(Qt::yellow).value()); QLinearGradient linearGrad(QPointF(0, 0), QPointF(0, mh)); linearGrad.setColorAt(0, Qt::red); linearGrad.setColorAt(1-(double)(h1-5)/(double)mh, yellRedRed); linearGrad.setColorAt(1-(double)(h1-6)/(double)mh, yellowRed); linearGrad.setColorAt(1-(double)h2/(double)mh, Qt::yellow); linearGrad.setColorAt(1, Qt::green); QColor darkYellowRed; darkYellowRed.setHsv(QColor(Qt::darkYellow).hue()-8, QColor(Qt::darkYellow).saturation(), QColor(Qt::darkYellow).value()); QColor darkYellRedRed; darkYellRedRed.setHsv(QColor(Qt::darkYellow).hue()-16, QColor(Qt::darkYellow).saturation(), QColor(Qt::darkYellow).value()); QLinearGradient linearDarkGrad(QPointF(0, 0), QPointF(0, mh)); linearDarkGrad.setColorAt(0, Qt::darkRed); linearDarkGrad.setColorAt(1-(double)(h1-5)/(double)mh, darkYellRedRed); linearDarkGrad.setColorAt(1-(double)(h1-6)/(double)mh, darkYellowRed); linearDarkGrad.setColorAt(1-(double)h2/(double)mh, Qt::darkYellow); linearDarkGrad.setColorAt(1, Qt::darkGreen); QPainter p; p.begin(&onPm); p.fillRect(0, 0, mw, mh, linearGrad); p.end(); p.begin(&offPm); p.fillRect(0, 0, mw, mh, linearDarkGrad); p.end(); }
void MeterSlider::setMeterVal(int channel, double v, double peak) { bool mustRedraw = false; if (meterval[channel] != v) { meterval[channel] = v; mustRedraw = true; } if (peak != meterPeak[channel]) { meterPeak[channel] = peak; mustRedraw = true; } if (mustRedraw) { int kh = sliderSize().height(); int mh = height() - kh; update(20, kh / 2, _meterWidth-1, mh); } }
void MeterSlider::paintEvent(QPaintEvent* ev) { int h = height(); int kh = sliderSize().height(); int mh = h - kh; double range = maxValue() - minValue(); int ppos = int(mh * (_value - minValue()) / range); if (_invert) ppos = mh - ppos; QPainter p(this); p.setRenderHint(QPainter::Antialiasing, false); //--------------------------------------------------- // draw meter //--------------------------------------------------- int mw = _meterWidth / _channel; int x = 20; int y1 = kh / 2; int y3 = h - y1; p.setPen(QPen(Qt::white, 2)); for (int i = 0; i < _channel; ++i) { int h = mh - (lrint(fast_log10(meterval[i]) * -20.0f * mh / range)); if (h < 0) h = 0; else if (h > mh) h = mh; p.drawPixmap(x, y1+mh-h, mw, h, onPm, 0, mh-h, mw, h); p.drawPixmap(x, y1, mw, mh-h, offPm, 0, 0, mw, mh-h); //--------------------------------------------------- // draw peak line //--------------------------------------------------- h = mh - (lrint(fast_log10(meterPeak[i]) * -20.0f * mh / range)); if (h > mh) h = mh; if (h > 0) p.drawLine(x, y3-h, x+mw, y3-h); x += mw; } // optimize common case: if (ev->rect() == QRect(20, kh/2, _meterWidth-1, mh)) return; QColor sc(isEnabled() ? _scaleColor : Qt::gray); QColor svc(isEnabled() ? _scaleValueColor : Qt::gray); p.setBrush(svc); //--------------------------------------------------- // draw scale //--------------------------------------------------- int y2 = h - (ppos + y1); p.fillRect(x, y1, _scaleWidth, y2-y1, sc); p.fillRect(x, y2, _scaleWidth, y3-y2, svc); //--------------------------------------------------- // draw tick marks //--------------------------------------------------- QFont f(p.font()); f.setPointSize(6); p.setFont(f); p.setPen(QPen(Qt::darkGray, 2)); QFontMetrics fm(f); int xt = 20 - fm.width("00") - 5; QString s; for (int i = 10; i < 70; i += 10) { h = y1 + lrint(i * mh / range); s.setNum(i - 10); p.drawText(xt, h - 3, s); p.drawLine(15, h, 20, h); } //--------------------------------------------------- // draw slider //--------------------------------------------------- x += _scaleWidth/2; p.setPen(QPen(svc, 0)); p.translate(QPointF(x, y2)); p.setRenderHint(QPainter::Antialiasing, true); p.drawPath(*points); }