summaryrefslogtreecommitdiffstats
path: root/kexi/widget/tableview/kexidataawarepropertyset.cpp
blob: 2219cf97d1251626d55882f12c2b5070ce9f1292 (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
/* This file is part of the KDE project
   Copyright (C) 2004-2005 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 "kexidataawarepropertyset.h"
#include "kexitableviewdata.h"
#include "kexidataawareobjectiface.h"

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

#define MAX_FIELDS 1009 //nice prime number (default prop. set vector size)

KexiDataAwarePropertySet::KexiDataAwarePropertySet(KexiViewBase *view,
	KexiDataAwareObjectInterface* dataObject)
 : TQObject( view, TQCString(view->name())+"KexiDataAwarePropertySet" )
 , m_view(view)
 , m_dataObject(dataObject)
 , m_row(-99)
{
	m_sets.setAutoDelete(true);

//	connect(m_dataObject, TQ_SIGNAL(dataSet(KexiTableViewData*)),
//		this, TQ_SLOT(slotDataSet(KexiTableViewData*)));
	m_dataObject->connectDataSetSignal(this, TQ_SLOT(slotDataSet(KexiTableViewData*)));
//	connect(m_dataObject, TQ_SIGNAL(cellSelected(int,int)),
//		this, TQ_SLOT(slotCellSelected(int,int)));
	m_dataObject->connectCellSelectedSignal(this, TQ_SLOT(slotCellSelected(int,int)));
//
	slotDataSet( m_dataObject->data() );
	const bool wasDirty = view->dirty();
	clear();
	if (!wasDirty)
		view->setDirty(false);
}

KexiDataAwarePropertySet::~KexiDataAwarePropertySet()
{
}

void KexiDataAwarePropertySet::slotDataSet( KexiTableViewData *data )
{
	if (!m_currentTVData.isNull()) {
		m_currentTVData->disconnect( this );
		clear();
	}
	m_currentTVData = data;
	if (!m_currentTVData.isNull()) {
		connect(m_currentTVData, TQ_SIGNAL(rowDeleted()), this, TQ_SLOT(slotRowDeleted()));
		connect(m_currentTVData, TQ_SIGNAL(rowsDeleted( const TQValueList<int> & )),
			this, TQ_SLOT(slotRowsDeleted( const TQValueList<int> & )));
		connect(m_currentTVData, TQ_SIGNAL(rowInserted(KexiTableItem*,uint,bool)),
			this, TQ_SLOT(slotRowInserted(KexiTableItem*,uint,bool)));
		connect(m_currentTVData, TQ_SIGNAL(reloadRequested()),
			this, TQ_SLOT(slotReloadRequested()));
	}
}

void KexiDataAwarePropertySet::removeCurrentPropertySet()
{
	remove( m_dataObject->currentRow() );
}

void KexiDataAwarePropertySet::remove(uint row)
{
	KoProperty::Set *set = m_sets.at(row);
	if (!set)
		return;
	set->debug();
	m_sets.remove(row);
	m_view->setDirty();
	m_view->propertySetSwitched();
}

uint KexiDataAwarePropertySet::size() const
{
	return m_sets.size();
}

void KexiDataAwarePropertySet::clear(uint minimumSize)
{
	m_sets.clear();
	m_sets.resize(TQMAX(minimumSize, MAX_FIELDS));
	m_view->setDirty(true);
	m_view->propertySetSwitched();
}

void KexiDataAwarePropertySet::slotReloadRequested()
{
	clear();
}

void KexiDataAwarePropertySet::insert(uint row, KoProperty::Set* set, bool newOne)
{
	if (!set || row >= m_sets.size()) {
		kexiwarn << "KexiDataAwarePropertySet::insert() invalid args: rew="<< row<< " propertyset="<< set<< endl;
		return;
	}
	if (set->parent() && set->parent()!=this) {
		kexiwarn << "KexiDataAwarePropertySet::insert() propertyset's parent must be NULL or this KexiDataAwarePropertySet" << endl;
		return;
	}

	m_sets.insert(row, set);

	connect(set, TQ_SIGNAL(propertyChanged(KoProperty::Set&, KoProperty::Property&)), m_view, TQ_SLOT(setDirty()));

	if (newOne) {
		//add a special property indicating that this is brand new set,
		//not just changed
		KoProperty::Property* prop = new KoProperty::Property("newrow");
		prop->setVisible(false);
		set->addProperty( prop );
		m_view->setDirty();
	}
}

KoProperty::Set* KexiDataAwarePropertySet::currentPropertySet() const
{
	return (m_dataObject->currentRow() >= 0) ? m_sets.at( m_dataObject->currentRow() ) : 0;
}

uint KexiDataAwarePropertySet::currentRow() const
{
	return m_dataObject->currentRow();
}

void KexiDataAwarePropertySet::slotRowDeleted()
{
	m_view->setDirty();
	removeCurrentPropertySet();

	//let's move up all property sets that are below that deleted
	m_sets.setAutoDelete(false);//to avoid auto deleting in insert()
	const int r = m_dataObject->currentRow();
	for (int i=r;i<int(m_sets.size()-1);i++) {
		KoProperty::Set *set = m_sets[i+1];
		m_sets.insert( i , set );
	}
	m_sets.insert( m_sets.size()-1, 0 );
	m_sets.setAutoDelete(true);//revert the flag

	m_view->propertySetSwitched();
	emit rowDeleted();
}

void KexiDataAwarePropertySet::slotRowsDeleted( const TQValueList<int> &rows )
{
	//let's move most property sets up & delete unwanted
	m_sets.setAutoDelete(false);//to avoid auto deleting in insert()
	const int orig_size = size();
	int prev_r = -1;
	int num_removed = 0, cur_r = -1;
	for (TQValueList<int>::ConstIterator r_it = rows.constBegin(); r_it!=rows.constEnd() && *r_it < orig_size; ++r_it) {
		cur_r = *r_it;// - num_removed;
		if (prev_r>=0) {
//			kdDebug() << "move " << prev_r+num_removed-1 << ".." << cur_r-1 << " to " << prev_r+num_removed-1 << ".." << cur_r-2 << endl;
			int i=prev_r;
			KoProperty::Set *set = m_sets.take(i+num_removed);
			kdDebug() << "property set " << i+num_removed << " deleted" << endl;
			delete set;
			num_removed++;
			for (; (i+num_removed)<cur_r; i++) {
				m_sets.insert( i, m_sets[i+num_removed] );
				kdDebug() << i << " <- " << i+num_removed << endl;
			}
		}
		prev_r = cur_r - num_removed;
	}
	//move remaining property sets up
	if (cur_r>=0) {
		KoProperty::Set *set = m_sets.take(cur_r);
		kdDebug() << "property set " << cur_r << " deleted" << endl;
		delete set;
		num_removed++;
		for (int i=prev_r; (i+num_removed)<orig_size; i++) {
			m_sets.insert( i, m_sets[i+num_removed] );
			kdDebug() << i << " <- " << i+num_removed << endl;
		}
	}
	//finally: clear last rows
	for (int i=orig_size-num_removed; i<orig_size; i++) {
		kdDebug() << i << " <- zero" << endl;
		m_sets.insert( i, 0 );
	}
	m_sets.setAutoDelete(true);//revert the flag

	if (num_removed>0)
		m_view->setDirty();
	m_view->propertySetSwitched();
}

//void KexiDataAwarePropertySet::slotEmptyRowInserted(KexiTableItem*, uint /*index*/)
void KexiDataAwarePropertySet::slotRowInserted(KexiTableItem*, uint row, bool /*repaint*/)
{
	m_view->setDirty();

	//let's move down all property set that are below
	m_sets.setAutoDelete(false);//to avoid auto deleting in insert()
//	const int r = m_dataObject->currentRow();
	m_sets.resize(m_sets.size()+1);
	for (int i=int(m_sets.size())-1; i>(int)row; i--) {
		KoProperty::Set *set = m_sets[i-1];
		m_sets.insert( i , set );
	}
	m_sets.insert( row, 0 );
	m_sets.setAutoDelete(true);//revert the flag

	m_view->propertySetSwitched();

	emit rowInserted();
}

void KexiDataAwarePropertySet::slotCellSelected(int, int row)
{
	if(row == m_row)
		return;
	m_row = row;
	m_view->propertySetSwitched();
}

KoProperty::Set* KexiDataAwarePropertySet::findPropertySetForItem(KexiTableItem& item)
{
	if (m_currentTVData.isNull())
		return 0;
	int idx = m_currentTVData->findRef(&item);
	if (idx<0)
		return 0;
	return m_sets[idx];
}

int KexiDataAwarePropertySet::findRowForPropertyValue(const TQCString& propertyName, const TQVariant& value)
{
	const int size = m_sets.size();
	for (int i=0; i<size; i++) {
		KoProperty::Set *set = m_sets[i];
		if (!set || !set->contains(propertyName))
			continue;
		if (set->property(propertyName).value() == value)
			return i;
	}
	return -1;
}

#include "kexidataawarepropertyset.moc"