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_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_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
@@ -33,34 +54,39 @@
// has changed it and multicolumn sorts.
// ----------------------------------------------------------------------------
class wxPersistentDataViewListCtrl :
public wxPersistentWindow<wxDataViewListCtrl>
class wxPersistentDataViewCtrl : public wxPersistentWindow<wxDataViewCtrl>
{
public:
wxPersistentDataViewListCtrl(wxDataViewListCtrl* control)
: wxPersistentWindow<wxDataViewListCtrl>(control)
wxPersistentDataViewCtrl(wxDataViewCtrl* control)
: wxPersistentWindow<wxDataViewCtrl>(control)
{
}
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++ )
{
wxDataViewColumn* column = control->GetColumn(col);
wxASSERT(column);
// Create a prefix string to identify each column.
wxString columnPrefix;
columnPrefix.Printf("%s%d", wxPERSIST_DVLC_COL_PREFIX, col);
columnPrefix.Printf("/%s/%s/", wxPERSIST_DVLC_COLUMNS,
column->GetTitle());
// Save the width of each column.
int width = control->GetColumn(col)->GetWidth();
SaveValue(columnPrefix + wxPERSIST_DVLC_WIDTH, width);
// 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());
// Check if this column is the current sort key.
if ( control->GetColumn(col)->IsSortKey() )
sortColumn = col;
if ( column->IsSortKey() )
sortColumn = column;
}
// Note: The current implementation does not save and restore multi-
@@ -68,45 +94,59 @@ public:
if (control->IsMultiColumnSortAllowed())
return;
// Save the current sort key.
SaveValue(wxPERSIST_DVLC_SORT_KEY, sortColumn);
// Save the sort direction.
if ( sortColumn > -1 )
// Save the sort key and direction if there is a valid sort.
if ( sortColumn )
{
bool sortAsc =
control->GetColumn(sortColumn)->IsSortOrderAscending();
SaveValue(wxPERSIST_DVLC_SORT_ASC, sortAsc);
SaveValue(wxPERSIST_DVLC_SORT_KEY, sortColumn->GetTitle());
SaveValue(wxPERSIST_DVLC_SORT_ASC,
sortColumn->IsSortOrderAscending());
}
}
virtual bool Restore() wxOVERRIDE
{
wxDataViewListCtrl* const control = Get();
wxDataViewCtrl* const control = Get();
wxDataViewColumn* column;
for ( unsigned int col = 0; col < control->GetColumnCount(); col++ )
{
// Create a prefix string to identify each column.
wxString columnPrefix =
wxString::Format("%s%d", wxPERSIST_DVLC_COL_PREFIX, col);
column = control->GetColumn(col);
wxASSERT(column);
// 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.
int 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.
int sortColumn;
wxString sortColumn;
if ( RestoreValue(wxPERSIST_DVLC_SORT_KEY, &sortColumn) &&
sortColumn > -1 &&
sortColumn < (int)control->GetColumnCount() )
sortColumn != "" )
{
bool sortAsc = true;
column = GetColumnByTitle(control, sortColumn);
RestoreValue(wxPERSIST_DVLC_SORT_ASC, &sortAsc);
control->GetColumn(sortColumn)->SetSortOrder(sortAsc);
control->GetModel()->Resort();
if ( column )
{
RestoreValue(wxPERSIST_DVLC_SORT_ASC, &sortAsc);
column->SetSortOrder(sortAsc);
control->GetModel()->Resort();
}
}
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_