diff options
author | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-02-10 19:54:13 +0000 |
---|---|---|
committer | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-02-10 19:54:13 +0000 |
commit | da7847adb43726079c7a4be1f06acbebe0bdde57 (patch) | |
tree | 0138139a2d4a3a213319cc5e23e409b90a9fcc6b /src/flowlayout.cpp | |
download | kima-da7847adb43726079c7a4be1f06acbebe0bdde57.tar.gz kima-da7847adb43726079c7a4be1f06acbebe0bdde57.zip |
Added old abandoned KDE3 version of Kima
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/kima@1088422 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'src/flowlayout.cpp')
-rw-r--r-- | src/flowlayout.cpp | 311 |
1 files changed, 311 insertions, 0 deletions
diff --git a/src/flowlayout.cpp b/src/flowlayout.cpp new file mode 100644 index 0000000..a0ead97 --- /dev/null +++ b/src/flowlayout.cpp @@ -0,0 +1,311 @@ +/*************************************************************************** + * Copyright (C) 2005 by Ken Werner * + * ken.werner@web.de * + * * + * 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 <qlabel.h> // debugging + +class FlowLayoutIterator :public QGLayoutIterator{ +public: + FlowLayoutIterator( QPtrList<QLayoutItem>* layoutItems ): + index(0), + mLayoutItems(layoutItems){ + } + uint count() const; + QLayoutItem* current(); + QLayoutItem* next(); + QLayoutItem* takeCurrent(); +private: + int index; + QPtrList<QLayoutItem>* mLayoutItems; +}; +QLayoutItem* FlowLayoutIterator::current(){ + return index < int(mLayoutItems->count()) ? mLayoutItems->at(index) : 0; + /*if(index < int(mLayoutItems->count())){ + QLayoutItem* 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; + }*/ +} +QLayoutItem* FlowLayoutIterator::next(){ + index++; + //kdDebug() << "FlowLayoutIterator::next, index: " << index << endl; + return current(); +} +QLayoutItem* FlowLayoutIterator::takeCurrent(){ + return index < int(mLayoutItems->count()) ? mLayoutItems->take(index) : 0; + /*if(index < int(mLayoutItems->count())){ + QLayoutItem* 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( QWidget* parent, Qt::Orientation orientation, int border, int space, const char* name ) + : QLayout( parent, border, space, name ), + mOrientation(orientation), mLastItem(NULL){ +} +FlowLayout::FlowLayout( QLayout* parent, Qt::Orientation orientation, int space, const char* name ) + : QLayout( parent, space, name ), + mOrientation(orientation), mLastItem(NULL){ +} +FlowLayout::FlowLayout( Qt::Orientation orientation, int space, const char* name ) + : QLayout( space, name ), + mOrientation(orientation), mLastItem(NULL){ +} + + +FlowLayout::~FlowLayout(){ + deleteAllItems(); +} + +int FlowLayout::heightForWidth( int w ) const{ + FlowLayout* mthis = (FlowLayout*)this; + int h = mthis->doLayout( QRect(0,0,w,0), TRUE ); + return h; +} + +int FlowLayout::widthForHeight( int h ) const{ + FlowLayout* mthis = (FlowLayout*)this; + int w = mthis->doLayout( QRect(0,0,0,h), TRUE ); + return w; +} + +void FlowLayout::addItem(QLayoutItem* item){ + //kdDebug() << "FlowLayout::addItem: " << (static_cast<QLabel*>(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 = 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. + QLayoutItem * qli = mLayoutItems.last(); + while(qli && mSources[qli]->getPosition() > src->getPosition()) + qli = mLayoutItems.prev(); + mLayoutItems.insert(mLayoutItems.at()+1, mLastItem); +} + +void FlowLayout::remove(QWidget* widget){ + //kdDebug() << "FlowLayout::remove: " << (static_cast<QLabel*>(widget))->text() << endl; + widget->hide(); + QPtrListIterator<QLayoutItem> it(mLayoutItems); + while(it.current() != NULL) { + if(it.current()->widget() == widget) { + mSources.erase(it.current()); + mLayoutItems.remove(it.current()); + // removes and deletes only the QLayoutItem + // (QWidgetItem) + QLayout::remove(widget); + break; + } + ++it; + } +} + +uint FlowLayout::count(){ + return mLayoutItems.count(); +} + +bool FlowLayout::moveItem(const QLayoutItem* which, const QLayoutItem* 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(KConfig * inKConfig){ + //kdDebug() << "updating all positions..." << endl; + int pos = 0; + QPtrListIterator<QLayoutItem> it(mLayoutItems); + while(it.current() != NULL) { + mSources[it.current()]->setPosition(pos, inKConfig); + ++it; + ++pos; + } + //kdDebug() << "positions updated!" << endl; +} + +bool FlowLayout::hasHeightForWidth() const{ + return mOrientation != Qt::Horizontal; +} + +bool FlowLayout::hasWidthForHeight() const{ + return mOrientation == Qt::Horizontal; +} + +QSize FlowLayout::sizeHint() const{ + //return minimumSize(); + QSize size(0,0); + QPtrListIterator<QLayoutItem> it(mLayoutItems); + QLayoutItem *o; + while((o=it.current()) != 0){ + ++it; + size = size.expandedTo( o->sizeHint() ); + } + return size; +} + +QSize FlowLayout::minimumSize() const{ + QSize size(0,0); + QPtrListIterator<QLayoutItem> it(mLayoutItems); + QLayoutItem *o; + while((o=it.current()) != 0){ + ++it; + size = size.expandedTo(o->minimumSize()); + } + return size; +} + +QSizePolicy::ExpandData FlowLayout::expanding() const{ + return QSizePolicy::NoDirection; +} + +QLayoutIterator FlowLayout::iterator(){ + return QLayoutIterator(new FlowLayoutIterator(&mLayoutItems)); +} + +Qt::Orientation FlowLayout::getOrientation() const{ + return mOrientation; +} + +void FlowLayout::setOrientation(Qt::Orientation orientation){ + mOrientation = orientation; +} + +void FlowLayout::setGeometry( const QRect& rect ){ + QLayout::setGeometry( rect ); + doLayout( rect ); +} + +int FlowLayout::doLayout( const QRect& rect, bool testonly ){ + if(mOrientation == Qt::Horizontal) + return doLayoutHorizontal(rect, testonly); + else + return doLayoutVertical(rect, testonly); +} + +int FlowLayout::doLayoutHorizontal( const QRect& 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 + QPtrListIterator<QLayoutItem> it(mLayoutItems); + QLayoutItem* layoutItem; + QPtrList<QLayoutItem> 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( QRect( QPoint( 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(QLayoutItem* item = column.first(); item; item = column.next()){ + rWidth = QMAX( 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(QLayoutItem* item = column.first(); item; item = column.next()){ + QRect r = item->geometry(); + item->setGeometry( QRect(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 = QMAX( width, layoutItem->sizeHint().width() ); + } + return x + width - rect.x(); // width +} + +int FlowLayout::doLayoutVertical( const QRect& rect, bool testOnly ){ + int x = rect.x(); + int y = rect.y(); + int height = 0; // height of this line so far + QPtrListIterator<QLayoutItem> it(mLayoutItems); + QLayoutItem* 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(QRect(x, y, rect.right(), itemHeight)); + x = nextX; + height = QMAX(height, itemHeight); + } + return y + height - rect.y(); // height +} |