summaryrefslogtreecommitdiffstats
path: root/kexi/plugins/forms/kexidatasourcepage.cpp
blob: ba21827e51e397796ff59ed1a43821623109d807 (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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* This file is part of the KDE project
   Copyright (C) 2005 Jaroslaw Staniek <[email protected]>

   This library 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 library 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 library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "kexidatasourcepage.h"

#include <tqlabel.h>
#include <tqlayout.h>
#include <tqtooltip.h>
#include <tqheader.h>

#include <kiconloader.h>
#include <tdelocale.h>
#include <tdetoolbarbutton.h>
#include <kdebug.h>
#include <tdepopupmenu.h>

#include <widget/kexipropertyeditorview.h>
#include <widget/kexidatasourcecombobox.h>
#include <widget/kexifieldlistview.h>
#include <widget/kexifieldcombobox.h>
#include <widget/kexismalltoolbutton.h>
#include <kexidb/connection.h>
#include <kexiproject.h>

#include <formeditor/commands.h>

#include <koproperty/property.h>
#include <koproperty/utils.h>

KexiDataSourcePage::KexiDataSourcePage(TQWidget *parent, const char *name)
 : TQWidget(parent, name)
 , m_insideClearDataSourceSelection(false)
{
	TQVBoxLayout *vlyr = new TQVBoxLayout(this);
	m_objectInfoLabel = new KexiObjectInfoLabel(this, "KexiObjectInfoLabel");
	vlyr->addWidget(m_objectInfoLabel);

	m_noDataSourceAvailableSingleText = i18n("No data source could be assigned for this widget.");
	m_noDataSourceAvailableMultiText = i18n("No data source could be assigned for multiple widgets.");

	vlyr->addSpacing(8);

	//Section 1: Form's/Widget's Data Source
	KoProperty::GroupContainer *container = new KoProperty::GroupContainer(i18n("Data Source"), this);
	vlyr->addWidget(container);

	TQWidget *contents = new TQWidget(container);
	container->setContents(contents);
	TQVBoxLayout *contentsVlyr = new TQVBoxLayout(contents);

	m_noDataSourceAvailableLabel = new TQLabel(m_noDataSourceAvailableSingleText, contents);
	m_noDataSourceAvailableLabel->setSizePolicy(TQSizePolicy::Preferred, TQSizePolicy::Fixed);
	m_noDataSourceAvailableLabel->setMargin(2);
	m_noDataSourceAvailableLabel->setAlignment(TQt::WordBreak | TQt::AlignBottom | TQt::AlignLeft);
	contentsVlyr->addWidget(m_noDataSourceAvailableLabel);

	//-Widget's Data Source
	TQHBoxLayout *hlyr = new TQHBoxLayout(contentsVlyr);
#if 0
//! @todo unhide this when expression work
//	m_widgetDSLabel = new TQLabel(i18n("Table Field, Query Field or Expression", "Source field or expression:"), this);
#else
	m_widgetDSLabel = new TQLabel(i18n("Table Field or Query Field", "Widget's data source:"), contents);
#endif
	m_widgetDSLabel->setSizePolicy(TQSizePolicy::Minimum, TQSizePolicy::Fixed);
	m_widgetDSLabel->setMargin(2);
	m_widgetDSLabel->setMinimumHeight(IconSize(TDEIcon::Small)+4);
	m_widgetDSLabel->setAlignment(AlignLeft|AlignBottom);
	hlyr->addWidget(m_widgetDSLabel);

	m_clearWidgetDSButton = new KexiSmallToolButton(contents, TQString(), "clear_left", "clearWidgetDSButton");
	m_clearWidgetDSButton->setMinimumHeight(m_widgetDSLabel->minimumHeight());
	TQToolTip::add(m_clearWidgetDSButton, i18n("Clear widget's data source"));
	hlyr->addWidget(m_clearWidgetDSButton);
	connect(m_clearWidgetDSButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(clearWidgetDataSourceSelection()));

	m_sourceFieldCombo = new KexiFieldComboBox(contents, "sourceFieldCombo");
	m_widgetDSLabel->setBuddy(m_sourceFieldCombo);
	contentsVlyr->addWidget(m_sourceFieldCombo);

/*	m_dataSourceSeparator = new TQFrame(contents);
	m_dataSourceSeparator->setFrameShape(TQFrame::HLine);
	m_dataSourceSeparator->setFrameShadow(TQFrame::Sunken);
	contentsVlyr->addWidget(m_dataSourceSeparator);*/

	contentsVlyr->addSpacing(8);

	//- Form's Data Source
	hlyr = new TQHBoxLayout(contentsVlyr);
	m_dataSourceLabel = new TQLabel(i18n("Form's data source:"), contents);
	m_dataSourceLabel->setSizePolicy(TQSizePolicy::Minimum, TQSizePolicy::Fixed);
	m_dataSourceLabel->setMargin(2);
	m_dataSourceLabel->setMinimumHeight(IconSize(TDEIcon::Small)+4);
	m_dataSourceLabel->setAlignment(AlignLeft|AlignBottom);
	hlyr->addWidget(m_dataSourceLabel);

	m_gotoButton = new KexiSmallToolButton(contents, TQString(), "goto", "gotoButton");
	m_gotoButton->setMinimumHeight(m_dataSourceLabel->minimumHeight());
	TQToolTip::add(m_gotoButton, i18n("Go to selected form's data source"));
	hlyr->addWidget(m_gotoButton);
	connect(m_gotoButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotGotoSelected()));

	m_clearDSButton = new KexiSmallToolButton(contents, TQString(), "clear_left", "clearDSButton");
	m_clearDSButton->setMinimumHeight(m_dataSourceLabel->minimumHeight());
	TQToolTip::add(m_clearDSButton, i18n("Clear form's data source"));
	hlyr->addWidget(m_clearDSButton);
	connect(m_clearDSButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(clearDataSourceSelection()));

	m_dataSourceCombo = new KexiDataSourceComboBox(contents, "dataSourceCombo");
	m_dataSourceLabel->setBuddy(m_dataSourceCombo);
	contentsVlyr->addWidget(m_dataSourceCombo);

#ifdef KEXI_NO_AUTOFIELD_WIDGET
	m_availableFieldsLabel = 0;
	m_addField = 0;
//	m_fieldListView = 0;
	vlyr->addStretch();
#else
	vlyr->addSpacing(fontMetrics().height());
/*	TQFrame *separator = new TQFrame(this);
	separator->setFrameShape(TQFrame::HLine);
	separator->setFrameShadow(TQFrame::Sunken);
	vlyr->addWidget(separator);*/
/*
	TDEPopupTitle *title = new TDEPopupTitle(this);
	title->setTitle(i18n("Inserting fields"));
	vlyr->addWidget(title);
	vlyr->addSpacing(4);*/


	//2. Inserting fields
	container = new KoProperty::GroupContainer(i18n("Inserting Fields"), this);
	vlyr->addWidget(container, 1);

	//helper info
//! @todo allow to hide such helpers by adding global option
	contents = new TQWidget(container);
	container->setContents(contents);
	contentsVlyr = new TQVBoxLayout(contents);
	hlyr = new TQHBoxLayout(contentsVlyr);
	m_mousePointerLabel = new TQLabel(contents);
	hlyr->addWidget(m_mousePointerLabel);
	m_mousePointerLabel->setPixmap( SmallIcon("mouse_pointer") );
	m_mousePointerLabel->setFixedWidth( m_mousePointerLabel->pixmap() ? m_mousePointerLabel->pixmap()->width() : 0);
	m_availableFieldsDescriptionLabel = new TQLabel(
		i18n("Select fields from the list below and drag them onto a form or click the \"Insert\" button"), contents);
	m_availableFieldsDescriptionLabel->setAlignment( TQt::AlignAuto | TQt::WordBreak );
	hlyr->addWidget(m_availableFieldsDescriptionLabel);

	//Available Fields
	contentsVlyr->addSpacing(4);
	hlyr = new TQHBoxLayout(contentsVlyr);
	m_availableFieldsLabel = new TQLabel(i18n("Available fields:"), contents);
	m_availableFieldsLabel->setSizePolicy(TQSizePolicy::Minimum, TQSizePolicy::Fixed);
	m_availableFieldsLabel->setMargin(2);
	m_availableFieldsLabel->setMinimumHeight(IconSize(TDEIcon::Small));
	hlyr->addWidget(m_availableFieldsLabel);

	m_addField = new KexiSmallToolButton(contents, i18n("Insert selected field into form", "Insert"), 
		"add_field", "addFieldButton");
	m_addField->setMinimumHeight(m_availableFieldsLabel->minimumHeight());
//	m_addField->setTextPosition(TQToolButton::Right);
	m_addField->setFocusPolicy(TQWidget::StrongFocus);
	TQToolTip::add(m_addField, i18n("Insert selected fields into form"));
	hlyr->addWidget(m_addField);
	connect(m_addField, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotInsertSelectedFields()));

	m_fieldListView = new KexiFieldListView(contents, "fieldListView",
		KexiFieldListView::ShowDataTypes | KexiFieldListView::AllowMultiSelection );
	m_fieldListView->setSizePolicy(TQSizePolicy(TQSizePolicy::Preferred, TQSizePolicy::Expanding));
	m_availableFieldsLabel->setBuddy(m_fieldListView);
	contentsVlyr->addWidget(m_fieldListView, 1);
	connect(m_fieldListView, TQT_SIGNAL(selectionChanged()), this, TQT_SLOT(slotFieldListViewSelectionChanged()));
	connect(m_fieldListView, TQT_SIGNAL(fieldDoubleClicked(const TQString&, const TQString&, const TQString&)),
		this, TQT_SLOT(slotFieldDoubleClicked(const TQString&, const TQString&, const TQString&)));
