From 0cbd3823e274a7ce6c2d5ff3da2f2799ee37db24 Mon Sep 17 00:00:00 2001 From: victor Date: Fri, 9 Jun 2017 13:48:49 +0900 Subject: [PATCH] =?UTF-8?q?=EC=8A=AC=EB=9D=BC=EC=9D=B4=EB=8D=94=20?= =?UTF-8?q?=EC=83=88=EB=A1=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/gui/oven_control/oven_control.pro | 6 +- app/gui/oven_control/slider.cpp | 164 ++++++++++++++++++++++++++++++++++ app/gui/oven_control/slider.h | 69 ++++++++++++++ 3 files changed, 237 insertions(+), 2 deletions(-) create mode 100644 app/gui/oven_control/slider.cpp create mode 100644 app/gui/oven_control/slider.h diff --git a/app/gui/oven_control/oven_control.pro b/app/gui/oven_control/oven_control.pro index f2f9894..8e7ab75 100644 --- a/app/gui/oven_control/oven_control.pro +++ b/app/gui/oven_control/oven_control.pro @@ -117,7 +117,8 @@ SOURCES += main.cpp\ reservetimepopup.cpp \ reservedtimepopup.cpp \ configdoormonitoring.cpp \ - config1digitsetandenablesetdlg.cpp + config1digitsetandenablesetdlg.cpp \ + slider.cpp HEADERS += mainwindow.h \ cook.h \ @@ -224,7 +225,8 @@ HEADERS += mainwindow.h \ reservetimepopup.h \ reservedtimepopup.h \ configdoormonitoring.h \ - config1digitsetandenablesetdlg.h + config1digitsetandenablesetdlg.h \ + slider.h FORMS += mainwindow.ui \ manualcookwindow.ui \ diff --git a/app/gui/oven_control/slider.cpp b/app/gui/oven_control/slider.cpp new file mode 100644 index 0000000..9ef6444 --- /dev/null +++ b/app/gui/oven_control/slider.cpp @@ -0,0 +1,164 @@ +#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); +} diff --git a/app/gui/oven_control/slider.h b/app/gui/oven_control/slider.h new file mode 100644 index 0000000..d7910b7 --- /dev/null +++ b/app/gui/oven_control/slider.h @@ -0,0 +1,69 @@ +#ifndef SLIDER_H +#define SLIDER_H + +#include +#include + +class Slider : public QWidget +{ + Q_OBJECT + + bool isSliderDown_; + int sliderPosition_; + int value_; + int minimum_; + int maximum_; + + QPixmap groove; + QPixmap sub; + QPixmap handle; + + int grooveMin; + int grooveMax; + + QPoint groovePoint; + QPoint subPoint; + +public: + explicit Slider(QWidget *parent = 0); + + void setSubPixmap(const QPixmap &pixmap); + void setSubPixmap(const QString &fileName); + + void setRange(int min, int max); + void setMinimum(int min); + void setMaximum(int max); + + int value(); + int minimum(); + int maximum(); + + bool isSliderDown(); + int sliderPosition(); + void setSliderPosition(int value); + + QList ticks; + QList bigTicks; + +protected: + virtual void mouseMoveEvent(QMouseEvent *event); + virtual void mousePressEvent(QMouseEvent *event); + virtual void mouseReleaseEvent(QMouseEvent *event); + virtual void paintEvent(QPaintEvent *event); + virtual void resizeEvent(QResizeEvent *event); + +private: + int calcSliderPosition(int mouseX); + int calcSubLength(int value); + void updatePixmapPosition(); + +signals: + void sliderMoved(int value); + void valueChanged(int value); + +public slots: + void setValue(int value); + +}; + +#endif // SLIDER_H -- 2.1.4