25#include <kcolorbutton.h>
29#include <kiconloader.h>
31#include "vgradientwidget.h"
34#include "vkopainter.h"
37#define midPoint_width 7
38#define midPoint_height 10
39static unsigned char midPoint_bits[] = {
40 0x08, 0x08, 0x1c, 0x1c, 0x2a, 0x2a, 0x08, 0x08, 0x08, 0x08
43#define colorStopBorder_width 11
44#define colorStopBorder_height 11
45static unsigned char colorStopBorder_bits[] = {
46 0x20, 0x00, 0x50, 0x00, 0x50, 0x00, 0x88, 0x00, 0x88, 0x00, 0x04, 0x01,
47 0x04, 0x01, 0x02, 0x02, 0x02, 0x02, 0x01, 0x04, 0xff, 0x07
50#define colorStop_width 9
51#define colorStop_height 10
52static unsigned char colorStop_bits[] = {
53 0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x38, 0x00, 0x38, 0x00, 0x7c, 0x00,
54 0x7c, 0x00, 0xfe, 0x00, 0xfe, 0x00, 0xff, 0x01
57VGradientWidget::VGradientWidget( VGradient& gradient, TQWidget* parent,
const char* name )
58 : TQWidget( parent, name ), m_gradient( &gradient )
60 setBackgroundMode( TQt::NoBackground );
61 setMinimumSize( 105, 35 );
64VGradientWidget::~VGradientWidget()
68void VGradientWidget::paintColorStop( TQPainter& p,
int x,
VColor& color )
72 bitmap = TQBitmap( colorStop_width, colorStop_height, colorStop_bits,
true );
73 bitmap.setMask( bitmap );
75 p.drawPixmap( x - 4, 1, bitmap );
77 bitmap = TQBitmap( colorStopBorder_width, colorStopBorder_height, colorStopBorder_bits,
true );
78 bitmap.setMask( bitmap );
79 p.setPen( TQt::black );
80 p.drawPixmap( x - 5, 1, bitmap );
83void VGradientWidget::paintMidPoint( TQPainter& p,
int x )
85 TQBitmap bitmap( midPoint_width, midPoint_height, midPoint_bits,
true );
86 bitmap.setMask( bitmap );
87 p.setPen( TQt::black );
88 p.drawPixmap( x - 3, 1, bitmap );
91void VGradientWidget::paintEvent( TQPaintEvent* )
95 int ph = colorStopBorder_height + 2;
98 TQPixmap pixmap( width(), height() );
99 VKoPainter gp( &pixmap, width(), height() );
100 gp.setRasterOp( TQt::XorROP );
101 VGradient gradient( *m_gradient );
102 gradient.setType( VGradient::linear );
103 gradient.setOrigin( KoPoint( 2, 2 ) );
104 gradient.setFocalPoint( KoPoint( 2, 2 ) );
105 gradient.setVector( KoPoint( 2 + w, 2 ) );
108 fill.pattern() = VPattern( il.iconPath(
"karbon.png", TDEIcon::Small ) );
111 gp.drawRect( KoRect( 2, 2, w, gh ) );
112 fill.gradient() = gradient;
115 gp.drawRect( KoRect( 2, 2, w, gh ) );
118 TQPainter p( &pixmap );
120 p.setPen( colorGroup().light() );
122 p.moveTo( 1, height() - 1 );
124 p.lineTo( width() - 1, 1 );
125 p.lineTo( width() - 1, height() - 1 );
126 p.lineTo( 1, height() - 1 );
129 p.moveTo( 1, 3 + gh );
130 p.lineTo( width() - 1, 3 + gh );
132 p.setPen( colorGroup().dark() );
134 p.moveTo( 0, height() - 1 );
136 p.lineTo( width() - 1, 0 );
139 p.moveTo( width() - 2, 2 );
140 p.lineTo( width() - 2, 2 + gh );
141 p.lineTo( 2, 2 + gh );
144 p.moveTo( 1, height() - 3 - ph );
145 p.lineTo( width() - 1, height() - 3 - ph );
148 p.moveTo( width() - 2, height() - ph - 1 );
149 p.lineTo( width() - 2, height() - 2 );
150 p.lineTo( 2, height() - 2 );
152 m_pntArea.setRect( 2, height() - ph - 2, w, ph );
154 p.fillRect( m_pntArea.x(), m_pntArea.y(), m_pntArea.width(), m_pntArea.height(), colorGroup().background() );
156 p.setClipRect( m_pntArea.x(), m_pntArea.y(), m_pntArea.width(), m_pntArea.height() );
157 p.translate( m_pntArea.x(), m_pntArea.y() );
159 TQPtrList<VColorStop>& colorStops = m_gradient->m_colorStops;
160 if( colorStops.count() > 1 )
162 VColorStop* stop, *nextstop;
163 for( stop = colorStops.first(), nextstop = colorStops.next();
164 nextstop; stop = nextstop, nextstop = colorStops.next() )
166 paintColorStop( p, (
int)( stop->rampPoint * m_pntArea.width() ), stop->color );
167 paintMidPoint( p, (
int)(( stop->rampPoint + ( nextstop->rampPoint - stop->rampPoint ) * stop->midPoint ) * m_pntArea.width() ) );
169 paintColorStop( p,
int( stop->rampPoint * w ), stop->color );
172 bitBlt(
this, 0, 0, &pixmap, 0, 0, width(), height() );
175void VGradientWidget::mousePressEvent( TQMouseEvent* e )
177 if( ! m_pntArea.contains( e->x(), e->y() ) )
180 TQPtrList<VColorStop>& colorStops = m_gradient->m_colorStops;
184 int x = e->x() - m_pntArea.left();
186 int i = colorStops.count() - 1;
187 VColorStop *stop, *nextstop = 0;
188 for( stop = colorStops.last(); i >= 0; i--, stop = colorStops.prev() )
190 int r = int( stop->rampPoint * m_pntArea.width() );
193 int m = int( ( stop->rampPoint + ( nextstop->rampPoint - stop->rampPoint ) * stop->midPoint ) * m_pntArea.width() );
194 if( ( x > m - 5 ) && ( x < m + 5 ) )
197 currentPoint = 2 * i + 2;
198 if( e->button() == TQt::LeftButton )
199 setCursor( VCursor::horzMove() );
203 if( ( x > r - 5 ) && ( x < r + 5 ) )
206 currentPoint = 2 * i + 1;
207 if( e->button() == TQt::LeftButton )
208 setCursor( VCursor::horzMove() );
216void VGradientWidget::mouseReleaseEvent( TQMouseEvent* e )
218 if( e->button() == TQt::RightButton && currentPoint )
220 if( m_pntArea.contains( e->x(), e->y() ) && ( currentPoint % 2 == 1 ) )
222 int x = e->x() - m_pntArea.left();
224 int r = int( m_gradient->m_colorStops.at(
int(0.5 * currentPoint) )->rampPoint * m_pntArea.width() );
225 if( ( x > r - 5 ) && ( x < r + 5 ) )
227 m_gradient->m_colorStops.remove(
int(0.5 * currentPoint) );
233 setCursor( TQCursor( TQt::ArrowCursor ) );
236void VGradientWidget::mouseDoubleClickEvent( TQMouseEvent* e )
238 if( ! m_pntArea.contains( e->x(), e->y() ) )
241 if( e->button() != TQt::LeftButton )
244 if( currentPoint % 2 == 1 )
247 VColorDlg* d =
new VColorDlg( m_gradient->m_colorStops.at( currentPoint / 2 )->color, this->topLevelWidget() );
248 if( d->exec() == TQDialog::Accepted )
250 m_gradient->m_colorStops.at( currentPoint / 2 )->color = d->Color();
256 else if( currentPoint == 0 )
259 VColorDlg* d =
new VColorDlg( m_gradient->m_colorStops.at( 0 )->color, this->topLevelWidget() );
260 if( d->exec() == TQDialog::Accepted )
262 m_gradient->addStop( d->Color(), (
float)( e->x() - 2 ) / ( m_pntArea.width() ), 0.5 );
270void VGradientWidget::mouseMoveEvent( TQMouseEvent* e )
272 if( e->state() & TQt::RightButton )
275 TQPtrList<VColorStop>& colorStops = m_gradient->m_colorStops;
277 if( currentPoint >= colorStops.count() * 2 )
280 int x = e->x() - m_pntArea.left();
282 if( currentPoint % 2 == 1 )
285 int actRP = int( 0.5 * ( currentPoint - 1 ) );
286 int prevRP = actRP - 1;
287 int nextRP = int( 0.5 * ( currentPoint + 1 ) );
289 x = kMin( x, ( actRP <
int( colorStops.count() - 1 ) ) ?
int( colorStops.at( nextRP )->rampPoint * m_pntArea.width() ) : m_pntArea.width() );
290 x = kMax( x, ( actRP > 0 ) ?
int( colorStops.at( prevRP )->rampPoint * m_pntArea.width() ) : 0 );
291 colorStops.at( actRP )->rampPoint = (float)( x ) / m_pntArea.width();
295 else if( currentPoint > 0 )
298 int prevRP = int( 0.5 * ( currentPoint - 1 ) );
299 int nextRP = int( 0.5 * ( currentPoint + 1 ) );
301 x = kMin( x,
int( colorStops.at( nextRP )->rampPoint * m_pntArea.width() ) );
302 x = kMax( x,
int( colorStops.at( prevRP )->rampPoint * m_pntArea.width() ) );
303 colorStops.at( prevRP )->midPoint = ( (float)( x ) / m_pntArea.width() - ( colorStops.at( prevRP )->rampPoint ) ) / ( colorStops.at( nextRP )->rampPoint - colorStops.at( prevRP )->rampPoint );
309#include "vgradientwidget.moc"
This class keeps track of color properties.