summaryrefslogtreecommitdiffstats
path: root/kexi/widget/tableview/kexicomboboxtableedit.cpp
blob: 9eecc49865e759b0d36ccf9c9d12d4f9a310aec8 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* This file is part of the KDE project
   Copyright (C) 2002   Peter Simonsson <psn@linux.se>
   Copyright (C) 2003-2006 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 <tqlayout.h>
#include <tqstyle.h>
#include <tqwindowsstyle.h>
#include <tqpainter.h>
#include <tqapplication.h>
#include <tqclipboard.h>

#include "kexicomboboxtableedit.h"
#include <widget/utils/kexicomboboxdropdownbutton.h>
#include "kexicomboboxpopup.h"
#include "kexitableview.h"
#include "kexitableitem.h"
#include "kexi.h"

#include <klineedit.h>

//! @internal
class KexiComboBoxTableEdit::Private
{
public:
	Private()
	 : popup(0)
	 , currentEditorWidth(0)
	 , visibleTableViewColumn(0)
	 , internalEditor(0)
	{
	}
	~Private()
	{
		delete internalEditor;
		delete visibleTableViewColumn;
	}

	KPushButton *button;
	KexiComboBoxPopup *popup;
	int currentEditorWidth;
	TQSize totalSize;
	KexiTableViewColumn* visibleTableViewColumn;
	KexiTableEdit* internalEditor;
};

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

KexiComboBoxTableEdit::KexiComboBoxTableEdit(KexiTableViewColumn &column, TQWidget *parent)
 : KexiInputTableEdit(column, parent)
 , KexiComboBoxBase()
 , d(new Private())
{
	setName("KexiComboBoxTableEdit");
	m_setVisibleValueOnSetValueInternal = true;
	d->button = new KexiComboBoxDropDownButton( parentWidget() /*usually a viewport*/ );
	d->button->hide();
	d->button->setFocusPolicy( TQWidget::NoFocus );
	connect(d->button, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotButtonClicked()));

	connect(m_lineedit, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotLineEditTextChanged(const TQString&)));

//	m_lineedit = new KLineEdit(this, "lineedit");
//	m_lineedit->setFrame(false);
//	m_lineedit->setFrameStyle( TQFrame::Plain | TQFrame::Box );
//	m_lineedit->setLineWidth( 1 );
//	if (f.isNumericType()) {
//		m_lineedit->setAlignment(AlignRight);
//	}
//	setView( m_lineedit );

//	layout->addWidget(m_view);
//	m_combo->setEditable( true );
//	m_combo->clear();
//	m_combo->insertStringList(f.enumHints());
//	TQStringList::ConstIterator it, end( f.enumHints().constEnd() );
//	for ( it = f.enumHints().constBegin(); it != end; ++it) {
//		if(!hints.at(i).isEmpty())
//			m_combo->insertItem(hints.at(i));
//	}

//js:	TODO
//js	static_cast<KComboBox*>(m_view)->insertStringList(list);
//js	static_cast<KComboBox*>(m_view)->setCurrentItem(static_cast<int>(t));
}

KexiComboBoxTableEdit::~KexiComboBoxTableEdit()
{
	delete d;
}

void KexiComboBoxTableEdit::createInternalEditor(KexiDB::QuerySchema& schema)
{
	if (!m_column->visibleLookupColumnInfo || d->visibleTableViewColumn/*sanity*/)
		return;
	const KexiDB::Field::Type t = m_column->visibleLookupColumnInfo->field->type();
//! @todo subtype?
	KexiCellEditorFactoryItem *item = KexiCellEditorFactory::item(t);
	if (!item || item->className()=="KexiInputTableEdit")
		return; //unsupported type or there is no need to use subeditor for KexiInputTableEdit
	//special cases: BLOB, Bool datatypes
//todo
	//find real type to display
	KexiDB::QueryColumnInfo *ci = m_column->visibleLookupColumnInfo;
	KexiDB::QueryColumnInfo *visibleLookupColumnInfo = 0;
	if (ci->indexForVisibleLookupValue() != -1) {
		//Lookup field is defined
		visibleLookupColumnInfo = schema.expandedOrInternalField( ci->indexForVisibleLookupValue() );
	}
	d->visibleTableViewColumn = new KexiTableViewColumn(schema, *ci, visibleLookupColumnInfo);
//! todo set d->internalEditor visible and use it to enable data entering by hand
	d->internalEditor = KexiCellEditorFactory::createEditor(*d->visibleTableViewColumn, 0);
	m_lineedit->hide();
}

KexiComboBoxPopup *KexiComboBoxTableEdit::popup() const
{
	return d->popup;
}

void KexiComboBoxTableEdit::setPopup(KexiComboBoxPopup *popup)
{
	d->popup = popup;
}

void KexiComboBoxTableEdit::showFocus( const TQRect& r, bool readOnly )
{
//	d->button->move( pos().x()+ width(), pos().y() );
	updateFocus( r );
	d->button->setEnabled(!readOnly);
	if (readOnly)
		d->button->hide();
	else
		d->button->show();
}

