int QgsDetailedItemDelegate::height( const QStyleOptionViewItem & theOption, const QgsDetailedItemData theData ) const { QFontMetrics myTitleMetrics( titleFont( theOption ) ); QFontMetrics myDetailMetrics( detailFont( theOption ) ); //we don't word wrap the title so its easy to measure int myHeight = myTitleMetrics.height() + verticalSpacing(); //the detail needs to be measured though QStringList myList = wordWrap( theData.detail(), myDetailMetrics, theOption.rect.width() - ( mpCheckBox->width() + horizontalSpacing() ) ); myHeight += ( myList.count() + 1 ) * ( myDetailMetrics.height() - verticalSpacing() ); return myHeight; }
int UFlowLayout::doLayout(const QRect &rect, bool testOnly) const { int left, top, right, bottom; getContentsMargins(&left, &top, &right, &bottom); QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom); int x = effectiveRect.x(); int y = effectiveRect.y(); int lineHeight = 0; QLayoutItem *item; foreach (item, itemList) { QWidget *wid = item->widget(); int spaceX = horizontalSpacing(); if (spaceX == -1) spaceX = wid->style()->layoutSpacing( QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal); int spaceY = verticalSpacing(); if (spaceY == -1) spaceY = wid->style()->layoutSpacing( QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical); int nextX = x + item->sizeHint().width() + spaceX; if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) { x = effectiveRect.x(); y = y + lineHeight + spaceY; nextX = x + item->sizeHint().width() + spaceX; lineHeight = 0; } if (!testOnly) item->setGeometry(QRect(QPoint(x, y), item->sizeHint())); x = nextX; lineHeight = qMax(lineHeight, item->sizeHint().height()); }
int QFormLayout::qt_metacall(QMetaObject::Call _c, int _id, void **_a) { _id = QLayout::qt_metacall(_c, _id, _a); if (_id < 0) return _id; #ifndef QT_NO_PROPERTIES if (_c == QMetaObject::ReadProperty) { void *_v = _a[0]; switch (_id) { case 0: *reinterpret_cast< FieldGrowthPolicy*>(_v) = fieldGrowthPolicy(); break; case 1: *reinterpret_cast< RowWrapPolicy*>(_v) = rowWrapPolicy(); break; case 2: *reinterpret_cast< Qt::Alignment*>(_v) = labelAlignment(); break; case 3: *reinterpret_cast< Qt::Alignment*>(_v) = formAlignment(); break; case 4: *reinterpret_cast< int*>(_v) = horizontalSpacing(); break; case 5: *reinterpret_cast< int*>(_v) = verticalSpacing(); break; } _id -= 6; } else if (_c == QMetaObject::WriteProperty) { void *_v = _a[0]; switch (_id) { case 0: setFieldGrowthPolicy(*reinterpret_cast< FieldGrowthPolicy*>(_v)); break; case 1: setRowWrapPolicy(*reinterpret_cast< RowWrapPolicy*>(_v)); break; case 2: setLabelAlignment(*reinterpret_cast< Qt::Alignment*>(_v)); break; case 3: setFormAlignment(*reinterpret_cast< Qt::Alignment*>(_v)); break; case 4: setHorizontalSpacing(*reinterpret_cast< int*>(_v)); break; case 5: setVerticalSpacing(*reinterpret_cast< int*>(_v)); break; } _id -= 6; } else if (_c == QMetaObject::ResetProperty) { switch (_id) { case 0: resetFieldGrowthPolicy(); break; case 1: resetRowWrapPolicy(); break; case 2: resetLabelAlignment(); break; case 3: resetFormAlignment(); break; } _id -= 6; } else if (_c == QMetaObject::QueryPropertyDesignable) { _id -= 6; } else if (_c == QMetaObject::QueryPropertyScriptable) { _id -= 6; } else if (_c == QMetaObject::QueryPropertyStored) { _id -= 6; } else if (_c == QMetaObject::QueryPropertyEditable) { _id -= 6; } else if (_c == QMetaObject::QueryPropertyUser) { _id -= 6; } #endif // QT_NO_PROPERTIES return _id; }
int QgsDetailedItemDelegate::height( const QStyleOptionViewItem &option, const QgsDetailedItemData &data ) const { QFontMetrics myTitleMetrics( titleFont( option ) ); QFontMetrics myDetailMetrics( detailFont( option ) ); QFontMetrics myCategoryMetrics( categoryFont( option ) ); //we don't word wrap the title so its easy to measure int myHeight = myTitleMetrics.height() + verticalSpacing(); //the detail needs to be measured though QStringList myList = wordWrap( data.detail(), myDetailMetrics, option.rect.width() - ( mpCheckBox->width() + horizontalSpacing() ) ); myHeight += ( myList.count() + 1 ) * ( myDetailMetrics.height() - verticalSpacing() ); //we don't word wrap the category so its easy to measure myHeight += myCategoryMetrics.height() + verticalSpacing(); #if 0 // if category should be wrapped use this code myList = wordWrap( data.category(), myCategoryMetrics, option.rect.width() - ( mpCheckBox->width() + horizontalSpacing() ) ); myHeight += ( myList.count() + 1 ) * ( myCategoryMetrics.height() - verticalSpacing() ); #endif return myHeight; }
int FixedSizeGridLayout::doLayout(QRect rect, bool testOnly) const { int left, top, right, bottom; getContentsMargins(&left, &top, &right, &bottom); QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom); int x = effectiveRect.x(); int y = effectiveRect.y(); int w = effectiveRect.width(); int lineHeight = 0; for (QLayoutItem *item : m_items) { int spaceX = widgetSpacing(horizontalSpacing(), item->widget(), Qt::Horizontal); int spaceY = widgetSpacing(verticalSpacing(), item->widget(), Qt::Vertical); int nbElements = qMax(1, (w + spaceX) / (m_fixedWidth + spaceX)); int totalSpace = w - (m_fixedWidth * nbElements); spaceX = qMax(spaceX, totalSpace / qMax(1, nbElements - 1)); int nextX = x + item->sizeHint().width() + spaceX; if (nextX - spaceX - 1 > effectiveRect.right() && lineHeight > 0) { x = effectiveRect.x(); y = y + lineHeight + spaceY; nextX = x + item->sizeHint().width() + spaceX; lineHeight = 0; } if (!testOnly) item->setGeometry(QRect(QPoint(x, y), item->sizeHint())); x = nextX; lineHeight = qMax(lineHeight, item->sizeHint().height()); } return y + lineHeight - rect.y() + bottom; }
int FlowLayout::doLayout(const QRect &rect, bool testOnly) const { int left, top, right, bottom; getContentsMargins(&left, &top, &right, &bottom); QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom); int x = effectiveRect.x(); int y = effectiveRect.y(); int lineHeight = 0; auto spacingX = [this]( QLayoutItem *item ) { int spaceX = horizontalSpacing(); if( spaceX == -1 ) { QWidget *widget = item->widget(); if( widget ) { return widget->style()->layoutSpacing( QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal ); } return 0; } return spaceX; }; auto spacingY = [this]( QLayoutItem *item ) { int spaceY = verticalSpacing(); if( spaceY == -1 ) { QWidget *widget = item->widget(); if( widget ) { return widget->style()->layoutSpacing( QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical ); } return 0; } return spaceY; }; for( auto iter = itemList.begin(), endLine = iter; iter != itemList.end(); ) { if( iter == endLine ) { x = effectiveRect.x(); int lineWidth = 0; for( ; endLine != itemList.end(); ++endLine ) { QLayoutItem *item = *endLine; int itemWidth = spacingX( item ) + item->sizeHint().width(); if( itemWidth+lineWidth > effectiveRect.width() && lineWidth > 0 ) { break; } lineWidth += itemWidth; } x += qMax( (effectiveRect.width()-lineWidth) / 2, 0 ); y += lineHeight; lineHeight = 0; } for( ; iter != endLine; ++iter ) { QLayoutItem *item = *iter; int spaceX = spacingX( item ); int spaceY = spacingY( item ); if( !testOnly ) { item->setGeometry( QRect(QPoint(x,y), item->sizeHint()) ); } x += spaceX + item->sizeHint().width(); lineHeight = qMax( lineHeight, item->sizeHint().height()+spaceY ); } } return y + lineHeight - rect.y() + bottom; }
void QgsDetailedItemDelegate::paintManually( QPainter *thepPainter, const QStyleOptionViewItem &theOption, const QgsDetailedItemData &theData ) const { // // Get the strings and check box properties // //bool myCheckState = theIndex.model()->data(theIndex, Qt::CheckStateRole).toBool(); mpCheckBox->setChecked( theData.isChecked() ); mpCheckBox->setEnabled( theData.isEnabled() ); QPixmap myCbxPixmap( mpCheckBox->size() ); mpCheckBox->render( &myCbxPixmap ); //we will draw this onto the widget further down // // Calculate the widget height and other metrics // QFontMetrics myTitleMetrics( titleFont( theOption ) ); QFontMetrics myDetailMetrics( detailFont( theOption ) ); int myTextStartX = theOption.rect.x() + horizontalSpacing(); int myTextStartY = theOption.rect.y() + verticalSpacing(); int myHeight = myTitleMetrics.height() + verticalSpacing(); // // Draw the item background with a gradient if its highlighted // if ( theOption.state & QStyle::State_Selected ) { drawHighlight( theOption, thepPainter, height( theOption, theData ) ); thepPainter->setPen( theOption.palette.highlightedText().color() ); } else { thepPainter->setPen( theOption.palette.text().color() ); } // // Draw the checkbox // if ( theData.isCheckable() ) { thepPainter->drawPixmap( theOption.rect.x(), theOption.rect.y() + mpCheckBox->height(), myCbxPixmap ); myTextStartX = theOption.rect.x() + myCbxPixmap.width() + horizontalSpacing(); } // // Draw the decoration (pixmap) // bool myIconFlag = false; QPixmap myDecoPixmap = theData.icon(); if ( !myDecoPixmap.isNull() ) { myIconFlag = true; int iconWidth = 32, iconHeight = 32; if ( myDecoPixmap.width() <= iconWidth && myDecoPixmap.height() <= iconHeight ) { // the pixmap has reasonable size int offsetX = 0, offsetY = 0; if ( myDecoPixmap.width() < iconWidth ) offsetX = ( iconWidth - myDecoPixmap.width() ) / 2; if ( myDecoPixmap.height() < iconHeight ) offsetY = ( iconHeight - myDecoPixmap.height() ) / 2; thepPainter->drawPixmap( myTextStartX + offsetX, myTextStartY + offsetY, myDecoPixmap ); } else { // shrink the pixmap, it's too big thepPainter->drawPixmap( myTextStartX, myTextStartY, iconWidth, iconHeight, myDecoPixmap ); } myTextStartX += iconWidth + horizontalSpacing(); } // // Draw the title // myTextStartY += myHeight / 2; thepPainter->setFont( titleFont( theOption ) ); thepPainter->drawText( myTextStartX, myTextStartY, theData.title() ); // // Draw the description with word wrapping if needed // thepPainter->setFont( detailFont( theOption ) ); //return to original font set by client if ( myIconFlag ) { myTextStartY += verticalSpacing(); } else { myTextStartY += myDetailMetrics.height() + verticalSpacing(); } QStringList myList = wordWrap( theData.detail(), myDetailMetrics, theOption.rect.width() - myTextStartX ); QStringListIterator myLineWrapIterator( myList ); while ( myLineWrapIterator.hasNext() ) { QString myLine = myLineWrapIterator.next(); thepPainter->drawText( myTextStartX, myTextStartY, myLine ); myTextStartY += myDetailMetrics.height() - verticalSpacing(); } // // Draw the category. Not sure if we need word wrapping for it. // thepPainter->setFont( categoryFont( theOption ) ); //return to original font set by client thepPainter->drawText( myTextStartX, myTextStartY, theData.category() ); // // Draw the category with word wrapping if needed // /* myTextStartY += verticalSpacing(); if ( myIconFlag ) { myTextStartY += verticalSpacing(); } else { myTextStartY += myCategoryMetrics.height() + verticalSpacing(); } myList = wordWrap( theData.category(), myCategoryMetrics, theOption.rect.width() - myTextStartX ); QStringListIterator myLineWrapIter( myList ); while ( myLineWrapIter.hasNext() ) { QString myLine = myLineWrapIter.next(); thepPainter->drawText( myTextStartX, myTextStartY, myLine ); myTextStartY += myCategoryMetrics.height() - verticalSpacing(); } */ } //render by manual painting
void CameraLayout::calculateCellsSize( ){ QSize parentSize = parentWidget()->size(); int wSize = (parentSize.width() / d->columns ) - horizontalSpacing(); int hSize = (parentSize.height() / d->rows) - verticalSpacing(); d->cellsSize = QSize( wSize, hSize ); }
void TileLayout::doLayout(const QRect& rect) const { if (m_items.isEmpty()) return; int left, top, right, bottom; getContentsMargins(&left, &top, &right, &bottom); QRect effectiveRect = rect.adjusted(left, top, -right, -bottom); int x = effectiveRect.x(); int y = effectiveRect.y(); QWidget* widget = m_items.first()->widget(); int spaceX = horizontalSpacing(); if (spaceX == -1) spaceX = widget->style()->layoutSpacing(QSizePolicy::DefaultType, QSizePolicy::DefaultType, Qt::Horizontal); int spaceY = verticalSpacing(); if (spaceY == -1) spaceY = widget->style()->layoutSpacing(QSizePolicy::DefaultType, QSizePolicy::DefaultType, Qt::Vertical); qreal layoutAr = qreal(effectiveRect.width()) / effectiveRect.height(); int count = m_items.size(); QSize sh = widget->sizeHint(); qreal pixels = sh.width() * sh.height() * count; // Approximation based on the layout's aspect ratio qreal totalWidth = qSqrt(pixels * layoutAr); int cols = totalWidth / sh.width() + 0.5; int rows = (count - 1) / cols + 1; // The approximation is far from perfect, so we perform some // additional checks to make sure we have as few rows and // columns as possible. int smallest = qMin(cols, rows); if (count <= smallest * smallest) { cols = smallest; rows = smallest; } if (count <= cols * (rows - 1)) rows--; if (count <= (cols - 1) * rows) cols--; // Often the column and row counts are backwards, meaning that // the available space isn't being used efficiently. In that // case we swap the values of 'cols' and 'rows'. if (qAbs(cols - rows) >= 1) { qreal ar1 = qreal(cols * sh.width()) / (rows * sh.height()); qreal ar2 = qreal(rows * sh.width()) / (cols * sh.height()); if (qAbs(ar1 - layoutAr) > qAbs(ar2 - layoutAr)) std::swap(cols, rows); } int itemWidth = (effectiveRect.width() - (cols - 1) * spaceX) / cols; int itemHeight = (effectiveRect.height() - (rows - 1) * spaceY) / rows; int col = 0; for (QLayoutItem* item : qAsConst(m_items)) { int nextX = x + itemWidth + spaceX; if (++col > cols) { col = 1; x = effectiveRect.x(); y += itemHeight + spaceY; nextX = x + itemWidth + spaceX; } item->setGeometry(QRect(QPoint(x, y), QSize(itemWidth, itemHeight))); x = nextX; } }