Organise persistence keys within subgroups and store columns by title rather than index.

This commit is contained in:
iwbnwif
2017-08-23 00:14:11 +01:00
parent 9cad6d1918
commit 110505543f

View File

@@ -19,11 +19,32 @@
#define wxPERSIST_DVLC_KIND "DataViewList" #define wxPERSIST_DVLC_KIND "DataViewList"
#define wxPERSIST_DVLC_COL_PREFIX "Column" #define wxPERSIST_DVLC_COLUMNS "Columns"
#define wxPERSIST_DVLC_HIDDEN "Hidden"
#define wxPERSIST_DVLC_POS "Position"
#define wxPERSIST_DVLC_TITLE "Title"
#define wxPERSIST_DVLC_WIDTH "Width" #define wxPERSIST_DVLC_WIDTH "Width"
#define wxPERSIST_DVLC_SORT_KEY "SortKey"
#define wxPERSIST_DVLC_SORT_ASC "SortAsc" #define wxPERSIST_DVLC_SORT_KEY "Sorting/Column"
#define wxPERSIST_DVLC_SORT_ASC "Sorting/Asc"
// ----------------------------------------------------------------------------
// Helper function to search for a column by its title.
// ----------------------------------------------------------------------------
wxDataViewColumn* GetColumnByTitle(wxDataViewCtrl* control,
const wxString& name)
{
wxASSERT(control);
for ( unsigned int col = 0; col < control->GetColumnCount(); col++)
{
if ( control->GetColumn(col)->GetTitle() == name)
return control->GetColumn(col);
}
return NULL;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxPersistentDataViewListCtrl: Saves and restores user modified column widths // wxPersistentDataViewListCtrl: Saves and restores user modified column widths
@@ -33,34 +54,39 @@
// has changed it and multicolumn sorts. // has changed it and multicolumn sorts.
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
class wxPersistentDataViewListCtrl : class wxPersistentDataViewCtrl : public wxPersistentWindow<wxDataViewCtrl>
public wxPersistentWindow<wxDataViewListCtrl>
{ {
public: public:
wxPersistentDataViewListCtrl(wxDataViewListCtrl* control) wxPersistentDataViewCtrl(wxDataViewCtrl* control)
: wxPersistentWindow<wxDataViewListCtrl>(control) : wxPersistentWindow<wxDataViewCtrl>(control)
{ {
} }
virtual void Save() const wxOVERRIDE virtual void Save() const wxOVERRIDE
{ {
wxDataViewListCtrl* const control = Get(); wxDataViewCtrl* const control = Get();
int sortColumn = -1; 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);
wxASSERT(column);
// Create a prefix string to identify each column. // Create a prefix string to identify each column.
wxString columnPrefix; wxString columnPrefix;
columnPrefix.Printf("%s%d", wxPERSIST_DVLC_COL_PREFIX, col); columnPrefix.Printf("/%s/%s/", wxPERSIST_DVLC_COLUMNS,
column->GetTitle());
// Save the column attributes.
SaveValue(columnPrefix + wxPERSIST_DVLC_HIDDEN, column->IsHidden());
SaveValue(columnPrefix + wxPERSIST_DVLC_POS,
control->GetColumnPosition(column));
SaveValue(columnPrefix + wxPERSIST_DVLC_WIDTH, column->GetWidth());
// Save the width of each column.
int width = control->GetColumn(col)->GetWidth();
SaveValue(columnPrefix + wxPERSIST_DVLC_WIDTH, width);
// Check if this column is the current sort key. // Check if this column is the current sort key.
if ( control->GetColumn(col)->IsSortKey() ) if ( column->IsSortKey() )
sortColumn = col; sortColumn = column;
} }
// Note: The current implementation does not save and restore multi- // Note: The current implementation does not save and restore multi-
@@ -68,45 +94,59 @@ public:
if (control->IsMultiColumnSortAllowed()) if (control->IsMultiColumnSortAllowed())
return; return;
// Save the current sort key. // Save the sort key and direction if there is a valid sort.
SaveValue(wxPERSIST_DVLC_SORT_KEY, sortColumn); if ( sortColumn )
// Save the sort direction.
if ( sortColumn > -1 )
{ {
bool sortAsc = SaveValue(wxPERSIST_DVLC_SORT_KEY, sortColumn->GetTitle());
control->GetColumn(sortColumn)->IsSortOrderAscending(); SaveValue(wxPERSIST_DVLC_SORT_ASC,
SaveValue(wxPERSIST_DVLC_SORT_ASC, sortAsc); sortColumn->IsSortOrderAscending());
} }
} }
virtual bool Restore() wxOVERRIDE virtual bool Restore() wxOVERRIDE
{ {
wxDataViewListCtrl* 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++ )
{ {
// Create a prefix string to identify each column. column = control->GetColumn(col);
wxString columnPrefix = wxASSERT(column);
wxString::Format("%s%d", wxPERSIST_DVLC_COL_PREFIX, col);
// Create a prefix string to identify each column within the
// persistence store (columns are stored by title). The persistence
// store benignly handles cases where the title is not found.
wxString columnPrefix;
columnPrefix.Printf("/%s/%s/", wxPERSIST_DVLC_COLUMNS,
column->GetTitle());
// Restore column hidden status.
bool hidden;
if ( RestoreValue(columnPrefix + wxPERSIST_DVLC_HIDDEN, &hidden) )
column->SetHidden(hidden);
// Restore the column width. // Restore the column width.
int width; int width;
if ( RestoreValue(columnPrefix + wxPERSIST_DVLC_WIDTH, &width) ) if ( RestoreValue(columnPrefix + wxPERSIST_DVLC_WIDTH, &width) )
control->GetColumn(col)->SetWidth(width); column->SetWidth(width);
// TODO: Set the column's view position.
} }
// Restore the sort key and order. // Restore the sort key and order.
int sortColumn; wxString sortColumn;
if ( RestoreValue(wxPERSIST_DVLC_SORT_KEY, &sortColumn) && if ( RestoreValue(wxPERSIST_DVLC_SORT_KEY, &sortColumn) &&
sortColumn > -1 && sortColumn != "" )
sortColumn < (int)control->GetColumnCount() )
{ {
bool sortAsc = true; bool sortAsc = true;
column = GetColumnByTitle(control, sortColumn);
RestoreValue(wxPERSIST_DVLC_SORT_ASC, &sortAsc); if ( column )
control->GetColumn(sortColumn)->SetSortOrder(sortAsc); {
control->GetModel()->Resort(); RestoreValue(wxPERSIST_DVLC_SORT_ASC, &sortAsc);
column->SetSortOrder(sortAsc);
control->GetModel()->Resort();
}
} }
return true; return true;
} }
@@ -117,9 +157,9 @@ public:
} }
}; };
inline wxPersistentObject *wxCreatePersistentObject(wxDataViewListCtrl* control) inline wxPersistentObject *wxCreatePersistentObject(wxDataViewCtrl* control)
{ {
return new wxPersistentDataViewListCtrl(control); return new wxPersistentDataViewCtrl(control);
} }
#endif // _WX_PERSIST_DATAVIEW_H_ #endif // _WX_PERSIST_DATAVIEW_H_