Skip to content

Commit

Permalink
Merge pull request #497 from marius-avram/save_load_script
Browse files Browse the repository at this point in the history
Saving and loading expression scripts
  • Loading branch information
dictoon committed Aug 5, 2014
2 parents deb6fed + 735d351 commit 8c49934
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,11 @@ void DisneyMaterialCustomUI::slot_open_file_picker(const QString& widget_name)
IInputWidgetProxy* widget_proxy = m_widget_proxies.get(widget_name.toStdString());

const filesystem::path project_root_path = filesystem::path(m_project.get_path()).parent_path();
std::cout << "project_root_path" << project_root_path << std::endl;
const filesystem::path file_path = absolute(widget_proxy->get(), project_root_path);
std::cout << "file_path" << file_path << std::endl;
const filesystem::path file_root_path = file_path.parent_path();
std::cout << "file_root_path" << file_root_path << std::endl;

QFileDialog::Options options;
QString selected_filter;
Expand Down Expand Up @@ -219,7 +222,8 @@ void DisneyMaterialCustomUI::slot_open_expression_editor(const QString& widget_n
if ((sstream >> value))
expression = sstream.str();

ExpressionEditorWindow* expression_editor_window = new ExpressionEditorWindow(widget_name, expression, m_parent);
ExpressionEditorWindow* expression_editor_window = new ExpressionEditorWindow(
m_project, widget_name, expression, m_parent);

connect(
expression_editor_window, SIGNAL(signal_expression_applied(const QString&, const QString&)),
Expand Down
115 changes: 107 additions & 8 deletions src/appleseed.studio/mainwindow/project/expressioneditorwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
// appleseed.renderer headers.
#include "renderer/global/globallogger.h"
#include "renderer/modeling/material/disneymaterial.h"
#include "renderer/modeling/project/project.h"

// appleseed.foundation headers.
#include "foundation/utility/foreach.h"
Expand All @@ -45,14 +46,23 @@
#include <SeExprEditor/SeExprEdControlCollection.h>

// Qt headers.
#include <QFileDialog>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QScrollArea>
#include <QString>
#include <QVBoxLayout>

// boost headers.
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"

// Standard headers.
#include <string>
#include <fstream>
#include <sstream>

using namespace boost;
using namespace foundation;
using namespace renderer;
using namespace std;
Expand All @@ -61,19 +71,21 @@ namespace appleseed {
namespace studio {

ExpressionEditorWindow::ExpressionEditorWindow(
const QString& widget_name,
const string& expression,
QWidget* parent)
: m_widget_name(widget_name)
const Project& project,
const QString& widget_name,
const string& expression,
QWidget* parent)
: QWidget(parent)
, m_project(project)
, m_widget_name(widget_name)
, m_ui(new Ui::ExpressionEditorWindow())
, QWidget(parent)
{
m_ui->setupUi(this);
setWindowFlags(Qt::Tool);
setAttribute(Qt::WA_DeleteOnClose);
QVBoxLayout* root_layout = new QVBoxLayout(m_ui->scrollarea);

// Expression controls
// Expression controls.
SeExprEdControlCollection *controls = new SeExprEdControlCollection();
QScrollArea* controls_scrollarea = new QScrollArea(this);
controls_scrollarea->setObjectName("expression_controls");
Expand All @@ -82,6 +94,19 @@ ExpressionEditorWindow::ExpressionEditorWindow(
controls_scrollarea->setWidget(controls);
root_layout->addWidget(controls_scrollarea);

// Clear, Save, Load buttons.
QHBoxLayout* file_buttonbox = new QHBoxLayout();
QPushButton* clear_button = new QPushButton("Clear");
connect(clear_button, SIGNAL(clicked()), SLOT(slot_clear_expression()));
QPushButton* save_button = new QPushButton("Save");
connect(save_button, SIGNAL(clicked()), SLOT(slot_save_script()));
QPushButton* load_button = new QPushButton("Load");
connect(load_button, SIGNAL(clicked()), SLOT(slot_load_script()));
file_buttonbox->addWidget(clear_button);
file_buttonbox->addWidget(save_button);
file_buttonbox->addWidget(load_button);
root_layout->addLayout(file_buttonbox);

QLabel* label_editor = new QLabel("SeExpression:");
root_layout->addWidget(label_editor);
m_editor = new SeExprEditor(this, controls);
Expand All @@ -97,7 +122,7 @@ ExpressionEditorWindow::ExpressionEditorWindow(

QPushButton* apply_button = m_ui->buttonbox->button(QDialogButtonBox::Apply);

// Create connections
// Create connections.
connect(m_ui->buttonbox, SIGNAL(accepted()), SLOT(slot_accept()));
connect(apply_button, SIGNAL(clicked()), SLOT(slot_apply()));
connect(m_ui->buttonbox, SIGNAL(rejected()), SLOT(slot_cancel()));
Expand Down Expand Up @@ -139,6 +164,80 @@ void ExpressionEditorWindow::slot_cancel()
close();
}

void ExpressionEditorWindow::slot_clear_expression()
{
m_editor->setExpr("");
}

void ExpressionEditorWindow::slot_save_script()
{
QFileDialog::Options options;
QString selected_filter;

if (m_script_filepath.empty())
{
QString filepath =
QFileDialog::getSaveFileName(
this,
"Save As...",
QString::fromStdString(get_project_path()),
"Expression scripts (*.se)",
&selected_filter,
options);

if (!filepath.isEmpty())
{
filepath = QDir::toNativeSeparators(filepath);
m_script_filepath = filepath.toStdString();
}
}

if (!m_script_filepath.empty())
{
ofstream script_file(m_script_filepath.c_str());
script_file << m_editor->getExpr();
script_file.close();
}
}

void ExpressionEditorWindow::slot_load_script()
{
QFileDialog::Options options;
QString selected_filter;

QString filepath =
QFileDialog::getOpenFileName(
this,
"Open...",
QString::fromStdString(get_project_path()),
"Expression scripts (*.se)",
&selected_filter,
options);

if (!filepath.isEmpty())
{
filepath = QDir::toNativeSeparators(filepath);

// Read script and set it as an expression.
ifstream script_file(filepath.toStdString().c_str());
stringstream script_buffer;
script_buffer << script_file.rdbuf();
script_file.close();

m_editor->setExpr(script_buffer.str());
apply_expression();
}
}

string ExpressionEditorWindow::get_project_path()
{
const filesystem::path project_root_path = filesystem::path(m_project.get_path()).parent_path();
const filesystem::path file_path = absolute("script.se", project_root_path);
const filesystem::path file_root_path = file_path.parent_path();

return file_root_path.string();
}

} // namespace studio
} // namespace appleseed

Expand Down
18 changes: 14 additions & 4 deletions src/appleseed.studio/mainwindow/project/expressioneditorwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
#include <string>

// Forward declarations.
class SeExprEditor;
namespace renderer { class Project; }
namespace Ui { class ExpressionEditorWindow; }
class SeExprEditor;
class QLabel;

namespace appleseed {
Expand All @@ -52,9 +53,10 @@ class ExpressionEditorWindow

public:
ExpressionEditorWindow(
const QString& widget_name,
const std::string& expression,
QWidget* parent = 0);
const renderer::Project& project,
const QString& widget_name,
const std::string& expression,
QWidget* parent = 0);

void apply_expression();

Expand All @@ -63,14 +65,22 @@ class ExpressionEditorWindow
void slot_apply();
void slot_cancel();

void slot_clear_expression();
void slot_save_script();
void slot_load_script();

signals:
void signal_expression_applied(const QString& widget_name, const QString& expression);

private:
std::string get_project_path();

Ui::ExpressionEditorWindow* m_ui;
const renderer::Project& m_project;
const QString m_widget_name;
SeExprEditor* m_editor;
QLabel* m_error;
std::string m_script_filepath;
};

} // namespace studio
Expand Down

0 comments on commit 8c49934

Please sign in to comment.