summaryrefslogtreecommitdiffstats
path: root/kdevdesigner/designer/layout.cpp
blob: 450f4633dc9ad06c3ee3f764932917526256cb78 (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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of TQt Designer.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Licensees holding valid TQt Enterprise Edition or TQt Professional Edition
** licenses may use this file in accordance with the TQt Commercial License
** Agreement provided with the Software.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
**   information about TQt Commercial License Agreements.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#include "formwindow.h"
#include "tqlayout.h"
#include <widgetdatabase.h>
#include "widgetfactory.h"

#include <tqlayout.h>
#include <tqevent.h>
#include <tqpainter.h>
#include <tqpen.h>
#include <tqbitmap.h>
#include <tqsplitter.h>
#include <tqvaluevector.h>
#include <tqmainwindow.h>

bool operator<( const TQGuardedPtr<TQWidget> &p1, const TQGuardedPtr<TQWidget> &p2 )
{
    return p1.operator->() < p2.operator->();
}

/*!
  \class Layout tqlayout.h
  \brief Baseclass for layouting widgets in the Designer

  Classes derived from this abstract base class are used for layouting
  operations in the Designer.

*/

/*!  \a p specifies the tqparent of the layoutBase \a lb. The tqparent
  might be changed in setup(). If the layoutBase is a
  container, the tqparent and the layoutBase are the same. Also they
  always have to be a widget known to the designer (e.g. in the case
  of the tabwidget tqparent and layoutBase are the tabwidget and not the
  page which actually gets laid out. For actual usage the correct
  widget is found later by Layout.)
 */

Layout::Layout( const TQWidgetList &wl, TQWidget *p, FormWindow *fw, TQWidget *lb, bool doSetup, bool splitter )
    : widgets( wl ), tqparent( p ), formWindow( fw ), isBreak( !doSetup ), useSplitter( splitter )
{
    widgets.setAutoDelete( FALSE );
    layoutBase = lb;
    if ( !doSetup && layoutBase )
	oldGeometry = layoutBase->tqgeometry();
}

/*!  The widget list we got in the constructor might contain too much
  widgets (like widgets with different parents, already laid out
  widgets, etc.). Here we set up the list and so the only the "best"
  widgets get laid out.
*/

void Layout::setup()
{
    startPoint = TQPoint( 32767, 32767 );
    TQValueList<TQWidgetList> lists;
    TQWidget *lastParent = 0;
    TQWidgetList *lastList = 0;
    TQWidget *w = 0;

    // Go through all widgets of the list we got. As we can only
    // tqlayout widgets which have the same tqparent, we first do some
    // sorting which means create a list for each tqparent containing
    // its child here. After that we keep working on the list of
    // childs which has the most entries.
    // Widgets which are already laid out are thrown away here too
    for ( w = widgets.first(); w; w = widgets.next() ) {
	if ( w->parentWidget() && WidgetFactory::layoutType( w->parentWidget() ) != WidgetFactory::NoLayout )
	    continue;
	if ( lastParent != w->parentWidget() ) {
	    lastList = 0;
	    lastParent = w->parentWidget();
	    TQValueList<TQWidgetList>::Iterator it = lists.begin();
	    for ( ; it != lists.end(); ++it ) {
		if ( ( *it ).first()->parentWidget() == w->parentWidget() )
		    lastList = &( *it );
	    }
	    if ( !lastList ) {
		TQWidgetList l;
		l.setAutoDelete( FALSE );
		lists.append( l );
		lastList = &lists.last();
	    }
	}
	lastList->append( w );
    }

    // So, now find the list with the most entries
    lastList = 0;
    TQValueList<TQWidgetList>::Iterator it = lists.begin();
    for ( ; it != lists.end(); ++it ) {
	if ( !lastList || ( *it ).count() > lastList->count() )
	    lastList = &( *it );
    }

    // If we found no list (because no widget did fit at all) or the
    // best list has only one entry and we do not tqlayout a container,
    // we leave here.
    if ( !lastList || ( lastList->count() < 2 &&
			( !layoutBase ||
			  ( !WidgetDatabase::isContainer( WidgetDatabase::idFromClassName( WidgetFactory::classNameOf( TQT_TQOBJECT(layoutBase) ) ) ) &&
			    layoutBase != formWindow->mainContainer() ) )
			) ) {
	widgets.clear();
	startPoint = TQPoint( 0, 0 );
	return;
    }

    // Now we have a new and clean widget list, which makes sense
    // to tqlayout
    widgets = *lastList;
    // Also use the only correct tqparent later, so store it
    tqparent = WidgetFactory::widgetOfContainer( widgets.first()->parentWidget() );
    // Now calculate the position where the tqlayout-meta-widget should
    // be placed and connect to widgetDestroyed() signals of the
    // widgets to get informed if one gets deleted to be able to
    // handle that and do not crash in this case
    for ( w = widgets.first(); w; w = widgets.next() ) {
	connect( w, TQT_SIGNAL( destroyed() ),
		 this, TQT_SLOT( widgetDestroyed() ) );
	startPoint = TQPoint( TQMIN( startPoint.x(), w->x() ),
			     TQMIN( startPoint.y(), w->y() ) );
	geometries.insert( w, TQRect( w->pos(), w->size() ) );
	// Change the Z-order, as saving/loading uses the Z-order for
	// writing/creating widgets and this has to be the same as in
	// the tqlayout. Else saving + loading will give different results
	w->raise();
    }
}

void Layout::widgetDestroyed()
{
     if ( sender() && sender()->isWidgetType() )
	widgets.removeRef( (TQWidget*)sender() );
}

bool Layout::prepareLayout( bool &needMove, bool &needRetqparent )
{
    if ( !widgets.count() )
	return FALSE;
    for ( TQWidget *w = widgets.first(); w; w = widgets.next() )
	w->raise();
    needMove = !layoutBase;
    needRetqparent = needMove || ::tqqt_cast<TQLayoutWidget*>(layoutBase) || ::tqqt_cast<TQSplitter*>(layoutBase);
    if ( !layoutBase ) {
	if ( !useSplitter )
	    layoutBase = WidgetFactory::create( WidgetDatabase::idFromClassName( TQLAYOUTWIDGET_OBJECT_NAME_STRING ),
						WidgetFactory::containerOfWidget( tqparent ) );
	else
	    layoutBase = WidgetFactory::create( WidgetDatabase::idFromClassName( TQSPLITTER_OBJECT_NAME_STRING ),
						WidgetFactory::containerOfWidget( tqparent ) );
    } else {
	WidgetFactory::deleteLayout( layoutBase );
    }

    return TRUE;
}

void Layout::finishLayout( bool needMove, TQLayout *tqlayout )
{
    if ( needMove )
	layoutBase->move( startPoint );
    TQRect g( TQRect( layoutBase->pos(), layoutBase->size() ) );
    if ( WidgetFactory::layoutType( layoutBase->parentWidget() ) == WidgetFactory::NoLayout && !isBreak )
	layoutBase->adjustSize();
    else if ( isBreak )
	layoutBase->setGeometry( oldGeometry );
    oldGeometry = g;
    layoutBase->show();
    tqlayout->activate();
    formWindow->insertWidget( layoutBase );
    formWindow->selectWidget( TQT_TQOBJECT(layoutBase) );
    TQString n = layoutBase->name();
    if ( n.tqfind( "qt_dead_widget_" ) != -1 ) {
	n.remove( 0, TQString( "qt_dead_widget_" ).length() );
	layoutBase->setName( n );
    }
}

void Layout::undoLayout()
{
    if ( !widgets.count() )
	return;
    TQMap<TQGuardedPtr<TQWidget>, TQRect>::Iterator it = geometries.begin();
    for ( ; it != geometries.end(); ++it ) {
	if ( !it.key() )
	    continue;
	it.key()->reparent( WidgetFactory::containerOfWidget( tqparent ), 0, ( *it ).topLeft(), it.key()->isVisibleTo( formWindow ) );
	it.key()->resize( ( *it ).size() );
    }
    formWindow->selectWidget( TQT_TQOBJECT(layoutBase), FALSE );
    WidgetFactory::deleteLayout( layoutBase );
    if ( tqparent != layoutBase && !::tqqt_cast<TQMainWindow*>(layoutBase) ) {
	layoutBase->hide();
	TQString n = layoutBase->name();
	n.prepend( "qt_dead_widget_" );
	layoutBase->setName( n );
    } else {
	layoutBase->setGeometry( oldGeometry );
    }
    if ( widgets.first() )
	formWindow->selectWidget( TQT_TQOBJECT(widgets.first()) );
    else
	formWindow->selectWidget( TQT_TQOBJECT(formWindow) );
}

void Layout::breakLayout()
{
    TQMap<TQWidget*, TQRect> rects;
    if ( !widgets.isEmpty() ) {
	TQWidget *w;
	for ( w = widgets.first(); w; w = widgets.next() )
	    rects.insert( w, w->tqgeometry() );
    }
    WidgetFactory::deleteLayout( layoutBase );
    bool needRetqparent = qstrcmp( layoutBase->className(), TQLAYOUTWIDGET_OBJECT_NAME_STRING ) == 0 ||
			qstrcmp( layoutBase->className(), TQSPLITTER_OBJECT_NAME_STRING ) == 0 ||
			( !WidgetDatabase::isContainer( WidgetDatabase::idFromClassName( WidgetFactory::classNameOf( TQT_TQOBJECT(layoutBase) ) ) ) &&
			  layoutBase != formWindow->mainContainer() );
    bool needResize = qstrcmp( layoutBase->className(), TQSPLITTER_OBJECT_NAME_STRING ) == 0;
    bool add = geometries.isEmpty();
    for ( TQWidget *w = widgets.first(); w; w = widgets.next() ) {
	if ( needRetqparent )
	    w->reparent( layoutBase->parentWidget(), 0,
			 layoutBase->pos() + w->pos(), TRUE );
	if ( needResize ) {
	    TQMap<TQWidget*, TQRect>::Iterator it = rects.tqfind( w );
	    if ( it != rects.end() )
		w->setGeometry( TQRect( layoutBase->pos() + (*it).topLeft(), (*it).size() ) );
	}
	if ( add )
	    geometries.insert( w, TQRect( w->pos(), w->size() ) );
    }
    if ( needRetqparent ) {
	layoutBase->hide();
	tqparent = layoutBase->parentWidget();
	TQString n = layoutBase->name();
	n.prepend( "qt_dead_widget_" );
	layoutBase->setName( n );
    } else {
	tqparent = layoutBase;
    }
    if ( widgets.first() && widgets.first()->isVisibleTo( formWindow ) )
	formWindow->selectWidget( TQT_TQOBJECT(widgets.first()) );
    else
	formWindow->selectWidget( TQT_TQOBJECT(formWindow) );
}

class HorizontalLayoutList : public TQWidgetList
{
public:
    HorizontalLayoutList( const TQWidgetList &l )
	: TQWidgetList( l ) {}

    int compareItems( TQPtrCollection::Item item1, TQPtrCollection::Item item2 ) {
	TQWidget *w1 = (TQWidget*)item1;
	TQWidget *w2 = (TQWidget*)item2;
	if ( w1->x() == w2->x() )
	    return 0;
	if ( w1->x() > w2->x() )
	    return 1;
	return -1;
    }

};

HorizontalLayout::HorizontalLayout( const TQWidgetList &wl, TQWidget *p, FormWindow *fw, TQWidget *lb, bool doSetup, bool splitter )
    : Layout( wl, p, fw, lb, doSetup, splitter )
{
    if ( doSetup )
	setup();
}

void HorizontalLayout::setup()
{
    HorizontalLayoutList l( widgets );
    l.sort();
    widgets = l;
    Layout::setup();
}

void HorizontalLayout::doLayout()
{
    bool needMove, needRetqparent;
    if ( !prepareLayout( needMove, needRetqparent ) )
	return;

    TQHBoxLayout *tqlayout = (TQHBoxLayout*)WidgetFactory::createLayout( layoutBase, 0, WidgetFactory::HBox );

    for ( TQWidget *w = widgets.first(); w; w = widgets.next() ) {
	if ( needRetqparent && TQT_BASE_OBJECT(w->tqparent()) != TQT_BASE_OBJECT(layoutBase) )
	    w->reparent( layoutBase, 0, TQPoint( 0, 0 ), FALSE );
	if ( !useSplitter ) {
	    if ( qstrcmp( w->className(), "Spacer" ) == 0 )
		tqlayout->addWidget( w, 0, ( (Spacer*)w )->tqalignment() );
	    else
		tqlayout->addWidget( w );
	    if ( ::tqqt_cast<TQLayoutWidget*>(w) )
		( (TQLayoutWidget*)w )->updateSizePolicy();
	}
	w->show();
    }

    if ( ::tqqt_cast<TQSplitter*>(layoutBase) )
	( (TQSplitter*)layoutBase )->setOrientation( Qt::Horizontal );

    finishLayout( needMove, tqlayout );
}




class VerticalLayoutList : public TQWidgetList
{
public:
    VerticalLayoutList( const TQWidgetList &l )
	: TQWidgetList( l ) {}

    int compareItems( TQPtrCollection::Item item1, TQPtrCollection::Item item2 ) {
	TQWidget *w1 = (TQWidget*)item1;
	TQWidget *w2 = (TQWidget*)item2;
	if ( w1->y() == w2->y() )
	    return 0;
	if ( w1->y() > w2->y() )
	    return 1;
	return -1;
    }

};

VerticalLayout::VerticalLayout( const TQWidgetList &wl, TQWidget *p, FormWindow *fw, TQWidget *lb, bool doSetup, bool splitter )
    : Layout( wl, p, fw, lb, doSetup, splitter )
{
    if ( doSetup )
	setup();
}

void VerticalLayout::setup()
{
    VerticalLayoutList l( widgets );
    l.sort();
    widgets = l;
    Layout::setup();
}

void VerticalLayout::doLayout()
{
    bool needMove, needRetqparent;
    if ( !prepareLayout( needMove, needRetqparent ) )
	return;

    TQVBoxLayout *tqlayout = (TQVBoxLayout*)WidgetFactory::createLayout( layoutBase, 0, WidgetFactory::VBox );

    for ( TQWidget *w = widgets.first(); w; w = widgets.next() ) {
	if ( needRetqparent && TQT_BASE_OBJECT(w->tqparent()) != TQT_BASE_OBJECT(layoutBase) )
	    w->reparent( layoutBase, 0, TQPoint( 0, 0 ), FALSE );
	if ( !useSplitter ) {
	    if ( qstrcmp( w->className(), "Spacer" ) == 0 )
		tqlayout->addWidget( w, 0, ( (Spacer*)w )->tqalignment() );
	    else
		tqlayout->addWidget( w );
	    if ( ::tqqt_cast<TQLayoutWidget*>(w) )
		( (TQLayoutWidget*)w )->updateSizePolicy();
	}
	w->show();
    }

    if ( ::tqqt_cast<TQSplitter*>(layoutBase) )
	( (TQSplitter*)layoutBase )->setOrientation( Qt::Vertical );

    finishLayout( needMove, tqlayout );
}





class Grid
{
public:
    Grid( int rows, int cols );
    ~Grid();

    TQWidget* cell( int row, int col ) const { return cells[ row * ncols + col]; }
    void setCell( int row, int col, TQWidget* w ) { cells[ row*ncols + col] = w; }
    void setCells( TQRect c, TQWidget* w ) {
	for ( int rows = c.bottom()-c.top(); rows >= 0; rows--)
	    for ( int cols = c.right()-c.left(); cols >= 0; cols--) {
		setCell(c.top()+rows, c.left()+cols, w);
	    }
    }
    int numRows() const { return nrows; }
    int numCols() const { return ncols; }

    void simplify();
    bool locateWidget( TQWidget* w, int& row, int& col, int& rowspan, int& colspan );

private:
    void merge();
    int countRow( int r, int c ) const;
    int countCol( int r, int c ) const;
    void setRow( int r, int c, TQWidget* w, int count );
    void setCol( int r, int c, TQWidget* w, int count );
    bool isWidgetStartCol( int c ) const;
    bool isWidgetEndCol( int c ) const;
    bool isWidgetStartRow( int r ) const;
    bool isWidgetEndRow( int r ) const;
    bool isWidgetTopLeft( int r, int c ) const;
    void extendLeft();
    void extendRight();
    void extendUp();
    void extendDown();
    TQWidget** cells;
    bool* cols;
    bool* rows;
    int nrows, ncols;

};

Grid::Grid( int r, int c )
    : nrows( r ), ncols( c )
{
    cells = new TQWidget*[ r * c ];
    memset( cells, 0, sizeof( cells ) * r * c );
    rows = new bool[ r ];
    cols = new bool[ c ];

}

Grid::~Grid()
{
    delete [] cells;
    delete [] cols;
    delete [] rows;
}

int Grid::countRow( int r, int c ) const
{
    TQWidget* w = cell( r, c );
    int i = c + 1;
    while ( i < ncols && cell( r, i ) == w )
	i++;
    return i - c;
}

int Grid::countCol( int r, int c ) const
{
    TQWidget* w = cell( r, c );
    int i = r + 1;
    while ( i < nrows && cell( i, c ) == w )
	i++;
    return i - r;
}

void Grid::setCol( int r, int c, TQWidget* w, int count )
{
    for (int i = 0; i < count; i++ )
	setCell( r + i, c, w );
}

void Grid::setRow( int r, int c, TQWidget* w, int count )
{
    for (int i = 0; i < count; i++ )
	setCell( r, c + i, w );
}

bool Grid::isWidgetStartCol( int c ) const
{
    int r;
    for ( r = 0; r < nrows; r++ ) {
	if ( cell( r, c ) && ( (c==0) || (cell( r, c)  != cell( r, c-1) )) ) {
	    return TRUE;
	}
    }
    return FALSE;
}

bool Grid::isWidgetEndCol( int c ) const
{
    int r;
    for ( r = 0; r < nrows; r++ ) {
	if ( cell( r, c ) && ((c == ncols-1) || (cell( r, c) != cell( r, c+1) )) )
	    return TRUE;
    }
    return FALSE;
}

bool Grid::isWidgetStartRow( int r ) const
{
    int c;
    for ( c = 0; c < ncols; c++ ) {
	if ( cell( r, c ) && ( (r==0) || (cell( r, c) != cell( r-1, c) )) )
	    return TRUE;
    }
    return FALSE;
}

bool Grid::isWidgetEndRow( int r ) const
{
    int c;
    for ( c = 0; c < ncols; c++ ) {
	if ( cell( r, c ) && ((r == nrows-1) || (cell( r, c) != cell( r+1, c) )) )
	    return TRUE;
    }
    return FALSE;
}


bool Grid::isWidgetTopLeft( int r, int c ) const
{
    TQWidget* w = cell( r, c );
    if ( !w )
	return FALSE;
    return ( !r || cell( r-1, c) != w ) && (!c || cell( r, c-1) != w);
}

void Grid::extendLeft()
{
    int r,c,i;
    for ( c = 1; c < ncols; c++ ) {
	for ( r = 0; r < nrows; r++ ) {
	    TQWidget* w = cell( r, c );
	    if ( !w )
		continue;
	    int cc = countCol( r, c);
	    int stretch = 0;
	    for ( i = c-1; i >= 0; i-- ) {
		if ( cell( r, i ) )
		    break;
		if ( countCol( r, i ) < cc )
		    break;
		if ( isWidgetEndCol( i ) )
		    break;
		if ( isWidgetStartCol( i ) ) {
		    stretch = c - i;
		    break;
		}
	    }
	    if ( stretch ) {
		for ( i = 0; i < stretch; i++ )
		    setCol( r, c-i-1, w, cc );
	    }
	}
    }
}


void Grid::extendRight()
{
    int r,c,i;
    for ( c = ncols - 2; c >= 0; c-- ) {
	for ( r = 0; r < nrows; r++ ) {
	    TQWidget* w = cell( r, c );
	    if ( !w )
		continue;
	    int cc = countCol( r, c);
	    int stretch = 0;
	    for ( i = c+1; i < ncols; i++ ) {
		if ( cell( r, i ) )
		    break;
		if ( countCol( r, i ) < cc )
		    break;
		if ( isWidgetStartCol( i ) )
		    break;
		if ( isWidgetEndCol( i ) ) {
		    stretch = i - c;
		    break;
		}
	    }
	    if ( stretch ) {
		for ( i = 0; i < stretch; i++ )
		    setCol( r, c+i+1, w, cc );
	    }
	}
    }

}

void Grid::extendUp()
{
    int r,c,i;
    for ( r = 1; r < nrows; r++ ) {
	for ( c = 0; c < ncols; c++ ) {
	    TQWidget* w = cell( r, c );
	    if ( !w )
		continue;
	    int cr = countRow( r, c);
	    int stretch = 0;
	    for ( i = r-1; i >= 0; i-- ) {
		if ( cell( i, c ) )
		    break;
		if ( countRow( i, c ) < cr )
		    break;
		if ( isWidgetEndRow( i ) )
		    break;
		if ( isWidgetStartRow( i ) ) {
		    stretch = r - i;
		    break;
		}
	    }
	    if ( stretch ) {
		for ( i = 0; i < stretch; i++ )
		    setRow( r-i-1, c, w, cr );
	    }
	}
    }
}

void Grid::extendDown()
{
    int r,c,i;
    for ( r = nrows - 2; r >= 0; r-- ) {
	for ( c = 0; c < ncols; c++ ) {
	    TQWidget* w = cell( r, c );
	    if ( !w )
		continue;
	    int cr = countRow( r, c);
	    int stretch = 0;
	    for ( i = r+1; i < nrows; i++ ) {
		if ( cell( i, c ) )
		    break;
		if ( countRow( i, c ) < cr )
		    break;
		if ( isWidgetStartRow( i ) )
		    break;
		if ( isWidgetEndRow( i ) ) {
		    stretch = i - r;
		    break;
		}
	    }
	    if ( stretch ) {
		for ( i = 0; i < stretch; i++ )
		    setRow( r+i+1, c, w, cr );
	    }
	}
    }

}

void Grid::simplify()
{
    extendLeft();
    extendRight();
    extendUp();
    extendDown();
    merge();
}


void Grid::merge()
{
    int r,c;
    for ( c = 0; c < ncols; c++ )
	cols[c] = FALSE;

    for ( r = 0; r < nrows; r++ )
	rows[r] = FALSE;

    for ( c = 0; c < ncols; c++ ) {
	for ( r = 0; r < nrows; r++ ) {
	    if ( isWidgetTopLeft( r, c ) ) {
		rows[r] = TRUE;
		cols[c] = TRUE;
	    }
	}
    }
}

bool Grid::locateWidget( TQWidget* w, int& row, int& col, int& rowspan, int & colspan )
{
    int r,c, r2, c2;
    for ( c = 0; c < ncols; c++ ) {
	for ( r = 0; r < nrows; r++ ) {
	    if ( cell( r, c ) == w  ) {
		row = 0;
		for ( r2 = 1; r2 <= r; r2++ ) {
		    if ( rows[ r2-1 ] )
			row++;
		}
		col = 0;
		for ( c2 = 1; c2 <= c; c2++ ) {
		    if ( cols[ c2-1 ] )
			col++;
		}
		rowspan = 0;
		for ( r2 = r ; r2 < nrows && cell( r2, c) == w; r2++ ) {
		    if ( rows[ r2 ] )
			rowspan++;
		}
		colspan = 0;
		for ( c2 = c; c2 < ncols && cell( r, c2) == w; c2++ ) {
		    if ( cols[ c2] )
			colspan++;
		}
		return TRUE;
	    }
	}
    }
    return FALSE;
}




GridLayout::GridLayout( const TQWidgetList &wl, TQWidget *p, FormWindow *fw, TQWidget *lb, const TQSize &res, bool doSetup )
    : Layout( wl, p, fw, lb, doSetup ), resolution( res )
{
    grid = 0;
    if ( doSetup )
	setup();
}

GridLayout::~GridLayout()
{
    delete grid;
}

void GridLayout::doLayout()
{
    bool needMove, needRetqparent;
    if ( !prepareLayout( needMove, needRetqparent ) )
	return;

    QDesignerGridLayout *tqlayout = (QDesignerGridLayout*)WidgetFactory::createLayout( layoutBase, 0, WidgetFactory::Grid );

    if ( !grid )
	buildGrid();

    TQWidget* w;
    int r, c, rs, cs;
    for ( w = widgets.first(); w; w = widgets.next() ) {
	if ( grid->locateWidget( w, r, c, rs, cs) ) {
	    if ( needRetqparent && TQT_BASE_OBJECT(w->tqparent()) != TQT_BASE_OBJECT(layoutBase) )
		w->reparent( layoutBase, 0, TQPoint( 0, 0 ), FALSE );
	    if ( rs * cs == 1 ) {
		tqlayout->addWidget( w, r, c, ::tqqt_cast<Spacer*>(w) ? ( (Spacer*)w )->tqalignment() : 0 );
	    } else {
		tqlayout->addMultiCellWidget( w, r, r+rs-1, c, c+cs-1, ::tqqt_cast<Spacer*>(w) ? ( (Spacer*)w )->tqalignment() : 0 );
	    }
	    if ( ::tqqt_cast<TQLayoutWidget*>(w) )
		( (TQLayoutWidget*)w )->updateSizePolicy();
	    w->show();
	} else {
	    qWarning("ooops, widget '%s' does not fit in tqlayout", w->name() );
	}
    }
    finishLayout( needMove, tqlayout );
}

void GridLayout::setup()
{
    Layout::setup();
    buildGrid();
}

void GridLayout::buildGrid()
{
    if ( !widgets.count() )
	return;

    // Pixel to cell conversion:
    // By keeping a list of start'n'stop values (x & y) for each widget,
    // it is possible to create a very small grid of cells to represent
    // the widget tqlayout.
    // -----------------------------------------------------------------

    // We need a list of both start and stop values for x- & y-axis
    TQValueVector<int> x( widgets.count()*2 );
    TQValueVector<int> y( widgets.count()*2 );

    // Using push_back would look nicer, but operator[] is much faster
    int index  = 0;
    TQWidget* w = 0;
    for ( w = widgets.first(); w; w = widgets.next() ) {
	TQRect widgetPos = w->tqgeometry();
	x[index]   = widgetPos.left();
	x[index+1] = widgetPos.right();
	y[index]   = widgetPos.top();
	y[index+1] = widgetPos.bottom();
	index += 2;
    }

    qHeapSort(x);
    qHeapSort(y);

    // Remove duplicate x enteries (Remove next, if equal to current)
    if ( !x.empty() ) {
	for (TQValueVector<int>::iterator current = x.begin() ;
	     (current != x.end()) && ((current+1) != x.end()) ; )
	    if ( (*current == *(current+1)) )
		x.erase(current+1);
	    else
		current++;
    }

    // Remove duplicate y enteries (Remove next, if equal to current)
    if ( !y.empty() ) {
	for (TQValueVector<int>::iterator current = y.begin() ;
	     (current != y.end()) && ((current+1) != y.end()) ; )
	    if ( (*current == *(current+1)) )
		y.erase(current+1);
	    else
		current++;
    }

    // Create the smallest grid possible to represent the current tqlayout
    // Since no widget will be placed in the last row and column, we'll
    // skip them to increase speed even further
    delete grid;
    grid = new Grid( y.size()-1, x.size()-1 );

    // Mark the cells in the grid that contains a widget
    for ( w = widgets.first(); w; w = widgets.next() ) {
	TQRect c(0,0,0,0), widgetPos = w->tqgeometry();
	// From left til right (not including)
	for (uint cw=0; cw<x.size(); cw++) {
	    if ( x[cw] == widgetPos.left() )
		c.setLeft(cw);
	    if ( x[cw] <  widgetPos.right())
		c.setRight(cw);
	}
	// From top til bottom (not including)
	for (uint ch=0; ch<y.size(); ch++) {
	    if ( y[ch] == widgetPos.top()    )
		c.setTop(ch);
	    if ( y[ch] <  widgetPos.bottom() )
		c.setBottom(ch);
	}
	grid->setCells(c, w); // Mark cellblock
    }
    grid->simplify();
}







Spacer::Spacer( TQWidget *tqparent, const char *name )
    : TQWidget( tqparent, name, WMouseNoMask ),
      orient(Qt::Vertical ), interactive(TRUE), sh( TQSize(20,20) )
{
    setSizeType( Expanding );
    setAutoMask( TRUE );
}

void Spacer::paintEvent( TQPaintEvent * )
{
    TQPainter p( this );
    p.setPen( TQt::blue );

    if ( orient ==Qt::Horizontal ) {
	const int dist = 3;
	const int amplitude = TQMIN( 3, height() / 3 );
	const int base = height() / 2;
	int i = 0;
	p.setPen( white );
	for ( i = 0; i < width() / 3 +2; ++i )
	    p.drawLine( i * dist, base - amplitude, i * dist + dist / 2, base + amplitude );
	p.setPen( blue );
	for ( i = 0; i < width() / 3 +2; ++i )
	    p.drawLine( i * dist + dist / 2, base + amplitude, i * dist + dist, base - amplitude );
	p.drawLine( 0, 0, 0, height() );
	p.drawLine( width() - 1, 0, width() - 1, height());
    } else {
	const int dist = 3;
	const int amplitude = TQMIN( 3, width() / 3 );
	const int base = width() / 2;
	int i = 0;
	p.setPen( white );
	for ( i = 0; i < height() / 3 +2; ++i )
	    p.drawLine( base - amplitude, i * dist, base + amplitude,i * dist + dist / 2 );
	p.setPen( blue );
	for ( i = 0; i < height() / 3 +2; ++i )
	    p.drawLine( base + amplitude, i * dist + dist / 2, base - amplitude, i * dist + dist );
	p.drawLine( 0, 0, width(), 0 );
	p.drawLine( 0, height() - 1, width(), height() - 1 );
    }
}

void Spacer::resizeEvent( TQResizeEvent* e)
{
    TQWidget::resizeEvent( e );
    if ( !parentWidget() || WidgetFactory::layoutType( parentWidget() ) == WidgetFactory::NoLayout )
	sh = size();
}

void Spacer::updateMask()
{
    TQRegion r( rect() );
    if ( orient ==Qt::Horizontal ) {
	const int amplitude = TQMIN( 3, height() / 3 );
	const int base = height() / 2;
	r = r.subtract( TQRect(1, 0, width() - 2, base - amplitude ) );
	r = r.subtract( TQRect(1, base + amplitude, width() - 2, height() - base - amplitude ) );
    } else {
	const int amplitude = TQMIN( 3, width() / 3 );
	const int base = width() / 2;
	r = r.subtract( TQRect(0, 1, base - amplitude, height() - 2 ) );
	r = r.subtract( TQRect( base + amplitude, 1, width() - base - amplitude, height() - 2 ) );
    }
    setMask( r );
}

void Spacer::setSizeType( SizeType t )
{
    TQSizePolicy sizeP;
    if ( orient ==Qt::Vertical )
	sizeP = TQSizePolicy( TQSizePolicy::Minimum, (TQSizePolicy::SizeType)t );
    else
	sizeP = TQSizePolicy( (TQSizePolicy::SizeType)t, TQSizePolicy::Minimum );
    tqsetSizePolicy( sizeP );
}


Spacer::SizeType Spacer::sizeType() const
{
    if ( orient ==Qt::Vertical )
	return (SizeType)tqsizePolicy().verData();
    return (SizeType)tqsizePolicy().horData();
}

int Spacer::tqalignment() const
{
    if ( orient ==Qt::Vertical )
	return AlignHCenter;
    return AlignVCenter;
}

TQSize Spacer::tqminimumSize() const
{
    TQSize s = TQSize( 20,20 );
    if ( sizeType() == Expanding )
	if ( orient ==Qt::Vertical )
	    s.rheight() = 0;
	else
	    s.rwidth() = 0;
    return s;
}

TQSize Spacer::tqsizeHint() const
{
    return sh;
}


void Spacer::setSizeHint( const TQSize &s )
{
    sh = s;
    if ( !parentWidget() || WidgetFactory::layoutType( parentWidget() ) == WidgetFactory::NoLayout )
	resize( tqsizeHint() );
    updateGeometry();
}

Qt::Orientation Spacer::orientation() const
{
    return orient;
}

void Spacer::setOrientation( Qt::Orientation o )
{
    if ( orient == o )
	return;

    SizeType st = sizeType();
    orient = o;
    setSizeType( st );
    if ( interactive ) {
	sh = TQSize( sh.height(), sh.width() );
	if (!parentWidget() || WidgetFactory::layoutType( parentWidget() ) == WidgetFactory::NoLayout )
	    resize( height(), width() );
    }
    updateMask();
    update();
    updateGeometry();
}


void QDesignerGridLayout::addWidget( TQWidget *w, int row, int col, int align_ )
{
    items.insert( w, Item(row, col, 1, 1) );
    TQGridLayout::addWidget( w, row, col, align_ );
}

void QDesignerGridLayout::addMultiCellWidget( TQWidget *w, int fromRow, int toRow,
					      int fromCol, int toCol, int align_ )
{
    items.insert( w, Item(fromRow, fromCol, toRow - fromRow + 1, toCol - fromCol +1) );
    TQGridLayout::addMultiCellWidget( w, fromRow, toRow, fromCol, toCol, align_ );
}