1
0
mirror of https://github.com/githubuser0xFFFF/Qt-Advanced-Docking-System.git synced 2025-04-01 02:42:39 +08:00

add maximize button to FloatingWidgetTitleBar

This commit is contained in:
helywin 2019-09-12 10:44:12 +08:00
parent a05078c947
commit bfbbadd7cc
4 changed files with 117 additions and 0 deletions

View File

@ -44,6 +44,7 @@
#include "DockOverlay.h"
#ifdef Q_OS_LINUX
#include <QScreen>
#include "linux/FloatingWidgetTitleBar.h"
#include <xcb/xcb.h>
#endif
@ -67,6 +68,7 @@ struct FloatingDockContainerPrivate
#ifdef Q_OS_LINUX
QWidget* MouseEventHandler = nullptr;
CFloatingWidgetTitleBar* TitleBar = nullptr;
QRect LastGeometry;
#endif
/**
@ -248,7 +250,10 @@ CFloatingDockContainer::CFloatingDockContainer(CDockManager *DockManager) :
QDockWidget::setFloating(true);
QDockWidget::setFeatures(QDockWidget::AllDockWidgetFeatures);
setTitleBarWidget(d->TitleBar);
d->LastGeometry = geometry();
connect(d->TitleBar, SIGNAL(closeRequested()), SLOT(close()));
connect(d->TitleBar, &CFloatingWidgetTitleBar::maximizeRequested,
this, &CFloatingDockContainer::onMaximizeRequest);
#else
setWindowFlags(
Qt::Window | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint);
@ -623,6 +628,51 @@ void CFloatingDockContainer::finishDragging()
#endif
}
#ifdef Q_OS_LINUX
void CFloatingDockContainer::onMaximizeRequest()
{
ADS_PRINT("CFloatingDockContainer::onMaximizeRequest()");
QRect screenRect;
QRect widgetRect;
const QScreen *currentScreen = nullptr;
int maxArea = 0;
// get current screen
for (const auto &screen : QGuiApplication::screens())
{
screenRect = screen->geometry();
widgetRect = this->geometry();
auto minX = qMax(screenRect.left(), widgetRect.left());
auto minY = qMax(screenRect.top(), widgetRect.top());
auto maxX = qMin(screenRect.right(), widgetRect.right());
auto maxY = qMin(screenRect.bottom(), widgetRect.bottom());
auto area = minX < maxX && minY < maxY ? (maxX - minX) * (maxY * minY) : 0;
if (area > maxArea)
{
maxArea = area;
currentScreen = screen;
}
}
if (!currentScreen)
{
return;
}
ADS_PRINT("CFloatingDockContainer::onMaximizeRequest() current screen: " + currentScreen->name());
// get current windows state, if it is maximized and moved or not
if (geometry().size() == currentScreen->availableGeometry().size())
{
setGeometry(d->LastGeometry);
d->TitleBar->setMaximizedIcon(false);
}
else
{
d->LastGeometry = geometry();
setGeometry(currentScreen->availableGeometry());
d->TitleBar->setMaximizedIcon(true);
}
}
#endif
} // namespace ads
//---------------------------------------------------------------------------

View File

@ -201,6 +201,15 @@ public:
* function of the internal container widget.
*/
QList<CDockWidget*> dockWidgets() const;
#ifdef Q_OS_LINUX
/**
* This is a function respond to FloatingWidgetTitleBar::maximizeRequest()
* maximize or normalize the container size.
*/
void onMaximizeRequest();
#endif
}; // class FloatingDockContainer
}
// namespace ads

View File

