Skip to content

Commit

Permalink
Add button to save svg files
Browse files Browse the repository at this point in the history
  • Loading branch information
Timple committed Jan 25, 2023
1 parent 4b8f5d3 commit 9afa30e
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 6 deletions.
15 changes: 13 additions & 2 deletions bt_editor/graphic_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <QMessageBox>
#include <QApplication>
#include <QInputDialog>
#include <QSvgGenerator>

using namespace QtNodes;

Expand Down Expand Up @@ -162,6 +163,18 @@ void GraphicContainer::nodeReorder()
emit undoableChange();
}

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);
}

void GraphicContainer::zoomHomeView()
{
QRectF rect = _scene->itemsBoundingRect();
Expand Down Expand Up @@ -720,5 +733,3 @@ void GraphicContainer::loadFromJson(const QByteArray &data)
clearScene();
scene()->loadFromMemory( data );
}


2 changes: 2 additions & 0 deletions bt_editor/graphic_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class GraphicContainer : public QObject

void nodeReorder();

void saveSvgFile(const QString path);

void zoomHomeView();

bool containsValidTree() const;
Expand Down
25 changes: 24 additions & 1 deletion bt_editor/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,13 +662,30 @@ void MainWindow::onAutoArrange()
currentTabInfo()->nodeReorder();
}

void MainWindow::onSaveSvg()
{
QSettings settings;
QString last_load_path = settings.value("MainWindow.lastLoadDirectory",
QDir::homePath() ).toString();
QString directory_path = settings.value("MainWindow.lastSaveSvgDirectory",
last_load_path ).toString();

QString fileName = QFileDialog::getSaveFileName(this,
tr("Save BehaviorTree to svg"), directory_path,
tr("SVG files (*.svg)"));
currentTabInfo()->saveSvgFile(fileName);

directory_path = QFileInfo(fileName).absolutePath();
settings.setValue("SidepanelEditor.lastSaveSvgDirectory", directory_path);
}

void MainWindow::onSceneChanged()
{
const bool valid_BT = currentTabInfo()->containsValidTree();

ui->toolButtonLayout->setEnabled(valid_BT);
ui->toolButtonReorder->setEnabled(valid_BT);
ui->toolButtonReorder->setEnabled(valid_BT);
ui->toolButtonSaveSvg->setEnabled(valid_BT);

ui->actionSave->setEnabled(valid_BT);
QPixmap pix;
Expand Down Expand Up @@ -1146,6 +1163,11 @@ void MainWindow::on_toolButtonReorder_pressed()
onAutoArrange();
}

void MainWindow::on_toolButtonSaveSvg_pressed()
{
onSaveSvg();
}

void MainWindow::on_toolButtonCenterView_pressed()
{
currentTabInfo()->zoomHomeView();
Expand Down Expand Up @@ -1292,6 +1314,7 @@ void MainWindow::updateCurrentMode()

ui->toolButtonSaveFile->setHidden( NOT_EDITOR );
ui->toolButtonReorder->setHidden( NOT_EDITOR );
ui->toolButtonSaveSvg->setHidden( NOT_EDITOR );

if( _current_mode == GraphicMode::EDITOR )
{
Expand Down
6 changes: 5 additions & 1 deletion bt_editor/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public slots:

void onAutoArrange();

void onSaveSvg();

void onSceneChanged();

void onPushUndo();
Expand Down Expand Up @@ -97,6 +99,8 @@ public slots:

void on_toolButtonReorder_pressed();

void on_toolButtonSaveSvg_pressed();

void on_toolButtonCenterView_pressed();

void onCreateAbsBehaviorTree(const AbsBehaviorTree &tree,
Expand Down Expand Up @@ -202,7 +206,7 @@ private slots:
#ifdef ZMQ_FOUND
SidepanelMonitor* _monitor_widget;
#endif

MainWindow::SavedState saveCurrentState();
void clearUndoStacks();
};
Expand Down
68 changes: 66 additions & 2 deletions bt_editor/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,7 @@ QToolButton:disabled{
<bool>true</bool>
</property>
</widget>
</item>
<item>
</item><item>
<widget class="QToolButton" name="toolButtonReorder">
<property name="enabled">
<bool>false</bool>
Expand Down Expand Up @@ -577,6 +576,71 @@ QToolButton:disabled{
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="toolButtonSaveSvg">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>80</horstretch>
<verstretch>70</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>80</width>
<height>70</height>
</size>
</property>
<property name="font">
<font>
<pointsize>9</pointsize>
</font>
</property>
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="styleSheet">
<string notr="true">QToolButton {
color:white;
}

QToolButton:hover{
background-color: rgb(110, 110, 110);
}

QToolButton:pressed{
background-color: rgb(50, 150, 0)
}

QToolButton:disabled{
color:gray;
background-color: rgb(50, 50, 50)
}
</string>
</property>
<property name="text">
<string>Save svg</string>
</property>
<property name="icon">
<iconset resource="resources/icons.qrc">
<normaloff>:/icons/svg/save_white.svg</normaloff>:/icons/svg/save_white.svg</iconset>
</property>
<property name="iconSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextUnderIcon</enum>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QFrame" name="semaphoreFrame">
<property name="minimumSize">
Expand Down

0 comments on commit 9afa30e

Please sign in to comment.