#endif

	vlyr->addStretch(1);

	connect(m_dataSourceCombo, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(slotDataSourceTextChanged(const TQString &)));
	connect(m_dataSourceCombo, TQT_SIGNAL(dataSourceChanged()), this, TQT_SLOT(slotDataSourceChanged()));
	connect(m_sourceFieldCombo, TQT_SIGNAL(selected()), this, TQT_SLOT(slotFieldSelected()));

	clearDataSourceSelection();
	slotFieldListViewSelectionChanged();
}

KexiDataSourcePage::~KexiDataSourcePage()
{
}

void KexiDataSourcePage::setProject(KexiProject *prj)
{
	m_sourceFieldCombo->setProject(prj);
	m_dataSourceCombo->setProject(prj);
}

void KexiDataSourcePage::clearDataSourceSelection(bool alsoClearComboBox)
{
	if (m_insideClearDataSourceSelection)
		return;
	m_insideClearDataSourceSelection = true;
	if (alsoClearComboBox && !m_dataSourceCombo->selectedName().isEmpty())
		m_dataSourceCombo->setDataSource("", "");
//	if (!m_dataSourceCombo->currentText().isEmpty()) {
//		m_dataSourceCombo->setCurrentText("");
//		emit m_dataSourceCombo->dataSourceSelected();
//	}
	m_clearDSButton->setEnabled(false);
	m_gotoButton->setEnabled(false);
#ifndef KEXI_NO_AUTOFIELD_WIDGET
	m_addField->setEnabled(false);
	m_fieldListView->clear();
#endif
	m_insideClearDataSourceSelection = false;
}

