!3 add check and listview

Merge pull request !3 from likehomedream/my-devel
This commit is contained in:
likehomedream 2023-06-21 09:51:14 +00:00 committed by Gitee
commit e33d0c2443
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
9 changed files with 338 additions and 52 deletions

View File

@ -9,7 +9,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
cursorthemewidget.cpp \
debmaker/debmaker.cpp \
filecopy.cpp \
fileoperate.cpp \
globalthemewidget.cpp \
iconthemewidget.cpp \
main.cpp \
@ -21,7 +21,7 @@ SOURCES += \
HEADERS += \
cursorthemewidget.h \
debmaker/debmaker.h \
filecopy.h \
fileoperate.h \
globalthemewidget.h \
iconthemewidget.h \
mainwindow.h \

View File

@ -1,14 +1,15 @@
#include "filecopy.h"
#include "fileoperate.h"
#include <QDir>
#include <QDebug>
FileCopy::FileCopy(QObject *parent) : QObject(parent)
FileOperate::FileOperate(QObject *parent) : QObject(parent)
{
}
bool FileCopy::copyDirectoryFiles(const QString &fromDir, const QString &toDir, bool coverFileIfExist)
bool FileOperate::copyDirectoryFiles(const QString &fromDir, const QString &toDir, bool coverFileIfExist)
{
QDir sourceDir(fromDir);
@ -46,3 +47,28 @@ bool FileCopy::copyDirectoryFiles(const QString &fromDir, const QString &toDir,
}
return true;
}
bool FileOperate::deleteDirectoryFiles(const QString &path)
{
qDebug()<<"deleteDirectoryFiles====start";
if (path.isEmpty()){
qDebug()<<"path.isEmpty()====start";
return false;
}
QDir dir(path);
qDebug()<<"path====start"<<path;
if(!dir.exists()){
return true;
}
dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot); //设置过滤
QFileInfoList fileList = dir.entryInfoList(); // 获取所有的文件信息
foreach (QFileInfo file, fileList){ //遍历文件信息
if (file.isFile()){ // 是文件,删除
file.dir().remove(file.fileName());
}else{ // 递归删除
deleteDirectoryFiles(file.absoluteFilePath());
}
}
return dir.rmdir(dir.absolutePath()); // 删除文件夹
}

View File

@ -3,12 +3,13 @@
#include <QObject>
class FileCopy : public QObject
class FileOperate : public QObject
{
Q_OBJECT
public:
explicit FileCopy(QObject *parent = nullptr);
explicit FileOperate(QObject *parent = nullptr);
static bool copyDirectoryFiles(const QString &fromDir, const QString &toDir, bool coverFileIfExist);
static bool deleteDirectoryFiles(const QString &Dir);
signals:
};

View File

