Optimize the save module

This commit is contained in:
dreamsourcelabTAI 2024-04-18 18:08:54 +08:00
parent 9509147727
commit 71f3620463
4 changed files with 140 additions and 131 deletions

View File

@ -24,7 +24,6 @@
#include "../sigsession.h" #include "../sigsession.h"
#include <QGridLayout> #include <QGridLayout>
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QTimer>
#include <QTextEdit> #include <QTextEdit>
#include <QPushButton> #include <QPushButton>
#include <QRadioButton> #include <QRadioButton>
@ -43,12 +42,13 @@ namespace pv {
namespace dialogs { namespace dialogs {
StoreProgress::StoreProgress(SigSession *session, QWidget *parent) : StoreProgress::StoreProgress(SigSession *session, QWidget *parent) :
DSDialog(parent), DSDialog(parent)
_store_session(session)
{ {
_fileLab = NULL; _fileLab = NULL;
_ckOrigin = NULL; _ckOrigin = NULL;
_store_session = new StoreSession(session);
this->setMinimumSize(550, 220); this->setMinimumSize(550, 220);
this->setModal(true); this->setModal(true);
@ -56,12 +56,10 @@ StoreProgress::StoreProgress(SigSession *session, QWidget *parent) :
_progress.setMaximum(100); _progress.setMaximum(100);
_isExport = false; _isExport = false;
_done = false; _is_done = false;
_isBusy = false;
_start_cursor = NULL; _start_cursor = NULL;
_end_cursor = NULL; _end_cursor = NULL;
_view = NULL; _view = NULL;
_is_normal_end = false;
QGridLayout *grid = new QGridLayout(); QGridLayout *grid = new QGridLayout();
_grid = grid; _grid = grid;
@ -99,60 +97,72 @@ StoreProgress::StoreProgress(SigSession *session, QWidget *parent) :
connect(_button_box, SIGNAL(rejected()), this, SLOT(reject())); connect(_button_box, SIGNAL(rejected()), this, SLOT(reject()));
connect(_button_box, SIGNAL(accepted()), this, SLOT(accept())); connect(_button_box, SIGNAL(accepted()), this, SLOT(accept()));
connect(&_store_session, SIGNAL(progress_updated()), connect(_store_session, SIGNAL(progress_updated()),
this, SLOT(on_progress_updated()), Qt::QueuedConnection); this, SLOT(on_progress_updated()), Qt::QueuedConnection);
connect(_openButton, SIGNAL(clicked()),this, SLOT(on_change_file())); connect(_openButton, SIGNAL(clicked()),this, SLOT(on_change_file()));
_progress.setVisible(false); _progress.setVisible(false);
connect(&m_timer, &QTimer::timeout, this, &StoreProgress::on_timeout);
m_timer.setInterval(100);
} }
StoreProgress::~StoreProgress() StoreProgress::~StoreProgress()
{ {
_store_session.wait(); _store_session->wait();
} }
void StoreProgress::on_change_file() void StoreProgress::closeEvent(QCloseEvent* event)
{ {
QString file = ""; //Wait the thread ends.
if (_isExport) if (_store_session->is_busy()){
file = _store_session.MakeExportFile(true); _store_session->cancel();
else event->ignore();
file = _store_session.MakeSaveFile(true); return;
if (file != ""){
_fileLab->setText(file);
if (_ckOrigin != NULL){
bool bFlag = file.endsWith(".csv");
_ckOrigin->setVisible(bFlag);
_ckCompress->setVisible(bFlag);
} }
_store_session->session()->set_saving(false);
_store_session->session()->broadcast_msg(DSV_MSG_SAVE_COMPLETE);
delete this;
}
void StoreProgress::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Escape) {
close();
}
else {
QWidget::keyPressEvent(event);
} }
} }
void StoreProgress::reject() void StoreProgress::reject()
{ {
using namespace Qt; close();
_store_session.cancel(); }
_store_session.session()->set_saving(false);
save_done(); void StoreProgress::on_timeout()
DSDialog::reject(); {
_store_session.session()->broadcast_msg(DSV_MSG_SAVE_COMPLETE); //The task is end, to close the window.
if (_store_session->is_busy() == false) {
close();
}
} }
void StoreProgress::accept() void StoreProgress::accept()
{ {
if (_store_session.GetFileName() == ""){ if (_store_session->GetFileName() == ""){
MsgBox::Show(NULL, L_S(STR_PAGE_MSG, S_ID(IDS_MSG_SEL_FILENAME), "You need to select a file name.")); MsgBox::Show(NULL, L_S(STR_PAGE_MSG, S_ID(IDS_MSG_SEL_FILENAME), "You need to select a file name."));
return; return;
} }
if (_isBusy) if (_is_done){
return; return;
}
if (_isExport && _store_session->IsLogicDataType()){
if (_isExport && _store_session.IsLogicDataType()){
bool ck = _ckOrigin->isChecked(); bool ck = _ckOrigin->isChecked();
AppConfig &app = AppConfig::Instance(); AppConfig &app = AppConfig::Instance();
if (app.appOptions.originalData != ck){ if (app.appOptions.originalData != ck){
@ -162,7 +172,7 @@ void StoreProgress::accept()
} }
// Get data range // Get data range
if (_store_session.IsLogicDataType() && _view != NULL) if (_store_session->IsLogicDataType() && _view != NULL)
{ {
uint64_t start_index = 0; uint64_t start_index = 0;
uint64_t end_index = 0; uint64_t end_index = 0;
@ -209,7 +219,7 @@ void StoreProgress::accept()
} }
} }
_store_session.SetDataRange(start_index, end_index); _store_session->SetDataRange(start_index, end_index);
} }
_progress.setVisible(true); _progress.setVisible(true);
@ -225,44 +235,25 @@ void StoreProgress::accept()
//start done //start done
if (_isExport){ if (_isExport){
if (_store_session.export_start()){ if (_store_session->export_start()){
_isBusy = true; _is_done = true;
_store_session.session()->set_saving(true); _store_session->session()->set_saving(true);
QTimer::singleShot(100, this, SLOT(timeout()));
setTitle(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_EXPORTING), "Exporting...")); setTitle(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_EXPORTING), "Exporting..."));
} m_timer.start();
else{
save_done();
close();
show_error();
} }
} }
else{ else{
if (_store_session.save_start()){ if (_store_session->save_start()){
_isBusy = true; _is_done = true;
_store_session.session()->set_saving(true); _store_session->session()->set_saving(true);
QTimer::singleShot(100, this, SLOT(timeout()));
setTitle(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_SAVING), "Saving...")); setTitle(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_SAVING), "Saving..."));
m_timer.start();
} }
else{ }
save_done();
close(); if (!_is_done){
show_error(); show_error();
}
}
//do not to call base class method, otherwise the window will be closed;
}
void StoreProgress::timeout()
{
if (_done) {
_store_session.session()->set_saving(false);
save_done();
close(); close();
delete this;
} else {
QTimer::singleShot(100, this, SLOT(timeout()));
} }
} }
@ -270,11 +261,11 @@ void StoreProgress::save_run(ISessionDataGetter *getter)
{ {
_isExport = false; _isExport = false;
setTitle(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_SAVE), "Save")); setTitle(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_SAVE), "Save"));
QString file = _store_session.MakeSaveFile(false); QString file = _store_session->MakeSaveFile(false);
_fileLab->setText(file); _fileLab->setText(file);
_store_session._sessionDataGetter = getter; _store_session->_sessionDataGetter = getter;
if (_store_session.IsLogicDataType() && _view != NULL) if (_store_session->IsLogicDataType() && _view != NULL)
{ {
QFormLayout *lay = new QFormLayout(); QFormLayout *lay = new QFormLayout();
lay->setContentsMargins(5, 0, 0, 0); lay->setContentsMargins(5, 0, 0, 0);
@ -304,7 +295,7 @@ void StoreProgress::save_run(ISessionDataGetter *getter)
void StoreProgress::export_run() void StoreProgress::export_run()
{ {
if (_store_session.IsLogicDataType()) if (_store_session->IsLogicDataType())
{ {
QFormLayout *lay = new QFormLayout(); QFormLayout *lay = new QFormLayout();
lay->setContentsMargins(5, 0, 0, 0); lay->setContentsMargins(5, 0, 0, 0);
@ -351,7 +342,7 @@ void StoreProgress::export_run()
_isExport = true; _isExport = true;
setTitle(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_EXPORT), "Export")); setTitle(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_EXPORT), "Export"));
QString file = _store_session.MakeExportFile(false); QString file = _store_session->MakeExportFile(false);
_fileLab->setText(file); _fileLab->setText(file);
if (_ckOrigin != NULL){ if (_ckOrigin != NULL){
@ -365,29 +356,17 @@ void StoreProgress::export_run()
void StoreProgress::show_error() void StoreProgress::show_error()
{ {
_done = true; if (!_store_session->error().isEmpty()) {
if (!_store_session.error().isEmpty()) { MsgBox::Show(NULL, _store_session->error().toStdString().c_str(), NULL);
MsgBox::Show(NULL, _store_session.error().toStdString().c_str(), NULL);
} }
} }
void StoreProgress::closeEvent(QCloseEvent* e)
{
if (!_is_normal_end){
_store_session.cancel();
}
_store_session.session()->set_saving(false);
save_done();
_store_session.session()->broadcast_msg(DSV_MSG_SAVE_COMPLETE);
}
void StoreProgress::on_progress_updated() void StoreProgress::on_progress_updated()
{ {
uint64_t writed = 0; uint64_t writed = 0;
uint64_t total = 0; uint64_t total = 0;
_store_session.get_progress(&writed, &total); _store_session->get_progress(&writed, &total);
if (writed < total){ if (writed < total){
int percent = writed * 1.0 / total * 100.0; int percent = writed * 1.0 / total * 100.0;
@ -397,14 +376,28 @@ void StoreProgress::on_progress_updated()
_progress.setValue(100); _progress.setValue(100);
} }
const QString err = _store_session.error(); const QString err = _store_session->error();
if (!err.isEmpty()) { if (!err.isEmpty()) {
show_error(); show_error();
} }
}
if (writed >= total){ void StoreProgress::on_change_file()
_is_normal_end = true; {
_done = true; // Set end flag. QString file = "";
if (_isExport)
file = _store_session->MakeExportFile(true);
else
file = _store_session->MakeSaveFile(true);
if (file != ""){
_fileLab->setText(file);
if (_ckOrigin != NULL){
bool bFlag = file.endsWith(".csv");
_ckOrigin->setVisible(bFlag);
_ckCompress->setVisible(bFlag);
}
} }
} }

View File

@ -24,6 +24,7 @@
#define DSVIEW_PV_DIALOGS_SAVEPROGRESS_H #define DSVIEW_PV_DIALOGS_SAVEPROGRESS_H
#include <QProgressBar> #include <QProgressBar>
#include <QTimer>
#include "../storesession.h" #include "../storesession.h"
#include "../dialogs/dsdialog.h" #include "../dialogs/dsdialog.h"
#include "../interface/icallbacks.h" #include "../interface/icallbacks.h"
@ -61,32 +62,26 @@ public:
_view = view; _view = view;
} }
protected:
void reject();
void accept();
private:
void show_error();
void closeEvent(QCloseEvent* e);
signals:
void save_done();
public slots:
void save_run(ISessionDataGetter *getter); void save_run(ISessionDataGetter *getter);
void export_run(); void export_run();
private:
void reject();
void accept();
void show_error();
void closeEvent(QCloseEvent* event) override;
void keyPressEvent(QKeyEvent *event) override;
private slots: private slots:
void on_progress_updated(); void on_progress_updated();
void timeout(); void on_timeout();
void on_change_file(); void on_change_file();
void on_ck_origin(bool ck); void on_ck_origin(bool ck);
void on_ck_compress(bool ck); void on_ck_compress(bool ck);
private: private:
pv::StoreSession _store_session; pv::StoreSession *_store_session;
QProgressBar _progress; QProgressBar _progress;
bool _done;
bool _isExport; bool _isExport;
QTextEdit *_fileLab; QTextEdit *_fileLab;
QRadioButton *_ckOrigin; QRadioButton *_ckOrigin;
@ -94,12 +89,11 @@ private:
QPushButton *_openButton; QPushButton *_openButton;
QGridLayout *_grid; QGridLayout *_grid;
QWidget *_space; QWidget *_space;
bool _isBusy;
QComboBox *_start_cursor; QComboBox *_start_cursor;
QComboBox *_end_cursor; QComboBox *_end_cursor;
view::View *_view; view::View *_view;
bool _is_normal_end; bool _is_done;
QTimer m_timer;
}; };
} // dialogs } // dialogs