void KexiDataSourcePage::clearWidgetDataSourceSelection()
{
	if (!m_sourceFieldCombo->currentText().isEmpty()) {
		m_sourceFieldCombo->setCurrentText("");
		m_sourceFieldCombo->setFieldOrExpression(TQString());
		slotFieldSelected();
	}
	m_clearWidgetDSButton->setEnabled(false);
}

void KexiDataSourcePage::slotGotoSelected()
{
	TQCString mime = m_dataSourceCombo->selectedMimeType().latin1();
	if (mime=="kexi/table" || mime=="kexi/query") {
		if (m_dataSourceCombo->isSelectionValid())
			emit jumpToObjectRequested(mime, m_dataSourceCombo->selectedName().latin1());
	}
}

void KexiDataSourcePage::slotInsertSelectedFields()
{
#ifndef KEXI_NO_AUTOFIELD_WIDGET
	TQStringList selectedFieldNames(m_fieldListView->selectedFieldNames());
	if (selectedFieldNames.isEmpty())
		return;

	emit insertAutoFields(m_fieldListView->schema()->table() ? "kexi/table" : "kexi/query",
		m_fieldListView->schema()->name(), selectedFieldNames);
#endif
}

void KexiDataSourcePage::slotFieldDoubleClicked(const TQString& sourceMimeType, const TQString& sourceName,
	const TQString& fieldName)
{
#ifndef KEXI_NO_AUTOFIELD_WIDGET
	TQStringList selectedFields;
	selectedFields.append(fieldName);
	emit insertAutoFields(sourceMimeType, sourceName, selectedFields);
#endif
}

