test_sdk/gui/gtl_gui_recorder_widget.cpp

150 lines
5.3 KiB
C++
Raw Permalink Normal View History

#include "gtl_gui_recorder_widget.h"
#include "ui_gtl_gui_recorder_widget.h"
#include <QFileDialog>
#include "hw/gtl_hw_recorder_gtr.h"
#include "hw/gtl_hw_recorder_wav.h"
#include "gui/gtl_gui_chart_widget.h"
namespace gtl
{
namespace gui
{
recorder_widget::recorder_widget(gtl::data_model* model, QWidget *parent)
: QWidget(parent)
, ui(new Ui::recorder_widget)
, _recorder(NULL)
{
ui->setupUi(this);
_selection_data_model = new gtl::selection_data_model(this);
_selection_data_model->setSourceModel(model);
ui->tree->setModel(_selection_data_model);
connect(ui->select_dir, &QToolButton::clicked, this, &recorder_widget::set_dir);
connect(ui->write, &QToolButton::toggled, this, &recorder_widget::write);
QHBoxLayout *layout = new QHBoxLayout(ui->progress);
layout->setContentsMargins(0, 0, 0, 0);
_time_label = new QLabel(ui->progress);
_time_label->setAlignment(Qt::AlignCenter);
layout->addWidget(_time_label);
ui->progress->setLayout(layout);
_chart = new gtl::gui::record_chart(this);
gtl::gui::chart_widget *chart_widget = new gtl::gui::chart_widget(_chart, this);
ui->splitter_2->addWidget(chart_widget);
}
recorder_widget::~recorder_widget()
{
delete ui;
}
void recorder_widget::save(QDomElement &root_element)
{
QDomElement selection_element = root_element.ownerDocument().createElement("selection");
root_element.appendChild(selection_element);
_selection_data_model->save(selection_element);
root_element.setAttribute("dir", ui->dir->text());
root_element.setAttribute("file", ui->file->text());
root_element.setAttribute("time", ui->time->value());
root_element.setAttribute("comment", ui->comment->toPlainText());
QDomElement splitter_element = root_element.ownerDocument().createElement("splitter");
root_element.appendChild(splitter_element);
splitter_element.setAttribute("state", QString(ui->splitter->saveState().toBase64()));
QDomElement splitter_2_element = root_element.ownerDocument().createElement("splitter_2");
root_element.appendChild(splitter_2_element);
splitter_2_element.setAttribute("state", QString(ui->splitter_2->saveState().toBase64()));
}
void recorder_widget::load(const QDomElement &root_element)
{
QDomElement selection_element = root_element.firstChildElement("selection");
_selection_data_model->load(selection_element);
ui->dir->setText(root_element.attribute("dir", ""));
ui->file->setText(root_element.attribute("file", "record"));
ui->time->setValue(root_element.attribute("time", "1").toDouble());
ui->comment->setPlainText(root_element.attribute("comment", ""));
QDomElement splitter_element = root_element.firstChildElement("splitter");
ui->splitter->restoreState(QByteArray::fromBase64(splitter_element.attribute("state", "").toUtf8()));
QDomElement splitter_2_element = root_element.firstChildElement("splitter_2");
ui->splitter_2->restoreState(QByteArray::fromBase64(splitter_2_element.attribute("state", "").toUtf8()));
}
void recorder_widget::set_dir()
{
QString path = QFileDialog::getExistingDirectory(this, tr("Select directory"), ui->dir->text());
if(!path.isEmpty())
ui->dir->setText(path);
}
void recorder_widget::write(bool value)
{
if(value)
{
if(_recorder)
delete _recorder;
std::vector<gtl::analog_data*> ad;
_selection_data_model->get_selections(std::back_inserter(ad));
_recorder = new gtl::hw::recorder_wav(ad, ui->time->value(), ui->comment->toPlainText(), ui->dir->text(), ui->file->text(), this);
_chart->set_file("");
_recorder->start();
ui->progress->setMaximum(ui->time->value() == 0 ? 0 : 1000);
if(!_recorder->is_success())
{
ui->write->setChecked(false);
return;
}
connect(_recorder, &gtl::hw::recorder::finished, this, &recorder_widget::recording_finished);
connect(_recorder, &gtl::hw::recorder::progress, this, &recorder_widget::recording_progress);
}
else
{
if(_recorder)
{
QString path = _recorder->path();
delete _recorder;
_recorder = NULL;
_chart->set_file(path);
}
ui->progress->setMaximum(1000);
}
}
void recorder_widget::recording_finished()
{
ui->write->setChecked(false);
}
void recorder_widget::recording_progress(qreal value)
{
_time_label->setText(QTime::fromMSecsSinceStartOfDay(_recorder->time()*1000).toString("hh:mm:ss.zzz"));
ui->progress->setValue(qRound(value*1000));
}
}
}