void KexiComboBoxTableEdit::resize(int w, int h)
{
	d->totalSize = TQSize(w,h);
	if (!column()->isReadOnly()) {
		d->button->resize( h, h );
		TQWidget::resize(w - d->button->width(), h);
	}
	m_rightMarginWhenFocused = m_rightMargin + (column()->isReadOnly() ? 0 : d->button->width());
	TQRect r( pos().x(), pos().y(), w+1, h+1 );
	if (m_scrollView)
		r.moveBy(m_scrollView->contentsX(), m_scrollView->contentsY());
	updateFocus( r );
	if (popup()) {
		popup()->updateSize();
	}
}

// internal
void KexiComboBoxTableEdit::updateFocus( const TQRect& r )
{
	if (!column()->isReadOnly()) {
		if (d->button->width() > r.width())
			moveChild(d->button, r.right() + 1, r.top());
		else
			moveChild(d->button, r.right() - d->button->width(), r.top() );
	}
}

void KexiComboBoxTableEdit::hideFocus()
{
	d->button->hide();
}

TQVariant KexiComboBoxTableEdit::visibleValue()
{
	return KexiComboBoxBase::visibleValue();
/*	KexiDB::LookupFieldSchema *lookupFieldSchema = this->lookupFieldSchema();
	if (!popup() || !lookupFieldSchema)
		return TQVariant();
	KexiTableItem *it = popup()->tableView()->selectedItem();
	return it ? it->at( lookupFieldSchema->visibleColumn() ) : TQVariant();*/
}

void KexiComboBoxTableEdit::clear()
{
	m_lineedit->clear();
	KexiComboBoxBase::clear();
}

bool KexiComboBoxTableEdit::valueChanged()
{
	const tristate res = valueChangedInternal();
	if (~res) //no result: just compare values
		return KexiInputTableEdit::valueChanged();
	return res == true;
}

void KexiComboBoxTableEdit::paintFocusBorders( TQPainter *p, TQVariant &, int x, int y, int w, int h )
{
//	d->currentEditorWidth = w;
	if (!column()->isReadOnly()) {
		if (w > d->button->width())
			w -= d->button->width();
	}
	p->drawRect(x, y, w, h);
}

void KexiComboBoxTableEdit::setupContents( TQPainter *p, bool focused, const TQVariant& val, 
	TQString &txt, int &align, int &x, int &y_offset, int &w, int &h  )
{
	if (d->internalEditor) {
		d->internalEditor->setupContents( p, focused, val, txt, align, x, y_offset, w, h );
	}
	else {
		KexiInputTableEdit::setupContents( p, focused, val, txt, align, x, y_offset, w, h );
	}
	if (!column()->isReadOnly() && focused && (w > d->button->width()))
		w -= (d->button->width() - x);
	if (!val.isNull()) {
		KexiTableViewData *relData = column()->relatedData();
		KexiDB::LookupFieldSchema *lookupFieldSchema = 0;
		if (relData) {
			int rowToHighlight;
			txt = valueForString(val.toString(), &rowToHighlight, 0, 1);
		}
		else if ((lookupFieldSchema = this->lookupFieldSchema())) {
		/* handled at at KexiTableView level
			if (popup()) {
				KexiTableItem *it = popup()->tableView()->selectedItem();
				if (it && lookupFieldSchema->visibleColumn()!=-1 && (int)it->size() >= lookupFieldSchema->visibleColumn())
					txt = it->at( lookupFieldSchema->visibleColumn() ).toString();
			}*/
		}
		else {
			//use 'enum hints' model
			txt = field()->enumHint( val.toInt() );
		}
	}
}

void KexiComboBoxTableEdit::slotButtonClicked()
{
	// this method is sometimes called by hand: 
	// do not allow to simulate clicks when the button is disabled
	if (column()->isReadOnly() || !d->button->isEnabled())
		return;

	if (m_mouseBtnPressedWhenPopupVisible) {
		m_mouseBtnPressedWhenPopupVisible = false;
		d->button->setOn(false);
		return;
	}
	kdDebug() << "KexiComboBoxTableEdit::slotButtonClicked()" << endl;
	if (!popup() || !popup()->isVisible()) {
		kdDebug() << "SHOW POPUP" << endl;
		showPopup();
		d->button->setOn(true);
	}
}

void KexiComboBoxTableEdit::slotPopupHidden()
{
	d->button->setOn(false);
//	d->currentEditorWidth = 0;
}

void KexiComboBoxTableEdit::updateButton()
{
	d->button->setOn(popup()->isVisible());
}

void KexiComboBoxTableEdit::hide()
{
	KexiInputTableEdit::hide();
	KexiComboBoxBase::hide();
	d->button->setOn(false);
}

void KexiComboBoxTableEdit::show()
{
	KexiInputTableEdit::show();
	if (!column()->isReadOnly()) {
		d->button->show();
	}
}

