QGIS API Documentation 3.39.0-Master (3aed037ce22)
Loading...
Searching...
No Matches
qgsprojectstylesettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprojectstylesettings.cpp
3 ---------------------------
4 begin : May 2022
5 copyright : (C) 2022 by Mathieu Pellerin
6 email : nirvn dot asia at gmail dot 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
16#include "qgscolorutils.h"
18#include "qgis.h"
19#include "qgsproject.h"
20#include "qgssymbol.h"
21#include "qgssymbollayerutils.h"
22#include "qgsmarkersymbol.h"
23#include "qgslinesymbol.h"
24#include "qgsfillsymbol.h"
25#include "qgscolorramp.h"
26#include "qgstextformat.h"
27#include "qgsstyle.h"
29#include "qgsxmlutils.h"
30
31#include <QDomElement>
32
34 : QObject( project )
35 , mProject( project )
36{
37 mCombinedStyleModel = new QgsCombinedStyleModel( this );
38}
39
41{
42 if ( mProjectStyle )
43 {
44 mProjectStyle->deleteLater();
45 mProjectStyle = nullptr;
46 }
47}
48
50{
51 switch ( symbolType )
52 {
54 return mDefaultMarkerSymbol ? mDefaultMarkerSymbol->clone() : nullptr;
55
57 return mDefaultLineSymbol ? mDefaultLineSymbol->clone() : nullptr;
58
60 return mDefaultFillSymbol ? mDefaultFillSymbol->clone() : nullptr;
61
63 break;
64 }
65
66 return nullptr;
67}
68
70{
71 switch ( symbolType )
72 {
74 mDefaultMarkerSymbol.reset( symbol ? symbol->clone() : nullptr );
75 break;
76
78 mDefaultLineSymbol.reset( symbol ? symbol->clone() : nullptr );
79 break;
80
82 mDefaultFillSymbol.reset( symbol ? symbol->clone() : nullptr );
83 break;
84
86 break;
87 }
88}
89
91{
92 return mDefaultColorRamp ? mDefaultColorRamp->clone() : nullptr;
93}
94
96{
97 mDefaultColorRamp.reset( colorRamp ? colorRamp->clone() : nullptr );
98}
99
101{
102 return mDefaultTextFormat;
103}
104
106{
107 mDefaultTextFormat = textFormat;
108}
109
111{
112 mDefaultMarkerSymbol.reset();
113 mDefaultLineSymbol.reset();
114 mDefaultFillSymbol.reset();
115 mDefaultColorRamp.reset();
116 mDefaultTextFormat = QgsTextFormat();
117 mRandomizeDefaultSymbolColor = true;
118 mDefaultSymbolOpacity = 1.0;
119
120 clearStyles();
121
122 if ( mProject && ( mProject->capabilities() & Qgis::ProjectCapability::ProjectStyles ) )
123 {
124 const QString stylePath = mProject->createAttachedFile( QStringLiteral( "styles.db" ) );
125 QgsStyle *style = new QgsStyle();
126 style->createDatabase( stylePath );
127 style->setName( tr( "Project Style" ) );
128 style->setFileName( stylePath );
129 setProjectStyle( style );
130 }
131
133}
134
136{
137 if ( mProjectStyle )
138 {
139 mCombinedStyleModel->removeStyle( mProjectStyle );
140 delete mProjectStyle;
141 mProjectStyle = nullptr;
142 }
143}
144
146{
147 if ( mProjectStyle )
148 {
149 mCombinedStyleModel->removeStyle( mProjectStyle );
150 mProjectStyle->deleteLater();
151 }
152 mProjectStyle = style;
153 mProjectStyle->setName( tr( "Project Styles" ) );
154
155 // if project color scheme changes, we need to redraw symbols - they may use project colors and accordingly
156 // need updating to reflect the new colors
157 if ( mProject )
158 {
159 connect( mProject, &QgsProject::projectColorsChanged, mProjectStyle, &QgsStyle::triggerIconRebuild );
160 }
161 mCombinedStyleModel->addStyle( mProjectStyle );
162
163 emit projectStyleChanged();
164}
165
167{
168 return mProjectStyle;
169}
170
171bool QgsProjectStyleSettings::readXml( const QDomElement &element, const QgsReadWriteContext &context, Qgis::ProjectReadFlags )
172{
173 mRandomizeDefaultSymbolColor = element.attribute( QStringLiteral( "RandomizeDefaultSymbolColor" ), QStringLiteral( "0" ) ).toInt();
174 mDefaultSymbolOpacity = element.attribute( QStringLiteral( "DefaultSymbolOpacity" ), QStringLiteral( "1.0" ) ).toDouble();
175 mColorModel = qgsEnumKeyToValue( element.attribute( QStringLiteral( "colorModel" ) ), Qgis::ColorModel::Rgb );
176
177 QDomElement elem = element.firstChildElement( QStringLiteral( "markerSymbol" ) );
178 if ( !elem.isNull() )
179 {
180 QDomElement symbolElem = elem.firstChildElement( QStringLiteral( "symbol" ) );
181 mDefaultMarkerSymbol.reset( !symbolElem.isNull() ? QgsSymbolLayerUtils::loadSymbol<QgsMarkerSymbol>( symbolElem, context ) : nullptr );
182 }
183 else
184 {
185 mDefaultMarkerSymbol.reset();
186 }
187
188 elem = element.firstChildElement( QStringLiteral( "lineSymbol" ) );
189 if ( !elem.isNull() )
190 {
191 QDomElement symbolElem = elem.firstChildElement( QStringLiteral( "symbol" ) );
192 mDefaultLineSymbol.reset( !symbolElem.isNull() ? QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( symbolElem, context ) : nullptr );
193 }
194 else
195 {
196 mDefaultLineSymbol.reset();
197 }
198
199 elem = element.firstChildElement( QStringLiteral( "fillSymbol" ) );
200 if ( !elem.isNull() )
201 {
202 QDomElement symbolElem = elem.firstChildElement( QStringLiteral( "symbol" ) );
203 mDefaultFillSymbol.reset( !symbolElem.isNull() ? QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElem, context ) : nullptr );
204 }
205 else
206 {
207 mDefaultFillSymbol.reset();
208 }
209
210 elem = element.firstChildElement( QStringLiteral( "colorramp" ) );
211 mDefaultColorRamp.reset( !elem.isNull() ? QgsSymbolLayerUtils::loadColorRamp( elem ) : nullptr );
212
213 elem = element.firstChildElement( QStringLiteral( "text-style" ) );
214 if ( !elem.isNull() )
215 {
216 mDefaultTextFormat.readXml( elem, context );
217 }
218 else
219 {
220 mDefaultTextFormat = QgsTextFormat();
221 }
222
223 {
224 clearStyles();
225 if ( !mProject || ( mProject->capabilities() & Qgis::ProjectCapability::ProjectStyles ) )
226 {
227 const QDomElement styleDatabases = element.firstChildElement( QStringLiteral( "databases" ) );
228 if ( !styleDatabases.isNull() )
229 {
230 const QDomNodeList styleEntries = styleDatabases.childNodes();
231 for ( int i = 0; i < styleEntries.count(); ++i )
232 {
233 const QDomElement styleElement = styleEntries.at( i ).toElement();
234 const QString path = styleElement.attribute( QStringLiteral( "path" ) );
235 const QString fullPath = context.pathResolver().readPath( path );
236 emit styleDatabaseAboutToBeAdded( fullPath );
237 mStyleDatabases.append( fullPath );
238 loadStyleAtPath( fullPath );
239 emit styleDatabaseAdded( fullPath );
240 }
241 }
242
243 if ( mProject && ( mProject->capabilities() & Qgis::ProjectCapability::ProjectStyles ) )
244 {
245 const QString projectStyleId = element.attribute( QStringLiteral( "projectStyleId" ) );
246 const QString projectStyleFile = mProject->resolveAttachmentIdentifier( projectStyleId );
247 QgsStyle *style = new QgsStyle();
248 if ( !projectStyleFile.isEmpty() && QFile::exists( projectStyleFile ) )
249 {
250 style->load( projectStyleFile );
251 style->setFileName( projectStyleFile );
252 }
253 else
254 {
255 const QString stylePath = mProject->createAttachedFile( QStringLiteral( "styles.db" ) );
256 style->createDatabase( stylePath );
257 style->setFileName( stylePath );
258 }
259 style->setName( tr( "Project Style" ) );
260 setProjectStyle( style );
261 }
262 }
263 }
264
265 const QString iccProfileId = element.attribute( QStringLiteral( "iccProfileId" ) );
266 mIccProfileFilePath = mProject ? mProject->resolveAttachmentIdentifier( iccProfileId ) : QString();
267 if ( !mIccProfileFilePath.isEmpty() )
268 {
269 QString errorMsg;
270 QColorSpace colorSpace = QgsColorUtils::iccProfile( mIccProfileFilePath, errorMsg );
271 if ( !errorMsg.isEmpty() )
272 context.pushMessage( errorMsg );
273
275 }
276
278
279 return true;
280}
281
282QDomElement QgsProjectStyleSettings::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
283{
284 QDomElement element = doc.createElement( QStringLiteral( "ProjectStyleSettings" ) );
285
286 element.setAttribute( QStringLiteral( "RandomizeDefaultSymbolColor" ), mRandomizeDefaultSymbolColor ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
287 element.setAttribute( QStringLiteral( "DefaultSymbolOpacity" ), QString::number( mDefaultSymbolOpacity ) );
288
289 element.setAttribute( QStringLiteral( "colorModel" ), qgsEnumValueToKey( mColorModel ) );
290
291 if ( mDefaultMarkerSymbol )
292 {
293 QDomElement markerSymbolElem = doc.createElement( QStringLiteral( "markerSymbol" ) );
294 markerSymbolElem.appendChild( QgsSymbolLayerUtils::saveSymbol( QString(), mDefaultMarkerSymbol.get(), doc, context ) );
295 element.appendChild( markerSymbolElem );
296 }
297
298 if ( mDefaultLineSymbol )
299 {
300 QDomElement lineSymbolElem = doc.createElement( QStringLiteral( "lineSymbol" ) );
301 lineSymbolElem.appendChild( QgsSymbolLayerUtils::saveSymbol( QString(), mDefaultLineSymbol.get(), doc, context ) );
302 element.appendChild( lineSymbolElem );
303 }
304
305 if ( mDefaultFillSymbol )
306 {
307 QDomElement fillSymbolElem = doc.createElement( QStringLiteral( "fillSymbol" ) );
308 fillSymbolElem.appendChild( QgsSymbolLayerUtils::saveSymbol( QString(), mDefaultFillSymbol.get(), doc, context ) );
309 element.appendChild( fillSymbolElem );
310 }
311
312 if ( mDefaultColorRamp )
313 {
314 QDomElement colorRampElem = QgsSymbolLayerUtils::saveColorRamp( QString(), mDefaultColorRamp.get(), doc );
315 element.appendChild( colorRampElem );
316 }
317
318 if ( mDefaultTextFormat.isValid() )
319 {
320 QDomElement textFormatElem = mDefaultTextFormat.writeXml( doc, context );
321 element.appendChild( textFormatElem );
322 }
323
324 {
325 QDomElement styleDatabases = doc.createElement( QStringLiteral( "databases" ) );
326 for ( const QString &db : mStyleDatabases )
327 {
328 QDomElement dbElement = doc.createElement( QStringLiteral( "db" ) );
329 dbElement.setAttribute( QStringLiteral( "path" ), context.pathResolver().writePath( db ) );
330 styleDatabases.appendChild( dbElement );
331 }
332 element.appendChild( styleDatabases );
333 }
334
335 if ( mProject && mProjectStyle )
336 {
337 element.setAttribute( QStringLiteral( "projectStyleId" ), mProject->attachmentIdentifier( mProjectStyle->fileName() ) );
338 }
339
340 if ( mProject )
341 {
342 element.setAttribute( QStringLiteral( "iccProfileId" ), mProject->attachmentIdentifier( mIccProfileFilePath ) );
343 }
344
345 return element;
346}
347
348QList<QgsStyle *> QgsProjectStyleSettings::styles() const
349{
350 QList< QgsStyle * > res;
351 res.reserve( mStyles.size() );
352 for ( QgsStyle *style : mStyles )
353 {
354 if ( style )
355 res.append( style );
356 }
357 return res;
358}
359
361{
362 if ( path == QgsStyle::defaultStyle()->fileName() )
363 return QgsStyle::defaultStyle();
364
365 if ( mProjectStyle && path == mProjectStyle->fileName() )
366 return mProjectStyle;
367
368 for ( QgsStyle *style : std::as_const( mStyles ) )
369 {
370 if ( style->fileName() == path )
371 return style;
372 }
373
374 return nullptr;
375}
376
378{
379 if ( mStyleDatabases.contains( path ) )
380 return;
381
382 emit styleDatabaseAboutToBeAdded( path );
383 mStyleDatabases.append( path );
384 loadStyleAtPath( path );
385 emit styleDatabaseAdded( path );
386
388}
389
391{
392 if ( paths == mStyleDatabases )
393 return;
394
395 clearStyles();
396
397 for ( const QString &path : paths )
398 {
399 emit styleDatabaseAboutToBeAdded( path );
400 mStyleDatabases.append( path );
401 loadStyleAtPath( path );
402 emit styleDatabaseAdded( path );
403 }
405}
406
407void QgsProjectStyleSettings::loadStyleAtPath( const QString &path )
408{
409 QgsStyle *style = new QgsStyle( this );
410
411 const QFileInfo fileInfo( path );
412 if ( fileInfo.suffix().compare( QLatin1String( "xml" ), Qt::CaseInsensitive ) == 0 )
413 {
414 style->createMemoryDatabase();
415 style->importXml( path );
416 style->setFileName( path );
417 style->setReadOnly( true );
418 }
419 else
420 {
421 style->load( path );
422 }
423 style->setName( fileInfo.completeBaseName() );
424 mStyles.append( style );
425 mCombinedStyleModel->addStyle( style );
426
427 if ( mProject )
428 {
429 // if project color scheme changes, we need to redraw symbols - they may use project colors and accordingly
430 // need updating to reflect the new colors
432 }
433}
434
435void QgsProjectStyleSettings::clearStyles()
436{
437 const QStringList pathsToRemove = mStyleDatabases;
438 for ( const QString &path : pathsToRemove )
439 {
441 mStyleDatabases.removeAll( path );
442 if ( QgsStyle *style = styleAtPath( path ) )
443 {
444 mCombinedStyleModel->removeStyle( style );
445 style->deleteLater();
446 mStyles.removeAll( style );
447 }
448 emit styleDatabaseRemoved( path );
449 }
450
451 // should already be empty, but play it safe..!
452 for ( QgsStyle *style : std::as_const( mStyles ) )
453 {
454 mCombinedStyleModel->removeStyle( style );
455 }
456 qDeleteAll( mStyles );
457 mStyles.clear();
458}
459
461{
462 return mCombinedStyleModel;
463}
464
466{
467 mColorModel = colorModel;
468#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
469 if ( mColorSpace.isValid() && QgsColorUtils::toColorModel( mColorSpace.colorModel() ) != colorModel )
470 {
471 setColorSpace( QColorSpace() );
472 }
473#endif
474}
475
477{
478 return mColorModel;
479}
480
481void QgsProjectStyleSettings::setColorSpace( const QColorSpace &colorSpace )
482{
483 if ( !mProject )
484 {
485 QgsDebugError( "Impossible to attach ICC profile, no project defined" );
486 return;
487 }
488
489 auto clearIccProfile = [this]()
490 {
491 mProject->removeAttachedFile( mIccProfileFilePath );
492 mIccProfileFilePath.clear();
493 mColorSpace = QColorSpace();
494 };
495
496 if ( !mIccProfileFilePath.isEmpty() )
497 clearIccProfile();
498
499#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
500 bool ok;
502 mColorSpace = ok ? colorSpace : QColorSpace();
503#else
504 mColorSpace = colorSpace;
505#endif
506
507 if ( !mColorSpace.isValid() )
508 return;
509
510#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
511 if ( colorModel != mColorModel )
512 mColorModel = colorModel;
513#endif
514
515 mIccProfileFilePath = mProject->createAttachedFile( QStringLiteral( "profile.icc" ) );
516 QFile file( mIccProfileFilePath );
517 if ( !file.open( QIODevice::WriteOnly ) || file.write( colorSpace.iccProfile() ) < 0 )
518 clearIccProfile();
519}
520
522{
523 return mColorSpace;
524}
525
526
527//
528// QgsProjectStyleDatabaseModel
529//
530
532 : QAbstractListModel( parent )
533 , mSettings( settings )
534{
535 connect( mSettings, &QgsProjectStyleSettings::styleDatabaseAboutToBeAdded, this, &QgsProjectStyleDatabaseModel::styleDatabaseAboutToBeAdded );
536 connect( mSettings, &QgsProjectStyleSettings::styleDatabaseAdded, this, &QgsProjectStyleDatabaseModel::styleDatabaseAdded );
537 connect( mSettings, &QgsProjectStyleSettings::styleDatabaseAboutToBeRemoved, this, &QgsProjectStyleDatabaseModel::styleDatabaseAboutToBeRemoved );
538 connect( mSettings, &QgsProjectStyleSettings::styleDatabaseRemoved, this, &QgsProjectStyleDatabaseModel::styleDatabaseRemoved );
539
540 if ( mSettings->projectStyle() )
541 setProjectStyle( mSettings->projectStyle() );
542 connect( mSettings, &QgsProjectStyleSettings::projectStyleChanged, this, &QgsProjectStyleDatabaseModel::projectStyleChanged );
543}
544
545int QgsProjectStyleDatabaseModel::rowCount( const QModelIndex &parent ) const
546{
547 Q_UNUSED( parent )
548 return ( mSettings ? mSettings->styleDatabasePaths().count() : 0 ) + ( mProjectStyle ? 1 : 0 ) + ( mShowDefault ? 1 : 0 );
549}
550
551QVariant QgsProjectStyleDatabaseModel::data( const QModelIndex &index, int role ) const
552{
553 if ( index.row() < 0 || index.row() >= rowCount( QModelIndex() ) )
554 return QVariant();
555
556 const bool isProjectStyle = index.row() == 0 && mProjectStyle;
557 const bool isDefault = mShowDefault && ( ( index.row() == 0 && !mProjectStyle ) || ( index.row() == 1 && mProjectStyle ) );
558 const int styleRow = index.row() - ( mShowDefault ? 1 : 0 ) - ( mProjectStyle ? 1 : 0 );
559
560 switch ( role )
561 {
562 case Qt::DisplayRole:
563 case Qt::EditRole:
564 if ( isDefault )
565 return QgsStyle::defaultStyle()->name();
566 else if ( isProjectStyle )
567 return mProjectStyle->name();
568 else
569 return mSettings ? mSettings->styles().at( styleRow )->name() : QVariant();
570
571 case Qt::ToolTipRole:
572 if ( isDefault )
573 return QDir::toNativeSeparators( QgsStyle::defaultStyle()->fileName() );
574 else if ( isProjectStyle )
575 return mProjectStyle->name();
576 else
577 return mSettings ? QDir::toNativeSeparators( mSettings->styles().at( styleRow )->fileName() ) : QVariant();
578
579 case static_cast< int >( CustomRole::Style ):
580 {
581 if ( isDefault )
582 return QVariant::fromValue( QgsStyle::defaultStyle() );
583 else if ( isProjectStyle )
584 return QVariant::fromValue( mProjectStyle.data() );
585 else if ( QgsStyle *style = mSettings->styles().value( styleRow ) )
586 return QVariant::fromValue( style );
587 else
588 return QVariant();
589 }
590
591 case static_cast< int >( CustomRole::Path ):
592 if ( isDefault )
594 else if ( isProjectStyle )
595 return mProjectStyle->fileName();
596 else
597 return mSettings ? mSettings->styles().at( styleRow )->fileName() : QVariant();
598
599 default:
600 return QVariant();
601 }
602}
603
605{
606 if ( index.row() == 0 && mProjectStyle )
607 return mProjectStyle;
608 else if ( mShowDefault && ( ( index.row() == 0 && !mProjectStyle ) || ( index.row() == 1 && mProjectStyle ) ) )
609 return QgsStyle::defaultStyle();
610 else if ( QgsStyle *style = qobject_cast< QgsStyle * >( qvariant_cast<QObject *>( data( index, static_cast< int >( CustomRole::Style ) ) ) ) )
611 return style;
612 else
613 return nullptr;
614}
615
617{
618 if ( style == mProjectStyle )
619 return index( 0, 0, QModelIndex() );
620 else if ( style == QgsStyle::defaultStyle() && mShowDefault )
621 return index( mProjectStyle ? 1 : 0, 0, QModelIndex() );
622
623 if ( !mSettings )
624 {
625 return QModelIndex();
626 }
627
628 const int r = mSettings->styles().indexOf( style );
629 if ( r < 0 )
630 return QModelIndex();
631
632 QModelIndex idx = index( r + ( mShowDefault ? 1 : 0 ) + ( mProjectStyle ? 1 : 0 ), 0, QModelIndex() );
633 if ( idx.isValid() )
634 {
635 return idx;
636 }
637
638 return QModelIndex();
639}
640
642{
643 if ( show == mShowDefault )
644 return;
645
646 const int row = mProjectStyle ? 1 : 0;
647 if ( show )
648 {
649 beginInsertRows( QModelIndex(), row, row );
650 mShowDefault = true;
651 endInsertRows();
652 }
653 else
654 {
655 beginRemoveRows( QModelIndex(), row, row );
656 mShowDefault = false;
657 endRemoveRows();
658 }
659}
660
661void QgsProjectStyleDatabaseModel::setProjectStyle( QgsStyle *style )
662{
663 if ( style == mProjectStyle )
664 return;
665
666 if ( mProjectStyle )
667 {
668 disconnect( mProjectStyle, &QgsStyle::aboutToBeDestroyed, this, &QgsProjectStyleDatabaseModel::projectStyleAboutToBeDestroyed );
669 disconnect( mProjectStyle, &QgsStyle::destroyed, this, &QgsProjectStyleDatabaseModel::projectStyleDestroyed );
670 beginRemoveRows( QModelIndex(), 0, 0 );
671 mProjectStyle = nullptr;
672 endRemoveRows();
673 }
674
675 if ( style )
676 {
677 beginInsertRows( QModelIndex(), 0, 0 );
678 mProjectStyle = style;
679 endInsertRows();
680
681 connect( mProjectStyle, &QgsStyle::aboutToBeDestroyed, this, &QgsProjectStyleDatabaseModel::projectStyleAboutToBeDestroyed );
682 connect( mProjectStyle, &QgsStyle::destroyed, this, &QgsProjectStyleDatabaseModel::projectStyleDestroyed );
683 }
684}
685
686void QgsProjectStyleDatabaseModel::styleDatabaseAboutToBeAdded( const QString & )
687{
688 int row = mSettings->styles().count() + ( mShowDefault ? 1 : 0 ) + ( mProjectStyle ? 1 : 0 );
689 beginInsertRows( QModelIndex(), row, row );
690}
691
692void QgsProjectStyleDatabaseModel::styleDatabaseAboutToBeRemoved( const QString &path )
693{
694 QgsStyle *style = mSettings->styleAtPath( path );
695 int row = mSettings->styles().indexOf( style ) + ( mShowDefault ? 1 : 0 ) + ( mProjectStyle ? 1 : 0 );
696 if ( row >= 0 )
697 beginRemoveRows( QModelIndex(), row, row );
698}
699
700void QgsProjectStyleDatabaseModel::styleDatabaseAdded( const QString & )
701{
702 endInsertRows();
703}
704
705void QgsProjectStyleDatabaseModel::styleDatabaseRemoved( const QString & )
706{
707 endRemoveRows();
708}
709
710void QgsProjectStyleDatabaseModel::projectStyleAboutToBeDestroyed()
711{
712 beginRemoveRows( QModelIndex(), 0, 0 );
713}
714
715void QgsProjectStyleDatabaseModel::projectStyleDestroyed()
716{
717 endRemoveRows();
718}
719
720void QgsProjectStyleDatabaseModel::projectStyleChanged()
721{
722 setProjectStyle( mSettings->projectStyle() );
723}
724
725//
726// QgsProjectStyleDatabaseProxyModel
727//
728
730 : QSortFilterProxyModel( parent )
731{
732 setSourceModel( model );
733 setDynamicSortFilter( true );
734}
735
736bool QgsProjectStyleDatabaseProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
737{
738 if ( mFilters & Filter::FilterHideReadOnly )
739 {
740 if ( const QgsStyle *style = qobject_cast< QgsStyle * >( sourceModel()->data( sourceModel()->index( sourceRow, 0, sourceParent ), static_cast< int >( QgsProjectStyleDatabaseModel::CustomRole::Style ) ).value< QObject * >() ) )
741 {
742 if ( style->isReadOnly() )
743 return false;
744 }
745 }
746
747 return true;
748}
749
754
756{
757 mFilters = filters;
758 invalidateFilter();
759}
QFlags< ProjectReadFlag > ProjectReadFlags
Project load flags.
Definition qgis.h:3833
@ ProjectStyles
Enable the project embedded style library. Enabling this flag can increase the time required to clear...
SymbolType
Symbol types.
Definition qgis.h:500
@ Marker
Marker symbol.
@ Line
Line symbol.
@ Fill
Fill symbol.
@ Hybrid
Hybrid symbol.
ColorModel
Color model types.
Definition qgis.h:5311
@ Rgb
RGB color model.
Abstract base class for color ramps.
virtual QgsColorRamp * clone() const =0
Creates a clone of the color ramp.
static Qgis::ColorModel toColorModel(QColorSpace::ColorModel colorModel, bool *ok=nullptr)
Convert and returns Qt colorModel to Qgis::ColorModel.
static QColorSpace iccProfile(const QString &iccProfileFilePath, QString &errorMsg)
Loads an ICC profile from iccProfileFilePath and returns associated color space.
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.
QColorSpace colorSpace() const
Returns the project's color space.
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.
void setProjectStyle(QgsStyle *style)
Sets the style database to use for the project style.
Qgis::ColorModel colorModel() const
Returns the project's color model.
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 setColorModel(Qgis::ColorModel colorModel)
Set the project's color model to colorModel.
void setColorSpace(const QColorSpace &colorSpace)
Set the project's current color space to colorSpace.
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,...
Definition qgsproject.h:107
bool removeAttachedFile(const QString &path)
Removes the attached file.
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...
Definition qgsproject.h:198
The class is used as a container of context for various read/write operations on other objects.
void pushMessage(const QString &message, Qgis::MessageLevel level=Qgis::MessageLevel::Warning) const
Append a message to the context.
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.
Definition qgsstyle.cpp:893
void aboutToBeDestroyed()
Emitted just before the style object is destroyed.
bool createDatabase(const QString &filename)
Creates an on-disk database.
Definition qgsstyle.cpp:548
void triggerIconRebuild()
Triggers emission of the rebuildIconPreviews() signal.
void setName(const QString &name)
Sets the name of the style.
Definition qgsstyle.cpp:101
QString fileName() const
Returns the current file name of the style database.
Definition qgsstyle.h:905
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.
Definition qgsstyle.cpp:145
bool createMemoryDatabase()
Creates a temporary memory database.
Definition qgsstyle.cpp:563
bool load(const QString &filename)
Loads a file into the style.
Definition qgsstyle.cpp:638
QString name() const
Returns the name of the style.
Definition qgsstyle.cpp:106
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.
Definition qgssymbol.h:94
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.
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
Definition qgis.h:5862
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:5843
#define QgsDebugError(str)
Definition qgslogger.h:38