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