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

Tim Sutton tim at linfiniti.com
Wed Jan 23 13:09:21 EST 2008


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