kexi

kexibooltableedit.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "kexibooltableedit.h"
21
22#include <kexidb/field.h>
23
24#include <tqpainter.h>
25#include <tqapplication.h>
26#include <tqclipboard.h>
27
28#include <tdeglobal.h>
29#include <tdelocale.h>
30#include <kdebug.h>
31#include <tdeglobalsettings.h>
32
33
34KexiBoolTableEdit::KexiBoolTableEdit(KexiTableViewColumn &column, TQWidget *parent)
35 : KexiTableEdit(column, parent)
36{
37 setName("KexiBoolTableEdit");
38 kdDebug() << "KexiBoolTableEdit: m_origValue.typeName()==" << m_origValue.typeName() << endl;
39 kdDebug() << "KexiBoolTableEdit: type== " << field()->typeName() << endl;
40 m_hasFocusableWidget = false;
41 m_acceptEditorAfterDeleteContents = true;
42 m_usesSelectedTextColor = false;
43}
44
45KexiBoolTableEdit::~KexiBoolTableEdit()
46{
47}
48
49void KexiBoolTableEdit::setValueInternal(const TQVariant& /*add*/, bool /*removeOld*/)
50{
51 m_currentValue = m_origValue;
52 //nothing to do more...
53}
54
56{
57 return m_currentValue.isNull();
58}
59
61{
62 return m_currentValue.isNull();
63}
64
66{
67// ok = true;
68 return m_currentValue;
69}
70
72{
73 if (field()->isNotNull())
74 m_currentValue = TQVariant(false);
75 else
76 m_currentValue = TQVariant();
77}
78
80{
81 return true;
82}
83
85{
86 return true;
87}
88
89void KexiBoolTableEdit::setupContents( TQPainter *p, bool focused, const TQVariant& val,
90 TQString &txt, int &align, int &x, int &y_offset, int &w, int &h )
91{
92 Q_UNUSED(focused);
93 Q_UNUSED(txt);
94 Q_UNUSED(align);
95 Q_UNUSED(x);
96#ifdef TQ_WS_WIN
97// x = 1;
98 y_offset = -1;
99#else
100// x = 1;
101 y_offset = 0;
102#endif
103 if (p) {
104 int s = TQMAX(h - 5, 12);
105 s = TQMIN( h-3, s );
106 s = TQMIN( w-3, s );//avoid too large box
107 TQRect r( TQMAX( w/2 - s/2, 0 ) , h/2 - s/2 /*- 1*/, s, s);
108//already set ouotside: p->setPen(TQPen(colorGroup().text(), 1));
109 p->drawRect(r);
110 if (val.isNull()) { // && !field()->isNotNull()) {
111 p->drawText( r, TQt::AlignCenter, "?" );
112 }
113 else if (val.toBool()) {
114 p->drawLine(r.x(), r.y(), r.right(), r.bottom());
115 p->drawLine(r.x(), r.bottom(), r.right(), r.y());
116 }
117 }
118}
119
121{
122 if (field()->isNotNull())
123 m_currentValue = TQVariant( !m_currentValue.toBool() );
124 else {
125 // null allowed: use the cycle: true -> false -> null
126 if (m_currentValue.isNull())
127 m_currentValue = TQVariant( true );
128 else
129 m_currentValue = m_currentValue.toBool() ? TQVariant( false ) : TQVariant();
130 }
131}
132
133void KexiBoolTableEdit::handleAction(const TQString& actionName)
134{
135 if (actionName=="edit_paste") {
136 emit editRequested();
137 bool ok;
138 const int value = tqApp->clipboard()->text( TQClipboard::Clipboard ).toInt(&ok);
139 if (ok) {
140 m_currentValue = (value==0) ? TQVariant(false) : TQVariant(true);
141 }
142 else {
144 ? TQVariant(false) : TQVariant();
145 }
147 }
148 else if (actionName=="edit_cut") {
149 emit editRequested();
152 ? TQVariant(false) : TQVariant();
153 handleCopyAction(m_origValue, TQVariant());
155 }
156}
157
158void KexiBoolTableEdit::handleCopyAction(const TQVariant& value, const TQVariant& visibleValue)
159{
160 Q_UNUSED(visibleValue);
161 if (value.type()==TQVariant::Bool)
162 tqApp->clipboard()->setText(value.toBool() ? "1" : "0");
163 else
164 tqApp->clipboard()->setText(TQString());
165}
166
167int KexiBoolTableEdit::widthForValue( TQVariant &val, const TQFontMetrics &fm )
168{
169 Q_UNUSED(fm);
170 return val.toPixmap().width();
171}
172
173//======================================================
174
175KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiBoolEditorFactoryItem, KexiBoolTableEdit)
176
177#include "kexibooltableedit.moc"
178
Cell editor for boolean type.
virtual bool cursorAtEnd()
virtual void setValueInternal(const TQVariant &add, bool removeOld)
initializes this editor with add value
virtual void handleAction(const TQString &actionName)
virtual void handleCopyAction(const TQVariant &value, const TQVariant &visibleValue)
virtual int widthForValue(TQVariant &val, const TQFontMetrics &fm)
virtual void setupContents(TQPainter *p, bool focused, const TQVariant &val, TQString &txt, int &align, int &x, int &y_offset, int &w, int &h)
virtual void clickedOnContents()
TQVariant m_currentValue
We've no editor widget that would store current value, so we do this here.
virtual void clear()
clears item's data, so the data will contain NULL data
virtual bool cursorAtStart()
virtual bool valueIsEmpty()
virtual bool valueIsNull()
virtual TQVariant value()
bool isNotNull() const
Definition field.h:243
virtual TQVariant visibleValue()
Abstract class for a cell editor. Handles cell painting and displaying the editor widget.
void repaintRelatedCell()
virtual KexiDB::Field * field() const