You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
269 lines
6.6 KiB
269 lines
6.6 KiB
15 years ago
|
/****************************************************************************
|
||
13 years ago
|
** $Id: buttonflowtqlayout.cpp 272 2005-05-18 08:12:51Z emw $
|
||
15 years ago
|
**
|
||
13 years ago
|
** Implementing your own layout: flow example
|
||
15 years ago
|
**
|
||
|
** Copyright (C) 1996 by Trolltech AS. All rights reserved.
|
||
|
**
|
||
14 years ago
|
** This file is part of an example program for TQt. This example
|
||
15 years ago
|
** 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 */
|
||
14 years ago
|
class ButtonFlowLayoutIterator :public TQGLayoutIterator
|
||
15 years ago
|
{
|
||
|
public:
|
||
14 years ago
|
ButtonFlowLayoutIterator( TQPtrList<TQLayoutItem> *l ) :idx(0), list(l) {}
|
||
15 years ago
|
uint count() const;
|
||
14 years ago
|
TQLayoutItem *current();
|
||
|
TQLayoutItem *next();
|
||
|
TQLayoutItem *takeCurrent();
|
||
15 years ago
|
|
||
|
private:
|
||
|
int idx;
|
||
14 years ago
|
TQPtrList<TQLayoutItem> *list;
|
||
15 years ago
|
|
||
|
};
|
||
|
|
||
|
uint ButtonFlowLayoutIterator::count() const
|
||
|
{
|
||
|
return list->count();
|
||
|
}
|
||
|
|
||
14 years ago
|
TQLayoutItem *ButtonFlowLayoutIterator::current()
|
||
15 years ago
|
{
|
||
|
return idx < int(count()) ? list->at(idx) : 0;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQLayoutItem *ButtonFlowLayoutIterator::next()
|
||
15 years ago
|
{
|
||
|
idx++; return current();
|
||
|
}
|
||
|
|
||
14 years ago
|
TQLayoutItem *ButtonFlowLayoutIterator::takeCurrent()
|
||
15 years ago
|
{
|
||
|
return idx < int(count()) ? list->take( idx ) : 0;
|
||
|
}
|
||
|
|
||
|
/**************************************************************/
|
||
|
|
||
13 years ago
|
ButtonFlowLayout::ButtonFlowLayout( TQWidget *parent, int margin, int spacing,
|
||
15 years ago
|
const char *name )
|
||
13 years ago
|
: TQLayout( parent, margin, spacing, name ),
|
||
15 years ago
|
cached_width(0)
|
||
|
{
|
||
|
}
|
||
|
|
||
14 years ago
|
ButtonFlowLayout::ButtonFlowLayout( TQLayout* parentLayout, int spacing, const char *name )
|
||
|
: TQLayout( parentLayout, spacing, name ),
|
||
15 years ago
|
cached_width(0)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
ButtonFlowLayout::ButtonFlowLayout( int spacing, const char *name )
|
||
14 years ago
|
: TQLayout( spacing, name ),
|
||
15 years ago
|
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;
|
||
14 years ago
|
int h = mthis->doLayout( TQRect(0,0,w,0), TRUE );
|
||
15 years ago
|
mthis->cached_hfw = h;
|
||
|
mthis->cached_width = w;
|
||
|
return h;
|
||
|
}
|
||
|
return cached_hfw;
|
||
|
}
|
||
|
|
||
13 years ago
|
void ButtonFlowLayout::addItem( TQLayoutItem *item)
|
||
15 years ago
|
{
|
||
14 years ago
|
list.append( TQT_TQLAYOUTITEM(item) );
|
||
15 years ago
|
}
|
||
|
|
||
|
bool ButtonFlowLayout::hasHeightForWidth() const
|
||
|
{
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
13 years ago
|
TQSize ButtonFlowLayout::sizeHint() const
|
||
15 years ago
|
{
|
||
13 years ago
|
return minimumSize();
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
TQSizePolicy::ExpandData ButtonFlowLayout::expanding() const
|
||
15 years ago
|
{
|
||
14 years ago
|
return TQ_SPNoDirection;
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
TQLayoutIterator ButtonFlowLayout::iterator()
|
||
15 years ago
|
{
|
||
14 years ago
|
// [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
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void ButtonFlowLayout::setGeometry( const TQRect &r )
|
||
15 years ago
|
{
|
||
14 years ago
|
TQLayout::setGeometry( r );
|
||
15 years ago
|
doLayout( r );
|
||
|
}
|
||
|
|
||
14 years ago
|
int ButtonFlowLayout::doLayout( const TQRect &r, bool testonly )
|
||
15 years ago
|
{
|
||
13 years ago
|
/* kdDebug() << "buttonflowlayout::doLayout ("
|
||
15 years ago
|
<< 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();
|
||
|
|
||
14 years ago
|
TQPtrListIterator<TQLayoutItem> it(list);
|
||
|
TQLayoutItem *o;
|
||
15 years ago
|
|
||
|
// get the width of the biggest Button
|
||
|
|
||
|
it.toFirst();
|
||
|
while ( (o=it.current()) != 0 ) {
|
||
|
++it;
|
||
13 years ago
|
buttonWidth = TQMAX( buttonWidth, o->sizeHint().width() );
|
||
|
buttonHeight = TQMAX( buttonHeight, o->sizeHint().height() );
|
||
15 years ago
|
}
|
||
|
|
||
|
// 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)
|
||
14 years ago
|
o->setGeometry( TQRect( TQPoint( btnLeft, (int)rint(y) ),
|
||
|
TQSize( btnRight - btnLeft + 1,
|
||
15 years ago
|
buttonHeight) )
|
||
|
);
|
||
|
|
||
|
x += buttonWidth + spacing();
|
||
14 years ago
|
h = TQMAX( h, buttonHeight );
|
||
15 years ago
|
}
|
||
|
|
||
|
int ret = (int)rint(y + h + deltaH) - r.y();
|
||
|
|
||
|
// kdDebug() << "ButtonFlowLayout::doLayout() = " << ret << endl;
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
|
||
13 years ago
|
TQSize ButtonFlowLayout::minimumSize() const
|
||
15 years ago
|
{
|
||
13 years ago
|
return minimumSize(geometry().size());
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
13 years ago
|
TQSize ButtonFlowLayout::minimumSize(const TQSize &r) const
|
||
15 years ago
|
{
|
||
14 years ago
|
TQSize s(0, 0);
|
||
15 years ago
|
|
||
14 years ago
|
for (TQPtrListIterator<TQLayoutItem> it(list); it.current(); ++it) {
|
||
|
TQLayoutItem *o = it.current();
|
||
13 years ago
|
s = s.expandedTo( o->sizeHint()); //minimumSize() );
|
||
15 years ago
|
}
|
||
|
|
||
|
s.setHeight(heightForWidth(r.width()));
|
||
|
|
||
|
return s;
|
||
|
}
|
||
14 years ago
|
|
||
|
#ifdef USE_QT4
|
||
|
/*!
|
||
|
\reimp
|
||
|
*/
|
||
|
int ButtonFlowLayout::count() const {
|
||
|
return list.count();
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
\reimp
|
||
|
*/
|
||
|
TQLayoutItem* ButtonFlowLayout::itemAt(int index) const {
|
||
13 years ago
|
return index >= 0 && index < list.count() ? (const_cast<TQPtrList<TQLayoutItem>&>(list).at(index)) : 0;
|
||
14 years ago
|
}
|
||
|
|
||
|
/*!
|
||
|
\reimp
|
||
|
*/
|
||
|
TQLayoutItem* ButtonFlowLayout::takeAt(int index) {
|
||
|
if (index < 0 || index >= list.count())
|
||
|
return 0;
|
||
13 years ago
|
TQLayoutItem *item = list.at(index);
|
||
|
list.remove(list.at(index));
|
||
14 years ago
|
delete item;
|
||
|
|
||
|
invalidate();
|
||
|
return item;
|
||
|
}
|
||
13 years ago
|
#endif // USE_QT4
|