OTB  6.7.0
Orfeo Toolbox
mvdStackedLayerModel.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef mvdStackedLayerModel_h
22 #define mvdStackedLayerModel_h
23 
24 //
25 // Configuration include.
27 #include "ConfigureMonteverdi.h"
28 
29 #include "OTBMonteverdiCoreExport.h"
30 
31 /*****************************************************************************/
32 /* INCLUDE SECTION */
33 
34 //
35 // Qt includes (sorted by alphabetic order)
37 #include <QtCore>
38 
39 //
40 // System includes (sorted by alphabetic order)
41 
42 //
43 // ITK includes (sorted by alphabetic order)
44 
45 //
46 // OTB includes (sorted by alphabetic order)
47 
48 //
49 // Monteverdi includes (sorted by alphabetic order)
50 #include "mvdAbstractModel.h"
51 #include "mvdTypes.h"
52 
53 /*****************************************************************************/
54 /* PRE-DECLARATION SECTION */
55 
56 //
57 // External classes pre-declaration.
58 namespace
59 {
60 }
61 
62 namespace mvd
63 {
64 //
65 // Internal classes pre-declaration.
66 class AbstractLayerModel;
67 
68 /*****************************************************************************/
69 /* CLASS DEFINITION SECTION */
70 
78 class OTBMonteverdiCore_EXPORT StackedLayerModel :
79  public AbstractModel
80 {
81 
82  /*-[ QOBJECT SECTION ]-----------------------------------------------------*/
83 
84  Q_OBJECT;
85 
86 //
87 // Private types
88 private:
89  // typedef std::vector< AbstractLayerModel * > LayerModelVector;
90  typedef std::map< std::string, AbstractLayerModel * > LayerModelMap;
91 
92  // typedef std::pair< AbstractLayerModel *, void * > LayerModelPair;
93  // typedef std::map< std::string, LayerDataPair > LayerModelPairMap;
94 
95  /*-[ PUBLIC SECTION ]------------------------------------------------------*/
96 
97 //
98 // Public types.
99 public:
100  typedef std::vector< LayerModelMap::key_type > KeyVector;
101 
102  typedef LayerModelMap::size_type SizeType;
103  typedef LayerModelMap::key_type KeyType;
104  typedef LayerModelMap::const_iterator ConstIterator;
105  static const SizeType NIL_INDEX;
106 
107 //
108 // Public methods.
109 public:
110 
112  StackedLayerModel( QObject* p =NULL );
113 
115  ~StackedLayerModel() override;
116 
117  inline const AbstractLayerModel * operator[]( SizeType ) const;
118  inline AbstractLayerModel * operator[]( SizeType );
119 
120  KeyType Add( AbstractLayerModel * );
121 
122  inline const AbstractLayerModel * At( SizeType ) const;
123  inline AbstractLayerModel * At( SizeType );
124 
125  inline ConstIterator Begin() const;
126 
127  inline void BeginEditPixelInfo() {};
128 
130 
131  inline bool Contains( const KeyType & key ) const;
132  bool Contains( const AbstractLayerModel * ) const;
133 
134  void CountSRT( size_t & unknown,
135  size_t & carto,
136  size_t & geo,
137  size_t & sensor ) const;
138 
139  inline ConstIterator End() const;
140 
141  inline void EndEditPixelInfo( const QPoint &, const PointType & );
142 
143  void EndEditResolutions();
144 
145  /*
146  inline const AbstractLayerModel * Front() const;
147  inline AbstractLayerModel * Front();
148  */
149  inline ConstIterator Find( const AbstractLayerModel * ) const;
150 
151  inline const AbstractLayerModel * Get( const KeyType & ) const;
152  inline AbstractLayerModel * Get( const KeyType & );
153 
154  inline SizeType GetCount() const;
155 
156  inline const KeyType & GetKey( SizeType ) const;
157 
158  inline const KeyVector & GetKeys() const;
159 
160  template< typename T >
161  T * GetCurrent() const;
162 
163  template< typename T >
164  T * GetCurrent();
165 
166  AbstractLayerModel * GetCurrent() const;
167  AbstractLayerModel * GetCurrent();
168 
169  inline SizeType GetCurrentIndex() const;
170 
171  inline const KeyType & GetCurrentKey() const;
172 
173  template< typename T >
174  T * GetReference() const;
175 
176  template< typename T >
177  T * GetReference();
178 
179  AbstractLayerModel * GetReference() const;
180  AbstractLayerModel * GetReference();
181 
182  inline SizeType GetReferenceIndex() const;
183 
184  inline bool HasCurrent() const;
185 
186  inline bool HasReference() const;
187 
188  inline SizeType IndexOf( const AbstractLayerModel * ) const;
189 
190  KeyType Insert( AbstractLayerModel *, SizeType index );
191 
192  inline bool IsEmpty() const;
193 
194  inline const PixelInfo::Vector & PixelInfos() const;
195  inline PixelInfo::Vector & PixelInfos();
196 
197  inline void SetCurrent( SizeType, bool =false );
198  void SetCurrent( const KeyType & );
199  void SetCurrent( const AbstractLayerModel * );
200 
201  inline void SetReference( SizeType, bool =false );
202  void SetReference( const KeyType & );
203  void SetReference( const AbstractLayerModel * );
204 
205  /*-[ PUBLIC SLOTS SECTION ]------------------------------------------------*/
206 
207 //
208 // Public SLOTS.
209 public slots:
210  void Clear();
211  void Deleting( unsigned int index );
212  inline void Delete( const KeyType & );
213  inline void DeleteCurrent();
214  inline void LowerCurrent();
215  inline void MoveCurrentToBottom();
216  inline void MoveCurrentToTop();
217  inline void RaiseCurrent();
218  inline void RotateLayers( int );
219  inline void SelectFirst();
220  inline void SelectLast();
221  inline void SelectPrevious();
222  inline void SelectNext();
223 
224  /*-[ SIGNALS SECTION ]-----------------------------------------------------*/
225 
226 //
227 // Signals.
228 signals:
229  void AboutToChangeSelectedLayerModel( const StackedLayerModel::KeyType & );
230  void SelectedLayerModelChanged( const StackedLayerModel::KeyType & );
231 
232  void ContentAboutToBeChanged();
233  void ContentAboutToBeReset();
234  void ContentChanged();
235  void ContentReset();
236 
237  void CurrentAboutToBeChanged( size_t );
238  void CurrentChanged( size_t );
239 
240  void LayerAboutToBeDeleted( size_t index );
241  void LayerAdded( size_t index );
242  void LayerDeleted( size_t index );
243 
244  void OrderAboutToBeChanged();
245  void OrderChanged();
246 
247  void PixelInfoChanged( const QPoint &, const PointType &, const PixelInfo::Vector & );
248 
249  void ReferenceAboutToBeChanged( size_t );
250  void ReferenceChanged( size_t );
251 
252  void ResolutionsChanged( const PixelInfo::Vector & );
253 
254  void LayerRenamed();
255 
256  /*-[ PROTECTED SECTION ]---------------------------------------------------*/
257 
258 //
259 // Protected methods.
260 protected:
261 
262 //
263 // Protected attributes.
264 protected:
265 
266  /*-[ PRIVATE SECTION ]-----------------------------------------------------*/
267 
268 //
269 // Private methods.
270 private:
271  static KeyType GenerateKey( AbstractLayerModel * );
272 
273  inline void ClearPixelInfos();
274 
275  void Delete( SizeType );
276 
277  inline SizeType FindKey( const KeyType & ) const;
278 
279  inline SizeType Next( SizeType );
280  inline SizeType Prev( SizeType );
281 
282  void RaiseLayer( SizeType );
283  void LowerLayer( SizeType );
284 
285  void RotateLayerUp( SizeType );
286  void RotateLayerDown( SizeType );
287 
288  void MoveTo( SizeType index, SizeType position );
289  void MoveToTop( SizeType );
290  void MoveToBottom( SizeType );
291 
292  // inline void Swap( SizeType, SizeType );
293 
294  inline const KeyType & KeyOf( const AbstractLayerModel * ) const;
295 
296 //
297 // Private attributes.
298 private:
299  static const KeyType NIL_KEY;
300 
302 
307 
308  // Usually, information is a singular term which denotes a
309  // plural. Since there's a container of several pixel-infos and we
310  // usually name container variables by using the plural form, an 's'
311  // is appended to PixelInfo.
313 
314  /*-[ PRIVATE SLOTS SECTION ]-----------------------------------------------*/
315 
316 //
317 // Slots.
318 private slots:
319 };
320 
321 #if OTB_DEBUG
322 inline void trace( const StringVector & );
323 #endif
324 
325 } // end namespace 'mvd'.
326 
327 /*****************************************************************************/
328 /* INLINE SECTION */
329 
330 //
331 // Qt includes (sorted by alphabetic order)
333 
334 //
335 // System includes (sorted by alphabetic order)
336 
337 //
338 // ITK includes (sorted by alphabetic order)
339 
340 //
341 // OTB includes (sorted by alphabetic order)
342 
343 //
344 // Monteverdi includes (sorted by alphabetic order)
345 
346 namespace mvd
347 {
348 
349 /*****************************************************************************/
350 inline
351 const AbstractLayerModel *
354 {
355  return At( i );
356 }
357 
358 /*****************************************************************************/
359 inline
363 {
364  return At( i );
365 }
366 
367 /*****************************************************************************/
368 inline
369 const AbstractLayerModel *
371 ::At( SizeType i ) const
372 {
373  return const_cast< StackedLayerModel * >( this )->At( i );
374 }
375 
376 /*****************************************************************************/
377 inline
381 {
382  if( i>=GetCount() )
383  return NULL;
384 
385  assert( !GetKey( i ).empty() );
386 
387  LayerModelMap::const_iterator it(
388  m_LayerModels.find( GetKey( i ) )
389  );
390 
391  assert( it!=m_LayerModels.end() );
392 
393  return it->second;
394 }
395 
396 /*****************************************************************************/
397 inline
400 ::Begin() const
401 {
402  return m_LayerModels.begin();
403 }
404 
405 /*****************************************************************************/
406 inline
407 void
410 {
411  m_PixelInfos.reserve( 0 );
412 }
413 
414 /*****************************************************************************/
415 inline
416 bool
418 ::Contains( const KeyType & key ) const
419 {
420  return m_LayerModels.find( key )!=m_LayerModels.end();
421 }
422 
423 /*****************************************************************************/
424 inline
425 void
427 ::Delete( const KeyType & key )
428 {
429  // qDebug() << this << "::Delete(" << key << ")";
430 
431  Delete( FindKey( key ) );
432 }
433 
434 /*****************************************************************************/
435 inline
436 void
439 {
440  // qDebug() << this << "::DeleteCurrent()";
441 
442  Delete( m_Current );
443 }
444 
445 /*****************************************************************************/
446 inline
449 ::FindKey( const KeyType & key ) const
450 {
451  KeyVector::const_iterator it( std::find( m_Keys.begin(), m_Keys.end(), key ) );
452 
453  if( it==m_Keys.end() )
455 
456  return std::distance( m_Keys.begin(), it );
457 }
458 
459 /*****************************************************************************/
460 inline
463 ::GetCount() const
464 {
465  return m_LayerModels.size();
466 }
467 
468 /*****************************************************************************/
469 inline
472 ::GetKeys() const
473 {
474  return m_Keys;
475 }
476 
477 /*****************************************************************************/
478 inline
479 bool
482 {
483  return m_Current<GetCount();
484 }
485 
486 /*****************************************************************************/
487 inline
488 bool
491 {
492  return m_Reference<GetCount();
493 }
494 
495 /*****************************************************************************/
496 inline
499 ::End() const
500 {
501  return m_LayerModels.end();
502 }
503 
504 /*****************************************************************************/
505 inline
506 void
508 ::EndEditPixelInfo( const QPoint & screen, const PointType & view )
509 {
510  emit PixelInfoChanged( screen, view, m_PixelInfos );
511 }
512 
516 ::Find( const AbstractLayerModel * layer ) const
517 {
518  for( LayerModelMap::const_iterator it( m_LayerModels.begin() );
519  it!=m_LayerModels.end();
520  ++it )
521  if( it->second==layer )
522  return it;
524 
525  return m_LayerModels.end();
526 }
527 
528 /*****************************************************************************/
529 const AbstractLayerModel *
531 ::Get( const KeyType & key ) const
532 {
533  return const_cast< StackedLayerModel * >( this )->Get( key );
534 }
535 
536 /*****************************************************************************/
539 ::Get( const KeyType & key )
540 {
541  ConstIterator it( m_LayerModels.find( key ) );
542 
543  return
544  it==m_LayerModels.end()
545  ? NULL
546  : it->second;
547 }
548 
549 /*****************************************************************************/
550 template< typename T >
551 inline
552 T *
555 {
556  return dynamic_cast< T * >( GetCurrent() );
557 }
558 
559 /*****************************************************************************/
560 template< typename T >
561 inline
562 T *
565 {
566  return dynamic_cast< T * >( GetCurrent() );
567 }
568 
569 /*****************************************************************************/
570 inline
574 {
575  if( m_Current>=GetCount() )
576  return NULL;
577 
578  LayerModelMap::const_iterator it(
579  m_LayerModels.find( m_Keys[ m_Current ] )
580  );
581 
582  if( it==m_LayerModels.end() )
583  return NULL;
584 
585  return it->second;
586 }
587 
588 /*****************************************************************************/
589 inline
593 {
594  return const_cast< StackedLayerModel * >( this )->GetCurrent();
595 }
596 
597 /*****************************************************************************/
598 inline
602 {
603  return m_Current;
604 }
605 
606 /*****************************************************************************/
607 inline
611 {
612  return GetKey( m_Current );
613 }
614 
615 /*****************************************************************************/
616 inline
619 ::GetKey( SizeType index ) const
620 {
621  if( index>=GetCount() )
623 
624  return m_Keys[ index ];
625 }
626 
627 /*****************************************************************************/
628 template< typename T >
629 inline
630 T *
633 {
634  return dynamic_cast< T * >( GetReference() );
635 }
636 
637 /*****************************************************************************/
638 template< typename T >
639 inline
640 T *
643 {
644  return dynamic_cast< T * >( GetReference() );
645 }
646 
647 /*****************************************************************************/
648 inline
652 {
653  if( m_Reference>=GetCount() )
654  return NULL;
655 
656  LayerModelMap::const_iterator it(
657  m_LayerModels.find( m_Keys[ m_Reference ] )
658  );
659 
660  if( it==m_LayerModels.end() )
661  return NULL;
662 
663  return it->second;
664 }
665 
666 /*****************************************************************************/
667 inline
671 {
672  return const_cast< StackedLayerModel * >( this )->GetReference();
673 }
674 
675 /*****************************************************************************/
676 inline
680 {
681  return m_Reference;
682 }
683 
684 
685 /*****************************************************************************/
686 inline
689 ::IndexOf( const AbstractLayerModel * layer ) const
690 {
691  KeyType key( KeyOf( layer ) );
692 
693  if( key==StackedLayerModel::NIL_KEY )
695 
696  for( KeyVector::size_type i=0;
697  i<m_Keys.size();
698  ++i )
699  if( m_Keys[ i ]==key )
700  return i;
701 
703 }
704 
705 /*****************************************************************************/
706 inline
707 bool
709 ::IsEmpty() const
710 {
711  return m_LayerModels.empty();
712 }
713 
715 inline
718 ::KeyOf( const AbstractLayerModel * layerModel ) const
719 {
720  for( LayerModelMap::const_iterator it( m_LayerModels.begin() );
721  it!=m_LayerModels.end();
722  ++it )
723  if( it->second==layerModel )
724  return it->first;
726 
728 }
729 
730 /*****************************************************************************/
731 inline
732 void
735 {
736  if( GetCount()<2 )
737  return;
738 
739  assert( m_Current<GetCount() );
740 
741  if( m_Current==GetCount()-1 )
742  RotateLayerDown( 1 );
743  else
744  LowerLayer( m_Current );
745 }
746 
747 /*****************************************************************************/
748 void
751 {
752  if( m_Current>=GetCount() )
753  return;
754 
755  MoveToBottom( m_Current );
756 }
757 
758 /*****************************************************************************/
759 void
762 {
763  if( m_Current>=GetCount() )
764  return;
765 
766  MoveToTop( m_Current );
767 }
768 
769 /*****************************************************************************/
770 inline
773 ::Next( SizeType index )
774 {
775  return
776  index>=GetCount()
777  ? 0
778  : ( index + 1 ) % GetCount();
779 }
780 
781 /*****************************************************************************/
782 inline
783 const PixelInfo::Vector &
786 {
787  return m_PixelInfos;
788 }
789 
790 /*****************************************************************************/
791 inline
795 {
796  return m_PixelInfos;
797 }
798 
799 /*****************************************************************************/
800 inline
803 ::Prev( SizeType index )
804 {
805  return
806  index>=GetCount() || index==0
807  ? GetCount() - 1
808  : index - 1;
809 }
810 
811 /*****************************************************************************/
812 inline
813 void
816 {
817  if( GetCount()<2 )
818  return;
819 
820  assert( m_Current<GetCount() );
821 
822  if( m_Current==0 )
823  RotateLayerUp( 1 );
824  else
825  RaiseLayer( m_Current );
826 }
827 
828 /*****************************************************************************/
829 inline
830 void
832 ::RotateLayers( int steps )
833 {
834  if( IsEmpty() )
835  return;
836 
837  if( steps>=0 )
838  RotateLayerUp( steps % GetCount() );
839 
840  else
841  RotateLayerDown( ( -steps ) % GetCount() );
842 }
843 
844 /*****************************************************************************/
845 inline
846 void
849 {
850  if( GetCount()<1 )
851  return;
852 
853  SetCurrent( SizeType( 0 ) );
854 }
855 
856 /*****************************************************************************/
857 inline
858 void
861 {
862  if( GetCount()<1 )
863  return;
864 
865  SetCurrent( m_Keys.size() - 1 );
866 }
867 
868 /*****************************************************************************/
869 inline
870 void
873 {
874  if( IsEmpty() )
875  return;
876 
877  SetCurrent( Next( m_Current ) );
878 }
879 
880 /*****************************************************************************/
881 inline
882 void
885 {
886  if( IsEmpty() )
887  return;
888 
889  SetCurrent( Prev( m_Current ) );
890 }
891 
892 /*****************************************************************************/
893 inline
894 void
896 ::SetCurrent( SizeType index, bool force )
897 {
898  // qDebug() << this << "::SetCurrent(" << index << ")";
899 
900  if( index==m_Current && !force )
901  return;
902 
903  KeyType key( GetKey( index ) );
904 
905  // qDebug()
906  // << QString( "'%1'" ).arg( GetCurrentKey().c_str() )
907  // << "->"
908  // << QString( "'%1'" ).arg( key.c_str() );
909 
910  emit CurrentAboutToBeChanged( index );
911  emit AboutToChangeSelectedLayerModel( key );
912 
913  m_Current = index;
914 
915  emit CurrentChanged( index );
916  emit SelectedLayerModelChanged( key );
917 }
918 
919 /*****************************************************************************/
920 inline
921 void
923 ::SetReference( SizeType index, bool force )
924 {
925  // qDebug() << this << "::SetReference(" << index << ")";
926 
927  if( index==m_Reference && !force )
928  return;
929 
930  emit ReferenceAboutToBeChanged( index );
931 
932  m_Reference = index;
933 
934  emit ReferenceChanged( index );
935 }
936 
937 /*****************************************************************************/
938 #if OTB_DEBUG
939 
940 inline
941 void
942 trace( const StringVector & strv )
943 {
944  qDebug() << "{";
945 
946  for( StringVector::const_iterator it( strv.begin() );
947  it!=strv.end();
948  ++it )
949  qDebug() << QString( "%1" ).arg( it->c_str() );
950 
951  qDebug() << "}";
952 }
953 
954 #endif
955 
956 } // end namespace 'mvd'
957 
958 #endif // mvdStackedLayerModel_h
const AbstractLayerModel * Get(const KeyType &) const
itk::Size< Monteverdi_DIMENSION > SizeType
Definition: mvdTypes.h:137
SizeType GetCurrentIndex() const
const AbstractLayerModel * operator[](SizeType) const
SizeType GetReferenceIndex() const
static const SizeType NIL_INDEX
SizeType IndexOf(const AbstractLayerModel *) const
const KeyType & KeyOf(const AbstractLayerModel *) const
LayerModelMap::size_type SizeType
const KeyType & GetCurrentKey() const
std::vector< PixelInfo > Vector
Definition: mvdTypes.h:206
std::map< std::string, AbstractLayerModel * > LayerModelMap
void Delete(const KeyType &)
std::vector< std::string > StringVector
Definition: mvdTypes.h:99
const PixelInfo::Vector & PixelInfos() const
void EndEditPixelInfo(const QPoint &, const PointType &)
static const KeyType NIL_KEY
const AbstractLayerModel * At(SizeType) const
void SetReference(SizeType, bool=false)
PixelInfo::Vector m_PixelInfos
OTBGdalAdapters_EXPORT bool Contains(OGRGeometry const &lhs, OGRGeometry const &rhs)
Tests for containment.
bool Contains(const KeyType &key) const
ConstIterator Find(const AbstractLayerModel *) const
ConstIterator End() const
SizeType FindKey(const KeyType &) const
ConstIterator Begin() const
void SetCurrent(SizeType, bool=false)
const KeyType & GetKey(SizeType) const
LayerModelMap::key_type KeyType
VectorImageType::PointType PointType
Definition: mvdTypes.h:189
LayerModelMap::const_iterator ConstIterator
const KeyVector & GetKeys() const
std::vector< LayerModelMap::key_type > KeyVector