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 <qpainter.h>
00024
#include <qdrawutil.h>
00025
#include <qapplication.h>
00026
#include <qclipboard.h>
00027
#include <qstyle.h>
00028
#include <kglobalsettings.h>
00029
#include <kstdaccel.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
QColor m_defaultColor;
00039 };
00040
00041 KColorButton::KColorButton(
QWidget *parent,
const char *name )
00042 :
QPushButton( parent, name )
00043 {
00044 d =
new KColorButtonPrivate;
00045 d->m_bdefaultColor =
false;
00046 d->m_defaultColor =
QColor();
00047 setAcceptDrops(
true);
00048
00049
00050 connect (
this, SIGNAL(
clicked()),
this, SLOT(chooseColor()));
00051 }
00052
00053 KColorButton::KColorButton(
const QColor &c,
QWidget *parent,
00054
const char *name )
00055 :
QPushButton( parent, name ), col(c)
00056 {
00057 d =
new KColorButtonPrivate;
00058 d->m_bdefaultColor =
false;
00059 d->m_defaultColor =
QColor();
00060 setAcceptDrops(
true);
00061
00062
00063 connect (
this, SIGNAL(
clicked()),
this, SLOT(chooseColor()));
00064 }
00065
00066 KColorButton::KColorButton(
const QColor &c,
const QColor &defaultColor,
QWidget *parent,
00067
const char *name )
00068 :
QPushButton( 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, SIGNAL(
clicked()),
this, SLOT(chooseColor()));
00077 }
00078
00079 KColorButton::~KColorButton()
00080 {
00081
delete d;
00082 }
00083
00084 void KColorButton::setColor(
const QColor &c )
00085 {
00086
if ( col != c ) {
00087 col = c;
00088 repaint(
false );
00089 emit
changed( col );
00090 }
00091 }
00092
00093 QColor KColorButton::defaultColor()
const
00094
{
00095
return d->m_defaultColor;
00096 }
00097
00098 void KColorButton::setDefaultColor(
const QColor &c )
00099 {
00100 d->m_bdefaultColor = c.
isValid();
00101 d->m_defaultColor = c;
00102 }
00103
00104
00105
void KColorButton::drawButtonLabel(
QPainter *painter )
00106 {
00107
int x, y, w, h;
00108
QRect r = style().subRect( QStyle::SR_PushButtonContents,
this );
00109 r.
rect(&x, &y, &w, &h);
00110
00111
int margin = style().pixelMetric( QStyle::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( QStyle::PM_ButtonShiftHorizontal,
this );
00119 y += style().pixelMetric( QStyle::PM_ButtonShiftVertical,
this );
00120 }
00121
00122
QColor 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
QRect focusRect = style().subRect( QStyle::SR_PushButtonFocusRect,
this );
00129 style().drawPrimitive( QStyle::PE_FocusRect, painter, focusRect, colorGroup() );
00130 }
00131 }
00132
00133
QSize KColorButton::sizeHint()
const
00134
{
00135
return style().sizeFromContents(QStyle::CT_PushButton,
this,
QSize(40, 15)).
00136 expandedTo(QApplication::globalStrut());
00137 }
00138
00139
void KColorButton::dragEnterEvent(
QDragEnterEvent *event)
00140 {
00141
event->accept( KColorDrag::canDecode( event) && isEnabled());
00142 }
00143
00144
void KColorButton::dropEvent(
QDropEvent *event)
00145 {
00146
QColor c;
00147
if(
KColorDrag::decode( event, c)) {
00148
setColor(c);
00149 }
00150 }
00151
00152
void KColorButton::keyPressEvent(
QKeyEvent *e )
00153 {
00154
KKey key( e );
00155
00156
if (
KStdAccel::copy().
contains( key ) ) {
00157
QMimeSource* mime =
new KColorDrag(
color() );
00158
QApplication::clipboard()->setData( mime, QClipboard::Clipboard );
00159 }
00160
else if (
KStdAccel::paste().
contains( key ) ) {
00161
QColor color;
00162
KColorDrag::decode( QApplication::clipboard()->data( QClipboard::Clipboard ), color );
00163
setColor( color );
00164 }
00165
else
00166 QPushButton::keyPressEvent( e );
00167 }
00168
00169
void KColorButton::mousePressEvent(
QMouseEvent *e)
00170 {
00171 mPos = e->
pos();
00172 QPushButton::mousePressEvent(e);
00173 }
00174
00175
void KColorButton::mouseMoveEvent(
QMouseEvent *e)
00176 {
00177
if( (e->
state() & LeftButton) &&
00178 (e->
pos()-mPos).manhattanLength() >
KGlobalSettings::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
QColor c =
color();
00190
if ( d->m_bdefaultColor )
00191 {
00192
if(
KColorDialog::getColor( c, d->m_defaultColor,
this ) != QDialog::Rejected ) {
00193
setColor( c );
00194 }
00195 }
00196
else
00197 {
00198
if(
KColorDialog::getColor( c,
this ) != QDialog::Rejected ) {
00199
setColor( c );
00200 }
00201 }
00202 }
00203
00204
void KColorButton::virtual_hook(
int,
void* )
00205 { }
00206
00207
#include "kcolorbutton.moc"