summaryrefslogtreecommitdiffstats
path: root/kexi/widget/tableview/kexitableedit.cpp
blob: fd4870fbdb76b0e11990cacdba03e063f98edc2b (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* This file is part of the KDE project
   Copyright (C) 2002 Peter Simonsson <[email protected]>
   Copyright (C) 2003-2006 Jaroslaw Staniek <[email protected]>

   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 "kexitableedit.h"
#include "kexidataawareobjectiface.h"
#include <kexidb/field.h>
#include <kexidb/utils.h>

#include <tqpalette.h>
#include <tqpainter.h>

#include <kglobal.h>
#include <klocale.h>
#include <kdebug.h>

KexiTableEdit::KexiTableEdit(KexiTableViewColumn &column, TQWidget* tqparent)
: TQWidget(dynamic_cast<TQScrollView*>(tqparent) ? dynamic_cast<TQScrollView*>(tqparent)->viewport() : tqparent)
 ,m_column(&column)
// ,m_field(&f)
// ,m_type(f.type()) //copied because the rest of code uses m_type
 ,m_scrollView(dynamic_cast<TQScrollView*>(tqparent))
 ,m_usesSelectedTextColor(true)
 ,m_view(0)
// ,m_hasFocusableWidget(true)
// ,m_acceptEditorAfterDeleteContents(false)
{
	setPaletteBackgroundColor( palette().color(TQPalette::Active, TQColorGroup::Base) );
	installEventFilter(this);

	//margins
	if (displayedField()->isFPNumericType()) {
#ifdef TQ_WS_WIN
		m_leftMargin = 0;
#else
		m_leftMargin = 0;
#endif
	}
	else if (displayedField()->isIntegerType()) {
#ifdef TQ_WS_WIN
		m_leftMargin = 1;
#else
		m_leftMargin = 0;
#endif
	}
	else {//default
#ifdef TQ_WS_WIN
		m_leftMargin = 5;
#else
		m_leftMargin = 5;
#endif
	}

	m_rightMargin = 0;
	m_rightMarginWhenFocused = 0;
}

KexiTableEdit::~KexiTableEdit()
{
}

KexiDB::Field *KexiTableEdit::displayedField() const
{
	if (m_column->visibleLookupColumnInfo)
		return m_column->visibleLookupColumnInfo->field; //mainly for lookup field in KexiComboBoxTableEdit:

	return m_column->field(); //typical case
}

void KexiTableEdit::setViewWidget(TQWidget *v)
{
	m_view = v;
	m_view->move(0,0);
	m_view->installEventFilter(this);
	setFocusProxy(m_view);
}

void KexiTableEdit::moveChild( TQWidget * child, int x, int y )
{
	if (m_scrollView)
		m_scrollView->moveChild(child, x, y);
}

void KexiTableEdit::resize(int w, int h)
{
	TQWidget::resize(w, h);
	if (m_view) {
		if (!tqlayout()) { //if there is tqlayout (eg. KexiInputTableEdit), resize is automatic
			m_view->move(0,0);
			m_view->resize(w, h);
		}
	}
}

bool
KexiTableEdit::eventFilter(TQObject* watched, TQEvent* e)
{
/*	if (watched == m_view) {
		if(e->type() == TQEvent::KeyPress) {
			TQKeyEvent* ev = static_cast<TQKeyEvent*>(e);
//			if (ev->key()==Key_Tab) {

//			}
		}
	}*/

	if(TQT_BASE_OBJECT(watched) == TQT_BASE_OBJECT(this))
	{
		if(e->type() == TQEvent::KeyPress)
		{
			TQKeyEvent* ev = TQT_TQKEYEVENT(e);

			if(ev->key() == Key_Escape)
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	return false;
//	return TQWidget::eventFilter(watched, e);
}

void KexiTableEdit::paintFocusBorders( TQPainter *p, TQVariant &, int x, int y, int w, int h )
{
	p->drawRect(x, y, w, h);
}

void KexiTableEdit::setupContents( TQPainter *p, bool focused, const TQVariant& val, 
	TQString &txt, int &align, int &/*x*/, int &y_offset, int &w, int &h  )
{
	Q_UNUSED(p);
	Q_UNUSED(focused);
	Q_UNUSED(h);
	KexiDB::Field *realField = displayedField();

#ifdef TQ_WS_WIN
//	x = 1;
	y_offset = -1;
#else
//	x = 1;
	y_offset = 0;
#endif

	if (realField->isFPNumericType()) {
//! @todo ADD OPTION to displaying NULL VALUES as e.g. "(null)"
		if (!val.isNull()) {
			txt = KexiDB::formatNumberForVisibleDecimalPlaces(
				val.toDouble(), realField->visibleDecimalPlaces());
		}
		w -= 6;
		align |= AlignRight;
	}
	else if (realField->isIntegerType()) {
		TQ_LLONG num = val.toLongLong();
		w -= 6;
		align |= AlignRight;
		if (!val.isNull())
			txt = TQString::number(num);
	}
	else {//default:
		if (!val.isNull()) {
			txt = val.toString();
		}
		align |= AlignLeft;
	}
}

void KexiTableEdit::paintSelectionBackground( TQPainter *p, bool /*focused*/, 
	const TQString& txt, int align, int x, int y_offset, int w, int h, const TQColor& fillColor,
	const TQFontMetrics &fm, bool readOnly, bool fullRowSelection )
{
	if (!readOnly && !fullRowSelection && !txt.isEmpty()) {
		TQRect bound=fm.boundingRect(x, y_offset, w - (x+x), h, align, txt);
		bound.setY(0);
		bound.setWidth( TQMIN( bound.width()+2, w - (x+x)+1 ) );
		if (align & TQt::AlignLeft) {
			bound.setX(bound.x()-1);
		}
		else if (align & TQt::AlignRight) {
			bound.moveLeft( w - bound.width() ); //move to left, if too wide
		}
//TODO align center
		bound.setHeight(h-1);
		p->fillRect(bound, fillColor);
	}
	else if (fullRowSelection) {
		p->fillRect(0, 0, w, h, fillColor);
	}
}

int KexiTableEdit::widthForValue( TQVariant &val, const TQFontMetrics &fm )
{
	return fm.width( val.toString() );
}

void KexiTableEdit::repaintRelatedCell()
{
	if (dynamic_cast<KexiDataAwareObjectInterface*>(m_scrollView))
		dynamic_cast<KexiDataAwareObjectInterface*>(m_scrollView)->updateCurrentCell();
}

bool KexiTableEdit::showToolTipIfNeeded(const TQVariant& value, const TQRect& rect, const TQFontMetrics& fm,
	bool focused)
{
	Q_UNUSED(value);
	Q_UNUSED(rect);
	Q_UNUSED(fm);
	Q_UNUSED(focused);
	return false;
}

int KexiTableEdit::rightMargin(bool focused) const
{
	return focused ? m_rightMarginWhenFocused : m_rightMargin;
}

#include "kexitableedit.moc"