| 1 |
////////////////////////////////////////////////////////////////////// |
| 2 |
// This file was auto-generated by codelite's wxCrafter Plugin |
| 3 |
// Do not modify this file by hand! |
| 4 |
////////////////////////////////////////////////////////////////////// |
| 5 |
|
| 6 |
#include "my_tree_list_model.hpp" |
| 7 |
#include <wx/dvrenderers.h> |
| 8 |
#include <wx/variant.h> |
| 9 |
|
| 10 |
////////////////////////////////////////// |
| 11 |
// Implementation |
| 12 |
////////////////////////////////////////// |
| 13 |
|
| 14 |
// ------------------------------------------------- |
| 15 |
// Help method |
| 16 |
// ------------------------------------------------- |
| 17 |
wxVariant MODEL_NAME::CreateIconTextVariant(const wxString &text, const wxBitmap& bmp) |
| 18 |
{ |
| 19 |
wxIcon icn; |
| 20 |
icn.CopyFromBitmap( bmp); |
| 21 |
wxDataViewIconText ict(text, icn); |
| 22 |
wxVariant v; |
| 23 |
v << ict; |
| 24 |
return v; |
| 25 |
} |
| 26 |
|
| 27 |
// ------------------------------------------------- |
| 28 |
// The model class |
| 29 |
// ------------------------------------------------- |
| 30 |
|
| 31 |
MODEL_NAME::MODEL_NAME() |
| 32 |
: m_colCount(0) |
| 33 |
{ |
| 34 |
} |
| 35 |
|
| 36 |
MODEL_NAME::~MODEL_NAME() |
| 37 |
{ |
| 38 |
for(size_t i=0; i<m_data.size(); ++i) { |
| 39 |
wxDELETE(m_data.at(i)); |
| 40 |
} |
| 41 |
m_data.clear(); |
| 42 |
} |
| 43 |
|
| 44 |
unsigned int MODEL_NAME::GetChildren(const wxDataViewItem& item, wxDataViewItemArray& children) const |
| 45 |
{ |
| 46 |
if(item.GetID() == NULL) { |
| 47 |
// Root |
| 48 |
for(size_t i=0; i<m_data.size(); ++i) { |
| 49 |
children.Add( wxDataViewItem( m_data.at(i) ) ); |
| 50 |
} |
| 51 |
return children.size(); |
| 52 |
} |
| 53 |
|
| 54 |
children.Clear(); |
| 55 |
MODEL_NAME_Item* node = reinterpret_cast<MODEL_NAME_Item*>(item.m_pItem); |
| 56 |
if ( node ) { |
| 57 |
for(size_t i=0; i<node->GetChildren().size(); ++i) { |
| 58 |
children.Add( wxDataViewItem( node->GetChildren().at(i) ) ); |
| 59 |
} |
| 60 |
} |
| 61 |
return children.GetCount(); |
| 62 |
} |
| 63 |
|
| 64 |
unsigned int MODEL_NAME::GetColumnCount() const |
| 65 |
{ |
| 66 |
return m_colCount; |
| 67 |
} |
| 68 |
|
| 69 |
wxString MODEL_NAME::GetColumnType(unsigned int col) const |
| 70 |
{ |
| 71 |
if ( !m_data.empty() && m_data.at(0)->GetData().size() > col ) { |
| 72 |
return m_data.at(0)->GetData().at(col).GetType(); |
| 73 |
} |
| 74 |
return "string"; |
| 75 |
} |
| 76 |
|
| 77 |
wxDataViewItem MODEL_NAME::GetParent(const wxDataViewItem& item) const |
| 78 |
{ |
| 79 |
if ( IsEmpty() ) { |
| 80 |
return wxDataViewItem(NULL); |
| 81 |
} |
| 82 |
|
| 83 |
MODEL_NAME_Item* node = reinterpret_cast<MODEL_NAME_Item*>(item.m_pItem); |
| 84 |
if ( node ) { |
| 85 |
return wxDataViewItem(node->GetParent()); |
| 86 |
} |
| 87 |
return wxDataViewItem(NULL); |
| 88 |
} |
| 89 |
|
| 90 |
bool MODEL_NAME::IsContainer(const wxDataViewItem& item) const |
| 91 |
{ |
| 92 |
MODEL_NAME_Item* node = reinterpret_cast<MODEL_NAME_Item*>(item.m_pItem); |
| 93 |
if ( node ) { |
| 94 |
return node->IsContainer(); |
| 95 |
} |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
void MODEL_NAME::GetValue(wxVariant& variant, const wxDataViewItem& item, unsigned int col) const |
| 100 |
{ |
| 101 |
MODEL_NAME_Item* node = reinterpret_cast<MODEL_NAME_Item*>(item.m_pItem); |
| 102 |
if ( node && node->GetData().size() > col ) { |
| 103 |
variant = node->GetData().at(col); |
| 104 |
} |
| 105 |
} |
| 106 |
wxDataViewItem MODEL_NAME::DoAppendItem(const wxDataViewItem& parent, const wxVector<wxVariant>& data, bool isContainer, wxClientData *clientData) |
| 107 |
{ |
| 108 |
MODEL_NAME_Item* parentNode = reinterpret_cast<MODEL_NAME_Item*>(parent.m_pItem); |
| 109 |
DoChangeItemType(parent, true); |
| 110 |
|
| 111 |
MODEL_NAME_Item* child = new MODEL_NAME_Item(); |
| 112 |
child->SetIsContainer(isContainer); |
| 113 |
child->SetClientObject( clientData ); |
| 114 |
child->SetData( data ); |
| 115 |
if ( parentNode ) { |
| 116 |
parentNode->AddChild( child ); |
| 117 |
|
| 118 |
} else { |
| 119 |
m_data.push_back( child ); |
| 120 |
} |
| 121 |
|
| 122 |
return wxDataViewItem(child); |
| 123 |
} |
| 124 |
|
| 125 |
wxDataViewItem MODEL_NAME::DoInsertItem(const wxDataViewItem& insertBeforeMe, const wxVector<wxVariant>& data, bool isContainer, wxClientData *clientData) |
| 126 |
{ |
| 127 |
MODEL_NAME_Item* child = new MODEL_NAME_Item(); |
| 128 |
child->SetIsContainer(isContainer); |
| 129 |
child->SetClientObject( clientData ); |
| 130 |
child->SetData( data ); |
| 131 |
|
| 132 |
// find the location where to insert the new item |
| 133 |
MODEL_NAME_Item* node = reinterpret_cast<MODEL_NAME_Item*>(insertBeforeMe.m_pItem); |
| 134 |
if ( !node ) |
| 135 |
return wxDataViewItem(); |
| 136 |
|
| 137 |
wxVector<MODEL_NAME_Item*>::iterator where = std::find(m_data.begin(), m_data.end(), node); |
| 138 |
|
| 139 |
if ( where != m_data.end() ) { |
| 140 |
// top level item |
| 141 |
m_data.insert( where, child ); |
| 142 |
|
| 143 |
} else { |
| 144 |
|
| 145 |
if ( !node->GetParent() ) |
| 146 |
return wxDataViewItem(); |
| 147 |
|
| 148 |
child->SetParent(node->GetParent()); |
| 149 |
where = std::find(node->GetParent()->GetChildren().begin(), node->GetParent()->GetChildren().end(), node); |
| 150 |
if ( where == node->GetParent()->GetChildren().end() ) { |
| 151 |
node->GetParent()->GetChildren().push_back( child ); |
| 152 |
|
| 153 |
} else { |
| 154 |
node->GetParent()->GetChildren().insert(where, child); |
| 155 |
|
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
return wxDataViewItem(child); |
| 160 |
} |
| 161 |
|
| 162 |
wxDataViewItem MODEL_NAME::AppendItem(const wxDataViewItem &parent, const wxVector<wxVariant>& data, wxClientData *clientData) |
| 163 |
{ |
| 164 |
wxDataViewItem ch = DoAppendItem(parent, data, false, clientData); |
| 165 |
ItemAdded(parent, ch); |
| 166 |
return ch; |
| 167 |
} |
| 168 |
|
| 169 |
wxDataViewItemArray MODEL_NAME::AppendItems(const wxDataViewItem &parent, const wxVector<wxVector<wxVariant> >& data) |
| 170 |
{ |
| 171 |
wxDataViewItemArray items; |
| 172 |
for(size_t i=0; i<data.size(); ++i) { |
| 173 |
items.push_back( DoAppendItem(parent, data.at(i), false, NULL) ); |
| 174 |
} |
| 175 |
ItemsAdded(parent, items); |
| 176 |
return items; |
| 177 |
} |
| 178 |
|
| 179 |
bool MODEL_NAME::SetValue(const wxVariant& variant, const wxDataViewItem& item, unsigned int col) |
| 180 |
{ |
| 181 |
MODEL_NAME_Item* node = reinterpret_cast<MODEL_NAME_Item*>(item.m_pItem); |
| 182 |
if ( node && node->GetData().size() > col ) { |
| 183 |
node->GetData().at(col) = variant; |
| 184 |
} |
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
void MODEL_NAME::DeleteItem(const wxDataViewItem& item) |
| 189 |
{ |
| 190 |
MODEL_NAME_Item* node = reinterpret_cast<MODEL_NAME_Item*>(item.m_pItem); |
| 191 |
if ( node ) { |
| 192 |
|
| 193 |
MODEL_NAME_Item* parent = node->GetParent(); |
| 194 |
wxDataViewItem parentItem(parent); |
| 195 |
ItemDeleted(parentItem, item); |
| 196 |
|
| 197 |
// this will also remove it from its model parent children list |
| 198 |
if ( parent == NULL ) { |
| 199 |
// root item, remove it from the roots array |
| 200 |
wxVector<MODEL_NAME_Item*>::iterator where = std::find(m_data.begin(), m_data.end(), node); |
| 201 |
if ( where != m_data.end() ) { |
| 202 |
m_data.erase(where); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
// If there are no more children, change the item back to 'normal' |
| 207 |
if ( parent && parent->GetChildren().empty() ) |
| 208 |
DoChangeItemType(parentItem, false); |
| 209 |
|
| 210 |
wxDELETE(node); |
| 211 |
} |
| 212 |
|
| 213 |
if ( IsEmpty() ) |
| 214 |
Cleared(); |
| 215 |
} |
| 216 |
|
| 217 |
void MODEL_NAME::DeleteItems(const wxDataViewItem& parent, const wxDataViewItemArray& items) |
| 218 |
{ |
| 219 |
// sanity |
| 220 |
for(size_t i=0; i<items.GetCount(); ++i) { |
| 221 |
MODEL_NAME_Item* node = reinterpret_cast<MODEL_NAME_Item*>(items.Item(i).m_pItem); |
| 222 |
wxUnusedVar(node); |
| 223 |
wxASSERT(node && node->GetParent() == parent.m_pItem); |
| 224 |
DeleteItem(items.Item(i)); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
void MODEL_NAME::Clear() |
| 229 |
{ |
| 230 |
wxVector<MODEL_NAME_Item*> roots = m_data; |
| 231 |
wxVector<MODEL_NAME_Item*>::iterator iter = roots.begin(); |
| 232 |
for(; iter != roots.end(); ++iter) { |
| 233 |
DeleteItem( wxDataViewItem(*iter) ); |
| 234 |
} |
| 235 |
m_data.clear(); |
| 236 |
Cleared(); |
| 237 |
} |
| 238 |
|
| 239 |
bool MODEL_NAME::IsEmpty() const |
| 240 |
{ |
| 241 |
return m_data.empty(); |
| 242 |
} |
| 243 |
|
| 244 |
wxClientData* MODEL_NAME::GetClientObject(const wxDataViewItem& item) const |
| 245 |
{ |
| 246 |
MODEL_NAME_Item* node = reinterpret_cast<MODEL_NAME_Item*>(item.GetID()); |
| 247 |
if ( node ) { |
| 248 |
return node->GetClientObject(); |
| 249 |
} |
| 250 |
return NULL; |
| 251 |
} |
| 252 |
|
| 253 |
void MODEL_NAME::SetClientObject(const wxDataViewItem& item, wxClientData *data) |
| 254 |
{ |
| 255 |
MODEL_NAME_Item* node = reinterpret_cast<MODEL_NAME_Item*>(item.GetID()); |
| 256 |
if ( node ) { |
| 257 |
node->SetClientObject(data); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
void MODEL_NAME::UpdateItem(const wxDataViewItem& item, const wxVector<wxVariant>& data) |
| 262 |
{ |
| 263 |
MODEL_NAME_Item* node = reinterpret_cast<MODEL_NAME_Item*>(item.GetID()); |
| 264 |
if ( node ) { |
| 265 |
node->SetData( data ); |
| 266 |
ItemChanged( item ); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
wxDataViewItem MODEL_NAME::InsertItem(const wxDataViewItem& insertBeforeMe, const wxVector<wxVariant>& data, wxClientData *clientData) |
| 271 |
{ |
| 272 |
wxDataViewItem ch = DoInsertItem(insertBeforeMe, data, false, clientData); |
| 273 |
if ( ch.IsOk() ) { |
| 274 |
MODEL_NAME_Item* node = reinterpret_cast<MODEL_NAME_Item*>(ch.GetID()); |
| 275 |
ItemAdded(wxDataViewItem(node->GetParent()), ch); |
| 276 |
} |
| 277 |
return ch; |
| 278 |
} |
| 279 |
|
| 280 |
wxVector<wxVariant> MODEL_NAME::GetItemColumnsData(const wxDataViewItem& item) const |
| 281 |
{ |
| 282 |
if ( !item.IsOk() ) |
| 283 |
return wxVector<wxVariant>(); |
| 284 |
|
| 285 |
MODEL_NAME_Item* node = reinterpret_cast<MODEL_NAME_Item*>(item.GetID()); |
| 286 |
if ( !node ) { |
| 287 |
return wxVector<wxVariant>(); |
| 288 |
} |
| 289 |
return node->GetData(); |
| 290 |
} |
| 291 |
|
| 292 |
bool MODEL_NAME::HasChildren(const wxDataViewItem& item) const |
| 293 |
{ |
| 294 |
if ( !item.IsOk() ) |
| 295 |
return false; |
| 296 |
|
| 297 |
MODEL_NAME_Item* node = reinterpret_cast<MODEL_NAME_Item*>(item.GetID()); |
| 298 |
if ( !node ) { |
| 299 |
return false; |
| 300 |
} |
| 301 |
return !node->GetChildren().empty(); |
| 302 |
} |
| 303 |
|
| 304 |
void MODEL_NAME::DoChangeItemType(const wxDataViewItem& item, bool changeToContainer) |
| 305 |
{ |
| 306 |
MODEL_NAME_Item* node = reinterpret_cast<MODEL_NAME_Item*>(item.GetID()); |
| 307 |
if ( !node ) |
| 308 |
return; |
| 309 |
|
| 310 |
if ( ( changeToContainer && !node->IsContainer()) || // change an item from non-container to container type |
| 311 |
( !changeToContainer && node->IsContainer()) ) { // change an item from container to non-container type |
| 312 |
#if defined(__WXGTK__) || defined(__WXMAC__) |
| 313 |
// change the item to container type: |
| 314 |
// 1st we need to delete it |
| 315 |
ItemDeleted(wxDataViewItem(node->GetParent()), item); |
| 316 |
|
| 317 |
// update the node type |
| 318 |
node->SetIsContainer(changeToContainer); |
| 319 |
ItemAdded(wxDataViewItem(node->GetParent()), item); |
| 320 |
#else |
| 321 |
node->SetIsContainer(changeToContainer); |
| 322 |
#endif |
| 323 |
} |
| 324 |
} |