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 save-as button and shortcut #195

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
50 changes: 39 additions & 11 deletions bt_editor/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ MainWindow::MainWindow(GraphicMode initial_mode,
connect( redo_shortcut, &QShortcut::activated, this, &MainWindow::onRedoInvoked );

QShortcut* save_shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_S), this);
QShortcut* save_as_shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_S), this);

connect( _editor_widget, &SidepanelEditor::nodeModelEdited,
this, &MainWindow::onTreeNodeEdited);
Expand Down Expand Up @@ -204,6 +205,11 @@ MainWindow::MainWindow(GraphicMode initial_mode,

connect( save_shortcut, &QShortcut::activated, this, &MainWindow::on_actionSave_triggered );

connect( ui->toolButtonSaveAsFile, &QToolButton::clicked,
this, &MainWindow::on_actionSaveAs_triggered );

connect( save_as_shortcut, &QShortcut::activated, this, &MainWindow::on_actionSaveAs_triggered );

connect( _replay_widget, &SidepanelReplay::changeNodeStyle,
this, &MainWindow::onChangeNodesStatus);

Expand Down Expand Up @@ -433,23 +439,25 @@ void MainWindow::on_actionLoad_triggered()
QString directory_path = settings.value("MainWindow.lastLoadDirectory",
QDir::homePath() ).toString();

QString fileName = QFileDialog::getOpenFileName(this,
QString filePath = QFileDialog::getOpenFileName(this,
tr("Load BehaviorTree from file"), directory_path,
tr("BehaviorTree files (*.xml)"));
if (!QFileInfo::exists(fileName)){
if (!QFileInfo::exists(filePath)){
return;
}

QFile file(fileName);
QFile file(filePath);

if (!file.open(QIODevice::ReadOnly)){
return;
}

directory_path = QFileInfo(fileName).absolutePath();
directory_path = QFileInfo(filePath).absolutePath();
settings.setValue("MainWindow.lastLoadDirectory", directory_path);
settings.sync();

_loaded_tree_file_path = filePath;

QString xml_text;

QTextStream in(&file);
Expand Down Expand Up @@ -633,7 +641,7 @@ void MainWindow::recursivelySaveNodeCanonically(QXmlStreamWriter &stream, const
}
}

void MainWindow::on_actionSave_triggered()
void MainWindow::saveTree(QString loadedPath)
{
for (auto& it: _tab_info)
{
Expand All @@ -656,26 +664,46 @@ void MainWindow::on_actionSave_triggered()
QString directory_path = settings.value("MainWindow.lastSaveDirectory",
QDir::currentPath() ).toString();

auto fileName = QFileDialog::getSaveFileName(this, "Save BehaviorTree to file",
auto filePath = loadedPath;
if(filePath.isEmpty())
{
filePath = QFileDialog::getSaveFileName(this, "Save BehaviorTree to file",
directory_path, "BehaviorTree files (*.xml)");
if (fileName.isEmpty()){
}

if (filePath.isEmpty())
{
return;
}
if (!fileName.endsWith(".xml"))

if (!filePath.endsWith(".xml"))
{
fileName += ".xml";
filePath += ".xml";
}

QString xml_text = saveToXML();

QFile file(fileName);
QFile file(filePath);
if (file.open(QIODevice::WriteOnly)) {
QTextStream stream(&file);
stream << xml_text;
}

directory_path = QFileInfo(fileName).absolutePath();
directory_path = QFileInfo(filePath).absolutePath();
settings.setValue("MainWindow.lastSaveDirectory", directory_path);
_loaded_tree_file_path = filePath;

}

void MainWindow::on_actionSave_triggered()
{
saveTree(_loaded_tree_file_path);
}


void MainWindow::on_actionSaveAs_triggered()
{
saveTree();
}

void MainWindow::onAutoArrange()
Expand Down
5 changes: 5 additions & 0 deletions bt_editor/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public slots:

void on_actionSave_triggered();

void on_actionSaveAs_triggered();

void onSubtreeSelected(const QString& subtreeName);

void on_splitter_splitterMoved(int pos = 0, int index = 0);
Expand Down Expand Up @@ -206,6 +208,8 @@ private slots:

QString _main_tree;

QString _loaded_tree_file_path;

SidepanelEditor* _editor_widget;
SidepanelReplay* _replay_widget;
#ifdef ZMQ_FOUND
Expand All @@ -219,6 +223,7 @@ private slots:

MainWindow::SavedState saveCurrentState();
void clearUndoStacks();
void saveTree(QString loadedPath = "");
};


Expand Down
76 changes: 76 additions & 0 deletions bt_editor/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,74 @@ QToolButton:disabled{
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="toolButtonSaveAsFile">
<property name="minimumSize">
<size>
<width>80</width>
<height>70</height>
</size>
</property>
<property name="maximumSize">
<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="autoFillBackground">
<bool>false</bool>
</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 Tree As</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>34</width>
<height>34</height>
</size>
</property>
<property name="checkable">
<bool>false</bool>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextUnderIcon</enum>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
Expand Down Expand Up @@ -858,6 +926,14 @@ QToolButton:disabled{
<string>Ctrl+S</string>
</property>
</action>
<action name="actionSaveAs">
<property name="text">
<string>Save</string>
</property>
<property name="shortcut">
<string>Ctrl+Shift+S</string>
</property>
</action>
<action name="actionQuit">
<property name="text">
<string>Quit</string>
Expand Down