diff options
Diffstat (limited to 'src/ui/widgets')
-rw-r--r-- | src/ui/widgets/FilePage.cpp | 29 | ||||
-rw-r--r-- | src/ui/widgets/FilePage.h | 2 | ||||
-rw-r--r-- | src/ui/widgets/FileTreeView.cpp | 82 | ||||
-rw-r--r-- | src/ui/widgets/FileTreeView.h | 6 |
4 files changed, 60 insertions, 59 deletions
diff --git a/src/ui/widgets/FilePage.cpp b/src/ui/widgets/FilePage.cpp index 95dbfac9..59a3c9ed 100644 --- a/src/ui/widgets/FilePage.cpp +++ b/src/ui/widgets/FilePage.cpp @@ -37,21 +37,20 @@ namespace GpgFrontend::UI { FilePage::FilePage(QWidget* parent, const QString& target_path) : QWidget(parent), - ui_(GpgFrontend::SecureCreateSharedObject<Ui_FilePage>()), - file_tree_view_(new FileTreeView(this, target_path)) { + ui_(GpgFrontend::SecureCreateSharedObject<Ui_FilePage>()) { ui_->setupUi(this); - ui_->trewViewLayout->addWidget(file_tree_view_); ui_->batchModeButton->setToolTip(tr("Switch Batch Mode")); - connect(ui_->upPathButton, &QPushButton::clicked, file_tree_view_, + connect(ui_->upPathButton, &QPushButton::clicked, ui_->treeView, &FileTreeView::SlotUpLevel); connect(ui_->refreshButton, &QPushButton::clicked, this, &FilePage::SlotGoPath); - connect(this->ui_->newDirButton, &QPushButton::clicked, file_tree_view_, + connect(this->ui_->newDirButton, &QPushButton::clicked, ui_->treeView, &FileTreeView::SlotMkdir); - ui_->pathEdit->setText(file_tree_view_->GetCurrentPath()); + ui_->treeView->SetPath(target_path); + ui_->pathEdit->setText(ui_->treeView->GetCurrentPath()); path_edit_completer_ = new QCompleter(this); path_complete_model_ = new QStringListModel(); @@ -64,13 +63,13 @@ FilePage::FilePage(QWidget* parent, const QString& target_path) option_popup_menu_ = new QMenu(this); auto* show_hidden_act = new QAction(tr("Show Hidden File"), this); show_hidden_act->setCheckable(true); - connect(show_hidden_act, &QAction::triggered, file_tree_view_, + connect(show_hidden_act, &QAction::triggered, ui_->treeView, &FileTreeView::SlotShowHiddenFile); option_popup_menu_->addAction(show_hidden_act); auto* show_system_act = new QAction(tr("Show System File"), this); show_system_act->setCheckable(true); - connect(show_system_act, &QAction::triggered, file_tree_view_, + connect(show_system_act, &QAction::triggered, ui_->treeView, &FileTreeView::SlotShowSystemFile); option_popup_menu_->addAction(show_system_act); @@ -105,30 +104,30 @@ FilePage::FilePage(QWidget* parent, const QString& target_path) connect(this, &FilePage::SignalRefreshInfoBoard, UISignalStation::GetInstance(), &UISignalStation::SignalRefreshInfoBoard); - connect(file_tree_view_, &FileTreeView::SignalPathChanged, this, + connect(ui_->treeView, &FileTreeView::SignalPathChanged, this, [this](const QString& path) { this->ui_->pathEdit->setText(path); }); - connect(file_tree_view_, &FileTreeView::SignalPathChanged, this, + connect(ui_->treeView, &FileTreeView::SignalPathChanged, this, &FilePage::SignalPathChanged); - connect(file_tree_view_, &FileTreeView::SignalOpenFile, + connect(ui_->treeView, &FileTreeView::SignalOpenFile, UISignalStation::GetInstance(), &UISignalStation::SignalMainWindowOpenFile); - connect(file_tree_view_, &FileTreeView::SignalSelectedChanged, this, + connect(ui_->treeView, &FileTreeView::SignalSelectedChanged, this, &FilePage::update_main_basic_opera_menu); connect(this, &FilePage::SignalCurrentTabChanged, this, [this]() { update_main_basic_opera_menu(GetSelected()); }); connect(this, &FilePage::SignalMainWindowUpdateBasicOperaMenu, UISignalStation::GetInstance(), &UISignalStation::SignalMainWindowUpdateBasicOperaMenu); - connect(ui_->batchModeButton, &QToolButton::toggled, file_tree_view_, + connect(ui_->batchModeButton, &QToolButton::toggled, ui_->treeView, &FileTreeView::SlotSwitchBatchMode); } auto FilePage::GetSelected() const -> QStringList { - return file_tree_view_->GetSelectedPaths(); + return ui_->treeView->GetSelectedPaths(); } void FilePage::SlotGoPath() { - file_tree_view_->SlotGoPath(ui_->pathEdit->text()); + ui_->treeView->SlotGoPath(ui_->pathEdit->text()); } void FilePage::keyPressEvent(QKeyEvent* event) { diff --git a/src/ui/widgets/FilePage.h b/src/ui/widgets/FilePage.h index fbf99ae9..94b90b0d 100644 --- a/src/ui/widgets/FilePage.h +++ b/src/ui/widgets/FilePage.h @@ -29,7 +29,6 @@ #pragma once #include "ui/GpgFrontendUI.h" -#include "ui/widgets/FileTreeView.h" #include "ui/widgets/InfoBoardWidget.h" class Ui_FilePage; @@ -126,7 +125,6 @@ class FilePage : public QWidget { QMenu* popup_menu_{}; ///< QMenu* option_popup_menu_{}; ///< - FileTreeView* file_tree_view_; bool ascii_mode_; private slots: diff --git a/src/ui/widgets/FileTreeView.cpp b/src/ui/widgets/FileTreeView.cpp index 8ebd7274..db628d60 100644 --- a/src/ui/widgets/FileTreeView.cpp +++ b/src/ui/widgets/FileTreeView.cpp @@ -35,7 +35,7 @@ namespace GpgFrontend::UI { -FileTreeView::FileTreeView(QWidget* parent, const QString& target_path) +FileTreeView::FileTreeView(QWidget* parent) : QTreeView(parent), dir_model_(new QFileSystemModel(this)) { dir_model_->setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot); dir_model_->setRootPath(QDir::homePath()); @@ -58,43 +58,6 @@ FileTreeView::FileTreeView(QWidget* parent, const QString& target_path) &FileTreeView::slot_adjust_column_widths); connect(dir_model_, &QFileSystemModel::dataChanged, this, &FileTreeView::slot_adjust_column_widths); - - LOG_D() << "try to open target path:" << target_path; - - QFileInfo info(target_path); - QString effective_path; - - if (info.exists()) { - effective_path = - info.isFile() ? info.absolutePath() : info.absoluteFilePath(); - } else { - effective_path = QDir::currentPath(); - } - - LOG_D() << "effective path:" << effective_path; - - dir_model_->setRootPath(effective_path); - current_path_ = dir_model_->rootPath(); - QModelIndex root_index = dir_model_->index(current_path_); - - if (root_index.isValid()) { - QPointer<FileTreeView> self(this); - this->setRootIndex(root_index); - QTimer::singleShot(200, [=]() { - if (self != nullptr && info.isFile()) { - self->setCurrentIndex(dir_model_->index(info.absoluteFilePath())); - self->expand(currentIndex().parent()); - self->scrollTo(currentIndex(), QAbstractItemView::PositionAtCenter); - self->selectionModel()->select( - currentIndex(), - QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); - } - }); - } else { - LOG_W() << "invalid path, fallback to current dir."; - current_path_ = QDir::currentPath(); - this->setRootIndex(dir_model_->index(current_path_)); - } } void FileTreeView::selectionChanged(const QItemSelection& selected, @@ -446,9 +409,8 @@ void FileTreeView::slot_calculate_hash() { return; } auto result = ExtractParams<QString>(data_object, 0); - emit UISignalStation::GetInstance() - -> SignalRefreshInfoBoard(result, - InfoBoardStatus::INFO_ERROR_OK); + emit UISignalStation::GetInstance() -> SignalRefreshInfoBoard( + result, InfoBoardStatus::INFO_ERROR_OK); }, "calculate_file_hash"); }); @@ -481,4 +443,42 @@ void FileTreeView::SlotSwitchBatchMode(bool batch) { selectionModel()->clearSelection(); } +void FileTreeView::SetPath(const QString& target_path) { + LOG_D() << "try to open target path:" << target_path; + + QFileInfo info(target_path); + QString effective_path; + + if (info.exists()) { + effective_path = + info.isFile() ? info.absolutePath() : info.absoluteFilePath(); + } else { + effective_path = QDir::currentPath(); + } + + LOG_D() << "effective path:" << effective_path; + + dir_model_->setRootPath(effective_path); + current_path_ = dir_model_->rootPath(); + QModelIndex root_index = dir_model_->index(current_path_); + + if (root_index.isValid()) { + QPointer<FileTreeView> self(this); + this->setRootIndex(root_index); + QTimer::singleShot(200, [=]() { + if (self != nullptr && info.isFile()) { + self->setCurrentIndex(dir_model_->index(info.absoluteFilePath())); + self->expand(currentIndex().parent()); + self->scrollTo(currentIndex(), QAbstractItemView::PositionAtCenter); + self->selectionModel()->select( + currentIndex(), + QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); + } + }); + } else { + LOG_W() << "invalid path, fallback to current dir."; + current_path_ = QDir::currentPath(); + this->setRootIndex(dir_model_->index(current_path_)); + } +} } // namespace GpgFrontend::UI diff --git a/src/ui/widgets/FileTreeView.h b/src/ui/widgets/FileTreeView.h index 333e06bc..e7ddb607 100644 --- a/src/ui/widgets/FileTreeView.h +++ b/src/ui/widgets/FileTreeView.h @@ -41,7 +41,11 @@ class FileTreeView : public QTreeView { * @param parent * @param target_path */ - explicit FileTreeView(QWidget* parent, const QString& target_path); + explicit FileTreeView(QWidget* parent); + + /** + */ + void SetPath(const QString& target_path); /** * @brief Get the Current Path object |