DiagramDesigner/diagramCavas/source/titleBar.cpp

114 lines
2.9 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)
: QWidget(parent)
, m_parentWindow(parent)
{
setupUI();
setFixedHeight(35); // 稍微矮一点
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: 30px;
min-height: 30px;
}
)";
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::Expanding, QSizePolicy::Preferred);
// 最大化/还原按钮
m_maximizeButton = new QPushButton(tr("🗖")); // 最大化图标
m_maximizeButton->setObjectName("maximizeButton");
m_maximizeButton->setToolTip("最大化");
// 关闭按钮
m_closeButton = new QPushButton("×");
m_closeButton->setObjectName("closeButton");
m_closeButton->setToolTip("关闭");
layout->addWidget(m_titleLabel);
layout->addWidget(m_maximizeButton);
layout->addWidget(m_closeButton);
// 连接信号
connect(m_maximizeButton, &QPushButton::clicked, this, &TitleBar::maximizeClicked);
connect(m_closeButton, &QPushButton::clicked, this, &TitleBar::closeClicked);
}
void TitleBar::setTitle(const QString &title)
{
m_titleLabel->setText(title);
}
void TitleBar::updateMaximizeButton()
{
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();
}
}