Skip to content

Commit

Permalink
Merge pull request #36 from ChrisHal/improvement_metadata_to_clipboard
Browse files Browse the repository at this point in the history
metadata export: added "copy to clipboard" and "export with system locale"
  • Loading branch information
ChrisHal authored Oct 24, 2023
2 parents d1794df + 9c56536 commit 9318f4e
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 36 deletions.
10 changes: 10 additions & 0 deletions QtPMbrowser/DlgExportMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include <QSettings>
#include <QPushButton>
#include "DlgExportMetadata.h"
#include "ui_DlgExportMetadata.h"

Expand All @@ -28,6 +29,8 @@ DlgExportMetadata::DlgExportMetadata(QWidget *parent)
ui->setupUi(this);
auto index = settings.value("DlgExportMetadata/selection", 3).toInt();
ui->comboBoxLevel->setCurrentIndex(index);
ui->checkBoxSystemLocale->setChecked(settings.value("DlgExportMetadata/nativeEncoding", 0).toInt());
QObject::connect(ui->pushButtonCopy, &QPushButton::clicked, this, &DlgExportMetadata::copyToClipboard);
}

DlgExportMetadata::~DlgExportMetadata()
Expand All @@ -39,5 +42,12 @@ void DlgExportMetadata::accept(){
QSettings settings;
selection = ui->comboBoxLevel->currentIndex();
settings.setValue("DlgExportMetadata/selection", selection);
m_nativeEncoding = ui->checkBoxSystemLocale->isChecked();
settings.setValue("DlgExportMetadata/nativeEncoding", m_nativeEncoding);
QDialog::accept();
};

void DlgExportMetadata::copyToClipboard() {
m_doCopy = true;
this->accept();
}
11 changes: 11 additions & 0 deletions QtPMbrowser/DlgExportMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,26 @@ class DlgExportMetadata : public QDialog
{
Q_OBJECT

public slots:
void copyToClipboard();

public:
DlgExportMetadata(QWidget* parent = nullptr);
~DlgExportMetadata();
int getSelection() {
return selection;
}
bool doCopy() {
return m_doCopy;
}
bool useSystemLocale() {
return m_nativeEncoding;
}
void accept() override;

private:
int selection;
Ui::DlgExportMetadata *ui;
bool m_doCopy{ false };
bool m_nativeEncoding{ false };
};
69 changes: 45 additions & 24 deletions QtPMbrowser/DlgExportMetadata.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,48 @@
<rect>
<x>0</x>
<y>0</y>
<width>274</width>
<height>174</height>
<width>307</width>
<height>198</height>
</rect>
</property>
<property name="windowTitle">
<string>Export metadata as table</string>
</property>
<layout class="QGridLayout" name="gridLayout" rowstretch="0,0,0,1,0" columnstretch="0,1">
<item row="1" column="1">
<layout class="QGridLayout" name="gridLayout" rowstretch="0,0,0,0,0,0,0,0" columnstretch="0,0,0,0">
<item row="1" column="0" colspan="3">
<widget class="QLabel" name="label">
<property name="text">
<string>One line per:</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="label_2">
<property name="text">
<string>About to export tab delimited file.
Only parameters marked &quot;export&quot;
will be included in export.</string>
<item row="6" column="1" colspan="3">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="4" column="0" colspan="2">
<item row="7" column="1" colspan="3">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Save</set>
</property>
<property name="centerButtons">
<bool>false</bool>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="2" column="0" colspan="4">
<widget class="QComboBox" name="comboBoxLevel">
<item>
<property name="text">
Expand All @@ -64,18 +71,32 @@ will be included in export.</string>
</item>
</widget>
</item>
<item row="3" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
<item row="0" column="0" colspan="4">
<widget class="QLabel" name="label_2">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;About to export tab delimited file. Only parameters marked &amp;quot;export&amp;quot;will be included in export.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
<property name="scaledContents">
<bool>false</bool>
</property>
</spacer>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="4" column="0" colspan="2">
<widget class="QPushButton" name="pushButtonCopy">
<property name="text">
<string>copy to clipboard</string>
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<widget class="QCheckBox" name="checkBoxSystemLocale">
<property name="text">
<string>use system default formatting</string>
</property>
</widget>
</item>
</layout>
</widget>
Expand Down
42 changes: 30 additions & 12 deletions QtPMbrowser/pmbrowserwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#define _CRT_SECURE_NO_WARNINGS // get rid of some unnecessary warnings
#include <QApplication>
#include <QGuiApplication>
#include <QClipboard>
#include <QSettings>
#include <QFileDialog>
#include <QFileInfo>
Expand Down Expand Up @@ -802,6 +804,11 @@ void PMbrowserWindow::on_actionExport_Metadata_as_Table_triggered()
}
DlgExportMetadata dlg(this);
if (dlg.exec()) {
std::locale old_locale;
if (dlg.useSystemLocale()) {
std::locale new_locale(""); // system default locale
old_locale = std::locale::global(new_locale);
}
auto selected = dlg.getSelection();
if (selected < 0)
{
Expand All @@ -810,20 +817,31 @@ void PMbrowserWindow::on_actionExport_Metadata_as_Table_triggered()
else {
++selected; // first item in box is level 1
}
auto export_file_name = QFileDialog::getSaveFileName(this, "Export Metadata as TXT",
lastexportpath, "tab separated file (*.txt *.csv)");
if (export_file_name.length() == 0) return;
std::ofstream export_file(export_file_name.toStdString());
if (!export_file) {
QMessageBox::warning(this, "Error",
QString("Cannot open file '%1'\nfor saving").arg(export_file_name));
return;
if (dlg.doCopy()) {
std::ostringstream s;
this->formatStimMetadataAsTableExport(s, selected);
QGuiApplication::clipboard()->setText(s.str().c_str());
}
try {
this->formatStimMetadataAsTableExport(export_file, selected);
else {
auto export_file_name = QFileDialog::getSaveFileName(this, "Export Metadata as TXT",
lastexportpath, "tab separated file (*.txt *.csv)");
if (export_file_name.length() > 0) {
std::ofstream export_file(export_file_name.toStdString());
if (!export_file) {
QMessageBox::warning(this, "Error",
QString("Cannot open file '%1'\nfor saving").arg(export_file_name));
return;
}
try {
this->formatStimMetadataAsTableExport(export_file, selected);
}
catch (const std::exception& e) {
QMessageBox::warning(this, "Error while exporting", e.what());
}
}
}
catch (const std::exception& e) {
QMessageBox::warning(this, "Error while exporting", e.what());
if (dlg.useSystemLocale()) {
std::locale::global(old_locale);
}
}
}
Expand Down

0 comments on commit 9318f4e

Please sign in to comment.