@@ -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,219 @@ 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+ const QPen & text_pen = vibesDefaults.pen (" #525252" ," -" , " 0.5" );
2326+
2327+ const QBrush & cream_brush = vibesDefaults.brush (" #fcf7e8" );
2328+
2329+ const QPen & empty_pen = vibesDefaults.pen (" transparent" ," -" , jsonValue (" LineWidth" ).toString ());
2330+
2331+
2332+ Q_ASSERT (json.contains (" type" ));
2333+ Q_ASSERT (json[" type" ].toString () == " cake" );
2334+
2335+ Q_ASSERT (center.size () == 2 );
2336+ Q_ASSERT (this ->_nbDim == center.size ());
2337+ Q_ASSERT (length > 0 .);
2338+
2339+ // Get center
2340+ const QPointF & centerPoint = QPointF (center[dimX].toDouble (), center[dimY].toDouble ());
2341+
2342+ /* This shape is inspired by the MOOS middleware GUI (see pMarineViewer) */
2343+
2344+ // If the shape has already been drawn, it has at least one child
2345+ // Update child items if they exist
2346+ if (this ->childItems ().size () > 0 )
2347+ {
2348+ // This cake is already perfect, please don't change it
2349+ }
2350+ // Else draw the shape for the first time
2351+ else {
2352+ // Set body shape
2353+ {
2354+ QGraphicsEllipseItem * disk = new QGraphicsEllipseItem (center[dimX].toDouble ()-length/2.0 , center[dimY].toDouble ()-length/8.0 , length, length/4 .);
2355+
2356+ disk->setPen (cake_pen);
2357+ disk->setBrush (cake_brush);
2358+ disk->setTransformOriginPoint (centerPoint);
2359+ disk->setRotation (orientation);
2360+ this ->addToGroup (disk);
2361+ }
2362+
2363+ {
2364+ QGraphicsRectItem * rect = new QGraphicsRectItem (center[dimX].toDouble ()-length/2.0 , center[dimY].toDouble (), length, length/2 .);
2365+ rect->setPen (empty_pen);
2366+ rect->setBrush (cake_brush);
2367+ rect->setTransformOriginPoint (centerPoint);
2368+ rect->setRotation (orientation);
2369+ this ->addToGroup (rect);
2370+
2371+ }
2372+
2373+ {
2374+ QGraphicsEllipseItem * disk = new QGraphicsEllipseItem (center[dimX].toDouble ()-length/2.0 , center[dimY].toDouble ()+length/4 ., length, length/4 .);
2375+
2376+ disk->setPen (empty_pen);
2377+ disk->setBrush (cream_brush);
2378+ disk->setTransformOriginPoint (centerPoint);
2379+ disk->setRotation (orientation);
2380+ this ->addToGroup (disk);
2381+ }
2382+
2383+ {
2384+ QGraphicsRectItem * rect = new QGraphicsRectItem (center[dimX].toDouble ()-length/2.0 , center[dimY].toDouble ()+length/4 . + length/8 ., length, length/8 .);
2385+ rect->setPen (empty_pen);
2386+ rect->setBrush (cream_brush);
2387+ rect->setTransformOriginPoint (centerPoint);
2388+ rect->setRotation (orientation);
2389+ this ->addToGroup (rect);
2390+ }
2391+
2392+ {
2393+ QPainterPath lef_line_details;
2394+ lef_line_details.moveTo (center[dimX].toDouble ()-length/2.0 , center[dimY].toDouble ());
2395+ lef_line_details.lineTo (center[dimX].toDouble ()-length/2.0 , center[dimY].toDouble ()+length/2 .);
2396+
2397+ QGraphicsPathItem * left_line = new QGraphicsPathItem (lef_line_details);
2398+
2399+ QPainterPath right_line_details;
2400+ right_line_details.moveTo (center[dimX].toDouble ()+length/2.0 , center[dimY].toDouble ());
2401+ right_line_details.lineTo (center[dimX].toDouble ()+length/2.0 , center[dimY].toDouble ()+length/2 .);
2402+
2403+ QGraphicsPathItem * right_line = new QGraphicsPathItem (right_line_details);
2404+
2405+ left_line->setPen (cake_pen);
2406+ right_line->setPen (cake_pen);
2407+
2408+ left_line->setTransformOriginPoint (centerPoint);
2409+ right_line->setTransformOriginPoint (centerPoint);
2410+
2411+ left_line->setRotation (orientation);
2412+ right_line->setRotation (orientation);
2413+
2414+ this ->addToGroup (left_line);
2415+ this ->addToGroup (right_line);
2416+ }
2417+
2418+ {
2419+ QGraphicsEllipseItem * disk = new QGraphicsEllipseItem (center[dimX].toDouble ()-length/2.0 , center[dimY].toDouble ()+length/2 .-length/8.0 , length, length/4 .);
2420+
2421+ disk->setPen (cake_pen);
2422+ disk->setBrush (cream_brush);
2423+ disk->setTransformOriginPoint (centerPoint);
2424+ disk->setRotation (orientation);
2425+ this ->addToGroup (disk);
2426+ }
2427+
2428+ {
2429+ QGraphicsEllipseItem * disk_1 = new QGraphicsEllipseItem (center[dimX].toDouble ()-length/2.0 , center[dimY].toDouble ()+length/2 .-length/16 ., length/8 ., length/8 .);
2430+ QGraphicsEllipseItem * disk_2 = new QGraphicsEllipseItem (center[dimX].toDouble ()+length/2.0 -length/8 ., center[dimY].toDouble ()+length/2 .-length/16 ., length/8 ., length/8 .);
2431+ QGraphicsEllipseItem * disk_3 = new QGraphicsEllipseItem (center[dimX].toDouble ()-length/2.0 +length/4 ., center[dimY].toDouble ()+length/2 .+length/16 ., length/8 ., length/8 .);
2432+ QGraphicsEllipseItem * disk_4 = new QGraphicsEllipseItem (center[dimX].toDouble ()+length/8 ., center[dimY].toDouble ()+length/2 .+length/16 ., length/8 ., length/8 .);
2433+ QGraphicsEllipseItem * disk_5 = new QGraphicsEllipseItem (center[dimX].toDouble ()-length/2.0 +length/4 ., center[dimY].toDouble ()+length/2 .-length/8 ., length/8 ., length/8 .);
2434+ QGraphicsEllipseItem * disk_6 = new QGraphicsEllipseItem (center[dimX].toDouble ()+length/8 ., center[dimY].toDouble ()+length/2 .-length/8 ., length/8 ., length/8 .);
2435+
2436+ QGraphicsSimpleTextItem * text = new QGraphicsSimpleTextItem (" 10" );
2437+ text->setTransform (QTransform (1 , 0 , 0 , -1 , center[dimX].toDouble ()-length/4.0 -length/8.0 ,center[dimY].toDouble ()+length));
2438+ text->setScale (0.04 *length);
2439+ text->setPen (text_pen);
2440+ text->setBrush (brush);
2441+
2442+ disk_1->setPen (pen);
2443+ disk_2->setPen (pen);
2444+ disk_3->setPen (pen);
2445+ disk_4->setPen (pen);
2446+ disk_5->setPen (pen);
2447+ disk_6->setPen (pen);
2448+
2449+ disk_1->setBrush (brush);
2450+ disk_2->setBrush (brush);
2451+ disk_3->setBrush (brush);
2452+ disk_4->setBrush (brush);
2453+ disk_5->setBrush (brush);
2454+ disk_6->setBrush (brush);
2455+
2456+ disk_1->setTransformOriginPoint (centerPoint);
2457+ disk_2->setTransformOriginPoint (centerPoint);
2458+ disk_3->setTransformOriginPoint (centerPoint);
2459+ disk_4->setTransformOriginPoint (centerPoint);
2460+ disk_5->setTransformOriginPoint (centerPoint);
2461+ disk_6->setTransformOriginPoint (centerPoint);
2462+
2463+ disk_1->setRotation (orientation);
2464+ disk_2->setRotation (orientation);
2465+ disk_3->setRotation (orientation);
2466+ disk_4->setRotation (orientation);
2467+ disk_5->setRotation (orientation);
2468+ disk_6->setRotation (orientation);
2469+
2470+ this ->addToGroup (disk_1);
2471+ this ->addToGroup (disk_2);
2472+ this ->addToGroup (disk_3);
2473+ this ->addToGroup (disk_4);
2474+
2475+ if (orientation == 0 )
2476+ this ->addToGroup (text);
2477+
2478+ this ->addToGroup (disk_5);
2479+ this ->addToGroup (disk_6);
2480+
2481+ }
2482+
2483+
2484+ }
2485+
2486+
2487+
2488+ // Update successful
2489+ return true ;
2490+ }
0 commit comments