QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgsreadwritecontext.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsreadwritecontext.h
3  ----------------------
4  begin : May 2017
5  copyright : (C) 2017 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSREADWRITECONTEXT_H
19 #define QGSREADWRITECONTEXT_H
20 
21 #include "qgspathresolver.h"
22 #include "qgis.h"
23 #include "qgsprojecttranslator.h"
24 
26 
33 class CORE_EXPORT QgsReadWriteContext
34 {
35  public:
36 
42  {
44  ReadWriteMessage( const QString &message, Qgis::MessageLevel level = Qgis::Warning, const QStringList &categories = QStringList() )
45  : mMessage( message )
46  , mLevel( level )
47  , mCategories( categories )
48  {}
49 
51  QString message() const {return mMessage;}
52 
54  Qgis::MessageLevel level() const {return mLevel;}
55 
57  QStringList categories() const {return mCategories;}
58 
59  private:
60  QString mMessage;
61  Qgis::MessageLevel mLevel;
62  QStringList mCategories;
63  };
64 
69 
71 
73  const QgsPathResolver &pathResolver() const;
74 
76  void setPathResolver( const QgsPathResolver &resolver );
77 
82  void pushMessage( const QString &message, Qgis::MessageLevel level = Qgis::Warning );
83 
95  MAYBE_UNUSED NODISCARD QgsReadWriteContextCategoryPopper enterCategory( const QString &category, const QString &details = QString() ) SIP_PYNAME( _enterCategory );
96 
101  QList<QgsReadWriteContext::ReadWriteMessage> takeMessages();
102 
107  const QgsProjectTranslator *projectTranslator( ) const { return mProjectTranslator; }
108 
115  void setProjectTranslator( QgsProjectTranslator *projectTranslator );
116 
117  private:
118 
119  class DefaultTranslator : public QgsProjectTranslator
120  {
121  // QgsProjectTranslator interface
122  public:
123  QString translate( const QString &context, const QString &sourceText, const char *disambiguation, int n ) const;
124  };
125 
127  void leaveCategory();
128 
129  QgsPathResolver mPathResolver;
130  QList<ReadWriteMessage> mMessages;
131  QStringList mCategories = QStringList();
132  QgsProjectTranslator *mProjectTranslator = nullptr;
134  DefaultTranslator mDefaultTranslator;
135 };
136 
137 
147 {
148  public:
150  QgsReadWriteContextCategoryPopper( QgsReadWriteContext &context ) : mContext( context ) {}
151  ~QgsReadWriteContextCategoryPopper() {mContext.leaveCategory();}
152  private:
153  QgsReadWriteContext &mContext;
154 };
155 
156 #endif // QGSREADWRITECONTEXT_H
The class is used as a container of context for various read/write operations on other objects...
#define NODISCARD
Definition: qgis.h:665
ReadWriteMessage(const QString &message, Qgis::MessageLevel level=Qgis::Warning, const QStringList &categories=QStringList())
Construct a container for QgsReadWriteContext error or warning messages.
Allows entering a context category and takes care of leaving this category on deletion of the class...
Qgis::MessageLevel level() const
Returns the message level.
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:79
QStringList categories() const
Returns the stack of categories of the message.
QString message() const
Returns the message string.
Struct for QgsReadWriteContext error or warning messages.
const QgsProjectTranslator * projectTranslator() const
Returns the project translator.
Wherever an object of this class is available, the derived translate function can be called from...
QgsReadWriteContextCategoryPopper(QgsReadWriteContext &context)
Creates a popper.
#define MAYBE_UNUSED
Definition: qgis.h:681
Resolves relative paths into absolute paths and vice versa.
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74