Files

229 lines
5.7 KiB
C++
Raw Permalink Normal View History

#include "CustomTitleBar.h"
#include <QMainWindow>
#include <QApplication>
#include <QScreen>
#include <QDesktopWidget>
#include <QStyleOption>
#include <QPainter>
#include <QLinearGradient>
#include <QFontMetrics>
CustomTitleBar::CustomTitleBar(QWidget* parent)
: QWidget(parent)
, m_dragging(false)
, m_titleLabel(nullptr)
, m_iconLabel(nullptr)
, m_minimizeButton(nullptr)
, m_maximizeButton(nullptr)
, m_closeButton(nullptr)
, m_parentWindow(parent)
{
setupUI();
setFixedHeight(32);
setAttribute(Qt::WA_TranslucentBackground, true);
}
void CustomTitleBar::setupUI()
{
QHBoxLayout* layout = new QHBoxLayout(this);
layout->setContentsMargins(8, 0, 0, 0);
layout->setSpacing(8);
// 图标
m_iconLabel = new QLabel(this);
m_iconLabel->setFixedSize(16, 16);
m_iconLabel->setScaledContents(true);
layout->addWidget(m_iconLabel);
// 标题
m_titleLabel = new QLabel(this);
m_titleLabel->setStyleSheet("color: #d4d4d4; font-size: 12px; font-weight: 500;");
layout->addWidget(m_titleLabel);
// 添加弹性空间
layout->addStretch();
// 最小化按钮
m_minimizeButton = new QPushButton("−", this);
m_minimizeButton->setFixedSize(32, 32);
m_minimizeButton->setStyleSheet(
"QPushButton {"
" background: transparent;"
" border: none;"
" color: #d4d4d4;"
" font-size: 14px;"
" font-weight: bold;"
"}"
"QPushButton:hover {"
" background-color: #3e3e42;"
"}"
"QPushButton:pressed {"
" background-color: #007acc;"
"}"
);
connect(m_minimizeButton, &QPushButton::clicked, this, &CustomTitleBar::minimizeWindow);
layout->addWidget(m_minimizeButton);
// 最大化/还原按钮
m_maximizeButton = new QPushButton("", this);
m_maximizeButton->setFixedSize(32, 32);
m_maximizeButton->setStyleSheet(
"QPushButton {"
" background: transparent;"
" border: none;"
" color: #d4d4d4;"
" font-size: 12px;"
" font-weight: bold;"
"}"
"QPushButton:hover {"
" background-color: #3e3e42;"
"}"
"QPushButton:pressed {"
" background-color: #007acc;"
"}"
);
connect(m_maximizeButton, &QPushButton::clicked, this, &CustomTitleBar::maximizeWindow);
layout->addWidget(m_maximizeButton);
// 关闭按钮
m_closeButton = new QPushButton("", this);
m_closeButton->setFixedSize(32, 32);
m_closeButton->setStyleSheet(
"QPushButton {"
" background: transparent;"
" border: none;"
" color: #d4d4d4;"
" font-size: 16px;"
" font-weight: bold;"
"}"
"QPushButton:hover {"
" background-color: #e81123;"
" color: white;"
"}"
"QPushButton:pressed {"
" background-color: #c50e1f;"
" color: white;"
"}"
);
connect(m_closeButton, &QPushButton::clicked, this, &CustomTitleBar::closeWindow);
layout->addWidget(m_closeButton);
updateMaximizeButton();
}
void CustomTitleBar::setTitle(const QString& title)
{
if (m_titleLabel) {
m_titleLabel->setText(title);
}
}
void CustomTitleBar::setWindowIcon(const QIcon& icon)
{
if (m_iconLabel) {
m_iconLabel->setPixmap(icon.pixmap(16, 16));
}
}
void CustomTitleBar::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
// 绘制深色渐变背景
QLinearGradient gradient(0, 0, 0, height());
gradient.setColorAt(0, QColor(37, 37, 38));
gradient.setColorAt(1, QColor(30, 30, 30));
painter.fillRect(rect(), gradient);
// 绘制底部边框
painter.setPen(QColor(60, 60, 60));
painter.drawLine(0, height() - 1, width(), height() - 1);
}
void CustomTitleBar::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton) {
m_dragging = true;
m_dragPosition = event->globalPos() - m_parentWindow->frameGeometry().topLeft();
event->accept();
}
}
void CustomTitleBar::mouseMoveEvent(QMouseEvent* event)
{
if (m_dragging && (event->buttons() & Qt::LeftButton)) {
m_parentWindow->move(event->globalPos() - m_dragPosition);
event->accept();
}
}
void CustomTitleBar::mouseReleaseEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton) {
m_dragging = false;
event->accept();
}
}
void CustomTitleBar::mouseDoubleClickEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton) {
maximizeWindow();
event->accept();
}
}
bool CustomTitleBar::eventFilter(QObject* obj, QEvent* event)
{
if (event->type() == QEvent::WindowStateChange) {
updateMaximizeButton();
}
return QWidget::eventFilter(obj, event);
}
void CustomTitleBar::minimizeWindow()
{
if (m_parentWindow) {
m_parentWindow->showMinimized();
}
}
void CustomTitleBar::maximizeWindow()
{
if (m_parentWindow) {
if (m_parentWindow->isMaximized()) {
m_parentWindow->showNormal();
} else {
m_parentWindow->showMaximized();
}
updateMaximizeButton();
}
}
void CustomTitleBar::closeWindow()
{
if (m_parentWindow) {
m_parentWindow->close();
}
}
void CustomTitleBar::updateMaximizeButton()
{
if (m_maximizeButton && m_parentWindow) {
if (m_parentWindow->isMaximized()) {
m_maximizeButton->setText("❐");
} else {
m_maximizeButton->setText("");
}
}
}
bool CustomTitleBar::isMaximized() const
{
return m_parentWindow ? m_parentWindow->isMaximized() : false;
}
#include "CustomTitleBar.moc"