QGIS API Documentation  2.14.0-Essen
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 
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 initialized = false;
53  if ( initialized )
54  return;
55 
64 
65  initialized = true;
66 }
67 
68 
70  : QWidget( parent )
71  , mEffect( effect )
72 {
73  setupUi( this );
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, SIGNAL( currentIndexChanged( int ) ), this, SLOT( 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 == "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  disconnect( stackedWidget->currentWidget(), SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) );
116  stackedWidget->removeWidget( stackedWidget->currentWidget() );
117  }
118 
120  QgsPaintEffectAbstractMetadata* am = registry->effectMetadata( effect->type() );
121  if ( am )
122  {
124  if ( w )
125  {
126  w->setPaintEffect( effect );
127  stackedWidget->addWidget( w );
128  stackedWidget->setCurrentWidget( w );
129  // start receiving updates from widget
130  connect( w, SIGNAL( changed() ), this, SLOT( emitSignalChanged() ) );
131  return;
132  }
133  }
134  // When anything is not right
135  stackedWidget->setCurrentWidget( pageDummy );
136 }
137 
139 {
140  QgsPaintEffect* effect = mEffect;
141  if ( !effect )
142  return;
143 
144  QString newEffectType = mEffectTypeCombo->itemData( mEffectTypeCombo->currentIndex() ).toString();
145  if ( effect->type() == newEffectType )
146  return;
147 
148  // get creation function for new effect from registry
150  QgsPaintEffectAbstractMetadata* am = registry->effectMetadata( newEffectType );
151  if ( !am ) // check whether the metadata is assigned
152  return;
153 
154  // change effect to a new (with different type)
155  // base new effect on existing effect's properties
156  QgsPaintEffect* newEffect = am->createPaintEffect( effect->properties() );
157  if ( !newEffect )
158  return;
159 
160  updateEffectWidget( newEffect );
161  emit changeEffect( newEffect );
162 
163  mEffect = newEffect;
164 }
165 
167 {
168  emit changed();
169 }
Convenience metadata class that uses static functions to create an effect and its widget...
void setupUi(QWidget *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:33
QString visibleName() const
Returns the user visible string representing the paint effect class.
static bool _initWidgetFunction(const QString &name, QgsPaintEffectWidgetFunc f)
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.
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QStringList effects() const
Returns a list of known paint effects.
static QgsPaintEffectWidget * create()
static QgsPaintEffectRegistry * instance()
Returns a reference to the singleton instance of the paint effect registry.
virtual QString type() const =0
Returns the effect type.
void emitSignalChanged()
Emits the changed signal.
QgsPaintEffectWidget *(* QgsPaintEffectWidgetFunc)()
QgsPaintEffectAbstractMetadata * effectMetadata(const QString &name) const
Returns the metadata for a specific effect.
static void _initWidgetFunctions()
static QgsPaintEffectWidget * create()
virtual QgsPaintEffect * createPaintEffect(const QgsStringMap &map)=0
Create a paint effect of this class given an encoded map of properties.
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()
static QgsPaintEffectWidget * create()
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
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.
Singleton registry of available paint effects.