QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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#include "moc_qgsformannotation.cpp"
23#include "qgsfeature.h"
24#include "qgsfeatureiterator.h"
25#include "qgslogger.h"
26#include "qgsmapcanvas.h"
27#include "qgsproject.h"
28#include "qgsmaptool.h"
29#include "qgsvectorlayer.h"
30#include "qgsgui.h"
31#include "qgsfillsymbol.h"
32
33#include <QDomElement>
34#include <QDir>
35#include <QFile>
36#include <QFileInfo>
37#include <QGraphicsProxyWidget>
38#include <QPainter>
39#include <QSettings>
40#include <QUiLoader>
41#include <QWidget>
42
44 : QgsAnnotation( parent )
45{}
46
48{
49 std::unique_ptr< QgsFormAnnotation > c( new QgsFormAnnotation() );
50 copyCommonProperties( c.get() );
51 c->setDesignerForm( mDesignerForm );
52 return c.release();
53}
54
55void QgsFormAnnotation::setDesignerForm( const QString &uiFile )
56{
57 mDesignerForm = uiFile;
58 mDesignerWidget.reset( createDesignerWidget( uiFile ) );
59 if ( mDesignerWidget )
60 {
61 mMinimumSize = mDesignerWidget->minimumSize();
62 if ( auto *lFillSymbol = fillSymbol() )
63 {
64 QgsFillSymbol *newFill = lFillSymbol->clone();
65 newFill->setColor( mDesignerWidget->palette().color( QPalette::Window ) );
66 setFillSymbol( newFill );
67 }
68 // convert from size in pixels at 96 dpi to mm
69 setFrameSizeMm( preferredFrameSize() / 3.7795275 );
70 }
71 emit appearanceChanged();
72}
73
74QWidget *QgsFormAnnotation::createDesignerWidget( const QString &filePath )
75{
76 QFile file( filePath );
77 if ( !file.open( QFile::ReadOnly ) )
78 {
79 return nullptr;
80 }
81
82 QUiLoader loader;
83 const QFileInfo fi( file );
84 loader.setWorkingDirectory( fi.dir() );
85 QWidget *widget = loader.load( &file, nullptr );
86 file.close();
87
88 //get feature and set attribute information
89 const QgsAttributeEditorContext context;
90 QgsVectorLayer *vectorLayer = qobject_cast< QgsVectorLayer * >( mapLayer() );
91 if ( vectorLayer && associatedFeature().isValid() )
92 {
93 const QgsFields fields = vectorLayer->fields();
95 for ( int i = 0; i < attrs.count(); ++i )
96 {
97 if ( i < fields.count() )
98 {
99 QWidget *attWidget = widget->findChild<QWidget *>( fields.at( i ).name() );
100 if ( attWidget )
101 {
102 QgsEditorWidgetWrapper *eww = QgsGui::editorWidgetRegistry()->create( vectorLayer, i, attWidget, widget, context );
103 if ( eww )
104 {
105 const QStringList additionalFields = eww->additionalFields();
106 QVariantList additionalFieldValues;
107 for ( const QString &additionalField : additionalFields )
108 {
109 const int index = vectorLayer->fields().indexFromName( additionalField );
110 additionalFieldValues.insert( index, attrs.at( index ) );
111 }
112 eww->setValues( attrs.at( i ), additionalFieldValues );
113 }
114 }
115 }
116 }
117 }
118 return widget;
119}
120
121void QgsFormAnnotation::renderAnnotation( QgsRenderContext &context, QSizeF size ) const
122{
123 if ( !mDesignerWidget || !context.painter() || ( context.feedback() && context.feedback()->isCanceled() ) )
124 return;
125
126 // scale painter back to 96 dpi, so that forms look good even in layout prints
127 const QgsScopedQPainterState painterState( context.painter() );
128 const double scaleFactor = context.painter()->device()->logicalDpiX() / 96.0;
129 context.painter()->scale( scaleFactor, scaleFactor );
130 size /= scaleFactor;
131
132 mDesignerWidget->setFixedSize( size.toSize() );
133 context.painter()->setBrush( Qt::NoBrush );
134 context.painter()->setPen( Qt::NoPen );
135 mDesignerWidget->render( context.painter(), QPoint( 0, 0 ) );
136}
137
139{
140 if ( mDesignerWidget )
141 {
142 const QSizeF widgetMinSize = mMinimumSize;
143 return QSizeF( contentsMargin().left() + contentsMargin().right() + widgetMinSize.width(),
144 contentsMargin().top() + contentsMargin().bottom() + widgetMinSize.height() );
145 }
146 else
147 {
148 return QSizeF( 0, 0 );
149 }
150}
151
153{
154 if ( mDesignerWidget )
155 {
156 return mDesignerWidget->sizeHint();
157 }
158 else
159 {
160 return QSizeF( 0, 0 );
161 }
162}
163
164void QgsFormAnnotation::writeXml( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
165{
166 QDomElement formAnnotationElem = doc.createElement( QStringLiteral( "FormAnnotationItem" ) );
167 formAnnotationElem.setAttribute( QStringLiteral( "designerForm" ), mDesignerForm );
168 _writeXml( formAnnotationElem, doc, context );
169 elem.appendChild( formAnnotationElem );
170}
171
172void QgsFormAnnotation::readXml( const QDomElement &itemElem, const QgsReadWriteContext &context )
173{
174 mDesignerForm = itemElem.attribute( QStringLiteral( "designerForm" ), QString() );
175 const QDomElement annotationElem = itemElem.firstChildElement( QStringLiteral( "AnnotationItem" ) );
176 if ( !annotationElem.isNull() )
177 {
178 _readXml( annotationElem, context );
179 }
180 // upgrade old layer
181 if ( !mapLayer() && itemElem.hasAttribute( QStringLiteral( "vectorLayer" ) ) )
182 {
183 setMapLayer( QgsProject::instance()->mapLayer( itemElem.attribute( QStringLiteral( "vectorLayer" ) ) ) );
184 }
185
186 mDesignerWidget.reset( createDesignerWidget( mDesignerForm ) );
187 if ( mDesignerWidget && fillSymbol() )
188 {
189 QgsFillSymbol *newFill = fillSymbol()->clone();
190 newFill->setColor( mDesignerWidget->palette().color( QPalette::Window ) );
191 setFillSymbol( newFill );
192 }
193}
194
196{
198
199 //create new embedded widget
200 mDesignerWidget.reset( createDesignerWidget( mDesignerForm ) );
201 if ( mDesignerWidget && fillSymbol() )
202 {
203 QgsFillSymbol *newFill = fillSymbol()->clone();
204 newFill->setColor( mDesignerWidget->palette().color( QPalette::Window ) );
205 setFillSymbol( newFill );
206 }
207 emit appearanceChanged();
208}
209
210
211
Abstract base class for annotation items which are drawn over a map.
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.
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.
This class contains context information for attribute editor widgets.
A vector of attributes.
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.
Manages an editor widget Widget and wrapper share the same parent.
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:58
QgsAttributes attributes
Definition qgsfeature.h:67
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
QString name
Definition qgsfield.h:62
Container of fields for a vector layer.
Definition qgsfields.h:46
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.
An annotation item that embeds a designer form showing the feature attribute.
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:94
static QgsProject * instance()
Returns the QgsProject singleton instance.
The class is used as a container of context for various read/write operations on other 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.
Represents a vector layer which manages a vector based data sets.
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