From 5fc6a2ddf3003c4eec1b04599e1420564996db67 Mon Sep 17 00:00:00 2001 From: Hugo Pereira Da Costa Date: Wed, 27 Jul 2016 12:26:45 +0200 Subject: [PATCH] Only add margin around tabwidget content when necessary, in order to keep the tabbar of minimal width --- kstyle/breezestyle.cpp | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/kstyle/breezestyle.cpp b/kstyle/breezestyle.cpp index d4b427f2..12a2f359 100644 --- a/kstyle/breezestyle.cpp +++ b/kstyle/breezestyle.cpp @@ -2817,8 +2817,46 @@ namespace Breeze } //______________________________________________________________ - QSize Style::tabWidgetSizeFromContents( const QStyleOption*, const QSize& contentsSize, const QWidget* ) const - { return expandSize( contentsSize, Metrics::TabWidget_MarginWidth ); } + QSize Style::tabWidgetSizeFromContents( const QStyleOption* option, const QSize& contentsSize, const QWidget* widget ) const + { + // cast option and check + const QStyleOptionTabWidgetFrame* tabOption = qstyleoption_cast( option ); + if( !tabOption ) return expandSize( contentsSize, Metrics::TabWidget_MarginWidth ); + + // try find direct children of type QTabBar and QStackedWidget + // this is needed in order to add TabWidget margins only if they are necessary around tabWidget content, not the tabbar + if( !widget ) return expandSize( contentsSize, Metrics::TabWidget_MarginWidth ); + QTabBar* tabBar = nullptr; + QStackedWidget* stack = nullptr; + auto children( widget->children() ); + foreach( auto child, children ) + { + if( !tabBar ) tabBar = qobject_cast( child ); + if( !stack ) stack = qobject_cast( child ); + if( tabBar && stack ) break; + } + + if( !( tabBar && stack ) ) return expandSize( contentsSize, Metrics::TabWidget_MarginWidth ); + + // tab orientation + const bool verticalTabs( tabOption && isVerticalTab( tabOption->shape ) ); + if( verticalTabs ) + { + const int tabBarHeight = tabBar->minimumSizeHint().height(); + const int stackHeight = stack->minimumSizeHint().height(); + if( contentsSize.height() == tabBarHeight && tabBarHeight + 2*(Metrics::Frame_FrameWidth - 1) >= stackHeight + 2*Metrics::TabWidget_MarginWidth ) return QSize( contentsSize.width() + 2*Metrics::TabWidget_MarginWidth, contentsSize.height() + 2*(Metrics::Frame_FrameWidth - 1) ); + else return expandSize( contentsSize, Metrics::TabWidget_MarginWidth ); + + } else { + + const int tabBarWidth = tabBar->minimumSizeHint().width(); + const int stackWidth = stack->minimumSizeHint().width(); + if( contentsSize.width() == tabBarWidth && tabBarWidth + 2*(Metrics::Frame_FrameWidth - 1) >= stackWidth + 2*Metrics::TabWidget_MarginWidth) return QSize( contentsSize.width() + 2*(Metrics::Frame_FrameWidth - 1), contentsSize.height() + 2*Metrics::TabWidget_MarginWidth ); + else return expandSize( contentsSize, Metrics::TabWidget_MarginWidth ); + + } + + } //______________________________________________________________ QSize Style::tabBarTabSizeFromContents( const QStyleOption* option, const QSize& contentsSize, const QWidget* ) const