Files
2024-02-27 15:29:15 +08:00

394 lines
12 KiB
C++

#include "titlebar.h"
#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QEvent>
#include <QMouseEvent>
#include <QApplication>
TitleBar::TitleBar(QWidget *parent):QWidget(parent)
{
setMouseTracking(true);
QString m_style = "QWidget{background-color: rgb(70, 70, 70)color:rgb(219,219,219);}";
this->setStyleSheet(m_style);
TitleBarInit();
SignalSlotsInit();
}
TitleBar::~TitleBar()
{
if(m_ZhButton)
{
delete m_ZhButton;
m_ZhButton = nullptr;
}
if(m_pIconLabel)
{
delete m_pIconLabel;
m_pIconLabel = nullptr;
}
if(m_FileButton)
{
delete m_FileButton;
m_FileButton = nullptr;
}
qDebug() << " [INFO] " << " ~TitleBar success! ";
}
void TitleBar::TitleBarInit()
{
QDir currPath = QApplication::applicationDirPath();
m_pIconLabel = new QLabel(this);
m_FileButton = new ButtonMenu(this);
m_SetButton = new ButtonMenu(this);
m_ZhButton = new QToolButton(this);
m_EnButton = new QToolButton(this);
m_UserButton = new QToolButton(this);
m_pMinimizeButton = new QToolButton(this);
m_pMaximizeButton = new QToolButton(this);
m_pCloseButton = new QToolButton(this);
QString tip_style = "QToolTip{border:1px solid rgb(50,50,50); background-color: rgb(50,50,50); color:rgb(255,255,255); font-size:12px;}";
QString menu_style = "ButtonMenu::menu-indicator{image: none;}"\
"ButtonMenu{font-family:Microsoft YaHei;font-size:15px;color:rgb(219,219,219);background-color: rgb(80, 80, 80);border:0px}"\
"ButtonMenu:hover{background-color: rgb(30,30,30);border:0px}"+tip_style;
m_pIconLabel->setFixedSize(70,38);
m_pIconLabel->setMouseTracking(true);
m_pIconLabel->setAlignment(Qt::AlignVCenter|Qt::AlignHCenter);
m_pIconLabel->setContentsMargins(0,0,10,0);
m_pIconLabel->setStyleSheet("QLabel{border-right:5px solid qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop:0 rgb(155,155,155),stop: 0.2 rgb(100,100,100),stop: 1 rgb(10,10,10));}");
/// LOGO
QIcon icon(currPath.path() + "/Icon/logo_Dongsheng.png");
m_pIconLabel->setPixmap(icon.pixmap(QSize(42,34)));
m_pIconLabel->hide();//隐藏logo
QToolBar * window_left_toolbar = new QToolBar(this);
window_left_toolbar->setMouseTracking(true);
window_left_toolbar->setOrientation(Qt::Horizontal);
window_left_toolbar->setMinimumHeight(38);
window_left_toolbar->layout()->setContentsMargins(5,5,5,5);
window_left_toolbar->setStyleSheet( "QToolBar{spacing: 12px;}");
QToolBar * window_right_toolbar = new QToolBar(this);
window_right_toolbar->setMouseTracking(true);
window_right_toolbar->setOrientation(Qt::Horizontal);
window_right_toolbar->setMinimumHeight(38);
window_right_toolbar->setStyleSheet( "QToolBar{spacing: 12px;}QToolBar::separator{background:qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop:0 rgb(155,155,155),stop: 0.2 rgb(100,100,100),stop: 1 rgb(10,10,10));width:5px;}");
m_FileButton->setFixedSize(45, 30);
m_SetButton->setFixedSize(45, 30);
m_ZhButton->setFixedSize(25, 25);
m_EnButton->setFixedSize(25, 25);
m_UserButton->setFixedSize(25, 25);
m_pMinimizeButton->setFixedSize(25, 25);
m_pMaximizeButton->setFixedSize(25, 25);
m_pCloseButton->setFixedSize(25, 25);
translateBtn();
QString QToolbutton_style_sheet ="QToolButton{font-size: 20px;color:rgb(147,147,147);background-color: rgb(80, 80, 80);border:0px}"
"QToolButton:hover{color:rgb(147,147,147);background-color: rgb(53, 53, 53);border:0px}"+tip_style;
m_ZhButton->setStyleSheet(QToolbutton_style_sheet);
m_EnButton->setStyleSheet(QToolbutton_style_sheet);
m_UserButton->setStyleSheet(QToolbutton_style_sheet);
m_pMinimizeButton->setStyleSheet(QToolbutton_style_sheet);
m_pMaximizeButton->setStyleSheet(QToolbutton_style_sheet);
m_pCloseButton->setStyleSheet(QToolbutton_style_sheet);
QPixmap m_map;
m_map.load(currPath.path() + "/Icon/user_n.png");
m_map = m_map.scaled(25,25,Qt::KeepAspectRatio);
m_UserButton->setIcon(m_map);
m_map.load(currPath.path() + "/Icon/min_n.png");
m_map = m_map.scaled(25,25,Qt::KeepAspectRatio);
m_pMinimizeButton->setIcon(m_map);
m_map.load( currPath.path() + "/Icon/Max_n.png");
m_map = m_map.scaled(25,25,Qt::KeepAspectRatio);
m_pMaximizeButton->setIcon(m_map);
m_map.load(currPath.path() + "/Icon/close_n.png");
m_map = m_map.scaled(25,25,Qt::KeepAspectRatio);
m_pCloseButton->setIcon(m_map);
QHBoxLayout *mainWidgetLayout = new QHBoxLayout(this);
QWidget *mainWidget = new QWidget;
mainWidget->setMouseTracking(true);
QHBoxLayout *pLayout = new QHBoxLayout;
//添加Act
m_FileButton->AddFileAction();
m_FileButton->setStyleSheet(menu_style);
m_SetButton->AddSetAction();
m_SetButton->setStyleSheet(menu_style);
mainWidgetLayout->addWidget(mainWidget);
mainWidget->setLayout(pLayout);
mainWidgetLayout->setMargin(0);
pLayout->setContentsMargins(7,0,7,0);
pLayout->setSpacing(12);
mainWidget->setStyleSheet("QWidget{background-color:rgb(80,80,80);}");
//添加到左侧工具栏
window_left_toolbar->addWidget(m_FileButton);
window_left_toolbar->addWidget(m_SetButton);
window_right_toolbar->addWidget(m_EnButton);
window_right_toolbar->addWidget(m_ZhButton);
window_right_toolbar->addWidget(m_UserButton);
window_right_toolbar->addWidget(m_pMinimizeButton);
window_right_toolbar->addWidget(m_pMaximizeButton);
window_right_toolbar->addWidget(m_pCloseButton);
pLayout->addWidget(m_pIconLabel);
pLayout->addWidget(window_left_toolbar);
pLayout->addStretch();
pLayout->addWidget(window_right_toolbar);
}
/**
* @ProjectName QtHanddleAI
* @Function 中英文翻译时分开部署
* @Author ZhouHongDe
* @Datetime 2022-02-15 14:42:55
*/
void TitleBar::translateBtn()
{
m_FileButton->setToolTip(tr("文件"));
m_SetButton->setToolTip(tr("设置"));
m_ZhButton->setToolTip(tr("中文"));
m_EnButton->setToolTip(tr("英文"));
m_UserButton->setToolTip(tr("用户"));
m_pMinimizeButton->setToolTip(tr("最小化"));
m_pMaximizeButton->setToolTip(tr("最大化"));
m_pCloseButton->setToolTip(tr("关闭"));
m_EnButton->setText("En");
m_ZhButton->setText("中");
m_FileButton->setText(tr("文件"));
m_SetButton->setText(tr("设置"));
m_pMinimizeButton->setObjectName(tr("最小化"));
m_pMaximizeButton->setObjectName(tr("最大化"));
m_pCloseButton->setObjectName(tr("关闭"));
}
void TitleBar::SignalSlotsInit()
{
connect(m_pMinimizeButton, SIGNAL(clicked(bool)), this, SLOT(onClicked()));
connect(m_pMaximizeButton, SIGNAL(clicked(bool)), this, SLOT(onClicked()));
connect(m_pCloseButton, SIGNAL(clicked(bool)), this, SLOT(onClicked()));
connect(m_FileButton,SIGNAL(OpenProJectSignal(QString)),this,SLOT(OnOpenProject(QString))); //打开配方
connect(m_FileButton,SIGNAL(SaveProJectSignal(QString)),this,SLOT(OnSaveProject(QString))); //保存配方
connect(m_FileButton,SIGNAL(CloseWindows()),this,SLOT(OnCloseProject())); //关闭
//设置按钮
connect(m_SetButton,SIGNAL(showstatisticalTable()),this,SLOT(OnShowResultTable())); //显示统计报表
//中英文切换
connect(m_EnButton,SIGNAL(clicked(bool)),this,SLOT(On_SwitchToEnglish())); //切换成英文
connect(m_ZhButton,SIGNAL(clicked(bool)),this,SLOT(On_SwitchToChinese())); //切换成英文
}
void TitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
Q_UNUSED(event);
// window()->isMaximized() ? window()->showNormal() : window()->showMaximized();
}
bool TitleBar::eventFilter(QObject *obj, QEvent *event)
{
switch (event->type())
{
case QEvent::Resize:
{
updateMaximize();
return true;
}
default:
return QWidget::eventFilter(obj, event);
}
return true;
}
void TitleBar::onClicked()
{
QToolButton *pButton = qobject_cast<QToolButton *>(sender());
if (window()->isTopLevel())
{
if (pButton == m_pMinimizeButton)
{
window()->showMinimized();
}
else if (pButton == m_pMaximizeButton)
{
window()->isMaximized() ? window()->showNormal() : window()->showMaximized();
}
else if (pButton == m_pCloseButton)
{
emit close_mainwidget();
window()->close();
}
}
}
void TitleBar::OnOpenProject(QString openProjectName)
{
emit OpenProjectSignal(openProjectName);
}
void TitleBar::OnSaveProject(QString saveProjectName)
{
emit SaveProjectSignal(saveProjectName);
}
void TitleBar::updateMaximize()
{
QWidget *pWindow = this->window();
if (pWindow->isTopLevel())
{
bool bMaximize = pWindow->isMaximized();
if (bMaximize)
{
m_pMaximizeButton->setToolTip(tr("恢复"));
m_pMaximizeButton->setProperty("maximizeProperty", "restore");
}
else
{
m_pMaximizeButton->setProperty("maximizeProperty", "maximize");
m_pMaximizeButton->setToolTip(tr("最大化"));
QPoint widgetPos = this->parentWidget()->pos();
this->parentWidget()->move(widgetPos.x() + 0, widgetPos.y() + 0);
}
m_pMaximizeButton->setStyle(QApplication::style());
}
}
void TitleBar::OnCloseProject()
{
emit close_mainwidget();
window()->close();
}
void TitleBar::OnShowResultTable()
{
emit showStatisticalTableSignal();
}
void TitleBar::mousePressEvent( QMouseEvent *event )
{
if ( m_buttonType == MIN_MAX_BUTTON )
{
if (m_pButtonMax->isVisible() )
{
m_isPressed = true;
m_startMovePos = event->globalPos();
}
}
else if ( event->button() == Qt::LeftButton )
{
m_isPressed = true;
m_startMovePos = event->globalPos();
}
return QWidget::mousePressEvent(event);
}
void TitleBar::mouseMoveEvent(QMouseEvent *event)
{
setCursor(Qt::ArrowCursor);
emit mouseRecover();
if (m_isPressed)
{
QWidget *pWindow = this->window();
if(pWindow->isMaximized())
{
// QRect rect = parentWidget()->normalGeometry();
// int desX = m_startMovePos.x()*rect.width()/parentWidget()->geometry().width();
// int desY = m_startMovePos.y();
// rect.moveTopLeft(event->globalPos() - QPoint(desX,desY));
// parentWidget()->showNormal();
// parentWidget()->setGeometry(rect);
// m_startMovePos = QPoint(desX,desY);
}
else
{
m_mouse_move = event->globalPos();
window()->move(window()->pos()+m_mouse_move-m_startMovePos);
m_startMovePos = m_mouse_move;
}
}
return QWidget::mouseMoveEvent(event);
}
void TitleBar::mouseReleaseEvent(QMouseEvent *event)
{
m_isPressed = false;
return QWidget::mouseReleaseEvent(event);
}
void TitleBar::On_ConnectToHost(QString ip,QString port,bool flag)
{
if(flag)
{
if ( !ip.isEmpty() || !port.isEmpty() )
emit ConnectToHostSignal(ip,port);
}
}
/**
* @ProjectName QtHanddleAI
* @Function 切换成英文
* @Author ZhouHongDe
* @Datetime 2022-01-14 13:31:58
*/
void TitleBar::On_SwitchToEnglish()
{
emit switchLanguageSignal("language_en");
}
/**
* @ProjectName QtHanddleAI
* @Function 切换成中文
* @Author ZhouHongDe
* @Datetime 2022-01-14 13:45:23
*/
void TitleBar::On_SwitchToChinese()
{
emit switchLanguageSignal("language_ch");
}
void TitleBar::switchMenu()
{
m_FileButton->FileTranslateBtn();
m_SetButton->SetTranslateBtn();
}
void TitleBar::setScreenSize()
{
}