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 QVariant
00059 BoolEdit::value() const
00060 {
00061     return QVariant(m_toggle->isOn(), 4);
00062 }
00063 
00064 void
00065 BoolEdit::setValue(const QVariant &value, bool emitChange)
00066 {
00067     m_toggle->blockSignals(true);
00068     m_toggle->setOn(value.toBool());
00069     setState(value.toBool());
00070     m_toggle->blockSignals(false);
00071     if (emitChange)
00072         emit valueChanged(this);
00073 }
00074 
00075 void
00076 BoolEdit::slotValueChanged(bool state)
00077 {
00078     setState(state);
00079     emit valueChanged(this);
00080 }
00081 
00082 void
00083 BoolEdit::drawViewer(QPainter *p, const QColorGroup &, const QRect &r, const QVariant &value)
00084 {
00085     p->eraseRect(r);
00086     QRect r2(r);
00087     r2.moveLeft(KIcon::SizeSmall + 6);
00088 
00089     if(value.toBool()) {
00090         p->drawPixmap(3, (r.height()-1-KIcon::SizeSmall)/2, SmallIcon("button_ok"));
00091         p->drawText(r2, Qt::AlignVCenter | Qt::AlignLeft, i18n("Yes"));
00092     }
00093     else  {
00094         p->drawPixmap(3, (r.height()-1-KIcon::SizeSmall)/2, SmallIcon("button_no"));
00095         p->drawText(r2, Qt::AlignVCenter | Qt::AlignLeft, i18n("No"));
00096     }
00097 }
00098 
00099 void
00100 BoolEdit::setState(bool state)
00101 {
00102     if(state)
00103     {
00104         m_toggle->setIconSet(QIconSet(SmallIcon("button_ok")));
00105         m_toggle->setTextLabel(i18n("Yes"));
00106     }
00107     else
00108     {
00109         m_toggle->setIconSet(QIconSet(SmallIcon("button_no")));
00110         m_toggle->setTextLabel(i18n("No"));
00111     }
00112 }
00113 
00114 void
00115 BoolEdit::resizeEvent(QResizeEvent *ev)
00116 {
00117     m_toggle->resize(ev->size());
00118 }
00119 
00120 bool
00121 BoolEdit::eventFilter(QObject* watched, QEvent* e)
00122 {
00123     if(e->type() == QEvent::KeyPress) {
00124         QKeyEvent* ev = static_cast<QKeyEvent*>(e);
00125         if(ev->key() == Key_Space) {
00126             m_toggle->toggle();
00127             return true;
00128         }
00129     }
00130     return Widget::eventFilter(watched, e);
00131 }
00132 
00133 void
00134 BoolEdit::setReadOnlyInternal(bool readOnly)
00135 {
00136     setVisibleFlag(!readOnly);
00137 }
00138 
00139 #include "booledit.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys