add plymouth delete function
This commit is contained in:
parent
ccfda61353
commit
8130c9a941
|
@ -1,4 +1,6 @@
|
|||
#include "plymouththemefeature.h"
|
||||
#include <QDebug>
|
||||
#include <QIcon>
|
||||
|
||||
PlymouthThemeFeature::PlymouthThemeFeature(QWidget *parent) : QWidget(parent)
|
||||
{
|
||||
|
@ -38,6 +40,7 @@ PlymouthImageWidget::PlymouthImageWidget(QWidget *parent)
|
|||
graphicsView->show();
|
||||
qreal initialScale = 0.3;
|
||||
graphicsView->scale(initialScale, initialScale);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -84,8 +87,8 @@ void PlymouthImageWidget::updatePlymouth(const QString &imagePath)
|
|||
qreal y = (sceneHeight - pixmaps.first().height()) / 2;
|
||||
pixmapItem->setPos(x, y);
|
||||
}
|
||||
qreal initialScale = 0.3;
|
||||
graphicsView->scale(initialScale, initialScale);
|
||||
// qreal initialScale = 0.3;
|
||||
// graphicsView->scale(initialScale, initialScale);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -144,3 +147,156 @@ void PlymouthGraphicsView::drawBackground(QPainter *painter, const QRectF &rect)
|
|||
// 绘制全黑背景
|
||||
painter->fillRect(rect, Qt::black);
|
||||
}
|
||||
/**
|
||||
* @brief 构造函数
|
||||
*
|
||||
* 创建一个 PlymouthCustomLabel 对象,并设置父对象。
|
||||
*
|
||||
* @param parent 父对象指针
|
||||
*/
|
||||
PlymouthCustomLabel::PlymouthCustomLabel(QWidget *parent) : QLabel(parent), pixmap(), closeIconRect(0, 0, 0, 0)
|
||||
{
|
||||
this->setMouseTracking(true);
|
||||
this->setFixedSize(64,64);
|
||||
initializeCloseIconRect();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 初始化关闭图标矩形
|
||||
*
|
||||
* 初始化关闭图标的矩形大小,根据窗口宽度进行设置。
|
||||
*/
|
||||
void PlymouthCustomLabel::initializeCloseIconRect()
|
||||
{
|
||||
closeIconRect = QRect(width() - 15, 5, 10, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 自定义标签的绘制事件
|
||||
*
|
||||
* 在绘制事件发生时,执行标签的绘制操作。
|
||||
*
|
||||
* @param event 绘制事件指针
|
||||
*/
|
||||
void PlymouthCustomLabel::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
QLabel::paintEvent(event);
|
||||
QPainter painter(this);
|
||||
QPen pen(Qt::blue);
|
||||
pen.setStyle(Qt::DashLine);
|
||||
painter.setPen(pen);
|
||||
|
||||
QBrush brush(QColor("#F5F5F5"));
|
||||
painter.setBrush(brush);
|
||||
|
||||
QRect roundedRect = rect().adjusted(1, 1, -1, -1);
|
||||
QPainterPath path;
|
||||
int radius = 6;
|
||||
path.addRoundedRect(roundedRect, radius, radius);
|
||||
painter.drawPath(path);
|
||||
|
||||
if (!pixmap.isNull()) {
|
||||
painter.drawPixmap(rect(), pixmap);
|
||||
}
|
||||
drawCloseIcon(painter); // 调用绘制关闭图标的函数
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 绘制关闭图标
|
||||
*
|
||||
* 使用指定的画笔在给定的位置绘制关闭图标。
|
||||
*
|
||||
* @param painter 画笔对象
|
||||
*/
|
||||
void PlymouthCustomLabel::drawCloseIcon(QPainter &painter)
|
||||
{
|
||||
if (!closeIconPixmap.isNull()) {
|
||||
painter.drawPixmap(closeIconRect, closeIconPixmap);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 设置 QPixmap
|
||||
*
|
||||
* 设置 CustomLabel 的 QPixmap。
|
||||
*
|
||||
* @param pixmap QPixmap 对象
|
||||
*/
|
||||
void PlymouthCustomLabel::setPixmap(const QPixmap &pixmap)
|
||||
{
|
||||
this->pixmap = pixmap;
|
||||
update();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 进入事件处理函数
|
||||
*
|
||||
* 当鼠标进入 CustomLabel 控件时,该函数将被调用。
|
||||
*
|
||||
* @param event 进入事件指针
|
||||
*/
|
||||
void PlymouthCustomLabel::enterEvent(QEvent *event)
|
||||
{
|
||||
QWidget::enterEvent(event);
|
||||
if(!this->pixmap.isNull()){
|
||||
showCloseIcon(); // 在这个函数中显示“x”图标
|
||||
}else{
|
||||
qDebug()<<"pixmap is null";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 处理鼠标离开事件
|
||||
*
|
||||
* 当鼠标离开控件时,该函数将被调用。在此函数中,调用 QWidget::leaveEvent() 处理鼠标离开事件,并隐藏“x”图标。
|
||||
*
|
||||
* @param event 鼠标事件指针
|
||||
*/
|
||||
void PlymouthCustomLabel::leaveEvent(QEvent *event)
|
||||
{
|
||||
QWidget::leaveEvent(event);
|
||||
hideCloseIcon(); // 在这个函数中隐藏“x”图标
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 鼠标按下事件处理函数
|
||||
*
|
||||
* 当鼠标按下时,该函数会处理事件。如果点击的位置在关闭图标矩形内,则将图标显示为空,并隐藏关闭图标。
|
||||
* 否则,处理其他点击事件。
|
||||
*
|
||||
* @param event 鼠标事件指针
|
||||
*/
|
||||
void PlymouthCustomLabel::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
QWidget::mousePressEvent(event);
|
||||
if (closeIconRect.contains(event->pos())) {
|
||||
setPixmap(QPixmap()); // 点击“x”时将 Icon 显示为空
|
||||
hideCloseIcon(); // 在这个函数中隐藏“x”图标
|
||||
emit deleteCustomIcon();
|
||||
} else {
|
||||
// 处理其他点击事件
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 显示关闭图标
|
||||
*
|
||||
* 将关闭图标的Pixmap从主题中加载,并更新控件的显示。
|
||||
*/
|
||||
void PlymouthCustomLabel::showCloseIcon()
|
||||
{
|
||||
closeIconPixmap = QIcon::fromTheme("window-close-symbolic").pixmap(10, 10);
|
||||
this->update();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 隐藏关闭图标
|
||||
*
|
||||
* 将关闭图标的pixmap置空,并重新绘制以清除关闭图标。
|
||||
*/
|
||||
void PlymouthCustomLabel::hideCloseIcon()
|
||||
{
|
||||
closeIconPixmap = QPixmap(); // 将关闭图标的pixmap置空
|
||||
update(); // 重新绘制以清除关闭图标
|
||||
}
|
||||
|
|
|
@ -26,8 +26,32 @@ protected:
|
|||
|
||||
|
||||
};
|
||||
class PlymouthCustomLabel : public QLabel {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit PlymouthCustomLabel(QWidget* parent = nullptr);
|
||||
|
||||
void setPixmap(const QPixmap& pixmap);
|
||||
void showCloseIcon();
|
||||
void hideCloseIcon();
|
||||
void initializeCloseIconRect();
|
||||
void drawCloseIcon(QPainter &painter);
|
||||
protected:
|
||||
void paintEvent(QPaintEvent* event) override;
|
||||
|
||||
void enterEvent(QEvent *event);
|
||||
void leaveEvent(QEvent *event);
|
||||
void mousePressEvent(QMouseEvent *event);
|
||||
signals:
|
||||
void deleteCustomIcon();
|
||||
private:
|
||||
QRect closeIconRect;
|
||||
QPixmap pixmap;
|
||||
QPixmap closeIconPixmap;
|
||||
};
|
||||
|
||||
class PlymouthImageWidget : public QWidget {
|
||||
Q_OBJECT
|
||||
public:
|
||||
PlymouthImageWidget(QWidget *parent = nullptr);
|
||||
void updatePlymouth(const QString& imagePath);
|
||||
|
|
|
@ -91,8 +91,7 @@ void PlymouthThemeWidget::initEditWidget()
|
|||
QPixmap pixmap(":/resource/background/openKylin.svg");
|
||||
pixmap = pixmap.scaled(64,64, Qt::KeepAspectRatio);
|
||||
defaultLabel->setPixmap(pixmap);
|
||||
m_customLabel= new CustomLabel();
|
||||
m_customLabel->setFixedSize(64,64);
|
||||
m_customLabel= new PlymouthCustomLabel();
|
||||
QPushButton *addButton = new QPushButton();
|
||||
addButton->setIcon(QIcon::fromTheme("list-add-symbolic"));
|
||||
addButton->setFixedSize(36,36);
|
||||
|
@ -107,6 +106,18 @@ void PlymouthThemeWidget::initEditWidget()
|
|||
g_themeChange = true;
|
||||
}
|
||||
});
|
||||
connect(m_customLabel,&PlymouthCustomLabel::deleteCustomIcon,this,[=](){
|
||||
m_preview->updatePlymouth(":/resource/plymouth/");
|
||||
m_preview->showPlymouth();
|
||||
|
||||
QDir cachedir(m_imagedirpath);
|
||||
QStringList fileList = cachedir.entryList(QDir::Files);
|
||||
foreach (const QString &fileName, fileList) {
|
||||
QString filePath = cachedir.absoluteFilePath(fileName);
|
||||
QFile::remove(filePath);
|
||||
}
|
||||
|
||||
});
|
||||
QLabel *tipLabel = new QLabel();
|
||||
tipLabel->setText("<html>大小不超过10MB<br>格式:MP4</html>");
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@ private:
|
|||
QWidget *m_rightwidget;
|
||||
QWidget *m_plymouthwidget;
|
||||
QVBoxLayout *m_previewlayout;
|
||||
CustomLabel *m_customLabel;
|
||||
PlymouthCustomLabel *m_customLabel;
|
||||
QString m_imagedirpath;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue