mymodels.h 7.4 KB

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