void KexiDataSourcePage::slotDataSourceTextChanged(const TQString & string)
{
	Q_UNUSED(string);
	const bool enable = m_dataSourceCombo->isSelectionValid(); //!string.isEmpty() && m_dataSourceCombo->selectedName() == string.latin1();
	if (!enable) {
		clearDataSourceSelection( m_dataSourceCombo->selectedName().isEmpty()/*alsoClearComboBox*/ );
	}
	updateSourceFieldWidgetsAvailability();
/*#ifndef KEXI_NO_AUTOFIELD_WIDGET
	m_fieldListView->setEnabled(enable);
//	m_addField->setEnabled(enable);
	m_availableFieldsLabel->setEnabled(enable);
#endif*/
}

void KexiDataSourcePage::slotDataSourceChanged()
{
	if (!m_dataSourceCombo->project())
		return;
	TQCString mime = m_dataSourceCombo->selectedMimeType().latin1();
	bool dataSourceFound = false;
	TQCString name = m_dataSourceCombo->selectedName().latin1();
	if ((mime=="kexi/table" || mime=="kexi/query") && m_dataSourceCombo->isSelectionValid()) {
		KexiDB::TableOrQuerySchema *tableOrQuery = new KexiDB::TableOrQuerySchema(
			m_dataSourceCombo->project()->dbConnection(), name, mime=="kexi/table");
		if (tableOrQuery->table() || tableOrQuery->query()) {
#ifdef KEXI_NO_AUTOFIELD_WIDGET
			m_tableOrQuerySchema = tableOrQuery;
#else
			m_fieldListView->setSchema( tableOrQuery );
#endif
			dataSourceFound = true;
			m_sourceFieldCombo->setTableOrQuery(name, mime=="kexi/table");
		}
		else {
			delete tableOrQuery;
		}
	}
	if (!dataSourceFound) {
		m_sourceFieldCombo->setTableOrQuery("", true);
	}
	//if (m_sourceFieldCombo->hasFocus())
//		m_dataSourceCombo->setFocus();
	m_clearDSButton->setEnabled(dataSourceFound);
	m_gotoButton->setEnabled(dataSourceFound);
	if (dataSourceFound) {
		slotFieldListViewSelectionChanged();
	} else {
#ifndef KEXI_NO_AUTOFIELD_WIDGET
		m_addField->setEnabled(false);
#endif
	}
	updateSourceFieldWidgetsAvailability();
	emit formDataSourceChanged(mime, name);
}

void KexiDataSourcePage::slotFieldSelected()
{
	KexiDB::Field::Type dataType = KexiDB::Field::InvalidType;
#ifdef KEXI_NO_AUTOFIELD_WIDGET
	KexiDB::Field *field = m_tableOrQuerySchema->field( m_sourceFieldCombo->fieldOrExpression() ); //temp
#else
//! @todo this should also work for expressions
	KexiDB::Field *field = m_fieldListView->schema()->field( m_sourceFieldCombo->fieldOrExpression() );
#endif
	if (field)
		dataType = field->type();

	m_clearWidgetDSButton->setEnabled( !m_sourceFieldCombo->fieldOrExpression().isEmpty() );

	emit dataSourceFieldOrExpressionChanged(
		m_sourceFieldCombo->fieldOrExpression(),
		m_sourceFieldCombo->fieldOrExpressionCaption(),
		dataType
	);
}

void KexiDataSourcePage::setDataSource(const TQCString& mimeType, const TQCString& name)
{
	m_dataSourceCombo->setDataSource(mimeType, name);
}

