[Qgis-developer] How do I change the symbol and its size from a provider?

Tim Sutton tim at linfiniti.com
Wed Jan 23 17:37:25 EST 2008


Hi

Yeah sorry the layer map has been changed to QMap in svn trunk so your
plugin will need a small update when 0.9.2rc1 comes out. Note that the
api will undergo changes until the 1.0 release at which point it will
be maintains in a backwards compatible state for the 1.x releases.

The second part of your question relates to the legend not refreshing.
You need to tell it to refresh after making changes to the symbology.
A good place to look how this is done is in

src/gui/qgsvectorlayerproperties.cpp/h

You will see it has a signal

     71   signals:     72
     73     /** emitted when changes to layer were saved to update legend */
     74     void refreshLegend(QString layerID, bool expandItem);

This is connected to the legend instance when the vector layer props
dialog is opened from the legend context menu:

legend/qgslegend.cpp:      connect(vlp,
SIGNAL(refreshLegend(QString,bool)), this,
SLOT(refreshLayerSymbology(QString,bool)));

In your case since you are not in the context of legend when opening
your dialog you will need to get teh legend instance from the qgis
application instance via the plugin interface, and connect your signal
that way.

Hope that helps!

Regards

Tim


2008/1/23, Ken Roser <kroser at roser.us>:
> This was very helpful.  I noticed the layer map was actually a std::map
> instead of a QMap, so I had to change to code to adjust to that.  I can
> now display my custom symbol on the map, but I noticed the graphic for
> the symbol you see on the stack of layers on the left pane of QGIS
> doesn't show my new symbol.  How would I update that symbol also?
>
> Tim Sutton wrote:
> > Hi
> >
> > You would need to create a modified version of the delimited text
> > plugin (the gui not the provider). After the gui creates the
> > vectorlayer based on the delimited text provider, you would apply your
> > symbology using code similar to my last post, and then return control
> > to the user interface. Here is the logic trail:
> >
> > http://svn.qgis.org/trac/browser/trunk/qgis/src/plugins/delimited_text/qgsdelimitedtextplugingui.cpp
> > 107       emit drawVectorLayer(uri,txtLayerName->text(),"delimitedtext");
> >
> > http://svn.qgis.org/trac/browser/trunk/qgis/src/plugins/delimited_text/qgsdelimitedtextplugin.cpp
> > 131   void QgsDelimitedTextPlugin::drawVectorLayer(QString thePathNameQString,
> > 132       QString theBaseNameQString, QString theProviderQString)
> > 133   {
> > 134     qGisInterface->addVectorLayer( thePathNameQString,
> > 135         theBaseNameQString, theProviderQString);
> > 136   }
> >
> > From there things get a bit tricky because addVectorLayer returns bool
> > so you dont at that point have a handle for the actual layer. So next
> > you need to trawl the registry and find your layer from the layers
> > map:
> >
> > QMap<QString, QgsMapLayer*> myMapLayers =
> >      QgsMapLayerRegistry::instance()->mapLayers();
> > QMap<QString, QgsMapLayer*>::const_iterator myIterator;
> > QgsMapLayer * mypLayer;
> > while (myIterator.hasNext())
> > {
> >   mypLayer = myIterator.value();
> >   if (mypLayer->source() == theFileNameYouUsedToLoadTheLayer)
> >  {
> >      //layer is found
> >      break;
> >   }
> > }
> >
> > Now you can cast the layer to a vector layer:
> >
> > QgsVectorLayer *mypVectorLayer  =
> >             dynamic_cast<QgsVectorLayer *> ( mypLayer );
> >     if ( mypVectorLayer )
> >     {
> >         const QgsRenderer* mypRenderer = mypVectorLayer->renderer();
> >         //now set symbology etc...
> >      }
> >
> >
> > Writing this makes me wonder if we shouldnt make addVectorLayer return
> > the registry layer id, but the above describes how you can achieve
> > your desired result.
> >
> > Hope that helps!
> >
> > Regards
> >
> > Tim
> >
> >
> >
> >
> > 2008/1/23, Ken Roser <kroser at roser.us>:
> >
> >> Sorry if I wasn't too clear.  Let me try to present the problem just a
> >> bit differently.
> >>
> >> Let's say I'm using the delimited text plugin and provider, as is, in
> >> the source code, but I want to modify it so that the points are
> >> displayed as triangles instead of squares and at size 20 instead of the
> >> default.  These choices would be built into the software, i.e., not user
> >> selectable.  How would I do that?
> >>
> >> Tim Sutton wrote:
> >>
> >>> Hi Ken
> >>>
> >>> I'm not sure that it makes sense to set symbology in your provider
> >>> itself so Im hoping you are trying to do it from some kind of add
> >>> layer gui, but you havent provided much information for me to know
> >>> whats really going on. That said here is how you can set the symbol on
> >>> a new vector layer instance programmatically:
> >>>
> >>>  1572           QgsSymbol *  mypSymbol = new
> >>> QgsSymbol(mypVectorLayer->vectorType());
> >>>  1573           QColor
> >>> myPresenceColor(mySettings.value("mapping/presenceColour",QColor(Qt::green).name()).toString());
> >>>  1574           mypSymbol->setFillColor(myPresenceColor);
> >>>  1575           mypSymbol->setColor(Qt::black); //outline
> >>>  1576           mypSymbol->setPointSize(mySymbolSize);
> >>>  1577           mypSymbol->setFillStyle(Qt::SolidPattern);
> >>>  1578           QgsSingleSymbolRenderer *mypRenderer = new
> >>> QgsSingleSymbolRenderer(mypVectorLayer->vectorType());
> >>>  1579           mypRenderer->addSymbol(mypSymbol);
> >>>  1580           mypVectorLayer->setRenderer(mypRenderer);
> >>>
> >>> The above is a single symbol renderer take from one of my cpp
> >>> projects. The same project also implements a unigue value renderer -
> >>> see the original source at http://tinyurl.com/3xystm for more info.
> >>>
> >>> I hope that helped!
> >>>
> >>> Regards
> >>>
> >>> Tim
> >>>
> >>> 2008/1/23, Ken Roser <kroser at roser.us>:
> >>>
> >>>
> >>>> I'm writing a plugin/provider that's similar in functionality to the
> >>>> delimited text plug in.  Things are working well, but I need to change
> >>>> the size, color and possibly the symbol itself when I add a point to the
> >>>> map.  I've been unable to determine a method of doing so other than
> >>>> going to the symbology tab on the layer properties dialog.  I want the
> >>>> change to happen automatically via the provider code.  Can anyone help out?
> >>>> _______________________________________________
> >>>> Qgis-developer mailing list
> >>>> Qgis-developer at lists.qgis.org
> >>>> http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
> >>>>
> >>>>
> >>>>
> >>>
> >>>
> >>
> >
> >
> >
>
>


-- 
Tim Sutton
QGIS Project Steering Committee Member - Release  Manager
Visit http://qgis.org for a great open source GIS
openModeller Desktop Developer
Visit http://openModeller.sf.net for a great open source ecological
niche modelling tool
Home Page: http://tim.linfiniti.com
Skype: timlinux
Irc: timlinux on #qgis at freenode.net



More information about the Qgis-developer mailing list