From ee6b379972a936e5e9181ca925584762f625c2c9 Mon Sep 17 00:00:00 2001 From: nguyenvukhang Date: Sun, 5 Feb 2023 19:49:45 +0800 Subject: [PATCH] refactor: more into TreeIndex --- app/clickable_tree_view.cc | 4 ++-- app/mainwindow.cc | 6 +++--- app/tree_model.h | 20 ++++++++++---------- test/main.cc | 35 +++++++++++++++++------------------ 4 files changed, 32 insertions(+), 33 deletions(-) diff --git a/app/clickable_tree_view.cc b/app/clickable_tree_view.cc index ce48fcf..5cd8fd6 100644 --- a/app/clickable_tree_view.cc +++ b/app/clickable_tree_view.cc @@ -10,7 +10,7 @@ void ClickableTreeView::context_menu(const QPoint &pos) if (index.column() == TreeItem::REMOTE_DIR) { menu.addAction("Track Folder"); } - if (!TreeIndex(index).get_local_dir().isEmpty()) { + if (!TreeIndex(index).local_dir().isEmpty()) { menu.addAction("Clear"); } auto e = menu.exec(mapToGlobal(pos)); @@ -55,7 +55,7 @@ bool expand_tracked_inner(ClickableTreeView *tv, const QModelIndex &index) QModelIndex child = model->index(i, 0, index); expand |= expand_tracked_inner(tv, child); } - if (!TreeIndex(index).get_local_dir().isEmpty() || expand) { + if (!TreeIndex(index).local_dir().isEmpty() || expand) { tv->expand(index.parent()); return true; } diff --git a/app/mainwindow.cc b/app/mainwindow.cc index 6e03dff..47f5825 100644 --- a/app/mainwindow.cc +++ b/app/mainwindow.cc @@ -182,7 +182,7 @@ void MainWindow::treeView_cleared(const QModelIndex &index) { ui->treeView->model()->itemFromIndex(index)->setData(TreeItem::LOCAL_DIR, ""); ui->guideText->setHidden(!this->gather_tracked().empty()); - QString folder_id = TreeIndex(index).get_id(); + QString folder_id = TreeIndex(index).id(); if (folder_id.isEmpty()) return; settings.remove(folder_id); settings.sync(); @@ -191,7 +191,7 @@ void MainWindow::treeView_cleared(const QModelIndex &index) void MainWindow::treeView_trackFolder(const QModelIndex &index) { if (!index.parent().isValid()) return; - TreeIndex ti(index); + TreeIndex ti = index; QFileDialog dialog(this, "Target for " + ti.get_ancestry("/"), start_dir); int result = dialog.exec(); @@ -223,7 +223,7 @@ void MainWindow::treeView_trackFolder(const QModelIndex &index) // update itself item->setData(TreeItem::LOCAL_DIR, local_dir); - settings.setValue(ti.get_id(), local_dir); + settings.setValue(ti.id(), local_dir); settings.sync(); ui->guideText->hide(); diff --git a/app/tree_model.h b/app/tree_model.h index 7999f0c..6edbe0d 100644 --- a/app/tree_model.h +++ b/app/tree_model.h @@ -113,32 +113,32 @@ class TreeModel : public QAbstractItemModel class TreeIndex : public QModelIndex { - QString data(int i) { return this->siblingAtColumn(i).data().toString(); } + QString data(int i) const { return siblingAtColumn(i).data().toString(); } public: TreeIndex(const QModelIndex i) : QModelIndex(i){}; const QModelIndex index() { return this->index(); } - QString get_id() { return data(TreeItem::FOLDER_ID); } - QString get_local_dir() { return data(TreeItem::LOCAL_DIR); } - QString get_remote_dir() { return data(TreeItem::REMOTE_DIR); } + QString id() const { return data(TreeItem::FOLDER_ID); } + QString local_dir() const { return data(TreeItem::LOCAL_DIR); } + QString remote_dir() const { return data(TreeItem::REMOTE_DIR); } - QString get_course() + QString course() const { - TreeIndex *a = this; + const TreeIndex *a = this; while (a->parent().isValid()) a = new TreeIndex(a->parent()); - return a->get_remote_dir(); + return a->remote_dir(); } QString get_ancestry(const char *delimiter) { QString path; TreeIndex *a = this; - path = this->get_remote_dir(); + path = this->remote_dir(); while (a->parent().isValid()) { a = new TreeIndex(a->parent()); - QString d = a->get_remote_dir(); + QString d = a->remote_dir(); if (d.size() > 10) { d.truncate(10); d.push_back("..."); @@ -148,7 +148,7 @@ class TreeIndex : public QModelIndex return path; } - const TreeIndex get_child(const int index) + const TreeIndex child(const int index) { return TreeIndex(model()->index(index, 0, this->index())); } diff --git a/test/main.cc b/test/main.cc index a85f5a1..88e948c 100644 --- a/test/main.cc +++ b/test/main.cc @@ -113,27 +113,26 @@ void TestGui::fetch_courses_test() TreeModel *model = app.ui->treeView->model(); QCOMPARE(model->childrenCount(), 3); - QModelIndex ptr; + TreeIndex ptr = model->index(0, 0); - ptr = model->index(0, 0); - QCOMPARE(get_course(ptr), "Calculus"); - ptr = get_child(ptr, 0); - QCOMPARE(get_remote_dir(ptr), "course files"); - QCOMPARE(get_remote_dir(get_child(ptr, 0)), "Admin"); - QCOMPARE(get_remote_dir(get_child(ptr, 1)), "Tutorials"); + QCOMPARE(ptr.course(), "Calculus"); + ptr = ptr.child(0); + QCOMPARE(ptr.remote_dir(), "course files"); + QCOMPARE(ptr.child(0).remote_dir(), "Admin"); + QCOMPARE(ptr.child(1).remote_dir(), "Tutorials"); ptr = model->index(1, 0); - QCOMPARE(get_course(ptr), "Geometry"); - ptr = get_child(ptr, 0); - QCOMPARE(get_remote_dir(ptr), "course files"); - QCOMPARE(get_remote_dir(get_child(ptr, 0)), "Course notes"); - QCOMPARE(get_remote_dir(get_child(ptr, 1)), "Lectures"); - QCOMPARE(get_remote_dir(get_child(ptr, 2)), "Tutorials"); + QCOMPARE(ptr.course(), "Geometry"); + ptr = ptr.child(0); + QCOMPARE(ptr.remote_dir(), "course files"); + QCOMPARE(ptr.child(0).remote_dir(), "Course ntoes"); + QCOMPARE(ptr.child(1).remote_dir(), "Lectures"); + QCOMPARE(ptr.child(2).remote_dir(), "Tutorials"); ptr = model->index(2, 0); - QCOMPARE(get_course(ptr), "Linear Algebra"); - ptr = get_child(ptr, 0); - QCOMPARE(get_remote_dir(ptr), "course files"); - QCOMPARE(get_remote_dir(get_child(ptr, 0)), "Lectures"); - QCOMPARE(get_remote_dir(get_child(ptr, 1)), "Tutorials"); + QCOMPARE(ptr.course(), "Linear Algebra"); + ptr = ptr.child(0); + QCOMPARE(ptr.remote_dir(), "course files"); + QCOMPARE(ptr.child(0).remote_dir(), "Lectures"); + QCOMPARE(ptr.child(1).remote_dir(), "Tutorials"); }