#include "slider.h" #include #include #include Slider::Slider(QWidget *parent) : QWidget(parent), isSliderDown_(false), sliderPosition_(0), value_(0), minimum_(0), maximum_(1) { 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::setRange(int min, int max) { minimum_ = min; maximum_ = max; if (value_ < min) setValue(min); else if (value_ > max) setValue(max); } void Slider::setMinimum(int min) { minimum_ = min; if (value_ < min) setValue(min); } void Slider::setMaximum(int max) { maximum_ = max; if (value_ > max) setValue(max); } 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; } void Slider::mouseReleaseEvent(QMouseEvent */*event*/) { isSliderDown_ = false; if (sliderPosition_ == value_) return; setValue(sliderPosition_); } void Slider::paintEvent(QPaintEvent */*event*/) { QPainter painter(this); painter.drawPixmap(groovePoint, groove); int subLength = calcSubLength(sliderPosition_); if (subLength > 0) painter.drawPixmap(subPoint, sub, QRect(0, 0, subLength, sub.height())); QPen tickPen(QColor(71, 71, 71)); tickPen.setWidth(2); painter.setPen(tickPen); foreach (int tick, ticks) { int x = subPoint.x() + calcSubLength(tick); painter.drawLine(x, groovePoint.y() + 22, x, groovePoint.y() + 28); } foreach (int tick, bigTicks) { int x = subPoint.x() + calcSubLength(tick); painter.drawLine(x, groovePoint.y() + 22, x, groovePoint.y() + 33); } 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); }