Skip to content
Permalink
Browse files

Start generalizing material widget handling

  • Loading branch information
nyalldawson committed Jul 28, 2020
1 parent 0058a83 commit beece1635d21987d40ab2151d2e2a2dc8a1f441e
@@ -9,6 +9,7 @@



class QgsMaterialSettingsWidget /External/;

class QgsMaterialSettingsAbstractMetadata
{
@@ -24,7 +24,7 @@

class QgsAbstractMaterialSettings;
class QgsReadWriteContext;
class QgsMaterialSettingsWidget;
class QgsMaterialSettingsWidget SIP_EXTERNAL;

/**
* \ingroup core
@@ -0,0 +1,33 @@
/***************************************************************************
qgs3dapputils.cpp
--------------------------------------
Date : July 2020
Copyright : (C) 2020 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgs3dapputils.h"
#include "qgsapplication.h"
#include "qgs3dsymbolregistry.h"
#include "qgs3d.h"
#include "qgsmaterialregistry.h"
#include "qgspoint3dsymbolwidget.h"
#include "qgspolygon3dsymbolwidget.h"
#include "qgsline3dsymbolwidget.h"
#include "qgsphongmaterialwidget.h"

void Qgs3DAppUtils::initialize()
{
dynamic_cast< Qgs3DSymbolMetadata * >( QgsApplication::symbol3DRegistry()->symbolMetadata( QStringLiteral( "point" ) ) )->setWidgetFunction( QgsPoint3DSymbolWidget::create );
dynamic_cast< Qgs3DSymbolMetadata * >( QgsApplication::symbol3DRegistry()->symbolMetadata( QStringLiteral( "line" ) ) )->setWidgetFunction( QgsLine3DSymbolWidget::create );
dynamic_cast< Qgs3DSymbolMetadata * >( QgsApplication::symbol3DRegistry()->symbolMetadata( QStringLiteral( "polygon" ) ) )->setWidgetFunction( QgsPolygon3DSymbolWidget::create );

dynamic_cast< QgsMaterialSettingsMetadata * >( Qgs3D::materialRegistry()->materialSettingsMetadata( QStringLiteral( "phong" ) ) )->setWidgetFunction( QgsPhongMaterialWidget::create );
}
@@ -0,0 +1,30 @@
/***************************************************************************
qgs3dapputils.h
--------------------------------------
Date : July 2020
Copyright : (C) 2020 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGS3DAPPUTILS_H
#define QGS3DAPPUTILS_H

class Qgs3DAppUtils
{
public:

/**
* Initializes 3D components belonging to the app library.
*/
static void initialize();

};

#endif // QGS3DAPPUTILS_H
@@ -106,7 +106,8 @@ Qgs3DMapConfigWidget::Qgs3DMapConfigWidget( Qgs3DMapSettings *map, QgsMapCanvas

groupTerrainShading->setChecked( mMap->isTerrainShadingEnabled() );
widgetTerrainMaterial->setDiffuseVisible( false );
widgetTerrainMaterial->setMaterial( mMap->terrainShadingMaterial() );
QgsPhongMaterialSettings terrainShadingMaterial = mMap->terrainShadingMaterial();
widgetTerrainMaterial->setSettings( &terrainShadingMaterial, nullptr );

widgetLights->setPointLights( mMap->pointLights() );
widgetLights->setDirectionalLights( mMap->directionalLights() );
@@ -225,7 +226,10 @@ void Qgs3DMapConfigWidget::apply()
mMap->setShowCameraViewCenter( chkShowCameraViewCenter->isChecked() );
mMap->setShowLightSourceOrigins( chkShowLightSourceOrigins->isChecked() );
mMap->setTerrainShadingEnabled( groupTerrainShading->isChecked() );
mMap->setTerrainShadingMaterial( widgetTerrainMaterial->material() );

std::unique_ptr< QgsAbstractMaterialSettings > terrainMaterial( widgetTerrainMaterial->settings() );
if ( QgsPhongMaterialSettings *phongMaterial = dynamic_cast< QgsPhongMaterialSettings * >( terrainMaterial.get() ) )
mMap->setTerrainShadingMaterial( *phongMaterial );

mMap->setPointLights( widgetLights->pointLights() );
mMap->setDirectionalLights( widgetLights->directionalLights() );
@@ -45,7 +45,7 @@ Qgs3DSymbolWidget *QgsLine3DSymbolWidget::create( QgsVectorLayer * )
return new QgsLine3DSymbolWidget();
}

