Example #1
0
 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;

     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());
     }
Example #2
0
void QwtPlotAbstractCanvas::drawStyled( QPainter *painter, bool hackStyledBackground )
{
    fillBackground( painter );

    if ( hackStyledBackground )
    {
        // Antialiasing rounded borders is done by
        // inserting pixels with colors between the 
        // border color and the color on the canvas,
        // When the border is painted before the plot items
        // these colors are interpolated for the canvas
        // and the plot items need to be clipped excluding
        // the anialiased pixels. In situations, where
        // the plot items fill the area at the rounded
        // borders this is noticeable.
        // The only way to avoid these annoying "artefacts"
        // is to paint the border on top of the plot items.

        if ( !d_data->styleSheet.hasBorder ||
            d_data->styleSheet.borderPath.isEmpty() )
        {
            // We have no border with at least one rounded corner
            hackStyledBackground = false;
        }
    }
    
    QWidget *w = canvasWidget();

    if ( hackStyledBackground )
    {
        painter->save();
        
        // paint background without border
        painter->setPen( Qt::NoPen );
        painter->setBrush( d_data->styleSheet.background.brush );
        painter->setBrushOrigin( d_data->styleSheet.background.origin );
        painter->setClipPath( d_data->styleSheet.borderPath );
        painter->drawRect( w->contentsRect() );

        painter->restore();

        drawCanvas( painter );

        // Now paint the border on top
        QStyleOptionFrame opt;
        opt.initFrom( w );
        w->style()->drawPrimitive( QStyle::PE_Frame, &opt, painter, w);
    }   
    else
    {
        QStyleOption opt;
        opt.initFrom( w );
        w->style()->drawPrimitive( QStyle::PE_Widget, &opt, painter, w );
    
        drawCanvas( painter );
    }   
}   
Example #3
0
void Style::repolish(QWidget* w)
{
    w->style()->unpolish(w);
    w->style()->polish(w);

    for (QObject* o : w->children()) {
        QWidget* c = qobject_cast<QWidget*>(o);
        if (c) {
            c->style()->unpolish(c);
            c->style()->polish(c);
        }
    }
}
Example #4
0
/*!
    \internal
*/
QLayout *QFormBuilder::createLayout(const QString &layoutName, QObject *parent, const QString &name)
{
    QLayout *l = 0;

    QWidget *parentWidget = qobject_cast<QWidget*>(parent);
    QLayout *parentLayout = qobject_cast<QLayout*>(parent);

    Q_ASSERT(parentWidget || parentLayout);

#define DECLARE_WIDGET(W, C)
#define DECLARE_COMPAT_WIDGET(W, C)

#define DECLARE_LAYOUT(L, C) \
    if (layoutName == QLatin1String(#L)) { \
        Q_ASSERT(l == 0); \
        l = parentLayout \
            ? new L() \
            : new L(parentWidget); \
    }

#include "widgets.table"

#undef DECLARE_LAYOUT
#undef DECLARE_COMPAT_WIDGET
#undef DECLARE_WIDGET

    if (l) {
        l->setObjectName(name);
        if (parentLayout) {
            QWidget *w = qobject_cast<QWidget *>(parentLayout->parent());
            if (w && w->inherits("Q3GroupBox")) {
                l->setContentsMargins(w->style()->pixelMetric(QStyle::PM_LayoutLeftMargin),
                                    w->style()->pixelMetric(QStyle::PM_LayoutTopMargin),
                                    w->style()->pixelMetric(QStyle::PM_LayoutRightMargin),
                                    w->style()->pixelMetric(QStyle::PM_LayoutBottomMargin));
                QGridLayout *grid = qobject_cast<QGridLayout *>(l);
                if (grid) {
                    grid->setHorizontalSpacing(-1);
                    grid->setVerticalSpacing(-1);
                } else {
                    l->setSpacing(-1);
                }
                l->setAlignment(Qt::AlignTop);
            }
        }
    } else {
        qWarning() << QCoreApplication::translate("QFormBuilder", "The layout type `%1' is not supported.").arg(layoutName);
    }

    return l;
}
/*!
  Draw the background of the canvas
  \param painter Painter
*/ 
void QwtPlotGLCanvas::drawBackground( QPainter *painter )
{
    painter->save();

    QWidget *w = qwtBGWidget( this );

    const QPoint off = mapTo( w, QPoint() );
    painter->translate( -off );

    const QRect fillRect = rect().translated( off );

    if ( w->testAttribute( Qt::WA_StyledBackground ) )
    {
        painter->setClipRect( fillRect );

        QStyleOption opt;
        opt.initFrom( w );
        w->style()->drawPrimitive( QStyle::PE_Widget, &opt, painter, w);
    }
    else 
    {
        painter->fillRect( fillRect,
            w->palette().brush( w->backgroundRole() ) );
    }

    painter->restore();
}
Example #6
0
QAccessible::State QAccessibleMenuItem::state() const
{
    QAccessible::State s;
    QWidget *own = owner();

    if (own && (own->testAttribute(Qt::WA_WState_Visible) == false || m_action->isVisible() == false)) {
        s.invisible = true;
    }

    if (QMenu *menu = qobject_cast<QMenu*>(own)) {
        if (menu->activeAction() == m_action)
            s.focused = true;
#ifndef QT_NO_MENUBAR
    } else if (QMenuBar *menuBar = qobject_cast<QMenuBar*>(own)) {
        if (menuBar->activeAction() == m_action)
            s.focused = true;
#endif
    }
    if (own && own->style()->styleHint(QStyle::SH_Menu_MouseTracking))
        s.hotTracked = true;
    if (m_action->isSeparator() || !m_action->isEnabled())
        s.disabled = true;
    if (m_action->isChecked())
        s.checked = true;

    return s;
}
Example #7
0
int FlowLayout::smartSpacing(QStyle::PixelMetric t_pm) const {
    if ((t_pm == QStyle::PM_LayoutHorizontalSpacing) &&
            (m_item_list.count() > 0) && (this->geometry().width() > 0)) {
        qreal refWidth = 40. / 2736.;
        QRect rect = qApp->primaryScreen()->geometry();
        int newWidth = static_cast<int>(rect.width() * refWidth + 1);

        auto width = this->geometry().width() - newWidth;
        auto single_width = m_item_list.first()->widget()->width();
        if (single_width == 0) return 0;
        int items_per_row = width / single_width;
        if (items_per_row == 0) items_per_row = 1;

        auto extra_space = width - single_width * items_per_row;

        if (items_per_row == 1) return extra_space;

        return extra_space / (items_per_row - 1) - 1;
    }

    QObject *parent = this->parent();

    if (!parent) {
        return -1;
    } else if (parent->isWidgetType()) {
        QWidget *pw = static_cast<QWidget *>(parent);
        return pw->style()->pixelMetric(t_pm, 0, pw);
    } else {
        return static_cast<QLayout *>(parent)->spacing();
    }
}
Example #8
0
//! Update the cached information about the current style sheet
void QwtPlotAbstractCanvas::updateStyleSheetInfo()
{
    QWidget *w = canvasWidget();

    if ( !w->testAttribute( Qt::WA_StyledBackground ) )
        return;

    QwtStyleSheetRecorder recorder( w->size() );

    QPainter painter( &recorder );

    QStyleOption opt;
    opt.initFrom(w);
    w->style()->drawPrimitive( QStyle::PE_Widget, &opt, &painter, w);

    painter.end();

    d_data->styleSheet.hasBorder = !recorder.border.rectList.isEmpty();
    d_data->styleSheet.cornerRects = recorder.clipRects;

    if ( recorder.background.path.isEmpty() )
    {
        if ( !recorder.border.rectList.isEmpty() )
        {
            d_data->styleSheet.borderPath =
                qwtCombinePathList( w->rect(), recorder.border.pathList );
        }
    }
    else
    {
        d_data->styleSheet.borderPath = recorder.background.path;
        d_data->styleSheet.background.brush = recorder.background.brush;
        d_data->styleSheet.background.origin = recorder.background.origin;
    }
}
Example #9
0
QRect ButtonTextTaskMenuInlineEditor::editRectangle() const
{
    QWidget *w = widget();
    QStyleOptionButton opt;
    opt.init(w);
    return w->style()->subElementRect(QStyle::SE_PushButtonContents, &opt, w);
}
Example #10
0
QStyle *QWidgetProto::style() const
{
  QWidget *item = qscriptvalue_cast<QWidget*>(thisObject());
  if (item)
    return item->style();
  return 0;
}
Example #11
0
QRect LinkDescriptionTaskMenuInlineEditor::editRectangle() const
{
    QWidget *w = widget(); // TODO: What is the exact description area?
    QStyleOptionButton opt;
    opt.init(w);
    return w->style()->subElementRect(QStyle::SE_PushButtonContents, &opt, w);
}
Example #12
0
BrowseModel::BrowseModel (QObject *parent, XClient *client) : QAbstractTableModel ()
{
	m_columns.append ("Name");
	QWidget *w = dynamic_cast<QWidget*>(parent);
	m_style = w->style ();
	m_filter_dot = true;
	m_client = client;
}
Example #13
0
QSize KxMenuItemWidget::sizeHint() const
{
    // Do not give invisible menu items any space
    if(!fMenuItem->isVisible()) return QSize(0,0);

    QSize sz;

    QWidget *q = parentWidget();
    QStyleOptionMenuItem opt = getStyleOption();
    KxMenuItem *action = fMenuItem;

    const int hmargin = q->style()->pixelMetric(QStyle::PM_MenuDesktopFrameWidth, 0, q),
        iconWidth = q->style()->pixelMetric(QStyle::PM_SmallIconSize, 0, q);

    QFontMetrics fm(action->font().resolve(q->font()));
    if (action->isSeparator()) {
        sz = QSize(2, 2);
    } else {
        // Text
        QString s = action->text();
        int w = fm.width(s);
        QKeySequence shortcutSeq = fMenuItem->shortcut();
        sz.setWidth(w);
        sz.setHeight(fm.height());
        // Keyboard shortcut
        if(!shortcutSeq.isEmpty()) {
            QString shortcut = shortcutSeq.toString(QKeySequence::NativeText);
            w = fm.width(shortcut);
            sz.rwidth() += w + kShortcutRightMargin;
        }
        // Checkbox
        if(action->isCheckable()) {
            sz.rwidth() += iconWidth;
        }
        // option boxes
        sz.rwidth() += iconWidth;
        // Margins
        sz.rwidth() += 2 * hmargin;
    }

    opt.rect = q->rect();
    sz = q->style()->sizeFromContents(QStyle::CT_MenuItem, &opt, sz, q);

    return sz;
}
Example #14
0
int Maemo5Docklet::GetIconSize()
{
    if (icon_size==0)
    {
        QWidget w;
        icon_size = w.style()->pixelMetric(QStyle::PM_ToolBarIconSize);
    }
    return icon_size;
}
Example #15
0
int FlowLayout::doLayout(const QRect &t_rect, const bool t_testOnly) const {
    int *left = new int;

    this->getContentsMargins(left, 0, 0, 0);
    QRect effectiveRect = t_rect.adjusted(+*left, 0, 0, 0);
    int x = effectiveRect.x();
    int y = effectiveRect.y();
    int lineHeight = 0;

    delete left;

    for (QLayoutItem *item : m_item_list) {
        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 (!t_testOnly) item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));

        x = nextX;
        lineHeight = qMax(lineHeight, item->sizeHint().height());
    }

    return y + lineHeight - t_rect.y();
}
Example #16
0
Q_GUI_EXPORT int qSmartSpacing(const QLayout *layout, QStyle::PixelMetric pm)
{
    QObject *parent = layout->parent();
    if (!parent) {
        return -1;
    } else if (parent->isWidgetType()) {
        QWidget *pw = static_cast<QWidget *>(parent);
        return pw->style()->pixelMetric(pm, 0, pw);
    } else {
        return static_cast<QLayout *>(parent)->spacing();
    }
}
Example #17
0
int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const
{
    QObject *parent = this->parent();
    if (!parent) {
        return -1;
    } else if (parent->isWidgetType()) {
        QWidget *pw = static_cast<QWidget *>(parent);
        return pw->style()->pixelMetric(pm, 0, pw);
    } else {
        return static_cast<QLayout *>(parent)->spacing();
    }
}
Example #18
0
int TileLayout::smartSpacing(QStyle::PixelMetric pm) const
{
	QObject* parent = this->parent();
	if (!parent)
		return -1;

	if (parent->isWidgetType())
	{
		QWidget* pw = static_cast<QWidget*>(parent);
		return pw->style()->pixelMetric(pm, nullptr, pw);
	}
	return static_cast<QLayout*>(parent)->spacing();
}
Example #19
0
void KxMenuItemWidget::mouseReleaseEvent(QMouseEvent * event)
{
    bool triggerOptionBoxAction = false;

    Qt::KeyboardModifiers mods = event->modifiers();

    bool optionBoxModifier = (mods == Qt::ShiftModifier);
    bool saveToShelfModifier = (mods == (Qt::ShiftModifier|Qt::ControlModifier));

    if(!fMenuItem->isSeparator() && fMenuItem->isEnabled()) {
        if(fMenuItem->hasOptionBox()) {
            QRect boxRect;
            QWidget *q = parentWidget();
            const int hmargin = q->style()->pixelMetric(QStyle::PM_MenuDesktopFrameWidth, 0, q),
                iconWidth = q->style()->pixelMetric(QStyle::PM_SmallIconSize, 0, q);
            boxRect.setRect(rect().right() - hmargin - iconWidth, rect().top(), iconWidth, rect().height());
            if(boxRect.contains(event->pos()) || optionBoxModifier) {
                triggerOptionBoxAction = true;
                QMenu *menu = qobject_cast<QMenu *>(parentWidget());
                if(menu && !saveToShelfModifier) {
                    menu->setActiveAction(NULL);
                }
            }
        }
    }

    if(saveToShelfModifier) {
        if(fMenuItem->saveToShelf(triggerOptionBoxAction ? KxMenuItem::kOptionBoxAction : KxMenuItem::kMainAction)) {
            return;
        }
    }

    QWidget::mouseReleaseEvent(event);

    if(triggerOptionBoxAction) {
        fMenuItem->optionBoxAction()->activate(QAction::Trigger);
    }
}
Example #20
0
QSize ListItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if (!m_itemHeight) {
        QStyleOptionViewItemV4 opt(option);
        initStyleOption(&opt, index);

        const QWidget* w = opt.widget;
        const QStyle* style = w ? w->style() : QApplication::style();
        const int padding = style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0) + 1;

        m_padding = padding > 5 ? padding : 5;

        m_itemHeight = 3 * m_padding + opt.fontMetrics.height() + m_iconSize;

        // Update height of parent widget
        QWidget* p = qobject_cast<QWidget*>(parent());
        if (p && m_updateParentHeight) {
            int frameWidth = p->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, p);
            p->setFixedHeight(m_itemHeight + 2 * frameWidth);
        }
    }

    int width = 2 * m_padding + option.fontMetrics.width(index.data(Qt::DisplayRole).toString());
    width = width > (m_iconSize + 2 * m_padding) ? width : m_iconSize + 2 * m_padding;

    if (m_uniformItemSizes) {
        if (width > m_itemWidth) {
            m_itemWidth = width;
        }
        else {
            width = m_itemWidth;
        }
    }

    return QSize(width, m_itemHeight);
}
Example #21
0
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;
}
Example #22
0
void KxMenuItemWidget::paintEvent(QPaintEvent *event)
{
    // Do not draw invisible menu items
    if(!fMenuItem->isVisible()) return;

    QStylePainter painter(this);
    QStyleOptionMenuItem opt = getStyleOption();
    QRect boxRect;
    bool inOptionBox = false;

    QWidget *q = parentWidget();

    const int hmargin = q->style()->pixelMetric(QStyle::PM_MenuDesktopFrameWidth, 0, q),
        iconWidth = q->style()->pixelMetric(QStyle::PM_SmallIconSize, 0, q);

    if(!fMenuItem->isSeparator()) {
        if(fMenuItem->hasOptionBox()) {
            boxRect.setRect(rect().right() - hmargin - iconWidth, rect().top(), iconWidth, rect().height());
            QPoint p = QCursor::pos();
            p = mapFromGlobal(p);
            if(boxRect.contains(p)) {
                inOptionBox = true;
            } else {
                // Subtract option box rect from style option rect
                int newRight = opt.rect.right() - iconWidth - hmargin;
                opt.rect.setRight(newRight);
            }
        }
    }
    // Draw general menu item
    opt.rect.adjust(0, 0, -1, 0);
    painter.drawControl(QStyle::CE_MenuItem, opt);
    // Draw shortcut
    QKeySequence shortcutSeq = fMenuItem->shortcut();
    if(!shortcutSeq.isEmpty()) {
        // shortcut bounds
        QRect scRect = opt.rect;
        QString shortcut = shortcutSeq.toString(QKeySequence::NativeText);
        QMenu *menu = qobject_cast<QMenu *>(parentWidget());
        Q_ASSERT(menu != NULL);
        int shortcutWidth = 12;	// default value in case there is no font
        if ( menu ) {
            QFontMetrics metrics(fMenuItem->font().resolve(menu->font()));
            shortcutWidth = metrics.width(shortcut);
        }
        scRect.setLeft(scRect.right() - shortcutWidth);
        if(inOptionBox || !fMenuItem->hasOptionBox()) {
            scRect.translate(-iconWidth, 0);
        }
        scRect.translate(-kShortcutRightMargin, 0);
        painter.drawItemText(scRect, Qt::AlignRight | Qt::AlignVCenter, palette(), true, shortcut);
    }
    // Draw option box
    if(!fMenuItem->isSeparator() && fMenuItem->hasOptionBox()) {
        QIcon* boxIcon = NULL;
        QVariant v = fMenuItem->optionBoxAction()->property( "optionBoxIcon" );
        if ( v.isValid() ) {
            QString optionBoxIcon;
            optionBoxIcon = v.toString();
            boxIcon = KxQtHelper::createIcon( optionBoxIcon );
        }
        if ( boxIcon == NULL ) {
            boxIcon = new QIcon(":/optionBox.png");
        }
        boxRect.setRect(rect().right() - hmargin - iconWidth, rect().top()+(rect().height()-iconWidth)/2, iconWidth, iconWidth);
        boxIcon->paint(&painter, boxRect, Qt::AlignCenter, fMenuItem->isEnabled() ? QIcon::Normal : QIcon::Disabled);
        delete boxIcon;
    }
}
Example #23
0
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;
	}
}
Example #24
0
int KMessageBox::createKMessageBox(KDialog *dialog, const QIcon &icon,
                             const QString &text, const QStringList &strlist,
                             const QString &ask, bool *checkboxReturn, Options options,
                             const QString &details, QMessageBox::Icon notifyType)
{
    QWidget *mainWidget = new QWidget(dialog);
    QVBoxLayout *mainLayout = new QVBoxLayout(mainWidget);
    mainLayout->setSpacing(KDialog::spacingHint() * 2); // provide extra spacing
    mainLayout->setMargin(0);

    QHBoxLayout *hLayout = new QHBoxLayout();
    hLayout->setMargin(0);
    hLayout->setSpacing(-1); // use default spacing
    mainLayout->addLayout(hLayout,5);

    QLabel *iconLabel = new QLabel(mainWidget);

    if (!icon.isNull()) {
        QStyleOption option;
        option.initFrom(mainWidget);
        iconLabel->setPixmap(icon.pixmap(mainWidget->style()->pixelMetric(QStyle::PM_MessageBoxIconSize, &option, mainWidget)));
    }

    QVBoxLayout *iconLayout = new QVBoxLayout();
    iconLayout->addStretch(1);
    iconLayout->addWidget(iconLabel);
    iconLayout->addStretch(5);

    hLayout->addLayout(iconLayout,0);
    hLayout->addSpacing(KDialog::spacingHint());

    QLabel *messageLabel = new QLabel(text, mainWidget);
    messageLabel->setOpenExternalLinks(options & KMessageBox::AllowLink);
    Qt::TextInteractionFlags flags = Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard;
    if (options & KMessageBox::AllowLink) {
        flags |= Qt::LinksAccessibleByMouse | Qt::LinksAccessibleByKeyboard;
    }
    messageLabel->setTextInteractionFlags(flags);

    QRect desktop = KGlobalSettings::desktopGeometry(dialog);
    bool usingSqueezedTextLabel = false;
    if (messageLabel->sizeHint().width() > desktop.width() * 0.5) {
        // enable automatic wrapping of messages which are longer than 50% of screen width
        messageLabel->setWordWrap(true);
        // display a text widget with scrollbar if still too wide
        usingSqueezedTextLabel = messageLabel->sizeHint().width() > desktop.width() * 0.85;
        if (usingSqueezedTextLabel)
        {
            delete messageLabel;
            messageLabel = new KSqueezedTextLabel(text, mainWidget);
            messageLabel->setOpenExternalLinks(options & KMessageBox::AllowLink);
            messageLabel->setTextInteractionFlags(flags);
        }
    }

    QPalette messagePal(messageLabel->palette());
    messagePal.setColor(QPalette::Window, Qt::transparent);
    messageLabel->setPalette(messagePal);


    bool usingScrollArea=desktop.height() / 3 < messageLabel->sizeHint().height();
    if (usingScrollArea)
    {
        QScrollArea* messageScrollArea = new QScrollArea(mainWidget);
        messageScrollArea->setWidget(messageLabel);
        messageScrollArea->setFrameShape(QFrame::NoFrame);
        messageScrollArea->setWidgetResizable(true);
        QPalette scrollPal(messageScrollArea->palette());
        scrollPal.setColor(QPalette::Window, Qt::transparent);
        messageScrollArea->viewport()->setPalette(scrollPal);
        hLayout->addWidget(messageScrollArea,5);
    }
    else
        hLayout->addWidget(messageLabel,5);


    const bool usingListWidget=!strlist.isEmpty();
    if (usingListWidget) {
        // enable automatic wrapping since the listwidget has already a good initial width
        messageLabel->setWordWrap(true);
        QListWidget *listWidget = new QListWidget(mainWidget);
        listWidget->addItems(strlist);

        QStyleOptionViewItem styleOption;
        styleOption.initFrom(listWidget);
        QFontMetrics fm(styleOption.font);
        int w = listWidget->width();
        Q_FOREACH(const QString &str, strlist) {
            w = qMax(w, fm.width(str));
        }
        const int borderWidth = listWidget->width() - listWidget->viewport()->width() + listWidget->verticalScrollBar()->height();
        w += borderWidth;
        if (w > desktop.width() * 0.85) { // limit listWidget size to 85% of screen width
            w = qRound(desktop.width() * 0.85);
        }
        listWidget->setMinimumWidth(w);

        mainLayout->addWidget(listWidget,usingScrollArea?10:50);
        listWidget->setSelectionMode(QListWidget::NoSelection);
        messageLabel->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Minimum);
    }