summaryrefslogtreecommitdiffstats
path: root/kexi/kexidb/lookupfieldschema.cpp
blob: 05728d00bb1ff6f8e0bd7adf572e04424c1dc912 (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
/* This file is part of the KDE project
   Copyright (C) 2006-2007 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 "lookupfieldschema.h"
#include "utils.h"

#include <tqdom.h>
#include <tqvariant.h>
#include <kdebug.h>

using namespace KexiDB;


LookupFieldSchema::RowSource::RowSource()
: m_type(NoType)
, m_values(0)
{
}

LookupFieldSchema::RowSource::~RowSource()
{
	delete m_values;
}

void LookupFieldSchema::RowSource::setName(const TQString& name)
{
	m_name = name;
	if (m_values)
		m_values->clear();
}

TQString LookupFieldSchema::RowSource::typeName() const
{
	switch (m_type) {
	case Table: return "table";
	case Query: return "query";
	case SQLStatement: return "sql";
	case ValueList: return "valuelist";
	case FieldList: return "fieldlist";
	default:;
	}
	return TQString();
}

void LookupFieldSchema::RowSource::setTypeByName( const TQString& typeName )
{
	if (typeName=="table")
		setType( Table );
	else if (typeName=="query")
		setType( Query );
	else if (typeName=="sql")
		setType( SQLStatement );
	else if (typeName=="valuelist")
		setType( ValueList );
	else if (typeName=="fieldlist")
		setType( FieldList );
	else
		setType( NoType );
}

TQStringList LookupFieldSchema::RowSource::values() const
{
	return m_values ? *m_values : TQStringList();
}

void LookupFieldSchema::RowSource::setValues(const TQStringList& values)
{
	m_name = TQString();
	if (m_values)
		*m_values = values;
	else
		m_values = new TQStringList(values);
}

TQString LookupFieldSchema::RowSource::debugString() const
{
	return TQString("rowSourceType:'%1' rowSourceName:'%2' rowSourceValues:'%3'\n")
		.arg(typeName()).arg(name()).arg(m_values ? m_values->join("|") : TQString());
}

void LookupFieldSchema::RowSource::debug() const
{
	KexiDBDbg << debugString() << endl;
}

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

LookupFieldSchema::LookupFieldSchema()
 : m_boundColumn(-1)
 , m_maximumListRows(KEXIDB_LOOKUP_FIELD_DEFAULT_LIST_ROWS)
 , m_displayWidget(KEXIDB_LOOKUP_FIELD_DEFAULT_DISPLAY_WIDGET)
 , m_columnHeadersVisible(KEXIDB_LOOKUP_FIELD_DEFAULT_HEADERS_VISIBLE)
 , m_limitToList(KEXIDB_LOOKUP_FIELD_DEFAULT_LIMIT_TO_LIST)
{
}

LookupFieldSchema::~LookupFieldSchema()
{
}

void LookupFieldSchema::setMaximumListRows(uint rows)
{
	if (rows==0)
		m_maximumListRows = KEXIDB_LOOKUP_FIELD_DEFAULT_LIST_ROWS;
	else if (rows>KEXIDB_LOOKUP_FIELD_MAX_LIST_ROWS)
		m_maximumListRows = KEXIDB_LOOKUP_FIELD_MAX_LIST_ROWS;
	else
		m_maximumListRows = rows;
}

TQString LookupFieldSchema::debugString() const
{
	TQString columnWidthsStr;
	foreach (TQValueList<int>::ConstIterator, it, m_columnWidths) {
		if (!columnWidthsStr.isEmpty())
			columnWidthsStr.append(";");
		columnWidthsStr.append( TQString::number(*it) );
	}

	TQString visibleColumnsString;
	foreach (TQValueList<uint>::ConstIterator, it, m_visibleColumns) {
		if (!visibleColumnsString.isEmpty())
			visibleColumnsString.append(";");
		visibleColumnsString.append(TQString::number(*it));
	}

	return TQString("LookupFieldSchema( %1\n"
		" boundColumn:%2 visibleColumns:%3 maximumListRows:%4 displayWidget:%5\n"
		" columnHeadersVisible:%6 limitToList:%7\n"
		" columnWidths:%8 )")
		.arg(m_rowSource.debugString())
		.arg(m_boundColumn).arg(visibleColumnsString).arg(m_maximumListRows)
		.arg( m_displayWidget==ComboBox ? "ComboBox" : "ListBox")
		.arg(m_columnHeadersVisible).arg(m_limitToList)
		.arg(columnWidthsStr);
}

void LookupFieldSchema::debug() const
{
	KexiDBDbg << debugString() << endl;
}

/* static */
LookupFieldSchema *LookupFieldSchema::loadFromDom(const TQDomElement& lookupEl)
{
	LookupFieldSchema *lookupFieldSchema = new LookupFieldSchema();
	for (TQDomNode node = lookupEl.firstChild(); !node.isNull(); node = node.nextSibling()) {
		TQDomElement el = node.toElement();
		TQString name( el.tagName() );
		if (name=="row-source") {
			/*<row-source>
				empty
				| <type>table|query|sql|valuelist|fieldlist</type>  #required because there can be table and query with the same name
									"fieldlist" (basically a list of column names of a table/query,
											  "Field List" as in MSA)
				<name>string</name> #table/query name, etc. or KEXISQL SELECT TQUERY
				<values><value>...</value> #for "valuelist" type
					<value>...</value>
				</values>
			 </row-source> */
			for (el = el.firstChild().toElement(); !el.isNull(); el=el.nextSibling().toElement()) {
				if (el.tagName()=="type")
					lookupFieldSchema->rowSource().setTypeByName( el.text() );
				else if (el.tagName()=="name")
					lookupFieldSchema->rowSource().setName( el.text() );
//! @todo handle fieldlist (retrieve from external table or so?), use lookupFieldSchema.rowSource().setValues()
			}
		}
		else if (name=="bound-column") {
			/* <bound-column>
			    <number>number</number> #in later implementation there can be more columns
			   </bound-column> */
			const TQVariant val = KexiDB::loadPropertyValueFromDom( el.firstChild() );
			if (val.type()==TQVariant::Int)
				lookupFieldSchema->setBoundColumn( val.toInt() );
		}
		else if (name=="visible-column") {
			/* <visible-column> #a column that has to be visible in the combo box
				<number>number 1</number>
				<number>number 2</number>
				[..]
			   </visible-column> */
			TQValueList<uint> list;
			for (TQDomNode childNode = el.firstChild(); !childNode.isNull(); childNode = childNode.nextSibling()) {
				const TQVariant val = KexiDB::loadPropertyValueFromDom( childNode );
				if (val.type()==TQVariant::Int)
					list.append( val.toUInt() );
			}
			lookupFieldSchema->setVisibleColumns( list );
		}
		else if (name=="column-widths") {
			/* <column-widths> #column widths, -1 means 'default'
			    <number>int</number>
			    ...
			    <number>int</number>
			   </column-widths> */
			TQVariant val;
			TQValueList<int> columnWidths;
			for (el = el.firstChild().toElement(); !el.isNull(); el=el.nextSibling().toElement()) {
				TQVariant val = KexiDB::loadPropertyValueFromDom( el );
				if (val.type()==TQVariant::Int)
					columnWidths.append(val.toInt());
			}
			lookupFieldSchema->setColumnWidths( columnWidths );
		}
		else if (name=="show-column-headers") {
			/* <show-column-headers>
			    <bool>true/false</bool>
			   </show-column-headers> */
			const TQVariant val = KexiDB::loadPropertyValueFromDom( el.firstChild() );
			if (val.type()==TQVariant::Bool)
				lookupFieldSchema->setColumnHeadersVisible( val.toBool() );
		}
		else if (name=="list-rows") {
			/* <list-rows>
			    <number>1..100</number>
			   </list-rows> */
			const TQVariant val = KexiDB::loadPropertyValueFromDom( el.firstChild() );
			if (val.type()==TQVariant::Int)
				lookupFieldSchema->setMaximumListRows( val.toUInt() );
		}
		else if (name=="limit-to-list") {
			/* <limit-to-list>
			    <bool>true/false</bool>
			   </limit-to-list> */
			const TQVariant val = KexiDB::loadPropertyValueFromDom( el.firstChild() );
			if (val.type()==TQVariant::Bool)
				lookupFieldSchema->setLimitToList( val.toBool() );
		}
		else if (name=="display-widget") {
			if (el.text()=="combobox")
				lookupFieldSchema->setDisplayWidget( LookupFieldSchema::ComboBox );
			else if (el.text()=="listbox")
				lookupFieldSchema->setDisplayWidget( LookupFieldSchema::ListBox );
		}
	}
	return lookupFieldSchema;
}

/* static */
void LookupFieldSchema::saveToDom(LookupFieldSchema& lookupSchema, TQDomDocument& doc, TQDomElement& parentEl)
{
	TQDomElement lookupColumnEl, rowSourceEl, rowSourceTypeEl, nameEl;
	if (!lookupSchema.rowSource().name().isEmpty()) {
		lookupColumnEl = doc.createElement("lookup-column");
		parentEl.appendChild( lookupColumnEl );

		rowSourceEl = doc.createElement("row-source");
		lookupColumnEl.appendChild( rowSourceEl );

		rowSourceTypeEl = doc.createElement("type");
		rowSourceEl.appendChild( rowSourceTypeEl );
		rowSourceTypeEl.appendChild( doc.createTextNode(lookupSchema.rowSource().typeName()) ); //can be empty

		nameEl = doc.createElement("name");
		rowSourceEl.appendChild( nameEl );
		nameEl.appendChild( doc.createTextNode(lookupSchema.rowSource().name()) );
	}

	const TQStringList& values( lookupSchema.rowSource().values() );
	if (!values.isEmpty()) {
		TQDomElement valuesEl( doc.createElement("values") );
		rowSourceEl.appendChild( valuesEl );
		for (TQStringList::ConstIterator it = values.constBegin(); it!=values.constEnd(); ++it) {
			TQDomElement valueEl( doc.createElement("value") );
			valuesEl.appendChild( valueEl );
			valueEl.appendChild( doc.createTextNode(*it) );
		}
	}

	if (lookupSchema.boundColumn()>=0)
		KexiDB::saveNumberElementToDom(doc, lookupColumnEl, "bound-column", lookupSchema.boundColumn());

	TQValueList<uint> visibleColumns(lookupSchema.visibleColumns());
	if (!visibleColumns.isEmpty()) {
		TQDomElement visibleColumnEl( doc.createElement("visible-column") );
		lookupColumnEl.appendChild( visibleColumnEl );
		foreach (TQValueList<uint>::ConstIterator, it, visibleColumns) {
			TQDomElement numberEl( doc.createElement("number") );
			visibleColumnEl.appendChild( numberEl );
			numberEl.appendChild( doc.createTextNode( TQString::number(*it) ) );
		}
	}

	const TQValueList<int> columnWidths(lookupSchema.columnWidths());
	if (!columnWidths.isEmpty()) {
		TQDomElement columnWidthsEl( doc.createElement("column-widths") );
		lookupColumnEl.appendChild( columnWidthsEl );
		for (TQValueList<int>::ConstIterator it = columnWidths.constBegin(); it!=columnWidths.constEnd(); ++it) {
			TQDomElement columnWidthEl( doc.createElement("number") );
			columnWidthsEl.appendChild( columnWidthEl );
			columnWidthEl.appendChild( doc.createTextNode( TQString::number(*it) ) );
		}
	}

	if (lookupSchema.columnHeadersVisible()!=KEXIDB_LOOKUP_FIELD_DEFAULT_HEADERS_VISIBLE)
		KexiDB::saveBooleanElementToDom(doc, lookupColumnEl, "show-column-headers", lookupSchema.columnHeadersVisible());
	if (lookupSchema.maximumListRows()!=KEXIDB_LOOKUP_FIELD_DEFAULT_LIST_ROWS)
		KexiDB::saveNumberElementToDom(doc, lookupColumnEl, "list-rows", lookupSchema.maximumListRows());
	if (lookupSchema.limitToList()!=KEXIDB_LOOKUP_FIELD_DEFAULT_LIMIT_TO_LIST)
		KexiDB::saveBooleanElementToDom(doc, lookupColumnEl, "limit-to-list", lookupSchema.limitToList());
	
	if (lookupSchema.displayWidget()!=KEXIDB_LOOKUP_FIELD_DEFAULT_DISPLAY_WIDGET) {
		TQDomElement displayWidgetEl( doc.createElement("display-widget") );
		lookupColumnEl.appendChild( displayWidgetEl );
		displayWidgetEl.appendChild( 
			doc.createTextNode( (lookupSchema.displayWidget()==ListBox) ? "listbox" : "combobox" ) );
	}
}

//static
bool LookupFieldSchema::setProperty( 
	LookupFieldSchema& lookup, const TQCString& propertyName, const TQVariant& value )
{
	bool ok;
	if ("rowSource" == propertyName || "rowSourceType" == propertyName || "rowSourceValues" == propertyName) {
		LookupFieldSchema::RowSource rowSource( lookup.rowSource() );
		if ("rowSource" == propertyName)
			rowSource.setName(value.toString());
		else if ("rowSourceType" == propertyName)
			rowSource.setTypeByName(value.toString());
		else if ("rowSourceValues" == propertyName)
			rowSource.setValues(value.toStringList());
		lookup.setRowSource(rowSource);
	}
	else if ("boundColumn" == propertyName ) {
		const int ival = value.toInt(&ok);
		if (!ok)
			return false;
		lookup.setBoundColumn( ival );
	}
	else if ("visibleColumn" == propertyName ) {
		TQValueList<TQVariant> variantList;
		if (value.type()==TQVariant::Int) {
//! @todo Remove this case: it's for backward compatibility with Kexi's 1.1.2 table designer GUI
//!       supporting only single lookup column.
			variantList.append( value.toInt() );
		}
		else {
			variantList = value.toList();
		}
		TQValueList<uint> visibleColumns;
		foreach (TQValueList<TQVariant>::ConstIterator, it, variantList) {
			const uint ival = (*it).toUInt(&ok);
			if (!ok)
				return false;
			visibleColumns.append( ival );
		}
		lookup.setVisibleColumns( visibleColumns );
	}
	else if ("columnWidths" == propertyName ) {
		TQValueList<TQVariant> variantList( value.toList() );
		TQValueList<int> widths;
		foreach (TQValueList<TQVariant>::ConstIterator, it, variantList) {
			const uint ival = (*it).toInt(&ok);
			if (!ok)
				return false;
			widths.append( ival );
		}
		lookup.setColumnWidths( widths );
	}
	else if ("showColumnHeaders" == propertyName ) {
		lookup.setColumnHeadersVisible( value.toBool() );
	}
	else if ("listRows" == propertyName ) {
		lookup.setMaximumListRows( value.toBool() );
	}
	else if ("limitToList" == propertyName ) {
		lookup.setLimitToList( value.toBool() );
	}
	else if ("displayWidget" == propertyName ) {
		const uint ival = value.toUInt(&ok);
		if (!ok || ival > LookupFieldSchema::ListBox)
			return false;
		lookup.setDisplayWidget((LookupFieldSchema::DisplayWidget)ival);
	}
	return true;
}