QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsterraintextureimage_p.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsterraintextureimage_p.cpp
3  --------------------------------------
4  Date : July 2017
5  Copyright : (C) 2017 by Martin Dobias
6  Email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 
18 #include <Qt3DRender/QTextureImageDataGenerator>
19 
21 
23 
24 class TerrainTextureImageDataGenerator : public Qt3DRender::QTextureImageDataGenerator
25 {
26  public:
27 
28  static QImage placeholderImage()
29  {
30  // simple placeholder image
31  QImage i( 2, 2, QImage::Format_RGB32 );
32  i.setPixelColor( 0, 0, Qt::darkGray );
33  i.setPixelColor( 1, 0, Qt::lightGray );
34  i.setPixelColor( 0, 1, Qt::lightGray );
35  i.setPixelColor( 1, 1, Qt::darkGray );
36  return i;
37  }
38 
39  TerrainTextureImageDataGenerator( const QgsRectangle &extent, const QString &debugText, const QImage &img, int version )
40  : mExtent( extent ), mDebugText( debugText ), mImage( img ), mVersion( version ) {}
41 
42  Qt3DRender::QTextureImageDataPtr operator()() override
43  {
44  Qt3DRender::QTextureImageDataPtr dataPtr = Qt3DRender::QTextureImageDataPtr::create();
45  dataPtr->setImage( mImage.isNull() ? placeholderImage() : mImage ); // will copy image data to the internal byte array
46  return dataPtr;
47  }
48 
49  bool operator ==( const QTextureImageDataGenerator &other ) const override
50  {
51  const TerrainTextureImageDataGenerator *otherFunctor = functor_cast<TerrainTextureImageDataGenerator>( &other );
52  return otherFunctor != nullptr && otherFunctor->mVersion == mVersion &&
53  mExtent == otherFunctor->mExtent;
54  }
55 
56  QT3D_FUNCTOR( TerrainTextureImageDataGenerator )
57 
58  private:
59  QgsRectangle mExtent;
60  QString mDebugText;
61  QImage mImage;
62  int mVersion;
63 };
64 
65 
66 
68 
69 
70 QgsTerrainTextureImage::QgsTerrainTextureImage( const QImage &image, const QgsRectangle &extent, const QString &debugText, Qt3DCore::QNode *parent )
71  : Qt3DRender::QAbstractTextureImage( parent )
72  , mExtent( extent )
73  , mDebugText( debugText )
74  , mImage( image )
75 {
76 }
77 
78 Qt3DRender::QTextureImageDataGeneratorPtr QgsTerrainTextureImage::dataGenerator() const
79 {
80  return Qt3DRender::QTextureImageDataGeneratorPtr( new TerrainTextureImageDataGenerator( mExtent, mDebugText, mImage, mVersion ) );
81 }
82 
83 void QgsTerrainTextureImage::invalidate()
84 {
85  mImage = QImage();
86  mVersion++;
87  notifyDataGeneratorChanged();
88 }
89 
90 void QgsTerrainTextureImage::setImage( const QImage &img )
91 {
92  this->mImage = img;
93  mVersion++;
94  notifyDataGeneratorChanged();
95 }
96 
operator==
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:400
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
qgsterraintextureimage_p.h
Qt3DRender
Definition: qgs3dmapscene.h:27
qgsterraintexturegenerator_p.h