summaryrefslogtreecommitdiffstats
path: root/kexi/widget/utils/kexiflowlayout.cpp
blob: b8a8601e113af576e593db2eb9ea6a1845646ba3 (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
/* This file is part of the KDE project
   Copyright (C) 2005 Cedric Pasteur <cedric.pasteur@free.fr>

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

#include <kdebug.h>

/// Iterator class

class KexiFlowLayoutIterator : public QGLayoutIterator
{
	public:
		KexiFlowLayoutIterator( QPtrList<QLayoutItem> *list )
		  : m_idx(0), m_list( list )
		{}
		uint count() const;
		QLayoutItem *current();
		QLayoutItem *next();
		QLayoutItem *takeCurrent();

	private:
		int m_idx;
		QPtrList<QLayoutItem> *m_list;
};

uint
KexiFlowLayoutIterator::count() const
{
	return m_list->count();
}

QLayoutItem *
KexiFlowLayoutIterator::current()
{
	return (m_idx < (int)count()) ? m_list->at(m_idx) : 0;
}

QLayoutItem *
KexiFlowLayoutIterator::next()
{
	m_idx++;
	return current();
}

QLayoutItem *
KexiFlowLayoutIterator::takeCurrent()
{
	return (m_idx < (int)count()) ? m_list->take(m_idx) : 0;
}

//// The layout itself

KexiFlowLayout::KexiFlowLayout(QWidget *parent, int border, int space, const char *name)
 : QLayout(parent, border, space, name)
{
	m_orientation = Horizontal;
	m_justify = false;
	m_cached_width = 0;
}

KexiFlowLayout::KexiFlowLayout(QLayout* parent, int space, const char *name)
 : QLayout( parent, space, name )
{
	m_orientation = Horizontal;
	m_justify = false;
	m_cached_width = 0;
}

KexiFlowLayout::KexiFlowLayout(int space, const char *name)
 : QLayout(space, name)
 {
	m_orientation = Horizontal;
	m_justify = false;
	m_cached_width = 0;
 }

KexiFlowLayout::~KexiFlowLayout()
{
	deleteAllItems();
}

void
KexiFlowLayout::addItem(QLayoutItem *item)
{
	m_list.append(item);
}

void
KexiFlowLayout::addSpacing(int size)
{
	if (m_orientation == Horizontal)
		addItem( new QSpacerItem( size, 0, QSizePolicy::Fixed, QSizePolicy::Minimum ) );
	else
		addItem( new QSpacerItem( 0, size, QSizePolicy::Minimum, QSizePolicy::Fixed ) );
}

QLayoutIterator
KexiFlowLayout::iterator()
{
	return QLayoutIterator( new KexiFlowLayoutIterator(&m_list) );
}

QPtrList<QWidget>*
KexiFlowLayout::widgetList() const
{
	QPtrList<QWidget> *list = new QPtrList<QWidget>();
	for (QPtrListIterator<QLayoutItem> it(m_list); it.current(); ++it) {
		if(it.current()->widget())
			list->append(it.current()->widget());
	}
	return list;
}

void
KexiFlowLayout::invalidate()
{
	QLayout::invalidate();
	m_cached_sizeHint = QSize();
	m_cached_minSize = QSize();
	m_cached_width = 0;
}

bool
KexiFlowLayout::isEmpty()
{
	return m_list.isEmpty();
}

bool
KexiFlowLayout::hasHeightForWidth() const
{
	return (m_orientation == Horizontal);
}

int
KexiFlowLayout::heightForWidth(int w) const
{
	if(m_cached_width != w) {
		// workaround to allow this method to stay 'const'
		KexiFlowLayout *mthis = (KexiFlowLayout*)this;
		int h = mthis->simulateLayout( QRect(0,0,w,0) );
		mthis->m_cached_hfw = h;
		mthis->m_cached_width = w;
		return h;
 	}
	return m_cached_hfw;
}

QSize
KexiFlowLayout::sizeHint() const
{
	if(m_cached_sizeHint.isEmpty()) {
		KexiFlowLayout *mthis = (KexiFlowLayout*)this;
		QRect r = QRect(0, 0, 2000, 2000);
		mthis->simulateLayout(r);
	}
	return m_cached_sizeHint;
}

QSize
KexiFlowLayout::minimumSize() const
{
//js: do we really need to simulate layout here?
//    I commented this out because it was impossible to stretch layout conveniently.
//    Now, minimum size is computed automatically based on item's minimumSize...
#if 0
	if(m_cached_minSize.isEmpty()) {
		KexiFlowLayout *mthis = (KexiFlowLayout*)this;
		QRect r = QRect(0, 0, 2000, 2000);
		mthis->simulateLayout(r);
	}
#endif
	return m_cached_minSize;
}

QSizePolicy::ExpandData
KexiFlowLayout::expanding() const
{
	if(m_orientation == Vertical)
		return QSizePolicy::Vertically;
	else
		return QSizePolicy::Horizontally;
}

void
KexiFlowLayout::setGeometry(const QRect &r)
{
	QLayout::setGeometry(r);
	if(m_orientation == Horizontal)
		doHorizontalLayout(r);
	else
		doVerticalLayout(r);
}

int
KexiFlowLayout::simulateLayout(const QRect &r)
{
	if(m_orientation == Horizontal)
		return doHorizontalLayout(r, true);
	else
		return doVerticalLayout(r, true);
}

int
KexiFlowLayout::doHorizontalLayout(const QRect &r, bool testOnly)
{
	int x = r.x();
	int y = r.y();
	int h = 0; // height of this line
	int availableSpace = r.width() + spacing();
	int expandingWidgets=0; // number of widgets in the line with QSizePolicy == Expanding
	QPtrListIterator<QLayoutItem> it(m_list);
	QPtrList<QLayoutItem> currentLine;
	QLayoutItem *o;
	QSize minSize, sizeHint(20, 20);
	int minSizeHeight = 0 - spacing();

	while ( (o = it.current()) != 0 ) {
		if(o->isEmpty()) { /// do not consider hidden widgets
			++it;
			continue;
		}

//		kdDebug() << "- doHorizontalLayout(): " << o->widget()->className() << " " << o->widget()->name() << endl;
		QSize oSizeHint = o->sizeHint(); // we cache these ones because it can take a while to get it (eg for child layouts)
		if ((x + oSizeHint.width()) > r.right() && h > 0) {
			// do the layout of current line
			QPtrListIterator<QLayoutItem> it2(currentLine);
			QLayoutItem *item;
			int wx = r.x();
			int sizeHintWidth = 0 -spacing(), minSizeWidth=0 - spacing(), lineMinHeight=0;
			while( (item = it2.current()) != 0 ) {
				QSize itemSizeHint = item->sizeHint(); // we cache these ones because it can take
				QSize itemMinSize = item->minimumSize(); // a while to get them
				QSize s;
				if(m_justify) {
					if(expandingWidgets != 0) {
						if(item->expanding() == QSizePolicy::Horizontally || item->expanding() == QSizePolicy::BothDirections)
							s = QSize( QMIN(itemSizeHint.width() + availableSpace / expandingWidgets
								, r.width()), itemSizeHint.height() );
						else
							s = QSize( QMIN(itemSizeHint.width(), r.width()), itemSizeHint.height() );
					}
					else
						s = QSize( QMIN(itemSizeHint.width() + availableSpace / (int)currentLine.count()
							, r.width()), itemSizeHint.height() );
				}
				else
					s = QSize ( QMIN(itemSizeHint.width(), r.width()), itemSizeHint.height() );
				if(!testOnly)
					item->setGeometry( QRect(QPoint(wx, y), s) );
				wx = wx + s.width() + spacing();
				minSizeWidth = minSizeWidth + spacing() + itemMinSize.width();
				sizeHintWidth = sizeHintWidth + spacing() +  itemSizeHint.width();
				lineMinHeight = QMAX( lineMinHeight, itemMinSize.height() );
				++it2;
			}
			sizeHint = sizeHint.expandedTo( QSize(sizeHintWidth, 0) );
			minSize = minSize.expandedTo( QSize(minSizeWidth, 0) );
			minSizeHeight = minSizeHeight + spacing() + lineMinHeight;
			// start a new line
			y = y + spacing() + h;
			h = 0;
			x = r.x();
			currentLine.clear();
			expandingWidgets = 0;
			availableSpace = r.width() + spacing();
		}

		x = x + spacing() + oSizeHint.width();
		h = QMAX( h,  oSizeHint.height() );
		currentLine.append(o);
		if(o->expanding() == QSizePolicy::Horizontally || o->expanding() == QSizePolicy::BothDirections)
			++expandingWidgets;
		availableSpace = QMAX(0, availableSpace - spacing() - oSizeHint.width());
		++it;
	}

	// don't forget to layout the last line
	QPtrListIterator<QLayoutItem> it2(currentLine);
	QLayoutItem *item;
	int wx = r.x();
	int sizeHintWidth = 0 -spacing(), minSizeWidth=0 - spacing(), lineMinHeight=0;
	while( (item = it2.current()) != 0 ) {
		QSize itemSizeHint = item->sizeHint(); // we cache these ones because it can take
		QSize itemMinSize = item->minimumSize(); // a while to get them
		QSize s;
		if(m_justify) {
			if(expandingWidgets != 0) {
				if(item->expanding() == QSizePolicy::Horizontally || item->expanding() == QSizePolicy::BothDirections)
					s = QSize( QMIN(itemSizeHint.width() + availableSpace / expandingWidgets
						, r.width()), itemSizeHint.height() );
				else
					s = QSize( QMIN(itemSizeHint.width(), r.width()), itemSizeHint.height() );
			}
			else
				s = QSize( QMIN(itemSizeHint.width() + availableSpace / (int)currentLine.count()
					, r.width()), itemSizeHint.height() );
		}
		else
			s = QSize ( QMIN(itemSizeHint.width(), r.width()), itemSizeHint.height() );
		if(!testOnly)
			item->setGeometry( QRect(QPoint(wx, y), s) );
		wx = wx + s.width() + spacing();
		minSizeWidth = minSizeWidth + spacing() + itemMinSize.width();
		sizeHintWidth = sizeHintWidth + spacing() +  itemSizeHint.width();
		lineMinHeight = QMAX( lineMinHeight, itemMinSize.height() );
		++it2;
	}
	sizeHint = sizeHint.expandedTo( QSize(sizeHintWidth, y + spacing() + h) );
	minSizeHeight = minSizeHeight + spacing() + lineMinHeight;
	minSize = minSize.expandedTo( QSize(minSizeWidth, minSizeHeight) );

	// store sizeHint() and minimumSize()
	m_cached_sizeHint = sizeHint + QSize(2* margin(), 2*margin());
	m_cached_minSize = minSize + QSize(2* margin() , 2*margin());
	// return our height
	return y + h - r.y();
}

int
KexiFlowLayout::doVerticalLayout(const QRect &r, bool testOnly)
{
	int x = r.x();
	int y = r.y();
	int w = 0; // width of this line
	int availableSpace = r.height() + spacing();
	int expandingWidgets=0; // number of widgets in the line with QSizePolicy == Expanding
	QPtrListIterator<QLayoutItem> it(m_list);
	QPtrList<QLayoutItem> currentLine;
	QLayoutItem *o;
	QSize minSize, sizeHint(20, 20);
	int minSizeWidth = 0 - spacing();

	while ( (o = it.current()) != 0 ) {
		if(o->isEmpty()) { /// do not consider hidden widgets
			++it;
			continue;
		}

		QSize oSizeHint = o->sizeHint(); // we cache these ones because it can take a while to get it (eg for child layouts)
		if (y + oSizeHint.height() > r.bottom() && w > 0) {
			// do the layout of current line
			QPtrListIterator<QLayoutItem> it2(currentLine);
			QLayoutItem *item;
			int wy = r.y();
			int sizeHintHeight = 0 - spacing(), minSizeHeight = 0 - spacing(), colMinWidth=0;
			while( (item = it2.current()) != 0 ) {
				QSize itemSizeHint = item->sizeHint(); // we cache these ones because it can take
				QSize itemMinSize = item->minimumSize(); // a while to get them
				QSize s;
				if(m_justify) {
					if(expandingWidgets != 0) {
						if(item->expanding() == QSizePolicy::Vertically || item->expanding() == QSizePolicy::BothDirections)
							s = QSize( itemSizeHint.width(), QMIN(itemSizeHint.height() + availableSpace / expandingWidgets
								, r.height()) );
						else
							s = QSize( itemSizeHint.width(), QMIN(itemSizeHint.height(), r.height()) );
					}
					else
						s = QSize( itemSizeHint.width(), QMIN(itemSizeHint.height() + availableSpace / (int)currentLine.count()
							, r.height()) );
				}
				else
					s = QSize (  itemSizeHint.width(), QMIN(itemSizeHint.height(), r.height()) );
				if(!testOnly)
					item->setGeometry( QRect(QPoint(x, wy), s) );
				wy = wy + s.height() + spacing();
				minSizeHeight = minSizeHeight + spacing() + itemMinSize.height();
				sizeHintHeight = sizeHintHeight + spacing() + itemSizeHint.height();
				colMinWidth = QMAX( colMinWidth, itemMinSize.width() );
				++it2;
			}
			sizeHint = sizeHint.expandedTo( QSize(0, sizeHintHeight) );
			minSize = minSize.expandedTo( QSize(0, minSizeHeight) );
			minSizeWidth = minSizeWidth + spacing() + colMinWidth;
			// start a new column
			x = x + spacing() + w;
			w = 0;
			y = r.y();
			currentLine.clear();
			expandingWidgets = 0;
			availableSpace = r.height() + spacing();
		}

		y = y + spacing() + oSizeHint.height();
		w = QMAX( w,  oSizeHint.width() );
		currentLine.append(o);
		if(o->expanding() == QSizePolicy::Vertically || o->expanding() == QSizePolicy::BothDirections)
			++expandingWidgets;
		availableSpace = QMAX(0, availableSpace - spacing() - oSizeHint.height());
		++it;
	}

	// don't forget to layout the last line
	QPtrListIterator<QLayoutItem> it2(currentLine);
	QLayoutItem *item;
	int wy = r.y();
	int sizeHintHeight = 0 - spacing(), minSizeHeight = 0 - spacing(), colMinWidth=0;
	while( (item = it2.current()) != 0 ) {
		QSize itemSizeHint = item->sizeHint(); // we cache these ones because it can take
		QSize itemMinSize = item->minimumSize(); // a while to get them
		QSize s;
		if(m_justify) {
			if(expandingWidgets != 0) {
				if(item->expanding() == QSizePolicy::Vertically || item->expanding() == QSizePolicy::BothDirections)
					s = QSize( itemSizeHint.width(), QMIN(itemSizeHint.height() + availableSpace / expandingWidgets
						, r.height()) );
				else
					s = QSize( itemSizeHint.width(), QMIN(itemSizeHint.height(), r.height()) );
			}
			else
				s = QSize( itemSizeHint.width(), QMIN(itemSizeHint.height() + availableSpace / (int)currentLine.count()
					, r.height()) );
		}
		else
			s = QSize (  itemSizeHint.width(), QMIN(itemSizeHint.height(), r.height()) );
		if(!testOnly)
			item->setGeometry( QRect(QPoint(x, wy), s) );
		wy = wy + s.height() + spacing();
		minSizeHeight = minSizeHeight + spacing() + itemMinSize.height();
		sizeHintHeight = sizeHintHeight + spacing() + itemSizeHint.height();
		colMinWidth = QMAX( colMinWidth, itemMinSize.width() );
		++it2;
	}
	sizeHint = sizeHint.expandedTo( QSize( x + spacing() + w, sizeHintHeight) );
	minSizeWidth = minSizeWidth + spacing() + colMinWidth;
	minSize = minSize.expandedTo( QSize(minSizeWidth, minSizeHeight) );

	// store sizeHint() and minimumSize()
	m_cached_sizeHint = sizeHint + QSize(2* margin(), 2*margin());
	m_cached_minSize = minSize + QSize(2* margin(), 2*margin());
	// return our width
	return x + w - r.x();
}