void KexiDataSourcePage::assignPropertySet(KoProperty::Set* propertySet)
{
	TQCString objectName;
	if (propertySet && propertySet->contains("name"))
		objectName = (*propertySet)["name"].value().toCString();
	if (!objectName.isEmpty() && objectName == m_currentObjectName)
		return; //the same object
	m_currentObjectName = objectName;

	TQCString objectClassName;
	if (propertySet && propertySet->contains("this:className"))
		objectClassName = (*propertySet)["this:className"].value().toCString();
/*moved	if (propertySet) {
		TQCString iconName;
		TQString objectClassString;
		if (propertySet->contains("this:iconName"))
			iconName = (*propertySet)["this:iconName"].value().toCString();
		if (propertySet->contains("this:classString"))
			objectClassString = (*propertySet)["this:classString"].value().toString();
		m_objectInfoLabel->setObjectName(objectName);
		m_objectInfoLabel->setObjectClassIcon(iconName);
		m_objectInfoLabel->setObjectClassName(objectClassString);
		if (propertySet->contains("this:className"))
			objectClassName = (*propertySet)["this:className"].value().toCString();
	}*/
	KexiPropertyEditorView::updateInfoLabelForPropertySet(
		m_objectInfoLabel, propertySet);

	const bool isForm = objectClassName=="KexiDBForm";
//	kdDebug() << "objectClassName=" << objectClassName << endl;
//	{
/*		//this is top level form's surface: data source means table or query
		TQCString dataSourceMimeType, dataSource;
		if (buffer->hasProperty("dataSourceMimeType"))
			dataSourceMimeType = (*buffer)["dataSourceMimeType"].value().toCString();
		if (buffer->hasProperty("dataSource"))
			dataSource = (*buffer)["dataSource"].value().toCString();
		m_dataSourceCombo->setDataSource(dataSourceMimeType, dataSource);*/
//	}
//	else {

	const bool multipleSelection = objectClassName=="special:multiple";
	const bool hasDataSourceProperty = propertySet && propertySet->contains("dataSource") && !multipleSelection;

	if (!isForm) {
		//this is a widget
		TQCString dataSource;
		if (hasDataSourceProperty) {
			if (propertySet)
				dataSource = (*propertySet)["dataSource"].value().toCString();
			m_noDataSourceAvailableLabel->hide();
			m_sourceFieldCombo->setFieldOrExpression(dataSource.data());
			m_sourceFieldCombo->setEnabled(true);
			m_clearWidgetDSButton->setEnabled(!m_sourceFieldCombo->currentText().isEmpty());
			m_widgetDSLabel->show();
			m_clearWidgetDSButton->show();
			m_sourceFieldCombo->show();
//			m_dataSourceSeparator->hide();
			updateSourceFieldWidgetsAvailability();
		}
	}

	if (isForm) {
		m_noDataSourceAvailableLabel->hide();
//		m_dataSourceSeparator->hide();
	}
	else if (!hasDataSourceProperty) {
		if (multipleSelection)
			m_noDataSourceAvailableLabel->setText(m_noDataSourceAvailableMultiText);
		else
			m_noDataSourceAvailableLabel->setText(m_noDataSourceAvailableSingleText);
		m_noDataSourceAvailableLabel->show();
//		m_dataSourceSeparator->show();
		//make 'No data source could be assigned' label's height the same as the 'source field' combo+label
		m_noDataSourceAvailableLabel->setMinimumHeight(m_widgetDSLabel->height()
			+ m_sourceFieldCombo->height()/*-m_dataSourceSeparator->height()*/);
		m_sourceFieldCombo->setCurrentText("");
	}

	if (isForm || !hasDataSourceProperty) {
		//no source field can be set
		m_widgetDSLabel->hide();
		m_clearWidgetDSButton->hide();
		m_sourceFieldCombo->hide();
	}
}

void KexiDataSourcePage::slotFieldListViewSelectionChanged()
{
#ifndef KEXI_NO_AUTOFIELD_WIDGET
	//update "add field" button's state
	for (TQListViewItemIterator it(m_fieldListView); it.current(); ++it) {
		if (it.current()->isSelected()) {
			m_addField->setEnabled(true);
			return;
		}
	}
	m_addField->setEnabled(false);
#endif
}

void KexiDataSourcePage::updateSourceFieldWidgetsAvailability()
{
	const bool hasDataSource = m_dataSourceCombo->isSelectionValid(); //!m_dataSourceCombo->selectedName().isEmpty();
	m_sourceFieldCombo->setEnabled( hasDataSource );
	m_widgetDSLabel->setEnabled( hasDataSource );
#ifndef KEXI_NO_AUTOFIELD_WIDGET
	m_fieldListView->setEnabled( hasDataSource );
	m_availableFieldsLabel->setEnabled( hasDataSource );
	m_mousePointerLabel->setEnabled( hasDataSource );
	m_availableFieldsDescriptionLabel->setEnabled( hasDataSource );
#endif
}

#include "kexidatasourcepage.moc"