|
| 1 | +#include "RangeWidget.h" |
| 2 | + |
| 3 | +#include <QtDebug> |
| 4 | + |
| 5 | +RangeWidget::RangeWidget(Qt::Orientation orientation, QWidget *parent) |
| 6 | + : QWidget(parent), |
| 7 | + _orientation(orientation), |
| 8 | + _handleWidth(8), |
| 9 | + _handleHeight(20), |
| 10 | + _minimum(0), |
| 11 | + _maximum(100), |
| 12 | + _firstValue(10), |
| 13 | + _secondValue(90), |
| 14 | + _firstHandlePressed(false), |
| 15 | + _secondHandlePressed(false), |
| 16 | + _firstHandleColor(style()->standardPalette().highlight().color()), |
| 17 | + _secondHandleColor(style()->standardPalette().highlight().color()) |
| 18 | +{ |
| 19 | + setMouseTracking(true); |
| 20 | +} |
| 21 | + |
| 22 | +void RangeWidget::paintEvent(QPaintEvent *event) |
| 23 | +{ |
| 24 | + QPainter p(this); |
| 25 | + |
| 26 | + // First value handle rect |
| 27 | + QRectF rv1 = firstHandleRect(); |
| 28 | + QColor c1(_firstHandleColor); |
| 29 | + if(_firstHandleHovered) |
| 30 | + c1 = c1.darker(); |
| 31 | + |
| 32 | + // Second value handle rect |
| 33 | + QRectF rv2 = secondHandleRect(); |
| 34 | + QColor c2(_secondHandleColor); |
| 35 | + if(_secondHandleHovered) |
| 36 | + c2 = c2.darker(); |
| 37 | + |
| 38 | + // Background |
| 39 | + QRect r; |
| 40 | + if(_orientation == Qt::Horizontal) |
| 41 | + r = QRect(0, (height()-_handleWidth)/2, width()-1, _handleWidth); |
| 42 | + else |
| 43 | + r = QRect((width()-_handleWidth)/2, 0, _handleWidth, height()-1); |
| 44 | + p.drawRect(r); |
| 45 | + |
| 46 | + // Handles |
| 47 | + QRectF rf(r); |
| 48 | + if(_orientation == Qt::Horizontal) |
| 49 | + { |
| 50 | + rf.setLeft(rv1.right()); |
| 51 | + rf.setRight(rv2.left()); |
| 52 | + rf.setBottom(rf.bottom()+1); |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + rf.setTop(rv1.bottom()); |
| 57 | + rf.setBottom(rv2.top()); |
| 58 | + rf.setRight(rf.right()+1); |
| 59 | + } |
| 60 | + p.fillRect(rf, QColor(Qt::green).darker(150)); |
| 61 | + p.fillRect(rv1, c1); |
| 62 | + p.fillRect(rv2, c2); |
| 63 | +} |
| 64 | + |
| 65 | +qreal RangeWidget::span() const |
| 66 | +{ |
| 67 | + int interval = qAbs(_maximum-_minimum); |
| 68 | + |
| 69 | + if(_orientation == Qt::Horizontal) |
| 70 | + return qreal(width()-_handleWidth)/qreal(interval); |
| 71 | + else |
| 72 | + return qreal(height()-_handleWidth)/qreal(interval); |
| 73 | +} |
| 74 | + |
| 75 | +QRectF RangeWidget::firstHandleRect() const |
| 76 | +{ |
| 77 | + return handleRect(_firstValue); |
| 78 | +} |
| 79 | + |
| 80 | +QRectF RangeWidget::secondHandleRect() const |
| 81 | +{ |
| 82 | + return handleRect(_secondValue); |
| 83 | +} |
| 84 | + |
| 85 | +QRectF RangeWidget::handleRect(int value) const |
| 86 | +{ |
| 87 | + qreal s = span(); |
| 88 | + |
| 89 | + QRectF r; |
| 90 | + if(_orientation == Qt::Horizontal) |
| 91 | + { |
| 92 | + r = QRectF(0, (height()-_handleHeight)/2, _handleWidth, _handleHeight); |
| 93 | + r.moveLeft(s*(value-_minimum)); |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + r = QRectF((width()-_handleHeight)/2, 0, _handleHeight, _handleWidth); |
| 98 | + r.moveTop(s*(value-_minimum)); |
| 99 | + } |
| 100 | + return r; |
| 101 | +} |
| 102 | + |
| 103 | +void RangeWidget::mousePressEvent(QMouseEvent* event) |
| 104 | +{ |
| 105 | + if(event->buttons() & Qt::LeftButton) |
| 106 | + { |
| 107 | + _secondHandlePressed = secondHandleRect().contains(event->pos()); |
| 108 | + _firstHandlePressed = !_secondHandlePressed && firstHandleRect().contains(event->pos()); |
| 109 | + emit sliderPressed(); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +void RangeWidget::mouseMoveEvent(QMouseEvent* event) |
| 114 | +{ |
| 115 | + if(event->buttons() & Qt::LeftButton) |
| 116 | + { |
| 117 | + int interval = qAbs(_maximum-_minimum); |
| 118 | + |
| 119 | + if(_secondHandlePressed) |
| 120 | + { |
| 121 | + if(_orientation == Qt::Horizontal) |
| 122 | + setSecondValue(event->pos().x()*interval/(width()-_handleWidth)); |
| 123 | + else |
| 124 | + setSecondValue(event->pos().y()*interval/(height()-_handleWidth)); |
| 125 | + } |
| 126 | + else if(_firstHandlePressed) |
| 127 | + { |
| 128 | + if(_orientation == Qt::Horizontal) |
| 129 | + setFirstValue(event->pos().x()*interval/(width()-_handleWidth)); |
| 130 | + else |
| 131 | + setFirstValue(event->pos().y()*interval/(height()-_handleWidth)); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + QRectF rv2 = secondHandleRect(); |
| 136 | + QRectF rv1 = firstHandleRect(); |
| 137 | + _secondHandleHovered = _secondHandlePressed || (!_firstHandlePressed && rv2.contains(event->pos())); |
| 138 | + _firstHandleHovered = _firstHandlePressed || (!_secondHandleHovered && rv1.contains(event->pos())); |
| 139 | + update(rv2.toRect()); |
| 140 | + update(rv1.toRect()); |
| 141 | +} |
| 142 | + |
| 143 | +void RangeWidget::mouseReleaseEvent(QMouseEvent* event) |
| 144 | +{ |
| 145 | + if(_firstHandlePressed || _secondHandlePressed) |
| 146 | + emit sliderReleased(); |
| 147 | + |
| 148 | + _firstHandlePressed = false; |
| 149 | + _secondHandlePressed = false; |
| 150 | +} |
| 151 | + |
| 152 | +QSize RangeWidget::minimumSizeHint() const |
| 153 | +{ |
| 154 | + return QSize(_handleHeight, _handleHeight); |
| 155 | +} |
| 156 | + |
| 157 | +void RangeWidget::setSecondValue(int secondValue) |
| 158 | +{ |
| 159 | + if(secondValue > _maximum) |
| 160 | + secondValue = _maximum; |
| 161 | + |
| 162 | + if(secondValue < _minimum) |
| 163 | + secondValue = _minimum; |
| 164 | + |
| 165 | + _secondValue = secondValue; |
| 166 | + emit secondValueChanged(_secondValue); |
| 167 | + |
| 168 | + update(); |
| 169 | +} |
| 170 | + |
| 171 | +void RangeWidget::setFirstValue(int firstValue) |
| 172 | +{ |
| 173 | + if(firstValue > _maximum) |
| 174 | + firstValue = _maximum; |
| 175 | + |
| 176 | + if(firstValue < _minimum) |
| 177 | + firstValue = _minimum; |
| 178 | + |
| 179 | + _firstValue = firstValue; |
| 180 | + emit firstValueChanged(_firstValue); |
| 181 | + |
| 182 | + update(); |
| 183 | +} |
| 184 | + |
| 185 | +void RangeWidget::setMaximum(int max) |
| 186 | +{ |
| 187 | + if(max >= minimum()) |
| 188 | + _maximum = max; |
| 189 | + else |
| 190 | + { |
| 191 | + int oldMin = minimum(); |
| 192 | + _maximum = oldMin; |
| 193 | + _minimum = max; |
| 194 | + } |
| 195 | + |
| 196 | + update(); |
| 197 | + |
| 198 | + if(firstValue() > maximum()) |
| 199 | + setFirstValue(maximum()); |
| 200 | + |
| 201 | + if(secondValue() > maximum()) |
| 202 | + setSecondValue(maximum()); |
| 203 | + |
| 204 | + emit rangeChanged(minimum(), maximum()); |
| 205 | +} |
| 206 | + |
| 207 | +void RangeWidget::setRange(int min, int max) |
| 208 | +{ |
| 209 | + setMinimum(min); |
| 210 | + setMaximum(max); |
| 211 | +} |
| 212 | + |
| 213 | +void RangeWidget::setMinimum(int min) |
| 214 | +{ |
| 215 | + if(min <= maximum()) |
| 216 | + _minimum = min; |
| 217 | + else |
| 218 | + { |
| 219 | + int oldMax = maximum(); |
| 220 | + _minimum = oldMax; |
| 221 | + _maximum = min; |
| 222 | + } |
| 223 | + |
| 224 | + update(); |
| 225 | + |
| 226 | + if(firstValue() < minimum()) |
| 227 | + setFirstValue(minimum()); |
| 228 | + |
| 229 | + if(secondValue() < minimum()) |
| 230 | + setSecondValue(minimum()); |
| 231 | + |
| 232 | + emit rangeChanged(minimum(), maximum()); |
| 233 | +} |
| 234 | + |
| 235 | +void RangeWidget::setOrientation(Qt::Orientation orientation) |
| 236 | +{ |
| 237 | + if(_orientation == orientation) |
| 238 | + return; |
| 239 | + |
| 240 | + _orientation = orientation; |
| 241 | + update(); |
| 242 | +} |
0 commit comments