kcolorcombo.cpp
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1997 Martin Jones (mjones@kde.org) 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 //----------------------------------------------------------------------------- 00020 // KDE color selection dialog. 00021 // 00022 // 1999-09-27 Espen Sand <espensa@online.no> 00023 // KColorDialog is now subclassed from KDialogBase. I have also extended 00024 // KColorDialog::getColor() so that in contains a parent argument. This 00025 // improves centering capability. 00026 // 00027 // layout management added Oct 1997 by Mario Weilguni 00028 // <mweilguni@sime.com> 00029 // 00030 00031 00032 #include <stdio.h> 00033 #include <stdlib.h> 00034 00035 #include <tqdrawutil.h> 00036 #include <tqevent.h> 00037 #include <tqfile.h> 00038 #include <tqimage.h> 00039 #include <tqlabel.h> 00040 #include <tqlayout.h> 00041 #include <tqlineedit.h> 00042 #include <tqvalidator.h> 00043 #include <tqpainter.h> 00044 #include <tqpushbutton.h> 00045 #include <tqtimer.h> 00046 00047 #include <kapplication.h> 00048 #include <kconfig.h> 00049 #include <kglobal.h> 00050 #include <kglobalsettings.h> 00051 #include <kiconloader.h> 00052 #include <klistbox.h> 00053 #include <klocale.h> 00054 #include <kmessagebox.h> 00055 #include <kseparator.h> 00056 #include <kpalette.h> 00057 #include <kimageeffect.h> 00058 00059 //#include "kcolordialog.h" 00060 //#include "kcolordrag.h" 00061 #include "kcolorcombo.h" 00062 00063 // This is repeated from the KColorDlg, but I didn't 00064 // want to make it public BL. 00065 // We define it out when compiling with --enable-final in which case 00066 // we use the version defined in KColorDlg 00067 00068 #ifndef KDE_USE_FINAL 00069 #define STANDARD_PAL_SIZE 17 00070 00071 static TQColor *standardPalette = 0; 00072 00073 static void createStandardPalette() 00074 { 00075 if ( standardPalette ) 00076 return; 00077 00078 standardPalette = new TQColor [STANDARD_PAL_SIZE]; 00079 00080 int i = 0; 00081 00082 standardPalette[i++] = Qt::red; 00083 standardPalette[i++] = Qt::green; 00084 standardPalette[i++] = Qt::blue; 00085 standardPalette[i++] = Qt::cyan; 00086 standardPalette[i++] = Qt::magenta; 00087 standardPalette[i++] = Qt::yellow; 00088 standardPalette[i++] = Qt::darkRed; 00089 standardPalette[i++] = Qt::darkGreen; 00090 standardPalette[i++] = Qt::darkBlue; 00091 standardPalette[i++] = Qt::darkCyan; 00092 standardPalette[i++] = Qt::darkMagenta; 00093 standardPalette[i++] = Qt::darkYellow; 00094 standardPalette[i++] = Qt::white; 00095 standardPalette[i++] = Qt::lightGray; 00096 standardPalette[i++] = Qt::gray; 00097 standardPalette[i++] = Qt::darkGray; 00098 standardPalette[i++] = Qt::black; 00099 } 00100 #endif 00101 00102 class KColorCombo::KColorComboPrivate 00103 { 00104 protected: 00105 friend class KColorCombo; 00106 KColorComboPrivate(){} 00107 ~KColorComboPrivate(){} 00108 bool showEmptyList; 00109 }; 00110 00111 KColorCombo::KColorCombo( TQWidget *parent, const char *name ) 00112 : TQComboBox( parent, name ) 00113 { 00114 d=new KColorComboPrivate(); 00115 d->showEmptyList=false; 00116 00117 customColor.setRgb( 255, 255, 255 ); 00118 internalcolor.setRgb( 255, 255, 255 ); 00119 00120 createStandardPalette(); 00121 00122 addColors(); 00123 00124 connect( this, TQT_SIGNAL( activated(int) ), TQT_SLOT( slotActivated(int) ) ); 00125 connect( this, TQT_SIGNAL( highlighted(int) ), TQT_SLOT( slotHighlighted(int) ) ); 00126 } 00127 00128 00129 KColorCombo::~KColorCombo() 00130 { 00131 delete d; 00132 } 00136 void KColorCombo::setColor( const TQColor &col ) 00137 { 00138 internalcolor = col; 00139 d->showEmptyList=false; 00140 addColors(); 00141 } 00142 00143 00147 TQColor KColorCombo::color() const { 00148 return internalcolor; 00149 } 00150 00151 void KColorCombo::resizeEvent( TQResizeEvent *re ) 00152 { 00153 TQComboBox::resizeEvent( re ); 00154 00155 addColors(); 00156 } 00157 00161 void KColorCombo::showEmptyList() 00162 { 00163 d->showEmptyList=true; 00164 addColors(); 00165 } 00166 00167 void KColorCombo::slotActivated( int index ) 00168 { 00169 if ( index == 0 ) 00170 { 00171 if ( KColorDialog::getColor( customColor, this ) == TQDialog::Accepted ) 00172 { 00173 TQPainter painter; 00174 TQPen pen; 00175 TQRect rect( 0, 0, width(), TQFontMetrics(painter.font()).height()+4); 00176 TQPixmap pixmap( rect.width(), rect.height() ); 00177 00178 if ( tqGray( customColor.rgb() ) < 128 ) 00179 pen.setColor( white ); 00180 else 00181 pen.setColor( black ); 00182 00183 painter.begin( &pixmap ); 00184 TQBrush brush( customColor ); 00185 painter.fillRect( rect, brush ); 00186 painter.setPen( pen ); 00187 painter.drawText( 2, TQFontMetrics(painter.font()).ascent()+2, i18n("Custom...") ); 00188 painter.end(); 00189 00190 changeItem( pixmap, 0 ); 00191 pixmap.detach(); 00192 } 00193 00194 internalcolor = customColor; 00195 } 00196 else 00197 internalcolor = standardPalette[ index - 1 ]; 00198 00199 emit activated( internalcolor ); 00200 } 00201 00202 void KColorCombo::slotHighlighted( int index ) 00203 { 00204 if ( index == 0 ) 00205 internalcolor = customColor; 00206 else 00207 internalcolor = standardPalette[ index - 1 ]; 00208 00209 emit highlighted( internalcolor ); 00210 } 00211 00212 void KColorCombo::addColors() 00213 { 00214 TQPainter painter; 00215 TQPen pen; 00216 TQRect rect( 0, 0, width(), TQFontMetrics(painter.font()).height()+4 ); 00217 TQPixmap pixmap( rect.width(), rect.height() ); 00218 int i; 00219 00220 clear(); 00221 if (d->showEmptyList) return; 00222 00223 createStandardPalette(); 00224 00225 for ( i = 0; i < STANDARD_PAL_SIZE; i++ ) 00226 if ( standardPalette[i] == internalcolor ) break; 00227 00228 if ( i == STANDARD_PAL_SIZE ) 00229 customColor = internalcolor; 00230 00231 if ( tqGray( customColor.rgb() ) < 128 ) 00232 pen.setColor( white ); 00233 else 00234 pen.setColor( black ); 00235 00236 painter.begin( &pixmap ); 00237 TQBrush brush( customColor ); 00238 painter.fillRect( rect, brush ); 00239 painter.setPen( pen ); 00240 painter.drawText( 2, TQFontMetrics(painter.font()).ascent()+2, i18n("Custom...") ); 00241 painter.end(); 00242 00243 insertItem( pixmap ); 00244 pixmap.detach(); 00245 00246 for ( i = 0; i < STANDARD_PAL_SIZE; i++ ) 00247 { 00248 painter.begin( &pixmap ); 00249 TQBrush brush( standardPalette[i] ); 00250 painter.fillRect( rect, brush ); 00251 painter.end(); 00252 00253 insertItem( pixmap ); 00254 pixmap.detach(); 00255 00256 if ( standardPalette[i] == internalcolor ) 00257 setCurrentItem( i + 1 ); 00258 } 00259 } 00260 00261 void KColorCombo::virtual_hook( int, void* ) 00262 { /*BASE::virtual_hook( id, data );*/ } 00263 00264 #include "kcolorcombo.moc"