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.
This commit is contained in:
Fabian Dellwing 2023-04-02 17:50:03 +02:00 committed by Andrew Kaster
parent dfce65a0ab
commit 14d78e10d1
Notes: sideshowbarker 2024-07-17 03:45:48 +09:00
3 changed files with 38 additions and 5 deletions

View file

@ -10,10 +10,21 @@
#include <LibFileSystem/FileSystem.h>
#include <LibFileSystemAccessClient/Client.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/SortingProxyModel.h>
namespace CertificateSettings {
NonnullRefPtr<CertificateStoreModel> CertificateStoreModel::create() { return adopt_ref(*new CertificateStoreModel); }
NonnullRefPtr<CertificateStoreModel> 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<void> CertificateStoreModel::load()
{
@ -113,8 +124,9 @@ Certificate CertificateStoreModel::get(int index)
ErrorOr<void> 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<void> 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);

View file

@ -8,11 +8,28 @@
#include <LibGUI/Model.h>
#include <LibGUI/SettingsWindow.h>
#include <LibGUI/SortingProxyModel.h>
#include <LibGUI/TableView.h>
#include <LibTLS/Certificate.h>
namespace CertificateSettings {
class CertificateStoreProxyModel : public GUI::SortingProxyModel {
public:
static ErrorOr<NonnullRefPtr<CertificateStoreProxyModel>> create(NonnullRefPtr<Model> source, NonnullRefPtr<GUI::TableView> 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<Model> source, NonnullRefPtr<GUI::TableView> view)
: SortingProxyModel(move(source))
, m_parent_table_view(move(view)) {};
NonnullRefPtr<GUI::TableView> m_parent_table_view;
};
class CertificateStoreModel : public GUI::Model {
public:
enum Column {
@ -51,6 +68,7 @@ private:
ErrorOr<void> export_pem();
RefPtr<CertificateStoreModel> m_root_ca_model;
RefPtr<CertificateStoreProxyModel> m_root_ca_proxy_model;
RefPtr<GUI::TableView> m_root_ca_tableview;
RefPtr<GUI::Button> m_import_ca_button;
RefPtr<GUI::Button> m_export_ca_button;

View file

@ -48,9 +48,10 @@ public:
virtual void sort(int column, SortOrder) override;
private:
protected:
explicit SortingProxyModel(NonnullRefPtr<Model> source);
private:
// NOTE: The internal_data() of indices points to the corresponding Mapping object for that index.
struct Mapping {
Vector<int> source_rows;