lib

coloredit.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
00003    Copyright (C) 2004  Alexander Dymo <cloudtemple@mskat.net>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
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"
KDE Home | KDE Accessibility Home | Description of Access Keys