summaryrefslogtreecommitdiffstats
path: root/kexi/widget/tableview/kexibooltableedit.cpp
blob: 2bc14f4acf10b6a78c9b7342b8a99a92fc708a54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* This file is part of the KDE project
   Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "kexibooltableedit.h"

#include <kexidb/field.h>

#include <tqpainter.h>
#include <tqapplication.h>
#include <tqclipboard.h>

#include <tdeglobal.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <tdeglobalsettings.h>


KexiBoolTableEdit::KexiBoolTableEdit(KexiTableViewColumn &column, TQWidget *parent)
 : KexiTableEdit(column, parent)
{
	setName("KexiBoolTableEdit");
	kdDebug() << "KexiBoolTableEdit: m_origValue.typeName()==" << m_origValue.typeName() << endl;
	kdDebug() << "KexiBoolTableEdit: type== " << field()->typeName() << endl;
	m_hasFocusableWidget = false;
	m_acceptEditorAfterDeleteContents = true;
	m_usesSelectedTextColor = false;
}

KexiBoolTableEdit::~KexiBoolTableEdit()
{
}

void KexiBoolTableEdit::setValueInternal(const TQVariant& /*add*/, bool /*removeOld*/)
{
	m_currentValue = m_origValue;
	//nothing to do more...
}

bool KexiBoolTableEdit::valueIsNull()
{
	return m_currentValue.isNull();
}

bool KexiBoolTableEdit::valueIsEmpty()
{
	return m_currentValue.isNull();
}

TQVariant KexiBoolTableEdit::value()
{
//	ok = true;
	return m_currentValue;
}

void KexiBoolTableEdit::clear()
{
	if (field()->isNotNull())
		m_currentValue = TQVariant(false, 0);
	else
		m_currentValue = TQVariant();
}

bool KexiBoolTableEdit::cursorAtStart()
{
	return true;
}

bool KexiBoolTableEdit::cursorAtEnd()
{
	return true;
}

void KexiBoolTableEdit::setupContents( TQPainter *p, bool focused, const TQVariant& val, 
	TQString &txt, int &align, int &x, int &y_offset, int &w, int &h  )
{
	Q_UNUSED(focused);
	Q_UNUSED(txt);
	Q_UNUSED(align);
	Q_UNUSED(x);
#ifdef TQ_WS_WIN
//	x = 1;
	y_offset = -1;
#else
//	x = 1;
	y_offset = 0;
#endif
	if (p) {
		int s = TQMAX(h - 5, 12);
		s = TQMIN( h-3, s );
		s = TQMIN( w-3, s );//avoid too large box
		TQRect r( TQMAX( w/2 - s/2, 0 ) , h/2 - s/2 /*- 1*/, s, s);
//already set ouotside:		p->setPen(TQPen(colorGroup().text(), 1));
		p->drawRect(r);
		if (val.isNull()) { // && !field()->isNotNull()) {
			p->drawText( r, TQt::AlignCenter, "?" );
		}
		else if (val.toBool()) {
			p->drawLine(r.x(), r.y(), r.right(), r.bottom());
			p->drawLine(r.x(), r.bottom(), r.right(), r.y());
		}
	}
}

void KexiBoolTableEdit::clickedOnContents()
{
	if (field()->isNotNull())
		m_currentValue = TQVariant( !m_currentValue.toBool(), 0 );
	else {
		// null allowed: use the cycle: true -> false -> null
		if (m_currentValue.isNull())
			m_currentValue = TQVariant( true, 1 );
		else
			m_currentValue = m_currentValue.toBool() ? TQVariant( false, 1 ) : TQVariant();
	}
}

void KexiBoolTableEdit::handleAction(const TQString& actionName)
{
	if (actionName=="edit_paste") {
		emit editRequested();
		bool ok;
		const int value = tqApp->clipboard()->text( TQClipboard::Clipboard ).toInt(&ok);
		if (ok) {
			m_currentValue = (value==0) ? TQVariant(false, 0) : TQVariant(true, 1);
		}
		else {
			m_currentValue = field()->isNotNull() 
				? TQVariant(0, false)/*0 instead of NULL - handle case when null is not allowed*/
				: TQVariant();
		}
		repaintRelatedCell();
	}
	else if (actionName=="edit_cut") {
		emit editRequested();
//! @todo handle defaultValue...
		m_currentValue = field()->isNotNull() 
			? TQVariant(0, false)/*0 instead of NULL - handle case when null is not allowed*/
			: TQVariant();
		handleCopyAction(m_origValue, TQVariant());
		repaintRelatedCell();
	}
}

void KexiBoolTableEdit::handleCopyAction(const TQVariant& value, const TQVariant& visibleValue)
{
	Q_UNUSED(visibleValue);
	if (value.type()==TQVariant::Bool)
		tqApp->clipboard()->setText(value.toBool() ? "1" : "0");
	else
		tqApp->clipboard()->setText(TQString());
}

int KexiBoolTableEdit::widthForValue( TQVariant &val, const TQFontMetrics &fm )
{
	Q_UNUSED(fm);
	return val.toPixmap().width();
}

//======================================================

KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiBoolEditorFactoryItem, KexiBoolTableEdit)

#include "kexibooltableedit.moc"