QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgsreadwritecontext.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsreadwritecontext.cpp
3  ---------------------
4  begin : May 2017
5  copyright : (C) 2017 by Martin Dobias
6  email : wonder dot sk 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 #include "qgsreadwritecontext.h"
16 
18 class DefaultTranslator : public QgsProjectTranslator
19 {
20  // QgsProjectTranslator interface
21  public:
22  QString translate( const QString &context, const QString &sourceText, const char *disambiguation, int n ) const override
23  {
24  Q_UNUSED( context )
25  Q_UNUSED( disambiguation )
26  Q_UNUSED( n )
27  return sourceText;
28  }
29 };
30 
32 
33 Q_GLOBAL_STATIC( DefaultTranslator, sDefaultTranslator )
34 
36  : mProjectTranslator( sDefaultTranslator() )
37 {
38 
39 }
40 
42 {
43  // be sure that categories have been emptied
44  Q_ASSERT( mCategories.isEmpty() );
45 }
46 
48 {
49  return mPathResolver;
50 }
51 
53 {
54  mPathResolver = resolver;
55 }
56 
57 void QgsReadWriteContext::pushMessage( const QString &message, Qgis::MessageLevel level ) const
58 {
59  mMessages.append( ReadWriteMessage( message, level, mCategories ) );
60 }
61 
62 QgsReadWriteContextCategoryPopper QgsReadWriteContext::enterCategory( const QString &category, const QString &details ) const
63 {
64  QString message = category;
65  if ( !details.isEmpty() )
66  message.append( QStringLiteral( " :: %1" ).arg( details ) );
67  mCategories.push_back( message );
68  return QgsReadWriteContextCategoryPopper( *this );
69 }
70 
71 void QgsReadWriteContext::leaveCategory() const
72 {
73  if ( !mCategories.isEmpty() )
74  mCategories.pop_back();
75 }
76 
78 {
79  return mCoordinateTransformContext;
80 }
81 
83 {
84  mCoordinateTransformContext = transformContext;
85 }
86 
88 {
89  mProjectTranslator = projectTranslator;
90 }
91 
92 
93 QList<QgsReadWriteContext::ReadWriteMessage > QgsReadWriteContext::takeMessages()
94 {
95  QList<QgsReadWriteContext::ReadWriteMessage > messages = mMessages;
96  mMessages.clear();
97  return messages;
98 }
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:107
Contains information about the context in which a coordinate transform is executed.
Resolves relative paths into absolute paths and vice versa.
Wherever an object of this class is available, the derived translate function can be called from.
virtual QString translate(const QString &context, const QString &sourceText, const char *disambiguation=nullptr, int n=-1) const =0
The derived translate() translates with QTranslator and qm file the sourceText.
Allows entering a context category and takes care of leaving this category on deletion of the class.
The class is used as a container of context for various read/write operations on other objects.
MAYBE_UNUSED NODISCARD QgsReadWriteContextCategoryPopper enterCategory(const QString &category, const QString &details=QString()) const
Push a category to the stack.
void setTransformContext(const QgsCoordinateTransformContext &transformContext)
Sets data coordinate transform context to transformContext.
const QgsProjectTranslator * projectTranslator() const
Returns the project translator.
QgsCoordinateTransformContext transformContext() const
Returns data provider coordinate transform context.
QList< QgsReadWriteContext::ReadWriteMessage > takeMessages()
Returns the stored messages and remove them.
friend class QgsReadWriteContextCategoryPopper
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 setProjectTranslator(QgsProjectTranslator *projectTranslator)
Sets the project translator.
void setPathResolver(const QgsPathResolver &resolver)
Sets up path resolver for conversion between relative and absolute paths.
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)
Struct for QgsReadWriteContext error or warning messages.