Skip to content

Commit

Permalink
use the signal name to carry through to various save files #456
Browse files Browse the repository at this point in the history
  • Loading branch information
3ll3d00d committed Jun 18, 2023
1 parent 207735c commit 8f5617e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 27 deletions.
49 changes: 34 additions & 15 deletions src/main/python/app.py
Expand Up @@ -621,19 +621,30 @@ def exportFilter(self):
'''
Allows the user to save the current filter to a file.
'''
dialog = QFileDialog(parent=self)
dialog.setFileMode(QFileDialog.AnyFile)
dialog.setNameFilter(f"*.filter")
dialog.setWindowTitle(f"Save Filter")
dialog.setLabelText(QFileDialog.Accept, 'Save')
if dialog.exec():
selected = dialog.selectedFiles()
if len(selected) > 0:
if not selected[0].endswith('.filter'):
selected[0] += '.filter'
with open(selected[0], 'w+') as outfile:
json.dump(self.__filter_model.filter.to_json(), outfile)
self.statusbar.showMessage(f"Saved filter to {outfile.name}")
signal_name = None
filter_path = None
selected_signal = self.__get_selected_signal()
if selected_signal:
from model.signal import SIGNAL_SOURCE_FILE
p = selected_signal.metadata.get(SIGNAL_SOURCE_FILE, '')
if p:
from pathlib import Path
filter_path = str((Path(p).absolute().parent / selected_signal.name).with_suffix('.filter'))
signal_name = selected_signal.name
if filter_path:
file_name, _ = QFileDialog.getSaveFileName(self, caption="Save Filter", directory=filter_path,
filter="*.filter")
elif signal_name:
file_name, _ = QFileDialog.getSaveFileName(self, caption="Save Filter", directory=signal_name,
filter="*.filter")
else:
file_name, _ = QFileDialog.getSaveFileName(self, caption="Save Filter", filter="*.filter")
if file_name:
if not file_name.endswith('.filter'):
file_name += '.filter'
with open(file_name, 'w+') as outfile:
json.dump(self.__filter_model.filter.to_json(), outfile)
self.statusbar.showMessage(f"Saved filter to {outfile.name}")

def exportChart(self):
'''
Expand Down Expand Up @@ -1126,12 +1137,13 @@ class SaveChartDialog(QDialog, Ui_saveChartDialog):
Save Chart dialog
'''

def __init__(self, parent, name, figure, processor, statusbar=None):
def __init__(self, parent, name, figure, processor, signal_data, statusbar=None):
super(SaveChartDialog, self).__init__(parent)
self.setupUi(self)
self.name = name
self.figure = figure
self.processor = processor
self.signal_data = signal_data
self.__x, self.__y = processor.get_dims(self.figure)
self.__aspectRatio = self.__x / self.__y
self.widthPixels.setValue(self.__x)
Expand All @@ -1141,7 +1153,14 @@ def __init__(self, parent, name, figure, processor, statusbar=None):

def accept(self):
formats = "Portable Network Graphic (*.png)"
file_name = self.__dialog.getSaveFileName(self, 'Export Chart', f"{self.name}.png", formats)
output = f'{self.name}.png'
if self.signal_data:
from model.signal import SIGNAL_SOURCE_FILE
p = self.signal_data.signal.metadata.get(SIGNAL_SOURCE_FILE, '')
if p:
from pathlib import Path
output = str((Path(p).absolute().parent / self.signal_data.name).with_suffix('.png'))
file_name = self.__dialog.getSaveFileName(self, 'Export Chart', output, formats)
if file_name:
output_file = str(file_name[0]).strip()
if len(output_file) == 0:
Expand Down
13 changes: 9 additions & 4 deletions src/main/python/model/analysis.py
Expand Up @@ -17,7 +17,8 @@
from model.preferences import GRAPH_X_MIN, GRAPH_X_MAX, POINT, ELLIPSE, SPECTROGRAM_CONTOURED, SPECTROGRAM_FLAT, \
AUDIO_ANALYSIS_MARKER_SIZE, AUDIO_ANALYSIS_MARKER_TYPE, AUDIO_ANALYSIS_ELLIPSE_WIDTH, AUDIO_ANALYSIS_ELLIPSE_HEIGHT, \
AUDIO_ANALYIS_MIN_FREQ, AUDIO_ANALYIS_MAX_UNFILTERED_FREQ, AUDIO_ANALYIS_MAX_FILTERED_FREQ, \
AUDIO_ANALYSIS_COLOUR_MAX, AUDIO_ANALYSIS_COLOUR_MIN, AUDIO_ANALYSIS_SIGNAL_MIN, AUDIO_ANALYSIS_GEOMETRY
AUDIO_ANALYSIS_COLOUR_MAX, AUDIO_ANALYSIS_COLOUR_MIN, AUDIO_ANALYSIS_SIGNAL_MIN, AUDIO_ANALYSIS_GEOMETRY, \
EXTRACTION_OUTPUT_DIR
from model.signal import select_file, readWav
from ui.analysis import Ui_analysisDialog

Expand Down Expand Up @@ -203,18 +204,22 @@ def __get_signal_data(self, signal_name):
def save_chart(self):
''' opens the save chart dialog '''
from app import SaveChartDialog, MatplotlibExportProcessor
selected_data = self.__get_signal_data(self.leftSignal.currentText())
if self.analysisTabs.currentIndex() == 0:
SaveChartDialog(self, 'peak spectrum', self.spectrumChart.canvas.figure,
MatplotlibExportProcessor(self.spectrumChart.canvas.figure)).exec()
MatplotlibExportProcessor(self.spectrumChart.canvas.figure),
selected_data).exec()
elif self.analysisTabs.currentIndex() == 1:
SaveChartDialog(self, 'waveform', self.waveformChart.canvas.figure,
MatplotlibExportProcessor(self.waveformChart.canvas.figure)).exec()
MatplotlibExportProcessor(self.waveformChart.canvas.figure),
selected_data).exec()

