QGIS API Documentation  3.2.0-Bonn (bc43194)
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( QString( "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( QString( "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  Q_FOREACH ( const QString &type, types )
95  {
96  //don't show stack effect
97  if ( type == QLatin1String( "effectStack" ) )
98  continue;
99 
100  mEffectTypeCombo->addItem( registry->effectMetadata( type )->visibleName(), type );
101  }
102 }
103 
104 void QgsPaintEffectPropertiesWidget::updateEffectWidget( QgsPaintEffect *effect )
105 {
106  if ( !effect )
107  {
108  stackedWidget->setCurrentWidget( pageDummy );
109  return;
110  }
111 
112  if ( stackedWidget->currentWidget() != pageDummy )
113  {
114  // stop updating from the original widget
115  if ( QgsPaintEffectWidget *pew = qobject_cast< QgsPaintEffectWidget * >( stackedWidget->currentWidget() ) )
117  stackedWidget->removeWidget( stackedWidget->currentWidget() );
118  }
119 
121  QgsPaintEffectAbstractMetadata *am = registry->effectMetadata( effect->type() );
122  if ( am )
123  {
125  if ( w )
126  {
127  w->setPaintEffect( effect );
128  stackedWidget->addWidget( w );
129  stackedWidget->setCurrentWidget( w );
130  // start receiving updates from widget
132  return;
133  }
134  }
135  // When anything is not right
136  stackedWidget->setCurrentWidget( pageDummy );
137 }
138 
140 {
141  QgsPaintEffect *effect = mEffect;
142  if ( !effect )
143  return;
144 
145  QString newEffectType = mEffectTypeCombo->currentData().toString();
146  if ( effect->type() == newEffectType )
147  return;
148 
149  // get creation function for new effect from registry
151  QgsPaintEffectAbstractMetadata *am = registry->effectMetadata( newEffectType );
152  if ( !am ) // check whether the metadata is assigned
153  return;
154 
155  // change effect to a new (with different type)
156  // base new effect on existing effect's properties
157  QgsPaintEffect *newEffect = am->createPaintEffect( effect->properties() );
158  if ( !newEffect )
159  return;
160 
161  updateEffectWidget( newEffect );
162  emit changeEffect( newEffect );
163 
164  mEffect = newEffect;
165 }
166 
168 {
169  emit changed();
170 }
Convenience metadata class that uses static functions to create an effect and its widget...
virtual QgsStringMap properties() const =0
Returns the properties describing the paint effect encoded in a string format.
QgsPaintEffectPropertiesWidget(QgsPaintEffect *effect, QWidget *parent=nullptr)
QgsPaintEffectPropertiesWidget constructor.
Base class for effect properties widgets.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
void changeEffect(QgsPaintEffect *effect)
Emitted when paint effect type changes.
Base class for visual effects which can be applied to QPicture drawings.
Stores metadata about a paint effect class.
static QgsPaintEffectRegistry * paintEffectRegistry()
Returns the application&#39;s paint effect registry, used for managing paint effects. ...
static QgsPaintEffectWidget * create()
void changed()
Emitted when properties of the effect are changed through the widget.
virtual QString type() const =0
Returns the effect type.
void emitSignalChanged()
Emits the changed signal.
QgsPaintEffectWidget *(* QgsPaintEffectWidgetFunc)()
QStringList effects() const
Returns a list of known paint effects.
static QgsPaintEffectWidget * create()
virtual QgsPaintEffect * createPaintEffect(const QgsStringMap &map)=0
Create a paint effect of this class given an encoded map of properties.
QgsPaintEffectAbstractMetadata * effectMetadata(const QString &name) const
Returns the metadata for a specific effect.
void effectTypeChanged()
Update widget when effect type changes.
virtual void setPaintEffect(QgsPaintEffect *effect)=0
Sets the paint effect to modify with the widget.
void changed()
Emitted when paint effect properties changes.
static QgsPaintEffectWidget * create()
QString visibleName() const
Returns the user visible string representing the paint effect class.
static QgsPaintEffectWidget * create()
static QgsPaintEffectWidget * create()
static QgsPaintEffectWidget * create()
void setWidgetFunction(QgsPaintEffectWidgetFunc f)
Sets the paint effect properties widget creation function for the paint effect class.
virtual QgsPaintEffectWidget * createWidget()
Create configuration widget for paint effect of this class.
Registry of available paint effects.