Skip to content

Commit

Permalink
Help Browser: implemented viewing of sub-sections/topics
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz authored and CelticMinstrel committed Apr 21, 2017
1 parent 6cc95dd commit f3da8fc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
9 changes: 8 additions & 1 deletion data/gui/window/help_browser.cfg
Expand Up @@ -28,6 +28,7 @@
[row]

[column]
grow_factor = 0
border = "all"
border_size = 5

Expand All @@ -38,16 +39,20 @@
[/column]

[column]
grow_factor = 1
border = "all"
border_size = 5
horizontal_grow = true

[label]
id = "topic_name"
linked_group = "names"
definition = "default_small"
[/label]
[/column]

[column]
grow_factor = 0
border = "all"
border_size = 5

Expand Down Expand Up @@ -128,9 +133,9 @@
[grid]

[row]
grow_factor = 0

[column]
grow_factor = 0
border = "all"
border_size = 5
horizontal_grow = true
Expand All @@ -141,6 +146,8 @@

[column]
grow_factor = 1
horizontal_grow = true
vertical_grow = true

[multi_page]
id = "topic_text_pages"
Expand Down
56 changes: 41 additions & 15 deletions src/gui/dialogs/help_browser.cpp
Expand Up @@ -61,33 +61,44 @@ void help_browser::pre_show(window& window)

window.keyboard_capture(&topic_tree);

for(const help::section* section : help::default_toplevel.sections) {
add_topic(window, section->id, section->title, true);
}
add_topics_for_section(help::default_toplevel, topic_tree.get_root_node());

on_topic_select(window);
}

void help_browser::add_topics_for_section(const help::section& parent_section, tree_view_node& parent_node)
{
for(const help::section* section : parent_section.sections) {
tree_view_node& section_node = add_topic(section->id, section->title, true, parent_node);

for(const help::topic& topic : help::default_toplevel.topics) {
add_topic(window, topic.id, topic.title, false);
add_topics_for_section(*section, section_node);
}

on_topic_select(window);
for(const help::topic& topic : parent_section.topics) {
add_topic(topic.id, topic.title, false, parent_node);
}
}

void help_browser::add_topic(window& window, const std::string& topic_id, const std::string& topic_title, bool expands, tree_view_node*) {
tree_view& topic_tree = find_widget<tree_view>(&window, "topic_tree", false);
tree_view_node& help_browser::add_topic(const std::string& topic_id, const std::string& topic_title,
bool expands, tree_view_node& parent)
{
std::map<std::string, string_map> data;
string_map item;

item["label"] = topic_title;
data.emplace("topic_name", item);

item.clear();
item["label"] = expands ? help::closed_section_img : help::topic_img;
data.emplace("topic_icon", item);

topic_tree.add_node("topic", data).set_id(std::string(expands ? "+" : "-") + topic_id);
tree_view_node& new_node = parent.add_child("topic", data);
new_node.set_id(std::string(expands ? "+" : "-") + topic_id);

return new_node;
}

static std::string format_help_text(const config& cfg) {
static std::string format_help_text(const config& cfg)
{
std::stringstream ss;
for(auto& item : cfg.all_children_range()) {
if(item.key == "text") {
Expand All @@ -100,7 +111,7 @@ static std::string format_help_text(const config& cfg) {
throw help::parse_error(msg.str());
};
// TODO: Get the proper link shade from somewhere
ss << font::format_as_link(font::escape_text(item.cfg["text"]), "#aaaa00");
ss << font::format_as_link(font::escape_text(item.cfg["text"]), "#ffff00");
} else if(item.key == "img") {
if(item.cfg["src"].empty()) {
throw help::parse_error("Img markup must have src attribute.");
Expand Down Expand Up @@ -131,19 +142,24 @@ static std::string format_help_text(const config& cfg) {
if(item.cfg["text"].empty()) {
throw help::parse_error("Header markup must have text attribute.");
}

ss << "<span";
if(item.cfg.has_attribute("font_size")) {
ss << " size='" << item.cfg["font_size"].to_int(font::SIZE_NORMAL) << "'";
}

if(item.cfg.has_attribute("color")) {
ss << " color='" << help::string_to_color(item.cfg["color"]).to_hex_string() << "'";
}

if(item.cfg["bold"]) {
ss << " font_weight='bold'";
}

if(item.cfg["italic"]) {
ss << " font_style='italic'";
}

ss << '>' << font::escape_text(item.cfg["text"]) << "</span>";
}
}
Expand All @@ -159,8 +175,17 @@ void help_browser::on_topic_select(window& window)
return;
}

assert(topic_tree.selected_item());
std::string topic_id = topic_tree.selected_item()->id();
tree_view_node* selected = topic_tree.selected_item();
assert(selected);

// FIXME: should we be manually doing this?
if(selected->is_folded()) {
selected->unfold();
} else {
selected->fold();
}

std::string topic_id = selected->id();

if(topic_id == "") {
return;
Expand All @@ -173,6 +198,7 @@ void help_browser::on_topic_select(window& window)
}

help::section& sec = help::default_toplevel;

auto iter = parsed_pages_.find(topic_id);
if(iter == parsed_pages_.end()) {
const help::topic* topic = help::find_topic(sec, topic_id);
Expand All @@ -188,12 +214,12 @@ void help_browser::on_topic_select(window& window)

parsed_pages_.emplace(topic_id, topic_pages.get_page_count());
topic_pages.add_page(data);

window.invalidate_layout();
}

const unsigned topic_i = parsed_pages_.at(topic_id);
topic_pages.select_page(topic_i);

}

} // namespace dialogs
Expand Down
8 changes: 7 additions & 1 deletion src/gui/dialogs/help_browser.hpp
Expand Up @@ -17,13 +17,17 @@

#include "gui/dialogs/modal_dialog.hpp"

#include "help/help_impl.hpp"

#include <map>

class config;
class CVideo;

namespace gui2
{
class tree_view_node;

namespace dialogs
{

Expand Down Expand Up @@ -51,7 +55,9 @@ class help_browser : public modal_dialog

void on_topic_select(window& window);

void add_topic(window& window, const std::string& topic_id, const std::string& topic_title, bool expands, class tree_view_node* parent = nullptr);
void add_topics_for_section(const help::section& parent_section, tree_view_node& parent_node);
tree_view_node& add_topic(const std::string& topic_id, const std::string& topic_title,
bool expands, tree_view_node& parent);
};

} // namespace dialogs
Expand Down

0 comments on commit f3da8fc

Please sign in to comment.