Skip to content

Commit f7c942c

Browse files
committedSep 15, 2017
Respect background color from project's main canvas
1 parent 63adb73 commit f7c942c

13 files changed

+63
-6
lines changed
 

‎python/gui/qgsmapcanvas.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,11 @@ Emitted when the extents of the map change
718718
void magnificationChanged( double );
719719
%Docstring
720720
.. versionadded:: 2.16
721+
%End
722+
723+
void canvasColorChanged();
724+
%Docstring
725+
.. versionadded:: 3.0
721726
%End
722727

723728
void renderComplete( QPainter * );

‎src/3d/map3d.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ Map3D::Map3D()
1616
: originX( 0 )
1717
, originY( 0 )
1818
, originZ( 0 )
19-
, backgroundColor( Qt::black )
2019
, skybox( false )
20+
, mBackgroundColor( Qt::black )
2121
, mTerrainVerticalScale( 1 )
2222
, mMapTileResolution( 512 )
2323
, mMaxTerrainScreenError( 3.f )
@@ -33,10 +33,10 @@ Map3D::Map3D( const Map3D &other )
3333
, originY( other.originY )
3434
, originZ( other.originZ )
3535
, crs( other.crs )
36-
, backgroundColor( other.backgroundColor )
3736
, skybox( other.skybox )
3837
, skyboxFileBase( other.skyboxFileBase )
3938
, skyboxFileExtension( other.skyboxFileExtension )
39+
, mBackgroundColor( other.mBackgroundColor )
4040
, mTerrainVerticalScale( other.mTerrainVerticalScale )
4141
, mTerrainGenerator( other.mTerrainGenerator ? other.mTerrainGenerator->clone() : nullptr )
4242
, mMapTileResolution( other.mMapTileResolution )
@@ -209,6 +209,20 @@ void Map3D::resolveReferences( const QgsProject &project )
209209
}
210210
}
211211

212+
void Map3D::setBackgroundColor( const QColor &color )
213+
{
214+
if ( color == mBackgroundColor )
215+
return;
216+
217+
mBackgroundColor = color;
218+
emit backgroundColorChanged();
219+
}
220+
221+
QColor Map3D::backgroundColor() const
222+
{
223+
return mBackgroundColor;
224+
}
225+
212226
void Map3D::setTerrainVerticalScale( double zScale )
213227
{
214228
if ( zScale == mTerrainVerticalScale )

‎src/3d/map3d.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ class _3D_EXPORT Map3D : public QObject
3939

4040
double originX, originY, originZ; //!< Coordinates in map CRS at which our 3D world has origin (0,0,0)
4141
QgsCoordinateReferenceSystem crs; //!< Destination coordinate system of the world (TODO: not needed? can be
42-
QColor backgroundColor; //!< Background color of the scene
42+
43+
void setBackgroundColor( const QColor &color );
44+
QColor backgroundColor() const;
4345

4446
//
4547
// terrain related config
@@ -80,6 +82,7 @@ class _3D_EXPORT Map3D : public QObject
8082
bool showTerrainTilesInfo() const { return mShowTerrainTileInfo; }
8183

8284
signals:
85+
void backgroundColorChanged();
8386
void layersChanged();
8487
void terrainGeneratorChanged();
8588
void terrainVerticalScaleChanged();
@@ -90,6 +93,7 @@ class _3D_EXPORT Map3D : public QObject
9093
void showTerrainTilesInfoChanged();
9194

9295
private:
96+
QColor mBackgroundColor; //!< Background color of the scene
9397
double mTerrainVerticalScale; //!< Multiplier of terrain heights to make the terrain shape more pronounced
9498
std::unique_ptr<TerrainGenerator> mTerrainGenerator; //!< Implementation of the terrain generation
9599
int mMapTileResolution; //!< Size of map textures of tiles in pixels (width/height)

‎src/3d/scene.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ Scene::Scene( const Map3D &map, Qt3DExtras::QForwardRenderer *defaultFrameGraph,
2828
: Qt3DCore::QEntity( parent )
2929
, mMap( map )
3030
, mTerrain( nullptr )
31+
, mForwardRenderer( defaultFrameGraph )
3132
{
32-
defaultFrameGraph->setClearColor( map.backgroundColor );
33+
34+
connect( &map, &Map3D::backgroundColorChanged, this, &Scene::onBackgroundColorChanged );
35+
onBackgroundColorChanged();
3336

3437
// TODO: strange - setting OnDemand render policy still keeps QGIS busy (Qt 5.9.0)
3538
// actually it is more busy than with the default "Always" policy although there are no changes in the scene.
@@ -226,6 +229,11 @@ void Scene::createTerrainDeferred()
226229
mTerrainUpdateScheduled = false;
227230
}
228231

232+
void Scene::onBackgroundColorChanged()
233+
{
234+
mForwardRenderer->setClearColor( mMap.backgroundColor() );
235+
}
236+
229237
void Scene::onLayerRenderer3DChanged()
230238
{
231239
QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( sender() );

‎src/3d/scene.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class _3D_EXPORT Scene : public Qt3DCore::QEntity
4646
void onLayerRenderer3DChanged();
4747
void onLayersChanged();
4848
void createTerrainDeferred();
49+
void onBackgroundColorChanged();
4950

5051
private:
5152
void addLayerEntity( QgsMapLayer *layer );
@@ -57,6 +58,8 @@ class _3D_EXPORT Scene : public Qt3DCore::QEntity
5758
Qt3DLogic::QFrameAction *mFrameAction;
5859
CameraController *mCameraController;
5960
Terrain *mTerrain;
61+
//! Forward renderer provided by 3D window
62+
Qt3DExtras::QForwardRenderer *mForwardRenderer;
6063
QList<ChunkedEntity *> chunkEntities;
6164
//! Keeps track of entities that belong to a particular layer
6265
QMap<QgsMapLayer *, Qt3DCore::QEntity *> mLayerEntities;

‎src/3d/terrain/maptexturegenerator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,6 @@ QgsMapSettings MapTextureGenerator::baseMapSettings()
113113
mapSettings.setLayers( map.layers() );
114114
mapSettings.setOutputSize( QSize( map.mapTileResolution(), map.mapTileResolution() ) );
115115
mapSettings.setDestinationCrs( map.crs );
116-
mapSettings.setBackgroundColor( Qt::gray );
116+
mapSettings.setBackgroundColor( map.backgroundColor() );
117117
return mapSettings;
118118
}

‎src/3d/terrain/terrain.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Terrain::Terrain( int maxLevel, const Map3D &map, Qt3DCore::QNode *parent )
4646
connect( &map, &Map3D::showTerrainBoundingBoxesChanged, this, &Terrain::onShowBoundingBoxesChanged );
4747
connect( &map, &Map3D::showTerrainTilesInfoChanged, this, &Terrain::invalidateMapImages );
4848
connect( &map, &Map3D::layersChanged, this, &Terrain::onLayersChanged );
49+
connect( &map, &Map3D::backgroundColorChanged, this, &Terrain::invalidateMapImages );
4950

5051
connectToLayersRepaintRequest();
5152

‎src/app/3d/qgs3dmapcanvas.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Qgs3DMapCanvas::Qgs3DMapCanvas( QWidget *parent )
2121
QHBoxLayout *hLayout = new QHBoxLayout( this );
2222
hLayout->setMargin( 0 );
2323
hLayout->addWidget( mContainer, 1 );
24+
25+
mWindow3D->setCursor( Qt::OpenHandCursor );
2426
}
2527

2628
Qgs3DMapCanvas::~Qgs3DMapCanvas()

‎src/app/3d/qgs3dmapcanvasdockwidget.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ void Qgs3DMapCanvasDockWidget::setMainCanvas( QgsMapCanvas *canvas )
4343
mMainCanvas = canvas;
4444

4545
connect( mMainCanvas, &QgsMapCanvas::layersChanged, this, &Qgs3DMapCanvasDockWidget::onMainCanvasLayersChanged );
46+
connect( mMainCanvas, &QgsMapCanvas::canvasColorChanged, this, &Qgs3DMapCanvasDockWidget::onMainCanvasColorChanged );
4647
}
4748

