QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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#include "moc_qgspainteffectpropertieswidget.cpp"
18
19#include <QFile>
20#include <QStandardItem>
21#include <QKeyEvent>
22#include <QMessageBox>
23
25#include "qgspainteffect.h"
27#include "qgseffectstack.h"
28#include "qgsapplication.h"
29#include "qgslogger.h"
30
31static bool _initWidgetFunction( const QString &name, QgsPaintEffectWidgetFunc f )
32{
34
35 QgsPaintEffectAbstractMetadata *abstractMetadata = registry->effectMetadata( name );
36 if ( !abstractMetadata )
37 {
38 QgsDebugError( QStringLiteral( "Failed to find paint effect entry in registry: %1" ).arg( name ) );
39 return false;
40 }
41 QgsPaintEffectMetadata *metadata = dynamic_cast<QgsPaintEffectMetadata *>( abstractMetadata );
42 if ( !metadata )
43 {
44 QgsDebugError( QStringLiteral( "Failed to cast paint effect's metadata: " ) .arg( name ) );
45 return false;
46 }
47 metadata->setWidgetFunction( f );
48 return true;
49}
50
51static void _initWidgetFunctions()
52{
53 static bool sInitialized = false;
54 if ( sInitialized )
55 return;
56
57 _initWidgetFunction( QStringLiteral( "blur" ), QgsBlurWidget::create );
58 _initWidgetFunction( QStringLiteral( "dropShadow" ), QgsShadowEffectWidget::create );
59 _initWidgetFunction( QStringLiteral( "innerShadow" ), QgsShadowEffectWidget::create );
60 _initWidgetFunction( QStringLiteral( "drawSource" ), QgsDrawSourceWidget::create );
61 _initWidgetFunction( QStringLiteral( "outerGlow" ), QgsGlowWidget::create );
62 _initWidgetFunction( QStringLiteral( "innerGlow" ), QgsGlowWidget::create );
63 _initWidgetFunction( QStringLiteral( "transform" ), QgsTransformWidget::create );
64 _initWidgetFunction( QStringLiteral( "color" ), QgsColorEffectWidget::create );
65
66 sInitialized = true;
67}
68
69
71 : QWidget( parent )
72 , mEffect( effect )
73{
74 setupUi( this );
75 _initWidgetFunctions();
76
77 populateEffectTypes();
78 // update effect type combo box
79 if ( effect )
80 {
81 const int idx = mEffectTypeCombo->findData( effect->type() );
82 mEffectTypeCombo->setCurrentIndex( idx );
83 }
84 // set the corresponding widget
85 updateEffectWidget( effect );
86 connect( mEffectTypeCombo, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsPaintEffectPropertiesWidget::effectTypeChanged );
87}
88
89
90void QgsPaintEffectPropertiesWidget::populateEffectTypes()
91{
93 const QStringList types = registry->effects();
94
95 const auto constTypes = types;
96 for ( const QString &type : constTypes )
97 {
98 //don't show stack effect
99 if ( type == QLatin1String( "effectStack" ) )
100 continue;
101
102 mEffectTypeCombo->addItem( registry->effectMetadata( type )->visibleName(), type );
103 }
104}
105
106void QgsPaintEffectPropertiesWidget::updateEffectWidget( QgsPaintEffect *effect )
107{
108 if ( !effect )
109 {
110 stackedWidget->setCurrentWidget( pageDummy );
111 return;
112 }
113
114 if ( stackedWidget->currentWidget() != pageDummy )
115 {
116 // stop updating from the original widget
117 if ( QgsPaintEffectWidget *pew = qobject_cast< QgsPaintEffectWidget * >( stackedWidget->currentWidget() ) )
119 stackedWidget->removeWidget( stackedWidget->currentWidget() );
120 }
121
123 QgsPaintEffectAbstractMetadata *am = registry->effectMetadata( effect->type() );
124 if ( am )
125 {
127 if ( w )
128 {
129 w->setPaintEffect( effect );
130 stackedWidget->addWidget( w );
131 stackedWidget->setCurrentWidget( w );
132 // start receiving updates from widget
134 return;
135 }
136 }
137 // When anything is not right
138 stackedWidget->setCurrentWidget( pageDummy );
139}
140
142{
143 QgsPaintEffect *effect = mEffect;
144 if ( !effect )
145 return;
146
147 const QString newEffectType = mEffectTypeCombo->currentData().toString();
148 if ( effect->type() == newEffectType )
149 return;
150
151 // get creation function for new effect from registry
153 QgsPaintEffectAbstractMetadata *am = registry->effectMetadata( newEffectType );
154 if ( !am ) // check whether the metadata is assigned
155 return;
156
157 // change effect to a new (with different type)
158 // base new effect on existing effect's properties
159 QgsPaintEffect *newEffect = am->createPaintEffect( effect->properties() );
160 if ( !newEffect )
161 return;
162
163 updateEffectWidget( newEffect );
164 emit changeEffect( newEffect );
165
166 mEffect = newEffect;
167}
168
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.
Base class for effect properties widgets.
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:38
QgsPaintEffectWidget *(* QgsPaintEffectWidgetFunc)()