tbx  0.7.6
viewitems.h
1 /*
2  * tbx RISC OS toolbox library
3  *
4  * Copyright (C) 2010 Alan Buckley All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 /*
26  * viewitems.h
27  *
28  * Created on: 27 Apr 2010
29  * Author: alanb
30  */
31 
32 #ifndef TBX_VIEWITEMS_H_
33 #define TBX_VIEWITEMS_H_
34 
35 #include <vector>
36 #include "itemview.h"
37 
38 namespace tbx
39 {
40 namespace view
41 {
42 
48 template<class T> class ViewItems
49 {
50  std::vector<T> _items;
51  ItemView *_view;
52 
53 public:
59  ViewItems(ItemView *v = 0) : _view(v) {}
60 
64  void view(ItemView *v)
65  {
66  if (_view) _view->cleared();
67  _view = v;
68  if (v)
69  {
70  v->cleared();
71  if (_items.size()) v->inserted(0, _items.size());
72  }
73  }
74 
78  unsigned int size() const {return _items.size();}
79 
83  void item(unsigned int index, const T&item)
84  {
85  if (_view) _view->changing(index,1);
86  _items[index] = item;
87  if (_view) _view->changed(index,1);
88  }
89 
93  const T &item(unsigned int index) const
94  {
95  return _items[index];
96  }
97 
101  const T &operator[](unsigned int index) const
102  {
103  return _items[index];
104  }
105 
114  void changing(unsigned int index, unsigned int how_many = 1)
115  {
116  if (_view) _view->changing(index, how_many);
117  }
118 
127  void changed(unsigned int index, unsigned int how_many = 1)
128  {
129  if (_view) _view->changed(index, how_many);
130  }
131 
137  void add(const T& item)
138  {
139  _items.push_back(item);
140  if (_view) _view->inserted(_items.size()-1, 1);
141  }
142 
150  void insert(unsigned int index, const T&item)
151  {
152  if (index == _items.size()) add(item);
153  else
154  {
155  _items.insert(_items.begin() + index, item);
156  if (_view) _view->inserted(index, 1);
157  }
158  }
159 
163  void erase(unsigned int index)
164  {
165  if (_view) _view->removing(index, 1);
166  _items.erase(_items.begin() + index);
167  if (_view) _view->removed(index, 1);
168  }
169 
173  void clear()
174  {
175  _items.clear();
176  if (_view) _view->cleared();
177  }
178 };
179 
180 
181 }
182 }
183 
184 #endif /* TBX_VIEWITEMS_H_ */
tbx::view::ViewItems::item
void item(unsigned int index, const T &item)
Set the item at a specified index.
Definition: viewitems.h:83
tbx
A library for creating RISC OS toolbox applications.
Definition: abouttobeshownlistener.cc:35
tbx::view::ViewItems::item
const T & item(unsigned int index) const
Get the item at the specified index.
Definition: viewitems.h:93
tbx::view::ItemView::inserted
virtual void inserted(unsigned int where, unsigned int how_many)=0
Inform the view that items have been inserted.
tbx::view::ViewItems::size
unsigned int size() const
Return size of items.
Definition: viewitems.h:78
tbx::view::ItemView::removed
virtual void removed(unsigned int where, unsigned int how_many)=0
Inform the view that items have been removed.
tbx::view::ViewItems::add
void add(const T &item)
Add an item to the end of the list.
Definition: viewitems.h:137
tbx::view::ItemView::changing
virtual void changing(unsigned int where, unsigned int how_many)
Inform the view that items are about to be changed.
Definition: itemview.h:235
tbx::view::ItemView::cleared
virtual void cleared()=0
Inform the view that all the data has been cleared.
tbx::view::ViewItems::clear
void clear()
Clear the whole list.
Definition: viewitems.h:173
tbx::view::ItemView::removing
virtual void removing(unsigned int where, unsigned int how_many)
Inform the view that items are about to be removed.
Definition: itemview.h:216
tbx::view::ViewItems::operator[]
const T & operator[](unsigned int index) const
Get item at specified index.
Definition: viewitems.h:101
tbx::view::ItemView::changed
virtual void changed(unsigned int where, unsigned int how_many)=0
Inform the view that items have been changed.
tbx::view::ViewItems::view
void view(ItemView *v)
Set view for the items.
Definition: viewitems.h:64
tbx::view::ItemView
Base class for views that show an indexed list of items.
Definition: itemview.h:68
tbx::view::ViewItems::changed
void changed(unsigned int index, unsigned int how_many=1)
If the items contain method and fields that can be changed call this function after a change to infor...
Definition: viewitems.h:127
tbx::view::ViewItems::insert
void insert(unsigned int index, const T &item)
Insert an item in the list.
Definition: viewitems.h:150
tbx::view::ViewItems
Template class to store a list of items for an item view and to call the item view update routines au...
Definition: viewitems.h:49
tbx::view::ViewItems::erase
void erase(unsigned int index)
Delete an item from a list.
Definition: viewitems.h:163
tbx::view::ViewItems::ViewItems
ViewItems(ItemView *v=0)
Construct the view items.
Definition: viewitems.h:59
tbx::view::ViewItems::changing
void changing(unsigned int index, unsigned int how_many=1)
If the items contain method and fields that can be changed call this function prior to a change to in...
Definition: viewitems.h:114