glc_selectionset.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  http://glc-lib.sourceforge.net
00006 
00007  GLC-lib is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU Lesser General Public License as published by
00009  the Free Software Foundation; either version 3 of the License, or
00010  (at your option) any later version.
00011 
00012  GLC-lib is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU Lesser General Public License for more details.
00016 
00017  You should have received a copy of the GNU Lesser General Public License
00018  along with GLC-lib; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021  *****************************************************************************/
00023 
00024 #include "glc_selectionset.h"
00025 #include "glc_worldhandle.h"
00026 
00027 GLC_SelectionSet::GLC_SelectionSet(GLC_WorldHandle* pWorldHandle)
00028 : m_pWorldHandle(pWorldHandle)
00029 , m_OccurenceHash()
00030 , m_InstanceToPrimitiveId()
00031 {
00032         Q_ASSERT(0 == m_pWorldHandle->collection()->selectionSize());
00033 
00034 }
00035 
00036 GLC_SelectionSet::~GLC_SelectionSet()
00037 {
00038 
00039 }
00040 
00042 // Get Functions
00044 
00045 bool GLC_SelectionSet::isEmpty() const
00046 {
00047         return m_OccurenceHash.isEmpty();
00048 }
00049 
00050 
00051 int GLC_SelectionSet::count() const
00052 {
00053         return m_OccurenceHash.size() - m_InstanceToPrimitiveId.size();
00054 }
00055 
00056 QList<GLC_StructOccurence*> GLC_SelectionSet::occurencesList() const
00057 {
00058         QList<GLC_StructOccurence*> listOfOccurence;
00059         QHash<GLC_uint, GLC_StructOccurence*>::const_iterator iOcc= m_OccurenceHash.constBegin();
00060         while (m_OccurenceHash.constEnd() != iOcc)
00061         {
00062                 if (!m_InstanceToPrimitiveId.contains(iOcc.key()))
00063                 {
00064                         listOfOccurence.append(iOcc.value());
00065                 }
00066                 ++iOcc;
00067         }
00068         return listOfOccurence;
00069 }
00070 
00071 QList<GLC_StructOccurence*> GLC_SelectionSet::occurencesListWithSelectedPrimitive() const
00072 {
00073         QList<GLC_StructOccurence*> listOfOccurence;
00074         QHash<GLC_uint, QSet<GLC_uint> >::const_iterator iOcc= m_InstanceToPrimitiveId.constBegin();
00075         while (m_InstanceToPrimitiveId.constEnd() != iOcc)
00076         {
00077                 listOfOccurence.append(m_OccurenceHash.value(iOcc.key()));
00078                 ++iOcc;
00079         }
00080         return listOfOccurence;
00081 }
00082 
00083 QSet<GLC_uint> GLC_SelectionSet::selectedPrimitivesId(GLC_uint instanceId) const
00084 {
00085         QSet<GLC_uint> primitivesIdSet;
00086         if (m_InstanceToPrimitiveId.contains(instanceId))
00087         {
00088                 primitivesIdSet= m_InstanceToPrimitiveId.value(instanceId);
00089         }
00090         return primitivesIdSet;
00091 }
00092 
00093 bool GLC_SelectionSet::hasPrimitiveSelected(GLC_uint instanceId) const
00094 {
00095         return m_InstanceToPrimitiveId.contains(instanceId);
00096 }
00097 
00099 // Set Functions
00101 bool GLC_SelectionSet::insert(GLC_StructOccurence* pOccurence)
00102 {
00103         return insert(pOccurence->id());
00104 }
00105 
00106 bool GLC_SelectionSet::insert(GLC_uint occurenceId)
00107 {
00108         Q_ASSERT(m_pWorldHandle->containsOccurence(occurenceId));
00109         if (!m_OccurenceHash.contains(occurenceId))
00110         {
00111                 GLC_StructOccurence* pOccurence= m_pWorldHandle->getOccurence(occurenceId);
00112                 m_OccurenceHash.insert(occurenceId, pOccurence);
00113                 return true;
00114         }
00115         else return false;
00116 }
00117 
00118 bool GLC_SelectionSet::remove(GLC_StructOccurence* pOccurence)
00119 {
00120         return remove(pOccurence->id());
00121 }
00122 
00123 bool GLC_SelectionSet::remove(GLC_uint occurenceId)
00124 {
00125         Q_ASSERT(m_pWorldHandle->containsOccurence(occurenceId));
00126         if (m_OccurenceHash.contains(occurenceId))
00127         {
00128                 m_OccurenceHash.remove(occurenceId);
00129                 m_InstanceToPrimitiveId.remove(occurenceId);
00130                 return true;
00131         }
00132         else return false;
00133 }
00134 
00135 void GLC_SelectionSet::clear()
00136 {
00137         m_OccurenceHash.clear();
00138         m_InstanceToPrimitiveId.clear();
00139 }
00140 
00141 bool GLC_SelectionSet::insertPrimitiveId(GLC_StructOccurence* pOccurence, GLC_uint primitiveId)
00142 {
00143         return insertPrimitiveId(pOccurence->id(), primitiveId);
00144 }
00145 
00146 bool GLC_SelectionSet::insertPrimitiveId(GLC_uint occurenceId, GLC_uint primitiveId)
00147 {
00148         bool returnValue= false;
00149         insert(occurenceId);
00150         if (m_InstanceToPrimitiveId.contains(occurenceId))
00151         {
00152                 if (!m_InstanceToPrimitiveId[occurenceId].contains(primitiveId))
00153                 {
00154                         m_InstanceToPrimitiveId[occurenceId].insert(primitiveId);
00155                         returnValue= true;
00156                 }
00157                 else returnValue= false;
00158         }
00159         else
00160         {
00161                 QSet<GLC_uint> primitiveSet;
00162                 primitiveSet.insert(primitiveId);
00163                 m_InstanceToPrimitiveId.insert(occurenceId, primitiveSet);
00164                 returnValue= true;
00165         }
00166 
00167         return returnValue;
00168 
00169 }
00170 
00171 void GLC_SelectionSet::insertSetOfPrimitivesId(GLC_StructOccurence* pOccurence, const QSet<GLC_uint>& setOfPrimitivesId)
00172 {
00173         insertSetOfPrimitivesId(pOccurence->id(), setOfPrimitivesId);
00174 }
00175 
00176 void GLC_SelectionSet::insertSetOfPrimitivesId(GLC_uint occurenceId, const QSet<GLC_uint>& setOfPrimitivesId)
00177 {
00178         insert(occurenceId);
00179         if (m_InstanceToPrimitiveId.contains(occurenceId))
00180         {
00181                 m_InstanceToPrimitiveId[occurenceId].unite(setOfPrimitivesId);
00182         }
00183         else
00184         {
00185                 m_InstanceToPrimitiveId.insert(occurenceId, setOfPrimitivesId);
00186         }
00187 }
00188 
00189 bool GLC_SelectionSet::removePrimitiveId(GLC_StructOccurence* pOccurence, GLC_uint primitiveId)
00190 {
00191         return removePrimitiveId(pOccurence->id(), primitiveId);
00192 }
00193 
00194 bool GLC_SelectionSet::removePrimitiveId(GLC_uint occurenceId, GLC_uint primitiveId)
00195 {
00196         Q_ASSERT(m_pWorldHandle->containsOccurence(occurenceId));
00197 
00198         if (m_InstanceToPrimitiveId.contains(occurenceId))
00199         {
00200                 if (m_InstanceToPrimitiveId[occurenceId].contains(primitiveId))
00201                 {
00202                         if (m_InstanceToPrimitiveId[occurenceId].size() == 1)
00203                         {
00204                                 remove(occurenceId);
00205                         }
00206                         else
00207                         {
00208                                 m_InstanceToPrimitiveId[occurenceId].remove(primitiveId);
00209                         }
00210                         return true;
00211                 }
00212                 else return false;
00213         }
00214         else return false;
00215 }

SourceForge.net Logo

©2005-2011 Laurent Ribon