add grubtheme and plymouththeme interaction

This commit is contained in:
likehomedream 2023-11-09 15:22:26 +08:00
parent c392841905
commit e214827e03
7 changed files with 141 additions and 31 deletions

View File

@ -38,45 +38,102 @@ void GrubGraphicsView::wheelEvent(QWheelEvent *event)
GrubImageWidget::GrubImageWidget(QWidget *parent)
{
QGraphicsScene* scene = new QGraphicsScene(this);
GrubGraphicsView* graphicsView = new GrubGraphicsView(scene);
m_scene = new QGraphicsScene(this);
graphicsView = new GrubGraphicsView(m_scene);
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(graphicsView);
setLayout(layout);
graphicsView->setScene(scene);
graphicsView->setScene(m_scene);
// 加载并显示背景图片
QPixmap pixmap(":/resource/background/1-warty-final-ubuntukylin.jpg");
QGraphicsPixmapItem* backgroundItem = new QGraphicsPixmapItem(pixmap);
scene->addItem(backgroundItem);
m_backgroundItem = new QGraphicsPixmapItem(pixmap);
m_backgroundItem->setData(Qt::UserRole, "background");
m_scene->addItem(m_backgroundItem);
// 创建毛玻璃特效并应用于背景图元
QGraphicsBlurEffect* blurEffect = new QGraphicsBlurEffect;
blurEffect->setBlurRadius(10);
backgroundItem->setGraphicsEffect(blurEffect);
m_backgroundItem->setGraphicsEffect(blurEffect);
// 设置图元居中显示
QRectF viewRect = graphicsView->viewport()->rect();
QPointF center(viewRect.center());
backgroundItem->setPos(center.x() - pixmap.width() / 2, center.y() - pixmap.height() / 2);
m_backgroundItem->setPos(center.x() - pixmap.width() / 2, center.y() - pixmap.height() / 2);
// 添加新的图元
QPixmap overlayPixmap(":/resource/background/grub-listview.png");
QGraphicsPixmapItem* overlayItem = new QGraphicsPixmapItem(overlayPixmap);
scene->addItem(overlayItem);
m_overlayPixmap.load(":/resource/background/grub-listview.png");
m_overlayItem = new QGraphicsPixmapItem(m_overlayPixmap);
m_scene->addItem(m_overlayItem);
// 放大图元
qreal scaleRatio = 2.0; // 放大倍数
overlayItem->setScale(scaleRatio);
m_overlayItem->setScale(scaleRatio);
// 计算新的图元位置
qreal scaledWidth = overlayPixmap.width() * scaleRatio; // 放大后的宽度
qreal scaledHeight = overlayPixmap.height() * scaleRatio; // 放大后的高度
overlayItem->setPos(center.x() - scaledWidth / 2, center.y() - scaledHeight / 2);
qreal scaledWidth = m_overlayPixmap.width() * scaleRatio; // 放大后的宽度
qreal scaledHeight = m_overlayPixmap.height() * scaleRatio; // 放大后的高度
m_overlayItem->setPos(center.x() - scaledWidth / 2, center.y() - scaledHeight / 2);
graphicsView->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
graphicsView->fitInView(m_scene->sceneRect(), Qt::KeepAspectRatio);
graphicsView->show();
// 设置初始缩放倍数为0.9倍
qreal initialScale = 0.4;
graphicsView->scale(initialScale, initialScale);
}
void GrubImageWidget::updateBackground(const QString &imagePath)
{
// 加载新的背景图片
QPixmap pixmap(imagePath);
if (pixmap.isNull()) {
// qDebug() << "Failed to load background image.";·
return;
}
// 移除旧的背景图元
m_scene->removeItem(m_backgroundItem);
delete m_backgroundItem;
// 创建并添加新的背景图元
m_backgroundItem = new QGraphicsPixmapItem(pixmap);
m_backgroundItem->setData(Qt::UserRole, "background");
m_scene->addItem(m_backgroundItem);
// 创建毛玻璃特效并应用于新的背景图元
QGraphicsBlurEffect* blurEffect = new QGraphicsBlurEffect;
blurEffect->setBlurRadius(10);
m_backgroundItem->setGraphicsEffect(blurEffect);
// 设置图元居中显示
QRectF viewRect = graphicsView->viewport()->rect();
QPointF center(viewRect.center());
m_backgroundItem->setPos(center.x() - pixmap.width() / 2, center.y() - pixmap.height() / 2);
// 创建并添加新的overlay图元
QGraphicsPixmapItem* newOverlayItem = new QGraphicsPixmapItem(m_overlayPixmap);
m_scene->addItem(newOverlayItem);
// 设置新的overlay图元位置
qreal scaleRatio = 2.0; // 放大倍数
qreal scaledWidth = m_overlayPixmap.width() * scaleRatio; // 放大后的宽度
qreal scaledHeight = m_overlayPixmap.height() * scaleRatio; // 放大后的高度
newOverlayItem->setPos(center.x() - scaledWidth / 2, center.y() - scaledHeight / 2);
// 移除旧的overlay图元
m_scene->removeItem(m_overlayItem);
delete m_overlayItem;
// 更新overlayItem指针
m_overlayItem = newOverlayItem;
graphicsView->fitInView(m_scene->sceneRect(), Qt::KeepAspectRatio);
graphicsView->show();
// 设置初始缩放倍数为0.9倍
qreal initialScale = 0.4;
graphicsView->resetTransform();
graphicsView->scale(initialScale, initialScale);
}

