src/model.cpp

changeset 1245
338d66111168
parent 1244
68e126e8c629
child 1247
7e1ce2fc066b
equal deleted inserted replaced
1244:68e126e8c629 1245:338d66111168
67 { 67 {
68 if (object->model() and object->model() != this) 68 if (object->model() and object->model() != this)
69 object->model()->withdraw(object); 69 object->model()->withdraw(object);
70 70
71 // TODO: check that the object isn't in the vector once there's a cheap way to do so! 71 // TODO: check that the object isn't in the vector once there's a cheap way to do so!
72 beginInsertRows({}, position, position);
72 _objects.insert(position, object); 73 _objects.insert(position, object);
73 _needsTriangleRecount = true; 74 _needsTriangleRecount = true;
74 object->setDocument(this); 75 object->setDocument(this);
75 emit objectAdded(object); 76 emit objectAdded(object);
77 endInsertRows();
76 } 78 }
77 79
78 /* 80 /*
79 * Swaps one object with another, assuming they both are in this model. 81 * Swaps one object with another, assuming they both are in this model.
80 */ 82 */
253 /* 255 /*
254 * Drops an object from the model at the provided position. The caller must immediately put the result value object into a new model. 256 * Drops an object from the model at the provided position. The caller must immediately put the result value object into a new model.
255 */ 257 */
256 LDObject* Model::withdrawAt(int position) 258 LDObject* Model::withdrawAt(int position)
257 { 259 {
260 beginRemoveRows({}, position, position);
258 LDObject* object = _objects[position]; 261 LDObject* object = _objects[position];
259 emit aboutToRemoveObject(object); 262 emit aboutToRemoveObject(object);
260 _objects.removeAt(position); 263 _objects.removeAt(position);
261 _needsTriangleRecount = true; 264 _needsTriangleRecount = true;
265 endRemoveRows();
262 return object; 266 return object;
263 } 267 }
264 268
265 /* 269 /*
266 * Returns whether or not this model is empty. 270 * Returns whether or not this model is empty.
593 else 597 else
594 { 598 {
595 return {}; 599 return {};
596 } 600 }
597 601
598 case ObjectRole: 602 case ObjectIdRole:
599 return {qMetaTypeId<LDObject*>(), object}; 603 return object->id();
600 604
601 default: 605 default:
602 return {}; 606 return {};
603 } 607 }
604 } 608 }
605 609
610 /*
606 bool Model::removeRows(int row, int count, const QModelIndex& parent) 611 bool Model::removeRows(int row, int count, const QModelIndex& parent)
607 { 612 {
608 if (row >= 0 and row < size() and count <= size() - row) 613 if (row >= 0 and row < size() and count <= size() - row)
609 { 614 {
610 beginRemoveRows(parent, row, row + count - 1); 615 beginRemoveRows(parent, row, row + count - 1);
618 else 623 else
619 { 624 {
620 return false; 625 return false;
621 } 626 }
622 } 627 }
628 */
629
630 /*
631 * Looks up an object by the given index.
632 */
633 LDObject* Model::lookup(const QModelIndex &index) const
634 {
635 if (index.row() >= 0 and index.row() < size())
636 return this->objects()[index.row()];
637 else
638 return nullptr;
639 }
623 640
624 int countof(Model& model) 641 int countof(Model& model)
625 { 642 {
626 return model.size(); 643 return model.size();
627 } 644 }

mercurial