00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022
00023 #include <tqpainter.h>
00024 #include <tqdrawutil.h>
00025 #include <tqapplication.h>
00026 #include <tqclipboard.h>
00027 #include <tqstyle.h>
00028 #include <tdeglobalsettings.h>
00029 #include <tdestdaccel.h>
00030 #include "kcolordialog.h"
00031 #include "kcolorbutton.h"
00032 #include "kcolordrag.h"
00033
00034 class KColorButton::KColorButtonPrivate
00035 {
00036 public:
00037 bool m_bdefaultColor;
00038 TQColor m_defaultColor;
00039 };
00040
00041 KColorButton::KColorButton( TQWidget *parent, const char *name )
00042 : TQPushButton( parent, name )
00043 {
00044 d = new KColorButtonPrivate;
00045 d->m_bdefaultColor = false;
00046 d->m_defaultColor = TQColor();
00047 setAcceptDrops( true);
00048
00049
00050 connect (this, TQT_SIGNAL(clicked()), this, TQT_SLOT(chooseColor()));
00051 }
00052
00053 KColorButton::KColorButton( const TQColor &c, TQWidget *parent,
00054 const char *name )
00055 : TQPushButton( parent, name ), col(c)
00056 {
00057 d = new KColorButtonPrivate;
00058 d->m_bdefaultColor = false;
00059 d->m_defaultColor = TQColor();
00060 setAcceptDrops( true);
00061
00062
00063 connect (this, TQT_SIGNAL(clicked()), this, TQT_SLOT(chooseColor()));
00064 }
00065
00066 KColorButton::KColorButton( const TQColor &c, const TQColor &defaultColor, TQWidget *parent,
00067 const char *name )
00068 : TQPushButton( parent, name ), col(c)
00069 {
00070 d = new KColorButtonPrivate;
00071 d->m_bdefaultColor = true;
00072 d->m_defaultColor = defaultColor;
00073 setAcceptDrops( true);
00074
00075
00076 connect (this, TQT_SIGNAL(clicked()), this, TQT_SLOT(chooseColor()));
00077 }
00078
00079 KColorButton::~KColorButton()
00080 {
00081 delete d;
00082 }
00083
00084 void KColorButton::setColor( const TQColor &c )
00085 {
00086 if ( col != c ) {
00087 col = c;
00088 repaint( false );
00089 emit changed( col );
00090 }
00091 }
00092
00093 TQColor KColorButton::defaultColor() const
00094 {
00095 return d->m_defaultColor;
00096 }
00097
00098 void KColorButton::setDefaultColor( const TQColor &c )
00099 {
00100 d->m_bdefaultColor = c.isValid();
00101 d->m_defaultColor = c;
00102 }
00103
00104
00105 void KColorButton::drawButtonLabel( TQPainter *painter )
00106 {
00107 int x, y, w, h;
00108 TQRect r = style().subRect( TQStyle::SR_PushButtonContents, this );
00109 r.rect(&x, &y, &w, &h);
00110
00111 int margin = style().pixelMetric( TQStyle::PM_ButtonMargin, this );
00112 x += margin;
00113 y += margin;
00114 w -= 2*margin;
00115 h -= 2*margin;
00116
00117 if (isOn() || isDown()) {
00118 x += style().pixelMetric( TQStyle::PM_ButtonShiftHorizontal, this );
00119 y += style().pixelMetric( TQStyle::PM_ButtonShiftVertical, this );
00120 }
00121
00122 TQColor fillCol = isEnabled() ? col : backgroundColor();
00123 qDrawShadePanel( painter, x, y, w, h, colorGroup(), true, 1, NULL);
00124 if ( fillCol.isValid() )
00125 painter->fillRect( x+1, y+1, w-2, h-2, fillCol );
00126
00127 if ( hasFocus() ) {
00128 TQRect focusRect = style().subRect( TQStyle::SR_PushButtonFocusRect, this );
00129 style().tqdrawPrimitive( TQStyle::PE_FocusRect, painter, focusRect, colorGroup() );
00130 }
00131 }
00132
00133 TQSize KColorButton::sizeHint() const
00134 {
00135 return style().tqsizeFromContents(TQStyle::CT_PushButton, this, TQSize(40, 15)).
00136 expandedTo(TQApplication::globalStrut());
00137 }
00138
00139 void KColorButton::dragEnterEvent( TQDragEnterEvent *event)
00140 {
00141 event->accept( KColorDrag::canDecode( event) && isEnabled());
00142 }
00143
00144 void KColorButton::dropEvent( TQDropEvent *event)
00145 {
00146 TQColor c;
00147 if( KColorDrag::decode( event, c)) {
00148 setColor(c);
00149 }
00150 }
00151
00152 void KColorButton::keyPressEvent( TQKeyEvent *e )
00153 {
00154 KKey key( e );
00155
00156 if ( TDEStdAccel::copy().contains( key ) ) {
00157 TQMimeSource* mime = new KColorDrag( color() );
00158 TQApplication::clipboard()->setData( mime, TQClipboard::Clipboard );
00159 }
00160 else if ( TDEStdAccel::paste().contains( key ) ) {
00161 TQColor color;
00162 KColorDrag::decode( TQApplication::clipboard()->data( TQClipboard::Clipboard ), color );
00163 setColor( color );
00164 }
00165 else
00166 TQPushButton::keyPressEvent( e );
00167 }
00168
00169 void KColorButton::mousePressEvent( TQMouseEvent *e)
00170 {
00171 mPos = e->pos();
00172 TQPushButton::mousePressEvent(e);
00173 }
00174
00175 void KColorButton::mouseMoveEvent( TQMouseEvent *e)
00176 {
00177 if( (e->state() & Qt::LeftButton) &&
00178 (e->pos()-mPos).manhattanLength() > TDEGlobalSettings::dndEventDelay() )
00179 {
00180
00181 KColorDrag *dg = new KColorDrag( color(), this);
00182 dg->dragCopy();
00183 setDown(false);
00184 }
00185 }
00186
00187 void KColorButton::chooseColor()
00188 {
00189 TQColor c = color();
00190 if ( d->m_bdefaultColor )
00191 {
00192 if( KColorDialog::getColor( c, d->m_defaultColor, this ) != TQDialog::Rejected ) {
00193 setColor( c );
00194 }
00195 }
00196 else
00197 {
00198 if( KColorDialog::getColor( c, this ) != TQDialog::Rejected ) {
00199 setColor( c );
00200 }
00201 }
00202 }
00203
00204 void KColorButton::virtual_hook( int, void* )
00205 { }
00206
00207 #include "kcolorbutton.moc"