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 delete mProjectStyle;
139 mProjectStyle =
nullptr;
148 mProjectStyle->deleteLater();
150 mProjectStyle = style;
151 mProjectStyle->
setName( tr(
"Project Styles" ) );
159 mCombinedStyleModel->
addStyle( mProjectStyle );
166 return mProjectStyle;
171 mRandomizeDefaultSymbolColor = element.attribute( QStringLiteral(
"RandomizeDefaultSymbolColor" ), QStringLiteral(
"0" ) ).toInt();
172 mDefaultSymbolOpacity = element.attribute( QStringLiteral(
"DefaultSymbolOpacity" ), QStringLiteral(
"1.0" ) ).toDouble();
174 QDomElement elem = element.firstChildElement( QStringLiteral(
"markerSymbol" ) );
175 if ( !elem.isNull() )
177 QDomElement symbolElem = elem.firstChildElement( QStringLiteral(
"symbol" ) );
178 mDefaultMarkerSymbol.reset( !symbolElem.isNull() ? QgsSymbolLayerUtils::loadSymbol<QgsMarkerSymbol>( symbolElem, context ) :
nullptr );
182 mDefaultMarkerSymbol.reset();
185 elem = element.firstChildElement( QStringLiteral(
"lineSymbol" ) );
186 if ( !elem.isNull() )
188 QDomElement symbolElem = elem.firstChildElement( QStringLiteral(
"symbol" ) );
189 mDefaultLineSymbol.reset( !symbolElem.isNull() ? QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( symbolElem, context ) :
nullptr );
193 mDefaultLineSymbol.reset();
196 elem = element.firstChildElement( QStringLiteral(
"fillSymbol" ) );
197 if ( !elem.isNull() )
199 QDomElement symbolElem = elem.firstChildElement( QStringLiteral(
"symbol" ) );
200 mDefaultFillSymbol.reset( !symbolElem.isNull() ? QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElem, context ) :
nullptr );
204 mDefaultFillSymbol.reset();
207 elem = element.firstChildElement( QStringLiteral(
"colorramp" ) );
210 elem = element.firstChildElement( QStringLiteral(
"text-style" ) );
211 if ( !elem.isNull() )
213 mDefaultTextFormat.
readXml( elem, context );
224 const QDomElement styleDatabases = element.firstChildElement( QStringLiteral(
"databases" ) );
225 if ( !styleDatabases.isNull() )
227 const QDomNodeList styleEntries = styleDatabases.childNodes();
228 for (
int i = 0; i < styleEntries.count(); ++i )
230 const QDomElement styleElement = styleEntries.at( i ).toElement();
231 const QString path = styleElement.attribute( QStringLiteral(
"path" ) );
234 mStyleDatabases.append( fullPath );
235 loadStyleAtPath( fullPath );
242 const QString projectStyleId = element.attribute( QStringLiteral(
"projectStyleId" ) );
245 if ( !projectStyleFile.isEmpty() && QFile::exists( projectStyleFile ) )
247 style->
load( projectStyleFile );
252 const QString stylePath = mProject->
createAttachedFile( QStringLiteral(
"styles.db" ) );
256 style->
setName( tr(
"Project Style" ) );
269 QDomElement element = doc.createElement( QStringLiteral(
"ProjectStyleSettings" ) );
271 element.setAttribute( QStringLiteral(
"RandomizeDefaultSymbolColor" ), mRandomizeDefaultSymbolColor ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
272 element.setAttribute( QStringLiteral(
"DefaultSymbolOpacity" ), QString::number( mDefaultSymbolOpacity ) );
274 if ( mDefaultMarkerSymbol )
276 QDomElement markerSymbolElem = doc.createElement( QStringLiteral(
"markerSymbol" ) );
278 element.appendChild( markerSymbolElem );
281 if ( mDefaultLineSymbol )
283 QDomElement lineSymbolElem = doc.createElement( QStringLiteral(
"lineSymbol" ) );
285 element.appendChild( lineSymbolElem );
288 if ( mDefaultFillSymbol )
290 QDomElement fillSymbolElem = doc.createElement( QStringLiteral(
"fillSymbol" ) );
292 element.appendChild( fillSymbolElem );
295 if ( mDefaultColorRamp )
298 element.appendChild( colorRampElem );
301 if ( mDefaultTextFormat.
isValid() )
303 QDomElement textFormatElem = mDefaultTextFormat.
writeXml( doc, context );
304 element.appendChild( textFormatElem );
308 QDomElement styleDatabases = doc.createElement( QStringLiteral(
"databases" ) );
309 for (
const QString &db : mStyleDatabases )
311 QDomElement dbElement = doc.createElement( QStringLiteral(
"db" ) );
313 styleDatabases.appendChild( dbElement );
315 element.appendChild( styleDatabases );
318 if ( mProject && mProjectStyle )
328 QList< QgsStyle * > res;
329 res.reserve( mStyles.size() );
343 if ( mProjectStyle && path == mProjectStyle->
fileName() )
344 return mProjectStyle;
346 for (
QgsStyle *style : std::as_const( mStyles ) )
348 if ( style->fileName() == path )
357 if ( mStyleDatabases.contains( path ) )
361 mStyleDatabases.append( path );
362 loadStyleAtPath( path );
370 if ( paths == mStyleDatabases )
375 for (
const QString &path : paths )
378 mStyleDatabases.append( path );
379 loadStyleAtPath( path );
385void QgsProjectStyleSettings::loadStyleAtPath(
const QString &path )
389 const QFileInfo fileInfo( path );
390 if ( fileInfo.suffix().compare( QLatin1String(
"xml" ), Qt::CaseInsensitive ) == 0 )
401 style->
setName( fileInfo.completeBaseName() );
402 mStyles.append( style );
403 mCombinedStyleModel->
addStyle( style );
413void QgsProjectStyleSettings::clearStyles()
415 const QStringList pathsToRemove = mStyleDatabases;
416 for (
const QString &path : pathsToRemove )
419 mStyleDatabases.removeAll( path );
423 style->deleteLater();
424 mStyles.removeAll( style );
430 for (
QgsStyle *style : std::as_const( mStyles ) )
434 qDeleteAll( mStyles );
440 return mCombinedStyleModel;
451 : QAbstractListModel( parent )
452 , mSettings( settings )
467 return ( mSettings ? mSettings->
styleDatabasePaths().count() : 0 ) + ( mProjectStyle ? 1 : 0 ) + ( mShowDefault ? 1 : 0 );
472 if ( index.row() < 0 || index.row() >=
rowCount( QModelIndex() ) )
475 const bool isProjectStyle = index.row() == 0 && mProjectStyle;
476 const bool isDefault = mShowDefault && ( ( index.row() == 0 && !mProjectStyle ) || ( index.row() == 1 && mProjectStyle ) );
477 const int styleRow = index.row() - ( mShowDefault ? 1 : 0 ) - ( mProjectStyle ? 1 : 0 );
481 case Qt::DisplayRole:
485 else if ( isProjectStyle )
486 return mProjectStyle->name();
488 return mSettings ? mSettings->
styles().at( styleRow )->name() : QVariant();
490 case Qt::ToolTipRole:
493 else if ( isProjectStyle )
494 return mProjectStyle->name();
496 return mSettings ? QDir::toNativeSeparators( mSettings->
styles().at( styleRow )->fileName() ) : QVariant();
502 else if ( isProjectStyle )
503 return QVariant::fromValue( mProjectStyle.data() );
504 else if (
QgsStyle *style = mSettings->
styles().value( styleRow ) )
505 return QVariant::fromValue( style );
513 else if ( isProjectStyle )
514 return mProjectStyle->fileName();
516 return mSettings ? mSettings->
styles().at( styleRow )->fileName() : QVariant();
525 if ( index.row() == 0 && mProjectStyle )
526 return mProjectStyle;
527 else if ( mShowDefault && ( ( index.row() == 0 && !mProjectStyle ) || ( index.row() == 1 && mProjectStyle ) ) )
529 else if (
QgsStyle *style = qobject_cast< QgsStyle * >( qvariant_cast<QObject *>(
data( index,
static_cast< int >(
CustomRole::Style ) ) ) ) )
537 if ( style == mProjectStyle )
538 return index( 0, 0, QModelIndex() );
540 return index( mProjectStyle ? 1 : 0, 0, QModelIndex() );
544 return QModelIndex();
547 const int r = mSettings->
styles().indexOf( style );
549 return QModelIndex();
551 QModelIndex idx = index( r + ( mShowDefault ? 1 : 0 ) + ( mProjectStyle ? 1 : 0 ), 0, QModelIndex() );
557 return QModelIndex();
562 if ( show == mShowDefault )
565 const int row = mProjectStyle ? 1 : 0;
568 beginInsertRows( QModelIndex(), row, row );
574 beginRemoveRows( QModelIndex(), row, row );
575 mShowDefault =
false;
580void QgsProjectStyleDatabaseModel::setProjectStyle(
QgsStyle *style )
582 if ( style == mProjectStyle )
588 disconnect( mProjectStyle, &QgsStyle::destroyed,
this, &QgsProjectStyleDatabaseModel::projectStyleDestroyed );
589 beginRemoveRows( QModelIndex(), 0, 0 );
590 mProjectStyle =
nullptr;
596 beginInsertRows( QModelIndex(), 0, 0 );
597 mProjectStyle = style;
601 connect( mProjectStyle, &QgsStyle::destroyed,
this, &QgsProjectStyleDatabaseModel::projectStyleDestroyed );
605void QgsProjectStyleDatabaseModel::styleDatabaseAboutToBeAdded(
const QString & )
607 int row = mSettings->
styles().count() + ( mShowDefault ? 1 : 0 ) + ( mProjectStyle ? 1 : 0 );
608 beginInsertRows( QModelIndex(), row, row );
611void QgsProjectStyleDatabaseModel::styleDatabaseAboutToBeRemoved(
const QString &path )
614 int row = mSettings->
styles().indexOf( style ) + ( mShowDefault ? 1 : 0 ) + ( mProjectStyle ? 1 : 0 );
616 beginRemoveRows( QModelIndex(), row, row );
619void QgsProjectStyleDatabaseModel::styleDatabaseAdded(
const QString & )
624void QgsProjectStyleDatabaseModel::styleDatabaseRemoved(
const QString & )
629void QgsProjectStyleDatabaseModel::projectStyleAboutToBeDestroyed()
631 beginRemoveRows( QModelIndex(), 0, 0 );
634void QgsProjectStyleDatabaseModel::projectStyleDestroyed()
639void QgsProjectStyleDatabaseModel::projectStyleChanged()
649 : QSortFilterProxyModel( parent )
651 setSourceModel( model );
652 setDynamicSortFilter(
true );
QFlags< ProjectReadFlag > ProjectReadFlags
Project load flags.
@ 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=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
@ FilterHideReadOnly
Hide read-only style databases.
QgsProjectStyleDatabaseProxyModel::Filters filters() const
Returns the current filters used for filtering available style.
QFlags< Filter > Filters
Available filter flags for filtering the model.
void setFilters(QgsProjectStyleDatabaseProxyModel::Filters filters)
Sets the current filters used for filtering available styles.
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
QgsProjectStyleDatabaseProxyModel(QgsProjectStyleDatabaseModel *model, QObject *parent=nullptr)
Constructor for QgsProjectStyleDatabaseProxyModel, for the specified style database model.
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 setProjectStyle(QgsStyle *style)
Sets the style database to use for the project style.
void projectStyleChanged()
Emitted when the style returned by projectStyle() is changed.
QgsColorRamp * defaultColorRamp() const
Returns the project default color ramp.
QgsSymbol * defaultSymbol(Qgis::SymbolType symbolType) const
Returns the project default symbol for a given type.
void setDefaultSymbol(Qgis::SymbolType symbolType, QgsSymbol *symbol)
Sets the project default symbol for a given type.
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.
void removeProjectStyle()
Removes and deletes the project style database.
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.
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.
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.
static QgsStyle * defaultStyle(bool initialize=true)
Returns the default application-wide style.
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.