add global file operate

This commit is contained in:
likehomedream 2023-06-29 15:52:19 +08:00
parent db08176957
commit 3768f6eac0
2 changed files with 95 additions and 31 deletions

View File

@ -4,6 +4,8 @@
#include <QPalette>
#include <QButtonGroup>
#include <QIcon>
#include <QLabel>
GlobalThemeWidget::GlobalThemeWidget(QWidget *parent) : WidgetBase(parent)
{
this->initThemeLabel("创建全局主题");
@ -52,11 +54,13 @@ void GlobalThemeWidget::initGlobalWidget()
for (int i = 0; i < m_colorlist.size(); ++i) {
QPushButton *button = new QPushButton;
button->setFixedSize(QSize(24, 24));
button->setStyleSheet(QString("background-color:%1").arg(m_colorlist.at(i)));
button->setProperty("isRoundButton", true);
QPalette pal = qApp->palette();
pal.setColor(QPalette::Button,QColor(m_colorlist.at(i)));
button->setPalette(pal);
if (m_accentcolorlayout) {
m_accentcolorlayout->addWidget(button);
}
m_accentcolorlayout->addWidget(button);
box->addButton(button, i);
}
@ -98,7 +102,7 @@ void GlobalThemeWidget::initGlobalWidget()
connect(m_coverbutton,&QPushButton::clicked,[=](){
importCover();
});
});
m_coverlayout->addWidget(coverlabel);
m_coverlayout->addWidget(m_coverbutton);
@ -144,6 +148,36 @@ void GlobalThemeWidget::initGlobalWidget()
}
void GlobalThemeWidget::onImageLabelDoubleClicked()
{
delete m_coverimagelabel;
m_coverbutton->show();
}
void GlobalThemeWidget::onWallpaperImageLabelDoubleClicked()
{
delete m_wallpaperimagelabel;
m_wallpaperbutton->show();
}
bool GlobalThemeWidget::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseButtonDblClick) {
if (obj == m_coverimagelabel) {
onImageLabelDoubleClicked();
QString clicked_path = m_globalpath+"cover.png";
QFile::remove(clicked_path);
return true;
}else if(obj == m_wallpaperimagelabel){
onWallpaperImageLabelDoubleClicked();
QString clicked_path = m_globalpath+"wallpaper.png";
QFile::remove(clicked_path);
return true;
}
}
return QWidget::eventFilter(obj, event);
}
void GlobalThemeWidget::importCover()
{
QString filters = tr("Cover files(*.png *.jpg)");
@ -160,20 +194,23 @@ void GlobalThemeWidget::importCover()
connect(m_fd, &QFileDialog::fileSelected, this, [this](const QString &file){
QFileInfo fileInfo(file);
QPixmap pixmap(fileInfo.absoluteFilePath());
QRect buttonGeometry = m_coverbutton->geometry();
m_coverimagelabel = new QLabel(m_coverwidget);
m_coverbutton->setIcon(pixmap);
m_coverbutton->setIconSize(m_coverbutton->size());
m_coverimagelabel->setGeometry(buttonGeometry);
QLayout* layout = m_coverbutton->layout();
if (layout != nullptr) {
QLayoutItem* item;
while ((item = layout->takeAt(0)) != nullptr) {
delete item->widget();
delete item;
}
delete layout;
}
QImage Image;
Image.load(fileInfo.absoluteFilePath());
QPixmap pixmap = QPixmap::fromImage(Image);
int with = m_coverimagelabel->width();
int height = m_coverimagelabel->height();
QPixmap fitpixmap = pixmap.scaled(with, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
m_coverimagelabel->setPixmap(fitpixmap);
m_coverimagelabel->show();
iscover = true;
m_coverbutton->hide();
m_coverimagelabel->installEventFilter(this);
});
openFileDialog();
@ -195,20 +232,24 @@ void GlobalThemeWidget::importWallpaper()
connect(m_fd, &QFileDialog::fileSelected, this, [this](const QString &file){
QFileInfo fileInfo(file);
QPixmap pixmap(fileInfo.absoluteFilePath());
QRect buttonGeometry = m_coverbutton->geometry();
m_wallpaperimagelabel = new QLabel(m_wallpaperwidget);
m_wallpaperbutton->setIcon(pixmap);
m_wallpaperbutton->setIconSize(m_wallpaperbutton->size());
m_wallpaperimagelabel->setGeometry(buttonGeometry);
QImage Image;
Image.load(fileInfo.absoluteFilePath());
QPixmap pixmap = QPixmap::fromImage(Image);
int with = m_wallpaperimagelabel->width();
int height = m_wallpaperimagelabel->height();
QPixmap fitpixmap = pixmap.scaled(with, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
m_wallpaperimagelabel->setPixmap(fitpixmap);
m_wallpaperimagelabel->show();
iscover = false;
m_wallpaperbutton->hide();
m_wallpaperimagelabel->installEventFilter(this);
QLayout* layout = m_wallpaperbutton->layout();
if (layout != nullptr) {
QLayoutItem* item;
while ((item = layout->takeAt(0)) != nullptr) {
delete item->widget();
delete item;
}
delete layout;
}
});
openFileDialog();
@ -220,6 +261,8 @@ void GlobalThemeWidget::openFileDialog()
return;
QString path = m_globalpath;
//-----需要补充判断文件大小和文件格式-----
QDir dir(path);
if (!dir.exists()) {
dir.mkpath(path);
@ -234,6 +277,16 @@ void GlobalThemeWidget::openFileDialog()
QString destinationFilePath = destinationFolderPath + "/" + fileInfo.fileName();
QFile::copy(sourceFilePath, destinationFilePath);
if(iscover){
QFile::copy(sourceFilePath, destinationFilePath);
QFile coverfile(m_globalpath+fileInfo.fileName());
coverfile.rename(m_globalpath+"cover.png");
}else{
QFile::copy(sourceFilePath, destinationFilePath);
QFile coverfile(m_globalpath+fileInfo.fileName());
coverfile.rename(m_globalpath+"wallpaper.png");
}
}

View File

@ -6,6 +6,8 @@
#include <QWidget>
#include <QMap>
#include <QFileDialog>
#include <QMouseEvent>
#include <QImage>
class GlobalThemeWidget : public WidgetBase
{
@ -17,6 +19,11 @@ public:
void importCover();
void importWallpaper();
void openFileDialog();
protected:
bool eventFilter(QObject* obj, QEvent* event) override;
signals:
private:
QString m_globalpath = "";
@ -33,17 +40,21 @@ private:
QWidget *m_coverwidget;
QHBoxLayout *m_coverlayout;
QPushButton *m_coverbutton;
QLabel *m_coverimagelabel;
QWidget *m_wallpaperwidget;
QHBoxLayout *m_wallpaperlayout;
QPushButton *m_wallpaperbutton;
QLabel *m_wallpaperimagelabel;
bool iscover = true;
QFileDialog *m_fd;
QString m_iconpath;
QImage *m_pImage;
private slots:
void onImageLabelDoubleClicked();
void onWallpaperImageLabelDoubleClicked();
};
#endif // GLOBALTHEMEWIDGET_H