@@ -247,6 +247,10 @@ VibesGraphicsItem * VibesGraphicsItem::newWithType(const QString type)
247247 {
248248 return new VibesGraphicsRaster ();
249249 }
250+ else if (type == " cake" )
251+ {
252+ return new VibesGraphicsCake ();
253+ }
250254 return 0 ;
251255}
252256
@@ -2268,3 +2272,199 @@ bool VibesGraphicsRaster::computeProjection(int dimX, int dimY)
22682272 // Update successful
22692273 return true ;
22702274}
2275+
2276+ //
2277+ // VibesGraphicsCake
2278+ //
2279+
2280+ bool VibesGraphicsCake::parseJsonGraphics (const QJsonObject &json)
2281+ {
2282+ // Now process shape-specific properties
2283+ // (we can only update properties of a shape, but mutation into another type is not supported)
2284+ if (json.contains (" type" ))
2285+ {
2286+ // Retrieve type
2287+ QString type = json[" type" ].toString ();
2288+
2289+ // JSON type for VibesGraphicsCake is "cake"
2290+ if (type == " cake" )
2291+ {
2292+ if (json.contains (" center" ) && json.contains (" length" ) && json.contains (" orientation" ))
2293+ {
2294+ int center_size = json[" center" ].toArray ().size ();
2295+ if (center_size == 2 && json[" length" ].toDouble () > 0 .)
2296+ {
2297+ // Set dimension
2298+ this ->_nbDim = center_size;
2299+
2300+ // Update successful
2301+ return true ;
2302+ }
2303+ }
2304+ }
2305+ }
2306+
2307+ // Unknown or empty JSON, update failed
2308+ return false ;
2309+ }
2310+
2311+ bool VibesGraphicsCake::computeProjection (int dimX, int dimY)
2312+ {
2313+ const QJsonObject & json = this ->_json ;
2314+
2315+ QJsonArray center = json[" center" ].toArray ();
2316+ double length = json[" length" ].toDouble ();
2317+ double orientation = json[" orientation" ].toDouble ();
2318+
2319+ // Get shape color (or default if not specified)
2320+ const QBrush & brush = vibesDefaults.brush (jsonValue (" FaceColor" ).toString ());
2321+ const QPen & pen = vibesDefaults.pen (jsonValue (" EdgeColor" ).toString (),jsonValue (" LineStyle" ).toString (), QString::number (7.0 *jsonValue (" LineWidth" ).toString ().toDouble ()/length));
2322+
2323+ const QBrush & cake_brush = vibesDefaults.brush (" #ffde85" );
2324+ const QPen & cake_pen = vibesDefaults.pen (" black" ," -" , " 0.1" );
2325+
2326+ const QBrush & cream_brush = vibesDefaults.brush (" #fcf7e8" );
2327+
2328+ const QPen & empty_pen = vibesDefaults.pen (" transparent" ," -" , jsonValue (" LineWidth" ).toString ());
2329+
2330+
2331+ Q_ASSERT (json.contains (" type" ));
2332+ Q_ASSERT (json[" type" ].toString () == " cake" );
2333+
2334+ Q_ASSERT (center.size () == 2 );
2335+ Q_ASSERT (this ->_nbDim == center.size ());
2336+ Q_ASSERT (length > 0 .);
2337+
2338+ // Get center
2339+ const QPointF & centerPoint = QPointF (center[dimX].toDouble (), center[dimY].toDouble ());
2340+
2341+ /* This shape is inspired by the MOOS middleware GUI (see pMarineViewer) */
2342+
2343+ // If the shape has already been drawn, it has at least one child
2344+ // Update child items if they exist
2345+ if (this ->childItems ().size () > 0 )
2346+ {
2347+ // This cake is already perfect, please don't change it
2348+ }
2349+ // Else draw the shape for the first time
2350+ else {
2351+ // Set body shape
2352+ {
2353+ QGraphicsEllipseItem * disk = new QGraphicsEllipseItem (center[dimX].toDouble ()-length/2.0 , center[dimY].toDouble ()-length/8.0 , length, length/4 .);
2354+
2355+ disk->setPen (cake_pen);
2356+ disk->setBrush (cake_brush);
2357+ disk->setTransformOriginPoint (centerPoint);
2358+ disk->setRotation (orientation);
2359+ this ->addToGroup (disk);
2360+ }
2361+
2362+ {
2363+ QGraphicsRectItem * rect = new QGraphicsRectItem (center[dimX].toDouble ()-length/2.0 , center[dimY].toDouble (), length, length/2 .);
2364+ rect->setPen (empty_pen);
2365+ rect->setBrush (cake_brush);
2366+ rect->setTransformOriginPoint (centerPoint);
2367+ rect->setRotation (orientation);
2368+ this ->addToGroup (rect);
2369+
2370+ }
2371+
2372+ {
2373+ QPainterPath lef_line_details;
2374+ lef_line_details.moveTo (center[dimX].toDouble ()-length/2.0 , center[dimY].toDouble ());
2375+ lef_line_details.lineTo (center[dimX].toDouble ()-length/2.0 , center[dimY].toDouble ()+length/2 .);
2376+
2377+ QGraphicsPathItem * left_line = new QGraphicsPathItem (lef_line_details);
2378+
2379+ QPainterPath right_line_details;
2380+ right_line_details.moveTo (center[dimX].toDouble ()+length/2.0 , center[dimY].toDouble ());
2381+ right_line_details.lineTo (center[dimX].toDouble ()+length/2.0 , center[dimY].toDouble ()+length/2 .);
2382+
2383+ QGraphicsPathItem * right_line = new QGraphicsPathItem (right_line_details);
2384+
2385+ left_line->setPen (cake_pen);
2386+ right_line->setPen (cake_pen);
2387+
2388+ left_line->setTransformOriginPoint (centerPoint);
2389+ right_line->setTransformOriginPoint (centerPoint);
2390+
2391+ left_line->setRotation (orientation);
2392+ right_line->setRotation (orientation);
2393+
2394+ this ->addToGroup (left_line);
2395+ this ->addToGroup (right_line);
2396+ }
2397+
2398+ {
2399+ QGraphicsEllipseItem * disk = new QGraphicsEllipseItem (center[dimX].toDouble ()-length/2.0 , center[dimY].toDouble ()+length/2 .-length/8.0 , length, length/4 .);
2400+
2401+ disk->setPen (cake_pen);
2402+ disk->setBrush (cream_brush);
2403+ disk->setTransformOriginPoint (centerPoint);
2404+ disk->setRotation (orientation);
2405+ this ->addToGroup (disk);
2406+ }
2407+
2408+ {
2409+ QGraphicsEllipseItem * disk_1 = new QGraphicsEllipseItem (center[dimX].toDouble ()-length/2.0 , center[dimY].toDouble ()+length/2 .-length/16 ., length/8 ., length/8 .);
2410+ QGraphicsEllipseItem * disk_2 = new QGraphicsEllipseItem (center[dimX].toDouble ()+length/2.0 -length/8 ., center[dimY].toDouble ()+length/2 .-length/16 ., length/8 ., length/8 .);
2411+ QGraphicsEllipseItem * disk_3 = new QGraphicsEllipseItem (center[dimX].toDouble ()-length/2.0 +length/4 ., center[dimY].toDouble ()+length/2 .+length/16 ., length/8 ., length/8 .);
2412+ QGraphicsEllipseItem * disk_4 = new QGraphicsEllipseItem (center[dimX].toDouble ()+length/8 ., center[dimY].toDouble ()+length/2 .+length/16 ., length/8 ., length/8 .);
2413+ QGraphicsEllipseItem * disk_5 = new QGraphicsEllipseItem (center[dimX].toDouble ()-length/2.0 +length/4 ., center[dimY].toDouble ()+length/2 .-length/8 ., length/8 ., length/8 .);
2414+ QGraphicsEllipseItem * disk_6 = new QGraphicsEllipseItem (center[dimX].toDouble ()+length/8 ., center[dimY].toDouble ()+length/2 .-length/8 ., length/8 ., length/8 .);
2415+
2416+ QGraphicsSimpleTextItem * text = new QGraphicsSimpleTextItem (" 10" );
2417+ text->setTransform (QTransform (1 , 0 , 0 , -1 , center[dimX].toDouble ()-length/4.0 -length/8.0 ,center[dimY].toDouble ()+length));
2418+ text->setScale (0.04 *length);
2419+ text->setPen (cake_pen);
2420+ text->setBrush (brush);
2421+
2422+ disk_1->setPen (pen);
2423+ disk_2->setPen (pen);
2424+ disk_3->setPen (pen);
2425+ disk_4->setPen (pen);
2426+ disk_5->setPen (pen);
2427+ disk_6->setPen (pen);
2428+
2429+ disk_1->setBrush (brush);
2430+ disk_2->setBrush (brush);
2431+ disk_3->setBrush (brush);
2432+ disk_4->setBrush (brush);
2433+ disk_5->setBrush (brush);
2434+ disk_6->setBrush (brush);
2435+
2436+ disk_1->setTransformOriginPoint (centerPoint);
2437+ disk_2->setTransformOriginPoint (centerPoint);
2438+ disk_3->setTransformOriginPoint (centerPoint);
2439+ disk_4->setTransformOriginPoint (centerPoint);
2440+ disk_5->setTransformOriginPoint (centerPoint);
2441+ disk_6->setTransformOriginPoint (centerPoint);
2442+
2443+ disk_1->setRotation (orientation);
2444+ disk_2->setRotation (orientation);
2445+ disk_3->setRotation (orientation);
2446+ disk_4->setRotation (orientation);
2447+ disk_5->setRotation (orientation);
2448+ disk_6->setRotation (orientation);
2449+
2450+ this ->addToGroup (disk_1);
2451+ this ->addToGroup (disk_2);
2452+ this ->addToGroup (disk_3);
2453+ this ->addToGroup (disk_4);
2454+
2455+ if (orientation == 0 )
2456+ this ->addToGroup (text);
2457+
2458+ this ->addToGroup (disk_5);
2459+ this ->addToGroup (disk_6);
2460+
2461+ }
2462+
2463+
2464+ }
2465+
2466+
2467+
2468+ // Update successful
2469+ return true ;
2470+ }
0 commit comments