Skip to content
This repository has been archived by the owner on Jul 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #280 from nmearl/master
Browse files Browse the repository at this point in the history
Fix unit crash bug, add validation
  • Loading branch information
nmearl authored Mar 31, 2017
2 parents c245cc3 + 495db05 commit bc30345
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 9 deletions.
6 changes: 5 additions & 1 deletion specviz/plugins/data_list_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ def remove_data_item(self, data=None):

@DispatchHandle.register_listener("on_remove_all_data")
def remove_all_data(self):
self.list_widget_data_list.clear()
print('*' * 100, self.list_widget_data_list.count())
for i in range(self.list_widget_data_list.count()):
item = self.list_widget_data_list.takeItem(i - 1)
print(i)
item.deleteLayer()

def get_data_item(self, data):
for i in range(self.list_widget_data_list.count()):
Expand Down
1 change: 1 addition & 0 deletions specviz/plugins/layer_list_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ def __init__(self, plugin):
plugin.button_export.setEnabled(False)
plugin.button_export.setMinimumSize(QSize(35, 35))
plugin.button_export.setIconSize(QSize(25, 25))
plugin.button_export.setToolTip("Export spectrum to a file.")

plugin.layout_horizontal.addWidget(plugin.button_layer_arithmetic)
plugin.layout_horizontal.addWidget(plugin.button_copy_model)
Expand Down
6 changes: 3 additions & 3 deletions specviz/plugins/plot_tools_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ def setup_connections(self):

def _show_unit_change_dialog(self):
# Populate the text fields with the current units
self._unit_change_dialog.line_edit_flux_unit.setText("{}".format(self.current_layer.unit))
self._unit_change_dialog.line_edit_disp_unit.setText("{}".format(self.current_layer.dispersion_unit))
self._unit_change_dialog.set_layer(self.current_layer)

if self._unit_change_dialog.exec_():
x_text = self._unit_change_dialog.disp_unit
y_text = self._unit_change_dialog.flux_unit

x_unit = y_unit = None
x_unit = self.current_layer.dispersion_unit
y_unit = self.current_layer.unit

try:
x_unit = Unit(x_text) if x_text else None
Expand Down
101 changes: 96 additions & 5 deletions specviz/ui/widgets/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from qtpy.QtGui import *
from qtpy.QtCore import *

from astropy.units import Unit
from astropy.units import Quantity, LogQuantity, LogUnit, spectral_density, spectral

import logging


Expand Down Expand Up @@ -209,19 +212,107 @@ def __init__(self, parent=None):
class UnitChangeDialog(UiUnitChangeDialog):
def __init__(self, parent=None):
super(UnitChangeDialog, self).__init__(parent)
self._layer = None
self._flux_unit = None
self._disp_unit = None

self.flux_unit = ''
self.disp_unit = ''
self.line_edit_disp_unit.textChanged.connect(self.check_state)
self.line_edit_disp_unit.textChanged.emit(self.line_edit_disp_unit.text())

def accept(self):
self.flux_unit = self.line_edit_flux_unit.text()
self.disp_unit = self.line_edit_disp_unit.text()
self.line_edit_flux_unit.textChanged.connect(self.check_state)
self.line_edit_flux_unit.textChanged.emit(self.line_edit_flux_unit.text())

def set_layer(self, layer):
self._layer = layer

self.line_edit_flux_unit.setValidator(DataUnitValidator(self._layer))
self.line_edit_disp_unit.setValidator(DispersionUnitValidator(self._layer))

self.line_edit_disp_unit.setText("{}".format(self._layer.dispersion_unit))
self.line_edit_flux_unit.setText("{}".format(self._layer.unit))

@property
def disp_unit(self):
return self.line_edit_disp_unit.text()

@property
def flux_unit(self):
return self.line_edit_flux_unit.text()

def accept(self):
super(UnitChangeDialog, self).accept()

def reject(self):
super(UnitChangeDialog, self).reject()

def check_state(self, *args, **kwargs):
sender = self.sender()

validator = sender.validator()

if validator is None:
return

state = validator.validate(sender.text(), 0)[0]

if state == QValidator.Acceptable:
color = '#c4df9b' # green
elif state == QValidator.Intermediate:
color = '#fff79a' # yellow
else:
color = '#f6989d' # red

sender.setStyleSheet('QLineEdit { background-color: %s }' % color)


class DataUnitValidator(QValidator):
def __init__(self, layer, *args, **kwargs):
super(DataUnitValidator, self).__init__(*args, **kwargs)

self._layer = layer

def validate(self, p_str, p_int):
try:
unit = Unit(p_str)

if unit.is_equivalent(self._layer.unit,
equivalencies=spectral_density(
self._layer.dispersion.data)):
return (QValidator.Acceptable, p_str, p_int)
else:
return (QValidator.Intermediate, p_str, p_int)
except ValueError as e:
return (QValidator.Intermediate, p_str, p_int)

return (QValidator.Invalid, p_str, p_int)

def fixup(self, p_str):
p_str.replace(p_str, "{}".format(self._layer.unit))


class DispersionUnitValidator(QValidator):
def __init__(self, layer, *args, **kwargs):
super(DispersionUnitValidator, self).__init__(*args, **kwargs)

self._layer = layer

def validate(self, p_str, p_int):
try:
unit = Unit(p_str)

if unit.is_equivalent(self._layer.dispersion_unit,
equivalencies=spectral()):
return (QValidator.Acceptable, p_str, p_int)
else:
return (QValidator.Intermediate, p_str, p_int)
except ValueError as e:
return (QValidator.Intermediate, p_str, p_int)

return (QValidator.Invalid, p_str, p_int)

def fixup(self, p_str):
p_str.replace(p_str, "{}".format(self._layer.dispersion_unit))


class UiSmoothingDialog(QDialog):
"""
Expand Down

0 comments on commit bc30345

Please sign in to comment.