opencv_mv/QPlugins/GeometryTools/FitLine/mytitlebar.cpp

301 lines
7.5 KiB
C++
Raw Permalink Normal View History

2023-05-22 09:27:46 +00:00
#include "mytitlebar.h"
#include <QHBoxLayout>
#include <QPainter>
#include <QFile>
#include <QMouseEvent>
#include <QDebug>
#define BUTTON_HEIGHT 30 //<2F><>ť<EFBFBD>߶<EFBFBD>
#define BUTTON_WIDTH 30 //<2F><>ť<EFBFBD><C5A5><EFBFBD><EFBFBD>
#define TITLE_HEIGHT 35 //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߶<EFBFBD>
MyTitleBar::MyTitleBar(QWidget *parent)
: QWidget(parent)
, m_colorR(120)
, m_colorG(120)
, m_colorB(120)
, m_isPressed(false)
, m_buttonType(MIN_MAX_BUTTON)
{
//<2F><>ʼ<EFBFBD><CABC>
initControl();
initConnections();
loadStyleSheet("MyTitle");
}
MyTitleBar::~MyTitleBar()
{
}
//<2F><>ʼ<EFBFBD><CABC><EFBFBD>ؼ<EFBFBD>
void MyTitleBar::initControl()
{
//<2F><>ť<EFBFBD><C5A5>ʽ
QString btnStyle(
"QWidget{"
"border: none;background-color: rgba(0, 0, 0, 0);"
"}"
"QToolTip{"
"border: none;background-color: rgb(235, 235, 255);color: rgb(45, 45, 45);font-size:15px;"
"}"
"QPushButton:hover{"
"background-color: rgba(255, 0, 0, 30);"
"}"
"QPushButton:pressed{"
"background-color: rgba(255, 0, 0, 30);"
"}"
);
m_pIcon = new QLabel(this);
m_pIcon->setObjectName("IconContent");
m_pTitleContent = new QLabel(this);
m_pButtonMin = new QPushButton(this);
m_pButtonRestore = new QPushButton(this);
m_pButtonMax = new QPushButton(this);
m_pButtonClose = new QPushButton(this);
m_pButtonMin->setFixedSize(QSize(BUTTON_WIDTH, BUTTON_HEIGHT));
m_pButtonRestore->setFixedSize(QSize(BUTTON_WIDTH, BUTTON_HEIGHT));
m_pButtonMax->setFixedSize(QSize(BUTTON_WIDTH, BUTTON_HEIGHT));
m_pButtonClose->setFixedSize(QSize(BUTTON_WIDTH, BUTTON_HEIGHT));
m_pTitleContent->setObjectName("TitleContent");
m_pButtonMin->setObjectName("ButtonMin");
m_pButtonRestore->setObjectName("ButtonRestore");
m_pButtonMax->setObjectName("ButtonMax");
m_pButtonClose->setObjectName("ButtonClose");
m_pButtonClose->setIconSize(QSize(30, 30));
m_pButtonClose->setIcon(QIcon(":/resource/close.png"));
m_pButtonClose->setToolTip("<EFBFBD>ر<EFBFBD>");
m_pButtonClose->setStyleSheet(btnStyle);
QHBoxLayout* mylayout = new QHBoxLayout(this);
mylayout->addWidget(m_pIcon);
mylayout->addWidget(m_pTitleContent);
mylayout->addWidget(m_pButtonMin);
mylayout->addWidget(m_pButtonRestore);
mylayout->addWidget(m_pButtonMax);
mylayout->addWidget(m_pButtonClose);
mylayout->setContentsMargins(5, 0, 0, 0);
mylayout->setSpacing(3);
m_pTitleContent->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
this->setFixedHeight(TITLE_HEIGHT);
this->setWindowFlags(Qt::FramelessWindowHint);
}
//<2F>źŲ۵İ<DBB5><C4B0><EFBFBD>
void MyTitleBar::initConnections()
{
connect(m_pButtonMin, SIGNAL(clicked()), this, SLOT(onButtonMinClicked()));
connect(m_pButtonRestore, SIGNAL(clicked()), this, SLOT(onButtonRestoreClicked()));
connect(m_pButtonMax, SIGNAL(clicked()), this, SLOT(onButtonMaxClicked()));
connect(m_pButtonClose, SIGNAL(clicked()), this, SLOT(onButtonCloseClicked()));
}
//<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ,<2C><>paintEvent<6E>¼<EFBFBD><C2BC>н<EFBFBD><D0BD>л<EFBFBD><D0BB>Ʊ<EFBFBD><C6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
//<2F>ڹ<EFBFBD><DAB9><EFBFBD><ECBAAF><EFBFBD>и<EFBFBD><D0B8><EFBFBD>Ĭ<EFBFBD><C4AC>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ⲿ<EFBFBD><E2B2BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫֵ<C9AB>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
void MyTitleBar::setBackgroundColor(int r, int g, int b)
{
m_colorR = r;
m_colorG = g;
m_colorB = b;
//<2F><><EFBFBD>»<EFBFBD><C2BB><EFBFBD>(<28><><EFBFBD><EFBFBD>paintEvent<6E>¼<EFBFBD>)
update();
}
//<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>
void MyTitleBar::setTitleIcon(QString filePath)
{
QPixmap titleIcon(filePath);
m_pIcon->setPixmap(titleIcon.scaled(32, 32));
}
//<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void MyTitleBar::setTitleContent(QString titleContent)
{
m_pTitleContent->setText(titleContent);
m_titleContent = titleContent;
}
//<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void MyTitleBar::setTitleWidth(int width)
{
this->setFixedWidth(width);
}
//<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϰ<EFBFBD>ť<EFBFBD><C5A5><EFBFBD><EFBFBD>
//<2F><><EFBFBD>ڲ<EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵİ<CFB5>ť<EFBFBD><C5A5><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><D4BF><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еİ<D0B5>ť
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E1B9A9><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD>ť<EFBFBD><C5A5><EFBFBD>ֱ<EFBFBD>Ϊ<EFBFBD><CEAA>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD>󻯡<EFBFBD><F3BBAFA1>رհ<D8B1>ť<EFBFBD><C5A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ť<EFBFBD><C5A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void MyTitleBar::setButtonType(ButtonType buttonType)
{
m_buttonType = buttonType;
switch (buttonType)
{
case MIN_BUTTON:
{
m_pButtonRestore->setVisible(false);
m_pButtonMax->setVisible(false);
}
break;
case MIN_MAX_BUTTON:
{
m_pButtonRestore->setVisible(false);
}
break;
case ONLY_CLOSE_BUTTON:
{
m_pButtonMin->setVisible(false);
m_pButtonRestore->setVisible(false);
m_pButtonMax->setVisible(false);
}
break;
default:
break;
}
}
//<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еı<D0B5><C4B1><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD>Ч<EFBFBD><D0A7>
void MyTitleBar::setTitleRoll()
{
connect(&m_titleRollTimer, SIGNAL(timeout()), this, SLOT(onRollTitle()));
m_titleRollTimer.start(200);
}
//<2F><><EFBFBD><EFBFBD><E6B4B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>ڵ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD>С
void MyTitleBar::saveRestoreInfo(const QPoint point, const QSize size)
{
m_restorePos = point;
m_restoreSize = size;
}
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>ڵ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD>С
void MyTitleBar::getRestoreInfo(QPoint& point, QSize& size)
{
point = m_restorePos;
size = m_restoreSize;
}
//<2F><><EFBFBD>Ʊ<EFBFBD><C6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
void MyTitleBar::paintEvent(QPaintEvent *event)
{
//<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD>ɫ
QPainter painter(this);
QPainterPath pathBack;
pathBack.setFillRule(Qt::WindingFill);
pathBack.addRoundedRect(QRect(0, 0, this->width(), this->height()), 3, 3);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
painter.fillPath(pathBack, QBrush(QColor(m_colorR, m_colorG, m_colorB)));
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󻯻<EFBFBD><F3BBAFBB>߻<EFBFBD>ԭ<EFBFBD>󣬴<EFBFBD><F3A3ACB4>ڳ<EFBFBD><DAB3>ȱ<EFBFBD><C8B1>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3><EFBFBD>Ӧ<EFBFBD><D3A6>һ<EFBFBD><D2BB><EFBFBD>ı<EFBFBD>
if (this->width() != this->parentWidget()->width())
{
this->setFixedWidth(this->parentWidget()->width());
}
QWidget::paintEvent(event);
}
//˫<><CBAB><EFBFBD><EFBFBD>Ӧ<EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>ʵ<EFBFBD><CAB5>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󻯺<EFBFBD><F3BBAFBA><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void MyTitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
//ֻ<>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󻯡<EFBFBD><F3BBAFA1><EFBFBD>ԭ<EFBFBD><D4AD>ťʱ˫<CAB1><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
if (m_buttonType == MIN_MAX_BUTTON)
{
//ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD>󻯰<EFBFBD>ť<EFBFBD><C5A5>״̬<D7B4>жϵ<D0B6>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>Ǵ<EFBFBD><C7B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󻯻<EFBFBD><F3BBAFBB><EFBFBD>ԭʼ<D4AD><CABC>С״̬
//<2F><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>״̬
if (m_pButtonMax->isVisible())
{
onButtonMaxClicked();
}
else
{
onButtonRestoreClicked();
}
}
return QWidget::mouseDoubleClickEvent(event);
}
//<2F><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>mousePressEvent<6E><74>mouseMoveEvent<6E><74>mouseReleaseEvent<6E><74><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD>ڵ<EFBFBD>Ч<EFBFBD><D0A7>
void MyTitleBar::mousePressEvent(QMouseEvent *event)
{
if (m_buttonType == MIN_MAX_BUTTON)
{
//<2F>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ֹ<EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD>
if (m_pButtonMax->isVisible())
{
m_isPressed = true;
m_startMovePos = event->globalPos();
}
}
else
{
m_isPressed = true;
m_startMovePos = event->globalPos();
}
return QWidget::mousePressEvent(event);
}
void MyTitleBar::mouseMoveEvent(QMouseEvent *event)
{
if (m_isPressed)
{
QPoint movePoint = event->globalPos() - m_startMovePos;
QPoint widgetPos = this->parentWidget()->pos();
m_startMovePos = event->globalPos();
this->parentWidget()->move(widgetPos.x() + movePoint.x(), widgetPos.y() + movePoint.y());
}
return QWidget::mouseMoveEvent(event);
}
void MyTitleBar::mouseReleaseEvent(QMouseEvent *event)
{
m_isPressed = false;
return QWidget::mouseReleaseEvent(event);
}
//<2F><><EFBFBD>ر<EFBFBD><D8B1><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD>ļ<EFBFBD>
//<2F><><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD>ʽֱ<CABD><D6B1>д<EFBFBD><D0B4><EFBFBD>ļ<EFBFBD><C4BC>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱֱ<CAB1>Ӽ<EFBFBD><D3BC>ؽ<EFBFBD><D8BD><EFBFBD>
void MyTitleBar::loadStyleSheet(const QString &sheetName)
{
QFile file(":/Resource Files/" + sheetName + ".css");
file.open(QFile::ReadOnly);
if (file.isOpen())
{
QString styleSheet = this->styleSheet();
styleSheet += QLatin1String(file.readAll());
this->setStyleSheet(styleSheet);
}
}
//<2F><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ť<EFBFBD><C5A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>IJ<EFBFBD>
void MyTitleBar::onButtonMinClicked()
{
emit signalButtonMinClicked();
}
void MyTitleBar::onButtonRestoreClicked()
{
m_pButtonRestore->setVisible(false);
m_pButtonMax->setVisible(true);
emit signalButtonRestoreClicked();
}
void MyTitleBar::onButtonMaxClicked()
{
m_pButtonMax->setVisible(false);
m_pButtonRestore->setVisible(true);
emit signalButtonMaxClicked();
}
void MyTitleBar::onButtonCloseClicked()
{
emit signalButtonCloseClicked();
}
//<2F>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еı<D0B5><C4B1><EFBFBD><EFBFBD><EFBFBD>ʾΪ<CABE><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7>
void MyTitleBar::onRollTitle()
{
static int nPos = 0;
QString titleContent = m_titleContent;
//<2F><><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>λ<EFBFBD>ñ<EFBFBD><C3B1>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>ʼ
if (nPos > titleContent.length())
nPos = 0;
m_pTitleContent->setText(titleContent.mid(nPos));
nPos++;
}