View File

@ -26,13 +26,17 @@ protected:
class GrubImageWidget : public QWidget {
public:
GrubImageWidget(QWidget *parent = nullptr);
void updateBackground(const QString& imagePath);
private:
GrubGraphicsView* graphicsView;
QGraphicsScene* m_scene;
QPixmap m_mergedImage;
QPixmap m_image;
QGraphicsPixmapItem* m_backgroundItem;
QPixmap m_overlayPixmap;
QGraphicsPixmapItem* m_overlayItem;
QGraphicsProxyWidget* m_proxy;
};

View File

@ -1,4 +1,6 @@
#include "grubthemewidget.h"
#include "../fileProcess/filecheck.h"
#include <QFileDialog>
GrubThemeWidget::GrubThemeWidget(QWidget *parent) : QWidget(parent)
{
@ -56,6 +58,23 @@ void GrubThemeWidget::initEditWidget()
QLabel *tipLabel1 = new QLabel();
tipLabel1->setText("<html>尺寸3840*2160<br>大小:不超过 10 MB<br>格式PNG</html>");
connect(showBtn, &QPushButton::clicked, this, [=]() {
QString newFilePath = QFileDialog::getOpenFileName(this, tr("Select picture file"), "", tr("Picture file (*.png *.jpg)"));
if (!newFilePath.isEmpty()/* && FileCheck::isLegalWallPaperFile(newFilePath,"wallpaper")*/) {
m_preview->updateBackground(newFilePath);
QPixmap pixmap(newFilePath);
showBtn->setIcon(QIcon(pixmap));
showBtn->setIconSize(showBtn->size());
} else {
qDebug() << "Selected file path:" << newFilePath;
}
});
widgetLayout->addWidget(showBtn);
widgetLayout->addWidget(tipLabel1);
grubWidget->setLayout(widgetLayout);

View File

@ -7,17 +7,17 @@ PlymouthThemeFeature::PlymouthThemeFeature(QWidget *parent) : QWidget(parent)
PlymouthImageWidget::PlymouthImageWidget(QWidget *parent)
{
QGraphicsScene* scene = new QGraphicsScene(this);
PlymouthGraphicsView* graphicsView = new PlymouthGraphicsView(scene);
scene = new QGraphicsScene(this);
graphicsView = new PlymouthGraphicsView(scene);
QVBoxLayout* layout = new QVBoxLayout(this);
layout = new QVBoxLayout(this);
layout->addWidget(graphicsView);
setLayout(layout);
graphicsView->setScene(scene);
// 加载并显示图片
QPixmap pixmap(":/resource/background/openKylin.svg");
QGraphicsPixmapItem* pixmapItem = new QGraphicsPixmapItem(pixmap);
pixmapItem = new QGraphicsPixmapItem(pixmap);
scene->addItem(pixmapItem);
graphicsView->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
@ -28,6 +28,26 @@ PlymouthImageWidget::PlymouthImageWidget(QWidget *parent)
graphicsView->scale(initialScale, initialScale);
}
void PlymouthImageWidget::updatePlymouth(const QString &imagePath)
{
// 移除之前的pixmapItem
scene->removeItem(pixmapItem);
delete pixmapItem;
// 加载新图片
QPixmap newPixmap(imagePath);
newPixmap = newPixmap.scaled(3000, 3000);
pixmapItem = new QGraphicsPixmapItem(newPixmap);
scene->addItem(pixmapItem);
// 调整视图
graphicsView->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
//FIX ME :pixmapItem没有放在视图正中间
// 设置初始缩放倍数为0.9倍
qreal initialScale = 0.4;
graphicsView->scale(initialScale, initialScale);
}
PlymouthGraphicsView::PlymouthGraphicsView(QGraphicsScene *scene)
{
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);

View File

@ -25,14 +25,12 @@ protected:
class PlymouthImageWidget : public QWidget {
public:
PlymouthImageWidget(QWidget *parent = nullptr);
void updatePlymouth(const QString& imagePath);
private:
PlymouthGraphicsView* graphicsView;
QString m_coverFilePath;
QGraphicsScene* m_scene;
QPixmap m_mergedImage;
QPixmap m_image;
QGraphicsProxyWidget* m_proxy;
QGraphicsScene* scene;
QGraphicsPixmapItem* pixmapItem;
QVBoxLayout* layout;
};
class PlymouthThemeFeature : public QWidget

View File

@ -1,5 +1,5 @@
#include "plymouththemewidget.h"
#include "iconwidgetfeature.h"
PlymouthThemeWidget::PlymouthThemeWidget(QWidget *parent) : QWidget(parent)
{
@ -62,19 +62,28 @@ void PlymouthThemeWidget::initEditWidget()
QLabel *defaultLabel = new QLabel();
defaultLabel->setFixedSize(64,64);
QPixmap pixmap(":/resource/background/openKylin.svg");
pixmap = pixmap.scaled(64,64, Qt::KeepAspectRatio);
pixmap = pixmap.scaled(64,64, Qt::KeepAspectRatio);
defaultLabel->setPixmap(pixmap);
CustomLabel *customLabel= new CustomLabel();
customLabel->setFixedSize(64,64);
m_customLabel= new CustomLabel();
m_customLabel->setFixedSize(64,64);
QPushButton *addButton = new QPushButton();
addButton->setIcon(QIcon::fromTheme("list-add-symbolic"));
addButton->setFixedSize(36,36);
connect(addButton, &QPushButton::clicked, this, [=]() {
QString newFilePath = QFileDialog::getOpenFileName(this, tr("Select SVG file"), "", tr("SVG file (*.svg *.png)"));
if (!newFilePath.isEmpty()) {
QPixmap pixmap(newFilePath);
pixmap = pixmap.scaled(64,64, Qt::KeepAspectRatio);
m_customLabel->setPixmap(pixmap);
m_preview->updatePlymouth(newFilePath);
}
});
QLabel *tipLabel = new QLabel();
tipLabel->setText("<html>大小不超过10MB<br>格式MP4</html>");
showLayout->addWidget(defaultLabel);
showLayout->addWidget(customLabel);
showLayout->addWidget(m_customLabel);
showLayout->addWidget(addButton);
showLayout->addStretch(1);
showLayout->addWidget(tipLabel);

View File

@ -1,5 +1,7 @@
#ifndef PLYMOUTHMOUTHTHEMEWIDGET_H
#define PLYMOUTHMOUTHTHEMEWIDGET_H
#include "plymouththemefeature.h"
#include "iconwidgetfeature.h"
#include <QObject>
#include <QWidget>
@ -8,7 +10,7 @@
#include <QScrollArea>
#include <QLabel>
#include <QCheckBox>
#include "plymouththemefeature.h"
class PlymouthThemeWidget : public QWidget
{
@ -26,6 +28,7 @@ private:
QWidget *m_rightwidget;
QWidget *m_plymouthwidget;
QVBoxLayout *m_previewlayout;
CustomLabel *m_customLabel;
};
#endif // PLYMOUTHMOUTHTHEMEWIDGET_H