From 1cdba285be9bebd9b793fc6ae0369d201a0e713b Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 1 Jan 2024 17:15:25 +0800 Subject: feat: improve file browser's functions and tidy up codes --- src/ui/widgets/FileTreeView.cpp | 405 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 405 insertions(+) create mode 100644 src/ui/widgets/FileTreeView.cpp (limited to 'src/ui/widgets/FileTreeView.cpp') diff --git a/src/ui/widgets/FileTreeView.cpp b/src/ui/widgets/FileTreeView.cpp new file mode 100644 index 00000000..7a321281 --- /dev/null +++ b/src/ui/widgets/FileTreeView.cpp @@ -0,0 +1,405 @@ +/** + * Copyright (C) 2021 Saturneric + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see . + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "FileTreeView.h" + +#include "core/utils/IOUtils.h" +#include "ui/UISignalStation.h" + +namespace GpgFrontend::UI { + +FileTreeView::FileTreeView(QWidget* parent) : QTreeView(parent) { + dir_model_ = new QFileSystemModel(); + dir_model_->setRootPath(QDir::currentPath()); + dir_model_->setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot); + + this->setModel(dir_model_); + this->setColumnWidth(0, 320); + this->sortByColumn(0, Qt::AscendingOrder); + current_path_ = std::filesystem::path(dir_model_->rootPath().toStdString()); + + slot_create_popup_menu(); + this->setContextMenuPolicy(Qt::CustomContextMenu); + connect(this, &QWidget::customContextMenuRequested, this, + &FileTreeView::slot_show_custom_context_menu); + + connect(this, &QTreeView::clicked, this, + &FileTreeView::slot_file_tree_view_item_selected); + connect(this, &QTreeView::doubleClicked, this, + &FileTreeView::slot_file_tree_view_item_double_clicked); +} + +void FileTreeView::slot_file_tree_view_item_selected(const QModelIndex& index) { + +} + +void FileTreeView::selectionChanged(const QItemSelection& selected, + const QItemSelection& deselected) { + QTreeView::selectionChanged(selected, deselected); + + if (!selected.indexes().empty()) { + selected_path_ = dir_model_->fileInfo(selected.indexes().first()) + .filesystemAbsoluteFilePath(); + SPDLOG_DEBUG("file tree view selected target path: {}", + selected_path_.u8string()); + emit SignalSelectedChanged(QString::fromStdString(selected_path_)); + } else { + selected_path_ = std::filesystem::path{}; + } +} + +void FileTreeView::SlotGoPath(const std::filesystem::path& target_path) { + auto file_info = QFileInfo(target_path); + if (file_info.isDir() && file_info.isReadable() && file_info.isExecutable()) { + current_path_ = file_info.filesystemAbsoluteFilePath(); + SPDLOG_DEBUG("file tree view set target path: {}", + current_path_.u8string()); + this->setRootIndex(dir_model_->index(file_info.filePath())); + dir_model_->setRootPath(file_info.filePath()); + for (int i = 1; i < dir_model_->columnCount(); ++i) { + this->resizeColumnToContents(i); + } + } else { + QMessageBox::critical( + this, _("Error"), + _("The path is not exists, unprivileged or unreachable.")); + } + emit SignalPathChanged(QString::fromStdString(current_path_.u8string())); +} + +void FileTreeView::slot_file_tree_view_item_double_clicked( + const QModelIndex& index) { + QFileInfo const file_info(dir_model_->fileInfo(index).absoluteFilePath()); + if (file_info.isFile()) { + if (file_info.isReadable()) { + emit SignalOpenFile(file_info.absoluteFilePath()); + } else { + QMessageBox::critical(this, _("Error"), + _("The file is unprivileged or unreachable.")); + } + } else { + SlotGoPath(file_info.filesystemAbsoluteFilePath()); + } +} + +void FileTreeView::SlotUpLevel() { + QModelIndex const current_root = this->rootIndex(); + + auto target_path = + dir_model_->fileInfo(current_root).filesystemAbsoluteFilePath(); + if (target_path.has_parent_path() && !target_path.parent_path().empty()) { + target_path = target_path.parent_path(); + SPDLOG_DEBUG("file tree view go parent path: {}", target_path.u8string()); + this->SlotGoPath(target_path); + } + current_path_ = target_path; +} + +auto FileTreeView::GetCurrentPath() -> std::filesystem::path { + return current_path_; +} + +void FileTreeView::SlotShowSystemFile(bool on) { + auto filters = on ? dir_model_->filter() | QDir::System + : dir_model_->filter() & ~QDir::System; + dir_model_->setFilter(filters); + dir_model_->setRootPath(QString::fromStdString(current_path_.u8string())); +} + +void FileTreeView::SlotShowHiddenFile(bool on) { + auto filters = on ? dir_model_->filter() | QDir::Hidden + : dir_model_->filter() & ~QDir::Hidden; + dir_model_->setFilter(filters); + dir_model_->setRootPath(QString::fromStdString(current_path_.u8string())); +} + +auto FileTreeView::GetPathByClickPoint(const QPoint& point) + -> std::filesystem::path { + auto const index = this->indexAt(point); + + if (!index.isValid()) { + return {}; + } + + auto index_path = dir_model_->fileInfo(index).filesystemAbsoluteFilePath(); + SPDLOG_DEBUG("file tree view right click on: {}", index_path.string()); + return index_path; +} + +auto FileTreeView::GetSelectedPath() -> std::filesystem::path { + return selected_path_; +} + +auto FileTreeView::SlotDeleteSelectedItem() -> void { + QModelIndex const index = this->currentIndex(); + QVariant const data = this->model()->data(index); + + auto ret = QMessageBox::warning(this, _("Warning"), + _("Are you sure you want to delete it?"), + QMessageBox::Ok | QMessageBox::Cancel); + + if (ret == QMessageBox::Cancel) return; + + SPDLOG_DEBUG("delete item: {}", data.toString().toStdString()); + + if (!dir_model_->remove(index)) { + QMessageBox::critical(this, _("Error"), + _("Unable to delete the file or folder.")); + } +} + +void FileTreeView::SlotMkdir() { + auto index = this->rootIndex(); + + QString new_dir_name; + bool ok; + new_dir_name = + QInputDialog::getText(this, _("Make New Directory"), _("Directory Name"), + QLineEdit::Normal, new_dir_name, &ok); + if (ok && !new_dir_name.isEmpty()) { + dir_model_->mkdir(index, new_dir_name); + } +} + +void FileTreeView::SlotMkdirBelowAtSelectedItem() { + auto index = this->currentIndex(); + + QString new_dir_name; + bool ok; + new_dir_name = + QInputDialog::getText(this, _("Make New Directory"), _("Directory Name"), + QLineEdit::Normal, new_dir_name, &ok); + if (ok && !new_dir_name.isEmpty()) { + dir_model_->mkdir(index, new_dir_name); + } +} + +void FileTreeView::SlotTouch() { +#ifdef WINDOWS + auto root_path_str = dir_model_->rootPath().toStdU16String(); +#else + auto root_path_str = dir_model_->rootPath().toStdString(); +#endif + std::filesystem::path root_path(root_path_str); + + QString new_file_name; + bool ok; + new_file_name = QInputDialog::getText(this, _("Create Empty File"), + _("Filename (you can given extension)"), + QLineEdit::Normal, new_file_name, &ok); + if (ok && !new_file_name.isEmpty()) { +#ifdef WINDOWS + auto file_path = root_path / new_file_name.toStdU16String(); +#else + auto file_path = root_path / new_file_name.toStdString(); +#endif + QFile new_file(file_path.u8string().c_str()); + if (!new_file.open(QIODevice::WriteOnly | QIODevice::NewOnly)) { + QMessageBox::critical(this, _("Error"), _("Unable to create the file.")); + } + new_file.close(); + } +} + +void FileTreeView::SlotTouchBelowAtSelectedItem() { + std::filesystem::path root_path(selected_path_); + + QString new_file_name; + bool ok; + new_file_name = QInputDialog::getText(this, _("Create Empty File"), + _("Filename (you can given extension)"), + QLineEdit::Normal, new_file_name, &ok); + if (ok && !new_file_name.isEmpty()) { +#ifdef WINDOWS + auto file_path = root_path / new_file_name.toStdU16String(); +#else + auto file_path = root_path / new_file_name.toStdString(); +#endif + QFile new_file(file_path.u8string().c_str()); + if (!new_file.open(QIODevice::WriteOnly | QIODevice::NewOnly)) { + QMessageBox::critical(this, _("Error"), _("Unable to create the file.")); + } + new_file.close(); + } +} + +void FileTreeView::keyPressEvent(QKeyEvent* event) { + QTreeView::keyPressEvent(event); + + if (this->currentIndex().isValid()) { + if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) { + slot_file_tree_view_item_double_clicked(this->currentIndex()); + } else if (event->key() == Qt::Key_Delete || + event->key() == Qt::Key_Backspace) { + SlotDeleteSelectedItem(); + } + } +} + +void FileTreeView::SlotOpenSelectedItemBySystemApplication() { + QFileInfo const info(QString::fromStdString(selected_path_.u8string())); + auto q_selected_path = QString::fromStdString(selected_path_.u8string()); + if (info.isDir()) { + const auto file_path = info.filePath().toUtf8().toStdString(); + QDesktopServices::openUrl(QUrl::fromLocalFile(q_selected_path)); + + } else { + QDesktopServices::openUrl(QUrl::fromLocalFile(q_selected_path)); + } +} + +void FileTreeView::SlotRenameSelectedItem() { + bool ok; + auto text = QInputDialog::getText( + this, _("Rename"), _("New Filename"), QLineEdit::Normal, + QString::fromStdString(selected_path_.filename().u8string()), &ok); + if (ok && !text.isEmpty()) { + try { +#ifdef WINDOWS + auto new_name_path = selected_path_.parent_path() / text.toStdU16String(); +#else + auto new_name_path = selected_path_.parent_path() / text.toStdString(); +#endif + SPDLOG_DEBUG("new name path: {}", new_name_path.u8string()); + std::filesystem::rename(selected_path_, new_name_path); + + // refresh + SlotGoPath(current_path_); + } catch (...) { + SPDLOG_ERROR("file tree view rename error: {}", + selected_path_.u8string()); + QMessageBox::critical(this, _("Error"), + _("Unable to rename the file or folder.")); + } + } +} + +auto FileTreeView::GetMousePointGlobal(const QPoint& point) -> QPoint { + return this->viewport()->mapToGlobal(point); +} + +void FileTreeView::slot_create_popup_menu() { + popup_menu_ = new QMenu(); + + action_open_file_ = new QAction(this); + action_open_file_->setText(_("Open")); + connect(action_open_file_, &QAction::triggered, this, [this](bool) { + emit SignalOpenFile(QString::fromStdString(GetSelectedPath())); + }); + + action_rename_file_ = new QAction(this); + action_rename_file_->setText(_("Rename")); + connect(action_rename_file_, &QAction::triggered, this, + &FileTreeView::SlotRenameSelectedItem); + + action_delete_file_ = new QAction(this); + action_delete_file_->setText(_("Delete")); + connect(action_delete_file_, &QAction::triggered, this, + &FileTreeView::SlotDeleteSelectedItem); + + action_calculate_hash_ = new QAction(this); + action_calculate_hash_->setText(_("Calculate Hash")); + connect(action_calculate_hash_, &QAction::triggered, this, + &FileTreeView::slot_calculate_hash); + + action_make_directory_ = new QAction(this); + action_make_directory_->setText(_("Directory")); + connect(action_make_directory_, &QAction::triggered, this, + &FileTreeView::SlotMkdirBelowAtSelectedItem); + + action_create_empty_file_ = new QAction(this); + action_create_empty_file_->setText(_("File")); + connect(action_create_empty_file_, &QAction::triggered, this, + &FileTreeView::SlotTouchBelowAtSelectedItem); + + action_compress_files_ = new QAction(this); + action_compress_files_->setText(_("Compress...")); + action_compress_files_->setVisible(false); + connect(action_compress_files_, &QAction::triggered, this, + &FileTreeView::slot_compress_files); + + auto* action_open_with_system_default_application = new QAction(this); + action_open_with_system_default_application->setText( + _("Open with Default System Application")); + connect(action_open_with_system_default_application, &QAction::triggered, + this, &FileTreeView::SlotOpenSelectedItemBySystemApplication); + + auto* new_item_action_menu = new QMenu(this); + new_item_action_menu->setTitle(_("New")); + new_item_action_menu->addAction(action_create_empty_file_); + new_item_action_menu->addAction(action_make_directory_); + + popup_menu_->addAction(action_open_file_); + popup_menu_->addAction(action_open_with_system_default_application); + + popup_menu_->addSeparator(); + popup_menu_->addMenu(new_item_action_menu); + popup_menu_->addSeparator(); + + popup_menu_->addAction(action_rename_file_); + popup_menu_->addAction(action_delete_file_); + popup_menu_->addAction(action_compress_files_); + popup_menu_->addAction(action_calculate_hash_); +} + +void FileTreeView::slot_show_custom_context_menu(const QPoint& point) { + auto target_path = this->GetPathByClickPoint(point); + + if (!target_path.empty()) { + action_open_file_->setEnabled(true); + action_rename_file_->setEnabled(true); + action_delete_file_->setEnabled(true); + + QFileInfo const info(QString::fromStdString(this->GetSelectedPath())); + action_calculate_hash_->setEnabled(info.isFile() && info.isReadable()); + + } else { + action_open_file_->setEnabled(false); + action_rename_file_->setEnabled(false); + action_delete_file_->setEnabled(false); + + action_calculate_hash_->setEnabled(false); + } + popup_menu_->exec(this->GetMousePointGlobal(point)); +} + +void FileTreeView::slot_calculate_hash() { + emit UISignalStation::GetInstance()->SignalRefreshInfoBoard( + QString::fromStdString(CalculateHash(this->GetSelectedPath())), + InfoBoardStatus::INFO_ERROR_OK); +} + +void FileTreeView::slot_compress_files() {} + +void FileTreeView::paintEvent(QPaintEvent* event) { + QTreeView::paintEvent(event); + for (int i = 1; i < dir_model_->columnCount(); ++i) { + this->resizeColumnToContents(i); + } +} +} // namespace GpgFrontend::UI -- cgit v1.2.3 From dcdd494d7f6654a5e2d608d37f69e16cb15fa845 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 1 Jan 2024 17:50:54 +0800 Subject: fix: find and solve some issues --- src/ui/widgets/FileTreeView.cpp | 7 ------- 1 file changed, 7 deletions(-) (limited to 'src/ui/widgets/FileTreeView.cpp') diff --git a/src/ui/widgets/FileTreeView.cpp b/src/ui/widgets/FileTreeView.cpp index 7a321281..9fc8aec4 100644 --- a/src/ui/widgets/FileTreeView.cpp +++ b/src/ui/widgets/FileTreeView.cpp @@ -47,17 +47,10 @@ FileTreeView::FileTreeView(QWidget* parent) : QTreeView(parent) { this->setContextMenuPolicy(Qt::CustomContextMenu); connect(this, &QWidget::customContextMenuRequested, this, &FileTreeView::slot_show_custom_context_menu); - - connect(this, &QTreeView::clicked, this, - &FileTreeView::slot_file_tree_view_item_selected); connect(this, &QTreeView::doubleClicked, this, &FileTreeView::slot_file_tree_view_item_double_clicked); } -void FileTreeView::slot_file_tree_view_item_selected(const QModelIndex& index) { - -} - void FileTreeView::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) { QTreeView::selectionChanged(selected, deselected); -- cgit v1.2.3 From 644aa4397b03dbef73f8bfedc13925b51cad836b Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 5 Jan 2024 20:55:15 +0800 Subject: feat: integrate logging api to core --- src/ui/widgets/FileTreeView.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'src/ui/widgets/FileTreeView.cpp') diff --git a/src/ui/widgets/FileTreeView.cpp b/src/ui/widgets/FileTreeView.cpp index 9fc8aec4..79db51ac 100644 --- a/src/ui/widgets/FileTreeView.cpp +++ b/src/ui/widgets/FileTreeView.cpp @@ -58,8 +58,8 @@ void FileTreeView::selectionChanged(const QItemSelection& selected, if (!selected.indexes().empty()) { selected_path_ = dir_model_->fileInfo(selected.indexes().first()) .filesystemAbsoluteFilePath(); - SPDLOG_DEBUG("file tree view selected target path: {}", - selected_path_.u8string()); + GF_UI_LOG_DEBUG("file tree view selected target path: {}", + selected_path_.u8string()); emit SignalSelectedChanged(QString::fromStdString(selected_path_)); } else { selected_path_ = std::filesystem::path{}; @@ -70,8 +70,8 @@ void FileTreeView::SlotGoPath(const std::filesystem::path& target_path) { auto file_info = QFileInfo(target_path); if (file_info.isDir() && file_info.isReadable() && file_info.isExecutable()) { current_path_ = file_info.filesystemAbsoluteFilePath(); - SPDLOG_DEBUG("file tree view set target path: {}", - current_path_.u8string()); + GF_UI_LOG_DEBUG("file tree view set target path: {}", + current_path_.u8string()); this->setRootIndex(dir_model_->index(file_info.filePath())); dir_model_->setRootPath(file_info.filePath()); for (int i = 1; i < dir_model_->columnCount(); ++i) { @@ -107,7 +107,8 @@ void FileTreeView::SlotUpLevel() { dir_model_->fileInfo(current_root).filesystemAbsoluteFilePath(); if (target_path.has_parent_path() && !target_path.parent_path().empty()) { target_path = target_path.parent_path(); - SPDLOG_DEBUG("file tree view go parent path: {}", target_path.u8string()); + GF_UI_LOG_DEBUG("file tree view go parent path: {}", + target_path.u8string()); this->SlotGoPath(target_path); } current_path_ = target_path; @@ -140,7 +141,7 @@ auto FileTreeView::GetPathByClickPoint(const QPoint& point) } auto index_path = dir_model_->fileInfo(index).filesystemAbsoluteFilePath(); - SPDLOG_DEBUG("file tree view right click on: {}", index_path.string()); + GF_UI_LOG_DEBUG("file tree view right click on: {}", index_path.string()); return index_path; } @@ -158,7 +159,7 @@ auto FileTreeView::SlotDeleteSelectedItem() -> void { if (ret == QMessageBox::Cancel) return; - SPDLOG_DEBUG("delete item: {}", data.toString().toStdString()); + GF_UI_LOG_DEBUG("delete item: {}", data.toString().toStdString()); if (!dir_model_->remove(index)) { QMessageBox::critical(this, _("Error"), @@ -278,14 +279,14 @@ void FileTreeView::SlotRenameSelectedItem() { #else auto new_name_path = selected_path_.parent_path() / text.toStdString(); #endif - SPDLOG_DEBUG("new name path: {}", new_name_path.u8string()); + GF_UI_LOG_DEBUG("new name path: {}", new_name_path.u8string()); std::filesystem::rename(selected_path_, new_name_path); // refresh SlotGoPath(current_path_); } catch (...) { - SPDLOG_ERROR("file tree view rename error: {}", - selected_path_.u8string()); + GF_UI_LOG_ERROR("file tree view rename error: {}", + selected_path_.u8string()); QMessageBox::critical(this, _("Error"), _("Unable to rename the file or folder.")); } -- cgit v1.2.3 From ba7dd4ef022b19e307557561e2c8d788768c5026 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 8 Jan 2024 15:22:03 +0800 Subject: feat: select a directory before entering into the File Browser --- src/ui/widgets/FileTreeView.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/ui/widgets/FileTreeView.cpp') diff --git a/src/ui/widgets/FileTreeView.cpp b/src/ui/widgets/FileTreeView.cpp index 79db51ac..8139fbf9 100644 --- a/src/ui/widgets/FileTreeView.cpp +++ b/src/ui/widgets/FileTreeView.cpp @@ -33,9 +33,11 @@ namespace GpgFrontend::UI { -FileTreeView::FileTreeView(QWidget* parent) : QTreeView(parent) { +FileTreeView::FileTreeView(QWidget* parent, const QString& target_path) + : QTreeView(parent) { dir_model_ = new QFileSystemModel(); - dir_model_->setRootPath(QDir::currentPath()); + dir_model_->setRootPath(target_path.isEmpty() ? QDir::currentPath() + : target_path); dir_model_->setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot); this->setModel(dir_model_); -- cgit v1.2.3 From bf538056b24a68b8fd235b1c50991ee8eb46a776 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 12 Jan 2024 14:02:37 +0800 Subject: refactor: use QString instead of std::string and improve threading system --- src/ui/widgets/FileTreeView.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ui/widgets/FileTreeView.cpp') diff --git a/src/ui/widgets/FileTreeView.cpp b/src/ui/widgets/FileTreeView.cpp index 8139fbf9..ef0fbf48 100644 --- a/src/ui/widgets/FileTreeView.cpp +++ b/src/ui/widgets/FileTreeView.cpp @@ -386,7 +386,7 @@ void FileTreeView::slot_show_custom_context_menu(const QPoint& point) { void FileTreeView::slot_calculate_hash() { emit UISignalStation::GetInstance()->SignalRefreshInfoBoard( - QString::fromStdString(CalculateHash(this->GetSelectedPath())), + CalculateHash(this->GetSelectedPath().c_str()), InfoBoardStatus::INFO_ERROR_OK); } -- cgit v1.2.3 From 620ae9e7c1a8b2db2515c080416cb592066e5fec Mon Sep 17 00:00:00 2001 From: saturneric Date: Tue, 16 Jan 2024 21:35:59 +0800 Subject: refactor: remove libgettext from project --- src/ui/widgets/FileTreeView.cpp | 70 +++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 34 deletions(-) (limited to 'src/ui/widgets/FileTreeView.cpp') diff --git a/src/ui/widgets/FileTreeView.cpp b/src/ui/widgets/FileTreeView.cpp index ef0fbf48..f3556dc9 100644 --- a/src/ui/widgets/FileTreeView.cpp +++ b/src/ui/widgets/FileTreeView.cpp @@ -81,8 +81,8 @@ void FileTreeView::SlotGoPath(const std::filesystem::path& target_path) { } } else { QMessageBox::critical( - this, _("Error"), - _("The path is not exists, unprivileged or unreachable.")); + this, tr("Error"), + tr("The path is not exists, unprivileged or unreachable.")); } emit SignalPathChanged(QString::fromStdString(current_path_.u8string())); } @@ -94,8 +94,8 @@ void FileTreeView::slot_file_tree_view_item_double_clicked( if (file_info.isReadable()) { emit SignalOpenFile(file_info.absoluteFilePath()); } else { - QMessageBox::critical(this, _("Error"), - _("The file is unprivileged or unreachable.")); + QMessageBox::critical(this, tr("Error"), + tr("The file is unprivileged or unreachable.")); } } else { SlotGoPath(file_info.filesystemAbsoluteFilePath()); @@ -155,8 +155,8 @@ auto FileTreeView::SlotDeleteSelectedItem() -> void { QModelIndex const index = this->currentIndex(); QVariant const data = this->model()->data(index); - auto ret = QMessageBox::warning(this, _("Warning"), - _("Are you sure you want to delete it?"), + auto ret = QMessageBox::warning(this, tr("Warning"), + tr("Are you sure you want to delete it?"), QMessageBox::Ok | QMessageBox::Cancel); if (ret == QMessageBox::Cancel) return; @@ -164,8 +164,8 @@ auto FileTreeView::SlotDeleteSelectedItem() -> void { GF_UI_LOG_DEBUG("delete item: {}", data.toString().toStdString()); if (!dir_model_->remove(index)) { - QMessageBox::critical(this, _("Error"), - _("Unable to delete the file or folder.")); + QMessageBox::critical(this, tr("Error"), + tr("Unable to delete the file or folder.")); } } @@ -174,9 +174,9 @@ void FileTreeView::SlotMkdir() { QString new_dir_name; bool ok; - new_dir_name = - QInputDialog::getText(this, _("Make New Directory"), _("Directory Name"), - QLineEdit::Normal, new_dir_name, &ok); + new_dir_name = QInputDialog::getText(this, tr("Make New Directory"), + tr("Directory Name"), QLineEdit::Normal, + new_dir_name, &ok); if (ok && !new_dir_name.isEmpty()) { dir_model_->mkdir(index, new_dir_name); } @@ -187,9 +187,9 @@ void FileTreeView::SlotMkdirBelowAtSelectedItem() { QString new_dir_name; bool ok; - new_dir_name = - QInputDialog::getText(this, _("Make New Directory"), _("Directory Name"), - QLineEdit::Normal, new_dir_name, &ok); + new_dir_name = QInputDialog::getText(this, tr("Make New Directory"), + tr("Directory Name"), QLineEdit::Normal, + new_dir_name, &ok); if (ok && !new_dir_name.isEmpty()) { dir_model_->mkdir(index, new_dir_name); } @@ -205,9 +205,9 @@ void FileTreeView::SlotTouch() { QString new_file_name; bool ok; - new_file_name = QInputDialog::getText(this, _("Create Empty File"), - _("Filename (you can given extension)"), - QLineEdit::Normal, new_file_name, &ok); + new_file_name = QInputDialog::getText( + this, tr("Create Empty File"), tr("Filename (you can given extension)"), + QLineEdit::Normal, new_file_name, &ok); if (ok && !new_file_name.isEmpty()) { #ifdef WINDOWS auto file_path = root_path / new_file_name.toStdU16String(); @@ -216,7 +216,8 @@ void FileTreeView::SlotTouch() { #endif QFile new_file(file_path.u8string().c_str()); if (!new_file.open(QIODevice::WriteOnly | QIODevice::NewOnly)) { - QMessageBox::critical(this, _("Error"), _("Unable to create the file.")); + QMessageBox::critical(this, tr("Error"), + tr("Unable to create the file.")); } new_file.close(); } @@ -227,9 +228,9 @@ void FileTreeView::SlotTouchBelowAtSelectedItem() { QString new_file_name; bool ok; - new_file_name = QInputDialog::getText(this, _("Create Empty File"), - _("Filename (you can given extension)"), - QLineEdit::Normal, new_file_name, &ok); + new_file_name = QInputDialog::getText( + this, tr("Create Empty File"), tr("Filename (you can given extension)"), + QLineEdit::Normal, new_file_name, &ok); if (ok && !new_file_name.isEmpty()) { #ifdef WINDOWS auto file_path = root_path / new_file_name.toStdU16String(); @@ -238,7 +239,8 @@ void FileTreeView::SlotTouchBelowAtSelectedItem() { #endif QFile new_file(file_path.u8string().c_str()); if (!new_file.open(QIODevice::WriteOnly | QIODevice::NewOnly)) { - QMessageBox::critical(this, _("Error"), _("Unable to create the file.")); + QMessageBox::critical(this, tr("Error"), + tr("Unable to create the file.")); } new_file.close(); } @@ -272,7 +274,7 @@ void FileTreeView::SlotOpenSelectedItemBySystemApplication() { void FileTreeView::SlotRenameSelectedItem() { bool ok; auto text = QInputDialog::getText( - this, _("Rename"), _("New Filename"), QLineEdit::Normal, + this, tr("Rename"), tr("New Filename"), QLineEdit::Normal, QString::fromStdString(selected_path_.filename().u8string()), &ok); if (ok && !text.isEmpty()) { try { @@ -289,8 +291,8 @@ void FileTreeView::SlotRenameSelectedItem() { } catch (...) { GF_UI_LOG_ERROR("file tree view rename error: {}", selected_path_.u8string()); - QMessageBox::critical(this, _("Error"), - _("Unable to rename the file or folder.")); + QMessageBox::critical(this, tr("Error"), + tr("Unable to rename the file or folder.")); } } } @@ -303,50 +305,50 @@ void FileTreeView::slot_create_popup_menu() { popup_menu_ = new QMenu(); action_open_file_ = new QAction(this); - action_open_file_->setText(_("Open")); + action_open_file_->setText(tr("Open")); connect(action_open_file_, &QAction::triggered, this, [this](bool) { emit SignalOpenFile(QString::fromStdString(GetSelectedPath())); }); action_rename_file_ = new QAction(this); - action_rename_file_->setText(_("Rename")); + action_rename_file_->setText(tr("Rename")); connect(action_rename_file_, &QAction::triggered, this, &FileTreeView::SlotRenameSelectedItem); action_delete_file_ = new QAction(this); - action_delete_file_->setText(_("Delete")); + action_delete_file_->setText(tr("Delete")); connect(action_delete_file_, &QAction::triggered, this, &FileTreeView::SlotDeleteSelectedItem); action_calculate_hash_ = new QAction(this); - action_calculate_hash_->setText(_("Calculate Hash")); + action_calculate_hash_->setText(tr("Calculate Hash")); connect(action_calculate_hash_, &QAction::triggered, this, &FileTreeView::slot_calculate_hash); action_make_directory_ = new QAction(this); - action_make_directory_->setText(_("Directory")); + action_make_directory_->setText(tr("Directory")); connect(action_make_directory_, &QAction::triggered, this, &FileTreeView::SlotMkdirBelowAtSelectedItem); action_create_empty_file_ = new QAction(this); - action_create_empty_file_->setText(_("File")); + action_create_empty_file_->setText(tr("File")); connect(action_create_empty_file_, &QAction::triggered, this, &FileTreeView::SlotTouchBelowAtSelectedItem); action_compress_files_ = new QAction(this); - action_compress_files_->setText(_("Compress...")); + action_compress_files_->setText(tr("Compress...")); action_compress_files_->setVisible(false); connect(action_compress_files_, &QAction::triggered, this, &FileTreeView::slot_compress_files); auto* action_open_with_system_default_application = new QAction(this); action_open_with_system_default_application->setText( - _("Open with Default System Application")); + tr("Open with Default System Application")); connect(action_open_with_system_default_application, &QAction::triggered, this, &FileTreeView::SlotOpenSelectedItemBySystemApplication); auto* new_item_action_menu = new QMenu(this); - new_item_action_menu->setTitle(_("New")); + new_item_action_menu->setTitle(tr("New")); new_item_action_menu->addAction(action_create_empty_file_); new_item_action_menu->addAction(action_make_directory_); -- cgit v1.2.3 From f22ceca734868a4cb946c232f661aad72da01ded Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 19 Jan 2024 20:10:17 +0800 Subject: fix: slove discovered faults and bugs --- src/ui/widgets/FileTreeView.cpp | 118 +++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 67 deletions(-) (limited to 'src/ui/widgets/FileTreeView.cpp') diff --git a/src/ui/widgets/FileTreeView.cpp b/src/ui/widgets/FileTreeView.cpp index f3556dc9..41cd9aff 100644 --- a/src/ui/widgets/FileTreeView.cpp +++ b/src/ui/widgets/FileTreeView.cpp @@ -43,7 +43,7 @@ FileTreeView::FileTreeView(QWidget* parent, const QString& target_path) this->setModel(dir_model_); this->setColumnWidth(0, 320); this->sortByColumn(0, Qt::AscendingOrder); - current_path_ = std::filesystem::path(dir_model_->rootPath().toStdString()); + current_path_ = dir_model_->rootPath(); slot_create_popup_menu(); this->setContextMenuPolicy(Qt::CustomContextMenu); @@ -56,24 +56,27 @@ FileTreeView::FileTreeView(QWidget* parent, const QString& target_path) void FileTreeView::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) { QTreeView::selectionChanged(selected, deselected); - + GF_UI_LOG_DEBUG( + "file tree view selected changed, selected: {}, deselected: {}", + selected.size(), deselected.size()); if (!selected.indexes().empty()) { - selected_path_ = dir_model_->fileInfo(selected.indexes().first()) - .filesystemAbsoluteFilePath(); - GF_UI_LOG_DEBUG("file tree view selected target path: {}", - selected_path_.u8string()); - emit SignalSelectedChanged(QString::fromStdString(selected_path_)); + selected_path_ = dir_model_->filePath(selected.indexes().first()); + GF_UI_LOG_DEBUG("file tree view selected target path: {}", selected_path_); + emit SignalSelectedChanged(selected_path_); } else { - selected_path_ = std::filesystem::path{}; + selected_path_ = {}; + if (!this->selectedIndexes().isEmpty()) { + selected_path_ = dir_model_->filePath(this->selectedIndexes().front()); + emit SignalSelectedChanged(selected_path_); + } } } -void FileTreeView::SlotGoPath(const std::filesystem::path& target_path) { +void FileTreeView::SlotGoPath(const QString& target_path) { auto file_info = QFileInfo(target_path); if (file_info.isDir() && file_info.isReadable() && file_info.isExecutable()) { - current_path_ = file_info.filesystemAbsoluteFilePath(); - GF_UI_LOG_DEBUG("file tree view set target path: {}", - current_path_.u8string()); + current_path_ = file_info.absoluteFilePath(); + GF_UI_LOG_DEBUG("file tree view set target path: {}", current_path_); this->setRootIndex(dir_model_->index(file_info.filePath())); dir_model_->setRootPath(file_info.filePath()); for (int i = 1; i < dir_model_->columnCount(); ++i) { @@ -84,7 +87,7 @@ void FileTreeView::SlotGoPath(const std::filesystem::path& target_path) { this, tr("Error"), tr("The path is not exists, unprivileged or unreachable.")); } - emit SignalPathChanged(QString::fromStdString(current_path_.u8string())); + emit SignalPathChanged(current_path_); } void FileTreeView::slot_file_tree_view_item_double_clicked( @@ -98,58 +101,51 @@ void FileTreeView::slot_file_tree_view_item_double_clicked( tr("The file is unprivileged or unreachable.")); } } else { - SlotGoPath(file_info.filesystemAbsoluteFilePath()); + SlotGoPath(file_info.absoluteFilePath()); } } void FileTreeView::SlotUpLevel() { QModelIndex const current_root = this->rootIndex(); - auto target_path = - dir_model_->fileInfo(current_root).filesystemAbsoluteFilePath(); - if (target_path.has_parent_path() && !target_path.parent_path().empty()) { - target_path = target_path.parent_path(); - GF_UI_LOG_DEBUG("file tree view go parent path: {}", - target_path.u8string()); + auto target_path = dir_model_->fileInfo(current_root).absoluteFilePath(); + if (auto parent_path = QDir(target_path); parent_path.cdUp()) { + target_path = parent_path.absolutePath(); + GF_UI_LOG_DEBUG("file tree view go parent path: {}", target_path); this->SlotGoPath(target_path); } current_path_ = target_path; } -auto FileTreeView::GetCurrentPath() -> std::filesystem::path { - return current_path_; -} +auto FileTreeView::GetCurrentPath() -> QString { return current_path_; } void FileTreeView::SlotShowSystemFile(bool on) { auto filters = on ? dir_model_->filter() | QDir::System : dir_model_->filter() & ~QDir::System; dir_model_->setFilter(filters); - dir_model_->setRootPath(QString::fromStdString(current_path_.u8string())); + dir_model_->setRootPath(current_path_); } void FileTreeView::SlotShowHiddenFile(bool on) { auto filters = on ? dir_model_->filter() | QDir::Hidden : dir_model_->filter() & ~QDir::Hidden; dir_model_->setFilter(filters); - dir_model_->setRootPath(QString::fromStdString(current_path_.u8string())); + dir_model_->setRootPath(current_path_); } -auto FileTreeView::GetPathByClickPoint(const QPoint& point) - -> std::filesystem::path { +auto FileTreeView::GetPathByClickPoint(const QPoint& point) -> QString { auto const index = this->indexAt(point); if (!index.isValid()) { return {}; } - auto index_path = dir_model_->fileInfo(index).filesystemAbsoluteFilePath(); - GF_UI_LOG_DEBUG("file tree view right click on: {}", index_path.string()); + auto index_path = dir_model_->fileInfo(index).absoluteFilePath(); + GF_UI_LOG_DEBUG("file tree view right click on: {}", index_path); return index_path; } -auto FileTreeView::GetSelectedPath() -> std::filesystem::path { - return selected_path_; -} +auto FileTreeView::GetSelectedPath() -> QString { return selected_path_; } auto FileTreeView::SlotDeleteSelectedItem() -> void { QModelIndex const index = this->currentIndex(); @@ -224,7 +220,7 @@ void FileTreeView::SlotTouch() { } void FileTreeView::SlotTouchBelowAtSelectedItem() { - std::filesystem::path root_path(selected_path_); + auto root_path(selected_path_); QString new_file_name; bool ok; @@ -232,12 +228,9 @@ void FileTreeView::SlotTouchBelowAtSelectedItem() { this, tr("Create Empty File"), tr("Filename (you can given extension)"), QLineEdit::Normal, new_file_name, &ok); if (ok && !new_file_name.isEmpty()) { -#ifdef WINDOWS - auto file_path = root_path / new_file_name.toStdU16String(); -#else - auto file_path = root_path / new_file_name.toStdString(); -#endif - QFile new_file(file_path.u8string().c_str()); + auto file_path = root_path + "/" + new_file_name; + + QFile new_file(file_path); if (!new_file.open(QIODevice::WriteOnly | QIODevice::NewOnly)) { QMessageBox::critical(this, tr("Error"), tr("Unable to create the file.")); @@ -260,40 +253,33 @@ void FileTreeView::keyPressEvent(QKeyEvent* event) { } void FileTreeView::SlotOpenSelectedItemBySystemApplication() { - QFileInfo const info(QString::fromStdString(selected_path_.u8string())); - auto q_selected_path = QString::fromStdString(selected_path_.u8string()); + QFileInfo const info(selected_path_); if (info.isDir()) { const auto file_path = info.filePath().toUtf8().toStdString(); - QDesktopServices::openUrl(QUrl::fromLocalFile(q_selected_path)); + QDesktopServices::openUrl(QUrl::fromLocalFile(selected_path_)); } else { - QDesktopServices::openUrl(QUrl::fromLocalFile(q_selected_path)); + QDesktopServices::openUrl(QUrl::fromLocalFile(selected_path_)); } } void FileTreeView::SlotRenameSelectedItem() { bool ok; - auto text = QInputDialog::getText( - this, tr("Rename"), tr("New Filename"), QLineEdit::Normal, - QString::fromStdString(selected_path_.filename().u8string()), &ok); + auto text = QInputDialog::getText(this, tr("Rename"), tr("New Filename"), + QLineEdit::Normal, + QFileInfo(selected_path_).fileName(), &ok); if (ok && !text.isEmpty()) { - try { -#ifdef WINDOWS - auto new_name_path = selected_path_.parent_path() / text.toStdU16String(); -#else - auto new_name_path = selected_path_.parent_path() / text.toStdString(); -#endif - GF_UI_LOG_DEBUG("new name path: {}", new_name_path.u8string()); - std::filesystem::rename(selected_path_, new_name_path); - - // refresh - SlotGoPath(current_path_); - } catch (...) { - GF_UI_LOG_ERROR("file tree view rename error: {}", - selected_path_.u8string()); + auto file_info = QFileInfo(selected_path_); + auto new_name_path = file_info.absolutePath() + "/" + text; + GF_UI_LOG_DEBUG("new filename path: {}", new_name_path); + if (QDir().rename(file_info.absoluteFilePath(), new_name_path)) { QMessageBox::critical(this, tr("Error"), tr("Unable to rename the file or folder.")); + return; } + + // refresh + SlotGoPath(current_path_); } } @@ -306,9 +292,8 @@ 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(QString::fromStdString(GetSelectedPath())); - }); + connect(action_open_file_, &QAction::triggered, this, + [this](bool) { emit SignalOpenFile(GetSelectedPath()); }); action_rename_file_ = new QAction(this); action_rename_file_->setText(tr("Rename")); @@ -368,12 +353,12 @@ void FileTreeView::slot_create_popup_menu() { void FileTreeView::slot_show_custom_context_menu(const QPoint& point) { auto target_path = this->GetPathByClickPoint(point); - if (!target_path.empty()) { + if (!target_path.isEmpty()) { action_open_file_->setEnabled(true); action_rename_file_->setEnabled(true); action_delete_file_->setEnabled(true); - QFileInfo const info(QString::fromStdString(this->GetSelectedPath())); + QFileInfo const info(this->GetSelectedPath()); action_calculate_hash_->setEnabled(info.isFile() && info.isReadable()); } else { @@ -388,8 +373,7 @@ void FileTreeView::slot_show_custom_context_menu(const QPoint& point) { void FileTreeView::slot_calculate_hash() { emit UISignalStation::GetInstance()->SignalRefreshInfoBoard( - CalculateHash(this->GetSelectedPath().c_str()), - InfoBoardStatus::INFO_ERROR_OK); + CalculateHash(this->GetSelectedPath()), InfoBoardStatus::INFO_ERROR_OK); } void FileTreeView::slot_compress_files() {} -- cgit v1.2.3 From 0ff4345f4ce20be2f0d20a597da324d4f4855999 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 22 Jan 2024 22:01:34 +0800 Subject: fix: stop reading all data into memory at file hash calculation --- src/ui/widgets/FileTreeView.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src/ui/widgets/FileTreeView.cpp') diff --git a/src/ui/widgets/FileTreeView.cpp b/src/ui/widgets/FileTreeView.cpp index 41cd9aff..0c299a97 100644 --- a/src/ui/widgets/FileTreeView.cpp +++ b/src/ui/widgets/FileTreeView.cpp @@ -28,8 +28,10 @@ #include "FileTreeView.h" +#include "core/utils/AsyncUtils.h" #include "core/utils/IOUtils.h" #include "ui/UISignalStation.h" +#include "ui/UserInterfaceUtils.h" namespace GpgFrontend::UI { @@ -372,8 +374,24 @@ void FileTreeView::slot_show_custom_context_menu(const QPoint& point) { } void FileTreeView::slot_calculate_hash() { - emit UISignalStation::GetInstance()->SignalRefreshInfoBoard( - CalculateHash(this->GetSelectedPath()), InfoBoardStatus::INFO_ERROR_OK); + CommonUtils::WaitForOpera( + this->parentWidget(), tr("Calculating"), [=](const OperaWaitingHd& hd) { + RunOperaAsync( + [=](DataObjectPtr data_object) { + data_object->Swap({CalculateHash(this->GetSelectedPath())}); + return 0; + }, + [hd](int rtn, DataObjectPtr data_object) { + hd(); + if (rtn < 0 || !data_object->Check()) { + return; + } + auto result = ExtractParams(data_object, 0); + emit UISignalStation::GetInstance()->SignalRefreshInfoBoard( + result, InfoBoardStatus::INFO_ERROR_OK); + }, + "calculate_file_hash"); + }); } void FileTreeView::slot_compress_files() {} -- cgit v1.2.3