From 14d78e10d11ab6732f6451cf14a3a24b56936469 Mon Sep 17 00:00:00 2001 From: Fabian Dellwing Date: Sun, 2 Apr 2023 17:50:03 +0200 Subject: [PATCH] LibGUI+CertificateSettings: Use custom SortingProxy The default SortingProxyModel does not allow to react to reodering. As we would like to keep the column width on sorting, we create a subclass and inject our code into the sorting method. --- .../CertificateSettings/CertificateStore.cpp | 22 +++++++++++++++---- .../CertificateSettings/CertificateStore.h | 18 +++++++++++++++ Userland/Libraries/LibGUI/SortingProxyModel.h | 3 ++- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Userland/Applications/CertificateSettings/CertificateStore.cpp b/Userland/Applications/CertificateSettings/CertificateStore.cpp index 0dc78a3f7d..0e434bac9a 100644 --- a/Userland/Applications/CertificateSettings/CertificateStore.cpp +++ b/Userland/Applications/CertificateSettings/CertificateStore.cpp @@ -10,10 +10,21 @@ #include #include #include +#include namespace CertificateSettings { -NonnullRefPtr CertificateStoreModel::create() { return adopt_ref(*new CertificateStoreModel); } +NonnullRefPtr CertificateStoreModel::create() +{ + return adopt_ref(*new CertificateStoreModel); +} + +void CertificateStoreProxyModel::sort(int column, GUI::SortOrder sort_order) +{ + SortingProxyModel::sort(column, sort_order); + m_parent_table_view->set_column_width(CertificateStoreModel::Column::IssuedTo, 150); + m_parent_table_view->set_column_width(CertificateStoreModel::Column::IssuedBy, 150); +} ErrorOr CertificateStoreModel::load() { @@ -113,8 +124,9 @@ Certificate CertificateStoreModel::get(int index) ErrorOr CertificateStoreWidget::export_pem() { - auto index = m_root_ca_tableview->selection().first().row(); - auto cert = m_root_ca_model->get(index); + auto index = m_root_ca_tableview->selection().first(); + auto real_index = m_root_ca_proxy_model->map_to_source(index); + auto cert = m_root_ca_model->get(real_index.row()); auto filename = cert.subject.subject.is_empty() ? cert.subject.unit : cert.subject.subject; auto file = FileSystemAccessClient::Client::the().save_file(window(), filename.replace(" "sv, "_"sv), "pem"sv); @@ -144,8 +156,10 @@ ErrorOr CertificateStoreWidget::initialize() m_root_ca_tableview->set_alternating_row_colors(false); m_root_ca_model = CertificateStoreModel::create(); + m_root_ca_proxy_model = TRY(CertificateStoreProxyModel::create(*m_root_ca_model, *m_root_ca_tableview)); + m_root_ca_proxy_model->set_sort_role(GUI::ModelRole::Display); TRY(m_root_ca_model->load()); - m_root_ca_tableview->set_model(m_root_ca_model); + m_root_ca_tableview->set_model(m_root_ca_proxy_model); m_root_ca_tableview->set_column_width(CertificateStoreModel::Column::IssuedTo, 150); m_root_ca_tableview->set_column_width(CertificateStoreModel::Column::IssuedBy, 150); diff --git a/Userland/Applications/CertificateSettings/CertificateStore.h b/Userland/Applications/CertificateSettings/CertificateStore.h index 1812d370c0..412276d0ef 100644 --- a/Userland/Applications/CertificateSettings/CertificateStore.h +++ b/Userland/Applications/CertificateSettings/CertificateStore.h @@ -8,11 +8,28 @@ #include #include +#include #include #include namespace CertificateSettings { +class CertificateStoreProxyModel : public GUI::SortingProxyModel { +public: + static ErrorOr> create(NonnullRefPtr source, NonnullRefPtr view) + { + return adopt_nonnull_ref_or_enomem(new (nothrow) CertificateStoreProxyModel(move(source), move(view))); + } + virtual void sort(int column, GUI::SortOrder) override; + +private: + CertificateStoreProxyModel(NonnullRefPtr source, NonnullRefPtr view) + : SortingProxyModel(move(source)) + , m_parent_table_view(move(view)) {}; + + NonnullRefPtr m_parent_table_view; +}; + class CertificateStoreModel : public GUI::Model { public: enum Column { @@ -51,6 +68,7 @@ private: ErrorOr export_pem(); RefPtr m_root_ca_model; + RefPtr m_root_ca_proxy_model; RefPtr m_root_ca_tableview; RefPtr m_import_ca_button; RefPtr m_export_ca_button; diff --git a/Userland/Libraries/LibGUI/SortingProxyModel.h b/Userland/Libraries/LibGUI/SortingProxyModel.h index 87ca108dd5..d0ff9e3a27 100644 --- a/Userland/Libraries/LibGUI/SortingProxyModel.h +++ b/Userland/Libraries/LibGUI/SortingProxyModel.h @@ -48,9 +48,10 @@ public: virtual void sort(int column, SortOrder) override; -private: +protected: explicit SortingProxyModel(NonnullRefPtr source); +private: // NOTE: The internal_data() of indices points to the corresponding Mapping object for that index. struct Mapping { Vector source_rows;