QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgspainteffectpropertieswidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayerpropertieswidget.cpp
3 ----------------------------
4 begin : June 2012
5 copyright : (C) 2012 by Arunmozhi
6 email : aruntheguy at gmail.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 "qgsapplication.h"
19#include "qgseffectstack.h"
20#include "qgslogger.h"
21#include "qgspainteffect.h"
24
25#include <QFile>
26#include <QKeyEvent>
27#include <QMessageBox>
28#include <QStandardItem>
29
30#include "moc_qgspainteffectpropertieswidget.cpp"
31
32static bool _initWidgetFunction( const QString &name, QgsPaintEffectWidgetFunc f )
33{
35
36 QgsPaintEffectAbstractMetadata *abstractMetadata = registry->effectMetadata( name );
37 if ( !abstractMetadata )
38 {
39 QgsDebugError( QStringLiteral( "Failed to find paint effect entry in registry: %1" ).arg( name ) );
40 return false;
41 }
42 QgsPaintEffectMetadata *metadata = dynamic_cast<QgsPaintEffectMetadata *>( abstractMetadata );
43 if ( !metadata )
44 {
45 QgsDebugError( QStringLiteral( "Failed to cast paint effect's metadata: " ).arg( name ) );
46 return false;
47 }
48 metadata->setWidgetFunction( f );
49 return true;
50}
51
52static void _initWidgetFunctions()
53{
54 static bool sInitialized = false;
55 if ( sInitialized )
56 return;
57
58 _initWidgetFunction( QStringLiteral( "blur" ), QgsBlurWidget::create );
59 _initWidgetFunction( QStringLiteral( "dropShadow" ), QgsShadowEffectWidget::create );
60 _initWidgetFunction( QStringLiteral( "innerShadow" ), QgsShadowEffectWidget::create );
61 _initWidgetFunction( QStringLiteral( "drawSource" ), QgsDrawSourceWidget::create );
62 _initWidgetFunction( QStringLiteral( "outerGlow" ), QgsGlowWidget::create );
63 _initWidgetFunction( QStringLiteral( "innerGlow" ), QgsGlowWidget::create );
64 _initWidgetFunction( QStringLiteral( "transform" ), QgsTransformWidget::create );
65 _initWidgetFunction( QStringLiteral( "color" ), QgsColorEffectWidget::create );
66
67 sInitialized = true;
68}
69
70
72 : QWidget( parent )
73 , mEffect( effect )
74{
75 setupUi( this );
76 _initWidgetFunctions();
77
78 populateEffectTypes();
79 // update effect type combo box
80 if ( effect )
81 {
82 const int idx = mEffectTypeCombo->findData( effect->type() );
83 mEffectTypeCombo->setCurrentIndex( idx );
84 }
85 // set the corresponding widget
86 updateEffectWidget( effect );
87 connect( mEffectTypeCombo, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsPaintEffectPropertiesWidget::effectTypeChanged );
88}
89
90
91void QgsPaintEffectPropertiesWidget::populateEffectTypes()
92{
94 const QStringList types = registry->effects();
95
96 const auto constTypes = types;
97 for ( const QString &type : constTypes )
98 {
99 //don't show stack effect
100 if ( type == QLatin1String( "effectStack" ) )
101 continue;
102
103 mEffectTypeCombo->addItem( registry->effectMetadata( type )->visibleName(), type );
104 }
105}
106
107void QgsPaintEffectPropertiesWidget::updateEffectWidget( QgsPaintEffect *effect )
108{
109 if ( !effect )
110 {
111 stackedWidget->setCurrentWidget( pageDummy );
112 return;
113 }
114
115 if ( stackedWidget->currentWidget() != pageDummy )
116 {
117 // stop updating from the original widget
118 if ( QgsPaintEffectWidget *pew = qobject_cast<QgsPaintEffectWidget *>( stackedWidget->currentWidget() ) )
120 stackedWidget->removeWidget( stackedWidget->currentWidget() );
121 }
122
123 QgsPaintEffectRegistry *registry = QgsApplication::paintEffectRegistry();
124 QgsPaintEffectAbstractMetadata *am = registry->effectMetadata( effect->type() );
125 if ( am )
126 {
127 QgsPaintEffectWidget *w = am->createWidget();
128 if ( w )
129 {
130 w->setPaintEffect( effect );
131 stackedWidget->addWidget( w );
132 stackedWidget->setCurrentWidget( w );
133 // start receiving updates from widget
135 return;
136 }
137 }
138 // When anything is not right
139 stackedWidget->setCurrentWidget( pageDummy );
140}
141
143{
144 QgsPaintEffect *effect = mEffect;
145 if ( !effect )
146 return;
147
148 const QString newEffectType = mEffectTypeCombo->currentData().toString();
149 if ( effect->type() == newEffectType )
150 return;
151
152 // get creation function for new effect from registry
154 QgsPaintEffectAbstractMetadata *am = registry->effectMetadata( newEffectType );
155 if ( !am ) // check whether the metadata is assigned
156 return;
157
158 // change effect to a new (with different type)
159 // base new effect on existing effect's properties
160 QgsPaintEffect *newEffect = am->createPaintEffect( effect->properties() );
161 if ( !newEffect )
162 return;
163
164 updateEffectWidget( newEffect );
165 emit changeEffect( newEffect );
166
167 mEffect = newEffect;
168}
169
static QgsPaintEffectRegistry * paintEffectRegistry()
Returns the application's paint effect registry, used for managing paint effects.
static QgsPaintEffectWidget * create()
static QgsPaintEffectWidget * create()
static QgsPaintEffectWidget * create()
static QgsPaintEffectWidget * create()
Stores metadata about a paint effect class.
virtual QgsPaintEffectWidget * createWidget()
Create configuration widget for paint effect of this class.
QString visibleName() const
Returns the user visible string representing the paint effect class.
virtual QgsPaintEffect * createPaintEffect(const QVariantMap &map)=0
Create a paint effect of this class given an encoded map of properties.
Convenience metadata class that uses static functions to create an effect and its widget.
void setWidgetFunction(QgsPaintEffectWidgetFunc f)
Sets the paint effect properties widget creation function for the paint effect class.
void emitSignalChanged()
Emits the changed signal.
void effectTypeChanged()
Update widget when effect type changes.
void changeEffect(QgsPaintEffect *effect)
Emitted when paint effect type changes.
void changed()
Emitted when paint effect properties changes.
QgsPaintEffectPropertiesWidget(QgsPaintEffect *effect, QWidget *parent=nullptr)
QgsPaintEffectPropertiesWidget constructor.
Registry of available paint effects.
QgsPaintEffectAbstractMetadata * effectMetadata(const QString &name) const
Returns the metadata for a specific effect.
QStringList effects() const
Returns a list of known paint effects.
virtual void setPaintEffect(QgsPaintEffect *effect)=0
Sets the paint effect to modify with the widget.
void changed()
Emitted when properties of the effect are changed through the widget.
Base class for visual effects which can be applied to QPicture drawings.
virtual QVariantMap properties() const =0
Returns the properties describing the paint effect encoded in a string format.
virtual QString type() const =0
Returns the effect type.
static QgsPaintEffectWidget * create()
static QgsPaintEffectWidget * create()
#define QgsDebugError(str)
Definition qgslogger.h:57
QgsPaintEffectWidget *(* QgsPaintEffectWidgetFunc)()