@ -7,8 +7,10 @@
#include <QFile>
#include <QDir>
#include <QMessageBox>
#include <QStringListModel>
#include <QMessageBox>
#define HOMEPATH "/home/"
IconThemeWidget::IconThemeWidget(QWidget *parent) : WidgetBase(parent)
@ -21,49 +23,98 @@ IconThemeWidget::IconThemeWidget(QWidget *parent) : WidgetBase(parent)
connect(m_importButton , &QPushButton::clicked, this, [=](){
this->importIcons();
});
connect(this,&IconThemeWidget::onConfirmButtonClicked,this, &IconThemeWidget::check);
// connect(this,&IconThemeWidget::onConfirmButtonClicked,this, &IconThemeWidget::check);
}
void IconThemeWidget::init()
{
QString name = qgetenv("USER");
qDebug()<<"QString name = qgetenv;"<<name;
m_iconpath = HOMEPATH +name + "/.cache/theme-build/icon/";
// m_allLayout->insertWidget(new QLabel(tr("WidgetA")));
// 创建新的 widget
QWidget* newWidget = new QWidget(this);
newWidget->setFixedHeight(291);
QLineEdit* lll= new QLineEdit(tr("WidgetA"),newWidget);
newWidget->setFixedWidth(480);
// QLineEdit* lll= new QLineEdit(tr("WidgetA"),newWidget);
m_allLayout->takeAt(1);
// m_allLayout->insertWidget(1,spacer);
// 创建一个QListView
m_listwidget = new QListWidget(newWidget);
m_listwidget->setViewMode(QListView::IconMode);
m_listwidget->setFixedSize(newWidget->size());
m_listwidget->setFlow(QListView::LeftToRight);
m_listwidget->setMovement(QListView::Static);
m_listwidget->setIconSize(QSize(48, 48));
// 初始化固定路径
QString fixedPath = m_iconpath;
// 获取文件列表,并将文件显示在列表视图中
QStringList fileNames = QDir(fixedPath).entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
foreach (QString fileName, fileNames) {
QListWidgetItem *item = new QListWidgetItem(QIcon(QIcon::fromTheme("folder")), fileName);
// DeletableItem *item = new DeletableItem(QIcon(QIcon::fromTheme("folder")), fileName);
m_listwidget->addItem(item);
}
connect(m_listwidget, &QListWidget::itemDoubleClicked, [&](QListWidgetItem *item) {
int row = m_listwidget->row(item);
QString clicked_path = m_iconpath+m_listwidget->currentItem()->data(Qt::DisplayRole).toString();
//删除.cache中的文件目录
FileOperate::deleteDirectoryFiles(clicked_path);
m_listwidget->takeItem(row);
delete item;
});
// 将新的 widget 插入到 m_allLayout 中
int index = m_allLayout->indexOf(m_buttonWidget);
qDebug()<< index;
m_allLayout->insertWidget(index, newWidget);
// m_allLayout->addWidget(m_headWidget);
// m_allLayout->addWidget(m_buttonWidget);
}
#include <QMessageBox>
void IconThemeWidget::importIcons()
{
QString name = qgetenv("USER");
qDebug()<<"QString name = qgetenv;"<<name;
m_iconpath = HOMEPATH +name + "/.cache/theme-build/icon/";
// QString filters = tr("Icon files(*.png)");
m_fd= new QFileDialog(this);
m_fd->setFileMode(QFileDialog::Directory);
m_fd->setDirectory(HOMEPATH);
m_fd->setDirectory(HOMEPATH+qgetenv("USER"));
m_fd->setAcceptMode(QFileDialog::AcceptOpen);
m_fd->setViewMode(QFileDialog::List);
// m_fd->setNameFilter(filters);
// m_fd->setFileMode(QFileDialog::ExistingFile);
m_fd->setWindowTitle(tr("Select Open Action"));
m_fd->setWindowTitle(tr("Select Import Dir"));
m_fd->setLabelText(QFileDialog::Accept, tr("Select"));
m_fd->setLabelText(QFileDialog::Reject, tr("Cancel"));
connect(m_fd, &QFileDialog::urlSelected, this, [=](const QUrl &url){
qDebug() << "demo urlSelected:" << url;
QStringList str_list = url.toString().split("/");
// 创建新的 QListWidgetItem并设置标志为 Qt::UserRole用于存储目录路径
QListWidgetItem *item = new QListWidgetItem(QIcon(QIcon::fromTheme("folder")), str_list.last());
item->setData(Qt::UserRole, str_list.last());
// 添加项到 QListWidget
m_listwidget->addItem(item);
});
openFileDialog();
}
void IconThemeWidget::openFileDialog()
{
if (m_fd->exec() != QDialog::Accepted)
return;
@ -71,39 +122,24 @@ void IconThemeWidget::importIcons()
QString selectedDirectory;
selectedDirectory = m_fd->selectedFiles().first();
QStringList str_list = selectedDirectory.split("/");
// bool isLegal = ThemesCheck::checkIconsDir(selectedDirectory);
// if(isLegal){
QStringList str_list = selectedDirectory.split("/");
QString path = m_iconpath+str_list.last();
QString path = m_iconpath+str_list.last();
QDir dir(path);
if (!dir.exists()) {
dir.mkpath(path);
}
QDir dir(path);
if (!dir.exists()) {
dir.mkpath(path);
}
FileOperate::copyDirectoryFiles(selectedDirectory,path,true);
FileCopy::copyDirectoryFiles(selectedDirectory,path,true);
//选中的图标文件加显示在界面中
qDebug()<<"path----------------"<<path;
//选中的图标文件加显示在界面中
// }
}
void IconThemeWidget::check()
{
//检查
bool islegal = ThemesCheck::checkIconsDir(m_iconpath);
if(!islegal){
//给出提示
QMessageBox *messageBox = new QMessageBox(this);
messageBox->show();
}else{
//输入主题包名,返回
QMessageBox *messageBox = new QMessageBox(this);
messageBox->show();
//
Q_EMIT readyToBuild();
}
}

View File

