From 017683806b0067d7a1deffc42d8fa567f6861cd8 Mon Sep 17 00:00:00 2001 From: Marcel Date: Wed, 1 Aug 2018 13:49:01 +0200 Subject: [PATCH] [new] if QSettings fails, ask to reset! [bugfix] checkBoxPG outdated in QSettings [bugfix] moved SetChannelComponents to utils (produced circular imports) --- PyLoT.py | 50 +++++++++++++++++++++------------- pylot/core/pick/utils.py | 3 +-- pylot/core/util/defaults.py | 51 ----------------------------------- pylot/core/util/utils.py | 53 +++++++++++++++++++++++++++++++++++++ pylot/core/util/widgets.py | 24 +++-------------- 5 files changed, 89 insertions(+), 92 deletions(-) diff --git a/PyLoT.py b/PyLoT.py index 04b5b509..54ede24c 100755 --- a/PyLoT.py +++ b/PyLoT.py @@ -67,7 +67,7 @@ from pylot.core.pick.compare import Comparison from pylot.core.pick.utils import symmetrize_error, getQualityFromUncertainty, getPickQuality from pylot.core.io.phases import picksdict_from_picks import pylot.core.loc.nll as nll -from pylot.core.util.defaults import FILTERDEFAULTS, SetChannelComponents +from pylot.core.util.defaults import FILTERDEFAULTS from pylot.core.util.errors import DatastructureError, \ OverwriteError from pylot.core.util.connection import checkurl @@ -76,7 +76,7 @@ from pylot.core.util.utils import fnConstructor, getLogin, \ full_range, readFilterInformation, trim_station_components, check4gaps, make_pen, pick_color_plt, \ pick_linestyle_plt, remove_underscores, check4doubled, identifyPhaseID, excludeQualityClasses, \ check4rotated, transform_colors_mpl, transform_colors_mpl_str, getAutoFilteroptions, check_all_obspy, \ - check_all_pylot, real_Bool + check_all_pylot, real_Bool, SetChannelComponents from pylot.core.util.event import Event from pylot.core.io.location import create_creation_info, create_event from pylot.core.util.widgets import FilterOptionsDialog, NewEventDlg, \ @@ -155,15 +155,32 @@ class MainWindow(QMainWindow): self.data._new = False self.autodata = Data(self) - if settings.value("user/FullName", None) is None: - fulluser = QInputDialog.getText(self, "Enter Name:", "Full name") - settings.setValue("user/FullName", fulluser) - settings.setValue("user/Login", getLogin()) - if settings.value("agency_id", None) is None: - agency = QInputDialog.getText(self, - "Enter authority/institution name:", - "Authority") - settings.setValue("agency_id", agency) + while True: + try: + if settings.value("user/FullName", None) is None: + fulluser = QInputDialog.getText(self, "Enter Name:", "Full name") + settings.setValue("user/FullName", fulluser) + settings.setValue("user/Login", getLogin()) + if settings.value("agency_id", None) is None: + agency = QInputDialog.getText(self, + "Enter authority/institution name:", + "Authority") + settings.setValue("agency_id", agency) + break + except Exception as e: + qmb = QMessageBox(self, icon=QMessageBox.Question, + text='Could not init application settings: {}.' + 'Do you want to reset application settings?'.format(e), + windowTitle='PyLoT - Init QSettings warning') + qmb.setStandardButtons(QMessageBox.Yes | QMessageBox.No) + qmb.setDefaultButton(QMessageBox.No) + ret = qmb.exec_() + if ret == qmb.Yes: + settings.clear() + else: + sys.exit() + print('Settings cleared!') + self.fnames = None self._stime = None structure_setting = settings.value("data/Structure", "PILOT") @@ -671,14 +688,9 @@ class MainWindow(QMainWindow): xlab = self.startTime.strftime('seconds since %Y/%m/%d %H:%M:%S (%Z)') plottitle = None # "Overview: {0} components ".format(self.getComponent()) self.disconnectWFplotEvents() - if str(settings.value('pyqtgraphic')) == 'false' or not pg: - self.pg = False - self.dataPlot = PylotCanvas(parent=self, connect_events=False, multicursor=True) - self.dataPlot.updateWidget(xlab, None, plottitle) - else: - self.pg = pg - self.dataPlot = WaveformWidgetPG(parent=self, - title=plottitle) + self.pg = pg + self.dataPlot = WaveformWidgetPG(parent=self, + title=plottitle) self.dataPlot.setCursor(Qt.CrossCursor) self.wf_scroll_area.setWidget(self.dataPlot) if self.get_current_event(): diff --git a/pylot/core/pick/utils.py b/pylot/core/pick/utils.py index 11d10aa9..20385535 100644 --- a/pylot/core/pick/utils.py +++ b/pylot/core/pick/utils.py @@ -13,8 +13,7 @@ import warnings import matplotlib.pyplot as plt import numpy as np from obspy.core import Stream, UTCDateTime -from pylot.core.util.utils import real_Bool, real_None -from pylot.core.util.defaults import SetChannelComponents +from pylot.core.util.utils import real_Bool, real_None, SetChannelComponents def earllatepicker(X, nfac, TSNR, Pick1, iplot=0, verbosity=1, fig=None, linecolor='k'): diff --git a/pylot/core/util/defaults.py b/pylot/core/util/defaults.py index 416cbd58..04ae60a4 100644 --- a/pylot/core/util/defaults.py +++ b/pylot/core/util/defaults.py @@ -41,54 +41,3 @@ OUTPUTFORMATS = {'.xml': 'QUAKEML', LOCTOOLS = dict(nll=nll, hyposat=hyposat, velest=velest, hypo71=hypo71, hypodd=hypodd) -class SetChannelComponents(object): - def __init__(self): - self.setDefaultCompPosition() - - def setDefaultCompPosition(self): - # default component order - self.compPosition_Map = dict(Z=2, N=1, E=0) - self.compName_Map = {'3': 'Z', - '1': 'N', - '2': 'E'} - - def _getCurrentPosition(self, component): - for key, value in self.compName_Map.items(): - if value == component: - return key, value - errMsg = 'getCurrentPosition: Could not find former position of component {}.'.format(component) - raise ValueError(errMsg) - - def _switch(self, component, component_alter): - # Without switching, multiple definitions of the same alter_comp are possible - old_alter_comp, _ = self._getCurrentPosition(component) - old_comp = self.compName_Map[component_alter] - if not old_alter_comp == component_alter and not old_comp == component: - self.compName_Map[old_alter_comp] = old_comp - print('switch: Automatically switched component {} to {}'.format(old_alter_comp, old_comp)) - - def setCompPosition(self, component_alter, component, switch=True): - component_alter = str(component_alter) - if not component_alter in self.compName_Map.keys(): - errMsg = 'setCompPosition: Unrecognized alternative component {}. Expecting one of {}.' - raise ValueError(errMsg.format(component_alter, self.compName_Map.keys())) - if not component in self.compPosition_Map.keys(): - errMsg = 'setCompPosition: Unrecognized target component {}. Expecting one of {}.' - raise ValueError(errMsg.format(component, self.compPosition_Map.keys())) - print('setCompPosition: set component {} to {}'.format(component_alter, component)) - if switch: - self._switch(component, component_alter) - self.compName_Map[component_alter] = component - - def getCompPosition(self, component): - return self._getCurrentPosition(component)[0] - - def getPlotPosition(self, component): - component = str(component) - if component in self.compPosition_Map.keys(): - return self.compPosition_Map[component] - elif component in self.compName_Map.keys(): - return self.compPosition_Map[self.compName_Map[component]] - else: - errMsg = 'getCompPosition: Unrecognized component {}. Expecting one of {} or {}.' - raise ValueError(errMsg.format(component, self.compPosition_Map.keys(), self.compName_Map.keys())) diff --git a/pylot/core/util/utils.py b/pylot/core/util/utils.py index abf10921..6d8dc235 100644 --- a/pylot/core/util/utils.py +++ b/pylot/core/util/utils.py @@ -1214,3 +1214,56 @@ if __name__ == "__main__": import doctest doctest.testmod() + + +class SetChannelComponents(object): + def __init__(self): + self.setDefaultCompPosition() + + def setDefaultCompPosition(self): + # default component order + self.compPosition_Map = dict(Z=2, N=1, E=0) + self.compName_Map = {'3': 'Z', + '1': 'N', + '2': 'E'} + + def _getCurrentPosition(self, component): + for key, value in self.compName_Map.items(): + if value == component: + return key, value + errMsg = 'getCurrentPosition: Could not find former position of component {}.'.format(component) + raise ValueError(errMsg) + + def _switch(self, component, component_alter): + # Without switching, multiple definitions of the same alter_comp are possible + old_alter_comp, _ = self._getCurrentPosition(component) + old_comp = self.compName_Map[component_alter] + if not old_alter_comp == component_alter and not old_comp == component: + self.compName_Map[old_alter_comp] = old_comp + print('switch: Automatically switched component {} to {}'.format(old_alter_comp, old_comp)) + + def setCompPosition(self, component_alter, component, switch=True): + component_alter = str(component_alter) + if not component_alter in self.compName_Map.keys(): + errMsg = 'setCompPosition: Unrecognized alternative component {}. Expecting one of {}.' + raise ValueError(errMsg.format(component_alter, self.compName_Map.keys())) + if not component in self.compPosition_Map.keys(): + errMsg = 'setCompPosition: Unrecognized target component {}. Expecting one of {}.' + raise ValueError(errMsg.format(component, self.compPosition_Map.keys())) + print('setCompPosition: set component {} to {}'.format(component_alter, component)) + if switch: + self._switch(component, component_alter) + self.compName_Map[component_alter] = component + + def getCompPosition(self, component): + return self._getCurrentPosition(component)[0] + + def getPlotPosition(self, component): + component = str(component) + if component in self.compPosition_Map.keys(): + return self.compPosition_Map[component] + elif component in self.compName_Map.keys(): + return self.compPosition_Map[self.compName_Map[component]] + else: + errMsg = 'getCompPosition: Unrecognized component {}. Expecting one of {} or {}.' + raise ValueError(errMsg.format(component, self.compPosition_Map.keys(), self.compName_Map.keys())) \ No newline at end of file diff --git a/pylot/core/util/widgets.py b/pylot/core/util/widgets.py index fd706d07..eead217e 100644 --- a/pylot/core/util/widgets.py +++ b/pylot/core/util/widgets.py @@ -49,13 +49,12 @@ from pylot.core.io.inputs import FilterOptions, PylotParameter from pylot.core.pick.utils import getSNR, earllatepicker, getnoisewin, \ getResolutionWindow, getQualityFromUncertainty from pylot.core.pick.compare import Comparison -from pylot.core.util.defaults import OUTPUTFORMATS, FILTERDEFAULTS, \ - SetChannelComponents +from pylot.core.util.defaults import OUTPUTFORMATS, FILTERDEFAULTS from pylot.core.util.utils import prepTimeAxis, full_range, scaleWFData, \ demeanTrace, isSorted, findComboBoxIndex, clims, pick_linestyle_plt, pick_color_plt, \ check4rotated, check4doubled, check4gaps, remove_underscores, find_horizontals, identifyPhase, \ loopIdentifyPhase, trim_station_components, transformFilteroptions2String, \ - identifyPhaseID, real_Bool, pick_color, getAutoFilteroptions + identifyPhaseID, real_Bool, pick_color, getAutoFilteroptions, SetChannelComponents from autoPyLoT import autoPyLoT from pylot.core.util.thread import Thread from pylot.core.util.dataprocessing import Metadata @@ -4617,31 +4616,16 @@ class GraphicsTab(PropTab): self.main_layout.addWidget(label, 1, 0) self.main_layout.addWidget(self.spinbox_nth_sample, 1, 1) - def add_pg_cb(self): - pg = True - text = {True: 'Use pyqtgraphic library for plotting', - False: 'Cannot use library: pyqtgraphic not found on system'} - label = QLabel('PyQt graphic') - label.setToolTip(text[bool(pg)]) - label.setEnabled(bool(pg)) - self.checkbox_pg = QtGui.QCheckBox() - self.checkbox_pg.setEnabled(bool(pg)) - self.checkbox_pg.setChecked(bool(pg)) - self.main_layout.addWidget(label, 0, 0) - self.main_layout.addWidget(self.checkbox_pg, 0, 1) - def set_current_style(self): selected_style = self.style_cb.currentText() self.pylot_mainwindow.set_style(selected_style) def getValues(self): - values = {'nth_sample': self.spinbox_nth_sample.value(), - 'pyqtgraphic': self.checkbox_pg.isChecked()} + values = {'nth_sample': self.spinbox_nth_sample.value()} return values def resetValues(self, infile=None): - values = {'nth_sample': self.spinbox_nth_sample.setValue(1), - 'pyqtgraphic': self.checkbox_pg.setChecked(True)} + values = {'nth_sample': self.spinbox_nth_sample.setValue(1)} return values