From c75e0babcc236d691b25d6b7e149a063c9fb09ed Mon Sep 17 00:00:00 2001 From: iwbnwif Date: Mon, 21 Aug 2017 22:12:56 +0100 Subject: [PATCH 1/8] Implement persistence for wxDataViewListCtrl --- include/wx/persist/dataview.h | 124 ++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 include/wx/persist/dataview.h diff --git a/include/wx/persist/dataview.h b/include/wx/persist/dataview.h new file mode 100644 index 0000000000..950a7707ee --- /dev/null +++ b/include/wx/persist/dataview.h @@ -0,0 +1,124 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: wx/persist/dataview.h +// Purpose: Persistence support for wxDataViewListCtrl +// Author: wxWidgets Team +// Created: 2017-08-21 +// Copyright: (c) 2017 wxWidgets.org +// Licence: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// + +#ifndef _WX_PERSIST_DATAVIEW_H_ +#define _WX_PERSIST_DATAVIEW_H_ + +#include "wx/persist/window.h" +#include "wx/dataview.h" + +// ---------------------------------------------------------------------------- +// String constants used by wxPersistentDataViewListCtrl. +// ---------------------------------------------------------------------------- + +#define wxPERSIST_DVLC_KIND "DataViewList" + +#define wxPERSIST_DVLC_COL_PREFIX "Column" + +#define wxPERSIST_DVLC_WIDTH "ColWidth" +#define wxPERSIST_DVLC_SORT_KEY "SortKey" +#define wxPERSIST_DVLC_SORT_ASC "SortAsc" + +// ---------------------------------------------------------------------------- +// wxPersistentDataViewListCtrl: Saves and restores user modified column widths +// and single column sort order. +// +// Future improvements could be to save and restore column order if the user +// has changed it and multicolumn sorts. +// ---------------------------------------------------------------------------- + +class wxPersistentDataViewListCtrl : + public wxPersistentWindow +{ +public: + wxPersistentDataViewListCtrl(wxDataViewListCtrl* control) + : wxPersistentWindow(control) + { + } + + virtual void Save() const wxOVERRIDE + { + wxDataViewListCtrl* const control = Get(); + + int sortColumn = -1; + + for ( unsigned int col = 0; col < control->GetColumnCount(); col++ ) + { + // Create a prefix string to identify each column. + wxString columnPrefix; + columnPrefix.Printf("%s %d ", wxPERSIST_DVLC_COL_PREFIX, col); + + // 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. + if ( control->GetColumn(col)->IsSortKey() ) + sortColumn = col; + } + + // Note: The current implementation does not save and restore multi- + // column sort keys. + if (control->IsMultiColumnSortAllowed()) + return; + + // Save the current sort key. + SaveValue(wxPERSIST_DVLC_SORT_KEY, sortColumn); + + // Save the sort direction. + if ( sortColumn > -1 ) + { + bool sortAsc = + control->GetColumn(sortColumn)->IsSortOrderAscending(); + SaveValue(wxPERSIST_DVLC_SORT_ASC, sortAsc); + } + } + + virtual bool Restore() wxOVERRIDE + { + wxDataViewListCtrl* const control = Get(); + + 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); + + // Restore the column width. + int width; + if ( RestoreValue(columnPrefix + wxPERSIST_DVLC_WIDTH, &width) ) + control->GetColumn(col)->SetWidth(width); + } + + // Restore the sort key and order. + int sortColumn; + if ( RestoreValue(wxPERSIST_DVLC_SORT_KEY, &sortColumn) && + sortColumn > -1 && + sortColumn < (int)control->GetColumnCount() ) + { + bool sortAsc = true; + + RestoreValue(wxPERSIST_DVLC_SORT_ASC, &sortAsc); + control->GetColumn(sortColumn)->SetSortOrder(sortAsc); + } + return true; + } + + virtual wxString GetKind() const wxOVERRIDE + { + return wxPERSIST_DVLC_KIND; + } +}; + +inline wxPersistentObject *wxCreatePersistentObject(wxDataViewListCtrl* control) +{ + return new wxPersistentDataViewListCtrl(control); +} + +#endif // _WX_PERSIST_DATAVIEW_H_ From 384d9f9043669cd35504ba1204f07ea76ec4885c Mon Sep 17 00:00:00 2001 From: iwbnwif Date: Mon, 21 Aug 2017 22:54:21 +0100 Subject: [PATCH 2/8] Minor documentation changes for wxPersistentObject. --- interface/wx/persist.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/interface/wx/persist.h b/interface/wx/persist.h index a60fd1f42c..d703e720b0 100644 --- a/interface/wx/persist.h +++ b/interface/wx/persist.h @@ -292,13 +292,14 @@ protected: bool SaveValue(const wxString& name, T value) const; /** - Restore the value saved by Save(). + Restore a value saved by SaveValue(). @param name - The same name as was used by Save(). + The same name as was used by SaveValue(). @param value - Non-@NULL pointer which will be filled with the value if it was - read successfully or not modified if it wasn't. + Non-@NULL pointer to the same type that was passed to SaveValue(). + The pointed to object will be filled with the saved value if it + was read successfully or not modified otherwise. @return @true if the value was successfully read or @false if it was not found or an error occurred. From 9cad6d191897545c240025f5e685d16894935117 Mon Sep 17 00:00:00 2001 From: iwbnwif Date: Tue, 22 Aug 2017 13:50:08 +0100 Subject: [PATCH 3/8] Make persistence store keys shorter and without spaces --- include/wx/persist/dataview.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/wx/persist/dataview.h b/include/wx/persist/dataview.h index 950a7707ee..52afdc1bdc 100644 --- a/include/wx/persist/dataview.h +++ b/include/wx/persist/dataview.h @@ -21,7 +21,7 @@ #define wxPERSIST_DVLC_COL_PREFIX "Column" -#define wxPERSIST_DVLC_WIDTH "ColWidth" +#define wxPERSIST_DVLC_WIDTH "Width" #define wxPERSIST_DVLC_SORT_KEY "SortKey" #define wxPERSIST_DVLC_SORT_ASC "SortAsc" @@ -52,7 +52,7 @@ public: { // Create a prefix string to identify each column. wxString columnPrefix; - columnPrefix.Printf("%s %d ", wxPERSIST_DVLC_COL_PREFIX, col); + columnPrefix.Printf("%s%d", wxPERSIST_DVLC_COL_PREFIX, col); // Save the width of each column. int width = control->GetColumn(col)->GetWidth(); @@ -88,7 +88,7 @@ public: { // Create a prefix string to identify each column. wxString columnPrefix = - wxString::Format("%s %d ", wxPERSIST_DVLC_COL_PREFIX, col); + wxString::Format("%s%d", wxPERSIST_DVLC_COL_PREFIX, col); // Restore the column width. int width; @@ -106,6 +106,7 @@ public: RestoreValue(wxPERSIST_DVLC_SORT_ASC, &sortAsc); control->GetColumn(sortColumn)->SetSortOrder(sortAsc); + control->GetModel()->Resort(); } return true; } From 110505543fad7909ec339381cde40735554bc170 Mon Sep 17 00:00:00 2001 From: iwbnwif Date: Wed, 23 Aug 2017 00:14:11 +0100 Subject: [PATCH 4/8] Organise persistence keys within subgroups and store columns by title rather than index. --- include/wx/persist/dataview.h | 116 +++++++++++++++++++++++----------- 1 file changed, 78 insertions(+), 38 deletions(-) diff --git a/include/wx/persist/dataview.h b/include/wx/persist/dataview.h index 52afdc1bdc..1394ec7289 100644 --- a/include/wx/persist/dataview.h +++ b/include/wx/persist/dataview.h @@ -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 +class wxPersistentDataViewCtrl : public wxPersistentWindow { public: - wxPersistentDataViewListCtrl(wxDataViewListCtrl* control) - : wxPersistentWindow(control) + wxPersistentDataViewCtrl(wxDataViewCtrl* control) + : wxPersistentWindow(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 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. - 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_ From 326b76e649252258e7682e12961f2fbc0930541e Mon Sep 17 00:00:00 2001 From: iwbnwif Date: Wed, 23 Aug 2017 00:21:02 +0100 Subject: [PATCH 5/8] Only attempt to update the sort order if the control has a model attached. --- include/wx/persist/dataview.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/include/wx/persist/dataview.h b/include/wx/persist/dataview.h index 1394ec7289..d0a1ad622f 100644 --- a/include/wx/persist/dataview.h +++ b/include/wx/persist/dataview.h @@ -133,10 +133,12 @@ public: // TODO: Set the column's view position. } - // Restore the sort key and order. + // Restore the sort key and order if there is a valid model and sort + // criteria. wxString sortColumn; - if ( RestoreValue(wxPERSIST_DVLC_SORT_KEY, &sortColumn) && - sortColumn != "" ) + if ( control->GetModel() && + RestoreValue(wxPERSIST_DVLC_SORT_KEY, &sortColumn) && + sortColumn != "" ) { bool sortAsc = true; column = GetColumnByTitle(control, sortColumn); @@ -145,6 +147,8 @@ public: { RestoreValue(wxPERSIST_DVLC_SORT_ASC, &sortAsc); column->SetSortOrder(sortAsc); + + // Resort the control based on the new sort criteria. control->GetModel()->Resort(); } } From 24b8f3a32df391a8fc10e87b476080c0d8c5d01a Mon Sep 17 00:00:00 2001 From: iwbnwif Date: Wed, 23 Aug 2017 19:27:47 +0100 Subject: [PATCH 6/8] Update documentation to include wxDataViewCtrl in the list of controls supported by the default persistent objects implementation. --- docs/doxygen/overviews/persistence.h | 2 ++ interface/wx/persist.h | 3 +++ 2 files changed, 5 insertions(+) diff --git a/docs/doxygen/overviews/persistence.h b/docs/doxygen/overviews/persistence.h index 396dddd587..a353ab6a7c 100644 --- a/docs/doxygen/overviews/persistence.h +++ b/docs/doxygen/overviews/persistence.h @@ -50,6 +50,8 @@ Currently the following classes are supported: - wxTopLevelWindow (and hence wxFrame and wxDialog) - wxBookCtrlBase (i.e. wxNotebook, wxListbook, wxToolbook and wxChoicebook) +- wxDataViewCtrl and its derivatives + (i.e. wxDataViewListCtrl and wxDataViewTreeCtrl) - wxTreebook To automatically save and restore the properties of the windows of classes diff --git a/interface/wx/persist.h b/interface/wx/persist.h index d703e720b0..683f6296b8 100644 --- a/interface/wx/persist.h +++ b/interface/wx/persist.h @@ -204,6 +204,9 @@ protected: /** Base class for persistent object adapters. + See @ref overview_persistence for an overview of persistent objects within + wxWidgets. + wxWidgets persistence framework is non-intrusive, i.e. can work with the classes which have no relationship to nor knowledge of it. To allow this, an intermediate persistence adapter is used: this is just a simple object From 63dead6350751be483484c05aabc94449e4fa60e Mon Sep 17 00:00:00 2001 From: iwbnwif Date: Wed, 23 Aug 2017 22:37:27 +0100 Subject: [PATCH 7/8] Add a simple test case for persistence of wxDataViewListCtrl (should test wxDataViewCtrl too) and wxFrame (should test wxTopLevelWindow). Further tests can be added in due course. --- tests/persist/persistence.cpp | 235 ++++++++++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 tests/persist/persistence.cpp diff --git a/tests/persist/persistence.cpp b/tests/persist/persistence.cpp new file mode 100644 index 0000000000..2be909b01c --- /dev/null +++ b/tests/persist/persistence.cpp @@ -0,0 +1,235 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/persistence.cpp +// Purpose: wxPersistentObjects unit test +// Author: wxWidgets Team +// Created: 2017-08-23 +// Copyright: (c) 2017 wxWidgets Team +/////////////////////////////////////////////////////////////////////////////// + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +#include "testprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#ifndef WX_PRECOMP + #include "wx/app.h" + #include "wx/config.h" + #include "wx/dataview.h" + #include "wx/frame.h" + #include "wx/persist/dataview.h" + #include "wx/persist/toplevel.h" +#endif // WX_PRECOMP + +// ---------------------------------------------------------------------------- +// macros +// ---------------------------------------------------------------------------- + +#define APP_NAME "cpptest" +#define PO_PREFIX "/Persistent_Options" +#define DVLC_PREFIX PO_PREFIX "/DataViewList/dvlc" +#define DVLC_COL "Column #" +#define DVLC_COL_PREFIX DVLC_PREFIX "/Columns/" DVLC_COL +#define DVLC_SORT_PREFIX DVLC_PREFIX "/Sorting" +#define DVLC_FRAME_PREFIX PO_PREFIX "/Window/frame" + +// -------------------------------------------------------------------------- +// test class +// -------------------------------------------------------------------------- + +class PersistenceTestCase : public CppUnit::TestCase +{ +public: + PersistenceTestCase() {} + + virtual void setUp(); + virtual void tearDown(); + +private: + CPPUNIT_TEST_SUITE( PersistenceTestCase ); + CPPUNIT_TEST( FrameSaveTest ); + CPPUNIT_TEST( FrameRestoreTest ); + CPPUNIT_TEST( DVLCSaveTest ); + CPPUNIT_TEST( DVLCRestoreTest ); + CPPUNIT_TEST_SUITE_END(); + + void FrameSaveTest(); + void FrameRestoreTest(); + void DVLCSaveTest(); + void DVLCRestoreTest(); + + wxDataViewListCtrl* m_list; + wxFrame* m_frame; + + wxDECLARE_NO_COPY_CLASS(PersistenceTestCase); +}; + +// register in the unnamed registry so that these tests are run by default +CPPUNIT_TEST_SUITE_REGISTRATION( PersistenceTestCase ); + +// also include in its own registry so that these tests can be run alone +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( PersistenceTestCase, "PersistenceTestCase" ); + +void PersistenceTestCase::setUp() +{ + wxConfigBase* conf = wxConfig::Get(); + conf->SetAppName("PersistTest"); + conf->SetVendorName("wxWidgets"); + + // Clear any pre-existing settings. + conf->DeleteGroup("/Persistence_Options"); + conf->Flush(); + + // Create the objects to persist. + m_frame = new wxFrame(wxTheApp->GetTopWindow(), wxID_ANY, + "Persistence Test", + wxDefaultPosition, wxSize(400,400)); + + m_list = new wxDataViewListCtrl(m_frame, wxID_ANY, + wxDefaultPosition, + wxDLG_UNIT(m_frame, wxSize(-1,-1)), + wxDV_ROW_LINES|wxDV_SINGLE); + + // Define names so the objects can be found using wxConfig. + m_frame->SetName("frame"); + m_list->SetName("dvlc"); + + // Add some columns to the DVLC. + m_list->AppendTextColumn(DVLC_COL "1", + wxDATAVIEW_CELL_INERT, -1, wxALIGN_LEFT, + wxDATAVIEW_COL_RESIZABLE | + wxDATAVIEW_COL_REORDERABLE | + wxDATAVIEW_COL_SORTABLE); + m_list->AppendTextColumn(DVLC_COL "2", + wxDATAVIEW_CELL_INERT, -1, wxALIGN_LEFT, + wxDATAVIEW_COL_RESIZABLE | + wxDATAVIEW_COL_REORDERABLE | + wxDATAVIEW_COL_SORTABLE); + + // Populate with DVLC data. + wxVector data; + + data.push_back("AAAA"); + data.push_back("BBBB"); + m_list->AppendItem(data); + + data.clear(); + data.push_back("CCCC"); + data.push_back("DDDD"); + m_list->AppendItem(data); + + data.clear(); + data.push_back("EEEE"); + data.push_back("FFFF"); + m_list->AppendItem(data); +} + +void PersistenceTestCase::tearDown() +{ + +} + +void PersistenceTestCase::FrameSaveTest() +{ + // Adjust the initial settings. + m_frame->SetPosition(wxPoint(100, 150)); + m_frame->SetSize(wxSize(450, 350)); + + // Get default managers. + wxPersistenceManager& PM = wxPersistenceManager::Get(); + wxConfigBase* conf = wxConfig::Get(); + + PM.Register(m_frame); + + // Destroy the frame immediately. + m_frame->wxWindowBase::Destroy(); + + // Test that the relevant keys have been stored correctly. + int val; + + CPPUNIT_ASSERT(conf->Read(DVLC_FRAME_PREFIX "/x", &val)); + CPPUNIT_ASSERT_EQUAL(100, val); + + CPPUNIT_ASSERT(conf->Read(DVLC_FRAME_PREFIX "/y", &val)); + CPPUNIT_ASSERT_EQUAL(150, val); + + CPPUNIT_ASSERT(conf->Read(DVLC_FRAME_PREFIX "/h", &val)); + CPPUNIT_ASSERT_EQUAL(350, val); + + CPPUNIT_ASSERT(conf->Read(DVLC_FRAME_PREFIX "/w", &val)); + CPPUNIT_ASSERT_EQUAL(450, val); + + CPPUNIT_ASSERT(conf->Read(DVLC_FRAME_PREFIX "/Iconized", &val)); + CPPUNIT_ASSERT_EQUAL(0, val); + + CPPUNIT_ASSERT(conf->Read(DVLC_FRAME_PREFIX "/Maximized", &val)); + CPPUNIT_ASSERT_EQUAL(0, val); +} + +void PersistenceTestCase::FrameRestoreTest() +{ + // Get default manager. + wxPersistenceManager& PM = wxPersistenceManager::Get(); + + // Test that the objects are registered and can be restored. + CPPUNIT_ASSERT(PM.RegisterAndRestore(m_frame)); + + CPPUNIT_ASSERT_EQUAL(100, m_frame->GetPosition().x); + CPPUNIT_ASSERT_EQUAL(150, m_frame->GetPosition().y); + CPPUNIT_ASSERT_EQUAL(350, m_frame->GetSize().GetHeight()); + CPPUNIT_ASSERT_EQUAL(450, m_frame->GetSize().GetWidth()); + CPPUNIT_ASSERT(!m_frame->IsMaximized()); + CPPUNIT_ASSERT(!m_frame->IsIconized()); +} + +void PersistenceTestCase::DVLCSaveTest() +{ + // Adjust the initial settings. + m_list->GetColumn(0)->SetWidth(150); + m_list->GetColumn(1)->SetWidth(250); + m_list->GetColumn(1)->SetSortOrder(false); + + // Get default managers. + wxPersistenceManager& PM = wxPersistenceManager::Get(); + wxConfigBase* conf = wxConfig::Get(); + + PM.Register(m_list); + + // Destroy the frame containing the DVLC immediately. + m_frame->wxWindowBase::Destroy(); + + // Test that the relevant keys have been stored correctly. + int val; + wxString text; + + CPPUNIT_ASSERT(conf->Read(DVLC_COL_PREFIX "1/Width", &val)); + CPPUNIT_ASSERT_EQUAL(150, val); + + CPPUNIT_ASSERT(conf->Read(DVLC_COL_PREFIX "2/Width", &val)); + CPPUNIT_ASSERT_EQUAL(250, val); + + CPPUNIT_ASSERT(conf->Read(DVLC_SORT_PREFIX "/Column", &text)); + CPPUNIT_ASSERT_EQUAL("Column #2", text); + + CPPUNIT_ASSERT(conf->Read(DVLC_SORT_PREFIX "/Asc", &val)); + CPPUNIT_ASSERT_EQUAL(0, val); +} + +void PersistenceTestCase::DVLCRestoreTest() +{ + // Get default manager. + wxPersistenceManager& PM = wxPersistenceManager::Get(); + + // Test that the objects are registered and can be restored. + CPPUNIT_ASSERT(PM.RegisterAndRestore(m_list)); + + // Test that the correct values were restored. + CPPUNIT_ASSERT_EQUAL(150, m_list->GetColumn(0)->GetWidth()); + CPPUNIT_ASSERT_EQUAL(250, m_list->GetColumn(1)->GetWidth()); + CPPUNIT_ASSERT(m_list->GetColumn(1)->IsSortKey()); + CPPUNIT_ASSERT(!m_list->GetColumn(1)->IsSortOrderAscending()); +} From 9d090fc9e903dc9f6faa93746e3fd2de3270757f Mon Sep 17 00:00:00 2001 From: iwbnwif Date: Wed, 23 Aug 2017 23:21:10 +0100 Subject: [PATCH 8/8] Rename all objects to 'DataViewCtrl' and 'DVC' instead of 'DataViewListCtrl' and 'DVLC' to reflect the change of implementation at the base class. --- include/wx/persist/dataview.h | 46 +++++++++++++++++------------------ tests/persist/persistence.cpp | 26 ++++++++++++-------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/include/wx/persist/dataview.h b/include/wx/persist/dataview.h index d0a1ad622f..1e099e8df2 100644 --- a/include/wx/persist/dataview.h +++ b/include/wx/persist/dataview.h @@ -1,6 +1,6 @@ /////////////////////////////////////////////////////////////////////////////// // Name: wx/persist/dataview.h -// Purpose: Persistence support for wxDataViewListCtrl +// Purpose: Persistence support for wxDataViewCtrl and its derivatives // Author: wxWidgets Team // Created: 2017-08-21 // Copyright: (c) 2017 wxWidgets.org @@ -14,20 +14,20 @@ #include "wx/dataview.h" // ---------------------------------------------------------------------------- -// String constants used by wxPersistentDataViewListCtrl. +// String constants used by wxPersistentDataViewCtrl. // ---------------------------------------------------------------------------- -#define wxPERSIST_DVLC_KIND "DataViewList" +#define wxPERSIST_DVC_KIND "DataView" -#define wxPERSIST_DVLC_COLUMNS "Columns" +#define wxPERSIST_DVC_COLUMNS "Columns" -#define wxPERSIST_DVLC_HIDDEN "Hidden" -#define wxPERSIST_DVLC_POS "Position" -#define wxPERSIST_DVLC_TITLE "Title" -#define wxPERSIST_DVLC_WIDTH "Width" +#define wxPERSIST_DVC_HIDDEN "Hidden" +#define wxPERSIST_DVC_POS "Position" +#define wxPERSIST_DVC_TITLE "Title" +#define wxPERSIST_DVC_WIDTH "Width" -#define wxPERSIST_DVLC_SORT_KEY "Sorting/Column" -#define wxPERSIST_DVLC_SORT_ASC "Sorting/Asc" +#define wxPERSIST_DVC_SORT_KEY "Sorting/Column" +#define wxPERSIST_DVC_SORT_ASC "Sorting/Asc" // ---------------------------------------------------------------------------- // Helper function to search for a column by its title. @@ -47,7 +47,7 @@ wxDataViewColumn* GetColumnByTitle(wxDataViewCtrl* control, } // ---------------------------------------------------------------------------- -// wxPersistentDataViewListCtrl: Saves and restores user modified column widths +// wxPersistentDataViewCtrl: Saves and restores user modified column widths // and single column sort order. // // Future improvements could be to save and restore column order if the user @@ -75,14 +75,14 @@ public: // Create a prefix string to identify each column. wxString columnPrefix; - columnPrefix.Printf("/%s/%s/", wxPERSIST_DVLC_COLUMNS, + columnPrefix.Printf("/%s/%s/", wxPERSIST_DVC_COLUMNS, column->GetTitle()); // Save the column attributes. - SaveValue(columnPrefix + wxPERSIST_DVLC_HIDDEN, column->IsHidden()); - SaveValue(columnPrefix + wxPERSIST_DVLC_POS, + SaveValue(columnPrefix + wxPERSIST_DVC_HIDDEN, column->IsHidden()); + SaveValue(columnPrefix + wxPERSIST_DVC_POS, control->GetColumnPosition(column)); - SaveValue(columnPrefix + wxPERSIST_DVLC_WIDTH, column->GetWidth()); + SaveValue(columnPrefix + wxPERSIST_DVC_WIDTH, column->GetWidth()); // Check if this column is the current sort key. if ( column->IsSortKey() ) @@ -97,8 +97,8 @@ public: // Save the sort key and direction if there is a valid sort. if ( sortColumn ) { - SaveValue(wxPERSIST_DVLC_SORT_KEY, sortColumn->GetTitle()); - SaveValue(wxPERSIST_DVLC_SORT_ASC, + SaveValue(wxPERSIST_DVC_SORT_KEY, sortColumn->GetTitle()); + SaveValue(wxPERSIST_DVC_SORT_ASC, sortColumn->IsSortOrderAscending()); } } @@ -117,17 +117,17 @@ public: // 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, + columnPrefix.Printf("/%s/%s/", wxPERSIST_DVC_COLUMNS, column->GetTitle()); // Restore column hidden status. bool hidden; - if ( RestoreValue(columnPrefix + wxPERSIST_DVLC_HIDDEN, &hidden) ) + if ( RestoreValue(columnPrefix + wxPERSIST_DVC_HIDDEN, &hidden) ) column->SetHidden(hidden); // Restore the column width. int width; - if ( RestoreValue(columnPrefix + wxPERSIST_DVLC_WIDTH, &width) ) + if ( RestoreValue(columnPrefix + wxPERSIST_DVC_WIDTH, &width) ) column->SetWidth(width); // TODO: Set the column's view position. @@ -137,7 +137,7 @@ public: // criteria. wxString sortColumn; if ( control->GetModel() && - RestoreValue(wxPERSIST_DVLC_SORT_KEY, &sortColumn) && + RestoreValue(wxPERSIST_DVC_SORT_KEY, &sortColumn) && sortColumn != "" ) { bool sortAsc = true; @@ -145,7 +145,7 @@ public: if ( column ) { - RestoreValue(wxPERSIST_DVLC_SORT_ASC, &sortAsc); + RestoreValue(wxPERSIST_DVC_SORT_ASC, &sortAsc); column->SetSortOrder(sortAsc); // Resort the control based on the new sort criteria. @@ -157,7 +157,7 @@ public: virtual wxString GetKind() const wxOVERRIDE { - return wxPERSIST_DVLC_KIND; + return wxPERSIST_DVC_KIND; } }; diff --git a/tests/persist/persistence.cpp b/tests/persist/persistence.cpp index 2be909b01c..492633d591 100644 --- a/tests/persist/persistence.cpp +++ b/tests/persist/persistence.cpp @@ -6,11 +6,15 @@ // Copyright: (c) 2017 wxWidgets Team /////////////////////////////////////////////////////////////////////////////// +// Note: The wxDataViewCtrl test currently uses the derivative class +// wxDataViewListCtrl for convenience. + // ---------------------------------------------------------------------------- // headers // ---------------------------------------------------------------------------- #include "testprec.h" + #include #ifdef __BORLANDC__ #pragma hdrstop @@ -31,7 +35,7 @@ #define APP_NAME "cpptest" #define PO_PREFIX "/Persistent_Options" -#define DVLC_PREFIX PO_PREFIX "/DataViewList/dvlc" +#define DVLC_PREFIX PO_PREFIX "/DataView/dvlc" #define DVLC_COL "Column #" #define DVLC_COL_PREFIX DVLC_PREFIX "/Columns/" DVLC_COL #define DVLC_SORT_PREFIX DVLC_PREFIX "/Sorting" @@ -44,10 +48,20 @@ class PersistenceTestCase : public CppUnit::TestCase { public: - PersistenceTestCase() {} + PersistenceTestCase() + { + suite_setUp(); + } virtual void setUp(); virtual void tearDown(); + + void suite_setUp() + { + wxTheApp->SetAppName("PersistTest"); + wxConfig::Get()->DeleteGroup("/Persistent_Options"); + wxConfig::Get()->Flush(); + } private: CPPUNIT_TEST_SUITE( PersistenceTestCase ); @@ -76,14 +90,6 @@ CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( PersistenceTestCase, "PersistenceTestCase void PersistenceTestCase::setUp() { - wxConfigBase* conf = wxConfig::Get(); - conf->SetAppName("PersistTest"); - conf->SetVendorName("wxWidgets"); - - // Clear any pre-existing settings. - conf->DeleteGroup("/Persistence_Options"); - conf->Flush(); - // Create the objects to persist. m_frame = new wxFrame(wxTheApp->GetTopWindow(), wxID_ANY, "Persistence Test",