1 /* This file is part of the KDE project
2 Copyright (C) 2001 Simon Hausmann <hausmann@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
20 #include "colorpicker.h"
27 #include <qlineedit.h>
29 #include <qpushbutton.h>
33 ColorPicker::ColorPicker( QWidget
*parent
, const char *name
)
34 : KDialogBase( parent
, name
, true /*modal*/, i18n( "Pick Color" ),
35 KDialogBase::Ok
| KDialogBase::Cancel
,
36 KDialogBase::Cancel
),
37 m_foregroundColor( -1 ), m_backgroundColor( -1 )
39 QVBox
*mainWidget
= makeVBoxMainWidget();
41 QWidget
*sampleBox
= new QWidget( mainWidget
);
42 QHBoxLayout
*sampleLayout
= new QHBoxLayout( sampleBox
);
44 QLabel
*preview
= new QLabel( i18n( "Preview:" ), sampleBox
);
45 sampleLayout
->addWidget( preview
);
47 m_sample
= new QLineEdit( i18n( "Sample Text" ), sampleBox
);
48 m_sample
->setFocusPolicy( NoFocus
);
49 m_sample
->setSizePolicy( QSizePolicy( QSizePolicy::Minimum
,
50 m_sample
->sizePolicy().verData() ) );
51 sampleLayout
->addWidget( m_sample
);
52 sampleLayout
->addStretch();
54 QHBox
*box
= new QHBox( mainWidget
);
55 QLabel
*description
= new QLabel( i18n( "&Foreground:" ), box
);
56 ColorBar
*foregroundColor
= new ColorBar( ksopts
->ircColors
.toValueVector(), box
);
57 description
->setBuddy( foregroundColor
);
59 box
= new QHBox( mainWidget
);
60 description
= new QLabel( i18n( "&Background:" ), box
);
61 ColorBar
*backgroundColor
= new ColorBar( ksopts
->ircColors
.toValueVector(), box
);
62 description
->setBuddy( backgroundColor
);
64 QPushButton
*ok
= actionButton( KDialogBase::Ok
);
65 QPushButton
*cancel
= actionButton( KDialogBase::Cancel
);
67 setTabOrder( foregroundColor
, backgroundColor
);
68 setTabOrder( backgroundColor
, ok
);
69 setTabOrder( ok
, cancel
);
71 ok
->setAutoDefault( false );
72 cancel
->setAutoDefault( false );
74 connect( foregroundColor
, SIGNAL( colorPicked( int ) ),
75 this, SLOT( setForegroundColor( int ) ) );
76 connect( backgroundColor
, SIGNAL( colorPicked( int ) ),
77 this, SLOT( setBackgroundColor( int ) ) );
79 ok
->setEnabled( false );
84 QString
ColorPicker::colorString() const
86 assert( m_foregroundColor
!= -1 );
87 QString
res( QString::number( m_foregroundColor
) );
88 if ( m_backgroundColor
!= -1 )
91 res
+= QString::number( m_backgroundColor
);
96 void ColorPicker::setForegroundColor( int col
)
98 QPushButton
* ok
=actionButton( KDialogBase::Ok
);
100 ok
->setEnabled( true );
102 m_foregroundColor
= col
;
106 void ColorPicker::setBackgroundColor( int col
)
108 m_backgroundColor
= col
;
112 void ColorPicker::updateSample()
114 QColorGroup
cg( colorGroup() );
116 QColor col
= ksopts
->textColor
;
117 if ( m_foregroundColor
!= -1 )
118 col
= ksopts
->ircColors
[ m_foregroundColor
];
120 cg
.setColor( QColorGroup::Foreground
, col
);
121 cg
.setColor( QColorGroup::Text
, col
);
123 if ( m_backgroundColor
!= -1 )
125 col
= ksopts
->ircColors
[ m_backgroundColor
];
126 cg
.setColor( QColorGroup::Background
, col
);
127 cg
.setColor( QColorGroup::Base
, col
);
130 m_sample
->setPalette( QPalette( cg
, cg
, cg
) );
133 ColorBar::ColorBar( const QValueVector
<QColor
> &colors
, QWidget
*parent
,
135 : QFrame( parent
, name
, WStaticContents
| WRepaintNoErase
),
136 m_currentCell( -1 ), m_focusedCell( - 1 ), m_colors( colors
),
139 setFrameStyle( StyledPanel
| Sunken
);
143 setFocusPolicy( StrongFocus
);
146 void ColorBar::drawContents( QPainter
*p
)
148 int x
= contentsRect().x();
149 int y
= contentsRect().y();
150 for ( unsigned int i
= 0; i
< m_colors
.size(); ++i
, x
+= m_cellSize
)
152 bool isCurrentCell
= ( m_currentCell
!= -1 &&
153 i
== static_cast<uint
>( m_currentCell
) );
154 bool isFocusedCell
= ( m_focusedCell
!= -1 &&
155 i
== static_cast<uint
>( m_focusedCell
) );
156 drawCell( p
, x
, y
, m_colors
[ i
], QString::number( i
),
157 isFocusedCell
, isCurrentCell
);
161 void ColorBar::keyPressEvent( QKeyEvent
*ev
)
163 if ( m_focusedCell
== -1 ) {
164 QFrame::keyPressEvent( ev
);
171 if ( m_focusedCell
> 1 )
177 if ( static_cast<uint
>( m_focusedCell
) < m_colors
.size() - 1 )
185 setCurrentCell( m_focusedCell
);
192 QFrame::keyPressEvent( ev
);
195 void ColorBar::focusInEvent( QFocusEvent
*ev
)
197 if ( ev
->reason() == QFocusEvent::Tab
||
198 ev
->reason() == QFocusEvent::Backtab
)
200 QFrame::focusInEvent( ev
);
203 void ColorBar::focusOutEvent( QFocusEvent
*ev
)
205 if ( ev
->reason() == QFocusEvent::Tab
||
206 ev
->reason() == QFocusEvent::Backtab
||
207 ev
->reason() == QFocusEvent::Mouse
)
209 QFrame::focusOutEvent( ev
);
212 void ColorBar::fontChange( const QFont
&oldFont
)
215 QFrame::fontChange( oldFont
);
218 void ColorBar::styleChange( QStyle
&oldStyle
)
221 QFrame::styleChange( oldStyle
);
224 bool ColorBar::focusNextPrevChild( bool next
)
228 assert( m_focusedCell
!= -1 );
230 if ( static_cast<uint
>( m_focusedCell
) < m_colors
.size() - 1 )
236 return QFrame::focusNextPrevChild( next
);
239 if ( m_focusedCell
> 1 )
246 return QFrame::focusNextPrevChild( next
);
249 void ColorBar::mousePressEvent( QMouseEvent
*ev
)
251 const QPoint
&p
= ev
->pos();
252 if ( contentsRect().contains( p
) )
254 m_focusedCell
= p
.x() / m_cellSize
;
258 QFrame::mousePressEvent( ev
);
261 void ColorBar::mouseReleaseEvent( QMouseEvent
*ev
)
263 if ( m_focusedCell
!= -1 )
265 setCurrentCell( m_focusedCell
);
268 QFrame::mouseReleaseEvent( ev
);
271 void ColorBar::updateCellSize()
273 setLineWidth( style().pixelMetric( QStyle::PM_DefaultFrameWidth
, this ) );
275 QFontMetrics
metrics( font() );
277 m_cellSize
= metrics
.width( QString::number( m_colors
.size() ) ) +
278 ( s_indicatorSize
* 2 ) +
279 ( s_focusSize
* 2 ) +
280 ( s_innerMargin
* 2 );
282 setFixedSize( QSize( ( m_colors
.size() * m_cellSize
) + ( frameWidth() * 2 ),
283 m_cellSize
+ ( frameWidth() * 2 ) ) );
286 void ColorBar::setCurrentCell( int cell
)
288 m_currentCell
= cell
;
289 emit
colorPicked( cell
);
292 void ColorBar::drawCell( QPainter
*p
, int x
, int y
, const QColor
&color
,
293 const QString
&text
, bool isFocusedCell
,
296 p
->fillRect( x
, y
, m_cellSize
, m_cellSize
, color
);
298 QColor penColor
= black
;
300 if ( color
.red() < 127 && color
.green() < 127 && color
.blue() < 127 )
303 p
->setPen( penColor
);
307 p
->fillRect( x
, y
, m_cellSize
, s_indicatorSize
, penColor
);
308 p
->fillRect( x
, y
+ s_indicatorSize
,
309 s_indicatorSize
, m_cellSize
- ( 2 * s_indicatorSize
), penColor
);
310 p
->fillRect( x
, y
+ m_cellSize
- s_indicatorSize
,
311 m_cellSize
, s_indicatorSize
, penColor
);
312 p
->fillRect( x
+ m_cellSize
- s_indicatorSize
, y
+ s_indicatorSize
,
313 s_indicatorSize
, m_cellSize
- ( 2 * s_indicatorSize
), penColor
);
318 int focusRectSize
= m_cellSize
- ( 2 * s_indicatorSize
);
320 p
->fillRect( x
+ s_indicatorSize
, y
+ s_indicatorSize
,
321 focusRectSize
, s_focusSize
, penColor
);
322 p
->fillRect( x
+ s_indicatorSize
, y
+ s_indicatorSize
+ s_focusSize
,
323 s_focusSize
, focusRectSize
- ( 2 * s_focusSize
), penColor
);
324 p
->fillRect( x
+ s_indicatorSize
,
325 y
+ m_cellSize
- s_indicatorSize
- s_focusSize
,
326 focusRectSize
, s_focusSize
, penColor
);
327 p
->fillRect( x
+ m_cellSize
- s_indicatorSize
- s_focusSize
,
328 y
+ s_indicatorSize
+ s_focusSize
,
329 s_focusSize
, focusRectSize
- ( 2 * s_focusSize
), penColor
);
332 QFontMetrics
metrics( p
->font() );
334 int offset
= ( m_cellSize
/ 2 ) - ( metrics
.width( text
) / 2 );
335 p
->drawText( x
+ offset
, y
+ s_focusSize
+ s_indicatorSize
+
336 + metrics
.ascent(), text
);
339 #include "colorpicker.moc"