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