From 378ff8bab02edb67110dc8b68694e6bce008fdfe Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Thu, 10 Oct 2024 10:36:25 +0200 Subject: [PATCH] Fix the maximization of sub windows (#7530) ## Fix rendering of maximized sub windows ### Adjustments in paintEvent In `SubWindow::paintEvent` don't paint anything if the sub window is maximized . Otherwise some gradients are visible behind the maximized child content. ### Adjustments in adjustTitleBar In `SubWindow::adjustTitleBar` hide the title label and the buttons if the sub window is maximized. Always show the title and close button if not maximized. This is needed to reset the state correctly after maximization. Remove some calls to `isMaximized` where we already know that the sub window is not maximized, i.e. where these calls would always return `false`. One adjustment would have resulted in a call to `setHidden(false)`. This was changed to `setVisible(true)` to get rid of the double negation. The other `false` would have gotten in an or statement and thus could be removed completely. ### Add method addTitleButton Add the helper method `SubWindow::addTitleButton` to reduce code repetition in the constructor. ### Other changes Remove a dependency on the `MdiArea` when checking if the sub window is the active one. Query its own window state to find out if it is active. When calling `setWindowFlags` in the constructor only adjust the existing flags with the changes that we actually want to do. It was ensured that all other flags that have been set before still apply with this change. --- include/SubWindow.h | 2 ++ src/gui/SubWindow.cpp | 73 ++++++++++++++++++++++++++----------------- 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/include/SubWindow.h b/include/SubWindow.h index d1cc6a7af..c5e3f02d4 100644 --- a/include/SubWindow.h +++ b/include/SubWindow.h @@ -78,6 +78,8 @@ protected: void paintEvent( QPaintEvent * pe ) override; void changeEvent( QEvent * event ) override; + QPushButton* addTitleButton(const std::string& iconName, const QString& toolTip); + signals: void focusLost(); diff --git a/src/gui/SubWindow.cpp b/src/gui/SubWindow.cpp index dc6e49297..1fa0f25a7 100644 --- a/src/gui/SubWindow.cpp +++ b/src/gui/SubWindow.cpp @@ -58,28 +58,13 @@ SubWindow::SubWindow(QWidget *parent, Qt::WindowFlags windowFlags) : m_borderColor = Qt::black; // close, maximize and restore (after maximizing) buttons - m_closeBtn = new QPushButton( embed::getIconPixmap( "close" ), QString(), this ); - m_closeBtn->resize( m_buttonSize ); - m_closeBtn->setFocusPolicy( Qt::NoFocus ); - m_closeBtn->setCursor( Qt::ArrowCursor ); - m_closeBtn->setAttribute( Qt::WA_NoMousePropagation ); - m_closeBtn->setToolTip( tr( "Close" ) ); + m_closeBtn = addTitleButton("close", tr("Close")); connect( m_closeBtn, SIGNAL(clicked(bool)), this, SLOT(close())); - m_maximizeBtn = new QPushButton( embed::getIconPixmap( "maximize" ), QString(), this ); - m_maximizeBtn->resize( m_buttonSize ); - m_maximizeBtn->setFocusPolicy( Qt::NoFocus ); - m_maximizeBtn->setCursor( Qt::ArrowCursor ); - m_maximizeBtn->setAttribute( Qt::WA_NoMousePropagation ); - m_maximizeBtn->setToolTip( tr( "Maximize" ) ); + m_maximizeBtn = addTitleButton("maximize", tr("Maximize")); connect( m_maximizeBtn, SIGNAL(clicked(bool)), this, SLOT(showMaximized())); - m_restoreBtn = new QPushButton( embed::getIconPixmap( "restore" ), QString(), this ); - m_restoreBtn->resize( m_buttonSize ); - m_restoreBtn->setFocusPolicy( Qt::NoFocus ); - m_restoreBtn->setCursor( Qt::ArrowCursor ); - m_restoreBtn->setAttribute( Qt::WA_NoMousePropagation ); - m_restoreBtn->setToolTip( tr( "Restore" ) ); + m_restoreBtn = addTitleButton("restore", tr("Restore")); connect( m_restoreBtn, SIGNAL(clicked(bool)), this, SLOT(showNormal())); // QLabel for the window title and the shadow effect @@ -93,10 +78,9 @@ SubWindow::SubWindow(QWidget *parent, Qt::WindowFlags windowFlags) : m_windowTitle->setAttribute( Qt::WA_TransparentForMouseEvents, true ); m_windowTitle->setGraphicsEffect( m_shadow ); - // disable the minimize button - setWindowFlags( Qt::SubWindow | Qt::WindowMaximizeButtonHint | - Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint | - Qt::CustomizeWindowHint ); + // Disable the minimize button and make sure that the custom window hint is set + setWindowFlags((this->windowFlags() & ~Qt::WindowMinimizeButtonHint) | Qt::CustomizeWindowHint); + connect( mdiArea(), SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(focusChanged(QMdiSubWindow*))); } @@ -111,12 +95,14 @@ SubWindow::SubWindow(QWidget *parent, Qt::WindowFlags windowFlags) : */ void SubWindow::paintEvent( QPaintEvent * ) { + // Don't paint any of the other stuff if the sub window is maximized + // so that only its child content is painted. + if (isMaximized()) { return; } + QPainter p( this ); QRect rect( 0, 0, width(), m_titleBarHeight ); - bool isActive = mdiArea() - ? mdiArea()->activeSubWindow() == this - : false; + const bool isActive = windowState() & Qt::WindowActive; p.fillRect( rect, isActive ? activeColor() : p.pen().brush() ); @@ -295,9 +281,28 @@ void SubWindow::moveEvent( QMoveEvent * event ) */ void SubWindow::adjustTitleBar() { + // Don't show the title or any button if the sub window is maximized. Otherwise they + // might show up behind the actual maximized content of the child widget. + if (isMaximized()) + { + m_closeBtn->hide(); + m_maximizeBtn->hide(); + m_restoreBtn->hide(); + m_windowTitle->hide(); + + return; + } + + // The sub window is not maximized, i.e. the title must be shown + // as well as some buttons. + + // Title adjustments + m_windowTitle->show(); + // button adjustments m_maximizeBtn->hide(); m_restoreBtn->hide(); + m_closeBtn->show(); const int rightSpace = 3; const int buttonGap = 1; @@ -322,12 +327,12 @@ void SubWindow::adjustTitleBar() buttonBarWidth = buttonBarWidth + m_buttonSize.width() + buttonGap; m_maximizeBtn->move( middleButtonPos ); m_restoreBtn->move( middleButtonPos ); - m_maximizeBtn->setHidden( isMaximized() ); + m_maximizeBtn->setVisible(true); } // we're keeping the restore button around if we open projects // from older versions that have saved minimized windows - m_restoreBtn->setVisible( isMaximized() || isMinimized() ); + m_restoreBtn->setVisible(isMinimized()); if( isMinimized() ) { m_restoreBtn->move( m_maximizeBtn->isHidden() ? middleButtonPos : leftButtonPos ); @@ -403,5 +408,17 @@ void SubWindow::resizeEvent( QResizeEvent * event ) } } +QPushButton* SubWindow::addTitleButton(const std::string& iconName, const QString& toolTip) +{ + auto button = new QPushButton(embed::getIconPixmap(iconName), QString(), this); + button->resize(m_buttonSize); + button->setFocusPolicy(Qt::NoFocus); + button->setCursor(Qt::ArrowCursor); + button->setAttribute(Qt::WA_NoMousePropagation); + button->setToolTip(toolTip); -} // namespace lmms::gui \ No newline at end of file + return button; +} + + +} // namespace lmms::gui