File: dataview.h

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (175 lines) | stat: -rw-r--r-- 6,198 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
///////////////////////////////////////////////////////////////////////////////
// Name:        wx/persist/dataview.h
// Purpose:     Persistence support for wxDataViewCtrl and its derivatives
// 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"

#if wxUSE_DATAVIEWCTRL

#include "wx/dataview.h"

// ----------------------------------------------------------------------------
// String constants used by wxPersistentDataViewCtrl.
// ----------------------------------------------------------------------------

#define wxPERSIST_DVC_KIND "DataView"

#define wxPERSIST_DVC_HIDDEN "Hidden"
#define wxPERSIST_DVC_POS "Position"
#define wxPERSIST_DVC_TITLE "Title"
#define wxPERSIST_DVC_WIDTH "Width"

#define wxPERSIST_DVC_SORT_KEY "Sorting/Column"
#define wxPERSIST_DVC_SORT_ASC "Sorting/Asc"

// ----------------------------------------------------------------------------
// 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
// has changed it and multicolumn sorts.
// ----------------------------------------------------------------------------

class wxPersistentDataViewCtrl : public wxPersistentWindow<wxDataViewCtrl>
{
public:
    wxPersistentDataViewCtrl(wxDataViewCtrl* control)
        : wxPersistentWindow<wxDataViewCtrl>(control)
    {
    }

    virtual void Save() const wxOVERRIDE
    {
        wxDataViewCtrl* const control = Get();

        const wxDataViewColumn* sortColumn = NULL;

        for ( unsigned int col = 0; col < control->GetColumnCount(); col++ )
        {
            const wxDataViewColumn* const column = control->GetColumn(col);

            // Create a prefix string to identify each column.
            const wxString columnPrefix = MakeColumnPrefix(column);

            // Save the column attributes.
            SaveValue(columnPrefix + wxASCII_STR(wxPERSIST_DVC_HIDDEN), column->IsHidden());
            SaveValue(columnPrefix + wxASCII_STR(wxPERSIST_DVC_POS),
                      control->GetColumnPosition(column));

            // We take special care to save only the specified width instead of
            // the currently used one. Usually they're one and the same, but
            // they can be different for the last column, whose size can be
            // greater than specified, as it's always expanded to fill the
            // entire control width.
            const int width = column->WXGetSpecifiedWidth();
            if ( width > 0 )
                SaveValue(columnPrefix + wxASCII_STR(wxPERSIST_DVC_WIDTH), width);

            // Check if this column is the current sort key.
            if ( column->IsSortKey() )
                sortColumn = column;
        }

        // Note: The current implementation does not save and restore multi-
        // column sort keys.
        if ( control->IsMultiColumnSortAllowed() )
            return;

        // Save the sort key and direction if there is a valid sort.
        if ( sortColumn )
        {
            SaveValue(wxASCII_STR(wxPERSIST_DVC_SORT_KEY), sortColumn->GetTitle());
            SaveValue(wxASCII_STR(wxPERSIST_DVC_SORT_ASC),
                      sortColumn->IsSortOrderAscending());
        }
    }

    virtual bool Restore() wxOVERRIDE
    {
        wxDataViewCtrl* const control = Get();

        for ( unsigned int col = 0; col < control->GetColumnCount(); col++ )
        {
            wxDataViewColumn* const column = control->GetColumn(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.
            const wxString columnPrefix = MakeColumnPrefix(column);

            // Restore column hidden status.
            bool hidden;
            if ( RestoreValue(columnPrefix + wxASCII_STR(wxPERSIST_DVC_HIDDEN), &hidden) )
                column->SetHidden(hidden);

            // Restore the column width.
            int width;
            if ( RestoreValue(columnPrefix + wxASCII_STR(wxPERSIST_DVC_WIDTH), &width) )
                column->SetWidth(width);

            // TODO: Set the column's view position.
        }

        // Restore the sort key and order if there is a valid model and sort
        // criteria.
        wxString sortColumn;
        if ( control->GetModel() &&
             RestoreValue(wxASCII_STR(wxPERSIST_DVC_SORT_KEY), &sortColumn) &&
             !sortColumn.empty() )
        {
            bool sortAsc = true;
            if ( wxDataViewColumn* column = GetColumnByTitle(control, sortColumn) )
            {
                RestoreValue(wxASCII_STR(wxPERSIST_DVC_SORT_ASC), &sortAsc);
                column->SetSortOrder(sortAsc);

                // Resort the control based on the new sort criteria.
                control->GetModel()->Resort();
            }
        }

        return true;
    }

    virtual wxString GetKind() const wxOVERRIDE
    {
        return wxASCII_STR(wxPERSIST_DVC_KIND);
    }

private:
    // Return a (slash-terminated) prefix for the column-specific entries.
    static wxString MakeColumnPrefix(const wxDataViewColumn* column)
    {
        return wxString::Format(wxASCII_STR("/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)
{
    return new wxPersistentDataViewCtrl(control);
}

#endif // wxUSE_DATAVIEWCTRL

#endif // _WX_PERSIST_DATAVIEW_H_