def select_wav_file(self):
'''
Allows the user to select a file and laods info about it
'''
file = select_file(self, ['wav', 'flac'])
out_dir = self.__preferences.get(EXTRACTION_OUTPUT_DIR)
file = select_file(self, ['wav', 'flac'], dir=out_dir)
if file is not None:
self.__clear()
self.file.setText(file)
Expand Down
11 changes: 9 additions & 2 deletions src/main/python/model/report.py
Expand Up @@ -442,7 +442,7 @@ def __table_print(self, filt):
filter_type += f" x{len(filt)}"
vals.append(filter_type)
if gain != 0 and len(filt) > 1:
vals.append(f"{len(filt)*gain:+g}{g_suffix}")
vals.append(f"{len(filt) * gain:+g}{g_suffix}")
else:
vals.append('')
return vals
Expand Down Expand Up @@ -505,7 +505,14 @@ def accept(self):
def __save_report(self):
''' writes the figure to the specified format '''
formats = "Report Files (*.png *.jpg *.jpeg)"
file_name = QFileDialog.getSaveFileName(parent=self, caption='Export Report', filter=formats)
from model.signal import SIGNAL_SOURCE_FILE
p = self.__selected_signal.metadata.get(SIGNAL_SOURCE_FILE, '')
report_path = None
if p:
from pathlib import Path
report_path = str((Path(p).absolute().parent / self.__selected_signal.name).with_suffix('.png'))
file_name = QFileDialog.getSaveFileName(parent=self, caption='Export Report', directory=report_path,
filter=formats)
if file_name:
output_file = str(file_name[0]).strip()
if len(output_file) == 0:
Expand Down
17 changes: 11 additions & 6 deletions src/main/python/model/signal.py
Expand Up @@ -24,7 +24,7 @@
DISPLAY_SHOW_SIGNALS, DISPLAY_SHOW_FILTERED_SIGNALS, ANALYSIS_TARGET_FS, BASS_MANAGEMENT_LPF_FS, \
BASS_MANAGEMENT_LPF_POSITION, BM_LPF_BEFORE, BM_LPF_AFTER, DISPLAY_SMOOTH_PRECALC, X_RESOLUTION, \
SHOWING_AVERAGE, SHOWING_PEAK, SHOWING_MEDIAN, SHOW_UNFILTERED_ONLY, ANALYSIS_WINDOW_DEFAULT, \
ANALYSIS_RESOLUTION_DEFAULT
ANALYSIS_RESOLUTION_DEFAULT, EXTRACTION_OUTPUT_DIR
from model.xy import MagnitudeData, interp
from ui.merge_signals import Ui_MergeSignalDialog
from ui.signal import Ui_addSignalDialog
Expand Down Expand Up @@ -113,7 +113,7 @@ def __init__(self, name=None, fs=None, filter=None, xy_data=None, duration_secon
self.__smoothing_type = None
self.__filter = None
self.__name = None
self.__signal = signal
self.__signal: Signal = signal
self.slaves = []
self.master = None
self.reference_name = None
Expand Down Expand Up @@ -207,14 +207,14 @@ def smoothing_type(self):
return self.__smoothing_type if self.signal is not None else None

@property
def signal(self):
def signal(self) -> 'Signal':
'''
:return: the underlying sample data (may not exist for signals loaded from txt).
'''
return self.__signal

@signal.setter
def signal(self, signal):
def signal(self, signal: 'Signal'):
self.__signal = signal

@property
Expand Down Expand Up @@ -1478,7 +1478,7 @@ def headerData(self, section: int, orientation: Qt.Orientation, role: int = ...)
return QVariant()


def select_file(owner, file_types):
def select_file(owner, file_types, dir=None):
'''
Presents a file picker for selecting a file that contains a signal.
'''
Expand All @@ -1487,6 +1487,8 @@ def select_file(owner, file_types):
filt = ' '.join([f"*.{f}" for f in file_types])
dialog.setNameFilter(f"Audio ({filt})")
dialog.setWindowTitle(f"Select Signal File")
if dir:
dialog.setDirectory(dir)
if dialog.exec():
selected = dialog.selectedFiles()
if len(selected) > 0:
Expand Down Expand Up @@ -1645,7 +1647,8 @@ def toggle_decimate(self, channel_idx):
self.prepare_signal(channel_idx)

def select_wav_file(self):
file = select_file(self.__dialog, ['wav', 'flac'])
out_dir = self.__preferences.get(EXTRACTION_OUTPUT_DIR)
file = select_file(self.__dialog, ['wav', 'flac'], dir=out_dir)
if file is not None:
self.clear_signal()
self.__dialog.wavFile.setText(file)
Expand Down Expand Up @@ -1683,6 +1686,8 @@ def __load_info(self):
self.__dialog.wavEndTime.setTime(QtCore.QTime(0, 0, 0).addMSecs(self.__duration))
self.__dialog.wavEndTime.setEnabled(True)
self.__dialog.wavSignalName.setEnabled(True)
from pathlib import Path
self.__dialog.wavSignalName.setText(str(Path(Path(info.name).name).stem))
self.prepare_signal(int(self.__dialog.wavChannelSelector.currentText()))
self.__dialog.applyTimeRangeButton.setEnabled(False)

Expand Down

0 comments on commit 8f5617e

Please sign in to comment.