src/model.cpp

changeset 204
52e10e8d88cc
parent 200
ca23936b455b
child 208
930928b760a2
equal deleted inserted replaced
203:1909a0123c72 204:52e10e8d88cc
109 std::optional<int> Model::find(ModelId id) const 109 std::optional<int> Model::find(ModelId id) const
110 { 110 {
111 return pointerToOptional(findInMap(this->positions, id)); 111 return pointerToOptional(findInMap(this->positions, id));
112 } 112 }
113 113
114 template<typename K, typename V>
115 void removeFromMap(std::map<K, V>& map, const K& key)
116 {
117 const auto it = map.find(key);
118 if (it != map.end()) {
119 map.erase(it);
120 }
121 }
122
114 void Model::remove(int index) 123 void Model::remove(int index)
115 { 124 {
116 if (index >= 0 and index < this->size()) { 125 if (index >= 0 and index < this->size()) {
117 Q_EMIT this->beginRemoveRows({}, index, index); 126 Q_EMIT this->beginRemoveRows({}, index, index);
127 removeFromMap(this->positions, this->body[index].id);
118 this->body.erase(this->body.begin() + index); 128 this->body.erase(this->body.begin() + index);
129 for (int i = index; i < this->size(); ++i) {
130 this->positions[this->body[i].id] = i;
131 }
119 Q_EMIT this->endRemoveRows(); 132 Q_EMIT this->endRemoveRows();
120 } 133 }
121 } 134 }
122 135
123 int Model::rowCount(const QModelIndex &) const 136 int Model::rowCount(const QModelIndex &) const

mercurial