Minor clean up of wxPersistentDataViewCtrl

Add another helper function, make variables const when possible, fix
some small style problems.

No real changes.
This commit is contained in:
Vadim Zeitlin
2017-12-15 15:26:32 +01:00
parent e3575d1f9c
commit ed23de08c7

View File

@@ -20,8 +20,6 @@
#define wxPERSIST_DVC_KIND "DataView" #define wxPERSIST_DVC_KIND "DataView"
#define wxPERSIST_DVC_COLUMNS "Columns"
#define wxPERSIST_DVC_HIDDEN "Hidden" #define wxPERSIST_DVC_HIDDEN "Hidden"
#define wxPERSIST_DVC_POS "Position" #define wxPERSIST_DVC_POS "Position"
#define wxPERSIST_DVC_TITLE "Title" #define wxPERSIST_DVC_TITLE "Title"
@@ -30,28 +28,6 @@
#define wxPERSIST_DVC_SORT_KEY "Sorting/Column" #define wxPERSIST_DVC_SORT_KEY "Sorting/Column"
#define wxPERSIST_DVC_SORT_ASC "Sorting/Asc" #define wxPERSIST_DVC_SORT_ASC "Sorting/Asc"
// ----------------------------------------------------------------------------
// Helper function to search for a column by its title.
// ----------------------------------------------------------------------------
namespace wxPrivate
{
inline
wxDataViewColumn*
GetColumnByTitle(wxDataViewCtrl* control, const wxString& name)
{
for ( unsigned int col = 0; col < control->GetColumnCount(); col++)
{
if ( control->GetColumn(col)->GetTitle() == name )
return control->GetColumn(col);
}
return NULL;
}
} // namespace wxPrivate
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxPersistentDataViewCtrl: Saves and restores user modified column widths // wxPersistentDataViewCtrl: Saves and restores user modified column widths
// and single column sort order. // and single column sort order.
@@ -72,17 +48,14 @@ public:
{ {
wxDataViewCtrl* const control = Get(); wxDataViewCtrl* const control = Get();
wxDataViewColumn* sortColumn = NULL; const wxDataViewColumn* sortColumn = NULL;
for ( unsigned int col = 0; col < control->GetColumnCount(); col++ ) for ( unsigned int col = 0; col < control->GetColumnCount(); col++ )
{ {
wxDataViewColumn* column = control->GetColumn(col); const wxDataViewColumn* const column = control->GetColumn(col);
wxASSERT(column);
// Create a prefix string to identify each column. // Create a prefix string to identify each column.
wxString columnPrefix; const wxString columnPrefix = MakeColumnPrefix(column);
columnPrefix.Printf("/%s/%s/", wxPERSIST_DVC_COLUMNS,
column->GetTitle());
// Save the column attributes. // Save the column attributes.
SaveValue(columnPrefix + wxPERSIST_DVC_HIDDEN, column->IsHidden()); SaveValue(columnPrefix + wxPERSIST_DVC_HIDDEN, column->IsHidden());
@@ -97,7 +70,7 @@ public:
// Note: The current implementation does not save and restore multi- // Note: The current implementation does not save and restore multi-
// column sort keys. // column sort keys.
if (control->IsMultiColumnSortAllowed()) if ( control->IsMultiColumnSortAllowed() )
return; return;
// Save the sort key and direction if there is a valid sort. // Save the sort key and direction if there is a valid sort.
@@ -112,19 +85,15 @@ public:
virtual bool Restore() wxOVERRIDE virtual bool Restore() wxOVERRIDE
{ {
wxDataViewCtrl* const control = Get(); wxDataViewCtrl* const control = Get();
wxDataViewColumn* column;
for ( unsigned int col = 0; col < control->GetColumnCount(); col++ ) for ( unsigned int col = 0; col < control->GetColumnCount(); col++ )
{ {
column = control->GetColumn(col); wxDataViewColumn* const column = control->GetColumn(col);
wxASSERT(column);
// Create a prefix string to identify each column within the // Create a prefix string to identify each column within the
// persistence store (columns are stored by title). The persistence // persistence store (columns are stored by title). The persistence
// store benignly handles cases where the title is not found. // store benignly handles cases where the title is not found.
wxString columnPrefix; const wxString columnPrefix = MakeColumnPrefix(column);
columnPrefix.Printf("/%s/%s/", wxPERSIST_DVC_COLUMNS,
column->GetTitle());
// Restore column hidden status. // Restore column hidden status.
bool hidden; bool hidden;
@@ -144,12 +113,10 @@ public:
wxString sortColumn; wxString sortColumn;
if ( control->GetModel() && if ( control->GetModel() &&
RestoreValue(wxPERSIST_DVC_SORT_KEY, &sortColumn) && RestoreValue(wxPERSIST_DVC_SORT_KEY, &sortColumn) &&
sortColumn != "" ) !sortColumn.empty() )
{ {
bool sortAsc = true; bool sortAsc = true;
column = wxPrivate::GetColumnByTitle(control, sortColumn); if ( wxDataViewColumn* column = GetColumnByTitle(control, sortColumn) )
if ( column )
{ {
RestoreValue(wxPERSIST_DVC_SORT_ASC, &sortAsc); RestoreValue(wxPERSIST_DVC_SORT_ASC, &sortAsc);
column->SetSortOrder(sortAsc); column->SetSortOrder(sortAsc);
@@ -158,6 +125,7 @@ public:
control->GetModel()->Resort(); control->GetModel()->Resort();
} }
} }
return true; return true;
} }
@@ -165,6 +133,26 @@ public:
{ {
return wxPERSIST_DVC_KIND; return wxPERSIST_DVC_KIND;
} }
private:
// Return a (slash-terminated) prefix for the column-specific entries.
static wxString MakeColumnPrefix(const wxDataViewColumn* column)
{
return wxString::Format("/Columns/%s/", column->GetTitle());
}
// Return the column with the given title or NULL.
static wxDataViewColumn*
GetColumnByTitle(wxDataViewCtrl* control, const wxString& title)
{
for ( unsigned int col = 0; col < control->GetColumnCount(); col++ )
{
if ( control->GetColumn(col)->GetTitle() == title )
return control->GetColumn(col);
}
return NULL;
}
}; };
inline wxPersistentObject *wxCreatePersistentObject(wxDataViewCtrl* control) inline wxPersistentObject *wxCreatePersistentObject(wxDataViewCtrl* control)