summaryrefslogtreecommitdiffstats
path: root/kexi/plugins/tables/kexilookupcolumnpage.cpp
blob: 9df9279459154b0204fedff0109fbc9306ae1f4c (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
/* This file is part of the KDE project
   Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>

   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 "kexilookupcolumnpage.h"

#include <qlabel.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qheader.h>

#include <kiconloader.h>
#include <klocale.h>
#include <ktoolbarbutton.h>
#include <kdebug.h>
#include <kpopupmenu.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 <koproperty/property.h>
#include <koproperty/utils.h>

QString mimeTypeToType(const QString& mimeType)
{
	if (mimeType=="kexi/table")
		return "table";
	else if (mimeType=="kexi/query")
		return "query";
//! @todo more types
	return mimeType;
}

QString typeToMimeType(const QString& type)
{
	if (type=="table")
		return "kexi/table";
	else if (type=="query")
		return "kexi/query";
//! @todo more types
	return type;
}

//----------------------------------------------

//! @internal
class KexiLookupColumnPage::Private
{
	public:
		Private()
		 : currentFieldUid(-1)
		 , insideClearRowSourceSelection(false)
		 , propertySetEnabled(true)
		{
		}
		~Private()
		{
		}

		bool hasPropertySet() const {
			return propertySet;
		}

		void setPropertySet(KoProperty::Set* aPropertySet) {
			propertySet = aPropertySet;
		}

		QVariant propertyValue(const QCString& propertyName) const {
			return propertySet ? propertySet->property(propertyName).value() : QVariant();
		}

		void changeProperty(const QCString &property, const QVariant &value)
		{
			if (!propertySetEnabled)
				return;
			propertySet->changeProperty(property, value);
		}

		void updateInfoLabelForPropertySet(const QString& textToDisplayForNullSet) {
			KexiPropertyEditorView::updateInfoLabelForPropertySet(
				objectInfoLabel, propertySet, textToDisplayForNullSet);
		}

		KexiDataSourceComboBox *rowSourceCombo;
		KexiFieldComboBox *boundColumnCombo, *visibleColumnCombo;
		KexiObjectInfoLabel *objectInfoLabel;
		QLabel *rowSourceLabel, *boundColumnLabel, *visibleColumnLabel;
		QToolButton *clearRowSourceButton, *gotoRowSourceButton, *clearBoundColumnButton,
			*clearVisibleColumnButton;
		//! Used only in assignPropertySet() to check whether we already have the set assigned
		int currentFieldUid;

		bool insideClearRowSourceSelection : 1;
		//! True is changeProperty() works. Used to block updating properties when within assignPropertySet().
		bool propertySetEnabled : 1;

	private:
		//! A property set that is displayed on the page. 
		//! The set is also updated after any change in this page's data.
		QGuardedPtr<KoProperty::Set> propertySet;
};

//----------------------------------------------

KexiLookupColumnPage::KexiLookupColumnPage(QWidget *parent)
 : QWidget(parent)
 , d(new Private())
{
	setName("KexiLookupColumnPage");

	QVBoxLayout *vlyr = new QVBoxLayout(this);
	d->objectInfoLabel = new KexiObjectInfoLabel(this, "KexiObjectInfoLabel");
	vlyr->addWidget(d->objectInfoLabel);

//todo	d->noDataSourceAvailableSingleText = i18n("No data source could be assigned for this widget.");
//todo	d->noDataSourceAvailableMultiText = i18n("No data source could be assigned for multiple widgets.");

	//-Row Source
	QWidget *contents = new QWidget(this);
	vlyr->addWidget(contents);
	QVBoxLayout *contentsVlyr = new QVBoxLayout(contents);

	QHBoxLayout *hlyr = new QHBoxLayout(contentsVlyr);
	d->rowSourceLabel = new QLabel(i18n("Row source:"), contents);
	d->rowSourceLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
	d->rowSourceLabel->setMargin(2);
	d->rowSourceLabel->setMinimumHeight(IconSize(KIcon::Small)+4);
	d->rowSourceLabel->setAlignment(Qt::AlignLeft|Qt::AlignBottom);
	hlyr->addWidget(d->rowSourceLabel);

	d->gotoRowSourceButton = new KexiSmallToolButton(contents, QString::null, "goto", "gotoRowSourceButton");
	d->gotoRowSourceButton->setMinimumHeight(d->rowSourceLabel->minimumHeight());
	QToolTip::add(d->gotoRowSourceButton, i18n("Go to selected row source"));
	hlyr->addWidget(d->gotoRowSourceButton);
	connect(d->gotoRowSourceButton, SIGNAL(clicked()), this, SLOT(slotGotoSelectedRowSource()));

	d->clearRowSourceButton = new KexiSmallToolButton(contents, QString::null,
		"clear_left", "clearRowSourceButton");
	d->clearRowSourceButton->setMinimumHeight(d->rowSourceLabel->minimumHeight());
	QToolTip::add(d->clearRowSourceButton, i18n("Clear row source"));
	hlyr->addWidget(d->clearRowSourceButton);
	connect(d->clearRowSourceButton, SIGNAL(clicked()), this, SLOT(clearRowSourceSelection()));

	d->rowSourceCombo = new KexiDataSourceComboBox(contents, "rowSourceCombo");
	d->rowSourceLabel->setBuddy(d->rowSourceCombo);
	contentsVlyr->addWidget(d->rowSourceCombo);

	contentsVlyr->addSpacing(8);

	//- Bound Column
	hlyr = new QHBoxLayout(contentsVlyr);
	d->boundColumnLabel = new QLabel(i18n("Bound column:"), contents);
	d->boundColumnLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
	d->boundColumnLabel->setMargin(2);
	d->boundColumnLabel->setMinimumHeight(IconSize(KIcon::Small)+4);
	d->boundColumnLabel->setAlignment(Qt::AlignLeft|Qt::AlignBottom);
	hlyr->addWidget(d->boundColumnLabel);

	d->clearBoundColumnButton = new KexiSmallToolButton(contents, QString::null,
		"clear_left", "clearBoundColumnButton");
	d->clearBoundColumnButton->setMinimumHeight(d->boundColumnLabel->minimumHeight());
	QToolTip::add(d->clearBoundColumnButton, i18n("Clear bound column"));
	hlyr->addWidget(d->clearBoundColumnButton);
	connect(d->clearBoundColumnButton, SIGNAL(clicked()), this, SLOT(clearBoundColumnSelection()));

	d->boundColumnCombo = new KexiFieldComboBox(contents, "boundColumnCombo");
	d->boundColumnLabel->setBuddy(d->boundColumnCombo);
	contentsVlyr->addWidget(d->boundColumnCombo);

	contentsVlyr->addSpacing(8);

	//- Visible Column
	hlyr = new QHBoxLayout(contentsVlyr);
	d->visibleColumnLabel = new QLabel(i18n("Visible column:"), contents);
	d->visibleColumnLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
	d->visibleColumnLabel->setMargin(2);
	d->visibleColumnLabel->setMinimumHeight(IconSize(KIcon::Small)+4);
	d->visibleColumnLabel->setAlignment(Qt::AlignLeft|Qt::AlignBottom);
	hlyr->addWidget(d->visibleColumnLabel);

	d->clearVisibleColumnButton = new KexiSmallToolButton(contents, QString::null,
		"clear_left", "clearVisibleColumnButton");
	d->clearVisibleColumnButton->setMinimumHeight(d->visibleColumnLabel->minimumHeight());
	QToolTip::add(d->clearVisibleColumnButton, i18n("Clear visible column"));
	hlyr->addWidget(d->clearVisibleColumnButton);
	connect(d->clearVisibleColumnButton, SIGNAL(clicked()), this, SLOT(clearVisibleColumnSelection()));

	d->visibleColumnCombo = new KexiFieldComboBox(contents, "visibleColumnCombo");
	d->visibleColumnLabel->setBuddy(d->visibleColumnCombo);
	contentsVlyr->addWidget(d->visibleColumnCombo);

	vlyr->addStretch(1);

	connect(d->rowSourceCombo, SIGNAL(textChanged(const QString &)), 
		this, SLOT(slotRowSourceTextChanged(const QString &)));
	connect(d->rowSourceCombo, SIGNAL(dataSourceChanged()), this, SLOT(slotRowSourceChanged()));
	connect(d->boundColumnCombo, SIGNAL(selected()), this, SLOT(slotBoundColumnSelected()));
	connect(d->visibleColumnCombo, SIGNAL(selected()), this, SLOT(slotVisibleColumnSelected()));

	clearBoundColumnSelection();
	clearVisibleColumnSelection();
}

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

void KexiLookupColumnPage::setProject(KexiProject *prj)
{
	d->rowSourceCombo->setProject(prj,
		true/*showTables*/, true/*showQueries*/
	);
	d->boundColumnCombo->setProject(prj);
	d->visibleColumnCombo->setProject(prj);
}

