src/model.h

changeset 116
aad3e897bc32
parent 112
5760cbb32bc0
child 117
121a40d5e34c
equal deleted inserted replaced
115:ed884a2fb009 116:aad3e897bc32
47 QModelIndex lookup(ldraw::id_t id) const; 47 QModelIndex lookup(ldraw::id_t id) const;
48 ldraw::id_t resolve(const QModelIndex& index) const; 48 ldraw::id_t resolve(const QModelIndex& index) const;
49 template<typename R> 49 template<typename R>
50 ldraw::Id<R> checkType(ldraw::id_t id) const; 50 ldraw::Id<R> checkType(ldraw::id_t id) const;
51 template<typename R> 51 template<typename R>
52 const R* get(ldraw::Id<R> id, QModelIndex* index_out = nullptr) const; 52 const R* get(ldraw::Id<R> id) const;
53 template<typename R>
54 struct Get2Result
55 {
56 QModelIndex index;
57 const R* object;
58 };
59 template<typename R>
60 Get2Result<R> get2(ldraw::Id<R> id) const;
53 Q_SIGNALS: 61 Q_SIGNALS:
54 void objectAdded(ldraw::id_t id, int position); 62 void objectAdded(ldraw::id_t id, int position);
55 private: 63 private:
56 using ModelObjectPointer = std::unique_ptr<ldraw::Object>; 64 using ModelObjectPointer = std::unique_ptr<ldraw::Object>;
57 template<typename T, typename... Args> 65 template<typename T, typename... Args>
129 this->needRecache = true; 137 this->needRecache = true;
130 return ldraw::Id<T>{pointer->id.value}; 138 return ldraw::Id<T>{pointer->id.value};
131 } 139 }
132 140
133 template<typename R> 141 template<typename R>
134 const R* Model::get(ldraw::Id<R> id, QModelIndex* index_out) const 142 const R* Model::get(ldraw::Id<R> id) const
135 { 143 {
136 QModelIndex index = this->lookup(id); 144 return this->get2(id).object;
137 if (index_out != nullptr) 145 }
146
147 template<typename R>
148 Model::Get2Result<R> Model::get2(const ldraw::Id<R> id) const
149 {
150 Get2Result<R> result;
151 result.index = this->lookup(id);
152 if (result.index.isValid())
138 { 153 {
139 *index_out = index; 154 result.object = static_cast<const R*>(this->objectAt(result.index));
140 }
141 if (index.isValid())
142 {
143 return static_cast<const R*>(this->objectAt(index));
144 } 155 }
145 else 156 else
146 { 157 {
147 return nullptr; 158 result.object = nullptr;
148 } 159 }
160 return result;
149 } 161 }

mercurial