From 8eff3b191070a264c5d949eba011fad88305eb53 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Tue, 13 Jun 2023 19:29:12 +0100 Subject: [PATCH] LibGUI: Add functions to get/set all a TableView's visible columns Specifically, this is to make it easier to save and restore this state to a config file. I had hoped to use the column names instead of their IDs, but some columns have an empty string as their name so we wouldn't be able to distinguish between those. --- .../Libraries/LibGUI/AbstractTableView.cpp | 33 +++++++++++++++++++ Userland/Libraries/LibGUI/AbstractTableView.h | 4 +++ 2 files changed, 37 insertions(+) diff --git a/Userland/Libraries/LibGUI/AbstractTableView.cpp b/Userland/Libraries/LibGUI/AbstractTableView.cpp index 8e00f6a32d..cd8956ef69 100644 --- a/Userland/Libraries/LibGUI/AbstractTableView.cpp +++ b/Userland/Libraries/LibGUI/AbstractTableView.cpp @@ -1,6 +1,7 @@ /* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2022, the SerenityOS developers. + * Copyright (c) 2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -387,6 +388,38 @@ void AbstractTableView::set_column_visible(int column, bool visible) column_header().set_section_visible(column, visible); } +ErrorOr AbstractTableView::get_visible_columns() const +{ + StringBuilder builder; + + bool first = true; + for (int column = 0; column < model()->column_count(); ++column) { + if (!column_header().is_section_visible(column)) + continue; + + if (first) { + TRY(builder.try_appendff("{}", column)); + first = false; + } else { + TRY(builder.try_appendff(",{}", column)); + } + } + + return builder.to_string(); +} + +void AbstractTableView::set_visible_columns(StringView column_names) +{ + for (int column = 0; column < model()->column_count(); ++column) + column_header().set_section_visible(column, false); + + column_names.for_each_split_view(',', SplitBehavior::Nothing, [&, this](StringView column_id_string) { + if (auto column = column_id_string.to_int(); column.has_value()) { + column_header().set_section_visible(column.value(), true); + } + }); +} + void AbstractTableView::set_column_headers_visible(bool visible) { column_header().set_visible(visible); diff --git a/Userland/Libraries/LibGUI/AbstractTableView.h b/Userland/Libraries/LibGUI/AbstractTableView.h index 636ae23aa0..931ffdabc2 100644 --- a/Userland/Libraries/LibGUI/AbstractTableView.h +++ b/Userland/Libraries/LibGUI/AbstractTableView.h @@ -2,6 +2,7 @@ * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2022, networkException * Copyright (c) 2022, the SerenityOS developers. + * Copyright (c) 2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -40,6 +41,9 @@ public: void set_column_headers_visible(bool); void set_column_visible(int, bool); + // These return/accept a comma-separated list of column ids, for storing in a config file. + ErrorOr get_visible_columns() const; + void set_visible_columns(StringView column_ids); int column_width(int column) const; void set_column_width(int column, int width);