Commit 0cbd3823e274a7ce6c2d5ff3da2f2799ee37db24

Authored by 김태훈
1 parent ffa23885eb
Exists in master and in 2 other branches fhd, fhd-demo

슬라이더 새로 구현

app/gui/oven_control/oven_control.pro
... ... @@ -117,7 +117,8 @@ SOURCES += main.cpp\
117 117 reservetimepopup.cpp \
118 118 reservedtimepopup.cpp \
119 119 configdoormonitoring.cpp \
120   - config1digitsetandenablesetdlg.cpp
  120 + config1digitsetandenablesetdlg.cpp \
  121 + slider.cpp
121 122  
122 123 HEADERS += mainwindow.h \
123 124 cook.h \
... ... @@ -224,7 +225,8 @@ HEADERS += mainwindow.h \
224 225 reservetimepopup.h \
225 226 reservedtimepopup.h \
226 227 configdoormonitoring.h \
227   - config1digitsetandenablesetdlg.h
  228 + config1digitsetandenablesetdlg.h \
  229 + slider.h
228 230  
229 231 FORMS += mainwindow.ui \
230 232 manualcookwindow.ui \
... ...
app/gui/oven_control/slider.cpp
... ... @@ -0,0 +1,164 @@
  1 +#include "slider.h"
  2 +
  3 +#include <QMouseEvent>
  4 +#include <QPainter>
  5 +#include <QDebug>
  6 +
  7 +Slider::Slider(QWidget *parent) : QWidget(parent),
  8 + isSliderDown_(false), sliderPosition_(0), value_(0), minimum_(0), maximum_(1)
  9 +{
  10 + groove.load(":/images/slider/groove.png");
  11 + sub.load(":/images/slider/humidity.png");
  12 + handle.load(":/images/slider/handle_big.png");
  13 + updatePixmapPosition();
  14 +
  15 +}
  16 +
  17 +void Slider::setSubPixmap(const QPixmap &pixmap)
  18 +{
  19 + sub = pixmap;
  20 +}
  21 +
  22 +void Slider::setSubPixmap(const QString &fileName)
  23 +{
  24 + setSubPixmap(QPixmap(fileName));
  25 +}
  26 +
  27 +void Slider::setRange(int min, int max)
  28 +{
  29 + minimum_ = min;
  30 + maximum_ = max;
  31 +
  32 + if (value_ < min)
  33 + setValue(min);
  34 + else if (value_ > max)
  35 + setValue(max);
  36 +}
  37 +
  38 +void Slider::setMinimum(int min)
  39 +{
  40 + minimum_ = min;
  41 +
  42 + if (value_ < min)
  43 + setValue(min);
  44 +}
  45 +
  46 +void Slider::setMaximum(int max)
  47 +{
  48 + maximum_ = max;
  49 +
  50 + if (value_ > max)
  51 + setValue(max);
  52 +}
  53 +
  54 +bool Slider::isSliderDown()
  55 +{
  56 + return isSliderDown_;
  57 +}
  58 +
  59 +int Slider::sliderPosition()
  60 +{
  61 + return sliderPosition_;
  62 +}
  63 +
  64 +void Slider::setSliderPosition(int value)
  65 +{
  66 + if (sliderPosition_ == value)
  67 + return;
  68 +
  69 + sliderPosition_ = value;
  70 +
  71 + emit sliderMoved(value);
  72 +
  73 + update();
  74 +}
  75 +
  76 +void Slider::mouseMoveEvent(QMouseEvent *event)
  77 +{
  78 + if (!isSliderDown_)
  79 + return;
  80 +
  81 + setSliderPosition(calcSliderPosition(event->x()));
  82 +}
  83 +
  84 +void Slider::mousePressEvent(QMouseEvent */*event*/)
  85 +{
  86 + isSliderDown_ = true;
  87 +}
  88 +
  89 +void Slider::mouseReleaseEvent(QMouseEvent */*event*/)
  90 +{
  91 + isSliderDown_ = false;
  92 +
  93 + if (sliderPosition_ == value_)
  94 + return;
  95 +
  96 + setValue(sliderPosition_);
  97 +}
  98 +
  99 +void Slider::paintEvent(QPaintEvent */*event*/)
  100 +{
  101 + QPainter painter(this);
  102 +
  103 + painter.drawPixmap(groovePoint, groove);
  104 +
  105 + int subLength = calcSubLength(sliderPosition_);
  106 + if (subLength > 0)
  107 + painter.drawPixmap(subPoint, sub, QRect(0, 0, subLength, sub.height()));
  108 +
  109 + QPen tickPen(QColor(71, 71, 71));
  110 + tickPen.setWidth(2);
  111 + painter.setPen(tickPen);
  112 + foreach (int tick, ticks)
  113 + {
  114 + int x = subPoint.x() + calcSubLength(tick);
  115 + painter.drawLine(x, groovePoint.y() + 22, x, groovePoint.y() + 28);
  116 + }
  117 +
  118 + foreach (int tick, bigTicks)
  119 + {
  120 + int x = subPoint.x() + calcSubLength(tick);
  121 + painter.drawLine(x, groovePoint.y() + 22, x, groovePoint.y() + 33);
  122 + }
  123 +
  124 + QPoint handlePoint(subPoint.x() + subLength - handle.width() / 2, (height() - handle.height()) / 2);
  125 + painter.drawPixmap(handlePoint, handle);
  126 +}
  127 +
  128 +void Slider::resizeEvent(QResizeEvent */*event*/)
  129 +{
  130 + updatePixmapPosition();
  131 +}
  132 +
  133 +int Slider::calcSliderPosition(int mouseX)
  134 +{
  135 + int x = qBound(grooveMin, mouseX, grooveMax);
  136 + qreal pos = (qreal) (x - grooveMin) / (grooveMax - grooveMin) * (maximum_ - minimum_) + minimum_;
  137 + return qRound(pos);
  138 +}
  139 +
  140 +int Slider::calcSubLength(int value)
  141 +{
  142 + return handle.width() / 4 + (sub.width() - handle.width() / 2) * (value - minimum_) / (maximum_ - minimum_);
  143 +}
  144 +
  145 +void Slider::updatePixmapPosition()
  146 +{
  147 + grooveMin = (width() - sub.width()) / 2;
  148 + grooveMax = width() - grooveMin;
  149 +
  150 + groovePoint = QPoint((width() - groove.width()) / 2, (height() - groove.height()) / 2);
  151 + subPoint = QPoint((width() - sub.width()) / 2, (height() - sub.height()) / 2);
  152 +}
  153 +
  154 +void Slider::setValue(int value)
  155 +{
  156 + if (value == value_)
  157 + return;
  158 +
  159 + setSliderPosition(value);
  160 +
  161 + value_ = value;
  162 +
  163 + emit valueChanged(value);
  164 +}
