Skip to content

Commit

Permalink
Changed export history to save history on every update from qserver
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Glowacki committed Nov 5, 2024
1 parent 30a2799 commit 8017493
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/mvc/BlueskyComm.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class BlueskyComm : public QThread
QJsonObject kwargs;
for(auto itr: plan.parameters)
{
logI<<itr.first.toStdString()<<" : "<<itr.second.default_val.toStdString()<<" :: "<<(int)(itr.second.kind)<< "\n";
//logI<<itr.first.toStdString()<<" : "<<itr.second.default_val.toStdString()<<" :: "<<(int)(itr.second.kind)<< "\n";
if(itr.second.name == "detectors")
{
QJsonArray inner_args; // need this for bluesky or it doesn't work
Expand Down
2 changes: 1 addition & 1 deletion src/mvc/CoLocalizationWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ void CoLocalizationWidget::onExportPng()
export_model_dir.cd("output");

QDateTime date = QDateTime::currentDateTime();
QString formattedTime = date.toString("yyyy.MM.dd_hh.mm.ss");
QString formattedTime = date.toString("yyyy_MM_dd_hh_mm_ss");
QByteArray formattedTimeMsg = formattedTime.toLocal8Bit();

QPixmap pixmap(rect().size());
Expand Down
69 changes: 54 additions & 15 deletions src/mvc/LiveMapsElementsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ void LiveMapsElementsWidget::createLayout()
connect(_scan_queue_widget, &ScanQueueWidget::onRemoveScan, this, &LiveMapsElementsWidget::callRemoveScan);
connect(_scan_queue_widget, &ScanQueueWidget::onPlanChanged, this, &LiveMapsElementsWidget::callUpdatePlan);
connect(_scan_queue_widget, &ScanQueueWidget::onAddScan, this, &LiveMapsElementsWidget::callQueueScan);
connect(_scan_queue_widget, &ScanQueueWidget::onExportHistory, this, &LiveMapsElementsWidget::exportHistory);
connect(_scan_queue_widget, &ScanQueueWidget::onSetHistory, this, &LiveMapsElementsWidget::setHistoryLocation);
connect(_scan_queue_widget, &ScanQueueWidget::onClearHistory, this, &LiveMapsElementsWidget::clearHistory);

_tab_widget = new QTabWidget();
_tab_widget->addTab(_mapsElementsWidget, "Counts");
Expand Down Expand Up @@ -325,7 +326,10 @@ void LiveMapsElementsWidget::getQueuedScans()
{
_scan_queue_widget->newDataArrived( msg );
}

else
{
saveHistory();
}
if (false == _qserverComm->get_queued_scans(msg, _queued_scans, _running_scan))
{
_scan_queue_widget->newDataArrived( msg );
Expand All @@ -335,33 +339,47 @@ void LiveMapsElementsWidget::getQueuedScans()

//---------------------------------------------------------------------------

void LiveMapsElementsWidget::exportHistory()
void LiveMapsElementsWidget::setHistoryLocation()
{

QDateTime date = QDateTime::currentDateTime();
QString formattedTime = date.toString("yyyy.MM.dd_hh.mm.ss");
QString formattedTime = date.toString("yyyy_MM_dd_hh_mm_ss");
QByteArray formattedTimeMsg = formattedTime.toLocal8Bit();
QString apath = "Scan_History_" + formattedTime + ".json";

QString fileName = QFileDialog::getSaveFileName(this,
"Scan History", apath,
tr("JSON (*.json);;CSV (*.csv)"));

if(fileName.length() > 0)
{
Preferences::inst()->setValue(STR_SAVE_QSERVER_HISTORY_LOCATION, fileName);
saveHistory();
}
}

//---------------------------------------------------------------------------

void LiveMapsElementsWidget::saveHistory()
{
QString fileName = Preferences::inst()->getValue(STR_SAVE_QSERVER_HISTORY_LOCATION).toString();

if(fileName.length() > 0)
{
QFile file(fileName);

if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox::warning(nullptr, "Export History", "Could not open the file.");
//QMessageBox::warning(nullptr, "Export History", "Could not open the file.");
logW<<"Could not open QServer history filepath "<<fileName.toStdString()<<"\n";
}
QString msg;

if (fileName.endsWith(".json"))
{
if (false == _qserverComm->get_scan_history(msg, _finished_scans, true))
{
QMessageBox::warning(nullptr, "Export History", "Failed to get scan history from QServer");
//QMessageBox::warning(nullptr, "Export History", "Failed to get scan history from QServer");
_scan_queue_widget->newDataArrived( msg );
}
file.write(msg.toUtf8());
Expand All @@ -370,7 +388,7 @@ void LiveMapsElementsWidget::exportHistory()
{
if (false == _qserverComm->get_scan_history(msg, _finished_scans, false))
{
QMessageBox::warning(nullptr, "Export History", "Failed to get scan history from QServer");
//QMessageBox::warning(nullptr, "Export History", "Failed to get scan history from QServer");
_scan_queue_widget->newDataArrived( msg );
}
if(_finished_scans.size() > 0)
Expand Down Expand Up @@ -398,17 +416,38 @@ void LiveMapsElementsWidget::exportHistory()
}
}
file.close();
}
}

QMessageBox::information(this, "Export History", "Saved!");
//---------------------------------------------------------------------------

if (false == _qserverComm->clear_history(msg))
{
QMessageBox::warning(nullptr, "Export History", "Failed to clear scan history from QServer");
_scan_queue_widget->newDataArrived( msg );
}
void LiveMapsElementsWidget::clearHistory()
{

QString fileName = Preferences::inst()->getValue(STR_SAVE_QSERVER_HISTORY_LOCATION).toString();

// Set history name to new date so we don't lose old history
if(fileName.length() > 0)
{
QFileInfo finfo = QFileInfo(fileName);
QDir dir = finfo.absoluteDir();

QDateTime date = QDateTime::currentDateTime();
QString formattedTime = date.toString("yyyy_MM_dd_hh_mm_ss");
QByteArray formattedTimeMsg = formattedTime.toLocal8Bit();
QString apath = dir.absolutePath() + QDir::separator() + finfo.baseName() + "_" + formattedTime + finfo.suffix();

Preferences::inst()->setValue(STR_SAVE_QSERVER_HISTORY_LOCATION, apath);
}

getQueuedScans();
QString msg;
if (false == _qserverComm->clear_history(msg))
{
QMessageBox::warning(nullptr, "Export History", "Failed to clear scan history from QServer");
_scan_queue_widget->newDataArrived( msg );
}

getQueuedScans();
}

//---------------------------------------------------------------------------
Expand Down
6 changes: 5 additions & 1 deletion src/mvc/LiveMapsElementsWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ public slots:

void getQueuedScans();

void exportHistory();
void setHistoryLocation();

void clearHistory();

void saveHistory();

void callOpenEnv();

Expand Down
2 changes: 1 addition & 1 deletion src/mvc/MapsElementsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2384,7 +2384,7 @@ void MapsElementsWidget::on_export_images()
_counts_window->render(&pixmap, QPoint(), QRegion(_counts_window->rect()));

QDateTime date = QDateTime::currentDateTime();
QString formattedTime = date.toString("yyyy.MM.dd_hh.mm.ss");
QString formattedTime = date.toString("yyyy_MM_dd_hh_mm_ss");
QByteArray formattedTimeMsg = formattedTime.toLocal8Bit();

qDebug() << "Date:" + formattedTime;
Expand Down
2 changes: 1 addition & 1 deletion src/mvc/RoiStatisticsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ void RoiStatisticsWidget::setData(QDir model_dir,
}

QDateTime date = QDateTime::currentDateTime();
QString formattedTime = date.toString("yyyy.MM.dd_hh.mm.ss");
QString formattedTime = date.toString("yyyy_MM_dd_hh_mm_ss");
filename += formattedTime + ".csv";

_str_export_filename = _export_dir.absolutePath() + QDir::separator() + filename;
Expand Down
18 changes: 13 additions & 5 deletions src/mvc/ScanQueueWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ void ScanQueueWidget::_createLayout()
_btn_add_batch_scan->setEnabled(false);
connect(_btn_add_batch_scan, &QPushButton::pressed, &_scan_dialog, &ScanRegionDialog::show);

_btn_export_history = new QPushButton("Export History");
connect(_btn_export_history, &QPushButton::pressed, this, &ScanQueueWidget::onExportHistory);
_btn_set_history = new QPushButton("Set History Location");
connect(_btn_set_history, &QPushButton::pressed, this, &ScanQueueWidget::onSetHistory);

_btn_clear_history = new QPushButton("Clear History");
connect(_btn_clear_history, &QPushButton::pressed, this, &ScanQueueWidget::onClearHistory);

_scan_dialog.setRegionNameVisible(false);
connect(&_scan_dialog, &ScanRegionDialog::ScanUpdated, this, &ScanQueueWidget::onAddScan);
Expand Down Expand Up @@ -131,8 +134,9 @@ void ScanQueueWidget::_createLayout()
grid->addWidget(_btn_close_env,0,4);
grid->addWidget(_btn_add_scan,0,6);
grid->addWidget(_btn_add_batch_scan,0,7);
grid->addWidget(_btn_export_history,0,8);
grid->addItem(new QSpacerItem(999,10), 0,9);
grid->addWidget(_btn_set_history,0,8);
grid->addWidget(_btn_clear_history,0,9);
grid->addItem(new QSpacerItem(999,10), 0,10);

layout->addItem(grid);

Expand Down Expand Up @@ -259,7 +263,11 @@ void ScanQueueWidget::newDataArrived(const QString& data)
|| data.count("The plan was exited") > 0
|| data.count("Removing item from the queue") > 0
|| data.count("Starting the plan") > 0
|| data.count("Queue is empty") > 0)
|| data.count("Queue is ") > 0
//|| data.count("Queue is empty") > 0
//|| data.count("Queue is stopped") > 0
)

