Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use registry to create material widget
- Loading branch information
1 parent
beece16
commit 7522886
Showing
14 changed files
with
244 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/*************************************************************************** | ||
qgsmaterialwidget.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. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsmaterialwidget.h" | ||
#include "qgs3d.h" | ||
#include "qgsmaterialregistry.h" | ||
#include "qgsabstractmaterialsettings.h" | ||
#include "qgsmaterialsettingswidget.h" | ||
#include "qgsreadwritecontext.h" | ||
#include "qgsphongmaterialsettings.h" | ||
|
||
QgsMaterialWidget::QgsMaterialWidget( QWidget *parent ) | ||
: QWidget( parent ) | ||
, mCurrentSettings( qgis::make_unique< QgsPhongMaterialSettings >() ) | ||
{ | ||
setupUi( this ); | ||
|
||
const QStringList materialTypes = Qgs3D::materialRegistry()->materialSettingsTypes(); | ||
for ( const QString &type : materialTypes ) | ||
{ | ||
mMaterialTypeComboBox->addItem( Qgs3D::materialRegistry()->materialSettingsMetadata( type )->visibleName(), type ); | ||
} | ||
|
||
connect( mMaterialTypeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsMaterialWidget::materialTypeChanged ); | ||
} | ||
|
||
void QgsMaterialWidget::setSettings( const QgsAbstractMaterialSettings *settings, QgsVectorLayer * ) | ||
{ | ||
mMaterialTypeComboBox->setCurrentIndex( mMaterialTypeComboBox->findData( settings->type() ) ); | ||
mCurrentSettings.reset( settings->clone() ); | ||
updateMaterialWidget(); | ||
} | ||
|
||
QgsAbstractMaterialSettings *QgsMaterialWidget::settings() | ||
{ | ||
return mCurrentSettings->clone(); | ||
} | ||
|
||
void QgsMaterialWidget::materialTypeChanged() | ||
{ | ||
std::unique_ptr< QgsAbstractMaterialSettings > currentSettings( settings() ); | ||
const QString existingType = currentSettings ? currentSettings->type() : QString(); | ||
const QString newType = mMaterialTypeComboBox->currentData().toString(); | ||
if ( existingType == newType ) | ||
return; | ||
|
||
if ( QgsMaterialSettingsAbstractMetadata *am = Qgs3D::materialRegistry()->materialSettingsMetadata( newType ) ) | ||
{ | ||
// change material to a new (with different type) | ||
// base new layer on existing materials's properties | ||
std::unique_ptr< QgsAbstractMaterialSettings > newMaterial( am->create() ); | ||
if ( newMaterial ) | ||
{ | ||
if ( currentSettings ) | ||
{ | ||
QDomDocument doc; | ||
QDomElement tempElem = doc.createElement( QStringLiteral( "temp" ) ); | ||
currentSettings->writeXml( tempElem, QgsReadWriteContext() ); | ||
newMaterial->readXml( tempElem, QgsReadWriteContext() ); | ||
} | ||
|
||
mCurrentSettings = std::move( newMaterial ); | ||
updateMaterialWidget(); | ||
emit changed(); | ||
} | ||
} | ||
} | ||
|
||
void QgsMaterialWidget::materialWidgetChanged() | ||
{ | ||
if ( QgsMaterialSettingsWidget *w = qobject_cast< QgsMaterialSettingsWidget * >( mStackedWidget->currentWidget() ) ) | ||
{ | ||
mCurrentSettings.reset( w->settings() ); | ||
} | ||
emit changed(); | ||
} | ||
|
||
void QgsMaterialWidget::updateMaterialWidget() | ||
{ | ||
if ( mStackedWidget->currentWidget() != mPageDummy ) | ||
{ | ||
// stop updating from the original widget | ||
if ( QgsMaterialSettingsWidget *w = qobject_cast< QgsMaterialSettingsWidget * >( mStackedWidget->currentWidget() ) ) | ||
disconnect( w, &QgsMaterialSettingsWidget::changed, this, &QgsMaterialWidget::materialWidgetChanged ); | ||
mStackedWidget->removeWidget( mStackedWidget->currentWidget() ); | ||
} | ||
|
||
const QString settingsType = mCurrentSettings->type(); | ||
if ( QgsMaterialSettingsAbstractMetadata *am = Qgs3D::materialRegistry()->materialSettingsMetadata( settingsType ) ) | ||
{ | ||
if ( QgsMaterialSettingsWidget *w = am->createWidget() ) | ||
{ | ||
w->setSettings( mCurrentSettings.get(), nullptr ); | ||
mStackedWidget->addWidget( w ); | ||
mStackedWidget->setCurrentWidget( w ); | ||
// start receiving updates from widget | ||
connect( w, &QgsMaterialSettingsWidget::changed, this, &QgsMaterialWidget::materialWidgetChanged ); | ||
return; | ||
} | ||
} | ||
// When anything is not right | ||
mStackedWidget->setCurrentWidget( mPageDummy ); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/*************************************************************************** | ||
qgsmaterialwidget.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 QGSMATERIALWIDGET_H | ||
#define QGSMATERIALWIDGET_H | ||
|
||
#include <QWidget> | ||
#include <ui_materialwidget.h> | ||
|
||
class QgsAbstractMaterialSettings; | ||
class QgsVectorLayer; | ||
|
||
//! Widget for configuration of material settings | ||
class QgsMaterialWidget : public QWidget, private Ui::MaterialWidgetBase | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit QgsMaterialWidget( QWidget *parent = nullptr ); | ||
|
||
void setSettings( const QgsAbstractMaterialSettings *settings, QgsVectorLayer *layer ); | ||
QgsAbstractMaterialSettings *settings(); | ||
|
||
signals: | ||
|
||
void changed(); | ||
|
||
private slots: | ||
void materialTypeChanged(); | ||
void materialWidgetChanged(); | ||
|
||
private: | ||
void updateMaterialWidget(); | ||
|
||
std::unique_ptr< QgsAbstractMaterialSettings > mCurrentSettings; | ||
|
||
|
||
}; | ||
|
||
#endif // QGSMATERIALWIDGET_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | ||
<class>MaterialWidgetBase</class> | ||
<widget class="QWidget" name="MaterialWidgetBase"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>382</width> | ||
<height>264</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle"> | ||
<string notr="true">Form</string> | ||
</property> | ||
<layout class="QVBoxLayout" name="verticalLayout"> | ||
<property name="leftMargin"> | ||
<number>0</number> | ||
</property> | ||
<property name="topMargin"> | ||
<number>0</number> | ||
</property> | ||
<property name="rightMargin"> | ||
<number>0</number> | ||
</property> | ||
<property name="bottomMargin"> | ||
<number>0</number> | ||
</property> | ||
<item> | ||
<widget class="QComboBox" name="mMaterialTypeComboBox"/> | ||
</item> | ||
<item> | ||
<widget class="QStackedWidget" name="mStackedWidget"> | ||
<widget class="QWidget" name="mPageDummy"> | ||
<layout class="QHBoxLayout" name="horizontalLayout"> | ||
<item> | ||
<widget class="QLabel" name="label_2"> | ||
<property name="text"> | ||
<string>This material doesn't have any editable properties</string> | ||
</property> | ||
<property name="alignment"> | ||
<set>Qt::AlignCenter</set> | ||
</property> | ||
</widget> | ||
</item> | ||
</layout> | ||
</widget> | ||
</widget> | ||
</item> | ||
</layout> | ||
</widget> | ||
<resources/> | ||
<connections/> | ||
</ui> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters