#include "slider.h" #include #include #include Slider::Slider(QWidget *parent) : QWidget(parent), isSliderDown_(false), sliderPosition_(0), value_(0), minimum_(0), maximum_(1), subVisible_(true), tickInterval(0), bigTickInterval(0) { groove.load(":/images/slider/groove.png"); sub.load(":/images/slider/humidity.png"); handle.load(":/images/slider/handle_big.png"); updatePixmapPosition(); } void Slider::setSubPixmap(const QPixmap &pixmap) { sub = pixmap; } void Slider::setSubPixmap(const QString &fileName) { setSubPixmap(QPixmap(fileName)); } void Slider::setSubVisible(bool visible) { if (subVisible_ == visible) return; subVisible_ = visible; update(); } void Slider::setRange(int min, int max) { if (minimum_ == min && maximum_ == max) return; minimum_ = min; maximum_ = max; if (value_ < min) setValue(min); else if (value_ > max) setValue(max); update(); } void Slider::setMinimum(int min) { if (minimum_ == min) return; minimum_ = min; if (value_ < min) setValue(min); update(); } void Slider::setMaximum(int max) { if (maximum_ == max) return; maximum_ = max; if (value_ > max) setValue(max); update(); } int Slider::value() { return value_; } bool Slider::isSliderDown() { return isSliderDown_; } int Slider::sliderPosition() { return sliderPosition_; } void Slider::setSliderPosition(int value) { if (sliderPosition_ == value) return; sliderPosition_ = value; emit sliderMoved(value); update(); } void Slider::mouseMoveEvent(QMouseEvent *event) { if (!isSliderDown_) return; setSliderPosition(calcSliderPosition(event->x())); } void Slider::mousePressEvent(QMouseEvent */*event*/) { isSliderDown_ = true; emit sliderPressed(); } void Slider::mouseReleaseEvent(QMouseEvent */*event*/) { isSliderDown_ = false; if (sliderPosition_ == value_) return; emit sliderReleased(); setValue(sliderPosition_); } void Slider::paintEvent(QPaintEvent */*event*/) { QPainter painter(this); painter.drawPixmap(groovePoint, groove); QPen tickPen(QColor(71, 71, 71)); tickPen.setWidth(2); painter.setPen(tickPen); if (tickInterval) for (int tick = minimum_; tick <= maximum_; tick += tickInterval) { int x = subPoint.x() + calcSubLength(tick); painter.drawLine(x, groovePoint.y() + 22, x, groovePoint.y() + 28); } else foreach (int tick, ticks) { int x = subPoint.x() + calcSubLength(tick); painter.drawLine(x, groovePoint.y() + 22, x, groovePoint.y() + 28); } if (bigTickInterval) for (int tick = minimum_; tick <= maximum_; tick += bigTickInterval) { int x = subPoint.x() + calcSubLength(tick); painter.drawLine(x, groovePoint.y() + 22, x, groovePoint.y() + 33); } else foreach (int tick, bigTicks) { int x = subPoint.x() + calcSubLength(tick); painter.drawLine(x, groovePoint.y() + 22, x, groovePoint.y() + 33); } if (subVisible_) { int subLength = calcSubLength(sliderPosition_); if (subLength > 0) painter.drawPixmap(subPoint, sub, QRect(0, 0, subLength, sub.height())); QPoint handlePoint(subPoint.x() + subLength - handle.width() / 2, (height() - handle.height()) / 2); painter.drawPixmap(handlePoint, handle); } } void Slider::resizeEvent(QResizeEvent */*event*/) { updatePixmapPosition(); } int Slider::calcSliderPosition(int mouseX) { int x = qBound(grooveMin, mouseX, grooveMax); qreal pos = (qreal) (x - grooveMin) / (grooveMax - grooveMin) * (maximum_ - minimum_) + minimum_; return qRound(pos); } int Slider::calcSubLength(int value) { return handle.width() / 4 + (sub.width() - handle.width() / 2) * (value - minimum_) / (maximum_ - minimum_); } void Slider::updatePixmapPosition() { grooveMin = (width() - sub.width()) / 2; grooveMax = width() - grooveMin; groovePoint = QPoint((width() - groove.width()) / 2, (height() - groove.height()) / 2); subPoint = QPoint((width() - sub.width()) / 2, (height() - sub.height()) / 2); } void Slider::setValue(int value) { if (value == value_) return; setSliderPosition(value); value_ = value; emit valueChanged(value); }