Skip to content

Commit

Permalink
refactor: remove headless functions on QModelIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenvukhang committed Feb 5, 2023
1 parent 26ec287 commit da25257
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 75 deletions.
13 changes: 5 additions & 8 deletions app/clickable_tree_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
void ClickableTreeView::context_menu(const QPoint &pos)
{
const QModelIndex index = indexAt(pos);
if (!index.isValid() || !index.parent().isValid())
return;
if (!index.isValid() || !index.parent().isValid()) return;

// create menu
QMenu menu;
if (index.column() == TreeItem::REMOTE_DIR) {
menu.addAction("Track Folder");
}
if (!get_local_dir(index).isEmpty()) {
if (!TreeIndex(index).get_local_dir().isEmpty()) {
menu.addAction("Clear");
}
auto e = menu.exec(mapToGlobal(pos));
if (e == nullptr)
return;
if (e == nullptr) return;

// get selected item
QString target = e->text();
Expand Down Expand Up @@ -49,16 +47,15 @@ void ClickableTreeView::setModel(TreeModel *model)

bool expand_tracked_inner(ClickableTreeView *tv, const QModelIndex &index)
{
if (!index.isValid())
return false;
if (!index.isValid()) return false;
TreeModel *model = tv->model();
bool expand = false;
int n = model->childrenCount();
for (int i = 0; i < n; i++) {
QModelIndex child = model->index(i, 0, index);
expand |= expand_tracked_inner(tv, child);
}
if (!get_local_dir(index).isEmpty() || expand) {
if (!TreeIndex(index).get_local_dir().isEmpty() || expand) {
tv->expand(index.parent());
return true;
}
Expand Down
8 changes: 4 additions & 4 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 = get_id(index);
QString folder_id = TreeIndex(index).get_id();
if (folder_id.isEmpty()) return;
settings.remove(folder_id);
settings.sync();
Expand All @@ -191,9 +191,9 @@ void MainWindow::treeView_cleared(const QModelIndex &index)
void MainWindow::treeView_trackFolder(const QModelIndex &index)
{
if (!index.parent().isValid()) return;
TreeIndex ti(index);

QFileDialog dialog(this, "Target for " + get_ancestry(index, " / "),
this->start_dir);
QFileDialog dialog(this, "Target for " + ti.get_ancestry("/"), start_dir);
int result = dialog.exec();
grabKeyboard();

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(get_id(index), local_dir);
settings.setValue(ti.get_id(), local_dir);
settings.sync();

ui->guideText->hide();
Expand Down
55 changes: 0 additions & 55 deletions app/tree_model.cc
Original file line number Diff line number Diff line change
@@ -1,60 +1,5 @@
#include "tree_model.h"

QString get_id(const QModelIndex &index)
{
return index.siblingAtColumn(TreeItem::FOLDER_ID).data().toString();
}

QString get_local_dir(const QModelIndex &index)
{
return index.siblingAtColumn(TreeItem::LOCAL_DIR).data().toString();
}

QString get_remote_dir(const QModelIndex &index)
{
return index.siblingAtColumn(TreeItem::REMOTE_DIR).data().toString();
}

QString get_course(const QModelIndex &index)
{
QModelIndex a = index;
while (a.parent().isValid()) {
a = a.parent();
}
return get_remote_dir(a);
}

QString get_ancestry(const QModelIndex &index, const char *delimiter)
{
QString path;
QModelIndex a = index;
path = get_remote_dir(index);
while (a.parent().isValid()) {
a = a.parent();
QString d = get_remote_dir(a);
if (d.size() > 10) {
d.truncate(10);
d.push_back("...");
}
path = d + delimiter + path;
}
return path;
}

const QModelIndex get_child(const QModelIndex &index, const int i)
{
return index.model()->index(i, 0, index);
}

const int count_children(const QModelIndex &index)
{
int i = 0;
const QAbstractItemModel *m = index.model();
while (m->index(i, 0, index).isValid())
i++;
return i;
}

QVector<QVariant> stringListToVariantList(const QStringList &data)
{
QVector<QVariant> variantData;
Expand Down
60 changes: 52 additions & 8 deletions app/tree_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@

#include "filetree.h"

QString get_id(const QModelIndex &);
QString get_local_dir(const QModelIndex &);
QString get_remote_dir(const QModelIndex &);
QString get_course(const QModelIndex &);
QString get_ancestry(const QModelIndex &, const char *delimiter);
const QModelIndex get_child(const QModelIndex &, const int index);
const int count_children(const QModelIndex &);

class TreeItem
{
public:
Expand Down Expand Up @@ -119,4 +111,56 @@ class TreeModel : public QAbstractItemModel
bool readOnly;
};

class TreeIndex : public QModelIndex
{
QString data(int i) { return this->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 get_course()
{
TreeIndex *a = this;
while (a->parent().isValid())
a = new TreeIndex(a->parent());
return a->get_remote_dir();
}

QString get_ancestry(const char *delimiter)
{
QString path;
TreeIndex *a = this;
path = this->get_remote_dir();
while (a->parent().isValid()) {
a = new TreeIndex(a->parent());
QString d = a->get_remote_dir();
if (d.size() > 10) {
d.truncate(10);
d.push_back("...");
}
path = d + delimiter + path;
}
return path;
}

const TreeIndex get_child(const int index)
{
return TreeIndex(model()->index(index, 0, this->index()));
}

const int children_count()
{
int i = 0;
const QAbstractItemModel *m = this->model();
while (m->index(i, 0, this->index()).isValid())
i++;
return i;
}
};

#endif // TREEMODEL_H

0 comments on commit da25257

Please sign in to comment.