void KexiLookupColumnPage::assignPropertySet(KoProperty::Set* propertySet)
{
	if (!d->hasPropertySet() && !propertySet)
		return;
	if (propertySet && d->currentFieldUid == (*propertySet)["uid"].value().toInt())
		return; //already assigned

	d->propertySetEnabled = false;
	d->setPropertySet( propertySet );
	d->updateInfoLabelForPropertySet( i18n("No field selected") );
	
	const bool hasRowSource = d->hasPropertySet() && !d->propertyValue("rowSourceType").isNull()
		&& !d->propertyValue("rowSource").isNull();

	QString rowSource, rowSourceType;
	if (hasRowSource) {
		rowSourceType = typeToMimeType( d->propertyValue("rowSourceType").toString() );
		rowSource = d->propertyValue("rowSource").toString();
	}
	d->rowSourceCombo->setDataSource( rowSourceType, rowSource );
	d->rowSourceLabel->setEnabled( d->hasPropertySet() );
	d->rowSourceCombo->setEnabled( d->hasPropertySet() );
	if (!d->hasPropertySet())
		d->clearRowSourceButton->setEnabled( false );

	int boundColumn = -1, visibleColumn = -1;
	if (d->rowSourceCombo->isSelectionValid()) {
		boundColumn = d->propertyValue("boundColumn").toInt();
		visibleColumn = d->propertyValue("visibleColumn").toInt();
	}
	d->boundColumnCombo->setFieldOrExpression(boundColumn);
	d->visibleColumnCombo->setFieldOrExpression(visibleColumn);
	updateBoundColumnWidgetsAvailability();
	d->propertySetEnabled = true;
}