@ -3,11 +3,68 @@
#include "widgetbase.h"
#include "filecopy.h"
#include "fileoperate.h"
#include <QObject>
#include <QWidget>
#include <QPushButton>
#include <QListView>
#include <QFileSystemModel>
#include <QStringList>
#include <QStandardItemModel>
#include <QListView>
#include <QListWidget>
class DeletableItem : public QListWidgetItem
{
public:
explicit DeletableItem(const QIcon &icon, const QString &text, QListWidget *parent = nullptr) : QListWidgetItem(text, parent)
{
// // 创建删除按钮
// QPushButton* deleteButton = new QPushButton("删除");
// deleteButton->setObjectName("deleteButton"); // 设置对象名称以便样式定制
// // 使用水平布局将按钮添加到项中
// QHBoxLayout* layout = new QHBoxLayout;
// layout->addWidget(deleteButton);
// layout->setAlignment(Qt::AlignRight);
// // 创建一个小部件来容纳布局
// QWidget* widget = new QWidget(parent);
// widget->setLayout(layout);
// widget->setProperty("isRoundButton",true);
// // 将小部件设置为项的右上角小部件
// setSizeHint(widget->sizeHint());
// parent->setItemWidget(this, widget);
// deleteButton->setFixedSize(20,20);
// deleteButton->setIcon(QIcon::fromTheme("window-close-symbolic"));
// 连接删除按钮的点击信号到槽函数
// QObject::connect(deleteButton, &QPushButton::clicked, this, &DeletableItem::deleteItem);
QWidget* widget = new QWidget(parent);
QHBoxLayout* layout = new QHBoxLayout(widget);
QPushButton* deleteButton = new QPushButton(widget);
deleteButton->setIcon(QIcon::fromTheme("window-close-symbolic"));
deleteButton->setFixedSize(80, 80);
// deleteButton->setStyleSheet("QPushButton { border: none; padding: 0; }");
deleteButton->setCursor(Qt::PointingHandCursor);
layout->addStretch();
layout->addWidget(deleteButton, 0, Qt::AlignTop | Qt::AlignRight);
widget->setLayout(layout);
setSizeHint(widget->sizeHint());
parent->setItemWidget(this, widget);
}
//private slots:
// void deleteItem()
// {
// QListWidget* listWidget = qobject_cast<QListWidget*>(listWidget());
// listWidget->takeItem(listWidget->row(this));
// }
};
class IconThemeWidget : public WidgetBase
{
@ -17,15 +74,18 @@ public:
void init();
void importIcons();
// bool check(QString path);
void openFileDialog();
Q_SIGNALS:
void readyToBuild();
public Q_SLOTS:
void check();
private:
QFileDialog *m_fd;
QString m_iconpath;
QListView *m_listview;
QStandardItemModel *m_model;
QListWidget *m_listwidget;
};
#endif // ICONTHEMEWIDGET_H

View File

