| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef UI_BASE_MODELS_LIST_MODEL_H_ | 5 #ifndef UI_BASE_MODELS_LIST_MODEL_H_ |
| 6 #define UI_BASE_MODELS_LIST_MODEL_H_ | 6 #define UI_BASE_MODELS_LIST_MODEL_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 void DeleteAt(size_t index) { | 55 void DeleteAt(size_t index) { |
| 56 delete RemoveAt(index); | 56 delete RemoveAt(index); |
| 57 } | 57 } |
| 58 | 58 |
| 59 // Removes and deletes all items from the model. | 59 // Removes and deletes all items from the model. |
| 60 void DeleteAll() { | 60 void DeleteAll() { |
| 61 ScopedVector<ItemType> to_be_deleted(items_.Pass()); | 61 ScopedVector<ItemType> to_be_deleted(items_.Pass()); |
| 62 NotifyItemsRemoved(0, to_be_deleted.size()); | 62 NotifyItemsRemoved(0, to_be_deleted.size()); |
| 63 } | 63 } |
| 64 | 64 |
| 65 // Moves the item at |index| to |target_index|. |target_index| is in terms |
| 66 // of the model *after* the item at |index| is removed. |
| 67 void Move(size_t index, size_t target_index) { |
| 68 DCHECK_LT(index, item_count()); |
| 69 DCHECK_LT(target_index, item_count()); |
| 70 |
| 71 if (index == target_index) |
| 72 return; |
| 73 |
| 74 ItemType* item = items_[index]; |
| 75 items_.weak_erase(items_.begin() + index); |
| 76 items_.insert(items_.begin() + target_index, item); |
| 77 NotifyItemMoved(index, target_index); |
| 78 } |
| 79 |
| 65 void AddObserver(ListModelObserver* observer) { | 80 void AddObserver(ListModelObserver* observer) { |
| 66 observers_.AddObserver(observer); | 81 observers_.AddObserver(observer); |
| 67 } | 82 } |
| 68 | 83 |
| 69 void RemoveObserver(ListModelObserver* observer) { | 84 void RemoveObserver(ListModelObserver* observer) { |
| 70 observers_.RemoveObserver(observer); | 85 observers_.RemoveObserver(observer); |
| 71 } | 86 } |
| 72 | 87 |
| 73 void NotifyItemsAdded(size_t start, size_t count) { | 88 void NotifyItemsAdded(size_t start, size_t count) { |
| 74 FOR_EACH_OBSERVER(ListModelObserver, | 89 FOR_EACH_OBSERVER(ListModelObserver, |
| 75 observers_, | 90 observers_, |
| 76 ListItemsAdded(start, count)); | 91 ListItemsAdded(start, count)); |
| 77 } | 92 } |
| 78 | 93 |
| 79 void NotifyItemsRemoved(size_t start, size_t count) { | 94 void NotifyItemsRemoved(size_t start, size_t count) { |
| 80 FOR_EACH_OBSERVER(ListModelObserver, | 95 FOR_EACH_OBSERVER(ListModelObserver, |
| 81 observers_, | 96 observers_, |
| 82 ListItemsRemoved(start, count)); | 97 ListItemsRemoved(start, count)); |
| 83 } | 98 } |
| 84 | 99 |
| 100 void NotifyItemMoved(size_t index, size_t target_index) { |
| 101 FOR_EACH_OBSERVER(ListModelObserver, |
| 102 observers_, |
| 103 ListItemMoved(index, target_index)); |
| 104 } |
| 105 |
| 85 void NotifyItemsChanged(size_t start, size_t count) { | 106 void NotifyItemsChanged(size_t start, size_t count) { |
| 86 FOR_EACH_OBSERVER(ListModelObserver, | 107 FOR_EACH_OBSERVER(ListModelObserver, |
| 87 observers_, | 108 observers_, |
| 88 ListItemsChanged(start, count)); | 109 ListItemsChanged(start, count)); |
| 89 } | 110 } |
| 90 | 111 |
| 91 size_t item_count() const { return items_.size(); } | 112 size_t item_count() const { return items_.size(); } |
| 92 | 113 |
| 93 const ItemType* GetItemAt(size_t index) const { | 114 const ItemType* GetItemAt(size_t index) const { |
| 94 DCHECK_LT(index, item_count()); | 115 DCHECK_LT(index, item_count()); |
| 95 return items_[index]; | 116 return items_[index]; |
| 96 } | 117 } |
| 97 ItemType* GetItemAt(size_t index) { | 118 ItemType* GetItemAt(size_t index) { |
| 98 return const_cast<ItemType*>( | 119 return const_cast<ItemType*>( |
| 99 const_cast<const ListModel<ItemType>*>(this)->GetItemAt(index)); | 120 const_cast<const ListModel<ItemType>*>(this)->GetItemAt(index)); |
| 100 } | 121 } |
| 101 | 122 |
| 102 private: | 123 private: |
| 103 ScopedVector<ItemType> items_; | 124 ScopedVector<ItemType> items_; |
| 104 ObserverList<ListModelObserver> observers_; | 125 ObserverList<ListModelObserver> observers_; |
| 105 | 126 |
| 106 DISALLOW_COPY_AND_ASSIGN(ListModel<ItemType>); | 127 DISALLOW_COPY_AND_ASSIGN(ListModel<ItemType>); |
| 107 }; | 128 }; |
| 108 | 129 |
| 109 } // namespace ui | 130 } // namespace ui |
| 110 | 131 |
| 111 #endif // UI_BASE_MODELS_LIST_MODEL_H_ | 132 #endif // UI_BASE_MODELS_LIST_MODEL_H_ |
| OLD | NEW |