4849
void Qgs3DMapCanvasDockWidget::resetView()
@@ -72,3 +73,8 @@ void Qgs3DMapCanvasDockWidget::onMainCanvasLayersChanged()
7273
{
7374
mCanvas->map()->setLayers( mMainCanvas->layers() );
7475
}
76+
77+
void Qgs3DMapCanvasDockWidget::onMainCanvasColorChanged()
78+
{
79+
mCanvas->map()->setBackgroundColor( mMainCanvas->canvasColor() );
80+
}

‎src/app/3d/qgs3dmapcanvasdockwidget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Qgs3DMapCanvasDockWidget : public QgsDockWidget
2525
void configure();
2626

2727
void onMainCanvasLayersChanged();
28+
void onMainCanvasColorChanged();
2829

2930
private:
3031
Qgs3DMapCanvas *mCanvas;

‎src/app/qgisapp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9910,7 +9910,7 @@ void QgisApp::new3DMapCanvas()
99109910
map->crs = prj->crs();
99119911
map->originX = fullExtent.center().x();
99129912
map->originY = fullExtent.center().y();
9913-
map->backgroundColor = mMapCanvas->canvasColor();
9913+
map->setBackgroundColor( mMapCanvas->canvasColor() );
99149914
map->setLayers( mMapCanvas->layers() );
99159915

99169916
FlatTerrainGenerator *flatTerrain = new FlatTerrainGenerator;

‎src/gui/qgsmapcanvas.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ QgsMapCanvas::QgsMapCanvas( QWidget *parent )
187187

188188
setInteractive( false );
189189

190+
// make sure we have the same default in QgsMapSettings and the scene's background brush
191+
// (by default map settings has white bg color, scene background brush is black)
192+
setCanvasColor( mSettings.backgroundColor() );
193+
190194
refresh();
191195

192196
} // QgsMapCanvas ctor
@@ -1560,6 +1564,9 @@ void QgsMapCanvas::unsetMapTool( QgsMapTool *tool )
15601564

15611565
void QgsMapCanvas::setCanvasColor( const QColor &color )
15621566
{
1567+
if ( canvasColor() == color )
1568+
return;
1569+
15631570
// background of map's pixmap
15641571
mSettings.setBackgroundColor( color );
15651572

@@ -1574,6 +1581,8 @@ void QgsMapCanvas::setCanvasColor( const QColor &color )
15741581

15751582
// background of QGraphicsScene
15761583
mScene->setBackgroundBrush( bgBrush );
1584+
1585+
emit canvasColorChanged();
15771586
}
15781587

15791588
QColor QgsMapCanvas::canvasColor() const

‎src/gui/qgsmapcanvas.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,10 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
636636
//! \since QGIS 2.16
637637
void magnificationChanged( double );
638638

639+
//! Emitted when canvas background color changes
640+
//! \since QGIS 3.0
641+
void canvasColorChanged();
642+
639643
/** Emitted when the canvas has rendered.
640644
* Passes a pointer to the painter on which the map was drawn. This is
641645
* useful for plugins that wish to draw on the map after it has been

0 commit comments

Comments
 (0)
Please sign in to comment.