void QgsLine3DSymbolWidget::setSymbol( const QgsAbstract3DSymbol *symbol, QgsVectorLayer * )
void QgsLine3DSymbolWidget::setSymbol( const QgsAbstract3DSymbol *symbol, QgsVectorLayer *layer )
{
const QgsLine3DSymbol *lineSymbol = dynamic_cast< const QgsLine3DSymbol *>( symbol );
if ( !lineSymbol )
@@ -58,7 +58,7 @@ void QgsLine3DSymbolWidget::setSymbol( const QgsAbstract3DSymbol *symbol, QgsVec
cboAltBinding->setCurrentIndex( static_cast<int>( lineSymbol->altitudeBinding() ) );
chkSimpleLines->setChecked( lineSymbol->renderAsSimpleLines() );
// TODO -- make material widget generic!
widgetMaterial->setMaterial( *dynamic_cast< QgsPhongMaterialSettings *>( lineSymbol->material() ) );
widgetMaterial->setSettings( lineSymbol->material(), layer );
updateGuiState();
}

@@ -71,7 +71,7 @@ QgsAbstract3DSymbol *QgsLine3DSymbolWidget::symbol()
sym->setAltitudeClamping( static_cast<Qgs3DTypes::AltitudeClamping>( cboAltClamping->currentIndex() ) );
sym->setAltitudeBinding( static_cast<Qgs3DTypes::AltitudeBinding>( cboAltBinding->currentIndex() ) );
sym->setRenderAsSimpleLines( chkSimpleLines->isChecked() );
sym->setMaterial( widgetMaterial->material().clone() );
sym->setMaterial( widgetMaterial->settings() );
return sym.release();
}

@@ -16,14 +16,15 @@
#include "qgsphongmaterialwidget.h"

#include "qgsphongmaterialsettings.h"

#include "qgis.h"

QgsPhongMaterialWidget::QgsPhongMaterialWidget( QWidget *parent )
: QWidget( parent )
: QgsMaterialSettingsWidget( parent )
{
setupUi( this );

setMaterial( QgsPhongMaterialSettings() );
QgsPhongMaterialSettings defaultMaterial;
setSettings( &defaultMaterial, nullptr );
textureScaleSpinBox->setClearValue( 0 );
textureRotationSpinBox->setClearValue( 0 );

@@ -38,6 +39,11 @@ QgsPhongMaterialWidget::QgsPhongMaterialWidget( QWidget *parent )
this->activateTexturingUI( false );
}

QgsMaterialSettingsWidget *QgsPhongMaterialWidget::create()
{
return new QgsPhongMaterialWidget();
}

void QgsPhongMaterialWidget::setDiffuseVisible( bool visible )
{
lblDiffuse->setVisible( visible );
@@ -49,30 +55,33 @@ bool QgsPhongMaterialWidget::isDiffuseVisible() const
return btnDiffuse->isVisible();
}

void QgsPhongMaterialWidget::setMaterial( const QgsPhongMaterialSettings &material )
void QgsPhongMaterialWidget::setSettings( const QgsAbstractMaterialSettings *settings, QgsVectorLayer * )
{
btnDiffuse->setColor( material.diffuse() );
btnAmbient->setColor( material.ambient() );
btnSpecular->setColor( material.specular() );
spinShininess->setValue( material.shininess() );
useDiffuseCheckBox->setCheckState( material.diffuseTextureEnabled() ? Qt::CheckState::Checked : Qt::CheckState::Unchecked );
textureFile->setSource( material.texturePath() );
textureScaleSpinBox->setValue( material.textureScale() );
textureRotationSpinBox->setValue( material.textureRotation() );
const QgsPhongMaterialSettings *phongMaterial = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
if ( !phongMaterial )
return;
btnDiffuse->setColor( phongMaterial->diffuse() );
btnAmbient->setColor( phongMaterial->ambient() );
btnSpecular->setColor( phongMaterial->specular() );
spinShininess->setValue( phongMaterial->shininess() );
useDiffuseCheckBox->setCheckState( phongMaterial->diffuseTextureEnabled() ? Qt::CheckState::Checked : Qt::CheckState::Unchecked );
textureFile->setSource( phongMaterial->texturePath() );
textureScaleSpinBox->setValue( phongMaterial->textureScale() );
textureRotationSpinBox->setValue( phongMaterial->textureRotation() );
}

