2323#include < QByteArray>
2424
2525QgsRasterPipe::QgsRasterPipe ()
26- : mProvider(0 )
27- , mRenderer(0 )
28- , mResampleFilter(0 )
29- , mProjector(0 )
26+ : mProvider( 0 )
27+ , mRenderer( 0 )
28+ , mResampleFilter( 0 )
29+ , mProjector( 0 )
3030{
3131}
3232
3333QgsRasterPipe::~QgsRasterPipe ()
3434{
35- foreach ( QgsRasterInterface* filter, mFilters )
35+ foreach ( QgsRasterInterface* filter, mFilters )
3636 {
3737 delete filter;
3838 }
3939}
4040
41- bool QgsRasterPipe::connectFilters ( QVector<QgsRasterInterface*> theFilters )
41+ bool QgsRasterPipe::connectFilters ( QVector<QgsRasterInterface*> theFilters )
4242{
4343 QgsDebugMsg ( " Entered" );
44- for ( int i = 1 ; i < theFilters.size (); i++ )
44+ for ( int i = 1 ; i < theFilters.size (); i++ )
4545 {
46- if ( ! theFilters[i]->setInput ( theFilters[i-1 ] ) )
47- {
48- QgsDebugMsg ( QString ( " cannot connect %1 to %2" ).arg ( typeid (*( theFilters[i]) ).name () ).arg ( typeid (*( theFilters[i-1 ]) ).name () ) );
46+ if ( ! theFilters[i]->setInput ( theFilters[i-1 ] ) )
47+ {
48+ QgsDebugMsg ( QString ( " cannot connect %1 to %2" ).arg ( typeid ( *( theFilters[i] ) ).name () ).arg ( typeid ( *( theFilters[i-1 ] ) ).name () ) );
4949 return false ;
5050 }
5151 }
5252 return true ;
5353}
5454
55- bool QgsRasterPipe::insert ( int idx, QgsRasterInterface* theFilter )
55+ bool QgsRasterPipe::insert ( int idx, QgsRasterInterface* theFilter )
5656{
57- QgsDebugMsg ( QString ( " insert %1 at %2" ).arg ( typeid (*theFilter).name () ).arg ( idx ) );
57+ QgsDebugMsg ( QString ( " insert %1 at %2" ).arg ( typeid ( *theFilter ).name () ).arg ( idx ) );
5858 if ( idx > mFilters .size () )
5959 {
6060 idx = mFilters .size ();
@@ -63,24 +63,24 @@ bool QgsRasterPipe::insert ( int idx, QgsRasterInterface* theFilter )
6363 // of the whole pipe, because the types and band numbers may change
6464 QVector<QgsRasterInterface*> filters = mFilters ;
6565
66- filters.insert ( idx, theFilter );
66+ filters.insert ( idx, theFilter );
6767 bool success = false ;
68- if ( connectFilters ( filters ) )
68+ if ( connectFilters ( filters ) )
6969 {
7070 success = true ;
71- mFilters .insert ( idx, theFilter );
72- setRole ( theFilter, idx );
71+ mFilters .insert ( idx, theFilter );
72+ setRole ( theFilter, idx );
7373 QgsDebugMsg ( " inserted ok" );
7474 }
7575
7676 // Connect or reconnect (after the test) filters
77- connectFilters ( mFilters );
78- return success;
77+ connectFilters ( mFilters );
78+ return success;
7979}
8080
81- bool QgsRasterPipe::replace ( int idx, QgsRasterInterface* theFilter )
81+ bool QgsRasterPipe::replace ( int idx, QgsRasterInterface* theFilter )
8282{
83- QgsDebugMsg ( QString ( " replace by %1 at %2" ).arg ( typeid (*theFilter).name () ).arg ( idx ) );
83+ QgsDebugMsg ( QString ( " replace by %1 at %2" ).arg ( typeid ( *theFilter ).name () ).arg ( idx ) );
8484 if ( idx < 0 || idx >= mFilters .size () )
8585 {
8686 return false ;
@@ -93,21 +93,21 @@ bool QgsRasterPipe::replace ( int idx, QgsRasterInterface* theFilter )
9393
9494 filters[idx] = theFilter;
9595 bool success = false ;
96- if ( connectFilters ( filters ) )
96+ if ( connectFilters ( filters ) )
9797 {
9898 success = true ;
9999 delete mFilters [idx];
100100 mFilters [idx] = theFilter;
101- setRole ( theFilter, idx );
101+ setRole ( theFilter, idx );
102102 QgsDebugMsg ( " replaced ok" );
103103 }
104104
105105 // Connect or reconnect (after the test) filters
106- connectFilters ( mFilters );
107- return success;
106+ connectFilters ( mFilters );
107+ return success;
108108}
109109
110- QgsRasterPipe::Role QgsRasterPipe::filterRole ( QgsRasterInterface * filter ) const
110+ QgsRasterPipe::Role QgsRasterPipe::filterRole ( QgsRasterInterface * filter ) const
111111{
112112 if ( dynamic_cast <QgsRasterDataProvider *>( filter ) ) return ProviderRole;
113113 if ( dynamic_cast <QgsRasterRenderer *>( filter ) ) return RendererRole;
@@ -116,16 +116,23 @@ QgsRasterPipe::Role QgsRasterPipe::filterRole ( QgsRasterInterface * filter ) co
116116 return UnknownRole;
117117}
118118
119- void QgsRasterPipe::setRole ( QgsRasterInterface * theFilter, int idx )
119+ void QgsRasterPipe::setRole ( QgsRasterInterface * theFilter, int idx )
120120{
121- Role role = filterRole ( theFilter );
121+ Role role = filterRole ( theFilter );
122122 if ( role == UnknownRole ) return ;
123- mRoleMap .insert ( role, idx );
123+ mRoleMap .insert ( role, idx );
124+ }
125+
126+ void QgsRasterPipe::unsetRole ( QgsRasterInterface * theFilter )
127+ {
128+ Role role = filterRole ( theFilter );
129+ if ( role == UnknownRole ) return ;
130+ mRoleMap .remove ( role );
124131}
125132
126133bool QgsRasterPipe::setFilter ( QgsRasterInterface* theFilter )
127134{
128- QgsDebugMsg ( QString ( " %1" ).arg ( typeid (*theFilter).name () ) );
135+ QgsDebugMsg ( QString ( " %1" ).arg ( typeid ( *theFilter ).name () ) );
129136
130137 if ( !theFilter ) return false ;
131138
@@ -134,28 +141,28 @@ bool QgsRasterPipe::setFilter( QgsRasterInterface* theFilter )
134141 QgsRasterResampleFilter * resampleFilter;
135142 QgsRasterProjector * projector;
136143
137- Role role = filterRole ( theFilter );
144+ Role role = filterRole ( theFilter );
138145
139146 // We dont know where to place unknown filter
140147 if ( role == UnknownRole ) return false ;
141-
148+
142149 // if ( mFiltersMap.value ( role ) )
143- if ( mRoleMap .contains ( role ) )
150+ if ( mRoleMap .contains ( role ) )
144151 {
145152 // An old filter of the same role exists -> replace
146153 // replace may still fail and return false
147- return replace ( mRoleMap .value (role), theFilter );
154+ return replace ( mRoleMap .value ( role ), theFilter );
148155 }
149156
150- int idx = 0 ;
157+ int idx = 0 ;
151158
152159 // Not found, find the best default position for this kind of filter
153160 // QgsRasterDataProvider - ProviderRole
154161 // QgsRasterRenderer - RendererRole
155162 // QgsRasterResampler - ResamplerRole
156163 // QgsRasterProjector - ProjectorRole
157164
158- int providerIdx = mRoleMap .value ( ProviderRole,- 1 );
165+ int providerIdx = mRoleMap .value ( ProviderRole, - 1 );
159166 int rendererIdx = mRoleMap .value ( RendererRole, -1 );
160167 int resamplerIdx = mRoleMap .value ( ResamplerRole, -1 );
161168
@@ -169,43 +176,78 @@ bool QgsRasterPipe::setFilter( QgsRasterInterface* theFilter )
169176 }
170177 else if ( role == ResamplerRole )
171178 {
172- idx = qMax ( providerIdx, rendererIdx ) + 1 ;
179+ idx = qMax ( providerIdx, rendererIdx ) + 1 ;
173180 }
174181 else if ( role == ProjectorRole )
175182 {
176- idx = qMax ( qMax ( providerIdx,rendererIdx ), resamplerIdx ) + 1 ;
183+ idx = qMax ( qMax ( providerIdx, rendererIdx ), resamplerIdx ) + 1 ;
177184 }
178185
179- return insert ( idx, theFilter ); // insert may still fail and return false
186+ return insert ( idx, theFilter ); // insert may still fail and return false
180187}
181188
182- QgsRasterInterface * QgsRasterPipe::filter ( Role role ) const
189+ QgsRasterInterface * QgsRasterPipe::filter ( Role role ) const
183190{
184- QgsDebugMsg ( QString ( " role = %1" ).arg ( role ) );
185- if ( mRoleMap .contains ( role ) )
191+ QgsDebugMsg ( QString ( " role = %1" ).arg ( role ) );
192+ if ( mRoleMap .contains ( role ) )
186193 {
187- return mFilters .value ( mRoleMap .value ( role) );
194+ return mFilters .value ( mRoleMap .value ( role ) );
188195 }
189196 return 0 ;
190197}
191198
192- QgsRasterDataProvider * QgsRasterPipe::provider () const
193- {
194- return dynamic_cast <QgsRasterDataProvider *>(filter ( ProviderRole));
199+ QgsRasterDataProvider * QgsRasterPipe::provider () const
200+ {
201+ return dynamic_cast <QgsRasterDataProvider *>( filter ( ProviderRole ) );
195202}
196203
197- QgsRasterRenderer * QgsRasterPipe::renderer () const
198- {
199- return dynamic_cast <QgsRasterRenderer *>(filter ( RendererRole ));
204+ QgsRasterRenderer * QgsRasterPipe::renderer () const
205+ {
206+ return dynamic_cast <QgsRasterRenderer *>( filter ( RendererRole ) );
200207}
201208
202- QgsRasterResampleFilter * QgsRasterPipe::resampleFilter () const
203- {
204- return dynamic_cast <QgsRasterResampleFilter *>(filter ( ResamplerRole ));
205- }
209+ QgsRasterResampleFilter * QgsRasterPipe::resampleFilter () const
210+ {
211+ return dynamic_cast <QgsRasterResampleFilter *>( filter ( ResamplerRole ) );
212+ }
213+
214+ QgsRasterProjector * QgsRasterPipe::projector () const
215+ {
216+ return dynamic_cast <QgsRasterProjector*>( filter ( ProjectorRole ) );
217+ }
218+
219+ bool QgsRasterPipe::remove ( int idx )
220+ {
221+ QgsDebugMsg ( QString ( " remove at %1" ).arg ( idx ) );
222+
223+ if ( idx < 0 || idx >= mFilters .size () )
224+ {
225+ return false ;
226+ }
227+
228+ // make a copy of pipe to test connection, we test the connections
229+ // of the whole pipe, because the types and band numbers may change
230+ QVector<QgsRasterInterface*> filters = mFilters ;
206231
207- QgsRasterProjector * QgsRasterPipe::projector () const
208- {
209- return dynamic_cast <QgsRasterProjector*>(filter ( ProjectorRole ));
232+ filters.remove ( idx );
233+ bool success = false ;
234+ if ( connectFilters ( filters ) )
235+ {
236+ success = true ;
237+ unsetRole ( mFilters [idx] );
238+ delete mFilters [idx];
239+ mFilters .remove ( idx );
240+ QgsDebugMsg ( " removed ok" );
241+ }
242+
243+ // Connect or reconnect (after the test) filters
244+ connectFilters ( mFilters );
245+ return success;
210246}
211247
248+ bool QgsRasterPipe::remove ( QgsRasterInterface * theFilter )
249+ {
250+ if ( !theFilter ) return false ;
251+
252+ return remove ( mFilters .indexOf ( theFilter ) );
253+ }
0 commit comments