src/model.h

changeset 173
8a3047468994
parent 152
03f8e6d42e13
child 200
ca23936b455b
equal deleted inserted replaced
172:50f055543ff6 173:8a3047468994
66 void remove(int position); 66 void remove(int position);
67 void emitDataChangedSignal(int position); 67 void emitDataChangedSignal(int position);
68 private: 68 private:
69 bool modified = false; 69 bool modified = false;
70 std::vector<ModelObjectPointer> body; 70 std::vector<ModelObjectPointer> body;
71 std::map<ldraw::id_t, ldraw::Object*> objectsById; 71 mutable std::map<ldraw::id_t, std::size_t> objectsById;
72 mutable bool needObjectsByIdRebuild = false;
72 }; 73 };
73 74
74 void save(const Model& model, QIODevice *device); 75 void save(const Model& model, QIODevice *device);
75 76
76 /** 77 /**
97 { 98 {
98 const int position = static_cast<int>(this->body.size()); 99 const int position = static_cast<int>(this->body.size());
99 Q_EMIT beginInsertRows({}, position, position); 100 Q_EMIT beginInsertRows({}, position, position);
100 this->body.push_back(std::make_unique<T>(args...)); 101 this->body.push_back(std::make_unique<T>(args...));
101 ldraw::Object* pointer = this->body.back().get(); 102 ldraw::Object* pointer = this->body.back().get();
102 this->objectsById[pointer->id] = pointer; 103 this->objectsById[pointer->id] = this->body.size() - 1;
103 Q_EMIT endInsertRows(); 104 Q_EMIT endInsertRows();
104 return ldraw::Id<T>{pointer->id.value}; 105 return ldraw::Id<T>{pointer->id.value};
105 } 106 }
106 107
107 template<typename T, typename... Args> 108 template<typename T, typename... Args>
108 ldraw::Id<T> Model::insert(const std::size_t position, Args&&... args) 109 ldraw::Id<T> Model::insert(const std::size_t position, Args&&... args)
109 { 110 {
110 Q_EMIT beginInsertRows({}, position, position); 111 Q_EMIT beginInsertRows({}, position, position);
111 this->body.insert(std::begin(this->body) + position, std::make_unique<T>(args...)); 112 this->body.insert(std::begin(this->body) + position, std::make_unique<T>(args...));
112 ldraw::Object* pointer = this->body[position].get(); 113 ldraw::Object* pointer = this->body[position].get();
113 this->objectsById[pointer->id] = pointer; 114 this->objectsById[pointer->id] = position;
114 Q_EMIT endInsertRows(); 115 Q_EMIT endInsertRows();
115 return ldraw::Id<T>{pointer->id.value}; 116 return ldraw::Id<T>{pointer->id.value};
116 } 117 }
117 118
118 template<typename R> 119 template<typename R>

mercurial