QgsPhongMaterialSettings QgsPhongMaterialWidget::material() const
QgsAbstractMaterialSettings *QgsPhongMaterialWidget::settings()
{
QgsPhongMaterialSettings m;
m.setDiffuse( btnDiffuse->color() );
m.setAmbient( btnAmbient->color() );
m.setSpecular( btnSpecular->color() );
m.setShininess( spinShininess->value() );
m.setDiffuseTextureEnabled( useDiffuseCheckBox->checkState() == Qt::CheckState::Checked );
m.setTexturePath( textureFile->source() );
m.setTextureScale( textureScaleSpinBox->value() );
m.setTextureRotation( textureRotationSpinBox->value() );
return m;
std::unique_ptr< QgsPhongMaterialSettings > m = qgis::make_unique< QgsPhongMaterialSettings >();
m->setDiffuse( btnDiffuse->color() );
m->setAmbient( btnAmbient->color() );
m->setSpecular( btnSpecular->color() );
m->setShininess( spinShininess->value() );
m->setDiffuseTextureEnabled( useDiffuseCheckBox->checkState() == Qt::CheckState::Checked );
m->setTexturePath( textureFile->source() );
m->setTextureScale( textureScaleSpinBox->value() );
m->setTextureRotation( textureRotationSpinBox->value() );
return m.release();
}

void QgsPhongMaterialWidget::activateTexturingUI( bool activated )
@@ -16,25 +16,27 @@
#ifndef QGSPHONGMATERIALWIDGET_H
#define QGSPHONGMATERIALWIDGET_H

#include <QWidget>
#include "qgsmaterialsettingswidget.h"

#include <ui_phongmaterialwidget.h>

class QgsPhongMaterialSettings;


//! Widget for configuration of Phong material settings
class QgsPhongMaterialWidget : public QWidget, private Ui::PhongMaterialWidget
class QgsPhongMaterialWidget : public QgsMaterialSettingsWidget, private Ui::PhongMaterialWidget
{
Q_OBJECT
public:
explicit QgsPhongMaterialWidget( QWidget *parent = nullptr );

static QgsMaterialSettingsWidget *create();

void setDiffuseVisible( bool visible );
bool isDiffuseVisible() const;

void setMaterial( const QgsPhongMaterialSettings &material );
QgsPhongMaterialSettings material() const;
void setSettings( const QgsAbstractMaterialSettings *settings, QgsVectorLayer *layer ) override;
QgsAbstractMaterialSettings *settings() override;

//! activates the texturing UI (to make sure texturing UI isn't visible when the user doesn't need it like in the 3D configuration window)
void activateTexturingUI( bool activated );
@@ -93,7 +93,7 @@ void QgsPoint3DSymbolWidget::onOverwriteMaterialChecked( int state )
emit changed();
}

