GUI2/Tree View [Node]: formatting cleanup

This commit is contained in:
Charles Dang 2018-07-14 11:54:35 +11:00
parent c1774005a9
commit e8fb922516
2 changed files with 87 additions and 105 deletions

View file

@ -16,22 +16,20 @@
#include "gui/widgets/tree_view.hpp"
#include "gettext.hpp"
#include "gui/core/log.hpp"
#include "gui/core/window_builder/helper.hpp"
#include "gui/core/register_widget.hpp"
#include "gui/core/window_builder/helper.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "gettext.hpp"
#include "wml_exception.hpp"
#include "utils/functional.hpp"
#include "wml_exception.hpp"
#define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
#define LOG_HEADER LOG_SCOPE_HEADER + ':'
namespace gui2
{
// ------------ WIDGET -----------{
REGISTER_WIDGET(tree_view)
@ -41,27 +39,23 @@ tree_view::tree_view(const implementation::builder_tree_view& builder)
, node_definitions_(builder.nodes)
, indentation_step_size_(0)
, need_layout_(false)
, root_node_(new tree_view_node("root",
nullptr,
*this,
std::map<std::string, string_map>()))
, root_node_(new tree_view_node("root", nullptr, *this, std::map<std::string, string_map>()))
, selected_item_(nullptr)
, selection_change_callback_()
{
connect_signal<event::LEFT_BUTTON_DOWN>(
std::bind(&tree_view::signal_handler_left_button_down, this, _2),
event::dispatcher::back_pre_child);
std::bind(&tree_view::signal_handler_left_button_down, this, _2), event::dispatcher::back_pre_child);
}
tree_view::~tree_view()
{
if (root_node_) {
if(root_node_) {
root_node_->clear_before_destruct();
}
}
tree_view_node& tree_view::add_node(
const std::string& id,
const std::map<std::string /* widget id */, string_map>& data,
const int index)
const std::string& id, const std::map<std::string /* widget id */, string_map>& data, const int index)
{
return get_root_node().add_child(id, data, index);
}
@ -73,9 +67,7 @@ std::pair<tree_view_node::ptr_t, int> tree_view::remove_node(tree_view_node* nod
tree_view_node::node_children_vector& siblings = node->parent_node_->children_;
auto node_itor = std::find_if(siblings.begin(), siblings.end(),
[node](const tree_view_node::ptr_t& c) { return c.get() == node; }
);
auto node_itor = std::find_if(siblings.begin(), siblings.end(), [node](const auto& c) { return c.get() == node; });
assert(node_itor != siblings.end());
@ -127,16 +119,19 @@ void tree_view::layout_children()
}
void tree_view::resize_content(const int width_modification,
const int height_modification,
const int width__modification_pos,
const int height_modification_pos)
const int height_modification,
const int width__modification_pos,
const int height_modification_pos)
{
DBG_GUI_L << LOG_HEADER << " current size " << content_grid()->get_size()
<< " width_modification " << width_modification
<< " height_modification " << height_modification << ".\n";
if(content_resize_request(width_modification, height_modification, width__modification_pos, height_modification_pos)) {
DBG_GUI_L << LOG_HEADER << " current size " << content_grid()->get_size() << " width_modification "
<< width_modification << " height_modification " << height_modification << ".\n";
if(content_resize_request(
width_modification,
height_modification,
width__modification_pos,
height_modification_pos
)) {
// Calculate new size.
point size = content_grid()->get_size();
size.x += width_modification;
@ -163,9 +158,7 @@ void tree_view::layout_children(const bool force)
assert(root_node_ && content_grid());
if(need_layout_ || force) {
root_node_->place(indentation_step_size_,
get_origin(),
content_grid()->get_size().x);
root_node_->place(indentation_step_size_, get_origin(), content_grid()->get_size().x);
root_node_->set_visible_rectangle(content_visible_area_);
need_layout_ = false;
@ -180,12 +173,8 @@ void tree_view::finalize_setup()
assert(content_grid());
content_grid()->set_rows_cols(1, 1);
content_grid()->set_child(root_node_,
0,
0,
grid::VERTICAL_GROW_SEND_TO_CLIENT
| grid::HORIZONTAL_GROW_SEND_TO_CLIENT,
0);
content_grid()->set_child(
root_node_, 0, 0, grid::VERTICAL_GROW_SEND_TO_CLIENT | grid::HORIZONTAL_GROW_SEND_TO_CLIENT, 0);
}
void tree_view::signal_handler_left_button_down(const event::ui_event event)
@ -194,33 +183,36 @@ void tree_view::signal_handler_left_button_down(const event::ui_event event)
get_window()->keyboard_capture(this);
}
template<tree_view_node* (tree_view_node::*func) ()>
template<tree_view_node* (tree_view_node::*func)()>
tree_view_node* tree_view::get_next_node()
{
tree_view_node* selected = selected_item();
if(!selected) {
return nullptr;
}
tree_view_node* visible = selected->get_last_visible_parent_node();
if(visible != selected) {
return visible;
}
return (selected->*func)();
}
template<tree_view_node* (tree_view_node::*func) ()>
template<tree_view_node* (tree_view_node::*func)()>
bool tree_view::handle_up_down_arrow()
{
if(tree_view_node* next = get_next_node<func>())
{
if(tree_view_node* next = get_next_node<func>()) {
next->select_node();
SDL_Rect visible = content_visible_area();
SDL_Rect rect = next->get_grid().get_rectangle();
visible.y = rect.y;// - content_grid()->get_y();
visible.y = rect.y; // - content_grid()->get_y();
visible.h = rect.h;
show_content_rect(visible);
return true;
}
return false;
}
@ -228,8 +220,7 @@ void tree_view::handle_key_up_arrow(SDL_Keymod modifier, bool& handled)
{
if(handle_up_down_arrow<&tree_view_node::get_selectable_node_above>()) {
handled = true;
}
else {
} else {
scrollbar_container::handle_key_up_arrow(modifier, handled);
}
}
@ -238,13 +229,11 @@ void tree_view::handle_key_down_arrow(SDL_Keymod modifier, bool& handled)
{
if(handle_up_down_arrow<&tree_view_node::get_selectable_node_below>()) {
handled = true;
}
else {
} else {
scrollbar_container::handle_key_down_arrow(modifier, handled);
}
}
void tree_view::handle_key_left_arrow(SDL_Keymod modifier, bool& handled)
{
tree_view_node* selected = selected_item();
@ -252,6 +241,7 @@ void tree_view::handle_key_left_arrow(SDL_Keymod modifier, bool& handled)
scrollbar_container::handle_key_left_arrow(modifier, handled);
return;
}
selected->fold();
handled = true;
}
@ -263,6 +253,7 @@ void tree_view::handle_key_right_arrow(SDL_Keymod modifier, bool& handled)
scrollbar_container::handle_key_right_arrow(modifier, handled);
return;
}
selected->unfold();
handled = true;
}
@ -303,7 +294,8 @@ tree_view_definition::tree_view_definition(const config& cfg)
* @end{parent}{name="gui/"}
*/
tree_view_definition::resolution::resolution(const config& cfg)
: resolution_definition(cfg), grid(nullptr)
: resolution_definition(cfg)
, grid(nullptr)
{
// Note the order should be the same as the enum state_t is listbox.hpp.
state.emplace_back(cfg.child("state_enabled"));
@ -372,19 +364,14 @@ tree_view_definition::resolution::resolution(const config& cfg)
namespace implementation
{
builder_tree_view::builder_tree_view(const config& cfg)
: builder_styled_widget(cfg)
, vertical_scrollbar_mode(
get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
, horizontal_scrollbar_mode(
get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
, vertical_scrollbar_mode(get_scrollbar_mode(cfg["vertical_scrollbar_mode"]))
, horizontal_scrollbar_mode(get_scrollbar_mode(cfg["horizontal_scrollbar_mode"]))
, indentation_step_size(cfg["indentation_step_size"])
, nodes()
{
for(const auto & node : cfg.child_range("node"))
{
for(const auto& node : cfg.child_range("node")) {
nodes.emplace_back(node);
}
@ -404,8 +391,7 @@ widget* builder_tree_view::build() const
widget->set_indentation_step_size(indentation_step_size);
DBG_GUI_G << "Window builder: placed tree_view '" << id
<< "' with definition '" << definition << "'.\n";
DBG_GUI_G << "Window builder: placed tree_view '" << id << "' with definition '" << definition << "'.\n";
const auto conf = widget->cast_config_to<tree_view_definition>();
assert(conf);
@ -423,8 +409,7 @@ tree_node::tree_node(const config& cfg)
{
VALIDATE(!id.empty(), missing_mandatory_wml_key("node", "id"));
VALIDATE(id != "root",
_("[node]id 'root' is reserved for the implementation."));
VALIDATE(id != "root", _("[node]id 'root' is reserved for the implementation."));
const config& node_definition = cfg.child("node_definition");

View file

@ -24,7 +24,6 @@
#include "gui/widgets/toggle_panel.hpp"
#include "gui/widgets/tree_view.hpp"
#include "sdl/rect.hpp"
#include "utils/functional.hpp"
#define LOG_SCOPE_HEADER get_control_type() + " [" + get_tree_view().id() + "] " + __func__
@ -32,9 +31,7 @@
namespace gui2
{
tree_view_node::tree_view_node(
const std::string& id,
tree_view_node::tree_view_node(const std::string& id,
tree_view_node* parent_node,
tree_view& parent_tree_view,
const std::map<std::string /* widget id */, string_map>& data)
@ -77,11 +74,12 @@ tree_view_node::tree_view_node(
if(toggle_) {
toggle_widget->set_visible(widget::visibility::hidden);
toggle_widget->connect_signal<event::LEFT_BUTTON_CLICK>(std::bind(
&tree_view_node::signal_handler_left_button_click, this, _2));
toggle_widget->connect_signal<event::LEFT_BUTTON_CLICK>(std::bind(
&tree_view_node::signal_handler_left_button_click, this, _2),
event::dispatcher::back_post_child);
toggle_widget->connect_signal<event::LEFT_BUTTON_CLICK>(
std::bind(&tree_view_node::signal_handler_left_button_click, this, _2));
toggle_widget->connect_signal<event::LEFT_BUTTON_CLICK>(
std::bind(&tree_view_node::signal_handler_left_button_click, this, _2),
event::dispatcher::back_post_child);
if(unfolded_) {
toggle_->set_value(1);
@ -92,12 +90,13 @@ tree_view_node::tree_view_node(
label_ = dynamic_cast<selectable_item*>(label_widget);
if(label_) {
label_widget->connect_signal<event::LEFT_BUTTON_CLICK>(std::bind(
&tree_view_node::signal_handler_label_left_button_click, this, _2, _3, _4),
event::dispatcher::front_child);
label_widget->connect_signal<event::LEFT_BUTTON_CLICK>(std::bind(
&tree_view_node::signal_handler_label_left_button_click, this, _2, _3, _4),
event::dispatcher::front_pre_child);
label_widget->connect_signal<event::LEFT_BUTTON_CLICK>(
std::bind(&tree_view_node::signal_handler_label_left_button_click, this, _2, _3, _4),
event::dispatcher::front_child);
label_widget->connect_signal<event::LEFT_BUTTON_CLICK>(
std::bind(&tree_view_node::signal_handler_label_left_button_click, this, _2, _3, _4),
event::dispatcher::front_pre_child);
if(!get_tree_view().selected_item_) {
get_tree_view().selected_item_ = this;
@ -281,8 +280,7 @@ void tree_view_node::clear()
int height_reduction = 0;
if(!is_folded()) {
for(const auto & node : children_)
{
for(const auto& node : children_) {
height_reduction += node->get_current_size().y;
}
}
@ -293,31 +291,29 @@ void tree_view_node::clear()
return;
}
get_tree_view().resize_content(0, -height_reduction, -1, calculate_ypos());
get_tree_view().resize_content(0, -height_reduction, -1, calculate_ypos());
}
struct tree_view_node_implementation
{
private:
template <class W, class It>
static W* find_at_aux(It begin,
It end,
const point& coordinate,
const bool must_be_active)
template<class W, class It>
static W* find_at_aux(It begin, It end, const point& coordinate, const bool must_be_active)
{
for(It it = begin; it != end; ++it) {
if(W* widget = (*it)->find_at(coordinate, must_be_active)) {
return widget;
}
}
return nullptr;
}
public:
template <class W>
template<class W>
static W* find_at(utils::const_clone_ref<tree_view_node, W> tree_view_node,
const point& coordinate,
const bool must_be_active)
const point& coordinate,
const bool must_be_active)
{
if(W* widget = tree_view_node.grid_.find_at(coordinate, must_be_active)) {
return widget;
@ -327,10 +323,8 @@ public:
return nullptr;
}
return find_at_aux<W>(tree_view_node.children_.begin(),
tree_view_node.children_.end(),
coordinate,
must_be_active);
return find_at_aux<W>(
tree_view_node.children_.begin(), tree_view_node.children_.end(), coordinate, must_be_active);
}
};
@ -444,6 +438,7 @@ point tree_view_node::get_folded_size() const
if(get_indentation_level() > 1) {
size.x += (get_indentation_level() - 1) * get_tree_view().indentation_step_size_;
}
return size;
}
@ -468,8 +463,7 @@ point tree_view_node::get_unfolded_size() const
return size;
}
point tree_view_node::calculate_best_size(const int indentation_level,
const unsigned indentation_step_size) const
point tree_view_node::calculate_best_size(const int indentation_level, const unsigned indentation_step_size) const
{
log_scope2(log_gui_layout, LOG_SCOPE_HEADER);
@ -515,9 +509,7 @@ void tree_view_node::place(const point& origin, const point& size)
get_tree_view().layout_children(true);
}
unsigned tree_view_node::place(const unsigned indentation_step_size,
point origin,
unsigned width)
unsigned tree_view_node::place(const unsigned indentation_step_size, point origin, unsigned width)
{
log_scope2(log_gui_layout, LOG_SCOPE_HEADER);
DBG_GUI_L << LOG_HEADER << " origin " << origin << ".\n";
@ -542,7 +534,7 @@ unsigned tree_view_node::place(const unsigned indentation_step_size,
}
DBG_GUI_L << LOG_HEADER << " set children.\n";
for(auto & node : children_) {
for(auto& node : children_) {
origin.y += node->place(indentation_step_size, origin, width);
}
@ -564,7 +556,7 @@ void tree_view_node::set_visible_rectangle(const SDL_Rect& rectangle)
return;
}
for(auto & node : children_) {
for(auto& node : children_) {
node->set_visible_rectangle(rectangle);
}
}
@ -579,7 +571,7 @@ void tree_view_node::impl_draw_children(surface& frame_buffer,
return;
}
for(auto & node : children_) {
for(auto& node : children_) {
node->impl_draw_children(frame_buffer, x_offset, y_offset);
}
}
@ -605,8 +597,7 @@ void tree_view_node::signal_handler_left_button_click(const event::ui_event even
fire(event::NOTIFY_MODIFIED, *this, nullptr);
}
void tree_view_node::signal_handler_label_left_button_click(
const event::ui_event event, bool& handled, bool& halt)
void tree_view_node::signal_handler_label_left_button_click(const event::ui_event event, bool& handled, bool& halt)
{
DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
@ -632,9 +623,7 @@ void tree_view_node::signal_handler_label_left_button_click(
}
}
void tree_view_node::init_grid(
grid* g,
const std::map<std::string /* widget id */, string_map>& data)
void tree_view_node::init_grid(grid* g, const std::map<std::string /* widget id */, string_map>& data)
{
assert(g);
@ -694,7 +683,7 @@ std::vector<int> tree_view_node::describe_path()
}
assert(!"tree_view_node was not found in parent nodes children");
throw "assertion ignored"; //To silence 'no return value in this codepath' warning.
throw "assertion ignored"; // To silence 'no return value in this codepath' warning.
}
int tree_view_node::calculate_ypos()
@ -745,8 +734,11 @@ tree_view_node* tree_view_node::get_node_above()
cur = &cur->get_child_at(cur->count_children() - 1);
}
if (!cur)
throw std::domain_error("tree_view_node::get_node_above(): Cannot determine which node is this line, or which node is the line above this one, if any.");
if(!cur) {
throw std::domain_error(
"tree_view_node::get_node_above(): Cannot determine which node is this line, or which "
"node is the line above this one, if any.");
}
return cur;
}
@ -769,6 +761,7 @@ tree_view_node* tree_view_node::get_node_below()
} else {
cur = &parent;
}
break;
}
}
@ -783,6 +776,7 @@ tree_view_node* tree_view_node::get_selectable_node_above()
do {
above = above->get_node_above();
} while(above != nullptr && above->label_ == nullptr);
return above;
}
@ -792,6 +786,7 @@ tree_view_node* tree_view_node::get_selectable_node_below()
do {
below = below->get_node_below();
} while(below != nullptr && below->label_ == nullptr);
return below;
}
@ -811,11 +806,13 @@ void tree_view_node::select_node(bool expand_parents)
if(get_tree_view().selected_item_ && get_tree_view().selected_item_->label_) {
get_tree_view().selected_item_->label_->set_value(false);
}
get_tree_view().selected_item_ = this;
if(get_tree_view().selection_change_callback_) {
get_tree_view().selection_change_callback_(get_tree_view());
}
label_->set_value_bool(true);
}
@ -826,7 +823,7 @@ void tree_view_node::layout_initialize(const bool full_initialization)
grid_.layout_initialize(full_initialization);
// Clear child caches.
for(auto & child : children_) {
for(auto& child : children_) {
child->layout_initialize(full_initialization);
}
}