• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

kalarm/lib

packedlayout.cpp

Go to the documentation of this file.
00001 /*
00002  *  packedlayout.cpp  -  layout to pack items into rows
00003  *  Program:  kalarm
00004  *  Copyright © 2007 by David Jarvie <djarvie@kde.org>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 #include "kalarm.h"   //krazy:exclude=includes (kalarm.h must be first)
00021 #include "packedlayout.h"
00022 
00023 
00024 PackedLayout::PackedLayout(QWidget* parent, Qt::Alignment alignment)
00025     : QLayout(parent),
00026       mAlignment(alignment),
00027       mWidthCached(0)
00028 {
00029 }
00030 
00031 PackedLayout::PackedLayout(Qt::Alignment alignment)
00032     : QLayout(),
00033       mAlignment(alignment),
00034       mWidthCached(0)
00035 {
00036 }
00037 
00038 PackedLayout::~PackedLayout()
00039 {
00040     while (!mItems.isEmpty())
00041         delete mItems.takeFirst();
00042 }
00043 
00044 /******************************************************************************
00045  * Inserts a button into the group.
00046  */
00047 void PackedLayout::addItem(QLayoutItem* item)
00048 {
00049     mWidthCached = 0;
00050     mItems.append(item);
00051 }
00052 
00053 QLayoutItem* PackedLayout::itemAt(int index) const
00054 {
00055     return (index >= 0 && index < mItems.count()) ? mItems[index] : 0;
00056 }
00057 
00058 QLayoutItem* PackedLayout::takeAt(int index)
00059 {
00060     if (index < 0  ||  index >= mItems.count())
00061         return 0;
00062     return mItems.takeAt(index);
00063 }
00064 
00065 int PackedLayout::heightForWidth(int w) const
00066 {
00067     if (w != mWidthCached)
00068     {
00069         mHeightCached = arrange(QRect(0, 0, w, 0), false);
00070         mWidthCached = w;
00071     }
00072     return mHeightCached;
00073 }
00074 
00075 void PackedLayout::setGeometry(const QRect& rect)
00076 {
00077     QLayout::setGeometry(rect);
00078     arrange(rect, true);
00079 }
00080 
00081 // Return the maximum size of any widget.
00082 QSize PackedLayout::minimumSize() const
00083 {
00084     QSize size;
00085     for (int i = 0, end = mItems.count();  i < end;  ++i)
00086         size = size.expandedTo(mItems[i]->minimumSize());
00087     int m = margin() * 2;
00088     return QSize(size.width() + m, size.height() + m);
00089 }
00090 
00091 // Arranges widgets and returns height required.
00092 int PackedLayout::arrange(const QRect& rect, bool set) const
00093 {
00094     int x = rect.x();
00095     int y = rect.y();
00096     int yrow = 0;
00097     QList<QRect> posn;
00098     int end = mItems.count();
00099     QList<QLayoutItem*> items;
00100     for (int i = 0;  i < end;  ++i)
00101     {
00102         QLayoutItem* item = mItems[i];
00103         if (item->isEmpty())
00104             continue;
00105         QSize size = item->sizeHint();
00106         int right = x + size.width();
00107         if (right > rect.right()  &&  x > rect.x())
00108         {
00109             x = rect.x();
00110             y = y + yrow + spacing();
00111             right = x + size.width();
00112             yrow = size.height();
00113         }
00114         else
00115             yrow = qMax(yrow, size.height());
00116         items.append(item);
00117         posn.append(QRect(QPoint(x, y), size));
00118         x = right + spacing();
00119     }
00120     if (set)
00121     {
00122         int count = items.count();
00123         if (mAlignment == Qt::AlignLeft)
00124         {
00125             // Left aligned: no position adjustment needed
00126             // Set the positions of all the layout items
00127             for (int i = 0;  i < count;  ++i)
00128                 items[i]->setGeometry(posn[i]);
00129         }
00130         else
00131         {
00132             // Set the positions of all the layout items
00133             for (int i = 0;  i < count; )
00134             {
00135                 // Adjust item positions a row at a time
00136                 y = posn[i].y();
00137                 int last;   // after last item in this row
00138                 for (last = i + 1;  last < count && posn[last].y() == y;  ++last) ;
00139                 int n = last - i;   // number of items in this row
00140                 int free = rect.right() - posn[last - 1].right();
00141                 switch (mAlignment)
00142                 {
00143                     case Qt::AlignJustify:
00144                         if (n == 1)
00145                         {
00146                             items[i]->setGeometry(posn[i]);
00147                             ++i;
00148                         }
00149                         else if (n > 1)
00150                         {
00151                             for (int j = 0;  i < last;  ++j, ++i)
00152                                 items[i]->setGeometry(QRect(QPoint(posn[i].x() + (free * j)/(n - 1), y), posn[i].size()));
00153                         }
00154                         break;
00155                     case Qt::AlignHCenter:
00156                         free /= 2;
00157                         // fall through to AlignRight
00158                     case Qt::AlignRight:
00159                         for ( ;  i < last;  ++i)
00160                             items[i]->setGeometry(QRect(QPoint(posn[i].x() + free, y), posn[i].size()));
00161                         break;
00162                     default:
00163                         break;
00164                 }
00165             }
00166         }
00167     }
00168     return y + yrow - rect.y();
00169 }

kalarm/lib

Skip menu "kalarm/lib"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  •   doc
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal