9 #include <QApplication> 10 #include <QMouseEvent> 13 #include "fwGuiQt/widget/QRangeSlider.hpp" 26 static T* safeCast(U *p)
28 T* casted =
dynamic_cast<T*
>(p);
42 m_brush(Qt::lightGray)
46 m_verticalPadding = 0.2;
47 m_tolerance = std::max(0, 10 - m_width);
50 virtual void draw(QPainter &painter,
bool )
52 int height = drawingArea().height()-1;
53 int top = height * m_verticalPadding;
54 int handleHeight = height - 2*top;
55 painter.setRenderHint(QPainter::Antialiasing);
56 painter.setPen(m_pen);
57 painter.setBrush(m_brush);
58 painter.drawRect(m_pos - halfWidth(), top, m_width, handleHeight);
61 virtual bool pick(
const QPoint &
point)
const 65 int height = drawingArea().height()-1;
66 int top = height * m_verticalPadding;
67 int handleHeight = height - 2*top;
69 if ( abs(point.x() - m_pos) <= (halfWidth() + m_tolerance)
70 && (top+handleHeight) >= point.y() && point.y() >= top )
77 int setHandleSize(
int size)
80 m_width = size + ((size+1)%2);
89 int setPos(
const int &pos)
93 p = std::max(p, xPosMin());
94 p = std::min(p, xPosMax());
113 return drawingArea().width() - halfWidth() - 1;
116 double toFloatingPos(
int p)
118 int posMin = xPosMin();
119 int posMax = xPosMax();
120 return ( (
double) (p - posMin) / (
double)(posMax - posMin) );
123 int fromFloatingPos(
double p)
125 assert( 0. <= p && p <= 1.);
126 int posMin = xPosMin();
127 int extend = (xPosMax()) - posMin;
129 return posMin + (int) (p*extend);
135 return toFloatingPos( pos() );
138 void setFloatingPos(
double pos)
140 setPos(fromFloatingPos(pos));
147 double m_verticalPadding;
162 m_reversePen(Qt::darkYellow),
163 m_reverseBrush(Qt::yellow)
171 return m_right-m_left;
174 virtual void draw(QPainter &painter,
bool enabled)
176 int w = this->width();
185 brush = m_reverseBrush;
195 pen = QPen(Qt::lightGray);
196 brush = QBrush(Qt::lightGray);
200 painter.setBrush(brush);
201 painter.setRenderHint(QPainter::Antialiasing);
202 painter.drawRect(m_left, 0, w, drawingArea().height()-1);
205 virtual bool pick(
const QPoint &
point)
const 208 int min = std::min(m_left, m_right);
209 int max = std::max(m_left, m_right);
210 picked = min <= point.x() && point.x() <= max;
214 void setPos(
const int &left,
const int &right)
227 QBrush m_reverseBrush;
235 QRangeSlider::QRangeSlider(QWidget *parent)
240 m_allowMinGreaterThanMax =
true;
241 m_minimumMinMaxDelta = 0.;
248 minh->setHandleSize(m_handleSize);
249 maxh->setHandleSize(m_handleSize);
253 m_window =
new Window(
this);
255 this->setPos(m_minValue, m_maxValue);
256 this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
260 QRangeSlider::~QRangeSlider()
267 void QRangeSlider::setPos(
double _min,
double _max)
269 Handle *minHandle = Handle::safeCast(m_minHandle);
270 Handle *maxHandle = Handle::safeCast(m_maxHandle);
271 Window *window = Window::safeCast(m_window);
273 assert(minHandle && maxHandle && window);
275 minHandle->setFloatingPos(_min);
276 maxHandle->setFloatingPos(_max);
279 min = minHandle->pos();
280 max = maxHandle->pos();
281 window->setPos(min, max);
283 this->movedTo(_min, _max);
287 void QRangeSlider::move(
int delta)
289 Handle *minHandle = Handle::safeCast(m_minHandle);
290 Handle *maxHandle = Handle::safeCast(m_maxHandle);
291 Window *window = Window::safeCast(m_window);
293 assert(minHandle && maxHandle && window);
295 int low, high, width, dir;
296 dir = ((minHandle->pos() < maxHandle->pos()) ? 1 : -1);
297 bool movingRight = (delta < 0);
299 low = minHandle->pos();
300 high = maxHandle->pos();
303 if( (movingRight && dir < 0)
304 || (!movingRight && dir > 0 ) )
306 low = minHandle->setPos(low-delta);
308 maxHandle->setPos(high);
312 high = maxHandle->setPos(high-delta);
314 minHandle->setPos(low);
317 window->setPos(low, high);
320 bool QRangeSlider::movedTo(
double _min,
double _max)
322 bool changed = m_minValue != _min || m_maxValue != _max;
327 Q_EMIT sliderRangeChanged( m_minValue, m_maxValue );
332 void QRangeSlider::paintEvent ( QPaintEvent * )
334 bool enabled = this->isEnabled();
335 QPainter painter(
this);
336 painter.setRenderHint(QPainter::Antialiasing);
338 QRect rect = this->rect();
339 rect.setLeft(rect.left() + m_handleSize/2);
340 rect.setRight(rect.right() - m_handleSize/2);
341 painter.fillRect(rect, QApplication::palette().base());
343 painter.setBrush(Qt::cyan);
344 m_window->draw(painter, enabled);
346 painter.setBrush(Qt::gray);
347 painter.setPen(Qt::darkGreen);
348 m_minHandle->draw(painter, enabled);
350 painter.setPen(Qt::darkRed);
351 m_maxHandle->draw(painter, enabled);
355 void QRangeSlider::mouseMoveEvent ( QMouseEvent * event )
360 Handle *minHandle = Handle::safeCast(m_minHandle);
361 Handle *maxHandle = Handle::safeCast(m_maxHandle);
362 Window *window = Window::safeCast(m_window);
366 if( (currentHandle = Handle::safeCast(m_current)) )
368 int oldPos = currentHandle->pos();
369 int newPos =
event->pos().x();
370 currentHandle->setPos(newPos);
372 if(!m_allowMinGreaterThanMax &&
373 minHandle->floatingPos() + m_minimumMinMaxDelta >= maxHandle->floatingPos() )
375 currentHandle->setPos(oldPos);
377 window->setPos(minHandle->pos(), maxHandle->pos());
379 else if( Window::safeCast(m_current) )
381 QPoint delta = m_pressPos -
event->pos();
383 minHandle->setPos(m_pressMin);
384 maxHandle->setPos(m_pressMax);
385 window->setPos(minHandle->pos(), maxHandle->pos());
386 this->move(delta.x());
389 double min = minHandle->floatingPos();
390 double max = maxHandle->floatingPos();
391 if( this->movedTo(min, max) )
393 Q_EMIT sliderRangeEdited ( min, max );
399 void QRangeSlider::mousePressEvent ( QMouseEvent * event )
401 Handle *minHandle = Handle::safeCast(m_minHandle);
402 Handle *maxHandle = Handle::safeCast(m_maxHandle);
405 m_pressPos =
event->pos();
406 m_pressMin = minHandle->pos();
407 m_pressMax = maxHandle->pos();
409 if(m_maxHandle->pick(m_pressPos))
411 m_current = m_maxHandle;
413 else if(m_minHandle->pick(m_pressPos))
415 m_current = m_minHandle;
417 else if(m_window->pick(m_pressPos))
419 m_current = m_window;
423 void QRangeSlider::mouseReleaseEvent ( QMouseEvent * )
429 void QRangeSlider::wheelEvent ( QWheelEvent * event )
431 Handle *minHandle = Handle::safeCast(m_minHandle);
432 Handle *maxHandle = Handle::safeCast(m_maxHandle);
433 Window *window = Window::safeCast(m_window);
435 int delta = this->size().width()/( ((double) event->delta())/4. );
436 int low = minHandle->pos();
437 int high = maxHandle->pos();
439 if(event->orientation() == Qt::Vertical)
441 if(!m_allowMinGreaterThanMax)
443 int diff = (high - low);
444 int minDiff = minHandle->fromFloatingPos(m_minimumMinMaxDelta);
445 delta = std::max(delta, -(diff - minDiff)/2);
448 low = minHandle->setPos(low - delta);
449 high = maxHandle->setPos(high + delta);
450 window->setPos(low, high);
457 double min = minHandle->floatingPos();
458 double max = maxHandle->floatingPos();
459 if( this->movedTo(min, max) )
461 Q_EMIT sliderRangeEdited ( min, max );
466 void QRangeSlider::resizeEvent ( QResizeEvent * event )
468 this->setPos(m_minValue, m_maxValue);
The namespace fwGuiQt contains classes which provide the implementation of the Gui using Qt library...