From 33945dcc4abd9b069ab08ace7f0dad9fe5b8fbdb Mon Sep 17 00:00:00 2001 From: likehomedream Date: Wed, 21 Jun 2023 17:45:15 +0800 Subject: [PATCH] add some function --- src/baseUI/baseUI.pro | 4 +- src/baseUI/{filecopy.cpp => fileoperate.cpp} | 32 +++++- src/baseUI/{filecopy.h => fileoperate.h} | 5 +- src/baseUI/iconthemewidget.cpp | 102 +++++++---------- src/baseUI/iconthemewidget.h | 56 ++++++++- src/baseUI/themescheck/themescheck.cpp | 6 + src/baseUI/wallpaperthemewidget.cpp | 114 +++++++++++++++++++ src/baseUI/wallpaperthemewidget.h | 9 ++ src/baseUI/widgetbase.h | 4 +- 9 files changed, 262 insertions(+), 70 deletions(-) rename src/baseUI/{filecopy.cpp => fileoperate.cpp} (52%) rename src/baseUI/{filecopy.h => fileoperate.h} (60%) diff --git a/src/baseUI/baseUI.pro b/src/baseUI/baseUI.pro index 0d8fed7..311a6ff 100644 --- a/src/baseUI/baseUI.pro +++ b/src/baseUI/baseUI.pro @@ -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 \ diff --git a/src/baseUI/filecopy.cpp b/src/baseUI/fileoperate.cpp similarity index 52% rename from src/baseUI/filecopy.cpp rename to src/baseUI/fileoperate.cpp index 73f7634..e8ff5e6 100644 --- a/src/baseUI/filecopy.cpp +++ b/src/baseUI/fileoperate.cpp @@ -1,14 +1,15 @@ -#include "filecopy.h" +#include "fileoperate.h" #include +#include -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"< -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: }; diff --git a/src/baseUI/iconthemewidget.cpp b/src/baseUI/iconthemewidget.cpp index 518bbe8..fbfc99a 100644 --- a/src/baseUI/iconthemewidget.cpp +++ b/src/baseUI/iconthemewidget.cpp @@ -10,7 +10,7 @@ #include #include -#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;"<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;"<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----------------"<(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; isetModel(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); -// } - - } diff --git a/src/baseUI/iconthemewidget.h b/src/baseUI/iconthemewidget.h index 438a70b..82a110e 100644 --- a/src/baseUI/iconthemewidget.h +++ b/src/baseUI/iconthemewidget.h @@ -3,7 +3,7 @@ #include "widgetbase.h" -#include "filecopy.h" +#include "fileoperate.h" #include #include @@ -12,6 +12,59 @@ #include #include #include +#include +#include + +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(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 diff --git a/src/baseUI/themescheck/themescheck.cpp b/src/baseUI/themescheck/themescheck.cpp index df1067b..164e617 100644 --- a/src/baseUI/themescheck/themescheck.cpp +++ b/src/baseUI/themescheck/themescheck.cpp @@ -73,3 +73,9 @@ bool ThemesCheck::checkIconsDir(QString folderPath) qDebug()<<"yes!!!!!!!!!!"; return true; } + +bool ThemesCheck::checkWallpaperDir(QString folderPath) +{ + //检查图片大小。 +// folderPath +} diff --git a/src/baseUI/wallpaperthemewidget.cpp b/src/baseUI/wallpaperthemewidget.cpp index cec8d11..fdb0d6b 100644 --- a/src/baseUI/wallpaperthemewidget.cpp +++ b/src/baseUI/wallpaperthemewidget.cpp @@ -1,7 +1,121 @@ #include "wallpaperthemewidget.h" +#include 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;"<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); + + + //选中的图标文件加显示在界面中 + + // } } diff --git a/src/baseUI/wallpaperthemewidget.h b/src/baseUI/wallpaperthemewidget.h index 03bb9bd..a4194e7 100644 --- a/src/baseUI/wallpaperthemewidget.h +++ b/src/baseUI/wallpaperthemewidget.h @@ -4,15 +4,24 @@ #include "widgetbase.h" #include +#include 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 diff --git a/src/baseUI/widgetbase.h b/src/baseUI/widgetbase.h index aade2f1..320ce28 100644 --- a/src/baseUI/widgetbase.h +++ b/src/baseUI/widgetbase.h @@ -9,7 +9,9 @@ #include #include -#include "filecopy.h" +#include "fileoperate.h" + +#define HOMEPATH "/home/" class WidgetBase : public QWidget {