Skip to content

Commit

Permalink
refactor: more into TreeIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenvukhang committed Feb 5, 2023
1 parent da25257 commit ee6b379
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
4 changes: 2 additions & 2 deletions app/clickable_tree_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions app/mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
20 changes: 10 additions & 10 deletions app/tree_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -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("...");
Expand All @@ -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()));
}
Expand Down
35 changes: 17 additions & 18 deletions test/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit ee6b379

Please sign in to comment.