add some function

This commit is contained in:
likehomedream 2023-06-21 17:45:15 +08:00
parent 77d0744d71
commit 33945dcc4a
9 changed files with 262 additions and 70 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

@ -10,7 +10,7 @@
#include <QStringListModel>
#include <QMessageBox>
#define HOMEPATH "/home/"
IconThemeWidget::IconThemeWidget(QWidget *parent) : WidgetBase(parent)
@ -28,6 +28,9 @@ IconThemeWidget::IconThemeWidget(QWidget *parent) : WidgetBase(parent)
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);
@ -39,16 +42,35 @@ void IconThemeWidget::init()
// m_allLayout->insertWidget(1,spacer);
// 创建一个QListView
m_listview = new QListView(newWidget);
m_listview->setViewMode(QListView::IconMode);
m_listview->setFixedSize(newWidget->size());
m_listview->setFlow(QListView::LeftToRight);
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));
m_model= new QStandardItemModel(newWidget);
// 初始化固定路径
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);
}
// 将QFileSystemModel设置为QListView的模型
m_listview->setModel(m_model);
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);
@ -60,14 +82,10 @@ void IconThemeWidget::init()
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);
@ -75,19 +93,22 @@ void IconThemeWidget::importIcons()
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;
// 清空模型
// model->clear();
QStringList str_list = url.toString().split("/");
QStandardItem *item = new QStandardItem(QIcon::fromTheme("folder"), str_list.last());
m_model->appendRow(item);
// 创建新的 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();
}
@ -112,53 +133,12 @@ void IconThemeWidget::openFileDialog()
dir.mkpath(path);
}
FileCopy::copyDirectoryFiles(selectedDirectory,path,true);
FileOperate::copyDirectoryFiles(selectedDirectory,path,true);
qDebug()<<"path----------------"<<path;
//选中的图标文件加显示在界面中
// QFileSystemModel* model = qobject_cast<QFileSystemModel*>(this);
// QModelIndex rootIndex = model->index(0, 0);
// model->setRootPath(path);
// m_listview->setRootIndex(rootIndex);
// QStringList fpath = QFileDialog::getOpenFileNames(this, "Please Select PS File", ".", "PS File(*.ps);;All Files(*.*)");
// QFileDialog::getExistingDirectory();
// QStringList pathlist = m_slFilePath; // QStringList类的成员变量
// QStringListModel *modelitem;
// for(int i=0; i<fpath.length(); i++)
// {
// bool flag = true;
// for(int j=0; j<pathlist.length(); j++)
// {
// if(pathlist.at(j) == fpath.at(i))
// {
// flag = false;
// QMessageBox::warning(this, "Error", "The file you selected is duplicated");
// return ;
// }
// }
// if(flag)
// m_slFilePath.append(fpath.at(i));
// }
//// modelitem = new QStringListModel(m_slFilePath);
// QStringList test = {
// "/home/like/workspace/svg","/home/like/workspace/ktb"
// };
// modelitem = new QStringListModel(test);
// m_listview->setModel(modelitem);//m_plistView是QListView类的成员变量这个函数就是把list添加到listView中。
// }else{
// QMessageBox::critical(this, tr("Error"), tr("Invalid file selected. Please try again."));
// // 在消息框关闭后再次打开文件选择对话框
// QMetaObject::invokeMethod(this, "openFileDialog", Qt::QueuedConnection);
// }
}

View File

@ -3,7 +3,7 @@
#include "widgetbase.h"
#include "filecopy.h"
#include "fileoperate.h"
#include <QObject>
#include <QWidget>
@ -12,6 +12,59 @@
#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
{
@ -32,6 +85,7 @@ private:
QString m_iconpath;
QListView *m_listview;
QStandardItemModel *m_model;
QListWidget *m_listwidget;
};
#endif // ICONTHEMEWIDGET_H

View File

@ -73,3 +73,9 @@ bool ThemesCheck::checkIconsDir(QString folderPath)
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
{