void QgsPoint3DSymbolWidget::setSymbol( const QgsAbstract3DSymbol *symbol, QgsVectorLayer * )
void QgsPoint3DSymbolWidget::setSymbol( const QgsAbstract3DSymbol *symbol, QgsVectorLayer *layer )
{
const QgsPoint3DSymbol *pointSymbol = dynamic_cast< const QgsPoint3DSymbol *>( symbol );
if ( !pointSymbol )
@@ -146,7 +146,7 @@ void QgsPoint3DSymbolWidget::setSymbol( const QgsAbstract3DSymbol *symbol, QgsVe
}

//TODO - handle other subclasses
widgetMaterial->setMaterial( *dynamic_cast< QgsPhongMaterialSettings * >( pointSymbol->material() ) );
widgetMaterial->setSettings( pointSymbol->material(), layer );

// decompose the transform matrix
// assuming the last row has values [0 0 0 1]
@@ -227,7 +227,7 @@ QgsAbstract3DSymbol *QgsPoint3DSymbolWidget::symbol()
sym->setAltitudeClamping( static_cast<Qgs3DTypes::AltitudeClamping>( cboAltClamping->currentIndex() ) );
sym->setShape( static_cast<QgsPoint3DSymbol::Shape>( cboShape->itemData( cboShape->currentIndex() ).toInt() ) );
sym->setShapeProperties( vm );
sym->setMaterial( widgetMaterial->material().clone() );
sym->setMaterial( widgetMaterial->settings() );
sym->setTransform( tr );
return sym.release();
}
@@ -68,7 +68,7 @@ void QgsPolygon3DSymbolWidget::setSymbol( const QgsAbstract3DSymbol *symbol, Qgs
chkInvertNormals->setChecked( polygonSymbol->invertNormals() );

// todo -- handle other subclasses
widgetMaterial->setMaterial( *dynamic_cast< QgsPhongMaterialSettings * >( polygonSymbol->material() ) );
widgetMaterial->setSettings( polygonSymbol->material(), layer );

btnHeightDD->init( QgsAbstract3DSymbol::PropertyHeight, polygonSymbol->dataDefinedProperties(), QgsAbstract3DSymbol::propertyDefinitions(), layer, true );
btnExtrusionDD->init( QgsAbstract3DSymbol::PropertyExtrusionHeight, polygonSymbol->dataDefinedProperties(), QgsAbstract3DSymbol::propertyDefinitions(), layer, true );
@@ -89,7 +89,7 @@ QgsAbstract3DSymbol *QgsPolygon3DSymbolWidget::symbol()
sym->setRenderedFacade( cboRenderedFacade->currentIndex() );
sym->setAddBackFaces( chkAddBackFaces->isChecked() );
sym->setInvertNormals( chkInvertNormals->isChecked() );
sym->setMaterial( widgetMaterial->material().clone() );
sym->setMaterial( widgetMaterial->settings() );

QgsPropertyCollection ddp;
ddp.setProperty( QgsAbstract3DSymbol::PropertyHeight, btnHeightDD->toProperty() );
@@ -261,6 +261,7 @@ IF (WITH_3D)
${QGIS_APP_SRCS}
3d/qgs3danimationwidget.cpp
3d/qgs3danimationexportdialog.cpp
3d/qgs3dapputils.cpp
3d/qgs3dmapcanvas.cpp
3d/qgs3dmapcanvasdockwidget.cpp
3d/qgs3dmapconfigwidget.cpp
@@ -112,9 +112,7 @@
#include "layout/qgslayout3dmapwidget.h"
#include "layout/qgslayoutviewrubberband.h"
#include "qgsvectorlayer3drendererwidget.h"
#include "qgspoint3dsymbolwidget.h"
#include "qgsline3dsymbolwidget.h"
#include "qgspolygon3dsymbolwidget.h"
#include "qgs3dapputils.h"
#endif

#ifdef HAVE_GEOREFERENCER
@@ -12658,11 +12656,7 @@ void QgisApp::init3D()
#ifdef HAVE_3D
// initialize 3D registries
Qgs3D::initialize();

dynamic_cast< Qgs3DSymbolMetadata * >( QgsApplication::symbol3DRegistry()->symbolMetadata( QStringLiteral( "point" ) ) )->setWidgetFunction( QgsPoint3DSymbolWidget::create );
dynamic_cast< Qgs3DSymbolMetadata * >( QgsApplication::symbol3DRegistry()->symbolMetadata( QStringLiteral( "line" ) ) )->setWidgetFunction( QgsLine3DSymbolWidget::create );
dynamic_cast< Qgs3DSymbolMetadata * >( QgsApplication::symbol3DRegistry()->symbolMetadata( QStringLiteral( "polygon" ) ) )->setWidgetFunction( QgsPolygon3DSymbolWidget::create );

Qgs3DAppUtils::initialize();
#else
mActionNew3DMapCanvas->setVisible( false );
#endif
@@ -479,6 +479,7 @@ SET(QGIS_GUI_SRCS
qgsmaptoolpan.cpp
qgsmaptoolzoom.cpp
qgsmaplayerconfigwidget.cpp
qgsmaterialsettingswidget.cpp
qgsmenuheader.cpp
qgsmessagebar.cpp
qgsmessagebaritem.cpp
@@ -715,6 +716,7 @@ SET(QGIS_GUI_HDRS
qgsmaptoolpan.h
qgsmaptoolzoom.h
qgsmaskingwidget.h
qgsmaterialsettingswidget.h
qgsmenuheader.h
qgsmessagebar.h
qgsmessagebaritem.h
@@ -0,0 +1,20 @@
/***************************************************************************
qgsmaterialsettingswidget.cpp
--------------------------------------
Date : July 2020
Copyright : (C) 2020 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsmaterialsettingswidget.h"

QgsMaterialSettingsWidget::QgsMaterialSettingsWidget( QWidget *parent )
: QWidget( parent )
{}

0 comments on commit beece16

Please sign in to comment.
You can’t perform that action at this time.