src/gui/kernel/qlayoutitem.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the QtGui module of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #include "qlayout.h"
00025 
00026 #include "qapplication.h"
00027 #include "qlayoutengine_p.h"
00028 #include "qmenubar.h"
00029 #include "qtoolbar.h"
00030 #include "qevent.h"
00031 #include "qstyle.h"
00032 #include "qvariant.h"
00033 
00037 QSizePolicy::operator QVariant() const
00038 {
00039     return QVariant(QVariant::SizePolicy, this);
00040 }
00041 
00149 void QLayoutItem::setAlignment(Qt::Alignment alignment)
00150 {
00151     align = alignment;
00152 }
00153 
00229 void QSpacerItem::changeSize(int w, int h, QSizePolicy::Policy hPolicy,
00230                              QSizePolicy::Policy vPolicy)
00231 {
00232     width = w;
00233     height = h;
00234     sizeP = QSizePolicy(hPolicy, vPolicy);
00235 }
00236 
00246 QLayoutItem::~QLayoutItem()
00247 {
00248 }
00249 
00253 void QLayoutItem::invalidate()
00254 {
00255 }
00256 
00261 QLayout * QLayoutItem::layout()
00262 {
00263     return 0;
00264 }
00265 
00270 QSpacerItem * QLayoutItem::spacerItem()
00271 {
00272     return 0;
00273 }
00274 
00278 QLayout * QLayout::layout()
00279 {
00280     return this;
00281 }
00282 
00286 QSpacerItem * QSpacerItem::spacerItem()
00287 {
00288     return this;
00289 }
00290 
00295 QWidget * QLayoutItem::widget()
00296 {
00297     return 0;
00298 }
00299 
00303 QWidget *QWidgetItem::widget()
00304 {
00305     return wid;
00306 }
00307 
00318 bool QLayoutItem::hasHeightForWidth() const
00319 {
00320     return false;
00321 }
00322 
00328 int QLayoutItem::minimumHeightForWidth(int w) const
00329 {
00330     return heightForWidth(w);
00331 }
00332 
00333 
00364 int QLayoutItem::heightForWidth(int /* w */) const
00365 {
00366     return -1;
00367 }
00368 
00372 void QSpacerItem::setGeometry(const QRect &r)
00373 {
00374     rect = r;
00375 }
00376 
00380 void QWidgetItem::setGeometry(const QRect &r)
00381 {
00382     if (isEmpty())
00383         return;
00384     QSize s = r.size().boundedTo(qSmartMaxSize(this));
00385     int x = r.x();
00386     int y = r.y();
00387     if (align & (Qt::AlignHorizontal_Mask | Qt::AlignVertical_Mask)) {
00388         QSize pref = wid->sizeHint().expandedTo(wid->minimumSize()); //###
00389         if (align & Qt::AlignHorizontal_Mask)
00390             s.setWidth(qMin(s.width(), pref.width()));
00391         if (align & Qt::AlignVertical_Mask) {
00392             if (hasHeightForWidth())
00393                 s.setHeight(qMin(s.height(), heightForWidth(s.width())));
00394             else
00395                 s.setHeight(qMin(s.height(), pref.height()));
00396         }
00397     }
00398     Qt::Alignment alignHoriz = QStyle::visualAlignment(wid->layoutDirection(), align);
00399     if (alignHoriz & Qt::AlignRight)
00400         x = x + (r.width() - s.width());
00401     else if (!(alignHoriz & Qt::AlignLeft))
00402         x = x + (r.width() - s.width()) / 2;
00403 
00404     if (align & Qt::AlignBottom)
00405         y = y + (r.height() - s.height());
00406     else if (!(align & Qt::AlignTop))
00407         y = y + (r.height() - s.height()) / 2;
00408 
00409     wid->setGeometry(x, y, s.width(), s.height());
00410 }
00411 
00415 QRect QSpacerItem::geometry() const
00416 {
00417     return rect;
00418 }
00419 
00423 QRect QWidgetItem::geometry() const
00424 {
00425     return wid->geometry();
00426 }
00427 
00428 
00432 bool QWidgetItem::hasHeightForWidth() const
00433 {
00434     if (isEmpty())
00435         return false;
00436     if (wid->layout())
00437         return wid->layout()->hasHeightForWidth();
00438     return wid->sizePolicy().hasHeightForWidth();
00439 }
00440 
00444 int QWidgetItem::heightForWidth(int w) const
00445 {
00446     if (isEmpty())
00447         return -1;
00448     int hfw;
00449     if (wid->layout())
00450         hfw = wid->layout()->totalHeightForWidth(w);
00451     else
00452         hfw = wid->heightForWidth(w);
00453 
00454     if (hfw > wid->maximumHeight())
00455         hfw = wid->maximumHeight();
00456     if (hfw < wid->minimumHeight())
00457         hfw = wid->minimumHeight();
00458     if (hfw < 0)
00459         hfw = 0;
00460     return hfw;
00461 }
00462 
00466 Qt::Orientations QSpacerItem::expandingDirections() const
00467 {
00468     return sizeP.expandingDirections();
00469 }
00470 
00474 Qt::Orientations QWidgetItem::expandingDirections() const
00475 {
00476     if (isEmpty())
00477         return Qt::Orientations(0);
00478 
00479     Qt::Orientations e = wid->sizePolicy().expandingDirections();
00480     /*
00481       If the layout is expanding, we make the widget expanding, even if
00482       its own size policy isn't expanding. This behavior should be
00483       reconsidered in Qt 4.0. (###)
00484     */
00485     if (wid->layout()) {
00486         if (wid->sizePolicy().horizontalPolicy() & QSizePolicy::GrowFlag
00487                 && (wid->layout()->expandingDirections() & Qt::Horizontal))
00488             e |= Qt::Horizontal;
00489         if (wid->sizePolicy().verticalPolicy() & QSizePolicy::GrowFlag
00490                 && (wid->layout()->expandingDirections() & Qt::Vertical))
00491             e |= Qt::Vertical;
00492     }
00493 
00494     if (align & Qt::AlignHorizontal_Mask)
00495         e &= ~Qt::Horizontal;
00496     if (align & Qt::AlignVertical_Mask)
00497         e &= ~Qt::Vertical;
00498     return e;
00499 }
00500 
00504 QSize QSpacerItem::minimumSize() const
00505 {
00506     return QSize(sizeP.horizontalPolicy() & QSizePolicy::ShrinkFlag ? 0 : width,
00507                  sizeP.verticalPolicy() & QSizePolicy::ShrinkFlag ? 0 : height);
00508 }
00509 
00513 QSize QWidgetItem::minimumSize() const
00514 {
00515     if (isEmpty())
00516         return QSize(0, 0);
00517     return qSmartMinSize(this);
00518 }
00519 
00523 QSize QSpacerItem::maximumSize() const
00524 {
00525     return QSize(sizeP.horizontalPolicy() & QSizePolicy::GrowFlag ? QLAYOUTSIZE_MAX : width,
00526                  sizeP.verticalPolicy() & QSizePolicy::GrowFlag ? QLAYOUTSIZE_MAX : height);
00527 }
00528 
00532 QSize QWidgetItem::maximumSize() const
00533 {
00534     if (isEmpty()) {
00535         return QSize(0, 0);
00536     } else {
00537         return qSmartMaxSize(this, align);
00538     }
00539 }
00540 
00544 QSize QSpacerItem::sizeHint() const
00545 {
00546     return QSize(width, height);
00547 }
00548 
00552 QSize QWidgetItem::sizeHint() const
00553 {
00554     QSize s;
00555     if (isEmpty()) {
00556         s = QSize(0, 0);
00557     } else {
00558         s = wid->sizeHint().expandedTo(wid->minimumSizeHint());
00559         if (wid->sizePolicy().horizontalPolicy() == QSizePolicy::Ignored)
00560             s.setWidth(0);
00561         if (wid->sizePolicy().verticalPolicy() == QSizePolicy::Ignored)
00562             s.setHeight(0);
00563         s = s.boundedTo(wid->maximumSize())
00564             .expandedTo(wid->minimumSize());
00565     }
00566     return s;
00567 }
00568 
00572 bool QSpacerItem::isEmpty() const
00573 {
00574     return true;
00575 }
00576 
00582 bool QWidgetItem::isEmpty() const
00583 {
00584     return wid->isHidden() || wid->isWindow();
00585 }
00586 

Generated on Thu Mar 15 11:55:11 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1