mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:48:10 +00:00
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:
parent
f33824d2e9
commit
8eff3b1910
2 changed files with 37 additions and 0 deletions
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue