22 #include <QDomElement> 
   23 #include <QTextStream> 
   32 QString QgsMapLayerStyleManager::defaultStyleName()
 const 
   34   return tr( 
"default" );
 
   41   mCurrentStyle = defaultStyleName();
 
   46   mCurrentStyle = mgrElement.attribute( QStringLiteral( 
"current" ) );
 
   47   if ( mCurrentStyle.isEmpty() )
 
   50     mCurrentStyle = defaultStyleName();
 
   54   QDomElement ch = mgrElement.firstChildElement( QStringLiteral( 
"map-layer-style" ) );
 
   55   while ( !ch.isNull() )
 
   57     QString name = ch.attribute( QStringLiteral( 
"name" ) );
 
   61       name = defaultStyleName();
 
   65     mStyles.insert( name, 
style );
 
   67     ch = ch.nextSiblingElement( QStringLiteral( 
"map-layer-style" ) );
 
   73   QDomDocument doc = mgrElement.ownerDocument();
 
   74   mgrElement.setAttribute( QStringLiteral( 
"current" ), mCurrentStyle );
 
   76   const auto constStyles = 
styles();
 
   77   for ( 
const QString &name : constStyles )
 
   79     QDomElement ch = doc.createElement( QStringLiteral( 
"map-layer-style" ) );
 
   80     ch.setAttribute( QStringLiteral( 
"name" ), name );
 
   81     mStyles[name].writeXml( ch );
 
   82     mgrElement.appendChild( ch );
 
   88   return mStyles.keys();
 
   98   if ( name == mCurrentStyle )
 
  106   return mStyles.value( name );
 
  111   if ( mStyles.contains( name ) )
 
  116   mStyles.insert( name, 
style );
 
  130   if ( !mStyles.contains( name ) )
 
  132   if ( mStyles.count() == 1 )
 
  136   if ( mCurrentStyle == name )
 
  138     QStringList keys = mStyles.keys();
 
  139     QString newCurrent = keys[0];
 
  140     if ( newCurrent == name )
 
  141       newCurrent = keys[1]; 
 
  145   mStyles.remove( name );
 
  152   if ( !mStyles.contains( name ) || mStyles.contains( newName ) )
 
  155   if ( name == mCurrentStyle )
 
  156     mCurrentStyle = newName;
 
  158   mStyles[newName] = mStyles[name];
 
  159   mStyles.remove( name );
 
  166   return mCurrentStyle;
 
  171   if ( !mStyles.contains( name ) )
 
  174   if ( mCurrentStyle == name )
 
  177   mStyles[mCurrentStyle].readFromLayer( mLayer ); 
 
  178   mCurrentStyle = name;
 
  179   mStyles[mCurrentStyle].writeToLayer( mLayer );
 
  180   mStyles[mCurrentStyle].clear(); 
 
  189   if ( mOverriddenOriginalStyle )
 
  192   mLayer->blockSignals( 
true );
 
  193   if ( mStyles.contains( styleDef ) )
 
  199     mStyles[styleDef].writeToLayer( mLayer );
 
  201   else if ( styleDef.startsWith( 
'<' ) )
 
  210   mLayer->blockSignals( 
false );
 
  217   if ( !mOverriddenOriginalStyle )
 
  220   mLayer->blockSignals( 
true );
 
  222   mLayer->blockSignals( 
false );
 
  224   delete mOverriddenOriginalStyle;
 
  225   mOverriddenOriginalStyle = 
nullptr;
 
  231   return styleName == defaultStyleName();
 
  236   const QStringList styleNames = other->mStyles.keys();
 
  238   for ( 
const QString &styleName : styleNames )
 
  240     mStyles.remove( styleName );
 
Management of styles for use with one map layer.
bool addStyle(const QString &name, const QgsMapLayerStyle &style)
Add a style with given name and data.
QString currentStyle() const
Returns name of the current style.
bool removeStyle(const QString &name)
Remove a stored style.
void copyStylesFrom(QgsMapLayerStyleManager *other)
Copies all styles from other.
QStringList styles() const
Returns list of all defined style names.
bool setCurrentStyle(const QString &name)
Set a different style as the current style - will apply it to the layer.
void styleAdded(const QString &name)
Emitted when a new style has been added.
void writeXml(QDomElement &mgrElement) const
Write configuration (for project saving)
void reset()
Reset the style manager to a basic state - with one default style which is set as current.
bool isDefault(const QString &styleName) const
Returns true if this is the default style.
void styleRenamed(const QString &oldName, const QString &newName)
Emitted when a style has been renamed.
bool restoreOverrideStyle()
Restore the original store after a call to setOverrideStyle()
bool addStyleFromLayer(const QString &name)
Add style by cloning the current one.
bool renameStyle(const QString &name, const QString &newName)
Rename a stored style to a different name.
QgsMapLayerStyleManager(QgsMapLayer *layer)
Construct a style manager associated with a map layer (must not be nullptr).
QMap< QString, QgsMapLayerStyle > mapLayerStyles() const
Gets available styles for the associated map layer.
bool setOverrideStyle(const QString &styleDef)
Temporarily apply a different style to the layer.
QgsMapLayerStyle style(const QString &name) const
Returns data of a stored style - accessed by its unique name.
void readXml(const QDomElement &mgrElement)
Read configuration (for project loading)
void currentStyleChanged(const QString ¤tName)
Emitted when the current style has been changed.
void styleRemoved(const QString &name)
Emitted when a style has been removed.
Stores style information (renderer, opacity, labeling, diagrams etc.) applicable to a map layer.
void readXml(const QDomElement &styleElement)
Read style configuration (for project file reading)
void readFromLayer(QgsMapLayer *layer)
Store layer's active style information in the instance.
void writeToLayer(QgsMapLayer *layer) const
Apply stored layer's style information to the layer.
bool isValid() const
Tell whether the style is valid (i.e. there is something stored in it)
Base class for all map layer types.
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.