void KexiLookupColumnPage::clearBoundColumnSelection()
{
	d->boundColumnCombo->setCurrentText("");
	d->boundColumnCombo->setFieldOrExpression(QString::null);
	slotBoundColumnSelected();
	d->clearBoundColumnButton->setEnabled(false);
}

void KexiLookupColumnPage::slotBoundColumnSelected()
{
//	KexiDB::Field::Type dataType = KexiDB::Field::InvalidType;
//! @todo this should also work for expressions
/*disabled	KexiDB::Field *field = d->fieldListView->schema()->field( d->boundColumnCombo->fieldOrExpression() );
	if (field)
		dataType = field->type();
*/
	d->clearBoundColumnButton->setEnabled( !d->boundColumnCombo->fieldOrExpression().isEmpty() );
	if (!d->boundColumnCombo->fieldOrExpression().isEmpty()) {
		kdDebug() << endl;
	}

	// update property set
	if (d->hasPropertySet()) {
		d->changeProperty("boundColumn", d->boundColumnCombo->indexOfField());
	}
/*
	emit boundColumnChanged(
		d->boundColumnCombo->fieldOrExpression(),
		d->boundColumnCombo->fieldOrExpressionCaption(),
		dataType
	);*/
}

void KexiLookupColumnPage::clearVisibleColumnSelection()
{
	d->visibleColumnCombo->setCurrentText("");
	d->visibleColumnCombo->setFieldOrExpression(QString::null);
	slotVisibleColumnSelected();
	d->clearVisibleColumnButton->setEnabled(false);
}

void KexiLookupColumnPage::slotVisibleColumnSelected()
{
//	KexiDB::Field::Type dataType = KexiDB::Field::InvalidType;
//! @todo this should also work for expressions
	d->clearVisibleColumnButton->setEnabled( !d->visibleColumnCombo->fieldOrExpression().isEmpty() );

	// update property set
	if (d->hasPropertySet()) {
//! @todo support expression in special "visibleExpression"
		d->changeProperty("visibleColumn", d->visibleColumnCombo->indexOfField());
	}
}

