659 // ============================================================================= |
662 // ============================================================================= |
660 // Hook the set accessors of certain properties to this changeProperty function. |
663 // Hook the set accessors of certain properties to this changeProperty function. |
661 // It takes care of history management so we can capture low-level changes, this |
664 // It takes care of history management so we can capture low-level changes, this |
662 // makes history stuff work out of the box. |
665 // makes history stuff work out of the box. |
663 // ----------------------------------------------------------------------------- |
666 // ----------------------------------------------------------------------------- |
664 template<class T> void changeProperty (LDObject* obj, T* ptr, const T& val) |
667 template<class T> static void changeProperty (LDObject* obj, T* ptr, const T& val) |
665 { long idx; |
668 { long idx; |
666 |
669 |
667 if (obj->file() && (idx = obj->getIndex()) != -1) |
670 if (obj->file() && (idx = obj->getIndex()) != -1) |
668 { str before = obj->raw(); |
671 { str before = obj->raw(); |
669 *ptr = val; |
672 *ptr = val; |
686 } |
689 } |
687 |
690 |
688 // ============================================================================= |
691 // ============================================================================= |
689 // ----------------------------------------------------------------------------- |
692 // ----------------------------------------------------------------------------- |
690 const vertex& LDObject::getVertex (int i) const |
693 const vertex& LDObject::getVertex (int i) const |
691 { return m_coords[i]; |
694 { return m_coords[i]->data(); |
692 } |
695 } |
693 |
696 |
694 void LDObject::setVertex (int i, const vertex& vert) |
697 void LDObject::setVertex (int i, const vertex& vert) |
695 { changeProperty (this, &m_coords[i], vert); |
698 { changeProperty (this, &m_coords[i], LDSharedVertex::getSharedVertex (vert)); |
696 } |
699 } |
697 |
700 |
698 // ============================================================================= |
701 // ============================================================================= |
699 // ----------------------------------------------------------------------------- |
702 // ----------------------------------------------------------------------------- |
700 READ_ACCESSOR (vertex, LDMatrixObject::position) |
703 void LDMatrixObject::setPosition (const vertex& a) |
701 { return m_position; |
704 { changeProperty (linkPointer(), &m_position, LDSharedVertex::getSharedVertex (a)); |
702 } |
|
703 |
|
704 SET_ACCESSOR (vertex, LDMatrixObject::setPosition) |
|
705 { changeProperty (linkPointer(), &m_position, val); |
|
706 } |
705 } |
707 |
706 |
708 // ============================================================================= |
707 // ============================================================================= |
709 // ----------------------------------------------------------------------------- |
708 // ----------------------------------------------------------------------------- |
710 READ_ACCESSOR (matrix, LDMatrixObject::transform) |
709 READ_ACCESSOR (matrix, LDMatrixObject::transform) |
712 } |
711 } |
713 |
712 |
714 SET_ACCESSOR (matrix, LDMatrixObject::setTransform) |
713 SET_ACCESSOR (matrix, LDMatrixObject::setTransform) |
715 { changeProperty (linkPointer(), &m_transform, val); |
714 { changeProperty (linkPointer(), &m_transform, val); |
716 } |
715 } |
|
716 |
|
717 // ============================================================================= |
|
718 // ----------------------------------------------------------------------------- |
|
719 static QMap<vertex, LDSharedVertex*> g_sharedVerts; |
|
720 |
|
721 LDSharedVertex* LDSharedVertex::getSharedVertex (const vertex& a) |
|
722 { auto it = g_sharedVerts.find (a); |
|
723 |
|
724 if (it == g_sharedVerts.end()) |
|
725 { LDSharedVertex* v = new LDSharedVertex (a); |
|
726 g_sharedVerts[a] = v; |
|
727 return v; |
|
728 } |
|
729 |
|
730 return *it; |
|
731 } |
|
732 |
|
733 // ============================================================================= |
|
734 // ----------------------------------------------------------------------------- |
|
735 void LDSharedVertex::addRef(LDObject* a) |
|
736 { m_refs << a; |
|
737 } |
|
738 |
|
739 // ============================================================================= |
|
740 // ----------------------------------------------------------------------------- |
|
741 void LDSharedVertex::delRef (LDObject* a) |
|
742 { m_refs.removeOne (a); |
|
743 |
|
744 if (m_refs.empty()) |
|
745 { g_sharedVerts.remove (m_data); |
|
746 delete this; |
|
747 } |
|
748 } |