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 );