@ -46,6 +46,7 @@ namespace ads
using tTabLabel = CElidingLabel;
using tCloseButton = QPushButton;
using tMaximizeButton = QPushButton;
/**
* @brief Private data class of public interface CFloatingWidgetTitleBar
@ -56,6 +57,7 @@ struct FloatingWidgetTitleBarPrivate
QLabel *IconLabel = nullptr;
tTabLabel *TitleLabel;
tCloseButton *CloseButton = nullptr;
tMaximizeButton* MaximizeButton = nullptr;
CFloatingDockContainer *FloatingWidget = nullptr;
eDragState DragState = DraggingInactive;
@ -82,6 +84,9 @@ void FloatingWidgetTitleBarPrivate::createLayout()
CloseButton = new tCloseButton();
CloseButton->setObjectName("floatingTitleCloseButton");
CloseButton->setFlat(true);
MaximizeButton = new tMaximizeButton();
MaximizeButton->setObjectName("floatingTitleMaximizeButton");
MaximizeButton->setFlat(true);
// The standard icons do does not look good on high DPI screens
QIcon CloseIcon;
@ -97,6 +102,12 @@ void FloatingWidgetTitleBarPrivate::createLayout()
CloseButton->setFocusPolicy(Qt::NoFocus);
_this->connect(CloseButton, SIGNAL(clicked()), SIGNAL(closeRequested()));
_this->setMaximizedIcon(false);
MaximizeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
MaximizeButton->setVisible(true);
MaximizeButton->setFocusPolicy(Qt::NoFocus);
_this->connect(MaximizeButton, &QPushButton::clicked, _this, &CFloatingWidgetTitleBar::maximizeRequested);
QFontMetrics fm(TitleLabel->font());
int Spacing = qRound(fm.height() / 4.0);
@ -107,6 +118,7 @@ void FloatingWidgetTitleBarPrivate::createLayout()
_this->setLayout(Layout);
Layout->addWidget(TitleLabel, 1);
Layout->addSpacing(Spacing);
Layout->addWidget(MaximizeButton);
Layout->addWidget(CloseButton);
Layout->setAlignment(Qt::AlignCenter);
@ -180,4 +192,38 @@ void CFloatingWidgetTitleBar::setTitle(const QString &Text)
d->TitleLabel->setText(Text);
}
void CFloatingWidgetTitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton)
{
emit maximizeRequested();
event->accept();
}
else
{
QWidget::mouseDoubleClickEvent(event);
}
}
void CFloatingWidgetTitleBar::setMaximizedIcon(bool maximized)
{
if (maximized)
{
QIcon normalIcon;
auto normalPixmap = this->style()->standardPixmap(QStyle::SP_TitleBarNormalButton, 0, d->MaximizeButton);
normalIcon.addPixmap(normalPixmap, QIcon::Normal);
normalIcon.addPixmap(internal::createTransparentPixmap(normalPixmap, 0.25), QIcon::Disabled);
d->MaximizeButton->setIcon(normalIcon);
}
else
{
QIcon MaxIcon;
auto maxPixmap = this->style()->standardPixmap(QStyle::SP_TitleBarMaxButton, 0, d->MaximizeButton);
MaxIcon.addPixmap(maxPixmap, QIcon::Normal);
MaxIcon.addPixmap(internal::createTransparentPixmap(maxPixmap, 0.25), QIcon::Disabled);
d->MaximizeButton->setIcon(MaxIcon);
}
}
} // namespace ads

View File

@ -55,6 +55,7 @@ protected:
virtual void mousePressEvent(QMouseEvent *ev) override;
virtual void mouseReleaseEvent(QMouseEvent *ev) override;
virtual void mouseMoveEvent(QMouseEvent *ev) override;
virtual void mouseDoubleClickEvent(QMouseEvent *event) override;
public:
using Super = QWidget;
@ -75,11 +76,22 @@ public:
*/
void setTitle(const QString &Text);
/**
* Change the maximize button icon according to current windows state
*/
void setMaximizedIcon(bool maximized);
signals:
/**
* This signal is emitted, if the close button is clicked.
*/
void closeRequested();
/**
* This signal is emitted, if the maximize button is clicked.
*/
void maximizeRequested();
};
} // namespace ads
#endif // FLOATINGWIDGETTITLEBAR_H