28 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
32 #include <QDomElement>
38 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
47 mProjectStyle->deleteLater();
48 mProjectStyle =
nullptr;
57 return mDefaultMarkerSymbol ? mDefaultMarkerSymbol->clone() :
nullptr;
60 return mDefaultLineSymbol ? mDefaultLineSymbol->clone() :
nullptr;
63 return mDefaultFillSymbol ? mDefaultFillSymbol->clone() :
nullptr;
77 mDefaultMarkerSymbol.reset( symbol ? symbol->
clone() :
nullptr );
81 mDefaultLineSymbol.reset( symbol ? symbol->
clone() :
nullptr );
85 mDefaultFillSymbol.reset( symbol ? symbol->
clone() :
nullptr );
95 return mDefaultColorRamp ? mDefaultColorRamp->clone() :
nullptr;
100 mDefaultColorRamp.reset( colorRamp ? colorRamp->
clone() :
nullptr );
105 return mDefaultTextFormat;
110 mDefaultTextFormat = textFormat;
115 mDefaultMarkerSymbol.reset();
116 mDefaultLineSymbol.reset();
117 mDefaultFillSymbol.reset();
118 mDefaultColorRamp.reset();
120 mRandomizeDefaultSymbolColor =
true;
121 mDefaultSymbolOpacity = 1.0;
127 const QString stylePath = mProject->
createAttachedFile( QStringLiteral(
"styles.db" ) );
130 style->
setName( tr(
"Project Style" ) );
142 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
145 mProjectStyle->deleteLater();
147 mProjectStyle = style;
148 mProjectStyle->
setName( tr(
"Project Styles" ) );
156 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
157 mCombinedStyleModel->
addStyle( mProjectStyle );
165 return mProjectStyle;
170 mRandomizeDefaultSymbolColor = element.attribute( QStringLiteral(
"RandomizeDefaultSymbolColor" ), QStringLiteral(
"0" ) ).toInt();
171 mDefaultSymbolOpacity = element.attribute( QStringLiteral(
"DefaultSymbolOpacity" ), QStringLiteral(
"1.0" ) ).toDouble();
173 QDomElement elem = element.firstChildElement( QStringLiteral(
"markerSymbol" ) );
174 if ( !elem.isNull() )
176 QDomElement symbolElem = elem.firstChildElement( QStringLiteral(
"symbol" ) );
177 mDefaultMarkerSymbol.reset( !symbolElem.isNull() ? QgsSymbolLayerUtils::loadSymbol<QgsMarkerSymbol>( symbolElem, context ) :
nullptr );
181 mDefaultMarkerSymbol.reset();
184 elem = element.firstChildElement( QStringLiteral(
"lineSymbol" ) );
185 if ( !elem.isNull() )
187 QDomElement symbolElem = elem.firstChildElement( QStringLiteral(
"symbol" ) );
188 mDefaultLineSymbol.reset( !symbolElem.isNull() ? QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( symbolElem, context ) :
nullptr );
192 mDefaultLineSymbol.reset();
195 elem = element.firstChildElement( QStringLiteral(
"fillSymbol" ) );
196 if ( !elem.isNull() )
198 QDomElement symbolElem = elem.firstChildElement( QStringLiteral(
"symbol" ) );
199 mDefaultFillSymbol.reset( !symbolElem.isNull() ? QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElem, context ) :
nullptr );
203 mDefaultFillSymbol.reset();
206 elem = element.firstChildElement( QStringLiteral(
"colorramp" ) );
209 elem = element.firstChildElement( QStringLiteral(
"text-style" ) );
210 if ( !elem.isNull() )
212 mDefaultTextFormat.
readXml( elem, context );
223 const QDomElement styleDatabases = element.firstChildElement( QStringLiteral(
"databases" ) );
224 if ( !styleDatabases.isNull() )
226 const QDomNodeList styleEntries = styleDatabases.childNodes();
227 for (
int i = 0; i < styleEntries.count(); ++i )
229 const QDomElement styleElement = styleEntries.at( i ).toElement();
230 const QString path = styleElement.attribute( QStringLiteral(
"path" ) );
233 mStyleDatabases.append( fullPath );
234 loadStyleAtPath( fullPath );
241 const QString projectStyleId = element.attribute( QStringLiteral(
"projectStyleId" ) );
244 if ( !projectStyleFile.isEmpty() && QFile::exists( projectStyleFile ) )
246 style->
load( projectStyleFile );
251 const QString stylePath = mProject->
createAttachedFile( QStringLiteral(
"styles.db" ) );
255 style->
setName( tr(
"Project Style" ) );
268 QDomElement element = doc.createElement( QStringLiteral(
"ProjectStyleSettings" ) );
270 element.setAttribute( QStringLiteral(
"RandomizeDefaultSymbolColor" ), mRandomizeDefaultSymbolColor ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
271 element.setAttribute( QStringLiteral(
"DefaultSymbolOpacity" ), QString::number( mDefaultSymbolOpacity ) );
273 if ( mDefaultMarkerSymbol )
275 QDomElement markerSymbolElem = doc.createElement( QStringLiteral(
"markerSymbol" ) );
277 element.appendChild( markerSymbolElem );
280 if ( mDefaultLineSymbol )
282 QDomElement lineSymbolElem = doc.createElement( QStringLiteral(
"lineSymbol" ) );
284 element.appendChild( lineSymbolElem );
287 if ( mDefaultFillSymbol )
289 QDomElement fillSymbolElem = doc.createElement( QStringLiteral(
"fillSymbol" ) );
291 element.appendChild( fillSymbolElem );
294 if ( mDefaultColorRamp )
297 element.appendChild( colorRampElem );
300 if ( mDefaultTextFormat.
isValid() )
302 QDomElement textFormatElem = mDefaultTextFormat.
writeXml( doc, context );
303 element.appendChild( textFormatElem );
307 QDomElement styleDatabases = doc.createElement( QStringLiteral(
"databases" ) );
308 for (
const QString &db : mStyleDatabases )
310 QDomElement dbElement = doc.createElement( QStringLiteral(
"db" ) );
312 styleDatabases.appendChild( dbElement );
314 element.appendChild( styleDatabases );
317 if ( mProject && mProjectStyle )
327 QList< QgsStyle * > res;
328 res.reserve( mStyles.size() );
342 if ( mProjectStyle && path == mProjectStyle->
fileName() )
343 return mProjectStyle;
345 for (
QgsStyle *style : std::as_const( mStyles ) )
347 if ( style->fileName() == path )
356 if ( mStyleDatabases.contains( path ) )
360 mStyleDatabases.append( path );
361 loadStyleAtPath( path );
369 if ( paths == mStyleDatabases )
374 for (
const QString &path : paths )
377 mStyleDatabases.append( path );
378 loadStyleAtPath( path );
384 void QgsProjectStyleSettings::loadStyleAtPath(
const QString &path )
388 const QFileInfo fileInfo( path );
389 if ( fileInfo.suffix().compare( QLatin1String(
"xml" ), Qt::CaseInsensitive ) == 0 )
400 style->
setName( fileInfo.completeBaseName() );
401 mStyles.append( style );
402 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
403 mCombinedStyleModel->
addStyle( style );
414 void QgsProjectStyleSettings::clearStyles()
416 const QStringList pathsToRemove = mStyleDatabases;
417 for (
const QString &path : pathsToRemove )
420 mStyleDatabases.removeAll( path );
423 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
426 style->deleteLater();
427 mStyles.removeAll( style );
433 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
434 for (
QgsStyle *style : std::as_const( mStyles ) )
439 qDeleteAll( mStyles );
443 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
446 return mCombinedStyleModel;
458 : QAbstractListModel( parent )
459 , mSettings( settings )
466 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
476 return ( mSettings ? mSettings->
styleDatabasePaths().count() : 0 ) + ( mProjectStyle ? 1 : 0 ) + ( mShowDefault ? 1 : 0 );
481 if ( index.row() < 0 || index.row() >=
rowCount( QModelIndex() ) )
484 const bool isProjectStyle = index.row() == 0 && mProjectStyle;
485 const bool isDefault = mShowDefault && ( ( index.row() == 0 && !mProjectStyle ) || ( index.row() == 1 && mProjectStyle ) );
486 const int styleRow = index.row() - ( mShowDefault ? 1 : 0 ) - ( mProjectStyle ? 1 : 0 );
490 case Qt::DisplayRole:
494 else if ( isProjectStyle )
495 return mProjectStyle->name();
497 return mSettings ? mSettings->
styles().at( styleRow )->name() : QVariant();
499 case Qt::ToolTipRole:
502 else if ( isProjectStyle )
503 return mProjectStyle->name();
505 return mSettings ? QDir::toNativeSeparators( mSettings->
styles().at( styleRow )->fileName() ) : QVariant();
511 else if ( isProjectStyle )
512 return QVariant::fromValue( mProjectStyle.data() );
513 else if (
QgsStyle *style = mSettings->
styles().value( styleRow ) )
514 return QVariant::fromValue( style );
522 else if ( isProjectStyle )
523 return mProjectStyle->fileName();
525 return mSettings ? mSettings->
styles().at( styleRow )->fileName() : QVariant();
534 if ( index.row() == 0 && mProjectStyle )
535 return mProjectStyle;
536 else if ( mShowDefault && ( ( index.row() == 0 && !mProjectStyle ) || ( index.row() == 1 && mProjectStyle ) ) )
538 else if (
QgsStyle *style = qobject_cast< QgsStyle * >( qvariant_cast<QObject *>(
data( index,
StyleRole ) ) ) )
546 if ( style == mProjectStyle )
547 return index( 0, 0, QModelIndex() );
549 return index( mProjectStyle ? 1 : 0, 0, QModelIndex() );
553 return QModelIndex();
556 const int r = mSettings->
styles().indexOf( style );
558 return QModelIndex();
560 QModelIndex idx = index( r + ( mShowDefault ? 1 : 0 ) + ( mProjectStyle ? 1 : 0 ), 0, QModelIndex() );
566 return QModelIndex();
571 if ( show == mShowDefault )
574 const int row = mProjectStyle ? 1 : 0;
577 beginInsertRows( QModelIndex(), row, row );
583 beginRemoveRows( QModelIndex(), row, row );
584 mShowDefault =
false;
589 void QgsProjectStyleDatabaseModel::setProjectStyle(
QgsStyle *style )
591 if ( style == mProjectStyle )
597 disconnect( mProjectStyle, &QgsStyle::destroyed,
this, &QgsProjectStyleDatabaseModel::projectStyleDestroyed );
598 beginRemoveRows( QModelIndex(), 0, 0 );
599 mProjectStyle =
nullptr;
605 beginInsertRows( QModelIndex(), 0, 0 );
606 mProjectStyle = style;
610 connect( mProjectStyle, &QgsStyle::destroyed,
this, &QgsProjectStyleDatabaseModel::projectStyleDestroyed );
614 void QgsProjectStyleDatabaseModel::styleDatabaseAboutToBeAdded(
const QString & )
616 int row = mSettings->
styles().count() + ( mShowDefault ? 1 : 0 ) + ( mProjectStyle ? 1 : 0 );
617 beginInsertRows( QModelIndex(), row, row );
620 void QgsProjectStyleDatabaseModel::styleDatabaseAboutToBeRemoved(
const QString &path )
623 int row = mSettings->
styles().indexOf( style ) + ( mShowDefault ? 1 : 0 ) + ( mProjectStyle ? 1 : 0 );
625 beginRemoveRows( QModelIndex(), row, row );
628 void QgsProjectStyleDatabaseModel::styleDatabaseAdded(
const QString & )
633 void QgsProjectStyleDatabaseModel::styleDatabaseRemoved(
const QString & )
638 void QgsProjectStyleDatabaseModel::projectStyleAboutToBeDestroyed()
640 beginRemoveRows( QModelIndex(), 0, 0 );
643 void QgsProjectStyleDatabaseModel::projectStyleDestroyed()
648 void QgsProjectStyleDatabaseModel::projectStyleChanged()
658 : QSortFilterProxyModel( parent )
660 setSourceModel( model );
661 setDynamicSortFilter(
true );
668 if (
const QgsStyle *style = qobject_cast< QgsStyle * >( sourceModel()->data( sourceModel()->index( sourceRow, 0, sourceParent ), QgsProjectStyleDatabaseModel::Role::StyleRole ).value< QObject * >() ) )