277 lines
9.1 KiB
C++
277 lines
9.1 KiB
C++
#include "sui_title.h"
|
||
sui_title::sui_title(QWidget *parent)
|
||
: QWidget(parent),
|
||
m_menu(nullptr),
|
||
s_menu(nullptr),
|
||
f_menu(nullptr)
|
||
{
|
||
m_parent = parent;
|
||
m_type=QD_TYPE::QD_EXIT;
|
||
// 给成员变量申请内存
|
||
m_icon_button = new QPushButton(/*this*/);
|
||
m_min_button = new QPushButton(this);
|
||
m_max_button = new QPushButton(this);
|
||
m_close_button = new QPushButton(this);
|
||
// 初始化图标Label
|
||
m_icon_button->setFixedSize(35, 35);
|
||
m_icon_button->setAutoFillBackground(true);
|
||
m_icon_button->setFlat(true);
|
||
|
||
// 设置按钮的固定大小、图片、取消边框
|
||
m_min_button->setIconSize(QSize(25, 25));
|
||
m_min_button->setAutoFillBackground(true);
|
||
m_min_button->setFlat(true);
|
||
//--
|
||
m_max_button->setIconSize(QSize(25, 25));
|
||
m_max_button->setAutoFillBackground(true);
|
||
m_max_button->setFlat(true);
|
||
//--
|
||
m_close_button->setIconSize(QSize(25, 25));
|
||
m_close_button->setAutoFillBackground(true);
|
||
m_close_button->setFlat(true);
|
||
|
||
// 设置窗口部件的名称
|
||
setObjectName(tr("sui_title"));
|
||
m_icon_button->setObjectName(tr("ico_button"));
|
||
m_min_button->setObjectName(tr("min_button"));
|
||
m_max_button->setObjectName(tr("max_button"));
|
||
m_close_button->setObjectName(tr("close_button"));
|
||
// 给按钮设置静态tooltip,当鼠标移上去时显示tooltip
|
||
m_min_button->setToolTip(tr("最小化"));
|
||
m_max_button->setToolTip(tr("最大化"));
|
||
m_close_button->setToolTip(tr("关闭"));
|
||
// 标题栏布局
|
||
m_title_label = new QLabel(this);
|
||
m_title_label->setAlignment(Qt::AlignCenter);
|
||
m_title_label->setObjectName(tr("HEAD_STR"));
|
||
QFont font = m_title_label->font();
|
||
font.setPointSize(28);
|
||
m_title_label->setFont(font);
|
||
m_layout = new QHBoxLayout(this);
|
||
m_layout->setAlignment(Qt::AlignCenter);
|
||
m_layout->addWidget(m_icon_button);
|
||
m_layout->addItem(sparcer_item);
|
||
m_layout->addWidget(m_title_label);
|
||
m_layout->addItem(sparcer_item);
|
||
m_layout->addWidget(m_min_button);
|
||
m_layout->addWidget(m_max_button);
|
||
m_layout->addWidget(m_close_button);
|
||
m_layout->setSpacing(5);
|
||
m_layout->setContentsMargins(5, 0, 5, 0);
|
||
setFixedHeight(40);
|
||
setLayout(m_layout);
|
||
set_font_style();
|
||
connect_slots();
|
||
}
|
||
sui_title::~sui_title()
|
||
{
|
||
}
|
||
|
||
void sui_title::connect_slots()
|
||
{
|
||
// 连接三个按钮的信号槽3
|
||
connect(m_icon_button, SIGNAL(clicked(bool)), this, SLOT(slots_ico_menu()));
|
||
connect(m_min_button, SIGNAL(clicked(bool)), this, SLOT(on_clicked()));
|
||
connect(m_max_button, SIGNAL(clicked(bool)), this, SLOT(on_clicked()));
|
||
connect(m_close_button, SIGNAL(clicked(bool)), this, SLOT(on_clicked()));
|
||
}
|
||
void sui_title::slots_ico_menu()
|
||
{
|
||
m_menu = new QMenu();
|
||
// 外观
|
||
if (!s_menu)
|
||
{
|
||
QActionGroup *s_QGroup = new QActionGroup(this);
|
||
s_menu = new QMenu(tr("外观设置"));
|
||
for (int i = 0; i <= style_table.count() - 1; i++)
|
||
{
|
||
QString sName = QFileInfo(style_table[i]).baseName();
|
||
QAction *s_Action = new QAction(sName, s_QGroup);
|
||
s_Action->setObjectName(QString::number(i));
|
||
s_Action->setCheckable(true);
|
||
if (i == _data.x_config.style)
|
||
{
|
||
s_Action->setChecked(true);
|
||
}
|
||
connect(s_Action, SIGNAL(triggered(bool)), this, SLOT(slots_set_style_change()));
|
||
s_menu->addAction(s_Action);
|
||
}
|
||
}
|
||
// 字体
|
||
if (!f_menu)
|
||
{
|
||
QActionGroup *f_QGroup = new QActionGroup(this);
|
||
f_menu = new QMenu(tr("字体设置"));
|
||
for (int i = 0; i <= font_table.count() - 1; i++)
|
||
{
|
||
QAction *f_Action = new QAction(font_table[i], f_QGroup);
|
||
f_Action->setObjectName(QString::number(i));
|
||
f_Action->setCheckable(true);
|
||
if (i == _data.x_config.font)
|
||
{
|
||
f_Action->setChecked(true);
|
||
}
|
||
connect(f_Action, SIGNAL(triggered(bool)), this, SLOT(slots_set_font_change()));
|
||
f_menu->addAction(f_Action);
|
||
}
|
||
QAction *separator = new QAction(nullptr);
|
||
separator->setSeparator(true);
|
||
f_menu->addAction(separator);
|
||
QActionGroup *b_QGroup = new QActionGroup(this);
|
||
QAction *b_Action = new QAction("字体加粗", b_QGroup);
|
||
b_Action->setObjectName("font_bold");
|
||
b_Action->setCheckable(true);
|
||
if (_data.x_config.font_bold)
|
||
{
|
||
b_Action->setChecked(true);
|
||
}
|
||
connect(b_Action, SIGNAL(triggered(bool)), this, SLOT(slots_set_font_change()));
|
||
f_menu->addAction(b_Action);
|
||
}
|
||
m_menu->addMenu(s_menu);
|
||
m_menu->addMenu(f_menu);
|
||
m_menu->exec(QCursor::pos());
|
||
delete m_menu;
|
||
m_menu = nullptr;
|
||
}
|
||
void sui_title::set_font_style(){
|
||
slots_set_style_change();
|
||
slots_set_font_change();
|
||
}
|
||
void sui_title::slots_set_style_change()
|
||
{
|
||
if (sender() != nullptr)
|
||
{
|
||
_data.x_config.style = sender()->objectName().toInt();
|
||
}
|
||
change_style(_data.x_config.style);
|
||
}
|
||
void sui_title::slots_set_font_change()
|
||
{
|
||
if (sender() != nullptr)
|
||
{
|
||
if (sender()->objectName() == "font_bold")
|
||
{
|
||
if (_data.x_config.font_bold)
|
||
{
|
||
_data.x_config.font_bold = 0;
|
||
((QAction *)sender())->setChecked(false);
|
||
}
|
||
else
|
||
{
|
||
_data.x_config.font_bold = 1;
|
||
((QAction *)sender())->setChecked(true);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_data.x_config.font = sender()->objectName().toInt();
|
||
}
|
||
}
|
||
QFont font;
|
||
if (!font_table[_data.x_config.font].isEmpty())
|
||
{
|
||
font.setFamily(font_table[_data.x_config.font]);
|
||
font.setPointSize(10);
|
||
if (_data.x_config.font_bold)
|
||
{
|
||
font.setBold(true);
|
||
}
|
||
else
|
||
{
|
||
font.setBold(false);
|
||
}
|
||
QApplication::setFont(font);
|
||
}
|
||
else
|
||
{
|
||
qDebug() << "Failed to load font.";
|
||
}
|
||
|
||
change_style(_data.x_config.style);
|
||
}
|
||
void sui_title::change_style(int index)
|
||
{
|
||
_data.x_config.style = index;
|
||
QString style = tr(":/res/qss/") + style_table[index];
|
||
QFile qss(style);
|
||
if (qss.open(QFile::ReadOnly))
|
||
{
|
||
QString styleSheet = this->styleSheet();
|
||
styleSheet += QLatin1String(qss.readAll());
|
||
m_parent->setStyleSheet(styleSheet);
|
||
}
|
||
qss.close();
|
||
m_parent->update();
|
||
}
|
||
|
||
// 双击标题栏进行界面的最大化/还原
|
||
void sui_title::mouseDoubleClickEvent(QMouseEvent *event)
|
||
{
|
||
Q_UNUSED(event); // 没有实质性的作用,只是用来允许event可以不使用,用来避免编译器警告
|
||
emit m_max_button->clicked();
|
||
}
|
||
// 进行最小化、最大化/还原、关闭操作
|
||
void sui_title::on_clicked()
|
||
{
|
||
// QObject::Sender()返回发送信号的对象的指针,返回类型为QObject *
|
||
QPushButton *pButton = qobject_cast<QPushButton *>(sender());
|
||
QWidget *pWindow = this->window(); // 获得标题栏所在的窗口
|
||
if (pWindow->isWindow())
|
||
{
|
||
// 判断发送信号的对象使哪个按钮
|
||
if (pButton == m_min_button)
|
||
{
|
||
pWindow->showMinimized(); // 窗口最小化显示
|
||
}
|
||
else if (pButton == m_max_button)
|
||
{
|
||
pWindow->isFullScreen() ? pWindow->showNormal() : pWindow->showFullScreen();
|
||
}
|
||
else if (pButton == m_close_button)
|
||
{
|
||
if (m_type == QD_TYPE::QD_CLOSE)
|
||
{
|
||
emit signals_close();
|
||
}
|
||
else if (m_type == QD_TYPE::QD_EXIT)
|
||
{
|
||
xsteam_save_data_write();
|
||
QApplication::quit();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
// 最大化/还原
|
||
void sui_title::up_maximize()
|
||
{
|
||
QWidget *pWindow = this->window(); // 获得标题栏所在的窗口
|
||
if (pWindow->isWindow())
|
||
{
|
||
bool bMaximize = pWindow->isFullScreen(); // 判断窗口是不是最大化状态,是则返回true,否则返回false
|
||
if (bMaximize)
|
||
{
|
||
// 目前窗口是最大化状态,则最大化/还原的toolTip设置为"Restore"
|
||
m_max_button->setToolTip(tr("Restore"));
|
||
// 设置按钮的属性名为"maximizeProperty"
|
||
m_max_button->setProperty("maximizeProperty", tr("restore"));
|
||
}
|
||
else
|
||
{
|
||
// 目前窗口是还原状态,则最大化/还原的toolTip设置为"Maximize"
|
||
m_max_button->setToolTip(tr("Maximize"));
|
||
// 设置按钮的属性名为"maximizeProperty"
|
||
m_max_button->setProperty("maximizeProperty", tr("maximize"));
|
||
}
|
||
m_max_button->setStyle(QApplication::style());
|
||
}
|
||
}
|
||
void sui_title::set_type(QD_TYPE _type)
|
||
{
|
||
m_type = _type;
|
||
disconnect(m_icon_button, SIGNAL(clicked(bool)), this, SLOT(slots_ico_menu()));
|
||
}
|
||
void sui_title::set_title(QString str)
|
||
{
|
||
m_title_label->setText(str);
|
||
} |