Now the systrayicon change it's color when a download is in progress. I simply change...
[kdenetwork.git] / ksirc / colorpicker.cpp
blob57b3de60ac0f9714903583cdb2d88be152c410a1
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"
21 #include "ksopts.h"
23 #include <qlayout.h>
24 #include <qpainter.h>
25 #include <qvbox.h>
26 #include <qstyle.h>
27 #include <qlineedit.h>
28 #include <qlabel.h>
29 #include <qpushbutton.h>
31 #include <klocale.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 );
81 updateSample();
84 QString ColorPicker::colorString() const
86 assert( m_foregroundColor != -1 );
87 QString res( QString::number( m_foregroundColor ) );
88 if ( m_backgroundColor != -1 )
90 res += ',';
91 res += QString::number( m_backgroundColor );
93 return res;
96 void ColorPicker::setForegroundColor( int col )
98 QPushButton * ok =actionButton( KDialogBase::Ok );
99 assert( ok );
100 ok->setEnabled( true );
102 m_foregroundColor = col;
103 updateSample();
106 void ColorPicker::setBackgroundColor( int col )
108 m_backgroundColor = col;
109 updateSample();
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,
134 const char *name )
135 : QFrame( parent, name, WStaticContents | WRepaintNoErase ),
136 m_currentCell( -1 ), m_focusedCell( - 1 ), m_colors( colors ),
137 m_cellSize( 0 )
139 setFrameStyle( StyledPanel | Sunken );
141 updateCellSize();
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 );
165 return;
168 switch ( ev->key() )
170 case Key_Left:
171 if ( m_focusedCell > 1 )
172 m_focusedCell--;
173 update();
174 ev->accept();
175 return;
176 case Key_Right:
177 if ( static_cast<uint>( m_focusedCell ) < m_colors.size() - 1 )
178 m_focusedCell++;
179 update();
180 ev->accept();
181 return;
182 case Key_Enter:
183 case Key_Return:
184 case Key_Space:
185 setCurrentCell( m_focusedCell );
186 update();
187 ev->accept();
188 return;
189 default: break;
192 QFrame::keyPressEvent( ev );
195 void ColorBar::focusInEvent( QFocusEvent *ev )
197 if ( ev->reason() == QFocusEvent::Tab ||
198 ev->reason() == QFocusEvent::Backtab )
199 m_focusedCell = 0;
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 )
208 m_focusedCell = -1;
209 QFrame::focusOutEvent( ev );
212 void ColorBar::fontChange( const QFont &oldFont )
214 updateCellSize();
215 QFrame::fontChange( oldFont );
218 void ColorBar::styleChange( QStyle &oldStyle )
220 updateCellSize();
221 QFrame::styleChange( oldStyle );
224 bool ColorBar::focusNextPrevChild( bool next )
226 if ( next )
228 assert( m_focusedCell != -1 );
230 if ( static_cast<uint>( m_focusedCell ) < m_colors.size() - 1 )
232 m_focusedCell++;
233 update();
234 return true;
236 return QFrame::focusNextPrevChild( next );
239 if ( m_focusedCell > 1 )
241 m_focusedCell--;
242 update();
243 return true;
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;
255 update();
258 QFrame::mousePressEvent( ev );
261 void ColorBar::mouseReleaseEvent( QMouseEvent *ev )
263 if ( m_focusedCell != -1 )
265 setCurrentCell( m_focusedCell );
266 update();
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,
294 bool isCurrentCell )
296 p->fillRect( x, y, m_cellSize, m_cellSize, color );
298 QColor penColor = black;
299 // ### hack
300 if ( color.red() < 127 && color.green() < 127 && color.blue() < 127 )
301 penColor = white;
303 p->setPen( penColor );
305 if ( isCurrentCell )
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 );
316 if ( isFocusedCell )
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"
341 /* vim: et sw=4