Hey, i`ve got following problem.
I`m developing a GIS-like application an for that, i define a class SpatialItem from which all spatial items (like Polygon, Point, Line...) inherit. SpatialItem itself inherits from QGraphicsItem and QObject. All inherited spatial items (SpatialPolygonItem for instance) have their own implementation of the paint() method (inherited from QGraphicsItem). There is no problem so far.
The thing is, every spatial item has a SpatialItemRenderer member, while SpatialPolygonItem of course has a SpatialPolygonRenderer member (which inherits from SpatialItemRenderer) and so on... A group of items with same type are now stored in a class SpatialLayer which is not inherited by any other class, respectively is not polymorphistic (because of many reasons). The SpatialItemRenderer is beeing given to the SpatialItems`s through the SpatialLayer (in form of a pointer), because all Items in a layer should have the same renderer. Now, in case that a layer can store various item types (as i sad: Point, Polygon...), and every item type has its own renderer type, how can i realize that in a method in the SpatialLayer class. The layer has a kind of polymorphistic character, because it can store different kinds of spatial items, but i guess i have to write a function like
void SpatialLayer::setSpatialRenderer(SpatialItemRenderer *renderer)
{
switch(layerType) // layerType stores the type of the layer (Point, Polygon...)
{
case Point:
SpatialPointRenderer *pointRenderer = dynamic_cast<SpatialPointRenderer *> renderer;
break;
case Polygon:
SpatialPolygonRenderer *polygonRenderer = dynamic_cast<SpatialPolygonRenderer *> renderer;
break;
...
}
}
void SpatialLayer::setSpatialRenderer(SpatialItemRenderer *renderer)
{
switch(layerType) // layerType stores the type of the layer (Point, Polygon...)
{
case Point:
SpatialPointRenderer *pointRenderer = dynamic_cast<SpatialPointRenderer *> renderer;
break;
case Polygon:
SpatialPolygonRenderer *polygonRenderer = dynamic_cast<SpatialPolygonRenderer *> renderer;
break;
...
}
}
To copy to clipboard, switch view to plain text mode
Is that my only chance to realize it? My problem is, im not happy about casting, i would rather preferr a way where i can avoid it.
Im in a pretty early development stage, im very happy about now. So i can change a lot of the concept i explained here.
I hope at least anybody get what i wanted to say.
Bookmarks