QSize KisCategorizedItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const { //on first calling this calculates the minimal height of the items if (m_minimumItemHeight == 0) { for(int i=0; i<index.model()->rowCount(); i++) { QSize indexSize = QStyledItemDelegate::sizeHint(option, index.model()->index(i, 0)); m_minimumItemHeight = qMax(m_minimumItemHeight, indexSize.height()); /** * Make all the items, including the ones having * checkboxes look the same. */ QStyle *style = QApplication::style(); QStyleOptionButton so; QSize size = style->sizeFromContents(QStyle::CT_CheckBox, &so, QSize(), 0); m_minimumItemHeight = qMax(size.height(), m_minimumItemHeight); } } int width = QStyledItemDelegate::sizeHint(option, index).width(); if (index.data(__CategorizedListModelBase::isLockableRole).toBool()) { width += m_minimumItemHeight; } return QSize(width, m_minimumItemHeight); }
/*! Returns the size needed by the delegate to display the item specified by \a index, taking into account the style information provided by \a option. This function uses the view's QStyle to determine the size of the item. \sa QStyle::sizeFromContents(), QStyle::CT_ItemViewItem */ QSize QStyledItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { QVariant value = index.data(Qt::SizeHintRole); if (value.isValid()) return qvariant_cast<QSize>(value); QStyleOptionViewItem opt = option; initStyleOption(&opt, index); const QWidget *widget = QStyledItemDelegatePrivate::widget(option); QStyle *style = widget ? widget->style() : QApplication::style(); return style->sizeFromContents(QStyle::CT_ItemViewItem, &opt, QSize(), widget); }
QSize ProgressBarDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const { QStyleOptionViewItemV4 opt = option; // initStyleOption( &opt, index ); QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); QStyleOptionProgressBar pbOption; pbOption.QStyleOption::operator=( option ); initStyleOptionProgressBar( &pbOption, index ); return style->sizeFromContents( QStyle::CT_ProgressBar, &pbOption, QSize(), opt.widget ); }
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override { QStyleOptionViewItem opt = option; initStyleOption(&opt, index); opt.decorationSize = QSize(0, 0); opt.text = index.model()->data(index, static_cast<int>(AppRole::Display)).toString();; const QWidget* widget = option.widget; QStyle* style = widget ? widget->style() : QApplication::style(); QSize contSize = style->sizeFromContents(QStyle::CT_ItemViewItem, &opt, QSize(), widget); return QSize( mParent ? qMin(mParent->width() - 2 * mFrameWidth, contSize.width()) : contSize.width(), contSize.height() ); }
void RenderThemeQStyle::computeSizeBasedOnStyle(RenderStyle* renderStyle) const { QSize size(0, 0); const QFontMetrics fm(renderStyle->font().font()); QStyle* style = qStyle(); switch (renderStyle->appearance()) { case TextAreaPart: case SearchFieldPart: case TextFieldPart: { int padding = findFrameLineWidth(style); renderStyle->setPaddingLeft(Length(padding, Fixed)); renderStyle->setPaddingRight(Length(padding, Fixed)); renderStyle->setPaddingTop(Length(padding, Fixed)); renderStyle->setPaddingBottom(Length(padding, Fixed)); break; } default: break; } // If the width and height are both specified, then we have nothing to do. if (!renderStyle->width().isIntrinsicOrAuto() && !renderStyle->height().isAuto()) return; switch (renderStyle->appearance()) { case CheckboxPart: { QStyleOption styleOption; styleOption.state |= QStyle::State_Small; int checkBoxWidth = style->pixelMetric(QStyle::PM_IndicatorWidth, &styleOption); checkBoxWidth *= renderStyle->effectiveZoom(); size = QSize(checkBoxWidth, checkBoxWidth); break; } case RadioPart: { QStyleOption styleOption; styleOption.state |= QStyle::State_Small; int radioWidth = style->pixelMetric(QStyle::PM_ExclusiveIndicatorWidth, &styleOption); radioWidth *= renderStyle->effectiveZoom(); size = QSize(radioWidth, radioWidth); break; } case PushButtonPart: case ButtonPart: { QStyleOptionButton styleOption; styleOption.state |= QStyle::State_Small; QSize contentSize = fm.size(Qt::TextShowMnemonic, QString::fromLatin1("X")); QSize pushButtonSize = style->sizeFromContents(QStyle::CT_PushButton, &styleOption, contentSize, 0); styleOption.rect = QRect(0, 0, pushButtonSize.width(), pushButtonSize.height()); QRect layoutRect = style->subElementRect(QStyle::SE_PushButtonLayoutItem, &styleOption, 0); // If the style supports layout rects we use that, and compensate accordingly // in paintButton() below. if (!layoutRect.isNull()) size.setHeight(layoutRect.height()); else size.setHeight(pushButtonSize.height()); break; } case MenulistPart: { QStyleOptionComboBox styleOption; styleOption.state |= QStyle::State_Small; int contentHeight = qMax(fm.lineSpacing(), 14) + 2; QSize menuListSize = style->sizeFromContents(QStyle::CT_ComboBox, &styleOption, QSize(0, contentHeight), 0); size.setHeight(menuListSize.height()); break; } default: break; } // FIXME: Check is flawed, since it doesn't take min-width/max-width into account. if (renderStyle->width().isIntrinsicOrAuto() && size.width() > 0) renderStyle->setMinWidth(Length(size.width(), Fixed)); if (renderStyle->height().isAuto() && size.height() > 0) renderStyle->setMinHeight(Length(size.height(), Fixed)); }
static void computeSizeBasedOnStyle(RenderStyle* renderStyle) { // If the width and height are both specified, then we have nothing to do. if (!renderStyle->width().isIntrinsicOrAuto() && !renderStyle->height().isAuto()) return; QSize size(0, 0); const QFontMetrics fm(renderStyle->font().font()); QStyle* applicationStyle = QApplication::style(); switch (renderStyle->appearance()) { case CheckboxAppearance: { QStyleOption styleOption; styleOption.state |= QStyle::State_Small; int checkBoxWidth = applicationStyle->pixelMetric(QStyle::PM_IndicatorWidth, &styleOption); size = QSize(checkBoxWidth, checkBoxWidth); break; } case RadioAppearance: { QStyleOption styleOption; styleOption.state |= QStyle::State_Small; int radioWidth = applicationStyle->pixelMetric(QStyle::PM_ExclusiveIndicatorWidth, &styleOption); size = QSize(radioWidth, radioWidth); break; } case PushButtonAppearance: case ButtonAppearance: { QStyleOptionButton styleOption; styleOption.state |= QStyle::State_Small; QSize contentSize = fm.size(Qt::TextShowMnemonic, QString::fromLatin1("X")); QSize pushButtonSize = applicationStyle->sizeFromContents(QStyle::CT_PushButton, &styleOption, contentSize, 0); styleOption.rect = QRect(0, 0, pushButtonSize.width(), pushButtonSize.height()); QRect layoutRect = applicationStyle->subElementRect(QStyle::SE_PushButtonLayoutItem, &styleOption, 0); // If the style supports layout rects we use that, and // compensate accordingly in paintButton() below. if (!layoutRect.isNull()) { size.setHeight(layoutRect.height()); } else { size.setHeight(pushButtonSize.height()); } break; } case MenulistAppearance: { QStyleOptionComboBox styleOption; styleOption.state |= QStyle::State_Small; int contentHeight = qMax(fm.lineSpacing(), 14) + 2; QSize menuListSize = applicationStyle->sizeFromContents(QStyle::CT_ComboBox, &styleOption, QSize(0, contentHeight), 0); size.setHeight(menuListSize.height()); break; } case TextFieldAppearance: { const int verticalMargin = 1; const int horizontalMargin = 2; int h = qMax(fm.lineSpacing(), 14) + 2*verticalMargin; int w = fm.width(QLatin1Char('x')) * 17 + 2*horizontalMargin; QStyleOptionFrameV2 opt; opt.lineWidth = applicationStyle->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt, 0); QSize sz = applicationStyle->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(w, h).expandedTo(QApplication::globalStrut()), 0); size.setHeight(sz.height()); break; } default: break; } // FIXME: Check is flawed, since it doesn't take min-width/max-width into account. if (renderStyle->width().isIntrinsicOrAuto() && size.width() > 0) renderStyle->setWidth(Length(size.width(), Fixed)); if (renderStyle->height().isAuto() && size.height() > 0) renderStyle->setHeight(Length(size.height(), Fixed)); }