test_sdk/gui/gtl_gui_scr_widget.cpp

141 lines
5.4 KiB
C++
Raw Permalink Normal View History

#include "gtl_gui_scr_widget.h"
#include "ui_gtl_gui_scr_widget.h"
#include "gui/gtl_gui_chart_widget.h"
namespace gtl
{
namespace gui
{
scr_widget::scr_widget(gtl::scr::engine* engine, gtl::data_model* model, QWidget *parent)
: QWidget(parent)
, ui(new Ui::scr_widget)
, _engine(engine)
, _files(this)
, _selection(&_files)
{
ui->setupUi(this);
_editor = new gtl::gui::scr_editor(this);
connect(_editor, &scr_editor::init_menu, this, &scr_widget::init_editor_menu);
ui->splitter->addWidget(_editor);
_selection_data_model = new gtl::selection_data_model(this);
_selection_data_model->setSourceModel(model);
ui->data_tree->setModel(_selection_data_model);
ui->data_tree->expandAll();
connect(_selection_data_model, &gtl::selection_data_model::selected, _engine, &gtl::scr::engine::add_ad);
connect(_selection_data_model, &gtl::selection_data_model::deselected, _engine, &gtl::scr::engine::remove_ad);
// connect(_engine, &gtl::scr::engine::analog_inputs_changed, this, &scr_widget::evaluate);
_chart = new record_chart(this);
chart_widget *chart_widget_ = new chart_widget(_chart, this);
ui->files_splitter->insertWidget(0, chart_widget_);
_files.setNameFilterDisables(false);
_files.setNameFilters(QStringList() << "*.gtr" << "*.wav");
_files.setFilter(QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);
ui->files->setModel(&_files);
ui->files->setSelectionModel(&_selection);
ui->files->setRootIndex(_files.setRootPath(""));
ui->files->setSortingEnabled(true);
ui->files->header()->setSectionsClickable(true);
ui->files->header()->setSortIndicatorShown(true);
ui->files->header()->setSortIndicator(0, Qt::SortOrder::AscendingOrder);
connect(&_selection, &QItemSelectionModel::currentChanged, this, &scr_widget::selection_changed);
connect(ui->mode, &QTabWidget::currentChanged, this, &scr_widget::mode_changed);
}
scr_widget::~scr_widget()
{
delete ui;
}
void scr_widget::save(QDomElement &root_element)
{
QDomElement selection_element = root_element.ownerDocument().createElement("selection");
root_element.appendChild(selection_element);
_selection_data_model->save(selection_element);
QDomElement script_element = root_element.ownerDocument().createElement("script");
root_element.appendChild(script_element);
script_element.setAttribute("text", _editor->toPlainText());
QDomElement splitter_element = root_element.ownerDocument().createElement("splitter");
root_element.appendChild(splitter_element);
splitter_element.setAttribute("state", QString(ui->splitter->saveState().toBase64()));
QDomElement files_splitter_element = root_element.ownerDocument().createElement("files_splitter");
root_element.appendChild(files_splitter_element);
files_splitter_element.setAttribute("state", QString(ui->files_splitter->saveState().toBase64()));
root_element.setAttribute("file", _files.filePath(_selection.currentIndex()));
root_element.setAttribute("mode", ui->mode->currentIndex());
}
void scr_widget::load(const QDomElement &root_element)
{
QDomElement selection_element = root_element.firstChildElement("selection");
_selection_data_model->load(selection_element);
_editor->clear();
QDomElement script_element = root_element.firstChildElement("script");
_editor->appendPlainText(script_element.attribute("text", ""));
// evaluate();
QDomElement splitter_element = root_element.firstChildElement("splitter");
ui->splitter->restoreState(QByteArray::fromBase64(splitter_element.attribute("state", "").toUtf8()));
QDomElement files_splitter_element = root_element.firstChildElement("files_splitter");
ui->files_splitter->restoreState(QByteArray::fromBase64(files_splitter_element.attribute("state", "").toUtf8()));
_selection.setCurrentIndex(_files.index(root_element.attribute("file", "")), QItemSelectionModel::Select);
ui->mode->setCurrentIndex(root_element.attribute("mode", "0").toInt());
}
void scr_widget::evaluate()
{
if(_engine)
_engine->evaluate(_editor->toPlainText());
}
void scr_widget::selection_changed(const QModelIndex &current, const QModelIndex &previous)
{
QString path = _files.filePath(current);
_chart->set_file(path);
if(ui->mode->currentIndex() != 0)
_engine->set_file(path);
}
void scr_widget::mode_changed(int value)
{
if(value == 0)
{
_engine->set_file();
std::vector<gtl::analog_data*> ad;
_selection_data_model->get_selections(std::back_inserter(ad));
for(auto it : ad)
_engine->add_ad(it);
}
else
{
_engine->set_file(_files.filePath(_selection.currentIndex()));
}
}
}
}