File indexing completed on 2024-12-01 11:20:40
0001 /*************************************************************************** 0002 * Copyright (C) 2005 by David Saxton * 0003 * david@bluehaze.org * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 ***************************************************************************/ 0010 0011 #include "mechanicsgroup.h" 0012 #include "mechanicsdocument.h" 0013 #include "mechanicsitem.h" 0014 0015 MechanicsGroup::MechanicsGroup(MechanicsDocument *mechanicsDocument) 0016 : ItemGroup(mechanicsDocument) 0017 { 0018 b_isRaised = false; 0019 } 0020 0021 MechanicsGroup::~MechanicsGroup() 0022 { 0023 } 0024 0025 bool MechanicsGroup::addItem(Item *item) 0026 { 0027 if (!item || !item->canvas() || m_itemList.contains(item)) { 0028 return false; 0029 } 0030 0031 // Check that the item's parent isn't already selected 0032 Item *parent = item->parentItem(); 0033 while (parent) { 0034 if (m_itemList.contains(parent)) 0035 return false; 0036 parent = parent->parentItem(); 0037 } 0038 removeChildren(item); 0039 0040 registerItem(item); 0041 updateInfo(); 0042 item->setSelected(true); 0043 if (MechanicsItem *mechanicsItem = dynamic_cast<MechanicsItem *>(item)) 0044 mechanicsItem->setRaised(b_isRaised); 0045 emit itemAdded(item); 0046 return true; 0047 } 0048 0049 bool MechanicsGroup::removeItem(Item *item) 0050 { 0051 if (!item || !m_itemList.contains(item)) { 0052 return false; 0053 } 0054 unregisterItem(item); 0055 updateInfo(); 0056 item->setSelected(false); 0057 MechanicsItem *mechanicsItem = dynamic_cast<MechanicsItem *>(item); 0058 if (mechanicsItem) 0059 mechanicsItem->setRaised(false); 0060 emit itemRemoved(item); 0061 return true; 0062 } 0063 0064 void MechanicsGroup::removeChildren(Item *item) 0065 { 0066 if (!item) 0067 return; 0068 0069 const ItemList children = item->children(); 0070 const ItemList::const_iterator end = children.end(); 0071 for (ItemList::const_iterator it = children.begin(); it != end; ++it) { 0072 removeChildren(*it); 0073 removeItem(*it); 0074 } 0075 } 0076 0077 void MechanicsGroup::setRaised(bool isRaised) 0078 { 0079 b_isRaised = isRaised; 0080 const ItemList::iterator end = m_itemList.end(); 0081 for (ItemList::iterator it = m_itemList.begin(); it != end; ++it) { 0082 MechanicsItem *mechanicsItem = dynamic_cast<MechanicsItem *>(static_cast<Item *>(*it)); 0083 if (mechanicsItem) 0084 mechanicsItem->setRaised(b_isRaised); 0085 } 0086 } 0087 0088 void MechanicsGroup::setSelectionMode(uint sm) 0089 { 0090 const ItemList::iterator end = m_itemList.end(); 0091 for (ItemList::iterator it = m_itemList.begin(); it != end; ++it) { 0092 MechanicsItem *mechanicsItem = dynamic_cast<MechanicsItem *>(static_cast<Item *>(*it)); 0093 if (mechanicsItem) 0094 mechanicsItem->setSelectionMode(static_cast<MechanicsItem::SelectionMode>(sm)); 0095 } 0096 } 0097 0098 MechanicsItemList MechanicsGroup::extractMechanicsItems() const 0099 { 0100 MechanicsItemList mechanicsItemList; 0101 0102 const ItemList::const_iterator end = m_itemList.end(); 0103 for (ItemList::const_iterator it = m_itemList.begin(); it != end; ++it) { 0104 MechanicsItem *mechanicsItem = dynamic_cast<MechanicsItem *>(static_cast<Item *>(*it)); 0105 if (mechanicsItem) 0106 mechanicsItemList.append(mechanicsItem); 0107 } 0108 0109 return mechanicsItemList; 0110 } 0111 0112 MechanicsItemList MechanicsGroup::toplevelMechItemList() const 0113 { 0114 MechanicsItemList toplevel; 0115 0116 MechanicsItemList mechItemList = extractMechanicsItems(); 0117 0118 const MechanicsItemList::const_iterator end = mechItemList.end(); 0119 for (MechanicsItemList::const_iterator it = mechItemList.begin(); it != end; ++it) { 0120 MechanicsItem *parent = *it; 0121 while (parent) { 0122 if (!parent->parentItem() && !toplevel.contains(parent)) 0123 toplevel.append(parent); 0124 0125 parent = dynamic_cast<MechanicsItem *>(parent->parentItem()); 0126 } 0127 } 0128 0129 return toplevel; 0130 } 0131 0132 void MechanicsGroup::setSelected(bool sel) 0133 { 0134 const ItemList::iterator end = m_itemList.end(); 0135 for (ItemList::iterator it = m_itemList.begin(); it != end; ++it) { 0136 if (*it && (*it)->isSelected() != sel) { 0137 (*it)->setSelected(sel); 0138 } 0139 } 0140 } 0141 0142 bool MechanicsGroup::addQCanvasItem(KtlQCanvasItem *item) 0143 { 0144 return addItem(dynamic_cast<Item *>(item)); 0145 } 0146 0147 bool MechanicsGroup::contains(KtlQCanvasItem *item) const 0148 { 0149 return m_itemList.contains(dynamic_cast<Item *>(item)); 0150 } 0151 0152 void MechanicsGroup::deleteAllItems() 0153 { 0154 const ItemList::iterator end = m_itemList.end(); 0155 for (ItemList::iterator it = m_itemList.begin(); it != end; ++it) { 0156 if (*it) 0157 (*it)->removeItem(); 0158 } 0159 0160 removeAllItems(); 0161 } 0162 0163 void MechanicsGroup::mergeGroup(ItemGroup *itemGroup) 0164 { 0165 MechanicsGroup *group = dynamic_cast<MechanicsGroup *>(itemGroup); 0166 if (!group) { 0167 return; 0168 } 0169 0170 const ItemList items = group->items(); 0171 const ItemList::const_iterator end = items.end(); 0172 for (ItemList::const_iterator it = items.begin(); it != end; ++it) { 0173 addItem(*it); 0174 } 0175 } 0176 0177 void MechanicsGroup::removeAllItems() 0178 { 0179 while (!m_itemList.isEmpty()) 0180 removeItem(m_itemList.first()); 0181 } 0182 0183 void MechanicsGroup::removeQCanvasItem(KtlQCanvasItem *item) 0184 { 0185 removeItem(dynamic_cast<Item *>(item)); 0186 } 0187 0188 void MechanicsGroup::setItems(KtlQCanvasItemList list) 0189 { 0190 { 0191 ItemList removeList; 0192 const ItemList::iterator end = m_itemList.end(); 0193 for (ItemList::iterator it = m_itemList.begin(); it != end; ++it) { 0194 if (!list.contains(*it)) { 0195 removeList.append(*it); 0196 } 0197 } 0198 const ItemList::iterator rend = removeList.end(); 0199 for (ItemList::iterator it = removeList.begin(); it != rend; ++it) { 0200 removeItem(*it); 0201 (*it)->setSelected(false); 0202 } 0203 } 0204 0205 const KtlQCanvasItemList::iterator end = list.end(); 0206 for (KtlQCanvasItemList::iterator it = list.begin(); it != end; ++it) { 0207 // We don't need to check that we've already got the item as it will 0208 // be checked in the function call 0209 addQCanvasItem(*it); 0210 } 0211 } 0212 0213 void MechanicsGroup::updateInfo() 0214 { 0215 } 0216 0217 #include "moc_mechanicsgroup.cpp"