View File

@ -77,6 +77,7 @@ StoreSession::StoreSession(SigSession *session) :
_sessionDataGetter = NULL; _sessionDataGetter = NULL;
_start_index = 0; _start_index = 0;
_end_index = 0; _end_index = 0;
_is_busy = false;
} }
StoreSession::~StoreSession() StoreSession::~StoreSession()
@ -518,6 +519,10 @@ void StoreSession::save_proc(data::Snapshot *snapshot)
data::AnalogSnapshot *analog_snapshot = NULL; data::AnalogSnapshot *analog_snapshot = NULL;
data::DsoSnapshot *dso_snapshot = NULL; data::DsoSnapshot *dso_snapshot = NULL;
_is_busy = true;
dsv_info("save task start.");
if ((logic_snapshot = dynamic_cast<data::LogicSnapshot*>(snapshot))) { if ((logic_snapshot = dynamic_cast<data::LogicSnapshot*>(snapshot))) {
save_logic(logic_snapshot); save_logic(logic_snapshot);
} }
@ -527,6 +532,11 @@ void StoreSession::save_proc(data::Snapshot *snapshot)
else if ((dso_snapshot = dynamic_cast<data::DsoSnapshot*>(snapshot))) { else if ((dso_snapshot = dynamic_cast<data::DsoSnapshot*>(snapshot))) {
save_dso(dso_snapshot); save_dso(dso_snapshot);
} }
dsv_info("save task end.");
std::this_thread::sleep_for(std::chrono::milliseconds(300));
_is_busy = false;
} }
bool StoreSession::meta_gen(data::Snapshot *snapshot, std::string &str) bool StoreSession::meta_gen(data::Snapshot *snapshot, std::string &str)
@ -842,6 +852,20 @@ bool StoreSession::export_start()
} }
void StoreSession::export_proc(data::Snapshot *snapshot) void StoreSession::export_proc(data::Snapshot *snapshot)
{
_is_busy = true;
dsv_info("export task start.");
export_exec(snapshot);
dsv_info("export task end.");
std::this_thread::sleep_for(std::chrono::milliseconds(300));
_is_busy = false;
}
void StoreSession::export_exec(data::Snapshot *snapshot)
{ {
assert(snapshot); assert(snapshot);
@ -1061,8 +1085,8 @@ void StoreSession::export_proc(data::Snapshot *snapshot)
progress_updated(); progress_updated();
} }
} }
}
} else if (channel_type == SR_CHANNEL_DSO) { else if (channel_type == SR_CHANNEL_DSO) {
_unit_count = snapshot->get_sample_count(); _unit_count = snapshot->get_sample_count();
unsigned int usize = 8192; unsigned int usize = 8192;
unsigned int size = usize; unsigned int size = usize;

View File

@ -56,19 +56,12 @@ public:
StoreSession(SigSession *session); StoreSession(SigSession *session);
~StoreSession(); ~StoreSession();
SigSession* session(); SigSession* session();
void get_progress(uint64_t *writed, uint64_t *total); void get_progress(uint64_t *writed, uint64_t *total);
const QString& error(); const QString& error();
bool save_start(); bool save_start();
bool export_start(); bool export_start();
void wait(); void wait();
void cancel(); void cancel();
private: private:
@ -78,6 +71,7 @@ private:
void save_dso(pv::data::DsoSnapshot *dso_snapshot); void save_dso(pv::data::DsoSnapshot *dso_snapshot);
bool meta_gen(data::Snapshot *snapshot, std::string &str); bool meta_gen(data::Snapshot *snapshot, std::string &str);
void export_proc(pv::data::Snapshot *snapshot); void export_proc(pv::data::Snapshot *snapshot);
void export_exec(pv::data::Snapshot *snapshot);
bool decoders_gen(std::string &str); bool decoders_gen(std::string &str);
@ -87,8 +81,9 @@ public:
QString MakeSaveFile(bool bDlg); QString MakeSaveFile(bool bDlg);
QString MakeExportFile(bool bDlg); QString MakeExportFile(bool bDlg);
inline QString GetFileName() inline QString GetFileName(){
{ return _file_name;} return _file_name;
}
bool IsLogicDataType(); bool IsLogicDataType();
@ -97,6 +92,10 @@ public:
_end_index = end_index; _end_index = end_index;
} }
inline bool is_busy(){
return _is_busy;
}
private: private:
QList<QString> getSuportedExportFormats(); QList<QString> getSuportedExportFormats();
double get_integer(GVariant * var); double get_integer(GVariant * var);
@ -112,9 +111,7 @@ private:
QString _file_name; QString _file_name;
QString _suffix; QString _suffix;
SigSession *_session; SigSession *_session;
std::thread _thread; std::thread _thread;
const struct sr_output_module* _outModule; const struct sr_output_module* _outModule;
uint64_t _units_stored; uint64_t _units_stored;
@ -125,6 +122,7 @@ private:
ZipMaker m_zipDoc; ZipMaker m_zipDoc;
uint64_t _start_index; uint64_t _start_index;
uint64_t _end_index; uint64_t _end_index;
volatile bool _is_busy;
}; };
} // pv } // pv