]>
Commit | Line | Data |
---|---|---|
03dfc8f5 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: mymodels.h | |
3 | // Purpose: wxDataViewCtrl wxWidgets sample | |
4 | // Author: Robert Roebling | |
5 | // Modified by: Francesco Montorsi, Bo Yang | |
6 | // Created: 06/01/06 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Robert Roebling | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | ||
13 | // ---------------------------------------------------------------------------- | |
14 | // MyMusicTreeModelNode: a node inside MyMusicTreeModel | |
15 | // ---------------------------------------------------------------------------- | |
16 | ||
17 | class MyMusicTreeModelNode; | |
18 | WX_DEFINE_ARRAY_PTR( MyMusicTreeModelNode*, MyMusicTreeModelNodePtrArray ); | |
19 | ||
20 | class MyMusicTreeModelNode | |
21 | { | |
22 | public: | |
23 | MyMusicTreeModelNode( MyMusicTreeModelNode* parent, | |
ecad59d8 | 24 | const wxString &title, const wxString &artist, |
03dfc8f5 VZ |
25 | unsigned int year ) |
26 | { | |
27 | m_parent = parent; | |
28 | ||
29 | m_title = title; | |
30 | m_artist = artist; | |
31 | m_year = year; | |
32 | m_quality = "good"; | |
ecad59d8 FM |
33 | |
34 | m_container = false; | |
03dfc8f5 VZ |
35 | } |
36 | ||
37 | MyMusicTreeModelNode( MyMusicTreeModelNode* parent, | |
38 | const wxString &branch ) | |
39 | { | |
40 | m_parent = parent; | |
41 | ||
42 | m_title = branch; | |
43 | m_year = -1; | |
ecad59d8 FM |
44 | |
45 | m_container = true; | |
03dfc8f5 VZ |
46 | } |
47 | ||
48 | ~MyMusicTreeModelNode() | |
49 | { | |
50 | // free all our children nodes | |
51 | size_t count = m_children.GetCount(); | |
52 | for (size_t i = 0; i < count; i++) | |
53 | { | |
54 | MyMusicTreeModelNode *child = m_children[i]; | |
55 | delete child; | |
56 | } | |
57 | } | |
58 | ||
59 | bool IsContainer() const | |
ecad59d8 | 60 | { return m_container; } |
03dfc8f5 VZ |
61 | |
62 | MyMusicTreeModelNode* GetParent() | |
63 | { return m_parent; } | |
64 | MyMusicTreeModelNodePtrArray& GetChildren() | |
65 | { return m_children; } | |
66 | MyMusicTreeModelNode* GetNthChild( unsigned int n ) | |
67 | { return m_children.Item( n ); } | |
68 | void Insert( MyMusicTreeModelNode* child, unsigned int n) | |
69 | { m_children.Insert( child, n); } | |
70 | void Append( MyMusicTreeModelNode* child ) | |
71 | { m_children.Add( child ); } | |
72 | unsigned int GetChildCount() const | |
73 | { return m_children.GetCount(); } | |
74 | ||
75 | public: // public to avoid getters/setters | |
76 | wxString m_title; | |
77 | wxString m_artist; | |
78 | int m_year; | |
79 | wxString m_quality; | |
80 | ||
ecad59d8 FM |
81 | // TODO/FIXME: |
82 | // the GTK version of wxDVC (in particular wxDataViewCtrlInternal::ItemAdded) | |
83 | // needs to know in advance if a node is or _will be_ a container. | |
84 | // Thus implementing: | |
85 | // bool IsContainer() const | |
86 | // { return m_children.GetCount()>0; } | |
87 | // doesn't work with wxGTK when MyMusicTreeModel::AddToClassical is called | |
88 | // AND the classical node was removed (a new node temporary without children | |
89 | // would be added to the control) | |
90 | bool m_container; | |
91 | ||
03dfc8f5 VZ |
92 | private: |
93 | MyMusicTreeModelNode *m_parent; | |
94 | MyMusicTreeModelNodePtrArray m_children; | |
95 | }; | |
96 | ||
97 | ||
98 | // ---------------------------------------------------------------------------- | |
99 | // MyMusicTreeModel | |
100 | // ---------------------------------------------------------------------------- | |
101 | ||
102 | /* | |
103 | Implement this data model | |
104 | Title Artist Year Judgement | |
105 | -------------------------------------------------------------------------- | |
106 | 1: My Music: | |
107 | 2: Pop music | |
108 | 3: You are not alone Michael Jackson 1995 good | |
109 | 4: Take a bow Madonna 1994 good | |
110 | 5: Classical music | |
111 | 6: Ninth Symphony Ludwig v. Beethoven 1824 good | |
112 | 7: German Requiem Johannes Brahms 1868 good | |
113 | */ | |
114 | ||
115 | class MyMusicTreeModel: public wxDataViewModel | |
116 | { | |
117 | public: | |
118 | MyMusicTreeModel(); | |
119 | ~MyMusicTreeModel() | |
120 | { | |
121 | delete m_root; | |
122 | } | |
123 | ||
124 | // helper method for wxLog | |
125 | ||
126 | wxString GetTitle( const wxDataViewItem &item ) const; | |
ecc32226 | 127 | wxString GetArtist( const wxDataViewItem &item ) const; |
03dfc8f5 VZ |
128 | int GetYear( const wxDataViewItem &item ) const; |
129 | ||
130 | // helper methods to change the model | |
131 | ||
ecad59d8 | 132 | void AddToClassical( const wxString &title, const wxString &artist, |
03dfc8f5 VZ |
133 | unsigned int year ); |
134 | void Delete( const wxDataViewItem &item ); | |
135 | ||
136 | wxDataViewItem GetNinthItem() const | |
137 | { | |
138 | return wxDataViewItem( m_ninth ); | |
139 | } | |
140 | ||
141 | // override sorting to always sort branches ascendingly | |
142 | ||
143 | int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2, | |
a82e28dd | 144 | unsigned int column, bool ascending ) const; |
03dfc8f5 VZ |
145 | |
146 | // implementation of base class virtuals to define model | |
147 | ||
148 | virtual unsigned int GetColumnCount() const | |
149 | { | |
150 | return 6; | |
151 | } | |
152 | ||
153 | virtual wxString GetColumnType( unsigned int col ) const | |
154 | { | |
155 | if (col == 2) | |
156 | return wxT("long"); | |
157 | ||
158 | return wxT("string"); | |
159 | } | |
160 | ||
161 | virtual void GetValue( wxVariant &variant, | |
162 | const wxDataViewItem &item, unsigned int col ) const; | |
163 | virtual bool SetValue( const wxVariant &variant, | |
164 | const wxDataViewItem &item, unsigned int col ); | |
165 | ||
166 | virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const; | |
167 | virtual bool IsContainer( const wxDataViewItem &item ) const; | |
ecad59d8 | 168 | virtual unsigned int GetChildren( const wxDataViewItem &parent, |
03dfc8f5 VZ |
169 | wxDataViewItemArray &array ) const; |
170 | ||
171 | private: | |
172 | MyMusicTreeModelNode* m_root; | |
173 | ||
174 | // pointers to some "special" nodes of the tree: | |
175 | MyMusicTreeModelNode* m_pop; | |
176 | MyMusicTreeModelNode* m_classical; | |
177 | MyMusicTreeModelNode* m_ninth; | |
178 | ||
179 | // ?? | |
180 | bool m_classicalMusicIsKnownToControl; | |
181 | }; | |
182 | ||
183 | ||
184 | // ---------------------------------------------------------------------------- | |
185 | // MyListModel | |
186 | // ---------------------------------------------------------------------------- | |
187 | ||
188 | class MyListModel: public wxDataViewVirtualListModel | |
189 | { | |
190 | public: | |
2746bccf VZ |
191 | enum |
192 | { | |
193 | Col_EditableText, | |
194 | Col_IconText, | |
195 | Col_TextWithAttr, | |
ef6833f9 | 196 | Col_Custom, |
2746bccf VZ |
197 | Col_Max |
198 | }; | |
199 | ||
03dfc8f5 VZ |
200 | MyListModel(); |
201 | ||
202 | // helper methods to change the model | |
203 | ||
204 | void Prepend( const wxString &text ); | |
205 | void DeleteItem( const wxDataViewItem &item ); | |
206 | void DeleteItems( const wxDataViewItemArray &items ); | |
207 | void AddMany(); | |
208 | ||
209 | ||
210 | // implementation of base class virtuals to define model | |
211 | ||
212 | virtual unsigned int GetColumnCount() const | |
213 | { | |
2746bccf | 214 | return Col_Max; |
03dfc8f5 VZ |
215 | } |
216 | ||
217 | virtual wxString GetColumnType( unsigned int col ) const | |
218 | { | |
2746bccf | 219 | if (col == Col_IconText) |
03dfc8f5 VZ |
220 | return wxT("wxDataViewIconText"); |
221 | ||
222 | return wxT("string"); | |
223 | } | |
224 | ||
225 | virtual unsigned int GetRowCount() | |
226 | { | |
205bdf20 | 227 | return m_textColValues.GetCount(); |
03dfc8f5 VZ |
228 | } |
229 | ||
230 | virtual void GetValueByRow( wxVariant &variant, | |
231 | unsigned int row, unsigned int col ) const; | |
7fadac88 VZ |
232 | virtual bool GetAttrByRow( unsigned int row, unsigned int col, |
233 | wxDataViewItemAttr &attr ) const; | |
03dfc8f5 VZ |
234 | virtual bool SetValueByRow( const wxVariant &variant, |
235 | unsigned int row, unsigned int col ); | |
236 | ||
237 | private: | |
205bdf20 VZ |
238 | wxArrayString m_textColValues; |
239 | wxArrayString m_iconColValues; | |
03dfc8f5 VZ |
240 | wxIcon m_icon[2]; |
241 | int m_virtualItems; | |
242 | }; | |
243 |