26 #include <QDomElement> 30 #include <QGraphicsProxyWidget> 37 :
QgsAnnotationItem( canvas ), mWidgetContainer( 0 ), mDesignerWidget( 0 ), mVectorLayer( vlayer ),
38 mHasAssociatedFeature( hasFeature ), mFeature( feature )
40 mWidgetContainer =
new QGraphicsProxyWidget(
this );
41 mWidgetContainer->setData( 0,
"AnnotationItem" );
45 QObject::connect( mVectorLayer, SIGNAL( layerModified() ),
this, SLOT( setFeatureForMapPosition() ) );
46 QObject::connect(
mMapCanvas, SIGNAL( renderComplete( QPainter* ) ),
this, SLOT( setFeatureForMapPosition() ) );
47 QObject::connect(
mMapCanvas, SIGNAL( layersChanged() ),
this, SLOT( updateVisibility() ) );
50 setFeatureForMapPosition();
55 delete mDesignerWidget;
60 mDesignerForm = uiFile;
61 mWidgetContainer->setWidget( 0 );
62 delete mDesignerWidget;
63 mDesignerWidget = createDesignerWidget( uiFile );
64 if ( mDesignerWidget )
67 mWidgetContainer->setWidget( mDesignerWidget );
72 QWidget* QgsFormAnnotationItem::createDesignerWidget(
const QString& filePath )
74 QFile file( filePath );
75 if ( !file.open( QFile::ReadOnly ) )
82 loader.setWorkingDirectory( fi.dir() );
83 QWidget* widget = loader.load( &file, 0 );
87 if ( mVectorLayer && mHasAssociatedFeature )
94 for (
int i = 0; i < attrs.count(); ++i )
96 if ( i < fields.
count() )
98 QWidget* attWidget = widget->findChild<QWidget*>( fields[i].name() );
113 setFeatureForMapPosition();
125 if ( !painter || !mWidgetContainer )
148 if ( mDesignerWidget )
150 QSizeF widgetMinSize = mDesignerWidget->minimumSize();
155 return QSizeF( 0, 0 );
161 if ( mDesignerWidget )
163 return mDesignerWidget->sizeHint();
167 return QSizeF( 0, 0 );
173 QDomElement documentElem = doc.documentElement();
174 if ( documentElem.isNull() )
179 QDomElement formAnnotationElem = doc.createElement(
"FormAnnotationItem" );
182 formAnnotationElem.setAttribute(
"vectorLayer", mVectorLayer->
id() );
184 formAnnotationElem.setAttribute(
"hasFeature", mHasAssociatedFeature );
185 formAnnotationElem.setAttribute(
"feature", mFeature );
186 formAnnotationElem.setAttribute(
"designerForm", mDesignerForm );
188 documentElem.appendChild( formAnnotationElem );
194 if ( itemElem.hasAttribute(
"vectorLayer" ) )
199 QObject::connect( mVectorLayer, SIGNAL( layerModified() ),
this, SLOT( setFeatureForMapPosition() ) );
200 QObject::connect(
mMapCanvas, SIGNAL( renderComplete( QPainter* ) ),
this, SLOT( setFeatureForMapPosition() ) );
201 QObject::connect(
mMapCanvas, SIGNAL( layersChanged() ),
this, SLOT( updateVisibility() ) );
204 mHasAssociatedFeature = itemElem.attribute(
"hasFeature",
"0" ).toInt();
205 mFeature = itemElem.attribute(
"feature",
"0" ).toInt();
206 mDesignerForm = itemElem.attribute(
"designerForm",
"" );
207 QDomElement annotationElem = itemElem.firstChildElement(
"AnnotationItem" );
208 if ( !annotationElem.isNull() )
213 mDesignerWidget = createDesignerWidget( mDesignerForm );
214 if ( mDesignerWidget )
217 mWidgetContainer->setWidget( mDesignerWidget );
222 void QgsFormAnnotationItem::setFeatureForMapPosition()
237 bool featureFound =
false;
239 while ( fit.nextFeature( currentFeature ) )
241 currentFeatureId = currentFeature.
id();
246 mHasAssociatedFeature = featureFound;
247 mFeature = currentFeatureId;
250 mWidgetContainer->setWidget( 0 );
251 delete mDesignerWidget;
252 mDesignerWidget = createDesignerWidget( mDesignerForm );
253 if ( mDesignerWidget )
256 mWidgetContainer->setWidget( mDesignerWidget );
260 void QgsFormAnnotationItem::updateVisibility()
267 setVisible( visible );
QgsFeatureId id() const
Get the feature id for this feature.
Wrapper for iterator of features from vector data provider or vector layer.
QString annotationForm() const
get annotation form
A rectangle specified with double values.
void _readXML(const QDomDocument &doc, const QDomElement &annotationElem)
double mFrameBorderWidth
Width of the frame.
Use exact geometry intersection (slower) instead of bounding boxes.
QPointF mOffsetFromReferencePoint
Describes the shift of the item content box to the reference point.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
static QgsMapLayerRegistry * instance()
QList< QgsMapLayer * > layers() const
return list of layers within map canvas.
Container of fields for a vector layer.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Map canvas is a class for displaying all GIS data types on a canvas.
void drawSelectionBoxes(QPainter *p)
virtual void setMapPosition(const QgsPoint &pos)
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QList< int > QgsAttributeList
const QgsAttributes & attributes() const
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
void drawMarkerSymbol(QPainter *p)
int count() const
Return number of items.
A class to represent a point.
An annotation item can be either placed either on screen corrdinates or on map coordinates.
bool mMapPositionFixed
True: the item stays at the same map position, False: the item stays on same screen position...
static Q_DECL_DEPRECATED QWidget * createAttributeEditor(QWidget *parent, QWidget *editor, QgsVectorLayer *vl, int idx, const QVariant &value, QMap< int, QWidget * > &proxyWidgets)
Creates or prepares a attribute editor widget.
void _writeXML(QDomDocument &doc, QDomElement &itemElem) const
QgsMapCanvas * mMapCanvas
pointer to map canvas
void setFrameSize(const QSizeF &size)
void drawFrame(QPainter *p)
QgsMapLayer * mapLayer(QString theLayerId)
Retrieve a pointer to a loaded layer by id.
QColor mFrameBackgroundColor
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
bool nextFeature(QgsFeature &f)
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Represents a vector layer which manages a vector based data sets.
QgsPoint mMapPosition
Map position (in case mMapPositionFixed is true)
QSizeF mFrameSize
Size of the frame (without balloon)