Skip to content

Commit

Permalink
Basic Text Export of Runs (#306)
Browse files Browse the repository at this point in the history
* Add basic export as text option.

* Don't use native file dialog.
  • Loading branch information
trisyoungs committed Feb 29, 2024
1 parent 5d69e79 commit 42a7c47
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions frontend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ qt6_add_executable(
args.h
data.cpp
errorHandling.cpp
export.cpp
filtering.cpp
finding.cpp
generation.cpp
Expand Down
58 changes: 58 additions & 0 deletions frontend/export.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2024 Team JournalViewer and contributors

#include "mainWindow.h"
#include <QDir>
#include <QFileDialog>
#include <QMessageBox>

void MainWindow::exportRunDataAsText()
{
// Save selection or all items?
auto saveSelectionOnly =
ui_.RunDataTable->selectionModel()->selection().count() > 0 &&
QMessageBox::question(this, "Export as Text",
"There are selected items in the table - would you like to export just these?",
QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes;

// Get a file name to save under
static QDir currentDirectory;
auto fileName = QFileDialog::getSaveFileName(this, "Save data as text file", currentDirectory.path(), {}, {},
QFileDialog::DontUseNativeDialog);
if (fileName.isEmpty())
return;

// Save dir for later
currentDirectory.setPath(fileName);

// Open the file
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream textStream(&file);

// Get selected runs (we might not use them)
auto selectedRuns = ui_.RunDataTable->selectionModel()->selectedRows();
auto nData = saveSelectionOnly ? selectedRuns.size() : runDataFilterProxy_.rowCount();
for (auto i = 0; i < nData; ++i)
{
// Get the run number displayed in ith row or the ith selected model index
auto runNo =
runDataFilterProxy_.getData("run_number", saveSelectionOnly ? selectedRuns[i] : runDataFilterProxy_.index(i, 0));
for (auto col = 0; col < runDataFilterProxy_.columnCount(); ++col)
{
textStream << runDataFilterProxy_
.data(runDataFilterProxy_.index(saveSelectionOnly ? selectedRuns[i].row() : i, col))
.toString()
<< " ";
}
}

file.close();
}

/*
* UI
*/

void MainWindow::on_actionExportAsText_triggered() { exportRunDataAsText(); }
10 changes: 10 additions & 0 deletions frontend/mainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ class MainWindow : public QMainWindow
protected:
void closeEvent(QCloseEvent *event) override;

/*
* Export
*/
private:
// Export run data as text
void exportRunDataAsText();

private slots:
void on_actionExportAsText_triggered();

/*
* Journal Sources
*/
Expand Down
17 changes: 17 additions & 0 deletions frontend/mainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,18 @@
<property name="title">
<string>&amp;File</string>
</property>
<widget class="QMenu" name="menuExport">
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="title">
<string>Export</string>
</property>
<addaction name="actionExportAsText"/>
</widget>
<addaction name="menuExport"/>
<addaction name="separator"/>
<addaction name="actionQuit"/>
</widget>
Expand Down Expand Up @@ -845,6 +857,11 @@
<string>Regenerate Source...</string>
</property>
</action>
<action name="actionExportAsText">
<property name="text">
<string>As Text</string>
</property>
</action>
</widget>
<resources>
<include location="resources.qrc"/>
Expand Down

0 comments on commit 42a7c47

Please sign in to comment.