00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "kcolorbutton.h"
00022
00023 #include <config.h>
00024
00025 #include <QtGui/QPainter>
00026 #include <QtGui/qdrawutil.h>
00027 #include <QtGui/QApplication>
00028 #include <QtGui/QClipboard>
00029 #include <QtGui/QStyle>
00030 #include <kglobalsettings.h>
00031 #include <kstandardshortcut.h>
00032 #include <QMouseEvent>
00033 #include <QStyleOptionButton>
00034 #include "kcolordialog.h"
00035 #include "kcolormimedata.h"
00036 #include "kdebug.h"
00037
00038 class KColorButton::KColorButtonPrivate
00039 {
00040 public:
00041 KColorButtonPrivate(KColorButton *q): q(q) {}
00042
00043 void _k_chooseColor();
00044
00045 KColorButton *q;
00046 QColor m_defaultColor;
00047 bool m_bdefaultColor : 1;
00048
00049 bool dragFlag : 1;
00050 QColor col;
00051 QPoint mPos;
00052
00053 void initStyleOption(QStyleOptionButton* opt) const;
00054 };
00055
00056 KColorButton::KColorButton( QWidget *parent )
00057 : QPushButton( parent )
00058 , d( new KColorButtonPrivate(this) )
00059 {
00060 d->m_bdefaultColor = false;
00061 d->m_defaultColor = QColor();
00062 setAcceptDrops( true);
00063
00064
00065 connect (this, SIGNAL(clicked()), this, SLOT(_k_chooseColor()));
00066 }
00067
00068 KColorButton::KColorButton( const QColor &c, QWidget *parent )
00069 : QPushButton( parent )
00070 , d( new KColorButtonPrivate(this) )
00071 {
00072 d->col = c;
00073 d->m_bdefaultColor = false;
00074 d->m_defaultColor = QColor();
00075 setAcceptDrops( true);
00076
00077
00078 connect (this, SIGNAL(clicked()), this, SLOT(_k_chooseColor()));
00079 }
00080
00081 KColorButton::KColorButton( const QColor &c, const QColor &defaultColor, QWidget *parent )
00082 : QPushButton( parent )
00083 , d( new KColorButtonPrivate(this) )
00084 {
00085 d->col = c;
00086 d->m_bdefaultColor = true;
00087 d->m_defaultColor = defaultColor;
00088 setAcceptDrops( true);
00089
00090
00091 connect (this, SIGNAL(clicked()), this, SLOT(_k_chooseColor()));
00092 }
00093
00094 KColorButton::~KColorButton()
00095 {
00096 delete d;
00097 }
00098
00099 QColor KColorButton::color() const
00100 {
00101 return d->col;
00102 }
00103
00104 void KColorButton::setColor( const QColor &c )
00105 {
00106 if ( d->col != c ) {
00107 d->col = c;
00108 repaint();
00109 emit changed( d->col );
00110 }
00111 }
00112
00113 QColor KColorButton::defaultColor() const
00114 {
00115 return d->m_defaultColor;
00116 }
00117
00118 void KColorButton::setDefaultColor( const QColor &c )
00119 {
00120 d->m_bdefaultColor = c.isValid();
00121 d->m_defaultColor = c;
00122 }
00123
00124 void KColorButton::KColorButtonPrivate::initStyleOption(QStyleOptionButton* opt) const
00125 {
00126 opt->initFrom(q);
00127 opt->state |= q->isDown() ? QStyle::State_Sunken : QStyle::State_Raised;
00128 opt->features = QStyleOptionButton::None;
00129 if (q->isDefault())
00130 opt->features |= QStyleOptionButton::DefaultButton;
00131 opt->text.clear();
00132 opt->icon = QIcon();
00133 }
00134
00135 void KColorButton::paintEvent( QPaintEvent* )
00136 {
00137 QPainter painter(this);
00138
00139
00140 QStyleOptionButton butOpt;
00141 d->initStyleOption(&butOpt);
00142 style()->drawControl( QStyle::CE_PushButtonBevel, &butOpt, &painter, this );
00143
00144
00145
00146 QRect labelRect = style()->subElementRect( QStyle::SE_PushButtonContents,
00147 &butOpt, this );
00148 int shift = style()->pixelMetric( QStyle::PM_ButtonMargin );
00149 labelRect.adjust(shift, shift, -shift, -shift);
00150 int x, y, w, h;
00151 labelRect.getRect(&x, &y, &w, &h);
00152
00153 if (isChecked() || isDown()) {
00154 x += style()->pixelMetric( QStyle::PM_ButtonShiftHorizontal );
00155 y += style()->pixelMetric( QStyle::PM_ButtonShiftVertical );
00156 }
00157
00158 QColor fillCol = isEnabled() ? d->col : palette().color(backgroundRole());
00159 qDrawShadePanel( &painter, x, y, w, h, palette(), true, 1, NULL);
00160 if ( fillCol.isValid() )
00161 painter.fillRect( x+1, y+1, w-2, h-2, fillCol );
00162
00163 if ( hasFocus() ) {
00164 QRect focusRect = style()->subElementRect( QStyle::SE_PushButtonFocusRect, &butOpt, this );
00165 QStyleOptionFocusRect focusOpt;
00166 focusOpt.init(this);
00167 focusOpt.rect = focusRect;
00168 focusOpt.backgroundColor = palette().background().color();
00169 style()->drawPrimitive( QStyle::PE_FrameFocusRect, &focusOpt, &painter, this );
00170 }
00171 }
00172
00173 QSize KColorButton::sizeHint() const
00174 {
00175 QStyleOptionButton opt;
00176 d->initStyleOption(&opt);
00177 return style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(40, 15), this).
00178 expandedTo(QApplication::globalStrut());
00179 }
00180
00181 QSize KColorButton::minimumSizeHint() const
00182 {
00183 QStyleOptionButton opt;
00184 d->initStyleOption(&opt);
00185 return style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(3, 3), this).
00186 expandedTo(QApplication::globalStrut());
00187 }
00188
00189 void KColorButton::dragEnterEvent( QDragEnterEvent *event)
00190 {
00191 event->setAccepted( KColorMimeData::canDecode( event->mimeData()) && isEnabled());
00192 }
00193
00194 void KColorButton::dropEvent( QDropEvent *event)
00195 {
00196 QColor c=KColorMimeData::fromMimeData( event->mimeData());
00197 if (c.isValid()) {
00198 setColor(c);
00199 }
00200 }
00201
00202 void KColorButton::keyPressEvent( QKeyEvent *e )
00203 {
00204 int key = e->key() | e->modifiers();
00205
00206 if ( KStandardShortcut::copy().contains( key ) ) {
00207 QMimeData *mime=new QMimeData;
00208 KColorMimeData::populateMimeData(mime,color());
00209 QApplication::clipboard()->setMimeData( mime, QClipboard::Clipboard );
00210 }
00211 else if ( KStandardShortcut::paste().contains( key ) ) {
00212 QColor color=KColorMimeData::fromMimeData( QApplication::clipboard()->mimeData( QClipboard::Clipboard ));
00213 setColor( color );
00214 }
00215 else
00216 QPushButton::keyPressEvent( e );
00217 }
00218
00219 void KColorButton::mousePressEvent( QMouseEvent *e)
00220 {
00221 d->mPos = e->pos();
00222 QPushButton::mousePressEvent(e);
00223 }
00224
00225 void KColorButton::mouseMoveEvent( QMouseEvent *e)
00226 {
00227 if( (e->buttons() & Qt::LeftButton) &&
00228 (e->pos()-d->mPos).manhattanLength() > KGlobalSettings::dndEventDelay() )
00229 {
00230 KColorMimeData::createDrag(color(),this)->start();
00231 setDown(false);
00232 }
00233 }
00234
00235 void KColorButton::KColorButtonPrivate::_k_chooseColor()
00236 {
00237 QColor c = q->color();
00238 if ( m_bdefaultColor )
00239 {
00240 if( KColorDialog::getColor( c, m_defaultColor, q ) != QDialog::Rejected ) {
00241 q->setColor( c );
00242 }
00243 }
00244 else
00245 {
00246 if( KColorDialog::getColor( c, q ) != QDialog::Rejected ) {
00247 q->setColor( c );
00248 }
00249 }
00250 }
00251
00252
00253 #include "kcolorbutton.moc"