Skip to content

Commit be4cb8e

Browse files
committed
refactor: FilePage to use embedded FileTreeView
This change refactors the FilePage widget to directly embed a FileTreeView instance. It improves code structure by removing the separate layout management and simplifies signal connections. Also, changes some log level from Error to Debug.
1 parent cc600ea commit be4cb8e

File tree

6 files changed

+80
-74
lines changed

6 files changed

+80
-74
lines changed

src/core/function/gpg/GpgContext.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class GpgAgentProcess {
8888
args.append("--disable-scdaemon");
8989
}
9090

91-
LOG_E() << "gpg-agent start args: " << args << "channel:" << channel_;
91+
LOG_D() << "gpg-agent start args: " << args << "channel:" << channel_;
9292

9393
process_.setProgram(info.absoluteFilePath());
9494
process_.setArguments(args);
@@ -162,8 +162,8 @@ class GpgContext::Impl {
162162

163163
[[nodiscard]] auto Good() const -> bool { return good_; }
164164

165-
auto SetPassphraseCb(const gpgme_ctx_t &ctx,
166-
gpgme_passphrase_cb_t cb) -> bool {
165+
auto SetPassphraseCb(const gpgme_ctx_t &ctx, gpgme_passphrase_cb_t cb)
166+
-> bool {
167167
if (gpgme_get_pinentry_mode(ctx) != GPGME_PINENTRY_MODE_LOOPBACK) {
168168
if (CheckGpgError(gpgme_set_pinentry_mode(
169169
ctx, GPGME_PINENTRY_MODE_LOOPBACK)) != GPG_ERR_NO_ERROR) {
@@ -258,8 +258,8 @@ class GpgContext::Impl {
258258
return res == pass_size + 1 ? 0 : GPG_ERR_CANCELED;
259259
}
260260

261-
static auto TestStatusCb(void *hook, const char *keyword,
262-
const char *args) -> gpgme_error_t {
261+
static auto TestStatusCb(void *hook, const char *keyword, const char *args)
262+
-> gpgme_error_t {
263263
FLOG_D("keyword %s", keyword);
264264
return GPG_ERR_NO_ERROR;
265265
}
@@ -544,7 +544,7 @@ class GpgContext::Impl {
544544

545545
args.append({"--kill", "gpg-agent"});
546546

547-
LOG_E() << "gpgconf kill args: " << args
547+
LOG_D() << "gpgconf kill args: " << args
548548
<< "channel:" << parent_->GetChannel();
549549

550550
QProcess process;

src/ui/widgets/FilePage.cpp

+14-15
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,20 @@ namespace GpgFrontend::UI {
3737

3838
FilePage::FilePage(QWidget* parent, const QString& target_path)
3939
: QWidget(parent),
40-
ui_(GpgFrontend::SecureCreateSharedObject<Ui_FilePage>()),
41-
file_tree_view_(new FileTreeView(this, target_path)) {
40+
ui_(GpgFrontend::SecureCreateSharedObject<Ui_FilePage>()) {
4241
ui_->setupUi(this);
43-
ui_->trewViewLayout->addWidget(file_tree_view_);
4442

4543
ui_->batchModeButton->setToolTip(tr("Switch Batch Mode"));
4644

47-
connect(ui_->upPathButton, &QPushButton::clicked, file_tree_view_,
45+
connect(ui_->upPathButton, &QPushButton::clicked, ui_->treeView,
4846
&FileTreeView::SlotUpLevel);
4947
connect(ui_->refreshButton, &QPushButton::clicked, this,
5048
&FilePage::SlotGoPath);
51-
connect(this->ui_->newDirButton, &QPushButton::clicked, file_tree_view_,
49+
connect(this->ui_->newDirButton, &QPushButton::clicked, ui_->treeView,
5250
&FileTreeView::SlotMkdir);
5351

54-
ui_->pathEdit->setText(file_tree_view_->GetCurrentPath());
52+
ui_->treeView->SetPath(target_path);
53+
ui_->pathEdit->setText(ui_->treeView->GetCurrentPath());
5554

5655
path_edit_completer_ = new QCompleter(this);
5756
path_complete_model_ = new QStringListModel();
@@ -64,13 +63,13 @@ FilePage::FilePage(QWidget* parent, const QString& target_path)
6463
option_popup_menu_ = new QMenu(this);
6564
auto* show_hidden_act = new QAction(tr("Show Hidden File"), this);
6665
show_hidden_act->setCheckable(true);
67-
connect(show_hidden_act, &QAction::triggered, file_tree_view_,
66+
connect(show_hidden_act, &QAction::triggered, ui_->treeView,
6867
&FileTreeView::SlotShowHiddenFile);
6968
option_popup_menu_->addAction(show_hidden_act);
7069

7170
auto* show_system_act = new QAction(tr("Show System File"), this);
7271
show_system_act->setCheckable(true);
73-
connect(show_system_act, &QAction::triggered, file_tree_view_,
72+
connect(show_system_act, &QAction::triggered, ui_->treeView,
7473
&FileTreeView::SlotShowSystemFile);
7574
option_popup_menu_->addAction(show_system_act);
7675

@@ -105,30 +104,30 @@ FilePage::FilePage(QWidget* parent, const QString& target_path)
105104
connect(this, &FilePage::SignalRefreshInfoBoard,
106105
UISignalStation::GetInstance(),
107106
&UISignalStation::SignalRefreshInfoBoard);
108-
connect(file_tree_view_, &FileTreeView::SignalPathChanged, this,
107+
connect(ui_->treeView, &FileTreeView::SignalPathChanged, this,
109108
[this](const QString& path) { this->ui_->pathEdit->setText(path); });
110-
connect(file_tree_view_, &FileTreeView::SignalPathChanged, this,
109+
connect(ui_->treeView, &FileTreeView::SignalPathChanged, this,
111110
&FilePage::SignalPathChanged);
112-
connect(file_tree_view_, &FileTreeView::SignalOpenFile,
111+
connect(ui_->treeView, &FileTreeView::SignalOpenFile,
113112
UISignalStation::GetInstance(),
114113
&UISignalStation::SignalMainWindowOpenFile);
115-
connect(file_tree_view_, &FileTreeView::SignalSelectedChanged, this,
114+
connect(ui_->treeView, &FileTreeView::SignalSelectedChanged, this,
116115
&FilePage::update_main_basic_opera_menu);
117116
connect(this, &FilePage::SignalCurrentTabChanged, this,
118117
[this]() { update_main_basic_opera_menu(GetSelected()); });
119118
connect(this, &FilePage::SignalMainWindowUpdateBasicOperaMenu,
120119
UISignalStation::GetInstance(),
121120
&UISignalStation::SignalMainWindowUpdateBasicOperaMenu);
122-
connect(ui_->batchModeButton, &QToolButton::toggled, file_tree_view_,
121+
connect(ui_->batchModeButton, &QToolButton::toggled, ui_->treeView,
123122
&FileTreeView::SlotSwitchBatchMode);
124123
}
125124

126125
auto FilePage::GetSelected() const -> QStringList {
127-
return file_tree_view_->GetSelectedPaths();
126+
return ui_->treeView->GetSelectedPaths();
128127
}
129128

130129
void FilePage::SlotGoPath() {
131-
file_tree_view_->SlotGoPath(ui_->pathEdit->text());
130+
ui_->treeView->SlotGoPath(ui_->pathEdit->text());
132131
}
133132

134133
void FilePage::keyPressEvent(QKeyEvent* event) {

src/ui/widgets/FilePage.h

-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#pragma once
3030

3131
#include "ui/GpgFrontendUI.h"
32-
#include "ui/widgets/FileTreeView.h"
3332
#include "ui/widgets/InfoBoardWidget.h"
3433

3534
class Ui_FilePage;
@@ -126,7 +125,6 @@ class FilePage : public QWidget {
126125

127126
QMenu* popup_menu_{}; ///<
128127
QMenu* option_popup_menu_{}; ///<
129-
FileTreeView* file_tree_view_;
130128
bool ascii_mode_;
131129

132130
private slots:

src/ui/widgets/FileTreeView.cpp

+41-41
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
namespace GpgFrontend::UI {
3737

38-
FileTreeView::FileTreeView(QWidget* parent, const QString& target_path)
38+
FileTreeView::FileTreeView(QWidget* parent)
3939
: QTreeView(parent), dir_model_(new QFileSystemModel(this)) {
4040
dir_model_->setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot);
4141
dir_model_->setRootPath(QDir::homePath());
@@ -58,43 +58,6 @@ FileTreeView::FileTreeView(QWidget* parent, const QString& target_path)
5858
&FileTreeView::slot_adjust_column_widths);
5959
connect(dir_model_, &QFileSystemModel::dataChanged, this,
6060
&FileTreeView::slot_adjust_column_widths);
61-
62-
LOG_D() << "try to open target path:" << target_path;
63-
64-
QFileInfo info(target_path);
65-
QString effective_path;
66-
67-
if (info.exists()) {
68-
effective_path =
69-
info.isFile() ? info.absolutePath() : info.absoluteFilePath();
70-
} else {
71-
effective_path = QDir::currentPath();
72-
}
73-
74-
LOG_D() << "effective path:" << effective_path;
75-
76-
dir_model_->setRootPath(effective_path);
77-
current_path_ = dir_model_->rootPath();
78-
QModelIndex root_index = dir_model_->index(current_path_);
79-
80-
if (root_index.isValid()) {
81-
QPointer<FileTreeView> self(this);
82-
this->setRootIndex(root_index);
83-
QTimer::singleShot(200, [=]() {
84-
if (self != nullptr && info.isFile()) {
85-
self->setCurrentIndex(dir_model_->index(info.absoluteFilePath()));
86-
self->expand(currentIndex().parent());
87-
self->scrollTo(currentIndex(), QAbstractItemView::PositionAtCenter);
88-
self->selectionModel()->select(
89-
currentIndex(),
90-
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
91-
}
92-
});
93-
} else {
94-
LOG_W() << "invalid path, fallback to current dir.";
95-
current_path_ = QDir::currentPath();
96-
this->setRootIndex(dir_model_->index(current_path_));
97-
}
9861
}
9962

10063
void FileTreeView::selectionChanged(const QItemSelection& selected,
@@ -446,9 +409,8 @@ void FileTreeView::slot_calculate_hash() {
446409
return;
447410
}
448411
auto result = ExtractParams<QString>(data_object, 0);
449-
emit UISignalStation::GetInstance()
450-
-> SignalRefreshInfoBoard(result,
451-
InfoBoardStatus::INFO_ERROR_OK);
412+
emit UISignalStation::GetInstance() -> SignalRefreshInfoBoard(
413+
result, InfoBoardStatus::INFO_ERROR_OK);
452414
},
453415
"calculate_file_hash");
454416
});
@@ -481,4 +443,42 @@ void FileTreeView::SlotSwitchBatchMode(bool batch) {
481443
selectionModel()->clearSelection();
482444
}
483445

446+
void FileTreeView::SetPath(const QString& target_path) {
447+
LOG_D() << "try to open target path:" << target_path;
448+
449+
QFileInfo info(target_path);
450+
QString effective_path;
451+
452+
if (info.exists()) {
453+
effective_path =
454+
info.isFile() ? info.absolutePath() : info.absoluteFilePath();
455+
} else {
456+
effective_path = QDir::currentPath();
457+
}
458+
459+
LOG_D() << "effective path:" << effective_path;
460+
461+
dir_model_->setRootPath(effective_path);
462+
current_path_ = dir_model_->rootPath();
463+
QModelIndex root_index = dir_model_->index(current_path_);
464+
465+
if (root_index.isValid()) {
466+
QPointer<FileTreeView> self(this);
467+
this->setRootIndex(root_index);
468+
QTimer::singleShot(200, [=]() {
469+
if (self != nullptr && info.isFile()) {
470+
self->setCurrentIndex(dir_model_->index(info.absoluteFilePath()));
471+
self->expand(currentIndex().parent());
472+
self->scrollTo(currentIndex(), QAbstractItemView::PositionAtCenter);
473+
self->selectionModel()->select(
474+
currentIndex(),
475+
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
476+
}
477+
});
478+
} else {
479+
LOG_W() << "invalid path, fallback to current dir.";
480+
current_path_ = QDir::currentPath();
481+
this->setRootIndex(dir_model_->index(current_path_));
482+
}
483+
}
484484
} // namespace GpgFrontend::UI

src/ui/widgets/FileTreeView.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ class FileTreeView : public QTreeView {
4141
* @param parent
4242
* @param target_path
4343
*/
44-
explicit FileTreeView(QWidget* parent, const QString& target_path);
44+
explicit FileTreeView(QWidget* parent);
45+
46+
/**
47+
*/
48+
void SetPath(const QString& target_path);
4549

4650
/**
4751
* @brief Get the Current Path object

ui/FilePage.ui

+14-9
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>858</width>
10-
<height>64</height>
9+
<width>805</width>
10+
<height>666</height>
1111
</rect>
1212
</property>
1313
<property name="sizePolicy">
@@ -16,12 +16,6 @@
1616
<verstretch>0</verstretch>
1717
</sizepolicy>
1818
</property>
19-
<property name="maximumSize">
20-
<size>
21-
<width>1041</width>
22-
<height>619</height>
23-
</size>
24-
</property>
2519
<property name="windowTitle">
2620
<string>Form</string>
2721
</property>
@@ -190,14 +184,25 @@
190184
</layout>
191185
</item>
192186
<item>
193-
<layout class="QVBoxLayout" name="trewViewLayout"/>
187+
<layout class="QVBoxLayout" name="trewViewLayout">
188+
<item>
189+
<widget class="GpgFrontend::UI::FileTreeView" name="treeView"/>
190+
</item>
191+
</layout>
194192
</item>
195193
</layout>
196194
</item>
197195
</layout>
198196
</item>
199197
</layout>
200198
</widget>
199+
<customwidgets>
200+
<customwidget>
201+
<class>GpgFrontend::UI::FileTreeView</class>
202+
<extends>QTreeView</extends>
203+
<header>ui/widgets/FileTreeView.h</header>
204+
</customwidget>
205+
</customwidgets>
201206
<resources>
202207
<include location="../gpgfrontend.qrc"/>
203208
</resources>

0 commit comments

Comments
 (0)