kexi

kexibooltableedit.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "kexibooltableedit.h"
00021 
00022 #include <kexidb/field.h>
00023 
00024 #include <qpainter.h>
00025 #include <qapplication.h>
00026 #include <qclipboard.h>
00027 
00028 #include <kglobal.h>
00029 #include <klocale.h>
00030 #include <kdebug.h>
00031 #include <kglobalsettings.h>
00032 
00033 
00034 KexiBoolTableEdit::KexiBoolTableEdit(KexiTableViewColumn &column, QWidget *parent)
00035  : KexiTableEdit(column, parent)
00036 {
00037     setName("KexiBoolTableEdit");
00038     kdDebug() << "KexiBoolTableEdit: m_origValue.typeName()==" << m_origValue.typeName() << endl;
00039     kdDebug() << "KexiBoolTableEdit: type== " << field()->typeName() << endl;
00040     m_hasFocusableWidget = false;
00041     m_acceptEditorAfterDeleteContents = true;
00042     m_usesSelectedTextColor = false;
00043 }
00044 
00045 KexiBoolTableEdit::~KexiBoolTableEdit()
00046 {
00047 }
00048 
00049 void KexiBoolTableEdit::setValueInternal(const QVariant& /*add*/, bool /*removeOld*/)
00050 {
00051     m_currentValue = m_origValue;
00052     //nothing to do more...
00053 }
00054 
00055 bool KexiBoolTableEdit::valueIsNull()
00056 {
00057     return m_currentValue.isNull();
00058 }
00059 
00060 bool KexiBoolTableEdit::valueIsEmpty()
00061 {
00062     return m_currentValue.isNull();
00063 }
00064 
00065 QVariant KexiBoolTableEdit::value()
00066 {
00067 //  ok = true;
00068     return m_currentValue;
00069 }
00070 
00071 void KexiBoolTableEdit::clear()
00072 {
00073     if (field()->isNotNull())
00074         m_currentValue = QVariant(false, 0);
00075     else
00076         m_currentValue = QVariant();
00077 }
00078 
00079 bool KexiBoolTableEdit::cursorAtStart()
00080 {
00081     return true;
00082 }
00083 
00084 bool KexiBoolTableEdit::cursorAtEnd()
00085 {
00086     return true;
00087 }
00088 
00089 void KexiBoolTableEdit::setupContents( QPainter *p, bool focused, const QVariant& val, 
00090     QString &txt, int &align, int &x, int &y_offset, int &w, int &h  )
00091 {
00092     Q_UNUSED(focused);
00093     Q_UNUSED(txt);
00094     Q_UNUSED(align);
00095     Q_UNUSED(x);
00096 #ifdef Q_WS_WIN
00097 //  x = 1;
00098     y_offset = -1;
00099 #else
00100 //  x = 1;
00101     y_offset = 0;
00102 #endif
00103     if (p) {
00104         int s = QMAX(h - 5, 12);
00105         s = QMIN( h-3, s );
00106         s = QMIN( w-3, s );//avoid too large box
00107         QRect r( QMAX( w/2 - s/2, 0 ) , h/2 - s/2 /*- 1*/, s, s);
00108 //already set ouotside:     p->setPen(QPen(colorGroup().text(), 1));
00109         p->drawRect(r);
00110         if (val.isNull()) { // && !field()->isNotNull()) {
00111             p->drawText( r, Qt::AlignCenter, "?" );
00112         }
00113         else if (val.toBool()) {
00114             p->drawLine(r.x(), r.y(), r.right(), r.bottom());
00115             p->drawLine(r.x(), r.bottom(), r.right(), r.y());
00116         }
00117     }
00118 }
00119 
00120 void KexiBoolTableEdit::clickedOnContents()
00121 {
00122     if (field()->isNotNull())
00123         m_currentValue = QVariant( !m_currentValue.toBool(), 0 );
00124     else {
00125         // null allowed: use the cycle: true -> false -> null
00126         if (m_currentValue.isNull())
00127             m_currentValue = QVariant( true, 1 );
00128         else
00129             m_currentValue = m_currentValue.toBool() ? QVariant( false, 1 ) : QVariant();
00130     }
00131 }
00132 
00133 void KexiBoolTableEdit::handleAction(const QString& actionName)
00134 {
00135     if (actionName=="edit_paste") {
00136         emit editRequested();
00137         bool ok;
00138         const int value = qApp->clipboard()->text( QClipboard::Clipboard ).toInt(&ok);
00139         if (ok) {
00140             m_currentValue = (value==0) ? QVariant(false, 0) : QVariant(true, 1);
00141         }
00142         else {
00143             m_currentValue = field()->isNotNull() 
00144                 ? QVariant(0, false)/*0 instead of NULL - handle case when null is not allowed*/
00145                 : QVariant();
00146         }
00147         repaintRelatedCell();
00148     }
00149     else if (actionName=="edit_cut") {
00150         emit editRequested();
00152         m_currentValue = field()->isNotNull() 
00153             ? QVariant(0, false)/*0 instead of NULL - handle case when null is not allowed*/
00154             : QVariant();
00155         handleCopyAction(m_origValue, QVariant());
00156         repaintRelatedCell();
00157     }
00158 }
00159 
00160 void KexiBoolTableEdit::handleCopyAction(const QVariant& value, const QVariant& visibleValue)
00161 {
00162     Q_UNUSED(visibleValue);
00163     if (value.type()==QVariant::Bool)
00164         qApp->clipboard()->setText(value.toBool() ? "1" : "0");
00165     else
00166         qApp->clipboard()->setText(QString::null);
00167 }
00168 
00169 int KexiBoolTableEdit::widthForValue( QVariant &val, const QFontMetrics &fm )
00170 {
00171     Q_UNUSED(fm);
00172     return val.toPixmap().width();
00173 }
00174 
00175 //======================================================
00176 
00177 KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiBoolEditorFactoryItem, KexiBoolTableEdit)
00178 
00179 #include "kexibooltableedit.moc"
00180 
KDE Home | KDE Accessibility Home | Description of Access Keys