Fix crash on logout
[kdenetwork.git] / ksirc / colorpicker.cpp
blob0bbc04f3036d7e72c6198c2e6c472cef19bb2a57
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., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "colorpicker.h"
21 #include "ksopts.h"
23 #include <qlayout.h>
24 #include <qpainter.h>
25 #include <kvbox.h>
26 #include <kstyle.h>
27 #include <qlineedit.h>
28 #include <qlabel.h>
29 #include <kpushbutton.h>
30 //Added by qt3to4:
31 #include <QFocusEvent>
32 #include <QKeyEvent>
33 #include <Q3Frame>
34 #include <QHBoxLayout>
35 #include <QMouseEvent>
37 #include <klocale.h>
39 using namespace Qt;
41 ColorPicker::ColorPicker( QWidget *parent, const char *name )
42 : KDialog( parent),
43 m_foregroundColor( -1 ), m_backgroundColor( -1 )
45 setCaption(i18n( "Pick Color" ));
46 setButtons(KDialog::Ok | KDialog::Cancel);
47 setDefaultButton(KDialog::Cancel);
48 setModal(true);
49 KVBox *mainWidget = new KVBox(this);
50 setMainWidget(mainWidget);
52 QWidget *sampleBox = new QWidget( mainWidget );
53 QHBoxLayout *sampleLayout = new QHBoxLayout( sampleBox );
55 QLabel *preview = new QLabel( i18n( "Preview:" ), sampleBox );
56 sampleLayout->addWidget( preview );
58 m_sample = new QLineEdit( i18n( "Sample Text" ), sampleBox );
59 m_sample->setFocusPolicy( NoFocus );
60 m_sample->setSizePolicy( QSizePolicy( QSizePolicy::Minimum,
61 m_sample->sizePolicy().verData() ) );
62 sampleLayout->addWidget( m_sample );
63 sampleLayout->addStretch();
65 KHBox *box = new KHBox( mainWidget );
66 QLabel *description = new QLabel( i18n( "&Foreground:" ), box );
67 ColorBar *foregroundColor = new ColorBar( ksopts->ircColors.toValueVector(), box );
68 description->setBuddy( foregroundColor );
70 box = new KHBox( mainWidget );
71 description = new QLabel( i18n( "&Background:" ), box );
72 ColorBar *backgroundColor = new ColorBar( ksopts->ircColors.toValueVector(), box );
73 description->setBuddy( backgroundColor );
74 #warning "kde4: actionButton ? for the moment there is not a method to replace it"
75 #if 0
76 QPushButton *ok = actionButton( KDialog::Ok );
77 QPushButton *cancel = actionButton( KDialog::Cancel );
79 setTabOrder( foregroundColor, backgroundColor );
80 setTabOrder( backgroundColor, ok );
81 setTabOrder( ok, cancel );
83 ok->setAutoDefault( false );
84 cancel->setAutoDefault( false );
85 #endif
86 connect( foregroundColor, SIGNAL( colorPicked( int ) ),
87 this, SLOT( setForegroundColor( int ) ) );
88 connect( backgroundColor, SIGNAL( colorPicked( int ) ),
89 this, SLOT( setBackgroundColor( int ) ) );
91 enableButton( ok, false );
93 updateSample();
96 QString ColorPicker::colorString() const
98 assert( m_foregroundColor != -1 );
99 QString res( QString::number( m_foregroundColor ) );
100 if ( m_backgroundColor != -1 )
102 res += ',';
103 res += QString::number( m_backgroundColor );
105 return res;
108 void ColorPicker::setForegroundColor( int col )
110 enableButton( ok, true );
112 m_foregroundColor = col;
113 updateSample();
116 void ColorPicker::setBackgroundColor( int col )
118 m_backgroundColor = col;
119 updateSample();
122 void ColorPicker::updateSample()
124 QColorGroup cg( colorGroup() );
126 QColor col = ksopts->textColor;
127 if ( m_foregroundColor != -1 )
128 col = ksopts->ircColors[ m_foregroundColor ];
130 cg.setColor( QPalette::Foreground, col );
131 cg.setColor( QPalette::Text, col );
133 if ( m_backgroundColor != -1 )
135 col = ksopts->ircColors[ m_backgroundColor ];
136 cg.setColor( QPalette::Background, col );
137 cg.setColor( QPalette::Base, col );
140 m_sample->setPalette( QPalette( cg, cg, cg ) );
143 ColorBar::ColorBar( const QVector<QColor> &colors, QWidget *parent,
144 const char *name )
145 : Q3Frame( parent, name, Qt::WStaticContents | Qt::WNoAutoErase ),
146 m_currentCell( -1 ), m_focusedCell( - 1 ), m_colors( colors ),
147 m_cellSize( 0 )
149 setFrameStyle( StyledPanel | Sunken );
151 updateCellSize();
153 setFocusPolicy( StrongFocus );
156 void ColorBar::drawContents( QPainter *p )
158 int x = contentsRect().x();
159 int y = contentsRect().y();
160 for ( int i = 0; i < m_colors.size(); ++i, x += m_cellSize )
162 bool isCurrentCell = ( m_currentCell != -1 &&
163 i == static_cast<uint>( m_currentCell ) );
164 bool isFocusedCell = ( m_focusedCell != -1 &&
165 i == static_cast<uint>( m_focusedCell ) );
166 drawCell( p, x, y, m_colors[ i ], QString::number( i ),
167 isFocusedCell, isCurrentCell );
171 void ColorBar::keyPressEvent( QKeyEvent *ev )
173 if ( m_focusedCell == -1 ) {
174 Q3Frame::keyPressEvent( ev );
175 return;
178 switch ( ev->key() )
180 case Qt::Key_Left:
181 if ( m_focusedCell > 1 )
182 m_focusedCell--;
183 update();
184 ev->accept();
185 return;
186 case Qt::Key_Right:
187 if ( m_focusedCell < m_colors.size() - 1 )
188 m_focusedCell++;
189 update();
190 ev->accept();
191 return;
192 case Qt::Key_Enter:
193 case Qt::Key_Return:
194 case Qt::Key_Space:
195 setCurrentCell( m_focusedCell );
196 update();
197 ev->accept();
198 return;
199 default: break;
202 Q3Frame::keyPressEvent( ev );
205 void ColorBar::focusInEvent( QFocusEvent *ev )
207 if ( ev->reason() == QFocusEvent::Tab ||
208 ev->reason() == QFocusEvent::Backtab )
209 m_focusedCell = 0;
210 Q3Frame::focusInEvent( ev );
213 void ColorBar::focusOutEvent( QFocusEvent *ev )
215 if ( ev->reason() == QFocusEvent::Tab ||
216 ev->reason() == QFocusEvent::Backtab ||
217 ev->reason() == QFocusEvent::Mouse )
218 m_focusedCell = -1;
219 Q3Frame::focusOutEvent( ev );
222 void ColorBar::fontChange( const QFont &oldFont )
224 updateCellSize();
225 Q3Frame::fontChange( oldFont );
228 void ColorBar::styleChange( QStyle &oldStyle )
230 updateCellSize();
231 Q3Frame::styleChange( oldStyle );
234 bool ColorBar::focusNextPrevChild( bool next )
236 if ( next )
238 assert( m_focusedCell != -1 );
240 if ( m_focusedCell < m_colors.size() - 1 )
242 m_focusedCell++;
243 update();
244 return true;
246 return Q3Frame::focusNextPrevChild( next );
249 if ( m_focusedCell > 1 )
251 m_focusedCell--;
252 update();
253 return true;
256 return Q3Frame::focusNextPrevChild( next );
259 void ColorBar::mousePressEvent( QMouseEvent *ev )
261 const QPoint &p = ev->pos();
262 if ( contentsRect().contains( p ) )
264 m_focusedCell = p.x() / m_cellSize;
265 update();
268 Q3Frame::mousePressEvent( ev );
271 void ColorBar::mouseReleaseEvent( QMouseEvent *ev )
273 if ( m_focusedCell != -1 )
275 setCurrentCell( m_focusedCell );
276 update();
278 Q3Frame::mouseReleaseEvent( ev );
281 void ColorBar::updateCellSize()
283 setLineWidth( style()->pixelMetric( QStyle::PM_DefaultFrameWidth ) );
285 QFontMetrics metrics( font() );
287 m_cellSize = metrics.width( QString::number( m_colors.size() ) ) +
288 ( s_indicatorSize * 2 ) +
289 ( s_focusSize * 2 ) +
290 ( s_innerMargin * 2 );
292 setFixedSize( QSize( ( m_colors.size() * m_cellSize ) + ( frameWidth() * 2 ),
293 m_cellSize + ( frameWidth() * 2 ) ) );
296 void ColorBar::setCurrentCell( int cell )
298 m_currentCell = cell;
299 emit colorPicked( cell );
302 void ColorBar::drawCell( QPainter *p, int x, int y, const QColor &color,
303 const QString &text, bool isFocusedCell,
304 bool isCurrentCell )
306 p->fillRect( x, y, m_cellSize, m_cellSize, color );
308 QColor penColor = black;
309 // ### hack
310 if ( color.red() < 127 && color.green() < 127 && color.blue() < 127 )
311 penColor = white;
313 p->setPen( penColor );
315 if ( isCurrentCell )
317 p->fillRect( x, y, m_cellSize, s_indicatorSize, penColor );
318 p->fillRect( x, y + s_indicatorSize,
319 s_indicatorSize, m_cellSize - ( 2 * s_indicatorSize ), penColor );
320 p->fillRect( x, y + m_cellSize - s_indicatorSize,
321 m_cellSize, s_indicatorSize, penColor );
322 p->fillRect( x + m_cellSize - s_indicatorSize, y + s_indicatorSize,
323 s_indicatorSize, m_cellSize - ( 2 * s_indicatorSize ), penColor );
326 if ( isFocusedCell )
328 int focusRectSize = m_cellSize - ( 2 * s_indicatorSize );
330 p->fillRect( x + s_indicatorSize, y + s_indicatorSize,
331 focusRectSize, s_focusSize, penColor );
332 p->fillRect( x + s_indicatorSize, y + s_indicatorSize + s_focusSize,
333 s_focusSize, focusRectSize - ( 2 * s_focusSize ), penColor );
334 p->fillRect( x + s_indicatorSize,
335 y + m_cellSize - s_indicatorSize - s_focusSize,
336 focusRectSize, s_focusSize, penColor );
337 p->fillRect( x + m_cellSize - s_indicatorSize - s_focusSize,
338 y + s_indicatorSize + s_focusSize,
339 s_focusSize, focusRectSize - ( 2 * s_focusSize ), penColor );
342 QFontMetrics metrics( p->font() );
344 int offset = ( m_cellSize / 2 ) - ( metrics.width( text ) / 2 );
345 p->drawText( x + offset, y + s_focusSize + s_indicatorSize +
346 + metrics.ascent(), text );
349 #include "colorpicker.moc"
351 /* vim: et sw=4