24 #include <QPaintEvent>
27 #include <QStyleOption>
29 #include "kcolorhelpers_p.h"
46 arrowPE = QStyle::PE_IndicatorArrowLeft;
51 QStyle::PrimitiveElement arrowPE;
60 QLinearGradient gradient;
66 : QAbstractSlider( parent )
69 setOrientation(Qt::Horizontal);
73 : QAbstractSlider( parent )
77 if(o == Qt::Horizontal)
98 int w =
indent() ? style()->pixelMetric( QStyle::PM_DefaultFrameWidth ) : 0;
102 if ( orientation() == Qt::Vertical ) {
128 int w = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
131 painter.begin(
this );
142 QStyleOptionFrame opt;
143 opt.initFrom(
this );
144 opt.state = QStyle::State_Sunken;
145 if ( orientation() == Qt::Vertical )
146 opt.rect.adjust( 0, iw - w, -5, w - iw );
148 opt.rect.adjust(iw - w, 0, w - iw, -5);
149 QBrush oldBrush = painter.brush();
150 painter.setBrush( Qt::NoBrush );
151 style()->drawPrimitive( QStyle::PE_Frame, &opt, &painter,
this );
152 painter.setBrush( oldBrush );
162 moveArrow( e->pos() );
167 moveArrow( e->pos() );
172 moveArrow( e->pos() );
173 setSliderDown(
false);
178 int val =
value() + e->delta()/120;
181 setSliderDown(
false);
184 void KSelector::moveArrow(
const QPoint &pos )
187 int w = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
190 if ( orientation() == Qt::Vertical )
191 val = ( maximum() - minimum() ) * (height() - pos.y() - iw)
192 / (height() - iw * 2) + minimum();
194 val = ( maximum() - minimum() ) * ( pos.x() - iw)
195 / (width() - iw * 2) + minimum();
201 QPoint KSelector::calcArrowPos(
int val )
204 int w = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
207 if ( orientation() == Qt::Vertical )
209 p.setY( height() - iw - 1 - (height() - 2 * iw - 1) * val / ( maximum() - minimum() ) );
211 if ( d->arrowPE == QStyle::PE_IndicatorArrowRight ) {
214 p.setX( width() - 5 );
219 p.setX( iw + (width() - 2 * iw - 1) * val / ( maximum() - minimum() ) );
221 if ( d->arrowPE == QStyle::PE_IndicatorArrowDown ) {
224 p.setY( height() - 5 );
233 switch ( direction ) {
235 if ( orientation() == Qt::Horizontal ) {
236 d->arrowPE = QStyle::PE_IndicatorArrowUp;
238 d->arrowPE = QStyle::PE_IndicatorArrowLeft;
242 if ( orientation() == Qt::Horizontal ) {
243 d->arrowPE = QStyle::PE_IndicatorArrowDown;
245 d->arrowPE = QStyle::PE_IndicatorArrowRight;
249 if ( orientation() == Qt::Vertical ) {
250 d->arrowPE = QStyle::PE_IndicatorArrowLeft;
252 d->arrowPE = QStyle::PE_IndicatorArrowDown;
256 if ( orientation() == Qt::Vertical ) {
257 d->arrowPE = QStyle::PE_IndicatorArrowRight;
259 d->arrowPE = QStyle::PE_IndicatorArrowUp;
270 switch ( d->arrowPE ) {
271 case QStyle::PE_IndicatorArrowUp:
274 case QStyle::PE_IndicatorArrowDown:
275 return Qt::DownArrow;
277 case QStyle::PE_IndicatorArrowRight:
278 return Qt::RightArrow;
280 case QStyle::PE_IndicatorArrowLeft:
282 return Qt::LeftArrow;
292 painter->setPen( QPen() );
293 painter->setBrush( QBrush( palette().color(QPalette::ButtonText) ) );
297 if ( orientation() == Qt::Vertical ) {
305 style()->drawPrimitive( d->arrowPE, &o, painter,
this );
311 :
KSelector( parent ), d(new KGradientSelectorPrivate(this))
317 :
KSelector( o, parent ), d(new KGradientSelectorPrivate(this))
331 if (orientation() == Qt::Vertical) {
336 QBrush gradientBrush(d->gradient);
340 if ( orientation() == Qt::Vertical )
342 int yPos =
contentsRect().top() + painter->fontMetrics().ascent() + 2;
344 painter->fontMetrics().width( d->text2 )) / 2;
345 QPen pen( qGray(
firstColor().rgb()) > 180 ? Qt::black : Qt::white );
346 painter->setPen( pen );
347 painter->drawText( xPos, yPos, d->text2 );
349 yPos =
contentsRect().bottom() - painter->fontMetrics().descent() - 2;
351 painter->fontMetrics().width( d->text1 )) / 2;
352 pen.setColor( qGray(
secondColor().rgb()) > 180 ? Qt::black : Qt::white );
353 painter->setPen( pen );
354 painter->drawText( xPos, yPos, d->text1 );
358 int yPos =
contentsRect().bottom()-painter->fontMetrics().descent()-2;
360 QPen pen( qGray(
firstColor().rgb()) > 180 ? Qt::black : Qt::white );
361 painter->setPen( pen );
362 painter->drawText(
contentsRect().left() + 2, yPos, d->text1 );
364 pen.setColor( qGray(
secondColor().rgb()) > 180 ? Qt::black : Qt::white );
365 painter->setPen( pen );
367 painter->fontMetrics().width( d->text2 ) - 2, yPos, d->text2 );
378 d->gradient.setStops(stops);
384 return d->gradient.stops();
389 d->gradient.setColorAt(0.0, col1);
390 d->gradient.setColorAt(1.0, col2);
403 d->gradient.setColorAt(0.0, col);
409 d->gradient.setColorAt(1.0, col);
427 return d->gradient.stops().first().second;
432 return d->gradient.stops().last().second;
445 #include "kselector.moc"
virtual QSize minimumSize() const
QString firstText() const
virtual void mousePressEvent(QMouseEvent *e)
void setText(const QString &t1, const QString &t2)
virtual void drawArrow(QPainter *painter, const QPoint &pos)
Override this function to draw the cursor which indicates the current value.
void setStops(const QGradientStops &stops)
Sets the colors that make up the gradient.
QString secondText() const
virtual void drawContents(QPainter *)
Override this function to draw the contents of the control.
Qt::ArrowType arrowDirection() const
QRect contentsRect() const
KSelector(QWidget *parent=0)
Constructs a horizontal one-dimensional selection widget.
The KGradientSelector widget allows the user to choose from a one-dimensional range of colors which i...
QColor secondColor() const
virtual void paintEvent(QPaintEvent *)
void setIndent(bool i)
Sets the indent option of the widget to i.
void setSecondText(const QString &t)
virtual void drawContents(QPainter *)
Override this function to draw the contents of the control.
virtual void mouseMoveEvent(QMouseEvent *e)
void setFirstText(const QString &t)
Set each description on its own.
virtual void mouseReleaseEvent(QMouseEvent *e)
virtual void wheelEvent(QWheelEvent *)
QGradientStops stops() const
Get the colors that make up the gradient.
QColor firstColor() const
friend class KGradientSelectorPrivate
KGradientSelector(QWidget *parent=0)
Constructs a horizontal color selector which contains a gradient between white and black...
void setFirstColor(const QColor &col)
Set each color on its own.
~KGradientSelector()
Destructs the widget.
void setColors(const QColor &col1, const QColor &col2)
Sets the two colors which span the gradient.
void fillOpaqueRect(QPainter *painter, const QRect &rect, const QBrush &brush)
KSelector is the base class for other widgets which provides the ability to choose from a one-dimensi...
void setArrowDirection(Qt::ArrowType direction)
Sets the arrow direction.
void setSecondColor(const QColor &col)