mechanicsgroup.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005 by David Saxton                                    *
00003  *   david@bluehaze.org                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 
00011 #include "mechanicsgroup.h"
00012 #include "mechanicsitem.h"
00013 #include "mechanicsdocument.h"
00014 
00015 MechanicsGroup::MechanicsGroup( MechanicsDocument *mechanicsDocument, const char *name )
00016         : ItemGroup( mechanicsDocument, name )
00017 {
00018         b_isRaised = false;
00019 }
00020 
00021 
00022 MechanicsGroup::~MechanicsGroup()
00023 {
00024 }
00025 
00026 
00027 bool MechanicsGroup::addItem( Item *item )
00028 {
00029         if ( !item || !item->canvas() || m_itemList.contains(item) ) {
00030                 return false;
00031         }
00032         
00033         // Check that the item's parent isn't already selected
00034         Item *parent = item->parentItem();
00035         while (parent)
00036         {
00037                 if ( m_itemList.contains(parent) )
00038                         return false;
00039                 parent = parent->parentItem();
00040         }
00041         removeChildren(item);
00042         
00043         registerItem(item);
00044         updateInfo();
00045         item->setSelected(true);
00046         if ( MechanicsItem *mechanicsItem = dynamic_cast<MechanicsItem*>(item) )
00047                 mechanicsItem->setRaised(b_isRaised);
00048         emit itemAdded(item);
00049         return true;
00050 }
00051 
00052 
00053 bool MechanicsGroup::removeItem( Item *item )
00054 {
00055         if ( !item || !m_itemList.contains(item) ) {
00056                 return false;
00057         }
00058         unregisterItem(item);
00059         updateInfo();
00060         item->setSelected(false);
00061         MechanicsItem *mechanicsItem = dynamic_cast<MechanicsItem*>(item);
00062         if (mechanicsItem)
00063                 mechanicsItem->setRaised(false);
00064         emit itemRemoved(item);
00065         return true;
00066 }
00067 
00068 
00069 void MechanicsGroup::removeChildren( Item *item )
00070 {
00071         if (!item)
00072                 return;
00073         
00074         const ItemList children = item->children();
00075         const ItemList::const_iterator end = children.end();
00076         for ( ItemList::const_iterator it = children.begin(); it != end; ++it )
00077         {
00078                 removeChildren(*it);
00079                 removeItem(*it);
00080         }
00081 }
00082 
00083 
00084 void MechanicsGroup::setRaised( bool isRaised )
00085 {
00086         b_isRaised = isRaised;
00087         const ItemList::iterator end = m_itemList.end();
00088         for ( ItemList::iterator it = m_itemList.begin(); it != end; ++it )
00089         {
00090                 MechanicsItem *mechanicsItem = dynamic_cast<MechanicsItem*>((Item*)*it);
00091                 if (mechanicsItem)
00092                         mechanicsItem->setRaised(b_isRaised);
00093         }
00094 }
00095 
00096 
00097 void MechanicsGroup::setSelectionMode( uint sm )
00098 {
00099         const ItemList::iterator end = m_itemList.end();
00100         for ( ItemList::iterator it = m_itemList.begin(); it != end; ++it )
00101         {
00102                 MechanicsItem *mechanicsItem = dynamic_cast<MechanicsItem*>((Item*)*it);
00103                 if (mechanicsItem)
00104                         mechanicsItem->setSelectionMode( (MechanicsItem::SelectionMode)sm );
00105         }
00106 }
00107 
00108 
00109 MechanicsItemList MechanicsGroup::extractMechanicsItems() const
00110 {
00111         MechanicsItemList mechanicsItemList;
00112         
00113         const ItemList::const_iterator end = m_itemList.end();
00114         for ( ItemList::const_iterator it = m_itemList.begin(); it != end; ++it )
00115         {
00116                 MechanicsItem *mechanicsItem = dynamic_cast<MechanicsItem*>((Item*)*it);
00117                 if (mechanicsItem)
00118                         mechanicsItemList.append(mechanicsItem);
00119         }
00120         
00121         return mechanicsItemList;
00122 }
00123 
00124 
00125 MechanicsItemList MechanicsGroup::toplevelMechItemList() const
00126 {
00127         MechanicsItemList toplevel;
00128         
00129         MechanicsItemList mechItemList = extractMechanicsItems();
00130         
00131         const MechanicsItemList::const_iterator end = mechItemList.end();
00132         for ( MechanicsItemList::const_iterator it = mechItemList.begin(); it != end; ++it )
00133         {
00134                 MechanicsItem* parent = *it;
00135                 while (parent)
00136                 {
00137                         if ( !parent->parentItem() && !toplevel.contains(parent) )
00138                                 toplevel.append(parent);
00139                         
00140                         parent = dynamic_cast<MechanicsItem*>(parent->parentItem());
00141                 }
00142         }
00143         
00144         return toplevel;
00145 }
00146 
00147 
00148 void MechanicsGroup::setSelected( bool sel )
00149 {
00150         const ItemList::iterator end = m_itemList.end();
00151         for ( ItemList::iterator it = m_itemList.begin(); it != end; ++it )
00152         {
00153                 if (*it && (*it)->isSelected() != sel ) {
00154                         (*it)->setSelected(sel);
00155                 }
00156         }
00157 }
00158 
00159 
00160 bool MechanicsGroup::addQCanvasItem( QCanvasItem* item )
00161 {
00162         return addItem( dynamic_cast<Item*>(item) );
00163 }
00164 
00165 bool MechanicsGroup::contains(QCanvasItem* item) const
00166 {
00167     return m_itemList.contains(dynamic_cast<Item*>(item));
00168 }
00169 
00170 
00171 void MechanicsGroup::deleteAllItems()
00172 {
00173         const ItemList::iterator end = m_itemList.end();
00174         for ( ItemList::iterator it = m_itemList.begin(); it != end; ++it )
00175         {
00176                 if (*it)
00177                         (*it)->removeItem();
00178         }
00179         
00180         removeAllItems();
00181 }
00182 
00183 void MechanicsGroup::mergeGroup(ItemGroup* itemGroup)
00184 {
00185         MechanicsGroup *group = dynamic_cast<MechanicsGroup*>(itemGroup);
00186         if (!group) {
00187                 return;
00188         }
00189         
00190         const ItemList items = group->items();
00191         const ItemList::const_iterator end = items.end();
00192         for ( ItemList::const_iterator it = items.begin(); it != end; ++it )
00193         {
00194                 addItem(*it);
00195         }
00196 }
00197 
00198 void MechanicsGroup::removeAllItems()
00199 {
00200         while ( !m_itemList.isEmpty() )
00201                 removeItem(m_itemList.first());
00202 }
00203 
00204 void MechanicsGroup::removeQCanvasItem(QCanvasItem* item)
00205 {
00206         removeItem(dynamic_cast<Item*>(item));
00207 }
00208 
00209 
00210 void MechanicsGroup::setItems(QCanvasItemList list)
00211 {
00212         {
00213                 ItemList removeList;
00214                 const ItemList::iterator end = m_itemList.end();
00215                 for ( ItemList::iterator it = m_itemList.begin(); it != end; ++it )
00216                 {
00217                         if ( !list.contains(*it) ) {
00218                                 removeList.append(*it);
00219                         }
00220                 }
00221                 const ItemList::iterator rend = removeList.end();
00222                 for ( ItemList::iterator it = removeList.begin(); it != rend; ++it )
00223                 {
00224                         removeItem(*it);
00225                         (*it)->setSelected(false);
00226                 }
00227         }
00228         
00229         const QCanvasItemList::iterator end = list.end();
00230         for ( QCanvasItemList::iterator it = list.begin(); it != end; ++it )
00231         {
00232                 // We don't need to check that we've already got the item as it will
00233                 // be checked in the function call
00234                 addQCanvasItem(*it);
00235         }
00236 }
00237 
00238 
00239 void MechanicsGroup::updateInfo()
00240 {
00241 }
00242 
00243 #include "mechanicsgroup.moc"

Generated on Tue May 8 17:05:31 2007 for KTechLab by  doxygen 1.5.1