diff options
author | saturneric <[email protected]> | 2025-02-03 18:01:26 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2025-02-03 18:01:26 +0000 |
commit | d3d94f5b7a3e60b3bbb73e832bab7cf2d44a1600 (patch) | |
tree | 5cc7838f1325b076b91bb898fd0da3cfcb60b455 /src/ui/widgets/FileTreeView.cpp | |
parent | doc: update README.md (diff) | |
parent | fix: set pb range when size of operas > 1 (diff) | |
download | GpgFrontend-d3d94f5b7a3e60b3bbb73e832bab7cf2d44a1600.tar.gz GpgFrontend-d3d94f5b7a3e60b3bbb73e832bab7cf2d44a1600.zip |
Merge branch 'develop'
Diffstat (limited to 'src/ui/widgets/FileTreeView.cpp')
-rw-r--r-- | src/ui/widgets/FileTreeView.cpp | 70 |
1 files changed, 48 insertions, 22 deletions
diff --git a/src/ui/widgets/FileTreeView.cpp b/src/ui/widgets/FileTreeView.cpp index 500d9787..86411d79 100644 --- a/src/ui/widgets/FileTreeView.cpp +++ b/src/ui/widgets/FileTreeView.cpp @@ -31,13 +31,13 @@ #include "core/utils/AsyncUtils.h" #include "core/utils/IOUtils.h" #include "ui/UISignalStation.h" -#include "ui/UserInterfaceUtils.h" +#include "ui/function/GpgOperaHelper.h" namespace GpgFrontend::UI { FileTreeView::FileTreeView(QWidget* parent, const QString& target_path) : QTreeView(parent) { - dir_model_ = new QFileSystemModel(); + dir_model_ = new QFileSystemModel(this); dir_model_->setRootPath(target_path.isEmpty() ? QDir::currentPath() : target_path); dir_model_->setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot); @@ -45,10 +45,12 @@ FileTreeView::FileTreeView(QWidget* parent, const QString& target_path) this->setModel(dir_model_); this->setColumnWidth(0, 320); this->sortByColumn(0, Qt::AscendingOrder); + this->setSortingEnabled(true); current_path_ = dir_model_->rootPath(); slot_create_popup_menu(); this->setContextMenuPolicy(Qt::CustomContextMenu); + this->setSelectionMode(QAbstractItemView::SingleSelection); connect(this, &QWidget::customContextMenuRequested, this, &FileTreeView::slot_show_custom_context_menu); @@ -64,16 +66,19 @@ void FileTreeView::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) { QTreeView::selectionChanged(selected, deselected); - if (!selected.indexes().empty()) { - selected_path_ = dir_model_->filePath(selected.indexes().first()); - emit SignalSelectedChanged(selected_path_); - } else { - selected_path_ = QString(); - if (!this->selectedIndexes().isEmpty()) { - selected_path_ = dir_model_->filePath(this->selectedIndexes().front()); - emit SignalSelectedChanged(selected_path_); + selected_paths_.clear(); + if (!this->selectedIndexes().isEmpty()) { + QSet<QString> paths; + for (const auto& index : this->selectedIndexes()) { + const auto path = dir_model_->filePath(index); + if (path == current_path_) continue; + + paths.insert(path); } + selected_paths_.append(paths.values()); } + + emit SignalSelectedChanged(selected_paths_); } void FileTreeView::SlotGoPath(const QString& target_path) { @@ -143,7 +148,7 @@ auto FileTreeView::GetPathByClickPoint(const QPoint& point) -> QString { return dir_model_->fileInfo(index).absoluteFilePath(); } -auto FileTreeView::GetSelectedPath() -> QString { return selected_path_; } +auto FileTreeView::GetSelectedPaths() -> QStringList { return selected_paths_; } auto FileTreeView::SlotDeleteSelectedItem() -> void { QModelIndex const index = this->currentIndex(); @@ -209,7 +214,9 @@ void FileTreeView::SlotTouch() { } void FileTreeView::SlotTouchBelowAtSelectedItem() { - auto root_path(selected_path_); + if (selected_paths_.size() != 1) return; + + auto root_path(selected_paths_.front()); if (root_path.isEmpty()) root_path = dir_model_->rootPath(); QString new_file_name; @@ -243,23 +250,29 @@ void FileTreeView::keyPressEvent(QKeyEvent* event) { } void FileTreeView::SlotOpenSelectedItemBySystemApplication() { - QFileInfo const info(selected_path_); + if (selected_paths_.size() != 1) return; + + auto selected_path = selected_paths_.front(); + QFileInfo const info(selected_path); if (info.isDir()) { const auto file_path = info.filePath().toUtf8(); - QDesktopServices::openUrl(QUrl::fromLocalFile(selected_path_)); + QDesktopServices::openUrl(QUrl::fromLocalFile(selected_path)); } else { - QDesktopServices::openUrl(QUrl::fromLocalFile(selected_path_)); + QDesktopServices::openUrl(QUrl::fromLocalFile(selected_path)); } } void FileTreeView::SlotRenameSelectedItem() { + if (selected_paths_.size() != 1) return; + bool ok; + auto selected_path = selected_paths_.front(); auto text = QInputDialog::getText(this, tr("Rename"), tr("New Filename"), QLineEdit::Normal, - QFileInfo(selected_path_).fileName(), &ok); + QFileInfo(selected_path).fileName(), &ok); if (ok && !text.isEmpty()) { - auto file_info = QFileInfo(selected_path_); + auto file_info = QFileInfo(selected_path); auto new_name_path = file_info.absolutePath() + "/" + text; if (!QDir().rename(file_info.absoluteFilePath(), new_name_path)) { @@ -282,8 +295,9 @@ void FileTreeView::slot_create_popup_menu() { action_open_file_ = new QAction(this); action_open_file_->setText(tr("Open")); - connect(action_open_file_, &QAction::triggered, this, - [this](bool) { emit SignalOpenFile(GetSelectedPath()); }); + connect(action_open_file_, &QAction::triggered, this, [this](bool) { + for (const auto& path : GetSelectedPaths()) emit SignalOpenFile(path); + }); action_rename_file_ = new QAction(this); action_rename_file_->setText(tr("Rename")); @@ -342,8 +356,11 @@ void FileTreeView::slot_create_popup_menu() { void FileTreeView::slot_show_custom_context_menu(const QPoint& point) { auto target_path = this->GetPathByClickPoint(point); - auto select_path = GetSelectedPath(); + auto select_paths = GetSelectedPaths(); + + if (select_paths.size() != 1) return; + auto select_path = select_paths.front(); if (target_path.isEmpty() && !select_path.isEmpty()) { target_path = select_path; } @@ -378,11 +395,14 @@ void FileTreeView::slot_show_custom_context_menu(const QPoint& point) { } void FileTreeView::slot_calculate_hash() { - CommonUtils::WaitForOpera( + if (GetSelectedPaths().empty()) return; + auto selected_path = GetSelectedPaths().front(); + + GpgOperaHelper::WaitForOpera( this->parentWidget(), tr("Calculating"), [=](const OperaWaitingHd& hd) { RunOperaAsync( [=](const DataObjectPtr& data_object) { - data_object->Swap({CalculateHash(this->GetSelectedPath())}); + data_object->Swap({CalculateHash(selected_path)}); return 0; }, [hd](int rtn, const DataObjectPtr& data_object) { @@ -420,4 +440,10 @@ void FileTreeView::slot_adjust_column_widths() { } } +void FileTreeView::SlotSwitchBatchMode(bool batch) { + this->setSelectionMode(batch ? QAbstractItemView::MultiSelection + : QAbstractItemView::SingleSelection); + selectionModel()->clearSelection(); +} + } // namespace GpgFrontend::UI |