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    Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "booledit.h"
00023 #include "../property.h"
00024 
00025 #include <kiconloader.h>
00026 #include <klocale.h>
00027 #include <kcombobox.h>
00028 #include <kdebug.h>
00029 
00030 #include <qtoolbutton.h>
00031 #include <qpainter.h>
00032 #include <qvariant.h>
00033 #include <qlayout.h>
00034 #include <qbitmap.h>
00035 
00036 using namespace KoProperty;
00037 
00038 BoolEdit::BoolEdit(Property *property, QWidget *parent, const char *name)
00039  : Widget(property, parent, name)
00040  , m_yesIcon( SmallIcon("button_ok") )
00041  , m_noIcon( SmallIcon("button_no") )
00042 {
00043     m_toggle = new QToolButton(this);
00044     m_toggle->setToggleButton( true );
00045     m_toggle->setFocusPolicy(QWidget::WheelFocus);
00046     m_toggle->setUsesTextLabel(true);
00047     m_toggle->setTextPosition(QToolButton::Right);
00048     m_toggle->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
00049     //we're not using layout to because of problems with button size
00050     m_toggle->move(0, 0);
00051     m_toggle->resize(width(), height());
00052     setFocusWidget(m_toggle);
00053     connect(m_toggle, SIGNAL(stateChanged(int)), this, SLOT(slotValueChanged(int)));
00054 }
00055 
00056 BoolEdit::~BoolEdit()
00057 {
00058 }
00059 
00060 QVariant
00061 BoolEdit::value() const
00062 {
00063     return QVariant(m_toggle->isOn(), 4);
00064 }
00065 
00066 void
00067 BoolEdit::setValue(const QVariant &value, bool emitChange)
00068 {
00069     m_toggle->blockSignals(true);
00070     m_toggle->setOn(value.toBool());
00071     setState( value.toBool() ? QButton::On : QButton::Off );
00072     m_toggle->blockSignals(false);
00073     if (emitChange)
00074         emit valueChanged(this);
00075 }
00076 
00077 void
00078 BoolEdit::slotValueChanged(int state)
00079 {
00080     setState(state);
00081     emit valueChanged(this);
00082 }
00083 
00084 static void drawViewerInternal(QPainter *p, const QRect &r, const QVariant &value,
00085  const QPixmap& yesIcon, const QPixmap& noIcon, const QString& nullText)
00086 {
00087     p->eraseRect(r);
00088     QRect r2(r);
00089     r2.moveLeft(KIcon::SizeSmall + 6);
00090 
00091     if(value.isNull() && !nullText.isEmpty()) {
00092         p->drawText(r2, Qt::AlignVCenter | Qt::AlignLeft, nullText);
00093     }
00094     else if(value.toBool()) {
00095         p->drawPixmap(3, (r.height()-1-KIcon::SizeSmall)/2, yesIcon);
00096         p->drawText(r2, Qt::AlignVCenter | Qt::AlignLeft, i18n("Yes"));
00097     }
00098     else {
00099         p->drawPixmap(3, (r.height()-1-KIcon::SizeSmall)/2, noIcon);
00100         p->drawText(r2, Qt::AlignVCenter | Qt::AlignLeft, i18n("No"));
00101     }
00102 }
00103 
00104 void
00105 BoolEdit::drawViewer(QPainter *p, const QColorGroup &cg, const QRect &r, const QVariant &value)
00106 {
00107     Q_UNUSED(cg);
00108     drawViewerInternal(p, r, value, m_yesIcon, m_noIcon, "");
00109 }
00110 
00111 void
00112 BoolEdit::setState(int state)
00113 {
00114     if(QButton::On == state) {
00115         m_toggle->setIconSet(QIconSet(m_yesIcon));
00116         m_toggle->setTextLabel(i18n("Yes"));
00117     }
00118     else if (QButton::Off == state) {
00119         m_toggle->setIconSet(QIconSet(m_noIcon));
00120         m_toggle->setTextLabel(i18n("No"));
00121     }
00122 }
00123 
00124 void
00125 BoolEdit::resizeEvent(QResizeEvent *ev)
00126 {
00127     m_toggle->resize(ev->size());
00128 }
00129 
00130 bool
00131 BoolEdit::eventFilter(QObject* watched, QEvent* e)
00132 {
00133     if(e->type() == QEvent::KeyPress) {
00134         QKeyEvent* ev = static_cast<QKeyEvent*>(e);
00135         const int k = ev->key();
00136         if(k == Qt::Key_Space || k == Qt::Key_Enter || k == Qt::Key_Return) {
00137             if (m_toggle)
00138                 m_toggle->toggle();
00139             return true;
00140         }
00141     }
00142     return Widget::eventFilter(watched, e);
00143 }
00144 
00145 void
00146 BoolEdit::setReadOnlyInternal(bool readOnly)
00147 {
00148     setVisibleFlag(!readOnly);
00149 }
00150 
00151 //--------------------------------------------------
00152 
00153 ThreeStateBoolEdit::ThreeStateBoolEdit(Property *property, QWidget *parent, const char *name)
00154  : ComboBox(property, parent, name)
00155  , m_yesIcon( SmallIcon("button_ok") )
00156  , m_noIcon( SmallIcon("button_no") )
00157 {
00158     m_edit->insertItem( m_yesIcon, i18n("Yes") );
00159     m_edit->insertItem( m_noIcon, i18n("No") );
00160     QVariant thirdState = property ? property->option("3rdState") : QVariant();
00161     QPixmap nullIcon( m_yesIcon.size() ); //transparent pixmap of appropriate size
00162     nullIcon.setMask(QBitmap(m_yesIcon.size(), true));
00163     m_edit->insertItem( nullIcon, thirdState.toString().isEmpty() ? i18n("None") : thirdState.toString() );
00164 }
00165 
00166 ThreeStateBoolEdit::~ThreeStateBoolEdit()
00167 {
00168 }
00169 
00170 QVariant
00171 ThreeStateBoolEdit::value() const
00172 {
00173     // list items: true, false, NULL
00174     const int idx = m_edit->currentItem();
00175     if (idx==0)
00176         return QVariant(true, 1);
00177     else
00178         return idx==1 ? QVariant(false) : QVariant();
00179 }
00180 
00181 void
00182 ThreeStateBoolEdit::setProperty(Property *prop)
00183 {
00184     m_setValueEnabled = false; //setValue() couldn't be called before fillBox()
00185     Widget::setProperty(prop);
00186     m_setValueEnabled = true;
00187     if(prop)
00188         setValue(prop->value(), false); //now the value can be set
00189 }
00190 
00191 void
00192 ThreeStateBoolEdit::setValue(const QVariant &value, bool emitChange)
00193 {
00194     if (!m_setValueEnabled)
00195         return;
00196 
00197     if (value.isNull())
00198         m_edit->setCurrentItem(2);
00199     else
00200         m_edit->setCurrentItem(value.toBool() ? 0 : 1);
00201 
00202     if (emitChange)
00203         emit valueChanged(this);
00204 }
00205 
00206 void
00207 ThreeStateBoolEdit::drawViewer(QPainter *p, const QColorGroup &cg, const QRect &r, const QVariant &value)
00208 {
00209     Q_UNUSED(cg);
00210     drawViewerInternal(p, r, value, m_yesIcon, m_noIcon, m_edit->text(2));
00211 }
00212 
00213 #include "booledit.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys