colorcombo.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005 by David Saxton                                    *
00003  *   david@bluehaze.org                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 
00011 #include "colorcombo.h"
00012 
00013 #include <kcolordialog.h>
00014 #include <klocale.h>
00015 #include <qpainter.h>
00016 
00017 bool ColorCombo::createdPalettes = false;
00018 QColor * ColorCombo::palette[ NumberOfSchemes ];
00019 int ColorCombo::paletteSize[ NumberOfSchemes ];
00020 
00021 ColorCombo::ColorCombo( ColorScheme colorScheme, QWidget *parent, const char *name )
00022         : QComboBox( parent, name )
00023 {
00024         m_colorScheme = colorScheme;
00025         
00026         customColor.setRgb( 255, 255, 255 );
00027         internalColor.setRgb( 255, 255, 255 );
00028 
00029         createPalettes();
00030 
00031         addColors();
00032 
00033         connect( this, SIGNAL( activated(int) ), SLOT( slotActivated(int) ) );
00034         connect( this, SIGNAL( highlighted(int) ), SLOT( slotHighlighted(int) ) );
00035 }
00036 
00037 
00038 ColorCombo::~ColorCombo()
00039 {
00040 }
00041 
00042 
00043 void ColorCombo::createPalettes()
00044 {
00045         if ( createdPalettes )
00046                 return;
00047         createdPalettes = true;
00048         
00049         paletteSize[ QtStandard ] = 17;
00050         palette[ QtStandard ] = new QColor[ paletteSize[ QtStandard ] ];
00051 
00052         int i = 0;
00053 
00054         palette[ QtStandard ][i++] = Qt::red;
00055         palette[ QtStandard ][i++] = Qt::green;
00056         palette[ QtStandard ][i++] = Qt::blue;
00057         palette[ QtStandard ][i++] = Qt::cyan;
00058         palette[ QtStandard ][i++] = Qt::magenta;
00059         palette[ QtStandard ][i++] = Qt::yellow;
00060         palette[ QtStandard ][i++] = Qt::darkRed;
00061         palette[ QtStandard ][i++] = Qt::darkGreen;
00062         palette[ QtStandard ][i++] = Qt::darkBlue;
00063         palette[ QtStandard ][i++] = Qt::darkCyan;
00064         palette[ QtStandard ][i++] = Qt::darkMagenta;
00065         palette[ QtStandard ][i++] = Qt::darkYellow;
00066         palette[ QtStandard ][i++] = Qt::white;
00067         palette[ QtStandard ][i++] = Qt::lightGray;
00068         palette[ QtStandard ][i++] = Qt::gray;
00069         palette[ QtStandard ][i++] = Qt::darkGray;
00070         palette[ QtStandard ][i++] = Qt::black;
00071         
00072         
00073         paletteSize[ LED ] = 6;
00074         palette[ LED ] = new QColor[ paletteSize[ LED ] ];
00075         
00076         i = 0;
00077         palette[ LED ][i++] = "#f62a2a";
00078         palette[ LED ][i++] = "#ff7733";
00079         palette[ LED ][i++] = "#ffbb33";
00080         palette[ LED ][i++] = "#eeee22";
00081         palette[ LED ][i++] = "#4cc308";
00082         palette[ LED ][i++] = "#22aaee";
00083 }
00084 
00085 
00086 void ColorCombo::setColor( const QColor &col )
00087 {
00088         internalColor = col;
00089         addColors();
00090 }
00091 
00092 
00093 void ColorCombo::resizeEvent( QResizeEvent *re )
00094 {
00095         QComboBox::resizeEvent( re );
00096         addColors();
00097 }
00098 
00099 
00100 void ColorCombo::slotActivated( int index )
00101 {
00102         if ( index == 0 )
00103         {
00104                 if ( KColorDialog::getColor( customColor, this ) == QDialog::Accepted )
00105                 {
00106                         QPainter painter;
00107                         QPen pen;
00108                         QRect rect( 0, 0, width(), QFontMetrics(painter.font()).height()+4);
00109                         QPixmap pixmap( rect.width(), rect.height() );
00110 
00111                         if ( qGray( customColor.rgb() ) < 128 )
00112                                 pen.setColor( white );
00113                         else
00114                                 pen.setColor( black );
00115 
00116                         painter.begin( &pixmap );
00117                         QBrush brush( customColor );
00118                         painter.fillRect( rect, brush );
00119                         painter.setPen( pen );
00120                         painter.drawText( 2, QFontMetrics(painter.font()).ascent()+2, i18n("Custom...") );
00121                         painter.end();
00122 
00123                         changeItem( pixmap, 0 );
00124                         pixmap.detach();
00125                 }
00126 
00127                 internalColor = customColor;
00128         }
00129         else
00130                 internalColor = palette[ m_colorScheme ][ index - 1 ];
00131 
00132         emit activated( internalColor );
00133 }
00134 
00135 void ColorCombo::slotHighlighted( int index )
00136 {
00137         if ( index == 0 )
00138                 internalColor = customColor;
00139         else
00140                 internalColor = palette[ m_colorScheme ][ index - 1 ];
00141 
00142         emit highlighted( internalColor );
00143 }
00144 
00145 void ColorCombo::addColors()
00146 {
00147         QPainter painter;
00148         QPen pen;
00149         QRect rect( 0, 0, width(), QFontMetrics(painter.font()).height()+4 );
00150         QPixmap pixmap( rect.width(), rect.height() );
00151         int i;
00152 
00153         clear();
00154 
00155         createPalettes();
00156 
00157         for ( i = 0; i < paletteSize[ m_colorScheme ]; i++ )
00158                 if ( palette[ m_colorScheme ][i] == internalColor ) break;
00159 
00160         if ( i == paletteSize[ m_colorScheme ] )
00161                 customColor = internalColor;
00162 
00163         if ( qGray( customColor.rgb() ) < 128 )
00164                 pen.setColor( white );
00165         else
00166                 pen.setColor( black );
00167 
00168         painter.begin( &pixmap );
00169         QBrush brush( customColor );
00170         painter.fillRect( rect, brush );
00171         painter.setPen( pen );
00172         painter.drawText( 2, QFontMetrics(painter.font()).ascent()+2, i18n("Custom...") );
00173         painter.end();
00174 
00175         insertItem( pixmap );
00176         pixmap.detach();
00177 
00178         for ( i = 0; i < paletteSize[ m_colorScheme ]; i++ )
00179         {
00180                 painter.begin( &pixmap );
00181                 QBrush brush( palette[ m_colorScheme ][i] );
00182                 painter.fillRect( rect, brush );
00183                 painter.end();
00184 
00185                 insertItem( pixmap );
00186                 pixmap.detach();
00187 
00188                 if ( palette[ m_colorScheme ][i] == internalColor )
00189                         setCurrentItem( i + 1 );
00190         }
00191 }
00192 
00193 
00194 #include "colorcombo.moc"

Generated on Tue May 8 17:05:28 2007 for KTechLab by  doxygen 1.5.1