void tst_QBoxLayout::testLayoutEngine() { QFETCH(DescrList, itemDescriptions); QFETCH(int, size); QFETCH(int, spacing); QFETCH(PosList, expectedPositions); QFETCH(SizeList, expectedSizes); QHBoxLayout box; box.setSpacing(spacing); int i; for (i = 0; i < itemDescriptions.count(); ++i) { Descr descr = itemDescriptions.at(i); LayoutItem *li = new LayoutItem(descr); box.addItem(li); box.setStretch(i, descr.stretch); } box.setGeometry(QRect(0,0,size,100)); for (i = 0; i < expectedSizes.count(); ++i) { int xSize = expectedSizes.at(i); int xPos = expectedPositions.at(i); QLayoutItem *item = box.itemAt(i); QCOMPARE(item->geometry().width(), xSize); QCOMPARE(item->geometry().x(), xPos); } }
void OverlayWidget::updatePositions() { if (!m_currentWidget || !m_currentToplevelWidget) { return; } if (!m_currentWidget->isVisible() || m_currentWidget->isHidden()) { m_widgetColor = Qt::green; } else { m_widgetColor = Qt::red; } const QPoint parentPos = m_currentWidget->mapTo(m_currentToplevelWidget, QPoint(0, 0)); m_widgetRect = QRect(parentPos.x(), parentPos.y(), m_currentWidget->width(), m_currentWidget->height()).adjusted(0, 0, -1, -1); m_layoutPath = QPainterPath(); if (m_currentWidget->layout() && qstrcmp(m_currentWidget->layout()->metaObject()->className(), "QMainWindowLayout") != 0) { const QRect layoutGeometry = m_currentWidget->layout()->geometry(); const QRect mappedOuterRect = QRect(m_currentWidget->mapTo(m_currentToplevelWidget, layoutGeometry.topLeft()), layoutGeometry.size()); QPainterPath outerPath; outerPath.addRect(mappedOuterRect.adjusted(1, 1, -2, -2)); QPainterPath innerPath; for (int i = 0; i < m_currentWidget->layout()->count(); ++i) { QLayoutItem *item = m_currentWidget->layout()->itemAt(i); if (item->widget() && !item->widget()->isVisible()) continue; const QRect mappedInnerRect = QRect(m_currentWidget->mapTo(m_currentToplevelWidget, item->geometry().topLeft()), item->geometry().size()); innerPath.addRect(mappedInnerRect); } m_layoutPath.setFillRule(Qt::OddEvenFill); m_layoutPath = outerPath.subtracted(innerPath); if (m_layoutPath.isEmpty()) { m_layoutPath = outerPath; m_layoutPath.addPath(innerPath); m_drawLayoutOutlineOnly = true; } else { m_drawLayoutOutlineOnly = false; } } update(); }
void FlagLayout::setGeometry(const QRect &rect) { int topHeight = 0; int bottomHeight = 0; QLayout::setGeometry(rect); // left side for (int i = 0; i < list.size(); ++i) { ItemWrapper *wrapper = list.at(i); QLayoutItem *item = wrapper->item; Position position = wrapper->position; if (position == TopLeft) { topHeight += spacing(); item->setGeometry(QRect(rect.x() + spacing(), topHeight, item->sizeHint().width(), item->sizeHint().height())); topHeight += item->geometry().height(); } else if (position == BottomLeft) { bottomHeight += item->geometry().height() + spacing(); item->setGeometry(QRect(rect.x() + spacing(), rect.height() - bottomHeight, item->sizeHint().width(), item->sizeHint().height())); } } // right side topHeight = 0; bottomHeight = 0; for (int i = 0; i < list.size(); ++i) { ItemWrapper *wrapper = list.at(i); QLayoutItem *item = wrapper->item; Position position = wrapper->position; int rightpos = item->sizeHint().width() + spacing(); if (position == TopRight) { topHeight += spacing(); item->setGeometry(QRect(rect.x() + rect.width() - rightpos, topHeight, item->sizeHint().width(), item->sizeHint().height())); topHeight += item->geometry().height(); } else if (position == BottomRight) { bottomHeight += item->geometry().height() + spacing(); item->setGeometry(QRect(rect.x() + rect.width() - rightpos, rect.height() - bottomHeight, item->sizeHint().width(), item->sizeHint().height())); } } }
QRect QLayoutItemProto::geometry() const { QLayoutItem *item = qscriptvalue_cast<QLayoutItem*>(thisObject()); if (item) return item->geometry(); return QRect(); }
void PreviewWidget::paintEvent( QPaintEvent * ) { int i_total = 0, i_offset = 0, i; QPainter painter( this ); QPixmap pixmaps[3]; for( int i=0; i<3; i++ ) { pixmaps[i] = QPixmap::grabWidget( bars[i], bars[i]->contentsRect() ); for( int j=0; j < bars[i]->layout()->count(); j++ ) { QLayoutItem *item = bars[i]->layout()->itemAt( j ); if ( !strcmp( item->widget()->metaObject()->className(), "QLabel" ) ) { QPainter eraser( &pixmaps[i] ); eraser.fillRect( item->geometry(), palette().background() ); eraser.end(); } } pixmaps[i] = pixmaps[i].scaled( size(), Qt::KeepAspectRatio ); } for( i=0; i<3; i++ ) i_total += pixmaps[i].size().height(); /* Draw top bars */ i = ( b_top ) ? 1 : 3; for( ; i<3; i++ ) { painter.drawPixmap( pixmaps[i].rect().translated( 0, i_offset ), pixmaps[i] ); i_offset += pixmaps[i].rect().height(); } /* Draw central area */ QRect conearea( 0, i_offset, size().width(), size().height() - i_total ); painter.fillRect( conearea, Qt::black ); QPixmap cone = QPixmap( ":/logo/vlc128.png" ); if ( ( conearea.size() - QSize(10, 10) - cone.size() ).isEmpty() ) cone = cone.scaled( conearea.size() - QSize(10, 10), Qt::KeepAspectRatio ); if ( cone.size().isValid() ) { painter.drawPixmap( ((conearea.size() - cone.size()) / 2).width(), i_offset + ((conearea.size() - cone.size()) / 2).height(), cone ); } /* Draw bottom bars */ i_offset += conearea.height(); for( i = 0 ; i< ((b_top) ? 1 : 3); i++ ) { painter.drawPixmap( pixmaps[i].rect().translated( 0, i_offset ), pixmaps[i] ); i_offset += pixmaps[i].rect().height(); } /* Draw overlay */ painter.fillRect( rect(), QColor( 255, 255, 255, 128 ) ); painter.end(); }
void FreeLayout::setGeometry(const QRect &r) { QList<QLayoutItem *>::const_iterator it, iEnd = m_items.end(); for (it = m_items.begin(); it != iEnd; ++it) { QLayoutItem *item = *it; const QRect &geom = item->geometry(); const QSize &sizeHint = item->sizeHint(); if (geom.size() != sizeHint) item->setGeometry(QRect(geom.topLeft(), sizeHint)); } }
void tst_QLayout::geometry() { // For QWindowsStyle we know that QWidgetItem::geometry() and QWidget::geometry() // should be the same. QApplication::setStyle(new QWindowsStyle); QWidget topLevel; QWidget w(&topLevel); QVBoxLayout layout(&w); SizeHinterFrame widget(QSize(100,100)); layout.addWidget(&widget); QLayoutItem *item = layout.itemAt(0); topLevel.show(); QApplication::processEvents(); QCOMPARE(item->geometry().size(), QSize(100,100)); widget.setMinimumSize(QSize(110,110)); QCOMPARE(item->geometry().size(), QSize(110,110)); widget.setMinimumSize(QSize(0,0)); widget.setMaximumSize(QSize(90,90)); widget.setSizeHint(QSize(100,100)); QCOMPARE(item->geometry().size(), QSize(90,90)); }
void TabsInkBar::refreshGeometry() { QLayoutItem *item = tabs->tabLayout()->itemAt(tabs->currentIndex()); if (item) { const QRect &r = item->geometry(); const qreal s = 1-_tween; if (QAbstractAnimation::Running != _animation->state()) { setGeometry(r.left(), r.bottom()-1, r.width(), 2); } else { const qreal left = _previousGeometry.left()*s + r.left()*_tween; const qreal width = _previousGeometry.width()*s + r.width()*_tween; setGeometry(left, r.bottom()-1, width, 2); } tabs->update(); } }
QSize DummyLayout::sizeHint() const { QRect geom, result; QList<QLayoutItem *>::const_iterator it, iEnd = m_items.end(); for (it = m_items.begin(); it != iEnd; ++it) { QLayoutItem *item = *it; geom = item->geometry(); geom.setSize(item->sizeHint()); result |= geom; } return result.size(); }
void ZBorderLayout::setGeometry(const QRect &rect) { ItemWrapper *center = 0; int eastWidth = 0; int westWidth = 0; int northHeight = 0; int southHeight = 0; int centerHeight = 0; int i; QLayout::setGeometry(rect); for (i = 0; i < list.size(); ++i) { ItemWrapper *wrapper = list.at(i); QLayoutItem *item = wrapper->item; Position position = wrapper->position; if (position == North) { item->setGeometry(QRect(rect.x(), northHeight, rect.width(), item->sizeHint().height())); northHeight += item->geometry().height() + spacing(); } else if (position == South) { item->setGeometry(QRect(item->geometry().x(), item->geometry().y(), rect.width(), item->sizeHint().height())); southHeight += item->geometry().height() + spacing(); item->setGeometry(QRect(rect.x(), rect.y() + rect.height() - southHeight + spacing(), item->geometry().width(), item->geometry().height())); } else if (position == Center) { center = wrapper; } } centerHeight = rect.height() - northHeight - southHeight; for (i = 0; i < list.size(); ++i) { ItemWrapper *wrapper = list.at(i); QLayoutItem *item = wrapper->item; Position position = wrapper->position; if (position == West) { item->setGeometry(QRect(rect.x() + westWidth, northHeight, item->sizeHint().width(), centerHeight)); westWidth += item->geometry().width() + spacing(); } else if (position == East) { item->setGeometry(QRect(item->geometry().x(), item->geometry().y(), item->sizeHint().width(), centerHeight)); eastWidth += item->geometry().width() + spacing(); item->setGeometry(QRect( rect.x() + rect.width() - eastWidth + spacing(), northHeight, item->geometry().width(), item->geometry().height())); } } if (center) center->item->setGeometry(QRect(westWidth, northHeight, rect.width() - eastWidth - westWidth, centerHeight)); }
/** * The purpose of this function is to make sure that widgets are not laid out outside its layout. * E.g. the layoutItemRect margins are only meant to take of the surrounding margins/spacings. * However, if the margin is 0, it can easily cover the area of a widget above it. */ void QBoxLayoutPrivate::effectiveMargins(int *left, int *top, int *right, int *bottom) const { int l = leftMargin; int t = topMargin; int r = rightMargin; int b = bottomMargin; #ifdef Q_WS_MAC Q_Q(const QBoxLayout); if (horz(dir)) { QBoxLayoutItem *leftBox = 0; QBoxLayoutItem *rightBox = 0; if (left || right) { leftBox = list.value(0); rightBox = list.value(list.count() - 1); if (dir == QBoxLayout::RightToLeft) qSwap(leftBox, rightBox); int leftDelta = 0; int rightDelta = 0; if (leftBox) { QLayoutItem *itm = leftBox->item; if (QWidget *w = itm->widget()) leftDelta = itm->geometry().left() - w->geometry().left(); } if (rightBox) { QLayoutItem *itm = rightBox->item; if (QWidget *w = itm->widget()) rightDelta = w->geometry().right() - itm->geometry().right(); } QWidget *w = q->parentWidget(); Qt::LayoutDirection layoutDirection = w ? w->layoutDirection() : QApplication::layoutDirection(); if (layoutDirection == Qt::RightToLeft) qSwap(leftDelta, rightDelta); l = qMax(l, leftDelta); r = qMax(r, rightDelta); } int count = top || bottom ? list.count() : 0; for (int i = 0; i < count; ++i) { QBoxLayoutItem *box = list.at(i); QLayoutItem *itm = box->item; QWidget *w = itm->widget(); if (w) { QRect lir = itm->geometry(); QRect wr = w->geometry(); if (top) t = qMax(t, lir.top() - wr.top()); if (bottom) b = qMax(b, wr.bottom() - lir.bottom()); } } } else { // vertical layout QBoxLayoutItem *topBox = 0; QBoxLayoutItem *bottomBox = 0; if (top || bottom) { topBox = list.value(0); bottomBox = list.value(list.count() - 1); if (dir == QBoxLayout::BottomToTop) { qSwap(topBox, bottomBox); } if (top && topBox) { QLayoutItem *itm = topBox->item; QWidget *w = itm->widget(); if (w) t = qMax(t, itm->geometry().top() - w->geometry().top()); } if (bottom && bottomBox) { QLayoutItem *itm = bottomBox->item; QWidget *w = itm->widget(); if (w) b = qMax(b, w->geometry().bottom() - itm->geometry().bottom()); } } int count = left || right ? list.count() : 0; for (int i = 0; i < count; ++i) { QBoxLayoutItem *box = list.at(i); QLayoutItem *itm = box->item; QWidget *w = itm->widget(); if (w) { QRect lir = itm->geometry(); QRect wr = w->geometry(); if (left) l = qMax(l, lir.left() - wr.left()); if (right) r = qMax(r, wr.right() - lir.right()); } } } #endif if (left) *left = l; if (top) *top = t; if (right) *right = r; if (bottom) *bottom = b; }
//----------------------------------------------------------------------------- //! //----------------------------------------------------------------------------- void tMediaButtonBox::paintEvent( QPaintEvent* /*pEvent*/ ) { QPainter painter( this ); const QRect rect = contentsRect(); // background painter.fillRect( rect, palette().window() ); // separator drawing is supported only for QBoxLayout QBoxLayout* pLayout = qobject_cast< QBoxLayout* >( layout() ); if( !pLayout ) return; // if there is no space between the items, we don't draw any separator int halfSpacing = qRound( pLayout->spacing() * 0.5 ); if( halfSpacing == 0 ) return; // setup the pen to draw separators tNOSStyle* pNosStyle = qobject_cast< tNOSStyle* >( style() ); if( pNosStyle ) { QColor c = pNosStyle->GetColor( tNOSStyle::eNCR_Alternative1 ); c.setAlpha( 127 ); QPen pen( c ); pen.setCosmetic( true ); painter.setPen( pen ); } // separator bounds (X for horizontal sep, Y for vertical sep) int separatorOffset; if( pLayout->direction() == QBoxLayout::LeftToRight || pLayout->direction() == QBoxLayout::RightToLeft ) { separatorOffset = rect.height() / 7; } else { separatorOffset = rect.width() / 8; } int separatorX1 = rect.left() + separatorOffset; int separatorX2 = rect.right() - separatorOffset; int separatorY1 = rect.top() + separatorOffset; int separatorY2 = rect.bottom() - separatorOffset; // find the first visible widget or spacer (layouts are not handled yet, see below) int from = 0; QLayoutItem* fromItem = 0; for( from = 0; from < pLayout->count(); ++from ) { fromItem = pLayout->itemAt( from ); if( ( fromItem->widget() && fromItem->widget()->isVisible() ) || fromItem->spacerItem() ) break; } // iterate over each item and draw a separator between: // - two visible widgets // - a spacer and a visible widget // - a visible widget and a spacer // to be noted that embedded layouts are not handled yet (must be wrapped in a widget) for( int to = from + 1; to < pLayout->count(); ++to ) { QLayoutItem* toItem = pLayout->itemAt( to ); if( toItem->widget() ) { // if toItem is a visible widget, process it; if it is hidden, skip it if( toItem->widget()->isHidden() ) continue; } else if( toItem->spacerItem() ) { // skip empty spacer if( toItem->geometry().isEmpty() ) continue; if( fromItem->spacerItem() ) { // if fromItem and toItem are spacers, just take toItem as // the new reference but don't draw any separator from = to; fromItem = toItem; continue; } } // because a spacer item "eat" the spacing (i.e. there is no spacing added after a spacer) // we need to adjust the separator offset int tunedHalfSpacing = halfSpacing; if( fromItem->spacerItem() ) tunedHalfSpacing -= pLayout->spacing(); switch( pLayout->direction() ) { case QBoxLayout::LeftToRight: { int x = fromItem->geometry().right() + tunedHalfSpacing; painter.drawLine( x, separatorY1, x, separatorY2 ); } break; case QBoxLayout::RightToLeft: { int x = fromItem->geometry().left() - tunedHalfSpacing; painter.drawLine( x, separatorY1, x, separatorY2 ); } break; case QBoxLayout::TopToBottom: { int y = fromItem->geometry().bottom() + tunedHalfSpacing; painter.drawLine( separatorX1, y, separatorX2, y ); } break; case QBoxLayout::BottomToTop: { int y = fromItem->geometry().top() - tunedHalfSpacing; painter.drawLine( separatorX1, y, separatorX2, y ); } break; } from = to; fromItem = toItem; } }