From 2c367afaf8ea3baf9afa7e9e003e09d97c674abc Mon Sep 17 00:00:00 2001 From: taynpg Date: Sat, 28 Jun 2025 22:53:42 +0800 Subject: [PATCH] fun: add sort func. --- Gui/Control/FileControl.cpp | 148 +++++++++++++++++++++++++++++++++--- Gui/Control/FileControl.h | 17 +++++ 2 files changed, 153 insertions(+), 12 deletions(-) diff --git a/Gui/Control/FileControl.cpp b/Gui/Control/FileControl.cpp index ed93898..6f437c8 100644 --- a/Gui/Control/FileControl.cpp +++ b/Gui/Control/FileControl.cpp @@ -81,6 +81,8 @@ void FileManager::InitControl() connect(ui->btnUp, &QPushButton::clicked, this, &FileManager::evtUp); connect(ui->tableWidget, &CustomTableWidget::sigTasks, this, [this](const QVector& tasks) { emit sigSendTasks(tasks); }); + + connect(ui->tableWidget->horizontalHeader(), &QHeaderView::sectionClicked, this, &FileManager::HeaderClicked); } void FileManager::InitMenu(bool remote) @@ -108,11 +110,101 @@ void FileManager::ShowPath(const QString& path) void FileManager::ShowFile(const DirFileInfoVec& info) { QMutexLocker locker(&tbMut_); - ui->tableWidget->setRowCount(0); - ui->tableWidget->setRowCount(info.vec.size()); + currentInfo_ = info; + SortFileInfo(SortMethod::SMD_BY_TYPE_DESC); + RefreshTab(); + ui->tableWidget->resizeColumnToContents(0); + SetRoot(currentInfo_.root); + ShowPath(GetRoot()); +} - for (int i = 0; i < info.vec.size(); ++i) { - const DirFileInfo& fileInfo = info.vec[i]; +void FileManager::SetRoot(const QString& path) +{ + if (isRemote_) { + GlobalData::Ins()->SetRemoteRoot(path); + } else { + GlobalData::Ins()->SetLocalRoot(path); + } +} + +void FileManager::SortFileInfo(SortMethod method) +{ + auto& vec = currentInfo_.vec; + + switch (method) { + case SortMethod::SMD_BY_NAME_ASC: + std::sort(vec.begin(), vec.end(), [](const DirFileInfo& a, const DirFileInfo& b) { + return QString::compare(a.name, b.name, Qt::CaseInsensitive) < 0; + }); + break; + + case SortMethod::SMD_BY_NAME_DESC: + std::sort(vec.begin(), vec.end(), [](const DirFileInfo& a, const DirFileInfo& b) { + return QString::compare(a.name, b.name, Qt::CaseInsensitive) > 0; + }); + break; + + case SortMethod::SMD_BY_TIME_ASC: + std::sort(vec.begin(), vec.end(), + [](const DirFileInfo& a, const DirFileInfo& b) { return a.lastModified < b.lastModified; }); + break; + + case SortMethod::SMD_BY_TIME_DESC: + std::sort(vec.begin(), vec.end(), + [](const DirFileInfo& a, const DirFileInfo& b) { return a.lastModified > b.lastModified; }); + break; + + case SortMethod::SMD_BY_TYPE_ASC: + std::sort(vec.begin(), vec.end(), [](const DirFileInfo& a, const DirFileInfo& b) { + if (a.type == b.type) { + return QString::compare(a.name, b.name, Qt::CaseInsensitive) < 0; + } + return a.type < b.type; + }); + break; + + case SortMethod::SMD_BY_TYPE_DESC: + std::sort(vec.begin(), vec.end(), [](const DirFileInfo& a, const DirFileInfo& b) { + if (a.type == b.type) { + return QString::compare(a.name, b.name, Qt::CaseInsensitive) < 0; + } + return a.type > b.type; + }); + break; + + case SortMethod::SMD_BY_SIZE_ASC: + std::sort(vec.begin(), vec.end(), [](const DirFileInfo& a, const DirFileInfo& b) { + if (a.type == b.type) { + return a.size < b.size; + } + return a.type == FileType::Dir && b.type != FileType::Dir; + }); + break; + + case SortMethod::SMD_BY_SIZE_DESC: + std::sort(vec.begin(), vec.end(), [](const DirFileInfo& a, const DirFileInfo& b) { + if (a.type == b.type) { + return a.size > b.size; + } + return a.type == FileType::Dir && b.type != FileType::Dir; + }); + break; + + default: + std::sort(vec.begin(), vec.end(), [](const DirFileInfo& a, const DirFileInfo& b) { + return QString::compare(a.name, b.name, Qt::CaseInsensitive) < 0; + }); + break; + } +} + +void FileManager::RefreshTab() +{ + ui->tableWidget->setUpdatesEnabled(false); + ui->tableWidget->setRowCount(0); + ui->tableWidget->setRowCount(currentInfo_.vec.size()); + for (int i = 0; i < currentInfo_.vec.size(); ++i) { + const DirFileInfo& fileInfo = currentInfo_.vec[i]; // *********************************************************************************** auto* iconItem = new QTableWidgetItem(""); @@ -177,18 +269,50 @@ void FileManager::ShowFile(const DirFileInfoVec& info) item->setFlags(item->flags() & ~Qt::ItemIsEditable); ui->tableWidget->setItem(i, 4, item); } - ui->tableWidget->resizeColumnToContents(0); - SetRoot(info.root); - ShowPath(GetRoot()); + ui->tableWidget->setUpdatesEnabled(true); + ui->tableWidget->viewport()->update(); } -void FileManager::SetRoot(const QString& path) +void FileManager::HeaderClicked(int column) { - if (isRemote_) { - GlobalData::Ins()->SetRemoteRoot(path); - } else { - GlobalData::Ins()->SetLocalRoot(path); + SortMethod curMed{}; + switch (column) { + case 1: + if (sortMedRecord_.count(column)) { + curMed = sortMedRecord_[column]; + } + curMed = (curMed == SortMethod::SMD_BY_NAME_ASC ? SortMethod::SMD_BY_NAME_DESC : SortMethod::SMD_BY_NAME_ASC); + SortFileInfo(curMed); + sortMedRecord_[column] = curMed; + break; + case 2: + if (sortMedRecord_.count(column)) { + curMed = sortMedRecord_[column]; + } + curMed = (curMed == SortMethod::SMD_BY_TIME_ASC ? SortMethod::SMD_BY_TIME_DESC : SortMethod::SMD_BY_TIME_ASC); + SortFileInfo(curMed); + sortMedRecord_[column] = curMed; + break; + case 3: + if (sortMedRecord_.count(column)) { + curMed = sortMedRecord_[column]; + } + curMed = (curMed == SortMethod::SMD_BY_TYPE_ASC ? SortMethod::SMD_BY_TYPE_DESC : SortMethod::SMD_BY_TYPE_ASC); + SortFileInfo(curMed); + sortMedRecord_[column] = curMed; + break; + case 4: + if (sortMedRecord_.count(column)) { + curMed = sortMedRecord_[column]; + } + curMed = (curMed == SortMethod::SMD_BY_SIZE_ASC ? SortMethod::SMD_BY_SIZE_DESC : SortMethod::SMD_BY_SIZE_ASC); + SortFileInfo(curMed); + sortMedRecord_[column] = curMed; + break; + default: + return; } + QMetaObject::invokeMethod(this, "RefreshTab", Qt::QueuedConnection); } QString FileManager::GetRoot() diff --git a/Gui/Control/FileControl.h b/Gui/Control/FileControl.h index b27d607..1dce9cd 100644 --- a/Gui/Control/FileControl.h +++ b/Gui/Control/FileControl.h @@ -9,11 +9,23 @@ #include #include #include +#include namespace Ui { class FileManager; } +enum class SortMethod { + SMD_BY_NAME_ASC, + SMD_BY_NAME_DESC, + SMD_BY_TIME_DESC, + SMD_BY_TIME_ASC, + SMD_BY_TYPE_DESC, + SMD_BY_TYPE_ASC, + SMD_BY_SIZE_DESC, + SMD_BY_SIZE_ASC, +}; + class FileManager : public QWidget { Q_OBJECT @@ -36,11 +48,14 @@ private: void ShowFile(const DirFileInfoVec& info); void doubleClick(int row, int column); void SetRoot(const QString& path); + void SortFileInfo(SortMethod method); + void HeaderClicked(int column); public slots: void evtHome(); void evtFile(); void evtUp(); + void RefreshTab(); private: bool isRemote_; @@ -49,6 +64,8 @@ private: ClientCore* cliCore_; QMutex cbMut_; QMutex tbMut_; + DirFileInfoVec currentInfo_; + std::map sortMedRecord_; std::shared_ptr fileHelper_; };