#include "QxtSpanSlider.h"
/****************************************************************************
** Copyright (c) 2006 - 2011, the LibQxt project.
** See the Qxt AUTHORS file for a list of authors and copyright holders.
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** * Neither the name of the LibQxt project nor the
** names of its contributors may be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
** DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
** <http://libqxt.org> <foundation@libqxt.org>
*****************************************************************************/
#include "QxtSpanSlider_p.h"
#include <QKeyEvent>
#include <QMouseEvent>
#include <QApplication>
#include <QStylePainter>
#include <QStyleOptionSlider>
#include <QStylePainter>
QxtSpanSliderPrivate::QxtSpanSliderPrivate() :
lower(0),
upper(0),
lowerPos(0),
upperPos(0),
offset(0),
position(0),
lastPressed(QxtSpanSlider::NoHandle),
mainControl(QxtSpanSlider::LowerHandle),
lowerPressed(QStyle::SC_None),
upperPressed(QStyle::SC_None),
movement(QxtSpanSlider::FreeMovement),
firstMovement(false),
blockTracking(false)
{
}
void QxtSpanSliderPrivate::initStyleOption(QStyleOptionSlider* option, QxtSpanSlider::SpanHandle handle) const
{
const QxtSpanSlider* p = q_ptr;
p->initStyleOption(option);
option->sliderPosition = (handle == QxtSpanSlider::LowerHandle ? lowerPos : upperPos);
option->sliderValue = (handle == QxtSpanSlider::LowerHandle ? lower : upper);
}
int QxtSpanSliderPrivate::pixelPosToRangeValue(int pos) const
{
QStyleOptionSlider opt;
initStyleOption(&opt);
int sliderMin = 0;
int sliderMax = 0;
int sliderLength = 0;
const QSlider* p = q_ptr;
const QRect gr = p->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, p);
const QRect sr = p->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, p);
if (p->orientation() == Qt::Horizontal)
{
sliderLength = sr.width();
sliderMin = gr.x();
sliderMax = gr.right() - sliderLength + 1;
}
else
{
sliderLength = sr.height();
sliderMin = gr.y();
sliderMax = gr.bottom() - sliderLength + 1;
}
return QStyle::sliderValueFromPosition(p->minimum(), p->maximum(), pos - sliderMin,
sliderMax - sliderMin, opt.upsideDown);
}
void QxtSpanSliderPrivate::handleMousePress(const QPoint& pos, QStyle::SubControl& control, int value, QxtSpanSlider::SpanHandle handle)
{
QStyleOptionSlider opt;
initStyleOption(&opt, handle);
QxtSpanSlider* p = q_ptr;
const QStyle::SubControl oldControl = control;
control = p->style()->hitTestComplexControl(QStyle::CC_Slider, &opt, pos, p);
const QRect sr = p->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, p);
if (control == QStyle::SC_SliderHandle)
{
position = value;
offset = pick(pos - sr.topLeft());
lastPressed = handle;
p->setSliderDown(true);
emit p->sliderPressed(handle);
}
if (control != oldControl)
p->update(sr);
}
void QxtSpanSliderPrivate::setupPainter(QPainter* painter, Qt::Orientation orientation, qreal x1, qreal y1, qreal x2, qreal y2) const
{
QColor highlight = q_ptr->palette().color(QPalette::Highlight);
QLinearGradient gradient(x1, y1, x2, y2);
gradient.setColorAt(0, highlight.dark(120));
gradient.setColorAt(1, highlight.light(108));
painter->setBrush(gradient);
if (orientation == Qt::Horizontal)
painter->setPen(QPen(highlight.dark(130), 0));
else
painter->setPen(QPen(highlight.dark(150), 0));
}
void QxtSpanSliderPrivate::drawSpan(QStylePainter* painter, const QRect& rect) const
{
QStyleOptionSlider opt;
initStyleOption(&opt);
const QSlider* p = q_ptr;
// area
QRect groove = p->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, p);
if (opt.orientation == Qt::Horizontal)
groove.adjust(0, 0, -1, 0);
else
groove.adjust(0, 0, 0, -1);
// pen & brush
painter->setPen(QPen(p->palette().color(QPalette::Dark).light(110), 0));
if (opt.orientation == Qt::Horizontal)
setupPainter(painter, opt.orientation, groove.center().x(), groove.top(), groove.center().x(), groove.bottom());
else
setupPainter(painter, opt.orientation, groove.left(), groove.center().y(), groove.right(), groove.center().y());
// draw groove
painter->drawRect(rect.intersected(groove));
}
void QxtSpanSliderPrivate::drawHandle(QStylePainter* painter, QxtSpanSlider::SpanHandle handle) const
{
QStyleOptionSlider opt;
initStyleOption(&opt, handle);
opt.subControls = QStyle::SC_SliderHandle;
QStyle::SubControl pressed = (handle == QxtSpanSlider::LowerHandle ? lowerPressed : upperPressed);
if (pressed == QStyle::SC_SliderHandle)
{
opt.activeSubControls = pressed;
opt.state |= QStyle::State_Sunken;
}
painter->drawComplexControl(QStyle::CC_Slider, opt);
}
void QxtSpanSliderPrivate::triggerAction(QAbstractSlider::SliderAction action, bool main)
{
int value = 0;
bool no = false;
bool up = false;
const int min = q_ptr->minimum();
const int max = q_ptr->maximum();
const QxtSpanSlider::SpanHandle altControl = (mainControl == QxtSpanSlider::LowerHandle ? QxtSpanSlider::UpperHandle : QxtSpanSlider::LowerHandle);
blockTracking = true;
switch (action)
{
case QAbstractSlider::SliderSingleStepAdd:
if ((main && mainControl == QxtSpanSlider::UpperHandle) || (!main && altControl == QxtSpanSlider::UpperHandle))
{
value = qBound(min, upper + q_ptr->singleStep(), max);
up = true;
break;
}
value = qBound(min, lower + q_ptr->singleStep(), max);
break;
case QAbstractSlider::SliderSingleStepSub:
if ((main && mainControl == QxtSpanSlider::UpperHandle) || (!main && altControl == QxtSpanSlider::UpperHandle))
{
value = qBound(min, upper - q_ptr->singleStep(), max);
up = true;
break;
}
value = qBound(min, lower - q_ptr->singleStep(), max);
break;
case QAbstractSlider::SliderToMinimum:
value = min;
if ((main && mainControl == QxtSpanSlider::UpperHandle) || (!main && altControl == QxtSpanSlider::UpperHandle))
up = true;
break;
case QAbstractSlider::SliderToMaximum:
value = max;
if ((main && mainControl == QxtSpanSlider::UpperHandle) ||