Skip to content

Commit

Permalink
removing interfaces from raster pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Jul 1, 2012
1 parent d2b01bc commit e331238
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 68 deletions.
4 changes: 2 additions & 2 deletions src/core/raster/qgsrasterlayer.cpp
Expand Up @@ -194,7 +194,7 @@ QgsRasterLayer::~QgsRasterLayer()
{
mValid = false;
delete mRasterShader;
delete mDataProvider; // delete in pipe ?
//delete mDataProvider; // deleted in pipe
//delete mRenderer;
//delete mResampleFilter;
}
Expand Down Expand Up @@ -1823,7 +1823,7 @@ void QgsRasterLayer::setDataProvider( QString const & provider )
void QgsRasterLayer::closeDataProvider()
{
mValid = false;
delete mDataProvider;
mPipe.remove( mDataProvider );
mDataProvider = 0;

mRasterStatsList.clear();
Expand Down
152 changes: 97 additions & 55 deletions src/core/raster/qgsrasterpipe.cpp
Expand Up @@ -23,38 +23,38 @@
#include <QByteArray>

QgsRasterPipe::QgsRasterPipe()
: mProvider(0)
, mRenderer(0)
, mResampleFilter(0)
, mProjector(0)
: mProvider( 0 )
, mRenderer( 0 )
, mResampleFilter( 0 )
, mProjector( 0 )
{
}

QgsRasterPipe::~QgsRasterPipe()
{
foreach ( QgsRasterInterface* filter, mFilters )
foreach( QgsRasterInterface* filter, mFilters )
{
delete filter;
}
}

bool QgsRasterPipe::connectFilters ( QVector<QgsRasterInterface*> theFilters )
bool QgsRasterPipe::connectFilters( QVector<QgsRasterInterface*> theFilters )
{
QgsDebugMsg( "Entered" );
for ( int i = 1; i < theFilters.size(); i++ )
for ( int i = 1; i < theFilters.size(); i++ )
{
if ( ! theFilters[i]->setInput ( theFilters[i-1] ) )
{
QgsDebugMsg( QString ( "cannot connect %1 to %2" ).arg( typeid(*(theFilters[i])).name() ).arg ( typeid(*(theFilters[i-1])).name() ) );
if ( ! theFilters[i]->setInput( theFilters[i-1] ) )
{
QgsDebugMsg( QString( "cannot connect %1 to %2" ).arg( typeid( *( theFilters[i] ) ).name() ).arg( typeid( *( theFilters[i-1] ) ).name() ) );
return false;
}
}
return true;
}

bool QgsRasterPipe::insert ( int idx, QgsRasterInterface* theFilter )
bool QgsRasterPipe::insert( int idx, QgsRasterInterface* theFilter )
{
QgsDebugMsg( QString ( "insert %1 at %2" ).arg( typeid(*theFilter).name() ).arg ( idx ) );
QgsDebugMsg( QString( "insert %1 at %2" ).arg( typeid( *theFilter ).name() ).arg( idx ) );
if ( idx > mFilters.size() )
{
idx = mFilters.size();
Expand All @@ -63,24 +63,24 @@ bool QgsRasterPipe::insert ( int idx, QgsRasterInterface* theFilter )
// of the whole pipe, because the types and band numbers may change
QVector<QgsRasterInterface*> filters = mFilters;

filters.insert ( idx, theFilter );
filters.insert( idx, theFilter );
bool success = false;
if ( connectFilters ( filters ) )
if ( connectFilters( filters ) )
{
success = true;
mFilters.insert ( idx, theFilter );
setRole ( theFilter, idx );
mFilters.insert( idx, theFilter );
setRole( theFilter, idx );
QgsDebugMsg( "inserted ok" );
}

// Connect or reconnect (after the test) filters
connectFilters ( mFilters );
return success;
connectFilters( mFilters );
return success;
}

bool QgsRasterPipe::replace ( int idx, QgsRasterInterface* theFilter )
bool QgsRasterPipe::replace( int idx, QgsRasterInterface* theFilter )
{
QgsDebugMsg( QString ( "replace by %1 at %2" ).arg( typeid(*theFilter).name() ).arg ( idx ) );
QgsDebugMsg( QString( "replace by %1 at %2" ).arg( typeid( *theFilter ).name() ).arg( idx ) );
if ( idx < 0 || idx >= mFilters.size() )
{
return false;
Expand All @@ -93,21 +93,21 @@ bool QgsRasterPipe::replace ( int idx, QgsRasterInterface* theFilter )

filters[idx] = theFilter;
bool success = false;
if ( connectFilters ( filters ) )
if ( connectFilters( filters ) )
{
success = true;
delete mFilters[idx];
mFilters[idx] = theFilter;
setRole ( theFilter, idx );
setRole( theFilter, idx );
QgsDebugMsg( "replaced ok" );
}

// Connect or reconnect (after the test) filters
connectFilters ( mFilters );
return success;
connectFilters( mFilters );
return success;
}

QgsRasterPipe::Role QgsRasterPipe::filterRole ( QgsRasterInterface * filter ) const
QgsRasterPipe::Role QgsRasterPipe::filterRole( QgsRasterInterface * filter ) const
{
if ( dynamic_cast<QgsRasterDataProvider *>( filter ) ) return ProviderRole;
if ( dynamic_cast<QgsRasterRenderer *>( filter ) ) return RendererRole;
Expand All @@ -116,16 +116,23 @@ QgsRasterPipe::Role QgsRasterPipe::filterRole ( QgsRasterInterface * filter ) co
return UnknownRole;
}

void QgsRasterPipe::setRole ( QgsRasterInterface * theFilter, int idx )
void QgsRasterPipe::setRole( QgsRasterInterface * theFilter, int idx )
{
Role role = filterRole ( theFilter );
Role role = filterRole( theFilter );
if ( role == UnknownRole ) return;
mRoleMap.insert ( role, idx );
mRoleMap.insert( role, idx );
}

void QgsRasterPipe::unsetRole( QgsRasterInterface * theFilter )
{
Role role = filterRole( theFilter );
if ( role == UnknownRole ) return;
mRoleMap.remove( role );
}

bool QgsRasterPipe::setFilter( QgsRasterInterface* theFilter )
{
QgsDebugMsg( QString ( "%1" ).arg( typeid(*theFilter).name() ) );
QgsDebugMsg( QString( "%1" ).arg( typeid( *theFilter ).name() ) );

if ( !theFilter ) return false;

Expand All @@ -134,28 +141,28 @@ bool QgsRasterPipe::setFilter( QgsRasterInterface* theFilter )
QgsRasterResampleFilter * resampleFilter;
QgsRasterProjector * projector;

Role role = filterRole ( theFilter );
Role role = filterRole( theFilter );

// We dont know where to place unknown filter
if ( role == UnknownRole ) return false;

//if ( mFiltersMap.value ( role ) )
if ( mRoleMap.contains ( role ) )
if ( mRoleMap.contains( role ) )
{
// An old filter of the same role exists -> replace
// replace may still fail and return false
return replace ( mRoleMap.value(role), theFilter );
return replace( mRoleMap.value( role ), theFilter );
}

int idx = 0;
int idx = 0;

// Not found, find the best default position for this kind of filter
// QgsRasterDataProvider - ProviderRole
// QgsRasterRenderer - RendererRole
// QgsRasterResampler - ResamplerRole
// QgsRasterProjector - ProjectorRole

int providerIdx = mRoleMap.value( ProviderRole,-1);
int providerIdx = mRoleMap.value( ProviderRole, -1 );
int rendererIdx = mRoleMap.value( RendererRole, -1 );
int resamplerIdx = mRoleMap.value( ResamplerRole, -1 );

Expand All @@ -169,43 +176,78 @@ bool QgsRasterPipe::setFilter( QgsRasterInterface* theFilter )
}
else if ( role == ResamplerRole )
{
idx = qMax ( providerIdx, rendererIdx ) + 1;
idx = qMax( providerIdx, rendererIdx ) + 1;
}
else if ( role == ProjectorRole )
{
idx = qMax ( qMax( providerIdx,rendererIdx ), resamplerIdx ) + 1;
idx = qMax( qMax( providerIdx, rendererIdx ), resamplerIdx ) + 1;
}

return insert ( idx, theFilter ); // insert may still fail and return false
return insert( idx, theFilter ); // insert may still fail and return false
}

QgsRasterInterface * QgsRasterPipe::filter ( Role role ) const
QgsRasterInterface * QgsRasterPipe::filter( Role role ) const
{
QgsDebugMsg( QString ( "role = %1" ).arg ( role ) );
if ( mRoleMap.contains ( role ) )
QgsDebugMsg( QString( "role = %1" ).arg( role ) );
if ( mRoleMap.contains( role ) )
{
return mFilters.value( mRoleMap.value( role) );
return mFilters.value( mRoleMap.value( role ) );
}
return 0;
}

QgsRasterDataProvider * QgsRasterPipe::provider() const
{
return dynamic_cast<QgsRasterDataProvider *>(filter( ProviderRole));
QgsRasterDataProvider * QgsRasterPipe::provider() const
{
return dynamic_cast<QgsRasterDataProvider *>( filter( ProviderRole ) );
}

QgsRasterRenderer * QgsRasterPipe::renderer() const
{
return dynamic_cast<QgsRasterRenderer *>(filter( RendererRole ));
QgsRasterRenderer * QgsRasterPipe::renderer() const
{
return dynamic_cast<QgsRasterRenderer *>( filter( RendererRole ) );
}

QgsRasterResampleFilter * QgsRasterPipe::resampleFilter() const
{
return dynamic_cast<QgsRasterResampleFilter *>(filter( ResamplerRole ));
}
QgsRasterResampleFilter * QgsRasterPipe::resampleFilter() const
{
return dynamic_cast<QgsRasterResampleFilter *>( filter( ResamplerRole ) );
}

QgsRasterProjector * QgsRasterPipe::projector() const
{
return dynamic_cast<QgsRasterProjector*>( filter( ProjectorRole ) );
}

bool QgsRasterPipe::remove( int idx )
{
QgsDebugMsg( QString( "remove at %1" ).arg( idx ) );

if ( idx < 0 || idx >= mFilters.size() )
{
return false;
}

// make a copy of pipe to test connection, we test the connections
// of the whole pipe, because the types and band numbers may change
QVector<QgsRasterInterface*> filters = mFilters;

QgsRasterProjector * QgsRasterPipe::projector() const
{
return dynamic_cast<QgsRasterProjector*>(filter ( ProjectorRole ));
filters.remove( idx );
bool success = false;
if ( connectFilters( filters ) )
{
success = true;
unsetRole( mFilters[idx] );
delete mFilters[idx];
mFilters.remove( idx );
QgsDebugMsg( "removed ok" );
}

// Connect or reconnect (after the test) filters
connectFilters( mFilters );
return success;
}

bool QgsRasterPipe::remove( QgsRasterInterface * theFilter )
{
if ( !theFilter ) return false;

return remove( mFilters.indexOf( theFilter ) );
}
32 changes: 21 additions & 11 deletions src/core/raster/qgsrasterpipe.h
Expand Up @@ -52,25 +52,32 @@ class CORE_EXPORT QgsRasterPipe //: public QObject

/** \brief Try to connect filters in pipe and to the provider at beginning.
Returns true if connected or false if connection failed */
bool connectFilters ( QVector<QgsRasterInterface*> theFilters );
bool connectFilters( QVector<QgsRasterInterface*> theFilters );


/** Try to insert filter at specified index and connect
* if connection would fail, the filter is not inserted and false is returned */
bool insert ( int idx, QgsRasterInterface* theFilter );
bool insert( int idx, QgsRasterInterface* theFilter );

/** Try to replace filter at specified index and connect
* if connection would fail, the filter is not inserted and false is returned */
bool replace ( int idx, QgsRasterInterface* theFilter );
bool replace( int idx, QgsRasterInterface* theFilter );

/** Insert a new filter in prefered place or replace similar filter if it
/** Insert a new filter in prefered place or replace similar filter if it
* already exists */
bool setFilter ( QgsRasterInterface * theFilter );
bool setFilter( QgsRasterInterface * theFilter );

QgsRasterInterface * filter ( Role role ) const;
/** Get known filter by role */
QgsRasterInterface * filter( Role role ) const;

/** Remove and delete filter at given index if possible */
bool remove( int idx );

/** Remove and delete filter from pipe if possible */
bool remove( QgsRasterInterface * theFilter );

int size() { return mFilters.size(); }
QgsRasterInterface * at( int idx ) { return mFilters.at(idx); }
QgsRasterInterface * at( int idx ) { return mFilters.at( idx ); }
QgsRasterInterface * last() { return mFilters.last(); }

// Getters for special types of interfaces
Expand All @@ -80,8 +87,8 @@ class CORE_EXPORT QgsRasterPipe //: public QObject
QgsRasterProjector * projector() const;

private:
/** Get known parent type_info of interface parent */
Role filterRole ( QgsRasterInterface * filter ) const;
/** Get known parent type_info of interface parent */
Role filterRole( QgsRasterInterface * filter ) const;

// Filters in pipe, the first is always provider
QVector<QgsRasterInterface*> mFilters;
Expand All @@ -92,10 +99,13 @@ class CORE_EXPORT QgsRasterPipe //: public QObject
QgsRasterResampleFilter * mResampleFilter;
QgsRasterProjector * mProjector;

QMap<Role,int> mRoleMap;
QMap<Role, int> mRoleMap;

// Set role in mFiltersMap
void setRole ( QgsRasterInterface * theFilter, int idx );
void setRole( QgsRasterInterface * theFilter, int idx );

// Unset role in mFiltersMap
void unsetRole( QgsRasterInterface * theFilter );
};

#endif
Expand Down

0 comments on commit e331238

Please sign in to comment.