QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsformannotation.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsformannotation.cpp
3 ------------------------
4 begin : February 26, 2010
5 copyright : (C) 2010 by Marco Hugentobler
6 email : marco dot hugentobler at hugis dot net
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#include "qgsformannotation.h"
19
23#include "qgsfeature.h"
24#include "qgsfeatureiterator.h"
25#include "qgsfillsymbol.h"
26#include "qgsgui.h"
27#include "qgslogger.h"
28#include "qgsmapcanvas.h"
29#include "qgsmaptool.h"
30#include "qgsproject.h"
31#include "qgsvectorlayer.h"
32
33#include <QDir>
34#include <QDomElement>
35#include <QFile>
36#include <QFileInfo>
37#include <QGraphicsProxyWidget>
38#include <QPainter>
39#include <QSettings>
40#include <QString>
41#include <QUiLoader>
42#include <QWidget>
43
44#include "moc_qgsformannotation.cpp"
45
46using namespace Qt::StringLiterals;
47
49 : QgsAnnotation( parent )
50{}
51
53{
54 auto c = std::make_unique<QgsFormAnnotation>();
55 copyCommonProperties( c.get() );
56 c->setDesignerForm( mDesignerForm );
57 return c.release();
58}
59
60void QgsFormAnnotation::setDesignerForm( const QString &uiFile )
61{
62 mDesignerForm = uiFile;
63 mDesignerWidget.reset( createDesignerWidget( uiFile ) );
64 if ( mDesignerWidget )
65 {
66 mMinimumSize = mDesignerWidget->minimumSize();
67 if ( auto *lFillSymbol = fillSymbol() )
68 {
69 QgsFillSymbol *newFill = lFillSymbol->clone();
70 newFill->setColor( mDesignerWidget->palette().color( QPalette::Window ) );
71 setFillSymbol( newFill );
72 }
73 // convert from size in pixels at 96 dpi to mm
74 setFrameSizeMm( preferredFrameSize() / 3.7795275 );
75 }
76 emit appearanceChanged();
77}
78
79QWidget *QgsFormAnnotation::createDesignerWidget( const QString &filePath )
80{
81 QFile file( filePath );
82 if ( !file.open( QFile::ReadOnly ) )
83 {
84 return nullptr;
85 }
86
87 QUiLoader loader;
88 const QFileInfo fi( file );
89 loader.setWorkingDirectory( fi.dir() );
90 QWidget *widget = loader.load( &file, nullptr );
91 file.close();
92
93 //get feature and set attribute information
94 const QgsAttributeEditorContext context;
95 QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( mapLayer() );
96 if ( vectorLayer && associatedFeature().isValid() )
97 {
98 const QgsFields fields = vectorLayer->fields();
99 const QgsAttributes attrs = associatedFeature().attributes();
100 for ( int i = 0; i < attrs.count(); ++i )
101 {
102 if ( i < fields.count() )
103 {
104 QWidget *attWidget = widget->findChild<QWidget *>( fields.at( i ).name() );
105 if ( attWidget )
106 {
107 QgsEditorWidgetWrapper *eww = QgsGui::editorWidgetRegistry()->create( vectorLayer, i, attWidget, widget, context );
108 if ( eww )
109 {
110 const QStringList additionalFields = eww->additionalFields();
111 QVariantList additionalFieldValues;
112 for ( const QString &additionalField : additionalFields )
113 {
114 const int index = vectorLayer->fields().indexFromName( additionalField );
115 additionalFieldValues.insert( index, attrs.at( index ) );
116 }
117 eww->setValues( attrs.at( i ), additionalFieldValues );
118 }
119 }
120 }
121 }
122 }
123 return widget;
124}
125
126void QgsFormAnnotation::renderAnnotation( QgsRenderContext &context, QSizeF size ) const
127{
128 if ( !mDesignerWidget || !context.painter() || ( context.feedback() && context.feedback()->isCanceled() ) )
129 return;
130
131 // scale painter back to 96 dpi, so that forms look good even in layout prints
132 const QgsScopedQPainterState painterState( context.painter() );
133 const double scaleFactor = context.painter()->device()->logicalDpiX() / 96.0;
134 context.painter()->scale( scaleFactor, scaleFactor );
135 size /= scaleFactor;
136
137 mDesignerWidget->setFixedSize( size.toSize() );
138 context.painter()->setBrush( Qt::NoBrush );
139 context.painter()->setPen( Qt::NoPen );
140 mDesignerWidget->render( context.painter(), QPoint( 0, 0 ) );
141}
142
144{
145 if ( mDesignerWidget )
146 {
147 const QSizeF widgetMinSize = mMinimumSize;
148 return QSizeF( contentsMargin().left() + contentsMargin().right() + widgetMinSize.width(), contentsMargin().top() + contentsMargin().bottom() + widgetMinSize.height() );
149 }
150 else
151 {
152 return QSizeF( 0, 0 );
153 }
154}
155
157{
158 if ( mDesignerWidget )
159 {
160 return mDesignerWidget->sizeHint();
161 }
162 else
163 {
164 return QSizeF( 0, 0 );
165 }
166}
167
168void QgsFormAnnotation::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
169{
170 QDomElement formAnnotationElem = doc.createElement( u"FormAnnotationItem"_s );
171 formAnnotationElem.setAttribute( u"designerForm"_s, mDesignerForm );
172 _writeXml( formAnnotationElem, doc, context );
173 elem.appendChild( formAnnotationElem );
174}
175
176void QgsFormAnnotation::readXml( const QDomElement &itemElem, const QgsReadWriteContext &context )
177{
178 mDesignerForm = itemElem.attribute( u"designerForm"_s, QString() );
179 const QDomElement annotationElem = itemElem.firstChildElement( u"AnnotationItem"_s );
180 if ( !annotationElem.isNull() )
181 {
182 _readXml( annotationElem, context );
183 }
184 // upgrade old layer
185 if ( !mapLayer() && itemElem.hasAttribute( u"vectorLayer"_s ) )
186 {
187 setMapLayer( QgsProject::instance()->mapLayer( itemElem.attribute( u"vectorLayer"_s ) ) );
188 }
189
190 mDesignerWidget.reset( createDesignerWidget( mDesignerForm ) );
191 if ( mDesignerWidget && fillSymbol() )
192 {
193 QgsFillSymbol *newFill = fillSymbol()->clone();
194 newFill->setColor( mDesignerWidget->palette().color( QPalette::Window ) );
195 setFillSymbol( newFill );
196 }
197}
198
200{
202
203 //create new embedded widget
204 mDesignerWidget.reset( createDesignerWidget( mDesignerForm ) );
205 if ( mDesignerWidget && fillSymbol() )
206 {
207 QgsFillSymbol *newFill = fillSymbol()->clone();
208 newFill->setColor( mDesignerWidget->palette().color( QPalette::Window ) );
209 setFillSymbol( newFill );
210 }
211 emit appearanceChanged();
212}
void appearanceChanged()
Emitted whenever the annotation's appearance changes.
void setFillSymbol(QgsFillSymbol *symbol)
Sets the fill symbol used for rendering the annotation frame.
QgsMargins contentsMargin() const
Returns the margins (in millimeters) between the outside of the frame and the annotation content.
void _writeXml(QDomElement &itemElem, QDomDocument &doc, const QgsReadWriteContext &context) const
Writes common annotation properties to a DOM element.
QgsMapLayer * mapLayer() const
Returns the map layer associated with the annotation.
void setFrameSizeMm(QSizeF size)
Sets the size (in millimeters) of the annotation's frame (the main area in which the annotation's con...
virtual void setAssociatedFeature(const QgsFeature &feature)
Sets the feature associated with the annotation.
QgsFeature associatedFeature() const
Returns the feature associated with the annotation, or an invalid feature if none has been set.
void _readXml(const QDomElement &annotationElem, const QgsReadWriteContext &context)
Reads common annotation properties from a DOM element.
void copyCommonProperties(QgsAnnotation *target) const
Copies common annotation properties to the targe annotation.
QgsAnnotation(QObject *parent=nullptr)
Constructor for QgsAnnotation.
QgsFillSymbol * fillSymbol() const
Returns the symbol that is used for rendering the annotation frame.
void setMapLayer(QgsMapLayer *layer)
Sets the map layer associated with the annotation.
QgsEditorWidgetWrapper * create(const QString &widgetId, QgsVectorLayer *vl, int fieldIdx, const QVariantMap &config, QWidget *editor, QWidget *parent, const QgsAttributeEditorContext &context=QgsAttributeEditorContext())
Create an attribute editor widget wrapper of a given type for a given field.
virtual QStringList additionalFields() const
Returns the list of additional fields which the editor handles.
void setValues(const QVariant &value, const QVariantList &additionalValues)
Is called when the value of the widget or additional field values needs to be changed.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsAttributes attributes
Definition qgsfeature.h:69
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:55
int count
Definition qgsfields.h:50
Q_INVOKABLE int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
QgsFillSymbol * clone() const override
Returns a deep copy of this symbol.
QgsFormAnnotation * clone() const override
Clones the annotation, returning a new copy of the annotation reflecting the annotation's current sta...
void renderAnnotation(QgsRenderContext &context, QSizeF size) const override
Renders the annotation's contents to a target /a context at the specified /a size.
QgsFormAnnotation(QObject *parent=nullptr)
Constructor for QgsFormAnnotation.
void setAssociatedFeature(const QgsFeature &feature) override
Sets the feature associated with the annotation.
void writeXml(QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context) const override
Writes the annotation state to a DOM element.
void readXml(const QDomElement &itemElem, const QgsReadWriteContext &context) override
Restores the annotation's state from a DOM element.
QSizeF minimumFrameSize() const override
Returns the minimum frame size for the annotation.
void setDesignerForm(const QString &uiFile)
Sets the path to the Qt Designer UI file to show in the annotation.
QSizeF preferredFrameSize() const
Returns the optimal frame size.
static QgsEditorWidgetRegistry * editorWidgetRegistry()
Returns the global editor widget registry, used for managing all known edit widget factories.
Definition qgsgui.cpp:109
static QgsProject * instance()
Returns the QgsProject singleton instance.
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
QgsFeedback * feedback() const
Returns the feedback object that can be queried regularly during rendering to check if rendering shou...
Scoped object for saving and restoring a QPainter object's state.
void setColor(const QColor &color) const
Sets the color for the symbol.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c