292 lines
9.0 KiB
C++
292 lines
9.0 KiB
C++
#include "sui_title.h"
|
||
sui_title::sui_title(QWidget *parent)
|
||
: QWidget(parent),
|
||
m_menu(nullptr),
|
||
s_menu(nullptr),
|
||
f_menu(nullptr),
|
||
m_type(QD_TYPE::QD_EXIT)
|
||
{
|
||
m_parent = parent;
|
||
setObjectName(tr("sui_title"));
|
||
for (int i = 0; i <= m_btn_obj_str.count() - 1; i++)
|
||
{
|
||
QPushButton *btn = new QPushButton(this);
|
||
btn->setAutoFillBackground(true);
|
||
btn->setFlat(true);
|
||
btn->setObjectName(m_btn_obj_str[i]);
|
||
btn->setToolTip(m_btn_tips_str[i]);
|
||
m_btn_vectors.push_back(btn);
|
||
}
|
||
// 标题栏布局
|
||
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_btn_vectors[0]);
|
||
m_layout->addItem(sparcer_item);
|
||
m_layout->addWidget(m_title_label);
|
||
m_layout->addItem(sparcer_item);
|
||
m_layout->addWidget(m_btn_vectors[1]);
|
||
m_layout->addWidget(m_btn_vectors[2]);
|
||
m_layout->addWidget(m_btn_vectors[3]);
|
||
m_layout->setSpacing(5);
|
||
m_layout->setContentsMargins(5, 0, 5, 0);
|
||
setMouseTracking(true);
|
||
setFixedHeight(40);
|
||
setLayout(m_layout);
|
||
connect_slots();
|
||
}
|
||
sui_title::~sui_title()
|
||
{
|
||
if (m_thread)
|
||
{
|
||
m_thread->quit();
|
||
m_thread->exit();
|
||
}
|
||
}
|
||
|
||
void sui_title::connect_slots()
|
||
{
|
||
for (auto btn : m_btn_vectors)
|
||
{
|
||
connect(btn, SIGNAL(clicked(bool)), this, SLOT(slots_on_clicked()));
|
||
}
|
||
}
|
||
void sui_title::btn_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 (_data.x_config.style_system)
|
||
{
|
||
s_Action->setEnabled(false);
|
||
}
|
||
else
|
||
{
|
||
connect(s_Action, SIGNAL(triggered(bool)), this, SLOT(slots_change_style()));
|
||
}
|
||
if (i == _data.x_config.style)
|
||
{
|
||
s_Action->setChecked(true);
|
||
}
|
||
s_menu->addAction(s_Action);
|
||
}
|
||
QAction *separator = new QAction(nullptr);
|
||
separator->setSeparator(true);
|
||
s_menu->addAction(separator);
|
||
QActionGroup *t_group = new QActionGroup(this);
|
||
QAction *t_action = new QAction("跟随系统", t_group);
|
||
t_action->setObjectName("track_system");
|
||
t_action->setCheckable(true);
|
||
if (_data.x_config.style_system)
|
||
{
|
||
t_action->setChecked(true);
|
||
}
|
||
connect(t_action, SIGNAL(triggered(bool)), this, SLOT(slots_style_system(bool)));
|
||
s_menu->addAction(t_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_change_font()));
|
||
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_change_font_bold(bool)));
|
||
f_menu->addAction(b_Action);
|
||
}
|
||
m_menu->addMenu(s_menu);
|
||
m_menu->addMenu(f_menu);
|
||
m_menu->exec(QCursor::pos());
|
||
delete m_menu;
|
||
delete s_menu;
|
||
delete f_menu;
|
||
m_menu = nullptr;
|
||
s_menu = nullptr;
|
||
f_menu = nullptr;
|
||
}
|
||
void sui_title::slots_style_system(bool state)
|
||
{
|
||
if (_data.x_config.style_system==1)
|
||
{
|
||
_data.x_config.style_system = 0;
|
||
m_style->state = 0;
|
||
}
|
||
else
|
||
{
|
||
_data.x_config.style_system = 1;
|
||
m_style->state = 1;
|
||
}
|
||
}
|
||
void sui_title::slots_change_font_bold(bool state)
|
||
{
|
||
if (_data.x_config.font_bold)
|
||
{
|
||
_data.x_config.font_bold = 0;
|
||
}
|
||
else
|
||
{
|
||
_data.x_config.font_bold = 1;
|
||
}
|
||
init_font_style();
|
||
}
|
||
void sui_title::slots_change_font()
|
||
{
|
||
if (sender() != nullptr)
|
||
{
|
||
_data.x_config.font = sender()->objectName().toInt();
|
||
}
|
||
init_font_style();
|
||
}
|
||
void sui_title::slots_change_style()
|
||
{
|
||
if (sender() != nullptr)
|
||
{
|
||
_data.x_config.style = sender()->objectName().toInt();
|
||
}
|
||
init_font_style();
|
||
}
|
||
|
||
void sui_title::init_font_style()
|
||
{
|
||
QString style;
|
||
QString qss_font = "QWidget { ";
|
||
qss_font.append("font-family: '" + font_table[_data.x_config.font] + "';");
|
||
if (_data.x_config.font_bold)
|
||
{
|
||
qss_font.append("font-weight: bold;");
|
||
}
|
||
qss_font.append("}");
|
||
|
||
QString style_file = tr(":/res/qss/") + style_table[_data.x_config.style];
|
||
QFile qss(style_file);
|
||
if (qss.open(QFile::ReadOnly))
|
||
{
|
||
style= QLatin1String(qss.readAll());
|
||
style.append(qss_font);
|
||
}
|
||
qss.close();
|
||
QApplication *main_app = static_cast<QApplication *>(QCoreApplication::instance());
|
||
main_app->setStyleSheet(style);
|
||
}
|
||
|
||
// 双击标题栏进行界面的最大化/还原
|
||
void sui_title::mouseDoubleClickEvent(QMouseEvent *event)
|
||
{
|
||
Q_UNUSED(event); // 没有实质性的作用,只是用来允许event可以不使用,用来避免编译器警告
|
||
emit m_btn_vectors[2]->clicked();
|
||
}
|
||
// 进行最小化、最大化/还原、关闭操作
|
||
void sui_title::slots_on_clicked()
|
||
{
|
||
if (sender()->objectName() == "btn_opt")
|
||
{
|
||
btn_ico_menu();
|
||
}
|
||
else if (sender()->objectName() == "btn_min")
|
||
{
|
||
window()->showMinimized();
|
||
}
|
||
else if (sender()->objectName() == "btn_max")
|
||
{
|
||
window()->isFullScreen() ? window()->showNormal() : window()->showFullScreen();
|
||
up_maximize();
|
||
}
|
||
else if (sender()->objectName() == "btn_close")
|
||
{
|
||
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_btn_vectors[2]->setToolTip(tr("Restore"));
|
||
// 设置按钮的属性名为"maximizeProperty"
|
||
m_btn_vectors[2]->setProperty("maximizeProperty", tr("restore"));
|
||
}
|
||
else
|
||
{
|
||
// 目前窗口是还原状态,则最大化/还原的toolTip设置为"Maximize"
|
||
m_btn_vectors[2]->setToolTip(tr("Maximize"));
|
||
// 设置按钮的属性名为"maximizeProperty"
|
||
m_btn_vectors[2]->setProperty("maximizeProperty", tr("maximize"));
|
||
}
|
||
m_btn_vectors[2]->style()->unpolish(m_btn_vectors[2]);
|
||
m_btn_vectors[2]->style()->polish(m_btn_vectors[2]);
|
||
}
|
||
}
|
||
void sui_title::set_type(QD_TYPE _type)
|
||
{
|
||
m_type = _type;
|
||
if (m_type == QD_TYPE::QD_EXIT)
|
||
{
|
||
// 启动系统主题监听线程
|
||
m_style = new sui_sys_style();
|
||
m_style->state = _data.x_config.style_system;
|
||
m_thread = new QThread();
|
||
m_style->moveToThread(m_thread);
|
||
connect(m_style, SIGNAL(signals_sys_style_change(bool)), this, SLOT(slots_style_system(bool)));
|
||
connect(m_thread, SIGNAL(started()), m_style, SLOT(slots_sys_style_thread()));
|
||
m_thread->start();
|
||
}
|
||
else
|
||
{
|
||
disconnect(m_btn_vectors[0], SIGNAL(clicked(bool)), this, SLOT(slots_on_clicked()));
|
||
m_btn_vectors[1]->setVisible(false);
|
||
m_btn_vectors[2]->setVisible(false);
|
||
}
|
||
init_font_style();
|
||
}
|
||
void sui_title::set_title(QString str)
|
||
{
|
||
m_title_label->setText(str);
|
||
} |