void KexiLookupColumnPage::slotRowSourceChanged()
{
	if (!d->rowSourceCombo->project())
		return;
	QString mime = d->rowSourceCombo->selectedMimeType();
	bool rowSourceFound = false;
	QString name = d->rowSourceCombo->selectedName();
	if ((mime=="kexi/table" || mime=="kexi/query") && d->rowSourceCombo->isSelectionValid()) {
		KexiDB::TableOrQuerySchema *tableOrQuery = new KexiDB::TableOrQuerySchema(
			d->rowSourceCombo->project()->dbConnection(), name.latin1(), mime=="kexi/table");
		if (tableOrQuery->table() || tableOrQuery->query()) {
//disabled			d->fieldListView->setSchema( tableOrQuery );
/*tmp*/			delete tableOrQuery;
			rowSourceFound = true;
			d->boundColumnCombo->setTableOrQuery(name, mime=="kexi/table");
			d->visibleColumnCombo->setTableOrQuery(name, mime=="kexi/table");
		}
		else {
			delete tableOrQuery;
		}
	}
	if (!rowSourceFound) {
		d->boundColumnCombo->setTableOrQuery("", true);
		d->visibleColumnCombo->setTableOrQuery("", true);
	}
	clearBoundColumnSelection();
	clearVisibleColumnSelection();
	d->clearRowSourceButton->setEnabled(rowSourceFound);
	d->gotoRowSourceButton->setEnabled(rowSourceFound);
/* disabled
	if (dataSourceFound) {
		slotFieldListViewSelectionChanged();
	} else {
		d->addField->setEnabled(false);
	}*/
	updateBoundColumnWidgetsAvailability();

	//update property set
	if (d->hasPropertySet()) {
		d->changeProperty("rowSourceType", mimeTypeToType(mime));
		d->changeProperty("rowSource", name);
	}

//disabled	emit formDataSourceChanged(mime, name);
//! @todo update d->propertySet ^^
}

void KexiLookupColumnPage::slotRowSourceTextChanged(const QString & string)
{
	Q_UNUSED(string);
	const bool enable = d->rowSourceCombo->isSelectionValid();
	if (enable) {
		updateBoundColumnWidgetsAvailability();
	}
	else {
		clearRowSourceSelection( d->rowSourceCombo->selectedName().isEmpty()/*alsoClearComboBox*/ );
	}
}

void KexiLookupColumnPage::clearRowSourceSelection(bool alsoClearComboBox)
{
	if (d->insideClearRowSourceSelection)
		return;
	d->insideClearRowSourceSelection = true;
	if (alsoClearComboBox && !d->rowSourceCombo->selectedName().isEmpty())
		d->rowSourceCombo->setDataSource("", "");
	d->clearRowSourceButton->setEnabled(false);
	d->gotoRowSourceButton->setEnabled(false);
	d->insideClearRowSourceSelection = false;
}

void KexiLookupColumnPage::slotGotoSelectedRowSource()
{
	QString mime = d->rowSourceCombo->selectedMimeType();
	if (mime=="kexi/table" || mime=="kexi/query") {
		if (d->rowSourceCombo->isSelectionValid())
			emit jumpToObjectRequested(mime.latin1(), d->rowSourceCombo->selectedName().latin1());
	}
}

void KexiLookupColumnPage::updateBoundColumnWidgetsAvailability()
{
	const bool hasRowSource = d->rowSourceCombo->isSelectionValid();
	d->boundColumnCombo->setEnabled( hasRowSource );
	d->boundColumnLabel->setEnabled( hasRowSource );
	d->clearBoundColumnButton->setEnabled( hasRowSource && !d->boundColumnCombo->fieldOrExpression().isEmpty() );
	d->visibleColumnCombo->setEnabled( hasRowSource );
	d->visibleColumnLabel->setEnabled( hasRowSource );
	d->clearVisibleColumnButton->setEnabled( hasRowSource && !d->visibleColumnCombo->fieldOrExpression().isEmpty() );
}

#include "kexilookupcolumnpage.moc"