From d1a9afa7cd965d09a6dfae5a1c90e4fd3ff01307 Mon Sep 17 00:00:00 2001 From: DexesTTP Date: Tue, 5 May 2020 13:58:31 +0200 Subject: [PATCH] Help: Clicking on link changes the treeview selection Fixes #1259 --- Applications/Help/ManualModel.cpp | 18 ++++++++++++++++++ Applications/Help/ManualModel.h | 3 +++ Applications/Help/main.cpp | 6 ++++++ 3 files changed, 27 insertions(+) diff --git a/Applications/Help/ManualModel.cpp b/Applications/Help/ManualModel.cpp index 0188dea1db8..477e5e52c21 100644 --- a/Applications/Help/ManualModel.cpp +++ b/Applications/Help/ManualModel.cpp @@ -47,6 +47,24 @@ ManualModel::ManualModel() m_page_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-unknown.png")); } +Optional ManualModel::index_from_path(const StringView& path) const +{ + for (int section = 0; section < row_count(); ++section) { + auto parent_index = index(section, 0); + for (int row = 0; row < row_count(parent_index); ++row) { + auto child_index = index(row, 0, parent_index); + auto* node = static_cast(child_index.internal_data()); + if (!node->is_page()) + continue; + auto* page = static_cast(node); + if (page->path() != path) + continue; + return child_index; + } + } + return {}; +} + String ManualModel::page_path(const GUI::ModelIndex& index) const { if (!index.is_valid()) diff --git a/Applications/Help/ManualModel.h b/Applications/Help/ManualModel.h index 2a23f5d4a1e..aeac63037fd 100644 --- a/Applications/Help/ManualModel.h +++ b/Applications/Help/ManualModel.h @@ -27,6 +27,7 @@ #pragma once #include +#include #include #include @@ -39,6 +40,8 @@ public: virtual ~ManualModel() override {}; + Optional index_from_path(const StringView&) const; + String page_path(const GUI::ModelIndex&) const; String page_and_section(const GUI::ModelIndex&) const; diff --git a/Applications/Help/main.cpp b/Applications/Help/main.cpp index 057faa17b12..5178e191dfc 100644 --- a/Applications/Help/main.cpp +++ b/Applications/Help/main.cpp @@ -156,6 +156,12 @@ int main(int argc, char* argv[]) char* dir_path = dirname(current_path); char* path = realpath(String::format("%s/%s", dir_path, href.characters()).characters(), nullptr); free(current_path); + auto tree_view_index = model->index_from_path(path); + if (tree_view_index.has_value()) { + dbg() << "Found path _" << path << "_ in model at index " << tree_view_index.value(); + tree_view.selection().set(tree_view_index.value()); + return; + } history.push(path); update_actions(); open_page(path);