Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add commandline options of editor file and svg output #187

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
58 changes: 57 additions & 1 deletion bt_editor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ main(int argc, char *argv[])
"Autoconnect to monitor");
parser.addOption(autoconnect_option);

QCommandLineOption file_option(QStringList() << "file",
"Load a file (only in editor mode)",
"tree.xml");
parser.addOption(file_option);

QCommandLineOption output_svg_option(QStringList() << "output-svg",
"Save the input file to an svg",
"output.svg");
parser.addOption(output_svg_option);

parser.process( app );

QFile styleFile( ":/stylesheet.qss" );
Expand Down Expand Up @@ -92,7 +102,7 @@ main(int argc, char *argv[])
else{
std::cout << "wrong mode passed to --mode. Use on of these: editor / monitor /replay"
<< std::endl;
return 0;
return 1;
}
}
else{
Expand All @@ -114,6 +124,52 @@ main(int argc, char *argv[])
// Start the main application.
MainWindow win( mode, monitor_address, monitor_pub_port,
monitor_srv_port, monitor_autoconnect );

if( parser.isSet(file_option) )
{
if ( mode != GraphicMode::EDITOR )
{
std::cout << "--file can only be passed in editor mode" << std::endl;
return 1;
}

QString fileName = parser.value(file_option);
std::cout << "Loading file: " << fileName.toStdString() << std::endl;

// Open file
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
{
std::cout << "Cannot open file" << std::endl;
return 1;
}

// Read file to xml
QString xml_text;
QTextStream in(&file);
while (!in.atEnd()) {
xml_text += in.readLine();
}

// Show xml
win.loadFromXML( xml_text );
}


if( parser.isSet(output_svg_option) )
{
if ( !parser.isSet(file_option))
{
std::cout << "--output-svg needs the --file" << std::endl;
return 1;
}
QString svgFile = parser.value(output_svg_option);

std::cout << "Writing to: " << svgFile.toStdString() << std::endl;
win.currentTabInfo()->saveSvgFile(svgFile);
return 0;
}

win.show();
return app.exec();
}
Expand Down
25 changes: 24 additions & 1 deletion bt_editor/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,13 +683,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 @@ -1167,6 +1184,11 @@ void MainWindow::on_toolButtonReorder_pressed()
onAutoArrange();
}

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

void MainWindow::on_toolButtonCenterView_pressed()
{
currentTabInfo()->zoomHomeView();
Expand Down Expand Up @@ -1313,6 +1335,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
4 changes: 4 additions & 0 deletions bt_editor/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public slots:

void onAutoArrange();

void onSaveSvg();

void onSceneChanged();

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

void on_toolButtonReorder_pressed();

void on_toolButtonSaveSvg_pressed();

void on_toolButtonCenterView_pressed();

void onCreateAbsBehaviorTree(const AbsBehaviorTree &tree,
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