bool KexiComboBoxTableEdit::handleKeyPress( TQKeyEvent *ke, bool editorActive )
{
	const int k = ke->key();
	if ((ke->state()==Qt::NoButton && k==TQt::Key_F4)
		|| (ke->state()==AltButton && k==TQt::Key_Down))
	{
		//show popup
		slotButtonClicked();
		return true;
	}
	else if (editorActive) {
		const bool enterPressed = k==TQt::Key_Enter || k==TQt::Key_Return;
		if (enterPressed && m_internalEditorValueChanged) {
			createPopup(false);
			selectItemForEnteredValueInLookupTable( m_userEnteredValue );
			return false;
		}

		return handleKeyPressForPopup( ke );
	}

	return false;
}

void KexiComboBoxTableEdit::slotLineEditTextChanged(const TQString& s)
{
	slotInternalEditorValueChanged(s);
}

int KexiComboBoxTableEdit::widthForValue( TQVariant &val, const TQFontMetrics &fm )
{
	KexiTableViewData *relData = column() ? column()->relatedData() : 0;
	if (lookupFieldSchema() || relData) {
		// in 'lookupFieldSchema' or  or 'related table data' model 
		// we're assuming val is already the text, not the index
//! @todo ok?
		return TQMAX(KEXITV_MINIMUM_COLUMN_WIDTH, fm.width(val.toString()));
	}
	//use 'enum hints' model
	TQValueVector<TQString> hints = field()->enumHints();
	bool ok;
	int idx = val.toInt(&ok);
	if (!ok || idx < 0 || idx > int(hints.size()-1))
		return KEXITV_MINIMUM_COLUMN_WIDTH;
	TQString txt = hints.at( idx, &ok );
	if (!ok)
		return KEXITV_MINIMUM_COLUMN_WIDTH;
	return fm.width( txt );
}

bool KexiComboBoxTableEdit::eventFilter( TQObject *o, TQEvent *e )
{
	if (!column()->isReadOnly() && e->type()==TQEvent::MouseButtonPress && m_scrollView) {
		TQPoint gp = TQT_TQMOUSEEVENT(e)->globalPos() 
			+ TQPoint(m_scrollView->childX(d->button), m_scrollView->childY(d->button));
		TQRect r(d->button->mapToGlobal(d->button->geometry().topLeft()), 
			d->button->mapToGlobal(d->button->geometry().bottomRight()));
		if (o==popup() && popup()->isVisible() && r.contains( gp )) {
			m_mouseBtnPressedWhenPopupVisible = true;
		}
	}
	return false;
}

TQSize KexiComboBoxTableEdit::totalSize() const
{
	return d->totalSize;
}

TQWidget *KexiComboBoxTableEdit::internalEditor() const
{
	return m_lineedit;
}

void KexiComboBoxTableEdit::moveCursorToEndInInternalEditor()
{
	moveCursorToEnd();
}

void KexiComboBoxTableEdit::selectAllInInternalEditor()
{
	selectAll();
}

void KexiComboBoxTableEdit::moveCursorToEnd()
{
	m_lineedit->end(false/*!mark*/);
}

void KexiComboBoxTableEdit::moveCursorToStart()
{
	m_lineedit->home(false/*!mark*/);
}

void KexiComboBoxTableEdit::selectAll()
{
	m_lineedit->selectAll();
}

void KexiComboBoxTableEdit::setValueInInternalEditor(const TQVariant& value)
{
	m_lineedit->setText(value.toString());
}

TQVariant KexiComboBoxTableEdit::valueFromInternalEditor()
{
	return m_lineedit->text();
}

TQPoint KexiComboBoxTableEdit::mapFromParentToGlobal(const TQPoint& pos) const
{
	KexiTableView *tv = dynamic_cast<KexiTableView*>(m_scrollView);
	if (!tv)
		return TQPoint(-1,-1);
	return tv->viewport()->mapToGlobal(pos);
}

int KexiComboBoxTableEdit::popupWidthHint() const
{
	return m_lineedit->width() + m_leftMargin + m_rightMarginWhenFocused; //TQMAX(popup()->width(), d->currentEditorWidth);
}

void KexiComboBoxTableEdit::handleCopyAction(const TQVariant& value, const TQVariant& visibleValue)
{
	Q_UNUSED(value);
//! @todo does not work with BLOBs!
	tqApp->clipboard()->setText( visibleValue.toString() );
}

void KexiComboBoxTableEdit::handleAction(const TQString& actionName)
{
	const bool alreadyVisible = m_lineedit->isVisible();

	if (actionName=="edit_paste") {
		if (!alreadyVisible) { //paste as the entire text if the cell was not in edit mode
			emit editRequested();
			m_lineedit->clear();
		}
//! @todo does not work with BLOBs!
		setValueInInternalEditor( tqApp->clipboard()->text() );
	}
	else
		KexiInputTableEdit::handleAction(actionName);
}


KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiComboBoxEditorFactoryItem, KexiComboBoxTableEdit)

#include "kexicomboboxtableedit.moc"