@ -1,5 +1,6 @@
#include "themescheck.h"
#include <QDebug>
#include <QImageReader>
ThemesCheck::ThemesCheck(QObject *parent) : QObject(parent)
{
@ -14,6 +15,17 @@ bool ThemesCheck::checkIconsDir(QString folderPath)
QDir dir(folderPath);
//判断文件夹的名字
QStringList validFolderNames = {"16", "24", "32", "48", "64", "96"};
QStringList str_list = folderPath.split("/");
if (!validFolderNames.contains(str_list.last())) {
qDebug() << "Invalid folder name: " << str_list.last();
return false;
}
// 获取当前目录下所有文件及文件夹
QFileInfoList fileList = dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
@ -24,6 +36,7 @@ bool ThemesCheck::checkIconsDir(QString folderPath)
// 判断是否为文件夹
if (fileInfo.isDir()) {
// 递归判断文件夹内所有文件
if (!checkIconsDir(path)) {
return false;
@ -31,13 +44,38 @@ bool ThemesCheck::checkIconsDir(QString folderPath)
}
// 判断文件名是否含有空格
else if (name.contains(" ")) {
qDebug() << "File name contains space: " << name;
return false;
}
// 判断文件格式是否为png或svg
else if (fileInfo.suffix() != "png" && !(fileInfo.suffix() == "svg" && fileInfo.dir().dirName() == "scalable")) {
else if (fileInfo.suffix() != "png" /*&& !(fileInfo.suffix() == "svg" && fileInfo.dir().dirName() == "scalable")*/) {
qDebug() << "Invalid file format: " << name;
return false;
}
// 解析文件路径
QStringList pathParts = path.split("/");
if (pathParts.size() < 3) {
qDebug() << "Invalid file path: " << path;
return false;
}
QString folderName = pathParts[pathParts.size() - 2];
int expectedSize = folderName.toInt();
// 判断图标文件宽度和高度是否与最开始的数字相匹配
QImageReader r(path);
if (r.size().width() != expectedSize || r.size().height() != expectedSize) {
qDebug() << "Icon size does not match folder name: " << name;
return false;
}
}
qDebug()<<"yes!!!!!!!!!!";
return true;
}
bool ThemesCheck::checkWallpaperDir(QString folderPath)
{
//检查图片大小。
// folderPath
}

View File

@ -1,7 +1,121 @@
#include "wallpaperthemewidget.h"
#include <QDebug>
WallpaperthemeWidget::WallpaperthemeWidget(QWidget *parent) : WidgetBase(parent)
{
this->initThemeLabel("创建桌面壁纸包");
this->initImportButton("导入图片");
init();
connect(m_importButton , &QPushButton::clicked, this, [=](){
this->importWallpaper();
});
}
void WallpaperthemeWidget::init()
{
QString name = qgetenv("USER");
qDebug()<<"QString name = qgetenv;"<<name;
m_wallpaperpath = HOMEPATH +name + "/.cache/theme-build/wallpapers/";
QWidget* newWidget = new QWidget(this);
newWidget->setFixedHeight(291);
newWidget->setFixedWidth(480);
m_allLayout->takeAt(1);
// 创建一个QListView
m_listwidget = new QListWidget(newWidget);
m_listwidget->setViewMode(QListView::IconMode);
m_listwidget->setFixedSize(newWidget->size());
m_listwidget->setFlow(QListView::LeftToRight);
m_listwidget->setMovement(QListView::Static);
m_listwidget->setIconSize(QSize(100, 56));
// 初始化固定路径
QString fixedPath = m_wallpaperpath;
// 获取文件列表,并将文件显示在列表视图中
QStringList fileNames = QDir(fixedPath).entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
foreach (QString fileName, fileNames) {
QString imgpath = m_wallpaperpath+fileName;
QListWidgetItem *item = new QListWidgetItem(fileName);
item->setIcon(QIcon(imgpath));
m_listwidget->addItem(item);
}
connect(m_listwidget, &QListWidget::itemDoubleClicked, [&](QListWidgetItem *item) {
int row = m_listwidget->row(item);
QString clicked_path = m_wallpaperpath+m_listwidget->currentItem()->data(Qt::DisplayRole).toString();
QFile::remove(clicked_path);
m_listwidget->takeItem(row);
delete item;
});
// 将新的 widget 插入到 m_allLayout 中
int index = m_allLayout->indexOf(m_buttonWidget);
qDebug()<< index;
m_allLayout->insertWidget(index, newWidget);
}
void WallpaperthemeWidget::importWallpaper()
{
QString filters = tr("Wallpaper files(*.png *.jpg)");
m_fd= new QFileDialog(this);
m_fd->setFileMode(QFileDialog::Directory);
m_fd->setViewMode(QFileDialog::List);
m_fd->setNameFilter(filters);
m_fd->setFileMode(QFileDialog::ExistingFile);
m_fd->setWindowTitle(tr("Select Import Wallpapers"));
m_fd->setLabelText(QFileDialog::Accept, tr("Select"));
m_fd->setLabelText(QFileDialog::Reject, tr("Cancel"));
connect(m_fd, &QFileDialog::fileSelected, this, [this](const QString &file){
QFileInfo fileInfo(file);
QListWidgetItem *item = new QListWidgetItem(fileInfo.fileName());
item->setIcon(QIcon(fileInfo.fileName()));
// item->setSizeHint(QSize(100, 100));
item->setData(Qt::UserRole, fileInfo.fileName());
m_listwidget->addItem(item);
});
openFileDialog();
}
void WallpaperthemeWidget::openFileDialog()
{
if (m_fd->exec() != QDialog::Accepted)
return;
// if(isLegal){
QString path = m_wallpaperpath;
QDir dir(path);
if (!dir.exists()) {
dir.mkpath(path);
}
QString sourceFilePath = m_fd->selectedFiles().first();
if (sourceFilePath.isEmpty())
return;
QFileInfo fileInfo(sourceFilePath);
QString destinationFolderPath = m_wallpaperpath; // 替换为目标文件夹的路径
QString destinationFilePath = destinationFolderPath + "/" + fileInfo.fileName();
QFile::copy(sourceFilePath, destinationFilePath);
//选中的图标文件加显示在界面中
// }
}

View File

@ -4,15 +4,24 @@
#include "widgetbase.h"
#include <QWidget>
#include <QListWidget>
class WallpaperthemeWidget : public WidgetBase
{
Q_OBJECT
public:
explicit WallpaperthemeWidget(QWidget *parent = nullptr);
void init();
void importWallpaper();
void openFileDialog();
signals:
private:
QFileDialog *m_fd;
QString m_wallpaperpath;
QListWidget *m_listwidget;
};
#endif // WALLPAPERTHEMEWIDGET_H

View File

@ -9,7 +9,9 @@
#include <QLineEdit>
#include <QFileDialog>
#include "filecopy.h"
#include "fileoperate.h"
#define HOMEPATH "/home/"
class WidgetBase : public QWidget
{