{
emit queueNeedsToBeUpdated();
}
Expand Down
8 changes: 6 additions & 2 deletions src/mvc/ScanQueueWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ class ScanQueueWidget : public QWidget

void onAddScan(const BlueskyPlan&);

void onExportHistory();
void onSetHistory();

void onClearHistory();

public slots:
void newDataArrived(const QString &);
Expand Down Expand Up @@ -105,7 +107,9 @@ public slots:

QPushButton* _btn_add_batch_scan;

QPushButton* _btn_export_history;
QPushButton* _btn_set_history;

QPushButton* _btn_clear_history;

QAction* _move_scan_up;

Expand Down
2 changes: 1 addition & 1 deletion src/mvc/ScatterPlotView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void ScatterPlotView::exportPngCsv()

QPixmap pixmap = _chartView->grab();
QDateTime date = QDateTime::currentDateTime();
QString formattedTime = date.toString("yyyy.MM.dd_hh.mm.ss");
QString formattedTime = date.toString("yyyy_MM_dd_hh_mm_ss");
QByteArray formattedTimeMsg = formattedTime.toLocal8Bit();

dir.cdUp();
Expand Down
3 changes: 2 additions & 1 deletion src/preferences/Preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ Preferences::Preferences()
{STR_PRF_FILE_SIZE, QVariant()},
{STR_PRF_SHOW_DATASET_ON_LOAD, QVariant()},
{STR_PREF_RADIO_LOAD_SELECTED_OPTION, QVariant()},
{STR_PREF_SPRECTRA_CONTROLS_HORIZONTAL_OPTION, QVariant()}
{STR_PREF_SPRECTRA_CONTROLS_HORIZONTAL_OPTION, QVariant()},
{STR_SAVE_QSERVER_HISTORY_LOCATION, QVariant()}
};
load();
}
Expand Down
1 change: 1 addition & 0 deletions src/preferences/Preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
#define STR_PRF_SHOW_DATASET_ON_LOAD "ShowOnLoad"
#define STR_PREF_RADIO_LOAD_SELECTED_OPTION "Load_Selected_Opt"
#define STR_PREF_SPRECTRA_CONTROLS_HORIZONTAL_OPTION "Spectra_Controls_Horizontal"
#define STR_SAVE_QSERVER_HISTORY_LOCATION "Save_Qserver_History_Location"

/**
* @brief Read and save preferences between application restarts, the vaule key
Expand Down

0 comments on commit 8017493

Please sign in to comment.