summaryrefslogtreecommitdiffstats
path: root/kradio3/plugins/gui-quickbar/buttonflowlayout.cpp
blob: 419fd10bd138f07135c0d4f28e677b837dd205fe (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
/****************************************************************************
** $Id: buttonflowtqlayout.cpp 272 2005-05-18 08:12:51Z emw $
**
** Implementing your own tqlayout: flow example
**
** Copyright (C) 1996 by Trolltech AS.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
/**
   Modified 2002 by Klas Kalass (klas.kalass@gmx.de) for kradio
 */

#include <kdebug.h>

#include "buttonflowlayout.h"

/*********************************************/
/*              Iterator                     */
class ButtonFlowLayoutIterator :public TQGLayoutIterator
{
public:
    ButtonFlowLayoutIterator( TQPtrList<TQLayoutItem> *l ) :idx(0), list(l)  {}
    uint count() const;
    TQLayoutItem *current();
    TQLayoutItem *next();
    TQLayoutItem *takeCurrent();

private:
    int idx;
    TQPtrList<TQLayoutItem> *list;

};

uint ButtonFlowLayoutIterator::count() const
{
    return list->count();
}

TQLayoutItem *ButtonFlowLayoutIterator::current()
{
    return idx < int(count()) ? list->at(idx) : 0;
}

TQLayoutItem *ButtonFlowLayoutIterator::next()
{
    idx++; return current();
}

TQLayoutItem *ButtonFlowLayoutIterator::takeCurrent()
{
    return idx < int(count()) ? list->take( idx ) : 0;
}

/**************************************************************/

ButtonFlowLayout::ButtonFlowLayout( TQWidget *tqparent, int margin, int spacing,
        const char *name )
  : TQLayout( tqparent, margin, spacing, name ),
  cached_width(0)
{
}

ButtonFlowLayout::ButtonFlowLayout( TQLayout* tqparentLayout, int spacing, const char *name )
  : TQLayout( tqparentLayout, spacing, name ),
  cached_width(0)
{
}

ButtonFlowLayout::ButtonFlowLayout( int spacing, const char *name )
  : TQLayout( spacing, name ),
  cached_width(0)
{
}

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


int ButtonFlowLayout::heightForWidth( int w ) const
{
    if ( cached_width != w ) {
        //Not all C++ compilers support "mutable" yet:
        ButtonFlowLayout * mthis = (ButtonFlowLayout*)this;
        int h = mthis->doLayout( TQRect(0,0,w,0), TRUE );
        mthis->cached_hfw = h;
        mthis->cached_width = w;
        return h;
    }
    return cached_hfw;
}

void ButtonFlowLayout::addItem( QLayoutItem *item)
{
    list.append( TQT_TQLAYOUTITEM(item) );
}

bool ButtonFlowLayout::hasHeightForWidth() const
{
    return TRUE;
}

TQSize ButtonFlowLayout::tqsizeHint() const
{
    return tqminimumSize();
}

TQSizePolicy::ExpandData ButtonFlowLayout::expanding() const
{
    return TQ_SPNoDirection;
}

TQLayoutIterator ButtonFlowLayout::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 ButtonFlowLayoutIterator( &list ) );
#endif // USE_QT4
}

void ButtonFlowLayout::setGeometry( const TQRect &r )
{
    TQLayout::setGeometry( r );
    doLayout( r );
}

int ButtonFlowLayout::doLayout( const TQRect &r, bool testonly )
{
/*    kdDebug() << "buttonflowtqlayout::doLayout ("
              << r.x()     << "," << r.y()      << ","
              << r.width() << "," << r.height() << ", " << testonly << ")\n";
*/
    float x = r.x();
    float y = r.y();
    int h = 0;        //height of this line so far.
    float buttonWidth = 0;
    int   buttonHeight = 0;
    int linecount = 0;
    int totalWidth  = r.width();
    int totalHeight = r.height();

    TQPtrListIterator<TQLayoutItem> it(list);
    TQLayoutItem *o;

    // get the width of the biggest Button

    it.toFirst();
    while ( (o=it.current()) != 0 ) {
      ++it;
      buttonWidth  = TQMAX( buttonWidth,  o->tqsizeHint().width() );
      buttonHeight = TQMAX( buttonHeight, o->tqsizeHint().height() );
    }

    // calculate the optimal width
    unsigned int columns = (totalWidth + spacing()) /
                           ((int)buttonWidth + spacing());
    if (columns > it.count() ) columns = it.count();
    if (columns == 0) columns = 1; // avoid division by zero


    int rows   = (it.count() - 1) / columns + 1;
    float deltaH = (float)(totalHeight - rows * buttonHeight - (rows - 1) * spacing())
                   / (float)(rows + 1) ;
    if (deltaH < 0) deltaH = 0;

    y += deltaH;

    buttonWidth = (float)(totalWidth - spacing()*(columns-1)) / (float)columns;

/*    fprintf (stderr, "cols = %i      col-width  = %f\n"
                     "rows = %i      row-height = %i\n"
                     "w = %i         h = %i\n",
             columns, buttonWidth,
             rows, buttonHeight,
             totalWidth, totalHeight
             );
*/
    // calculate the positions and sizes
    it.toFirst();
    while ( (o = it.current()) != 0 ) {

//        fprintf (stderr, "x = %i    y = %i\n", x, (int)y);
        ++it;
        int btnRight = (int)rint(x + buttonWidth) - 1,
            btnLeft  = (int)rint(x);

        if ( btnRight > r.right() && h > 0 ) {
            x = r.x();
            btnRight = (int)rint(x + buttonWidth) - 1;
            btnLeft  = (int)rint(x);

            y += h + spacing() + deltaH;
              h = 0;
            linecount++;
        }
        if (!testonly)
              o->setGeometry( TQRect( TQPoint( btnLeft, (int)rint(y) ),
                                   TQSize(  btnRight - btnLeft + 1,
                                           buttonHeight) )
                          );

        x += buttonWidth + spacing();
        h = TQMAX( h,  buttonHeight );
    }

    int ret = (int)rint(y + h + deltaH) - r.y();

//    kdDebug() << "ButtonFlowLayout::doLayout() = " << ret << endl;
    return ret;
}


TQSize ButtonFlowLayout::tqminimumSize() const
{
    return tqminimumSize(tqgeometry().size());
}


TQSize ButtonFlowLayout::tqminimumSize(const TQSize &r) const
{
    TQSize s(0, 0);

    for (TQPtrListIterator<TQLayoutItem> it(list); it.current(); ++it) {
        TQLayoutItem *o = it.current();
        s = s.expandedTo( o->tqsizeHint()); //tqminimumSize() );
    }

    s.setHeight(heightForWidth(r.width()));

    return s;
}

#ifdef USE_QT4
/*!
    \reimp
*/
int ButtonFlowLayout::count() const {
	return list.count();
}

/*!
    \reimp
*/
TQLayoutItem* ButtonFlowLayout::itemAt(int index) const {
	return index >= 0 && index < list.count() ? (const_cast<TQPtrList<TQLayoutItem>&>(list).tqat(index)) : 0;
}

/*!
    \reimp
*/
TQLayoutItem* ButtonFlowLayout::takeAt(int index) {
	if (index < 0 || index >= list.count())
		return 0;
	TQLayoutItem *item = list.tqat(index);
	list.remove(list.tqat(index));
	delete item;

	invalidate();
	return item;
}
#endif // USE_QT4