Warning, file /graphics/kdiagram/examples/demo/colorslider.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /** 0002 * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved. 0003 * 0004 * This file is part of the KD Chart library. 0005 * 0006 * This program is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU General Public License as 0008 * published by the Free Software Foundation; either version 2 of 0009 * the License, or (at your option) any later version. 0010 * 0011 * This program is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 * GNU General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU General Public License 0017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include "colorslider.h" 0021 0022 #include <QPaintEvent> 0023 #include <QPainter> 0024 #include <QMouseEvent> 0025 0026 #include <QDebug> 0027 0028 class ColorSlider::Private 0029 { 0030 public: 0031 Private( ColorSlider *q ); 0032 void drawSliderMarker( QPainter *painter ); 0033 QColor m_startColor; 0034 QColor m_endColor; 0035 bool m_dragging; 0036 ColorSlider *qq; 0037 }; 0038 0039 ColorSlider::Private::Private( ColorSlider *q ) 0040 : m_dragging( false ) 0041 , qq( q ) 0042 { 0043 0044 } 0045 0046 void ColorSlider::Private::drawSliderMarker( QPainter *painter ) 0047 { 0048 QVector< QPointF > points; points << QPointF( -0.5, 0 ) << QPointF( 0.0, 0.5 ) << QPointF( 0.5, 0 ); 0049 QPolygonF triangle( points ); 0050 painter->drawPolygon( triangle ); 0051 } 0052 0053 ColorSlider::ColorSlider( QWidget *parent ) 0054 : QAbstractSlider( parent ) 0055 , d( new Private( this ) ) 0056 { 0057 setSizePolicy( QSizePolicy( QSizePolicy::Minimum ,QSizePolicy::Fixed) ); 0058 setMinimum( 0 ); 0059 setMaximum( 255 ); 0060 setValue( 25 ); 0061 } 0062 0063 ColorSlider::~ColorSlider() 0064 { 0065 delete d; 0066 } 0067 0068 QColor ColorSlider::startColor() const 0069 { 0070 return d->m_startColor; 0071 } 0072 void ColorSlider::setStartColor( const QColor &color ) 0073 { 0074 if ( d->m_startColor != color ) 0075 { 0076 d->m_startColor = color; 0077 Q_EMIT startColorChanged(); 0078 update(); 0079 } 0080 } 0081 QColor ColorSlider::endColor() const 0082 { 0083 return d->m_endColor; 0084 } 0085 void ColorSlider::setEndColor( const QColor &color ) 0086 { 0087 if ( d->m_endColor != color ) 0088 { 0089 d->m_endColor = color; 0090 Q_EMIT startColorChanged(); 0091 update(); 0092 } 0093 } 0094 0095 void ColorSlider::paintEvent( QPaintEvent *event ) 0096 { 0097 QAbstractSlider::paintEvent( event ); 0098 const double percentage = value() / ( double )( maximum() - minimum() ); 0099 QPainter painter( this ); 0100 QLinearGradient gradient; 0101 gradient.setStart( 0, 0 ); 0102 gradient.setFinalStop( 1, 0 ); 0103 gradient.setCoordinateMode( QGradient::ObjectBoundingMode ); 0104 gradient.setColorAt( 0, d->m_startColor ); 0105 gradient.setColorAt( 1, d->m_endColor ); 0106 painter.setBrush( gradient ); 0107 painter.drawRect( event->rect() ); 0108 0109 painter.setBrush( Qt::white ); 0110 painter.save(); 0111 painter.translate( event->rect().width() * percentage, 0 ); 0112 QPen oldPen = painter.pen(); 0113 QPen linePen = oldPen; 0114 linePen.setColor( Qt::white ); 0115 painter.setPen( linePen ); 0116 painter.drawLine( 0, 0, 0, rect().height() ); 0117 painter.setPen( oldPen ); 0118 0119 painter.save(); 0120 painter.translate( 0, rect().height() ); 0121 painter.rotate( 180 ); 0122 painter.scale( 15, 15 ); 0123 d->drawSliderMarker( &painter ); 0124 painter.restore(); 0125 painter.scale( 15, 15 ); 0126 d->drawSliderMarker( &painter ); 0127 painter.restore(); 0128 0129 } 0130 0131 void ColorSlider::mousePressEvent(QMouseEvent *event ) 0132 { 0133 if ( event->button() == Qt::LeftButton ) 0134 { 0135 d->m_dragging = true; 0136 const double relPos = qMin( 1.0, qMax( 0.0, event->localPos().x() / rect().width() ) ); 0137 setValue( (maximum() - minimum() ) * relPos ); 0138 } 0139 } 0140 0141 void ColorSlider::mouseReleaseEvent(QMouseEvent *event ) 0142 { 0143 if ( event->button() != Qt::LeftButton ) 0144 { 0145 d->m_dragging = false; 0146 const double relPos = qMin( 1.0, qMax( 0.0, event->localPos().x() / rect().width() ) ); 0147 setValue( (maximum() - minimum() ) * relPos ); 0148 } 0149 } 0150 0151 void ColorSlider::mouseMoveEvent( QMouseEvent *event ) 0152 { 0153 if ( d->m_dragging ) 0154 { 0155 const double relPos = qMin( 1.0, qMax( 0.0, event->localPos().x() / rect().width() ) ); 0156 setValue( (maximum() - minimum() ) * relPos ); 0157 } 0158 } 0159 0160 QSize ColorSlider::sizeHint() const 0161 { 0162 return QSize( 300, 50 ); 0163 }