lib

booledit.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 "booledit.h"
00022 
00023 #ifndef QT_ONLY
00024 #include <kiconloader.h>
00025 #include <klocale.h>
00026 #endif
00027 
00028 #include <qtoolbutton.h>
00029 #include <qpainter.h>
00030 #include <qvariant.h>
00031 #include <qlayout.h>
00032 
00033 #include <kdebug.h>
00034 
00035 using namespace KoProperty;
00036 
00037 BoolEdit::BoolEdit(Property *property, QWidget *parent, const char *name)
00038  : Widget(property, parent, name)
00039 {
00040     m_toggle = new QToolButton(this);
00041     m_toggle->setFocusPolicy(QWidget::WheelFocus);
00042     m_toggle->setToggleButton(true);
00043     m_toggle->setUsesTextLabel(true);
00044     m_toggle->setTextPosition(QToolButton::Right);
00045     m_toggle->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
00046     //we're not using layout to because of problems with button size
00047     m_toggle->move(0, 0);
00048     m_toggle->resize(width(), height());
00049 //    m_toggle->show();
00050 
00051     setFocusWidget(m_toggle);
00052     connect(m_toggle, SIGNAL(toggled(bool)), this, SLOT(slotValueChanged(bool)));
00053 }
00054 
00055 BoolEdit::~BoolEdit()
00056 {
00057 }
00058 
00059 QVariant
00060 BoolEdit::value() const
00061 {
00062     return QVariant(m_toggle->isOn(), 4);
00063 }
00064 
00065 void
00066 BoolEdit::setValue(const QVariant &value, bool emitChange)
00067 {
00068     m_toggle->blockSignals(true);
00069     m_toggle->setOn(value.toBool());
00070     setState(value.toBool());
00071     m_toggle->blockSignals(false);
00072     if (emitChange)
00073         emit valueChanged(this);
00074 }
00075 
00076 void
00077 BoolEdit::slotValueChanged(bool state)
00078 {
00079     setState(state);
00080     emit valueChanged(this);
00081 }
00082 
00083 void
00084 BoolEdit::drawViewer(QPainter *p, const QColorGroup &, const QRect &r, const QVariant &value)
00085 {
00086     p->eraseRect(r);
00087     QRect r2(r);
00088     r2.moveLeft(KIcon::SizeSmall + 6);
00089 
00090     if(value.toBool()) {
00091         p->drawPixmap(3, (r.height()-1-KIcon::SizeSmall)/2, SmallIcon("button_ok"));
00092         p->drawText(r2, Qt::AlignVCenter | Qt::AlignLeft, i18n("Yes"));
00093     }
00094     else  {
00095         p->drawPixmap(3, (r.height()-1-KIcon::SizeSmall)/2, SmallIcon("button_no"));
00096         p->drawText(r2, Qt::AlignVCenter | Qt::AlignLeft, i18n("No"));
00097     }
00098 }
00099 
00100 void
00101 BoolEdit::setState(bool state)
00102 {
00103     if(state)
00104     {
00105         m_toggle->setIconSet(QIconSet(SmallIcon("button_ok")));
00106         m_toggle->setTextLabel(i18n("Yes"));
00107     }
00108     else
00109     {
00110         m_toggle->setIconSet(QIconSet(SmallIcon("button_no")));
00111         m_toggle->setTextLabel(i18n("No"));
00112     }
00113 }
00114 
00115 void
00116 BoolEdit::resizeEvent(QResizeEvent *ev)
00117 {
00118     m_toggle->resize(ev->size());
00119 }
00120 
00121 bool
00122 BoolEdit::eventFilter(QObject* watched, QEvent* e)
00123 {
00124     if(e->type() == QEvent::KeyPress) {
00125         QKeyEvent* ev = static_cast<QKeyEvent*>(e);
00126         if(ev->key() == Key_Space) {
00127             if (m_toggle)
00128                 m_toggle->toggle();
00129             return true;
00130         }
00131     }
00132     return Widget::eventFilter(watched, e);
00133 }
00134 
00135 void
00136 BoolEdit::setReadOnlyInternal(bool readOnly)
00137 {
00138     setVisibleFlag(!readOnly);
00139 }
00140 
00141 #include "booledit.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys