212 lines
5.2 KiB
C++
212 lines
5.2 KiB
C++
#include "titleBar.h"
|
||
#include <QStyle>
|
||
#include <QScreen>
|
||
#include <QGuiApplication>
|
||
#include <QTimer>
|
||
#include <QWindow>
|
||
|
||
TitleBar::TitleBar(QWidget *parent,bool bClose,bool bMaxMin)
|
||
: QWidget(parent)
|
||
, m_parentWindow(parent)
|
||
,_bShowClose(bClose)
|
||
,_bShowMaxMin(bMaxMin)
|
||
{
|
||
setupUI();
|
||
setFixedHeight(25); // 稍微矮一点
|
||
|
||
setAttribute(Qt::WA_StyledBackground, true); // 启用样式背景
|
||
setAttribute(Qt::WA_NoSystemBackground, false);
|
||
|
||
// 确保有自己的调色板
|
||
setAutoFillBackground(true);
|
||
|
||
// 使用更具体的样式表
|
||
QString style = R"(
|
||
QWidget {
|
||
background: #2c5282;
|
||
}
|
||
|
||
QLabel {
|
||
color: white;
|
||
font-size: 12px;
|
||
background: transparent;
|
||
}
|
||
|
||
QPushButton {
|
||
background: transparent;
|
||
border: none;
|
||
color: white;
|
||
min-width: 20px;
|
||
min-height: 20px;
|
||
}
|
||
)";
|
||
|
||
setStyleSheet(style);
|
||
|
||
// ✅ 每次 show 都居中
|
||
if (m_parentWindow) {
|
||
connect(m_parentWindow, &QWidget::show,
|
||
this, &TitleBar::centerWindowOnce);
|
||
}
|
||
}
|
||
|
||
void TitleBar::setupUI()
|
||
{
|
||
QHBoxLayout *layout = new QHBoxLayout(this);
|
||
layout->setContentsMargins(4, 0, 0, 0);
|
||
layout->setSpacing(0);
|
||
|
||
// 标题
|
||
m_titleLabel = new QLabel("窗口标题");
|
||
m_titleLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
|
||
|
||
layout->addWidget(m_titleLabel);
|
||
|
||
/* ===== 中间占位,用于居中 ===== */
|
||
layout->addStretch();
|
||
|
||
/* ===== 中间内容 Label ===== */
|
||
m_centerLabel = new QLabel();
|
||
m_centerLabel->setAlignment(Qt::AlignCenter);
|
||
m_centerLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
|
||
|
||
layout->addWidget(m_centerLabel);
|
||
|
||
/* ===== 右侧占位 ===== */
|
||
layout->addStretch();
|
||
|
||
if(_bShowMaxMin){
|
||
// 最大化/还原按钮
|
||
m_maximizeButton = new QPushButton(tr("🗖")); // 最大化图标
|
||
m_maximizeButton->setObjectName("maximizeButton");
|
||
m_maximizeButton->setToolTip("最大化");
|
||
|
||
layout->addWidget(m_maximizeButton);
|
||
connect(m_maximizeButton, &QPushButton::clicked, this, &TitleBar::maximizeClicked);
|
||
}
|
||
|
||
if(_bShowClose){
|
||
// 关闭按钮
|
||
m_closeButton = new QPushButton("×");
|
||
m_closeButton->setObjectName("closeButton");
|
||
m_closeButton->setToolTip("关闭");
|
||
|
||
layout->addWidget(m_closeButton);
|
||
connect(m_closeButton, &QPushButton::clicked, this, &TitleBar::closeClicked);
|
||
}
|
||
}
|
||
|
||
void TitleBar::centerWindowOnce()
|
||
{
|
||
if (!m_parentWindow)
|
||
return;
|
||
|
||
if (m_parentWindow->isMaximized() ||
|
||
m_parentWindow->isFullScreen())
|
||
return;
|
||
|
||
#ifdef Q_OS_WIN
|
||
QTimer::singleShot(0, this, [this] {
|
||
QScreen *screen = m_parentWindow->screen();
|
||
if (!screen)
|
||
screen = QGuiApplication::primaryScreen();
|
||
if (!screen)
|
||
return;
|
||
|
||
QRect screenRect = screen->availableGeometry();
|
||
QRect windowRect = m_parentWindow->geometry();
|
||
|
||
m_parentWindow->move(
|
||
screenRect.center() - windowRect.center()
|
||
);
|
||
});
|
||
|
||
#elif defined(Q_OS_MACOS)
|
||
QTimer::singleShot(0, this, [this] {
|
||
QScreen *screen = m_parentWindow->screen();
|
||
if (!screen)
|
||
screen = QGuiApplication::primaryScreen();
|
||
if (!screen)
|
||
return;
|
||
|
||
QRect screenRect = screen->availableGeometry();
|
||
QRect windowRect = m_parentWindow->geometry();
|
||
|
||
m_parentWindow->move(
|
||
screenRect.center() - windowRect.center()
|
||
);
|
||
});
|
||
#endif
|
||
// ✅ Linux:什么都不做,交给窗口管理器
|
||
}
|
||
|
||
void TitleBar::setTitle(const QString &title)
|
||
{
|
||
m_titleLabel->setText(title);
|
||
}
|
||
|
||
void TitleBar::setCenterText(const QString &text)
|
||
{
|
||
if (m_centerLabel)
|
||
m_centerLabel->setText(text);
|
||
}
|
||
|
||
void TitleBar::updateMaximizeButton()
|
||
{
|
||
if(_bShowMaxMin){
|
||
if (m_parentWindow) {
|
||
m_isMaximized = m_parentWindow->isMaximized();
|
||
if (m_isMaximized) {
|
||
m_maximizeButton->setText(tr("🗗")); // 还原图标
|
||
m_maximizeButton->setToolTip("还原");
|
||
} else {
|
||
m_maximizeButton->setText(tr("🗖")); // 最大化图标
|
||
m_maximizeButton->setToolTip("最大化");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void TitleBar::mousePressEvent(QMouseEvent *event)
|
||
{
|
||
if (event->button() == Qt::LeftButton) {
|
||
m_dragStartPosition = event->pos();
|
||
event->accept();
|
||
}
|
||
}
|
||
|
||
void TitleBar::mouseMoveEvent(QMouseEvent *event)
|
||
{
|
||
if (!(event->buttons() & Qt::LeftButton))
|
||
return;
|
||
|
||
#ifdef Q_OS_WIN
|
||
m_parentWindow->move(
|
||
m_parentWindow->pos() +
|
||
(event->pos() - m_dragStartPosition)
|
||
);
|
||
|
||
#elif defined(Q_OS_MACOS)
|
||
// ✅ macOS:Qt 原生拖拽
|
||
m_parentWindow->move(
|
||
m_parentWindow->pos() +
|
||
(event->pos() - m_dragStartPosition)
|
||
);
|
||
|
||
#elif defined(Q_OS_LINUX)
|
||
QWindow *win = m_parentWindow->windowHandle();
|
||
if (win && win->handle()) {
|
||
win->startSystemMove();
|
||
}
|
||
#endif
|
||
|
||
event->accept();
|
||
}
|
||
|
||
void TitleBar::mouseDoubleClickEvent(QMouseEvent *event)
|
||
{
|
||
if (event->button() == Qt::LeftButton) {
|
||
emit maximizeClicked();
|
||
}
|
||
}
|