Files
EgtGeomKernel/GdbObj.cpp
T
Dario Sassi 2a2c0065ed EgtGeomKernel 1.5c1 :
- aggiunta prima gestione attributi.
2014-03-06 08:17:26 +00:00

274 lines
7.2 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2013-2013
//----------------------------------------------------------------------------
// File : GdbObj.cpp Data : 05.03.14 Versione : 1.5c1
// Contenuto : Implementazione della classe GdbObj.
//
//
//
// Modifiche : 28.11.13 DS Creazione modulo.
//
//
//----------------------------------------------------------------------------
//--------------------------- Include ----------------------------------------
#include "stdafx.h"
#include "GdbObj.h"
#include "GdbGroup.h"
#include "Attribs.h"
#include "/EgtDev/Include/EGnStringUtils.h"
#include <new>
using namespace std ;
//----------------------------------------------------------------------------
GdbObj::GdbObj( void)
: m_nId( GDB_ID_NULL), m_pAttribs( nullptr),
m_pNext( nullptr), m_pPrev( nullptr), m_pParent( nullptr)
{
}
//----------------------------------------------------------------------------
GdbObj::~GdbObj( void)
{
if ( m_pAttribs != nullptr)
delete m_pAttribs ;
m_pAttribs = nullptr ;
}
//----------------------------------------------------------------------------
bool
GdbObj::Copy( const GdbObj* pSou)
{
// elimino eventuali attributi pre-esistenti
if ( m_pAttribs != nullptr)
delete m_pAttribs ;
m_pAttribs = nullptr ;
// se l'oggetto sorgente non esiste
if ( pSou == nullptr) {
m_nId = GDB_ID_NULL ;
return false ;
}
// copio Id
m_nId = pSou->m_nId ;
// copio gli attributi
if ( pSou->m_pAttribs != nullptr) {
m_pAttribs = pSou->m_pAttribs->Clone() ;
if ( m_pAttribs == nullptr)
return false ;
}
return true ;
}
//----------------------------------------------------------------------------
// Node
//----------------------------------------------------------------------------
int
GdbObj::GetParentId( void) const
{
if ( m_pParent != nullptr)
return m_pParent->m_nId ;
else
return GDB_ID_NULL ;
}
//----------------------------------------------------------------------------
bool
GdbObj::AddTail( GdbGroup* pParent)
{
// se il padre non è definito, errore
if ( pParent == nullptr)
return false ;
// se non ci sono figli
if ( pParent->m_pLastObj == nullptr) {
pParent->m_pLastObj = this ;
pParent->m_pFirstObj = this ;
m_pParent = pParent ;
m_pNext = nullptr ;
m_pPrev = nullptr ;
m_pParent->ObjCountInc() ;
return true ;
}
// caso standard
return InsertAfter( pParent->m_pLastObj) ;
}
//----------------------------------------------------------------------------
bool
GdbObj::AddHead( GdbGroup* pParent)
{
// se il padre non è definito, errore
if ( pParent == nullptr)
return false ;
// se non ci sono figli
if ( pParent->m_pFirstObj == nullptr) {
pParent->m_pFirstObj = this ;
pParent->m_pLastObj = this ;
m_pParent = pParent ;
m_pNext = nullptr ;
m_pPrev = nullptr ;
m_pParent->ObjCountInc() ;
return true ;
}
// caso standard
return InsertBefore( pParent->m_pFirstObj) ;
}
//----------------------------------------------------------------------------
bool
GdbObj::InsertAfter( GdbObj* pRef)
{
// se riferimento non definito, errore
if ( pRef == nullptr)
return false ;
// sistemazione puntatori di elemento inserito
m_pNext = pRef->m_pNext ;
m_pPrev = pRef ;
m_pParent = pRef->m_pParent ;
// sistemazione puntatori di elemento di riferimento
pRef->m_pNext = this ;
// sistemazione puntatori di eventuale elemento successivo
if ( m_pNext != nullptr)
m_pNext->m_pPrev = this ;
// sistemazione dell'eventuale padre
if ( m_pParent != nullptr) {
// se nuovo ultimo nodo
if ( m_pParent->m_pLastObj == pRef)
m_pParent->m_pLastObj = this ;
m_pParent->ObjCountInc() ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
GdbObj::InsertBefore( GdbObj* pRef)
{
// se riferimento non definito, errore
if ( pRef == nullptr)
return false ;
// sistemazione puntatori di elemento inserito
m_pNext = pRef ;
m_pPrev = pRef->m_pPrev ;
m_pParent = pRef->m_pParent ;
// sistemazione puntatori di elemento corrente
pRef->m_pPrev = this ;
// sistemazione puntatori di eventuale elemento precedente
if ( m_pPrev != nullptr)
m_pPrev->m_pNext = this ;
// sistemazione dell'eventuale padre
if ( m_pParent != nullptr) {
// se nuovo primo nodo
if ( m_pParent->m_pFirstObj == pRef)
m_pParent->m_pFirstObj = this ;
m_pParent->ObjCountInc() ;
}
return true ;
}
/*-------------------------------------------------------------------------*/
bool
GdbObj::Remove( void)
{
// sistemazione dell'eventuale padre
if ( m_pParent != nullptr) {
if ( m_pParent->m_pFirstObj == this)
m_pParent->m_pFirstObj = m_pNext ;
if ( m_pParent->m_pLastObj == this)
m_pParent->m_pLastObj = m_pPrev ;
m_pParent->ObjCountDec() ;
}
// sistemazione di eventuale precedente
if ( m_pPrev != nullptr)
m_pPrev->m_pNext = m_pNext ;
// sistemazione di eventuale successivo
if ( m_pNext != nullptr)
m_pNext->m_pPrev = m_pPrev ;
return true ;
}
//----------------------------------------------------------------------------
// Attribs
//----------------------------------------------------------------------------
bool
GdbObj::SaveAttribs( std::ostream& osOut) const
{
if ( m_pAttribs != nullptr)
return m_pAttribs->Save( osOut) ;
return true ;
}
//----------------------------------------------------------------------------
bool
GdbObj::LoadAttribs( Scanner& TheScanner)
{
if ( GetSafeAttribs() == nullptr)
return false ;
return m_pAttribs->Load( TheScanner) ;
}
//----------------------------------------------------------------------------
Attribs*
GdbObj::GetSafeAttribs( void)
{
if ( m_pAttribs == nullptr) {
Attribs* pAttribs = new (nothrow) Attribs ;
if ( pAttribs == nullptr)
return nullptr ;
m_pAttribs = pAttribs ;
}
return m_pAttribs ;
}
//----------------------------------------------------------------------------
bool
GdbObj::SetColor( Color cCol)
{
// recupero gli attributi (se non ci sono, vengono creati)
Attribs* pAttribs = GetSafeAttribs() ;
if ( pAttribs == nullptr)
return false ;
// assegno il colore
pAttribs->SetColor( cCol) ;
return true ;
}
//----------------------------------------------------------------------------
bool
GdbObj::GetColor( Color& cCol) const
{
// se non ci sono attributi o ci si deve riferire al padre
if ( m_pAttribs == nullptr ||
m_pAttribs->GetMaterial() == Attribs::MAT_PARENT) {
if ( GetParent() != nullptr)
return GetParent()->GetColor( cCol) ;
else
return false ;
}
// se il colore è definito
if ( m_pAttribs->GetMaterial() != Attribs::MAT_NULL) {
cCol = m_pAttribs->GetColor() ;
return true ;
}
return false ;
}