From 78c58b2b7c11e893b9295bba12ff02bb984af356 Mon Sep 17 00:00:00 2001 From: likehomedream Date: Thu, 21 Sep 2023 16:41:58 +0800 Subject: [PATCH] add delete history dir and update historywidget layout --- kylin-theme-builder.pro | 4 + src/fileProcess/diroperation.cpp | 24 ++++++ src/fileProcess/diroperation.h | 16 ++++ src/fileProcess/historyinfoload.cpp | 72 +++++++++++++++++ src/fileProcess/historyinfoload.h | 22 ++++++ src/module/historywidget.cpp | 117 +++++++++++++--------------- src/module/historywidget.h | 22 ++++-- 7 files changed, 209 insertions(+), 68 deletions(-) create mode 100644 src/fileProcess/diroperation.cpp create mode 100644 src/fileProcess/diroperation.h create mode 100644 src/fileProcess/historyinfoload.cpp create mode 100644 src/fileProcess/historyinfoload.h diff --git a/kylin-theme-builder.pro b/kylin-theme-builder.pro index a5122bb..1d71eab 100644 --- a/kylin-theme-builder.pro +++ b/kylin-theme-builder.pro @@ -18,10 +18,12 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ src/build/buildcheck.cpp \ src/fileProcess/bridge.cpp \ + src/fileProcess/diroperation.cpp \ src/fileProcess/filecheck.cpp \ src/fileProcess/fileprocess.cpp \ src/fileProcess/configfilemanager.cpp \ src/build/buildcheckwidget.cpp \ + src/fileProcess/historyinfoload.cpp \ src/module/cacheconfirmedwidget.cpp \ src/module/cursorthemefeature.cpp \ src/module/cursorthemewidget.cpp \ @@ -40,10 +42,12 @@ SOURCES += \ HEADERS += \ src/build/buildcheck.h \ src/fileProcess/bridge.h \ + src/fileProcess/diroperation.h \ src/fileProcess/filecheck.h \ src/fileProcess/fileprocess.h \ src/fileProcess/configfilemanager.h \ src/build/buildcheckwidget.h \ + src/fileProcess/historyinfoload.h \ src/module/cacheconfirmedwidget.h \ src/module/cursorthemefeature.h \ src/module/cursorthemewidget.h \ diff --git a/src/fileProcess/diroperation.cpp b/src/fileProcess/diroperation.cpp new file mode 100644 index 0000000..cd3eb8e --- /dev/null +++ b/src/fileProcess/diroperation.cpp @@ -0,0 +1,24 @@ +#include "diroperation.h" +#include +#include + +DirOperation::DirOperation(QObject *parent) : QObject(parent) +{ + +} + +void DirOperation::DeleteHistoryDir(const QString &date) +{ + QString dirPath = QDir::homePath() + "/.cache/theme-build/" + date; + + QDir builderDir(dirPath); + if (builderDir.exists()){ + if (builderDir.removeRecursively()){ + qDebug() << "成功删除目录:" << dirPath; + }else{ + qDebug() << "无法删除目录:" << dirPath; + } + }else{ + qDebug() << "目录不存在:" << dirPath; + } +} diff --git a/src/fileProcess/diroperation.h b/src/fileProcess/diroperation.h new file mode 100644 index 0000000..637a650 --- /dev/null +++ b/src/fileProcess/diroperation.h @@ -0,0 +1,16 @@ +#ifndef DIROPERATION_H +#define DIROPERATION_H + +#include + +class DirOperation : public QObject +{ + Q_OBJECT +public: + explicit DirOperation(QObject *parent = nullptr); + void DeleteHistoryDir(const QString& date); +signals: + +}; + +#endif // DIROPERATION_H diff --git a/src/fileProcess/historyinfoload.cpp b/src/fileProcess/historyinfoload.cpp new file mode 100644 index 0000000..dc509f6 --- /dev/null +++ b/src/fileProcess/historyinfoload.cpp @@ -0,0 +1,72 @@ +#include "historyinfoload.h" +#include +#include +#include +HistoryInfoLoad::HistoryInfoLoad(const QString &date, QObject *parent) +{ + m_historytime = date; +} + +QString HistoryInfoLoad::getCover() +{ + QString filePath = QDir::homePath() + "/.cache/theme-build/" + m_historytime + "/config/preview"; + QString previewFilePath; + + QFileInfo pngFileInfo(filePath + ".png"); + if (pngFileInfo.exists()) { + previewFilePath = pngFileInfo.filePath(); + } else { + QFileInfo jpgFileInfo(filePath + ".jpg"); + if (jpgFileInfo.exists()) { + previewFilePath = jpgFileInfo.filePath(); + } else { + previewFilePath = nullptr; + } + } + m_coverpath = previewFilePath; + return m_coverpath; +} + +QString HistoryInfoLoad::getThemeName() +{ + QString filePath = QDir::homePath() + "/.cache/theme-build/" + m_historytime + "/debian/control"; + + QFile file(filePath); + if (file.exists() && file.open(QIODevice::ReadOnly)) { + QString content(file.readAll()); + file.close(); + + QString sourceString = "Source:"; + int sourceIndex = content.indexOf(sourceString); + + if (sourceIndex != -1) { + int startIndex = sourceIndex + sourceString.length(); + QString fieldValue = content.mid(startIndex).trimmed().split("\n").at(0); + + qDebug() << "Source field value: " << fieldValue; + m_themename = fieldValue; + } else { + qDebug() << "No Source field found."; + } + } else { + qDebug() << "File not found or cannot open file."; + } + return m_themename; +} + +QString HistoryInfoLoad::getThemeType() +{ + QString filePath = QDir::homePath() + "/.cache/theme-build/" + m_historytime; + QDir themeDir(filePath); + + if (themeDir.exists("globalTheme")) { + m_themetype = "globalTheme"; + } else { + if (themeDir.exists("cursorTheme")) { + m_themetype = "cursorTheme"; + } else if (themeDir.exists("iconTheme")) { + m_themetype = "iconTheme"; + } + } + return m_themetype; +} diff --git a/src/fileProcess/historyinfoload.h b/src/fileProcess/historyinfoload.h new file mode 100644 index 0000000..d8d4671 --- /dev/null +++ b/src/fileProcess/historyinfoload.h @@ -0,0 +1,22 @@ +#ifndef HISTORYINFOLOAD_H +#define HISTORYINFOLOAD_H + +#include + +class HistoryInfoLoad : public QObject { + Q_OBJECT +public: + explicit HistoryInfoLoad(const QString& date, QObject* parent = nullptr); + QString getCover(); + QString getThemeName(); + QString getThemeType(); +signals: + +private: + QString m_historytime = nullptr; + QString m_coverpath = nullptr; + QString m_themename = nullptr; + QString m_themetype = nullptr; +}; + +#endif // HISTORYINFOLOAD_H diff --git a/src/module/historywidget.cpp b/src/module/historywidget.cpp index f77be3e..e52432a 100644 --- a/src/module/historywidget.cpp +++ b/src/module/historywidget.cpp @@ -1,5 +1,6 @@ #include "historywidget.h" - +#include "../fileProcess/historyinfoload.h" +#include "../fileProcess/diroperation.h" HistoryWidget::HistoryWidget(QWidget *parent) : QWidget(parent) { @@ -44,14 +45,14 @@ HistoryWidget::HistoryWidget(QWidget *parent) : QWidget(parent) foreach (QString folder, validFolders) { HistoryButton *button = new HistoryButton(this); + HistoryInfoLoad *info = new HistoryInfoLoad(folder); + button->setHistoryTime(folder); - button->setCover(); - button->setThemeName(); - button->setThemeType(); + button->setCover(info->getCover()); + button->setThemeName(info->getThemeName()); + button->setThemeType(info->getThemeType()); button->initUI(); - layout->addWidget(button, row, col); - col++; if (col == 4) { col = 0; @@ -96,10 +97,12 @@ void HistoryWidget::updateHistoryDir() foreach (QString folder, validFolders) { HistoryButton *button = new HistoryButton(this); + HistoryInfoLoad *info = new HistoryInfoLoad(folder); + button->setHistoryTime(folder); - button->setCover(); - button->setThemeName(); - button->setThemeType(); + button->setCover(info->getCover()); + button->setThemeName(info->getThemeName()); + button->setThemeType(info->getThemeType()); button->initUI(); layout->addWidget(button, row, col); @@ -127,7 +130,9 @@ void HistoryWidget::updateHistoryDir() HistoryButton::HistoryButton(QWidget* parent): QPushButton(parent) { +// this->setBackgroundRole(QPalette::Base); this->setFixedSize(240, 280); + m_menu = new QMenu(this); } void HistoryButton::setHistoryTime(const QString &text) @@ -135,65 +140,19 @@ void HistoryButton::setHistoryTime(const QString &text) m_historytime = text; } -void HistoryButton::setCover() +void HistoryButton::setCover(const QString & covePath) { - QString filePath = QDir::homePath() + "/.cache/theme-build/" + m_historytime + "/config/preview"; - QString previewFilePath; - - QFileInfo pngFileInfo(filePath + ".png"); - if (pngFileInfo.exists()) { - previewFilePath = pngFileInfo.filePath(); - } else { - QFileInfo jpgFileInfo(filePath + ".jpg"); - if (jpgFileInfo.exists()) { - previewFilePath = jpgFileInfo.filePath(); - } else { - previewFilePath = nullptr; - } - } - m_coverpath = previewFilePath; + m_coverpath = covePath; } -void HistoryButton::setThemeName() +void HistoryButton::setThemeName(const QString &themeName) { - QString filePath = QDir::homePath() + "/.cache/theme-build/" + m_historytime + "/debian/control"; - - QFile file(filePath); - if (file.exists() && file.open(QIODevice::ReadOnly)) { - QString content(file.readAll()); - file.close(); - - QString sourceString = "Source:"; - int sourceIndex = content.indexOf(sourceString); - - if (sourceIndex != -1) { - int startIndex = sourceIndex + sourceString.length(); - QString fieldValue = content.mid(startIndex).trimmed().split("\n").at(0); - - qDebug() << "Source field value: " << fieldValue; - m_themename = fieldValue; - } else { - qDebug() << "No Source field found."; - } - } else { - qDebug() << "File not found or cannot open file."; - } + m_themename = themeName; } -void HistoryButton::setThemeType() +void HistoryButton::setThemeType(const QString &themeType) { - QString filePath = QDir::homePath() + "/.cache/theme-build/" + m_historytime; - QDir themeDir(filePath); - - if (themeDir.exists("globalTheme")) { - m_themetype = "globalTheme"; - } else { - if (themeDir.exists("cursorTheme")) { - m_themetype = "cursorTheme"; - } else if (themeDir.exists("iconTheme")) { - m_themetype = "iconTheme"; - } - } + m_themetype = themeType; } void HistoryButton::initUI() @@ -228,7 +187,41 @@ void HistoryButton::initUI() QLabel* label2 = new QLabel(m_historytime); layout->addWidget(label2); + QWidget * btnwidget = new QWidget(this); + QHBoxLayout * btnwidgetlayout = new QHBoxLayout(btnwidget); + QPushButton* button = new QPushButton(m_themetype); - layout->addWidget(button); + QPushButton* btn_menu = new QPushButton("···"); + btn_menu->setFixedWidth(50); + btnwidgetlayout->addWidget(button); + btnwidgetlayout->addStretch(1); + btnwidgetlayout->addWidget(btn_menu); + btnwidget->setLayout(btnwidgetlayout); + layout->addWidget(btnwidget); + this->setLayout(layout); + // 创建并添加菜单选项 + QAction* action1 = m_menu->addAction("删除"); + QAction* action2 = m_menu->addAction("导出"); + + connect(btn_menu, &QPushButton::clicked, this, [=]() { + m_menu->exec(btn_menu->mapToGlobal(btn_menu->rect().bottomLeft())); + qDebug()<<"aaaaaaaaaaaaa"; + }); + // 连接菜单选项的槽函数 + connect(action1, &QAction::triggered, this, [=]() { + DirOperation *dop = new DirOperation(); + dop->DeleteHistoryDir(m_historytime); + this->deleteLater(); +// emit updateHistoryRequested(); + this->parentWidget()->layout()->update(); + }); + + +} + + +void HistoryWidget::handleButtonClick() +{ + } diff --git a/src/module/historywidget.h b/src/module/historywidget.h index a6fc4e9..bd516e5 100644 --- a/src/module/historywidget.h +++ b/src/module/historywidget.h @@ -1,6 +1,5 @@ -#ifndef HISTORYWIDGET_H +#ifndef HISTORYWIDGET_H #define HISTORYWIDGET_H - #include #include #include @@ -9,22 +8,30 @@ #include #include #include +#include +#include +#include class HistoryButton : public QPushButton { + Q_OBJECT public: HistoryButton(QWidget* parent = nullptr); void setHistoryTime(const QString &text); - void setCover(); - void setThemeName(); - void setThemeType(); + void setCover(const QString &covePath); + void setThemeName(const QString &themeName); + void setThemeType(const QString &themeType); void initUI(); - +// void showContextMenu(); +// void mousePressEvent(QMouseEvent* event); +signals: + void updateHistoryRequested(); private: QString m_historytime = nullptr; QString m_coverpath = nullptr; QString m_themename = nullptr; QString m_themetype = nullptr; + QMenu *m_menu; }; @@ -34,6 +41,9 @@ class HistoryWidget : public QWidget public: explicit HistoryWidget(QWidget *parent = nullptr); void updateHistoryDir(); +public slots: + void handleButtonClick(); + signals: