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
|
/***************************************************************************
* Copyright (C) 2005 by Ken Werner *
* [email protected] *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include "flowlayout.h"
#include "sources/source.h"
//#include "kdebug.h"
//#include <tqlabel.h> // debugging
class FlowLayoutIterator :public TQGLayoutIterator{
public:
FlowLayoutIterator( TQPtrList<TQLayoutItem>* layoutItems ):
index(0),
mLayoutItems(layoutItems){
}
uint count() const;
TQLayoutItem* current();
TQLayoutItem* next();
TQLayoutItem* takeCurrent();
private:
int index;
TQPtrList<TQLayoutItem>* mLayoutItems;
};
TQLayoutItem* FlowLayoutIterator::current(){
return index < int(mLayoutItems->count()) ? mLayoutItems->at(index) : 0;
/*if(index < int(mLayoutItems->count())){
TQLayoutItem* item = mLayoutItems->at(index);
kdDebug() << "FlowLayoutIterator::current(index " << index << ") returns: " << item << endl;
return item;
}else{
kdDebug() << "FlowLayoutIterator::current(index " << index << ") returns: NULL" << endl;
return 0;
}*/
}
TQLayoutItem* FlowLayoutIterator::next(){
index++;
//kdDebug() << "FlowLayoutIterator::next, index: " << index << endl;
return current();
}
TQLayoutItem* FlowLayoutIterator::takeCurrent(){
return index < int(mLayoutItems->count()) ? mLayoutItems->take(index) : 0;
/*if(index < int(mLayoutItems->count())){
TQLayoutItem* item = mLayoutItems->take(index);
kdDebug() << "FlowLayoutIterator::takeCurrent(index " << index << ") returns: " << item << endl;
return item;
}else{
kdDebug() << "FlowLayoutIterator::takeCurrent(index " << index << ") returns: NULL" << endl;
return 0;
}*/
}
FlowLayout::FlowLayout( TQWidget* parent, Qt::Orientation orientation, int border, int space, const char* name )
: TQLayout( parent, border, space, name ),
mOrientation(orientation), mLastItem(NULL){
}
FlowLayout::FlowLayout( TQLayout* parent, Qt::Orientation orientation, int space, const char* name )
: TQLayout( parent, space, name ),
mOrientation(orientation), mLastItem(NULL){
}
FlowLayout::FlowLayout( Qt::Orientation orientation, int space, const char* name )
: TQLayout( space, name ),
mOrientation(orientation), mLastItem(NULL){
}
FlowLayout::~FlowLayout(){
deleteAllItems();
}
int FlowLayout::heightForWidth( int w ) const{
FlowLayout* mthis = (FlowLayout*)this;
int h = mthis->doLayout( TQRect(0,0,w,0), TRUE );
return h;
}
int FlowLayout::widthForHeight( int h ) const{
FlowLayout* mthis = (FlowLayout*)this;
int w = mthis->doLayout( TQRect(0,0,0,h), TRUE );
return w;
}
void FlowLayout::addItem(TQLayoutItem* item){
//kdDebug() << "FlowLayout::addItem: " << (static_cast<TQLabel*>(item->widget()))->text() << ", width: " << item->widget()->width() << ", height: " << item->widget()->height()<< endl;
// we are indirectly called from addSource. this
// is a hint for addSource, to let it know which item
// was added.
mLastItem = TQT_TQLAYOUTITEM(item);
}
void FlowLayout::addSource(Source* src){
add(src->getWidget());
mSources[mLastItem] = src;
src->getWidget()->show();
// step back until we find an item which has a
// smaller position stored in its config. then, we found
// the right position for the new item.
TQLayoutItem * qli = mLayoutItems.last();
while(qli && mSources[qli]->getPosition() > src->getPosition())
qli = mLayoutItems.prev();
mLayoutItems.insert(mLayoutItems.at()+1, mLastItem);
}
void FlowLayout::remove(TQWidget* widget){
//kdDebug() << "FlowLayout::remove: " << (static_cast<TQLabel*>(widget))->text() << endl;
widget->hide();
TQPtrListIterator<TQLayoutItem> it(mLayoutItems);
while(it.current() != NULL) {
if(it.current()->widget() == widget) {
mSources.erase(it.current());
mLayoutItems.remove(it.current());
// removes and deletes only the TQLayoutItem
// (TQWidgetItem)
TQLayout::remove(widget);
break;
}
++it;
}
}
uint FlowLayout::count(){
return mLayoutItems.count();
}
bool FlowLayout::moveItem(const TQLayoutItem* which, const TQLayoutItem* relate, DIRECTION direction){
int newPos = mLayoutItems.findRef(relate);
int oldPos = mLayoutItems.findRef(which);
// check whether the widget is already at a correct position
if(oldPos+1 == newPos && direction == ABOVE || oldPos-1 == newPos && direction == BELOW)
return false;
// remove the item
mLayoutItems.remove(which);
if(oldPos < newPos)
newPos--;
newPos += direction;
// actually reinsert the item
mLayoutItems.insert(newPos, which);
activate(); // relayout
// kdDebug() << "oldPos: " << oldPos << ", newPos: " << newPos << endl;
return true;
}
void FlowLayout::updatePositions(TDEConfig * inTDEConfig){
//kdDebug() << "updating all positions..." << endl;
int pos = 0;
TQPtrListIterator<TQLayoutItem> it(mLayoutItems);
while(it.current() != NULL) {
mSources[it.current()]->setPosition(pos, inTDEConfig);
++it;
++pos;
}
//kdDebug() << "positions updated!" << endl;
}
bool FlowLayout::hasHeightForWidth() const{
return mOrientation != Qt::Horizontal;
}
bool FlowLayout::hasWidthForHeight() const{
return mOrientation == Qt::Horizontal;
}
TQSize FlowLayout::sizeHint() const{
//return minimumSize();
TQSize size(0,0);
TQPtrListIterator<TQLayoutItem> it(mLayoutItems);
TQLayoutItem *o;
while((o=it.current()) != 0){
++it;
size = size.expandedTo( o->sizeHint() );
}
return size;
}
TQSize FlowLayout::minimumSize() const{
TQSize size(0,0);
TQPtrListIterator<TQLayoutItem> it(mLayoutItems);
TQLayoutItem *o;
while((o=it.current()) != 0){
++it;
size = size.expandedTo(o->minimumSize());
}
return size;
}
TQSizePolicy::ExpandData FlowLayout::expanding() const{
return TQ_SPNoDirection;
}
TQLayoutIterator FlowLayout::iterator(){
// [FIXME]
#ifdef USE_QT4
#warning [FIXME] ContainerAreaLayout iterators may not function correctly under Qt4
return TQLayoutIterator( this ); // [FIXME]
#else // USE_QT4
return TQLayoutIterator(new FlowLayoutIterator(&mLayoutItems));
#endif // USE_QT4
}
Qt::Orientation FlowLayout::getOrientation() const{
return mOrientation;
}
void FlowLayout::setOrientation(Qt::Orientation orientation){
mOrientation = orientation;
}
void FlowLayout::setGeometry( const TQRect& rect ){
TQLayout::setGeometry( rect );
doLayout( rect );
}
int FlowLayout::doLayout( const TQRect& rect, bool testonly ){
if(mOrientation == Qt::Horizontal)
return doLayoutHorizontal(rect, testonly);
else
return doLayoutVertical(rect, testonly);
}
int FlowLayout::doLayoutHorizontal( const TQRect& rect, bool testOnly ){
//kdDebug() << "spacing: " << spacing() << endl;
int x = rect.x();
int y = rect.y();
int width = 0; // width of this column so far
int height = 0; // height of this column so far
TQPtrListIterator<TQLayoutItem> it(mLayoutItems);
TQLayoutItem* layoutItem;
TQPtrList<TQLayoutItem> column; // stores the items of one column
while((layoutItem = it.current()) != 0){
++it;
//int nextY = y + layoutItem->sizeHint().height() + spacing(); // next y
int nextY = y + layoutItem->sizeHint().height(); // next y
//if( nextY - spacing() > rect.bottom() && width > 0 ) {
if( nextY > rect.bottom() && width > 0 ) {
// next column
y = rect.y(); // reset y
x = x + width + spacing(); // new x
//nextY = y + layoutItem->sizeHint().height() + spacing(); // next y with changed y
nextY = y + layoutItem->sizeHint().height(); // next y with changed y
width = 0; // reset width for the next column
}
if(!testOnly){
layoutItem->setGeometry( TQRect( TQPoint( x, y ), layoutItem->sizeHint() ) );
column.append(layoutItem);
height += layoutItem->sizeHint().height(); // add the height of the current item to the column height
if( it.current() == 0 || nextY + it.current()->sizeHint().height() > rect.bottom() ){ // test it it's the last item (of this column)
// calculate real needed width
int rWidth = 0;
for(TQLayoutItem* item = column.first(); item; item = column.next()){
rWidth = TQMAX( rWidth, item->widget()->sizeHint().width() );
}
// relayout the items of the former column
int space = (rect.height() - height) / (column.count() + 1);
int i = 0; // counts the items of this column
for(TQLayoutItem* item = column.first(); item; item = column.next()){
TQRect r = item->geometry();
item->setGeometry( TQRect(r.left(), r.top() + ((++i) * space), rWidth, r.height()) );
}
column.clear(); // remove the items of the former column
height = 0; // reset height for the next column
}
}
y = nextY;
width = TQMAX( width, layoutItem->sizeHint().width() );
}
return x + width - rect.x(); // width
}
int FlowLayout::doLayoutVertical( const TQRect& rect, bool testOnly ){
int x = rect.x();
int y = rect.y();
int height = 0; // height of this line so far
TQPtrListIterator<TQLayoutItem> it(mLayoutItems);
TQLayoutItem* layoutItem;
while((layoutItem = it.current() ) != 0){
++it;
//int nextX = x + layoutItem->sizeHint().width() + spacing();
int nextX = x + layoutItem->sizeHint().width();
if(nextX - spacing() > rect.right() && height > 0) {
// next line
x = rect.x(); // reset x
//y = y + height + spacing(); // new y
y = y + height; // new y
//nextX = x + layoutItem->sizeHint().width() + spacing(); // next x
nextX = x + layoutItem->sizeHint().width(); // next x
height = 0; // reset height for the next line
}
const int itemHeight = layoutItem->sizeHint().height();
if(!testOnly)
layoutItem->setGeometry(TQRect(x, y, rect.right(), itemHeight));
x = nextX;
height = TQMAX(height, itemHeight);
}
return y + height - rect.y(); // height
}
#ifdef USE_QT4
/*!
\reimp
*/
int FlowLayout::count() const {
return mLayoutItems.count();
}
/*!
\reimp
*/
TQLayoutItem* FlowLayout::itemAt(int index) const {
return index >= 0 && index < mLayoutItems.count() ? (const_cast<TQPtrList<TQLayoutItem>&>(mLayoutItems).at(index)) : 0;
}
/*!
\reimp
*/
TQLayoutItem* FlowLayout::takeAt(int index) {
if (index < 0 || index >= mLayoutItems.count())
return 0;
TQLayoutItem *item = mLayoutItems.at(index);
mLayoutItems.remove(mLayoutItems.at(index));
delete item;
invalidate();
return item;
}
#endif // USE_QT4
|