lib
coloredit.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "coloredit.h"
00022
00023 #include <qvariant.h>
00024 #include <qlayout.h>
00025 #include <qcolor.h>
00026 #include <qpainter.h>
00027
00028 #ifdef QT_ONLY
00029 #include <qcolordialog.h>
00030 #include <qpushbutton.h>
00031 #include <qpixmap.h>
00032 #else
00033 #include <kcolorcombo.h>
00034 #endif
00035
00036 using namespace KoProperty;
00037
00038 ColorButton::ColorButton(Property *property, QWidget *parent, const char *name)
00039 : Widget(property, parent, name)
00040 {
00041 QHBoxLayout *l = new QHBoxLayout(this, 0, 0);
00042 #ifdef QT_ONLY
00043 m_edit = new QPushButton(this);
00044 connect(m_edit, SIGNAL(clicked()), this, SLOT(selectColor()));
00045 #else
00046 m_edit = new KColorCombo(this);
00047 m_edit->setFocusPolicy(QWidget::NoFocus);
00048 connect(m_edit, SIGNAL(activated(int)), this, SLOT(slotValueChanged(int)));
00049 #endif
00050 m_edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
00051 m_edit->setMinimumHeight(5);
00052 l->addWidget(m_edit);
00053 setFocusWidget(m_edit);
00054 }
00055
00056 ColorButton::~ColorButton()
00057 {}
00058
00059 QVariant
00060 ColorButton::value() const
00061 {
00062 #ifdef QT_ONLY
00063 return m_color;
00064 #else
00065 return m_edit->color();
00066 #endif
00067 }
00068
00069 void
00070 ColorButton::setValue(const QVariant &value, bool emitChange)
00071 {
00072 #ifdef QT_ONLY
00073 m_color = value.toColor();
00074 m_edit->setText(m_color.name());
00075 QPixmap px;
00076 px.resize(14,14);
00077 px.fill(m_color);
00078 m_edit->setIconSet(px);
00079 #else
00080 m_edit->blockSignals(true);
00081 m_edit->setColor(value.toColor());
00082 m_edit->blockSignals(false);
00083 #endif
00084 if (emitChange)
00085 emit valueChanged(this);
00086 }
00087
00088 void
00089 ColorButton::drawViewer(QPainter *p, const QColorGroup &, const QRect &r, const QVariant &value)
00090 {
00091 p->eraseRect(r);
00092
00093 p->setBrush(value.toColor());
00094 p->setPen(Qt::SolidLine);
00095 QRect r2(r);
00096 r2.setTopLeft(r.topLeft() + QPoint(5,5));
00097 r2.setBottomRight(r.bottomRight() - QPoint(5,5));
00098 p->drawRect(r2);
00099 }
00100
00101 void
00102 ColorButton::selectColor()
00103 {
00104 #ifdef QT_ONLY
00105 m_color = QColorDialog::getColor(m_color,this);
00106 emit valueChanged(this);
00107 m_edit->setText(m_color.name());
00108 QPixmap px;
00109 px.resize(14,14);
00110 px.fill(m_color);
00111 m_edit->setIconSet(px);
00112 #endif
00113 }
00114
00115 void
00116 ColorButton::slotValueChanged(int)
00117 {
00118 emit valueChanged(this);
00119 }
00120
00121
00122 bool
00123 ColorButton::eventFilter(QObject* watched, QEvent* e)
00124 {
00125 #ifdef QT_ONLY
00126 if(e->type() == QEvent::KeyPress) {
00127 QKeyEvent* ev = static_cast<QKeyEvent*>(e);
00128 if(ev->key() == Key_Space) {
00129 m_edit->animteClick();
00130 return true;
00131 }
00132 }
00133 #endif
00134 return Widget::eventFilter(watched, e);
00135 }
00136
00137 void
00138 ColorButton::setReadOnlyInternal(bool readOnly)
00139 {
00140 setVisibleFlag(!readOnly);
00141 }
00142
00143 #include "coloredit.moc"
|