GridFrame/common/source/titleBar.cpp

141 lines
3.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "titleBar.h"
#include <QStyle>
#include <QScreen>
#include <QGuiApplication>
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);
}
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::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->globalPosition().toPoint() - m_parentWindow->frameGeometry().topLeft();
event->accept();
}
}
void TitleBar::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
m_parentWindow->move(event->globalPosition().toPoint() - m_dragStartPosition);
event->accept();
}
}
void TitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
emit maximizeClicked();
}
}