Groot icon indicating copy to clipboard operation
Groot copied to clipboard

Can we export the tree structure as a figure.

Open zhen-liu-github opened this issue 2 years ago • 9 comments

Thanks for your great work. I have a behavior tree with some son node, and want to save the structure as a figure. If i direct screenshot, the image equality is poor. So dose this ui tool provide the save as figure func? I just find the save as xml func. Thanks for your advice.

zhen-liu-github avatar Jun 02 '22 06:06 zhen-liu-github

I was facing this problem myself as I was trying to generate nice figures for a paper.

It turns out that you can use the QSvgGenerator class for saving an svg view of the tree (https://doc.qt.io/qt-5/qtsvg-svggenerator-example.html).

void GraphicContainer::saveSvgFile(const QString path)
{
    QSvgGenerator generator;
    QRectF rect = _scene->itemsBoundingRect();
    generator.setFileName(path);
    generator.setSize(QSize(rect.width(), rect.height()));
    generator.setViewBox(rect);
    QPainter painter;
    painter.begin(&generator);
    _scene->render(&painter, rect, rect);
}

In order to have vector images, we also need to disable the drop shadow and the cache (https://forum.qt.io/topic/6008/how-to-save-a-qgraphicsscene-to-an-svg-file).

This allows us to generate: test

After a bit of tweaking: test2

Notice that we still have some weird additional space after the names, which I haven't figured out yet.

Check https://github.com/Affonso-Gui/Groot/commit/0ca13500d3e095bd9fa9ff44cb5b161acc9ba150 and https://github.com/Affonso-Gui/Groot/commit/be5442d730c2b63c9b0890071e32722e1432f253 for a working example.

Affonso-Gui avatar Sep 14 '22 23:09 Affonso-Gui

Thanks for your help!

zhen-liu-github avatar Sep 15 '22 05:09 zhen-liu-github

So @Affonso-Gui showed how it could be implemented and even demonstrated a working example. This doesn't mean this issue is fully closed now is it?

Timple avatar Sep 15 '22 06:09 Timple

Sure, repoened it and wait for more solutions.

zhen-liu-github avatar Sep 15 '22 07:09 zhen-liu-github

The saveSvgFile function itself should be pretty harmless, but I think there are three main points that still need to be considered if we are planning on actually implementing this feature.

  1. Shading and Cache: hopefully can be disabled just for the save and then re-enabled?
  2. Palletes: having colors is great for developing, but somewhat more questionable for external use.
  3. Interfacing: probably not worth an additional button at the side or top bars. Maybe a right-click option?

Affonso-Gui avatar Sep 15 '22 11:09 Affonso-Gui

Oh, and of course the weird additional spacing (different fonts when displaying on canvas vs on the svg image?)

Affonso-Gui avatar Sep 15 '22 11:09 Affonso-Gui

  1. I like the default colors :slightly_smiling_face: . Nobody works on black-and-white screens anymore :wink:
  2. Idealy a commandline-option. Would be great to have a generation command: Groot tree.xml --output-svg. Ideal for automated documenation generation.

Timple avatar Sep 15 '22 12:09 Timple

  1. Fair enough. Just for clarity, I was thinking of black-and-white as a save-time-only feature, though.
  2. Not sure, I also like the possibility of tweaking the position of each node to my liking before saving.

Affonso-Gui avatar Sep 15 '22 13:09 Affonso-Gui

Although the command line option does make 1 (and 2) much easier, we could just start the program with save configurations and get the job done.

Affonso-Gui avatar Sep 15 '22 13:09 Affonso-Gui