From baf3ec81ca5d5cd38b54dc853c12109956a98cb9 Mon Sep 17 00:00:00 2001 From: Thierry Bastian Date: Tue, 4 Aug 2009 11:16:01 +0200 Subject: QMenu now takes minimum width into account for the action rects The action now try to take advantage of the space given by a minimum width. Patch proposed initially by Aron Seigo and improved later on. Reviewed-by: Trustme --- src/gui/widgets/qmenu.cpp | 11 ++++++----- tests/auto/qmenu/tst_qmenu.cpp | 12 ++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/gui/widgets/qmenu.cpp b/src/gui/widgets/qmenu.cpp index 0b85eec..67bb10a 100644 --- a/src/gui/widgets/qmenu.cpp +++ b/src/gui/widgets/qmenu.cpp @@ -228,6 +228,10 @@ void QMenuPrivate::updateActionRects() const const int hmargin = style->pixelMetric(QStyle::PM_MenuHMargin, 0, q), vmargin = style->pixelMetric(QStyle::PM_MenuVMargin, 0, q), icone = style->pixelMetric(QStyle::PM_SmallIconSize, 0, q); + const int fw = style->pixelMetric(QStyle::PM_MenuPanelWidth, 0, q); + + const int sfcMargin = style->sizeFromContents(QStyle::CT_Menu, 0, QApplication::globalStrut(), q).width() - QApplication::globalStrut().width(); + const int min_column_width = q->minimumWidth() - (sfcMargin + leftmargin + rightmargin + 2 * (fw + hmargin)); //for compatability now - will have to refactor this away.. tabWidth = 0; @@ -300,7 +304,7 @@ void QMenuPrivate::updateActionRects() const if (!sz.isEmpty()) { - max_column_width = qMax(max_column_width, sz.width()); + max_column_width = qMax(min_column_width, qMax(max_column_width, sz.width())); //wrapping if (!scroll && y+sz.height()+vmargin > dh - (style->pixelMetric(QStyle::PM_MenuDesktopFrameWidth, 0, q) * 2)) { @@ -316,7 +320,6 @@ void QMenuPrivate::updateActionRects() const max_column_width += tabWidth; //finally add in the tab width //calculate position - const int fw = style->pixelMetric(QStyle::PM_MenuPanelWidth, 0, q); const int base_y = vmargin + fw + topmargin + (scroll ? scroll->scrollOffset : 0) + (tearoff ? style->pixelMetric(QStyle::PM_MenuTearoffHeight, 0, q) : 0); @@ -1703,9 +1706,7 @@ QSize QMenu::sizeHint() const QSize s; QStyleOption opt(0); - opt.rect = rect(); - opt.palette = palette(); - opt.state = QStyle::State_None; + opt.init(this); for (int i = 0; i < d->actionRects.count(); ++i) { const QRect &rect = d->actionRects.at(i); if (rect.isNull()) diff --git a/tests/auto/qmenu/tst_qmenu.cpp b/tests/auto/qmenu/tst_qmenu.cpp index ec9c7b4..6ba6466 100644 --- a/tests/auto/qmenu/tst_qmenu.cpp +++ b/tests/auto/qmenu/tst_qmenu.cpp @@ -96,6 +96,7 @@ private slots: void task256918_setFont(); void menuSizeHint(); void task258920_mouseBorder(); + void setFixedWidth(); protected slots: void onActivated(QAction*); void onHighlighted(QAction*); @@ -798,6 +799,17 @@ void tst_QMenu::task258920_mouseBorder() QVERIFY(menu.painted); } +void tst_QMenu::setFixedWidth() +{ + QMenu menu; + menu.addAction("action"); + menu.setFixedWidth(300); + //the sizehint should reflect the minimumwidth because the action will try to + //get as much space as possible + QCOMPARE(menu.sizeHint().width(), menu.minimumWidth()); +} + + QTEST_MAIN(tst_QMenu) #include "tst_qmenu.moc" -- cgit v0.12