Skip to content

Commit 75d3ca3

Browse files
authored
Merge pull request #47 from ChrisHal/bugfix_crash
prevent exception from reaching Qt event loop, fixes #46
2 parents b3dfe9a + 8545cb8 commit 75d3ca3

File tree

1 file changed

+44
-34
lines changed

1 file changed

+44
-34
lines changed

QtPMbrowser/pmbrowserwindow.cpp

+44-34
Original file line numberDiff line numberDiff line change
@@ -793,51 +793,61 @@ void PMbrowserWindow::on_actionExport_All_as_IBW_triggered()
793793
}
794794
}
795795

796+
class locale_manager {
797+
std::locale old_locale{};
798+
public:
799+
void setLocale(const char* name){
800+
old_locale = std::locale::global(std::locale(name));
801+
}
802+
~locale_manager() {
803+
std::locale::global(old_locale);
804+
}
805+
};
806+
796807
void PMbrowserWindow::on_actionExport_Metadata_as_Table_triggered()
797808
{
798809
if (!assertDatFileOpen()) {
799810
return;
800811
}
801812
DlgExportMetadata dlg(this);
802813
if (dlg.exec()) {
803-
std::locale old_locale;
804-
if (dlg.useSystemLocale()) {
805-
std::locale new_locale(""); // system default locale
806-
old_locale = std::locale::global(new_locale);
807-
}
808-
auto selected = dlg.getSelection();
809-
if (selected < 0)
810-
{
811-
selected = hkTreeNode::LevelTrace;
812-
}
813-
else {
814-
++selected; // first item in box is level 1
815-
}
816-
if (dlg.doCopy()) {
817-
std::ostringstream s;
818-
this->formatStimMetadataAsTableExport(s, selected);
819-
QGuiApplication::clipboard()->setText(s.str().c_str());
820-
}
821-
else {
822-
auto export_file_name = QFileDialog::getSaveFileName(this, "Export Metadata as TXT",
823-
lastexportpath, "tab separated file (*.txt *.csv)");
824-
if (export_file_name.length() > 0) {
825-
std::ofstream export_file(export_file_name.toStdString());
826-
if (!export_file) {
827-
QMessageBox::warning(this, "Error",
828-
QString("Cannot open file '%1'\nfor saving").arg(export_file_name));
829-
return;
830-
}
831-
try {
814+
try {
815+
locale_manager lm;
816+
if (dlg.useSystemLocale()) {
817+
lm.setLocale(""); // set default locale
818+
}
819+
else {
820+
lm.setLocale("C");
821+
}
822+
auto selected = dlg.getSelection();
823+
if (selected < 0)
824+
{
825+
selected = hkTreeNode::LevelTrace;
826+
}
827+
else {
828+
++selected; // first item in box is level 1
829+
}
830+
if (dlg.doCopy()) {
831+
std::ostringstream s;
832+
this->formatStimMetadataAsTableExport(s, selected);
833+
QGuiApplication::clipboard()->setText(s.str().c_str());
834+
}
835+
else {
836+
auto export_file_name = QFileDialog::getSaveFileName(this, "Export Metadata as TXT",
837+
lastexportpath, "tab separated file (*.txt *.csv)");
838+
if (export_file_name.length() > 0) {
839+
std::ofstream export_file(export_file_name.toStdString());
840+
if (!export_file) {
841+
QMessageBox::warning(this, "Error",
842+
QString("Cannot open file '%1'\nfor saving").arg(export_file_name));
843+
return;
844+
}
832845
this->formatStimMetadataAsTableExport(export_file, selected);
833846
}
834-
catch (const std::exception& e) {
835-
QMessageBox::warning(this, "Error while exporting", e.what());
836-
}
837847
}
838848
}
839-
if (dlg.useSystemLocale()) {
840-
std::locale::global(old_locale);
849+
catch (const std::exception& e) {
850+
QMessageBox::warning(this, "Error while exporting", e.what());
841851
}
842852
}
843853
}

0 commit comments

Comments
 (0)