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.
This commit is contained in:
Sam Atkins 2023-06-13 19:29:12 +01:00 committed by Andreas Kling
parent f33824d2e9
commit 8eff3b1910
2 changed files with 37 additions and 0 deletions

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
*
* 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<String> 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);

View file

@ -2,6 +2,7 @@
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, networkException <networkexception@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
*
* 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<String> 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);