... ...
app/gui/oven_control/slider.h
... ... @@ -0,0 +1,69 @@
  1 +#ifndef SLIDER_H
  2 +#define SLIDER_H
  3 +
  4 +#include <QWidget>
  5 +#include <QPixmap>
  6 +
  7 +class Slider : public QWidget
  8 +{
  9 + Q_OBJECT
  10 +
  11 + bool isSliderDown_;
  12 + int sliderPosition_;
  13 + int value_;
  14 + int minimum_;
  15 + int maximum_;
  16 +
  17 + QPixmap groove;
  18 + QPixmap sub;
  19 + QPixmap handle;
  20 +
  21 + int grooveMin;
  22 + int grooveMax;
  23 +
  24 + QPoint groovePoint;
  25 + QPoint subPoint;
  26 +
  27 +public:
  28 + explicit Slider(QWidget *parent = 0);
  29 +
  30 + void setSubPixmap(const QPixmap &pixmap);
  31 + void setSubPixmap(const QString &fileName);
  32 +
  33 + void setRange(int min, int max);
  34 + void setMinimum(int min);
  35 + void setMaximum(int max);
  36 +
  37 + int value();
  38 + int minimum();
  39 + int maximum();
  40 +
  41 + bool isSliderDown();
  42 + int sliderPosition();
  43 + void setSliderPosition(int value);
  44 +
  45 + QList<int> ticks;
  46 + QList<int> bigTicks;
  47 +
  48 +protected:
  49 + virtual void mouseMoveEvent(QMouseEvent *event);
  50 + virtual void mousePressEvent(QMouseEvent *event);
  51 + virtual void mouseReleaseEvent(QMouseEvent *event);
  52 + virtual void paintEvent(QPaintEvent *event);
  53 + virtual void resizeEvent(QResizeEvent *event);
  54 +
  55 +private:
  56 + int calcSliderPosition(int mouseX);
  57 + int calcSubLength(int value);
  58 + void updatePixmapPosition();
  59 +
  60 +signals:
  61 + void sliderMoved(int value);
  62 + void valueChanged(int value);
  63 +
  64 +public slots:
  65 + void setValue(int value);
  66 +
  67 +};
  68 +
  69 +#endif // SLIDER_H
... ...