add plymouth delete function

This commit is contained in:
likehomedream 2023-11-28 17:18:25 +08:00
parent ccfda61353
commit 8130c9a941
4 changed files with 196 additions and 5 deletions

View File

@ -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(); // 重新绘制以清除关闭图标
}

View File

@ -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);

View File

@ -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>");

View File

@ -35,7 +35,7 @@ private:
QWidget *m_rightwidget;
QWidget *m_plymouthwidget;
QVBoxLayout *m_previewlayout;
CustomLabel *m_customLabel;
PlymouthCustomLabel *m_customLabel;
QString m_imagedirpath;
};