42 mProjectStyle->deleteLater();
43 mProjectStyle =
nullptr;
52 return mDefaultMarkerSymbol ? mDefaultMarkerSymbol->clone() :
nullptr;
55 return mDefaultLineSymbol ? mDefaultLineSymbol->clone() :
nullptr;
58 return mDefaultFillSymbol ? mDefaultFillSymbol->clone() :
nullptr;
72 mDefaultMarkerSymbol.reset( symbol ? symbol->
clone() :
nullptr );
76 mDefaultLineSymbol.reset( symbol ? symbol->
clone() :
nullptr );
80 mDefaultFillSymbol.reset( symbol ? symbol->
clone() :
nullptr );
90 return mDefaultColorRamp ? mDefaultColorRamp->clone() :
nullptr;
95 mDefaultColorRamp.reset( colorRamp ? colorRamp->
clone() :
nullptr );
100 return mDefaultTextFormat;
105 mDefaultTextFormat = textFormat;
110 mDefaultMarkerSymbol.reset();
111 mDefaultLineSymbol.reset();
112 mDefaultFillSymbol.reset();
113 mDefaultColorRamp.reset();
115 mRandomizeDefaultSymbolColor =
true;
116 mDefaultSymbolOpacity = 1.0;
122 const QString stylePath = mProject->
createAttachedFile( QStringLiteral(
"styles.db" ) );
125 style->
setName( tr(
"Project Style" ) );
138 mProjectStyle->deleteLater();
140 mProjectStyle = style;
141 mProjectStyle->
setName( tr(
"Project Styles" ) );
149 mCombinedStyleModel->
addStyle( mProjectStyle );
156 return mProjectStyle;
161 mRandomizeDefaultSymbolColor = element.attribute( QStringLiteral(
"RandomizeDefaultSymbolColor" ), QStringLiteral(
"0" ) ).toInt();
162 mDefaultSymbolOpacity = element.attribute( QStringLiteral(
"DefaultSymbolOpacity" ), QStringLiteral(
"1.0" ) ).toDouble();
164 QDomElement elem = element.firstChildElement( QStringLiteral(
"markerSymbol" ) );
165 if ( !elem.isNull() )
167 QDomElement symbolElem = elem.firstChildElement( QStringLiteral(
"symbol" ) );
168 mDefaultMarkerSymbol.reset( !symbolElem.isNull() ? QgsSymbolLayerUtils::loadSymbol<QgsMarkerSymbol>( symbolElem, context ) :
nullptr );
172 mDefaultMarkerSymbol.reset();
175 elem = element.firstChildElement( QStringLiteral(
"lineSymbol" ) );
176 if ( !elem.isNull() )
178 QDomElement symbolElem = elem.firstChildElement( QStringLiteral(
"symbol" ) );
179 mDefaultLineSymbol.reset( !symbolElem.isNull() ? QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( symbolElem, context ) :
nullptr );
183 mDefaultLineSymbol.reset();
186 elem = element.firstChildElement( QStringLiteral(
"fillSymbol" ) );
187 if ( !elem.isNull() )
189 QDomElement symbolElem = elem.firstChildElement( QStringLiteral(
"symbol" ) );
190 mDefaultFillSymbol.reset( !symbolElem.isNull() ? QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElem, context ) :
nullptr );
194 mDefaultFillSymbol.reset();
197 elem = element.firstChildElement( QStringLiteral(
"colorramp" ) );
200 elem = element.firstChildElement( QStringLiteral(
"text-style" ) );
201 if ( !elem.isNull() )
203 mDefaultTextFormat.
readXml( elem, context );
214 const QDomElement styleDatabases = element.firstChildElement( QStringLiteral(
"databases" ) );
215 if ( !styleDatabases.isNull() )
217 const QDomNodeList styleEntries = styleDatabases.childNodes();
218 for (
int i = 0; i < styleEntries.count(); ++i )
220 const QDomElement styleElement = styleEntries.at( i ).toElement();
221 const QString path = styleElement.attribute( QStringLiteral(
"path" ) );
224 mStyleDatabases.append( fullPath );
225 loadStyleAtPath( fullPath );
232 const QString projectStyleId = element.attribute( QStringLiteral(
"projectStyleId" ) );
235 if ( !projectStyleFile.isEmpty() && QFile::exists( projectStyleFile ) )
237 style->
load( projectStyleFile );
242 const QString stylePath = mProject->
createAttachedFile( QStringLiteral(
"styles.db" ) );
246 style->
setName( tr(
"Project Style" ) );
259 QDomElement element = doc.createElement( QStringLiteral(
"ProjectStyleSettings" ) );
261 element.setAttribute( QStringLiteral(
"RandomizeDefaultSymbolColor" ), mRandomizeDefaultSymbolColor ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
262 element.setAttribute( QStringLiteral(
"DefaultSymbolOpacity" ), QString::number( mDefaultSymbolOpacity ) );
264 if ( mDefaultMarkerSymbol )
266 QDomElement markerSymbolElem = doc.createElement( QStringLiteral(
"markerSymbol" ) );
268 element.appendChild( markerSymbolElem );
271 if ( mDefaultLineSymbol )
273 QDomElement lineSymbolElem = doc.createElement( QStringLiteral(
"lineSymbol" ) );
275 element.appendChild( lineSymbolElem );
278 if ( mDefaultFillSymbol )
280 QDomElement fillSymbolElem = doc.createElement( QStringLiteral(
"fillSymbol" ) );
282 element.appendChild( fillSymbolElem );
285 if ( mDefaultColorRamp )
288 element.appendChild( colorRampElem );
291 if ( mDefaultTextFormat.
isValid() )
293 QDomElement textFormatElem = mDefaultTextFormat.
writeXml( doc, context );
294 element.appendChild( textFormatElem );
298 QDomElement styleDatabases = doc.createElement( QStringLiteral(
"databases" ) );
299 for (
const QString &db : mStyleDatabases )
301 QDomElement dbElement = doc.createElement( QStringLiteral(
"db" ) );
303 styleDatabases.appendChild( dbElement );
305 element.appendChild( styleDatabases );
308 if ( mProject && mProjectStyle )
318 QList< QgsStyle * > res;
319 res.reserve( mStyles.size() );
333 if ( mProjectStyle && path == mProjectStyle->
fileName() )
334 return mProjectStyle;
336 for (
QgsStyle *style : std::as_const( mStyles ) )
338 if ( style->fileName() == path )
347 if ( mStyleDatabases.contains( path ) )
351 mStyleDatabases.append( path );
352 loadStyleAtPath( path );
360 if ( paths == mStyleDatabases )
365 for (
const QString &path : paths )
368 mStyleDatabases.append( path );
369 loadStyleAtPath( path );
375void QgsProjectStyleSettings::loadStyleAtPath(
const QString &path )
379 const QFileInfo fileInfo( path );
380 if ( fileInfo.suffix().compare( QLatin1String(
"xml" ), Qt::CaseInsensitive ) == 0 )
391 style->
setName( fileInfo.completeBaseName() );
392 mStyles.append( style );
393 mCombinedStyleModel->
addStyle( style );
403void QgsProjectStyleSettings::clearStyles()
405 const QStringList pathsToRemove = mStyleDatabases;
406 for (
const QString &path : pathsToRemove )
409 mStyleDatabases.removeAll( path );
413 style->deleteLater();
414 mStyles.removeAll( style );
420 for (
QgsStyle *style : std::as_const( mStyles ) )
424 qDeleteAll( mStyles );
430 return mCombinedStyleModel;
441 : QAbstractListModel( parent )
442 , mSettings( settings )
457 return ( mSettings ? mSettings->
styleDatabasePaths().count() : 0 ) + ( mProjectStyle ? 1 : 0 ) + ( mShowDefault ? 1 : 0 );
462 if ( index.row() < 0 || index.row() >=
rowCount( QModelIndex() ) )
465 const bool isProjectStyle = index.row() == 0 && mProjectStyle;
466 const bool isDefault = mShowDefault && ( ( index.row() == 0 && !mProjectStyle ) || ( index.row() == 1 && mProjectStyle ) );
467 const int styleRow = index.row() - ( mShowDefault ? 1 : 0 ) - ( mProjectStyle ? 1 : 0 );
471 case Qt::DisplayRole:
475 else if ( isProjectStyle )
476 return mProjectStyle->name();
478 return mSettings ? mSettings->
styles().at( styleRow )->name() : QVariant();
480 case Qt::ToolTipRole:
483 else if ( isProjectStyle )
484 return mProjectStyle->name();
486 return mSettings ? QDir::toNativeSeparators( mSettings->
styles().at( styleRow )->fileName() ) : QVariant();
492 else if ( isProjectStyle )
493 return QVariant::fromValue( mProjectStyle.data() );
494 else if (
QgsStyle *style = mSettings->
styles().value( styleRow ) )
495 return QVariant::fromValue( style );
503 else if ( isProjectStyle )
504 return mProjectStyle->fileName();
506 return mSettings ? mSettings->
styles().at( styleRow )->fileName() : QVariant();
515 if ( index.row() == 0 && mProjectStyle )
516 return mProjectStyle;
517 else if ( mShowDefault && ( ( index.row() == 0 && !mProjectStyle ) || ( index.row() == 1 && mProjectStyle ) ) )
519 else if (
QgsStyle *style = qobject_cast< QgsStyle * >( qvariant_cast<QObject *>(
data( index,
StyleRole ) ) ) )
527 if ( style == mProjectStyle )
528 return index( 0, 0, QModelIndex() );
530 return index( mProjectStyle ? 1 : 0, 0, QModelIndex() );
534 return QModelIndex();
537 const int r = mSettings->
styles().indexOf( style );
539 return QModelIndex();
541 QModelIndex idx = index( r + ( mShowDefault ? 1 : 0 ) + ( mProjectStyle ? 1 : 0 ), 0, QModelIndex() );
547 return QModelIndex();
552 if ( show == mShowDefault )
555 const int row = mProjectStyle ? 1 : 0;
558 beginInsertRows( QModelIndex(), row, row );
564 beginRemoveRows( QModelIndex(), row, row );
565 mShowDefault =
false;
570void QgsProjectStyleDatabaseModel::setProjectStyle(
QgsStyle *style )
572 if ( style == mProjectStyle )
578 disconnect( mProjectStyle, &QgsStyle::destroyed,
this, &QgsProjectStyleDatabaseModel::projectStyleDestroyed );
579 beginRemoveRows( QModelIndex(), 0, 0 );
580 mProjectStyle =
nullptr;
586 beginInsertRows( QModelIndex(), 0, 0 );
587 mProjectStyle = style;
591 connect( mProjectStyle, &QgsStyle::destroyed,
this, &QgsProjectStyleDatabaseModel::projectStyleDestroyed );
595void QgsProjectStyleDatabaseModel::styleDatabaseAboutToBeAdded(
const QString & )
597 int row = mSettings->
styles().count() + ( mShowDefault ? 1 : 0 ) + ( mProjectStyle ? 1 : 0 );
598 beginInsertRows( QModelIndex(), row, row );
601void QgsProjectStyleDatabaseModel::styleDatabaseAboutToBeRemoved(
const QString &path )
604 int row = mSettings->
styles().indexOf( style ) + ( mShowDefault ? 1 : 0 ) + ( mProjectStyle ? 1 : 0 );
606 beginRemoveRows( QModelIndex(), row, row );
609void QgsProjectStyleDatabaseModel::styleDatabaseAdded(
const QString & )
614void QgsProjectStyleDatabaseModel::styleDatabaseRemoved(
const QString & )
619void QgsProjectStyleDatabaseModel::projectStyleAboutToBeDestroyed()
621 beginRemoveRows( QModelIndex(), 0, 0 );
624void QgsProjectStyleDatabaseModel::projectStyleDestroyed()
629void QgsProjectStyleDatabaseModel::projectStyleChanged()
639 : QSortFilterProxyModel( parent )
641 setSourceModel( model );
642 setDynamicSortFilter(
true );
649 if (
const QgsStyle *style = qobject_cast< QgsStyle * >( sourceModel()->data( sourceModel()->index( sourceRow, 0, sourceParent ), QgsProjectStyleDatabaseModel::Role::StyleRole ).value< QObject * >() ) )
@ ProjectStyles
Enable the project embedded style library. Enabling this flag can increase the time required to clear...
Abstract base class for color ramps.
virtual QgsColorRamp * clone() const =0
Creates a clone of the color ramp.
A model which contains entities from multiple QgsStyle databases.
void removeStyle(QgsStyle *style)
Removes a style from the model.
void addStyle(QgsStyle *style)
Adds a style to the model.
QString writePath(const QString &filename) const
Prepare a filename to save it to the project file.
QString readPath(const QString &filename) const
Turn filename read from the project file to an absolute path.
List model representing the style databases associated with a QgsProject.
QVariant data(const QModelIndex &index, int role) const override
void setShowDefaultStyle(bool show)
Sets whether the default style should also be included in the model.
QModelIndex indexFromStyle(QgsStyle *style) const
Returns the model index corresponding to a style.
QgsProjectStyleDatabaseModel(QgsProjectStyleSettings *settings, QObject *parent SIP_TRANSFERTHIS=nullptr)
Constructor for QgsProjectStyleDatabaseModel, showing the styles from the specified settings.
QgsStyle * styleFromIndex(const QModelIndex &index) const
Returns the style at the corresponding index.
int rowCount(const QModelIndex &parent) const override
QgsProjectStyleDatabaseProxyModel(QgsProjectStyleDatabaseModel *model, QObject *parent SIP_TRANSFERTHIS=nullptr)
Available filter flags for filtering the model.
QgsProjectStyleDatabaseProxyModel::Filters filters() const
Returns the current filters used for filtering available style.
void setFilters(QgsProjectStyleDatabaseProxyModel::Filters filters)
Sets the current filters used for filtering available styles.
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
@ FilterHideReadOnly
Hide read-only style databases.
Contains settings and properties relating to how a QgsProject should handle styling.
void setDefaultTextFormat(const QgsTextFormat &textFormat)
Sets the project default text format.
QList< QgsStyle * > styles() const
Returns a list of all the styles associated with the project.
void setStyleDatabasePaths(const QStringList &paths)
Sets the paths to all style databases associated with the project.
QgsStyle * styleAtPath(const QString &path)
Returns a reference to the style database associated with the project with matching file path.
QgsTextFormat defaultTextFormat() const
Returns the project default text format.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Returns a DOM element representing the settings.
~QgsProjectStyleSettings() override
void projectStyleChanged()
Emitted when the style returned by projectStyle() is changed.
void setDefaultSymbol(Qgis::SymbolType symbolType, QgsSymbol *symbol)
Sets the project default symbol for a given type.
void setProjectStyle(QgsStyle *style SIP_TRANSFER)
Sets the style database to use for the project style.
QStringList styleDatabasePaths() const
Returns a list of all style databases (file paths) associated with the project.
void reset()
Resets the settings to a default state.
QgsProjectStyleSettings(QgsProject *project=nullptr)
Constructor for QgsProjectStyleSettings for the specified project.
void styleDatabaseAdded(const QString &path)
Emitted when a style database path is added.
void styleDatabaseAboutToBeRemoved(const QString &path)
Emitted when a style database path is about to be removed.
QgsSymbol * defaultSymbol(Qgis::SymbolType symbolType) const SIP_FACTORY
Returns the project default symbol for a given type.
void setDefaultColorRamp(QgsColorRamp *colorRamp)
Sets the project default color ramp.
void addStyleDatabasePath(const QString &path)
Adds a style database path to the project.
void styleDatabaseRemoved(const QString &path)
Emitted when a style database path is removed.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context, Qgis::ProjectReadFlags flags=Qgis::ProjectReadFlags())
Reads the settings's state from a DOM element.
QgsStyle * projectStyle()
Returns the style database to use for project specific styles.
QgsCombinedStyleModel * combinedStyleModel()
Returns the combined style model which includes all style databases associated with the project.
QgsColorRamp * defaultColorRamp() const SIP_FACTORY
Returns the project default color ramp.
void styleDatabaseAboutToBeAdded(const QString &path)
Emitted when a style database path is about to be added.
void styleDatabasesChanged()
Emitted whenever the set of style databases associated with the project is changed.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
QString createAttachedFile(const QString &nameTemplate)
Attaches a file to the project.
QString attachmentIdentifier(const QString &attachedFile) const
Returns an identifier for an attachment file path An attachment identifier is a string which does not...
QString resolveAttachmentIdentifier(const QString &identifier) const
Resolves an attachment identifier to a attachment file path.
void projectColorsChanged()
Emitted whenever the project's color scheme has been changed.
Qgis::ProjectCapabilities capabilities() const
Returns the project's capabilities, which dictate optional functionality which can be selectively ena...
The class is used as a container of context for various read/write operations on other objects.
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.
void setFileName(const QString &filename)
Sets the current file name of the style database.
void aboutToBeDestroyed()
Emitted just before the style object is destroyed.
bool createDatabase(const QString &filename)
Creates an on-disk database.
static QgsStyle * defaultStyle()
Returns default application-wide style.
void triggerIconRebuild()
Triggers emission of the rebuildIconPreviews() signal.
void setName(const QString &name)
Sets the name of the style.
QString fileName() const
Returns the current file name of the style database.
bool isReadOnly() const
Returns true if the style is considered a read-only library.
bool createMemoryDatabase()
Creates a temporary memory database.
bool load(const QString &filename)
Loads a file into the style.
QString name() const
Returns the name of the style.
void setReadOnly(bool readOnly)
Sets whether the style is considered a read-only library.
bool importXml(const QString &filename)
Imports the symbols and colorramps into the default style database from the given XML file.
static QgsColorRamp * loadColorRamp(QDomElement &element)
Creates a color ramp from the settings encoded in an XML element.
static QDomElement saveColorRamp(const QString &name, QgsColorRamp *ramp, QDomDocument &doc)
Encodes a color ramp's settings to an XML element.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
Abstract base class for all rendered symbols.
virtual QgsSymbol * clone() const =0
Returns a deep copy of this symbol.
Container for all settings relating to text rendering.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
bool isValid() const
Returns true if the format is valid.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.