Commit e10a83d6 authored by Josef Brandt's avatar Josef Brandt

Fix in Setting SpinBoxes and Log Preview can handle negative values

parent 2e588d5b
...@@ -5,8 +5,8 @@ Created on Fri Oct 25 09:39:14 2019 ...@@ -5,8 +5,8 @@ Created on Fri Oct 25 09:39:14 2019
@author: brandt @author: brandt
""" """
from PyQt5 import QtWidgets, QtCore from PyQt5 import QtWidgets, QtCore
import sys
import numpy as np import numpy as np
import math
class ZLevelSetter(QtWidgets.QLabel): class ZLevelSetter(QtWidgets.QLabel):
...@@ -21,9 +21,9 @@ class ZLevelSetter(QtWidgets.QLabel): ...@@ -21,9 +21,9 @@ class ZLevelSetter(QtWidgets.QLabel):
super(ZLevelSetter, self).__init__() super(ZLevelSetter, self).__init__()
layout = QtWidgets.QVBoxLayout() layout = QtWidgets.QVBoxLayout()
self.setLayout(layout) self.setLayout(layout)
self.radioGroup = QtWidgets.QButtonGroup() self.radioGroup = QtWidgets.QButtonGroup()
layout.addWidget(self._createRangeGroupBox()) layout.addWidget(self._createRangeGroupBox())
layout.addLayout(self._createLevelSelectionLayout()) layout.addLayout(self._createLevelSelectionLayout())
...@@ -36,10 +36,10 @@ class ZLevelSetter(QtWidgets.QLabel): ...@@ -36,10 +36,10 @@ class ZLevelSetter(QtWidgets.QLabel):
:return: :return:
""" """
rangeGroupBox = QtWidgets.QGroupBox('Select Range of z-steps') rangeGroupBox = QtWidgets.QGroupBox('Select Range of z-steps')
rangeGroupBox.setMinimumHeight(80)
rangesLayout = QtWidgets.QHBoxLayout() rangesLayout = QtWidgets.QHBoxLayout()
rangeGroupBox.setLayout(rangesLayout) rangeGroupBox.setLayout(rangesLayout)
rangeGroupBox.setFixedHeight(60)
self.minLevelSpinbox = QtWidgets.QSpinBox() self.minLevelSpinbox = QtWidgets.QSpinBox()
self.minLevelSpinbox.setMinimum(-1000) self.minLevelSpinbox.setMinimum(-1000)
self.minLevelSpinbox.setMaximum(1000) self.minLevelSpinbox.setMaximum(1000)
...@@ -48,23 +48,28 @@ class ZLevelSetter(QtWidgets.QLabel): ...@@ -48,23 +48,28 @@ class ZLevelSetter(QtWidgets.QLabel):
self.maxLevelSpinbox = QtWidgets.QSpinBox() self.maxLevelSpinbox = QtWidgets.QSpinBox()
self.maxLevelSpinbox.setValue(50) self.maxLevelSpinbox.setValue(50)
self._minLevelBoxChanged() self._minLevelBoxChanged()
self.numLevelsSpinbox = QtWidgets.QSpinBox() self.numLevelsSpinbox = QtWidgets.QSpinBox()
self.numLevelsSpinbox.setMinimum(1) self.numLevelsSpinbox.setMinimum(1)
self.numLevelsSpinbox.setMaximum(100) self.numLevelsSpinbox.setMaximum(100)
self.numLevelsSpinbox.setValue(5) self.numLevelsSpinbox.setValue(5)
for box in [self.minLevelSpinbox, self.maxLevelSpinbox, self.numLevelsSpinbox]: for box in [self.minLevelSpinbox, self.maxLevelSpinbox, self.numLevelsSpinbox]:
box.valueChanged.connect(self._updateLevels) box.valueChanged.connect(self._updateLevels)
rangesLayout.addWidget(QtWidgets.QLabel('z_min (µm): ')) minLabel = QtWidgets.QLabel('z_min\n(µm): ')
maxLabel = QtWidgets.QLabel(', z_max\n(µm): ')
numLabel = QtWidgets.QLabel(', Num.\nsteps: ')
rangesLayout.addWidget(minLabel)
rangesLayout.addWidget(self.minLevelSpinbox) rangesLayout.addWidget(self.minLevelSpinbox)
rangesLayout.addWidget(QtWidgets.QLabel(', z_max (µm): ')) rangesLayout.addWidget(maxLabel)
rangesLayout.addWidget(self.maxLevelSpinbox) rangesLayout.addWidget(self.maxLevelSpinbox)
rangesLayout.addWidget(QtWidgets.QLabel(', Num. steps: ')) rangesLayout.addWidget(numLabel)
rangesLayout.addWidget(self.numLevelsSpinbox) rangesLayout.addWidget(self.numLevelsSpinbox)
return rangeGroupBox return rangeGroupBox
def _createLevelSelectionLayout(self): def _createLevelSelectionLayout(self):
""" """
Creates the Parent Group for selecting mode of level distribution Creates the Parent Group for selecting mode of level distribution
...@@ -75,7 +80,7 @@ class ZLevelSetter(QtWidgets.QLabel): ...@@ -75,7 +80,7 @@ class ZLevelSetter(QtWidgets.QLabel):
modeLayout.addWidget(self._createLogGroup()) modeLayout.addWidget(self._createLogGroup())
modeLayout.addWidget(self._createCustomGroup()) modeLayout.addWidget(self._createCustomGroup())
return modeLayout return modeLayout
def _createLinGroup(self): def _createLinGroup(self):
""" """
Creates Widget group for linear level distribution Creates Widget group for linear level distribution
...@@ -85,18 +90,18 @@ class ZLevelSetter(QtWidgets.QLabel): ...@@ -85,18 +90,18 @@ class ZLevelSetter(QtWidgets.QLabel):
linLayout = QtWidgets.QVBoxLayout() linLayout = QtWidgets.QVBoxLayout()
linGroup.setLayout(linLayout) linGroup.setLayout(linLayout)
linGroup.sizePolicy().setVerticalPolicy(QtWidgets.QSizePolicy.Expanding) linGroup.sizePolicy().setVerticalPolicy(QtWidgets.QSizePolicy.Expanding)
self.linChecker = QtWidgets.QRadioButton('Linear') self.linChecker = QtWidgets.QRadioButton('Linear')
self.linChecker.setChecked(True) self.linChecker.setChecked(True)
self.linChecker.toggled.connect(self._updateLevels) self.linChecker.toggled.connect(self._updateLevels)
linLayout.addWidget(self.linChecker) linLayout.addWidget(self.linChecker)
self.linPreviewLayout = QtWidgets.QVBoxLayout() self.linPreviewLayout = QtWidgets.QVBoxLayout()
linLayout.addLayout(self.linPreviewLayout) linLayout.addLayout(self.linPreviewLayout)
self.linPreviewSteps = [] self.linPreviewSteps = []
self._updateLinPreview() self._updateLinPreview()
return linGroup return linGroup
def _createLogGroup(self): def _createLogGroup(self):
""" """
Creates Widget group for logarithmic level distribution Creates Widget group for logarithmic level distribution
...@@ -106,19 +111,19 @@ class ZLevelSetter(QtWidgets.QLabel): ...@@ -106,19 +111,19 @@ class ZLevelSetter(QtWidgets.QLabel):
logLayout = QtWidgets.QVBoxLayout() logLayout = QtWidgets.QVBoxLayout()
logGroup.setLayout(logLayout) logGroup.setLayout(logLayout)
logGroup.sizePolicy().setVerticalPolicy(QtWidgets.QSizePolicy.Expanding) logGroup.sizePolicy().setVerticalPolicy(QtWidgets.QSizePolicy.Expanding)
self.logChecker = QtWidgets.QRadioButton('Logarithmic') self.logChecker = QtWidgets.QRadioButton('Logarithmic')
self.logChecker.setChecked(False) self.logChecker.setChecked(False)
self.logChecker.toggled.connect(self._updateLevels) self.logChecker.toggled.connect(self._updateLevels)
logLayout.addWidget(self.logChecker) logLayout.addWidget(self.logChecker)
self.logPreviewLayout = QtWidgets.QVBoxLayout() self.logPreviewLayout = QtWidgets.QVBoxLayout()
logLayout.addLayout(self.logPreviewLayout) logLayout.addLayout(self.logPreviewLayout)
self.logPreviewSteps = [] self.logPreviewSteps = []
self._updateLogPreview() self._updateLogPreview()
return logGroup return logGroup
def _createCustomGroup(self): def _createCustomGroup(self):
""" """
Creates Widget group for custom level distribution Creates Widget group for custom level distribution
...@@ -128,18 +133,18 @@ class ZLevelSetter(QtWidgets.QLabel): ...@@ -128,18 +133,18 @@ class ZLevelSetter(QtWidgets.QLabel):
customLayout = QtWidgets.QVBoxLayout() customLayout = QtWidgets.QVBoxLayout()
customGroup.setLayout(customLayout) customGroup.setLayout(customLayout)
customGroup.sizePolicy().setVerticalPolicy(QtWidgets.QSizePolicy.Expanding) customGroup.sizePolicy().setVerticalPolicy(QtWidgets.QSizePolicy.Expanding)
self.customChecker = QtWidgets.QRadioButton('Custom') self.customChecker = QtWidgets.QRadioButton('Custom')
self.customChecker.setChecked(False) self.customChecker.setChecked(False)
self.customChecker.toggled.connect(self._updateLevels) self.customChecker.toggled.connect(self._updateLevels)
customLayout.addWidget(self.customChecker) customLayout.addWidget(self.customChecker)
self.customPreviewLayout = QtWidgets.QVBoxLayout() self.customPreviewLayout = QtWidgets.QVBoxLayout()
customLayout.addLayout(self.customPreviewLayout) customLayout.addLayout(self.customPreviewLayout)
self.customPreviewSteps = [] self.customPreviewSteps = []
self._updateCustomPreview() self._updateCustomPreview()
return customGroup return customGroup
def _updateLevels(self): def _updateLevels(self):
""" """
Updates the previewed levels for all modes Updates the previewed levels for all modes
...@@ -149,7 +154,7 @@ class ZLevelSetter(QtWidgets.QLabel): ...@@ -149,7 +154,7 @@ class ZLevelSetter(QtWidgets.QLabel):
self._updateLogPreview() self._updateLogPreview()
self._updateCustomPreview() self._updateCustomPreview()
self._updateSize() self._updateSize()
def _updateLinPreview(self): def _updateLinPreview(self):
""" """
Updates linear level preview Updates linear level preview
...@@ -158,32 +163,38 @@ class ZLevelSetter(QtWidgets.QLabel): ...@@ -158,32 +163,38 @@ class ZLevelSetter(QtWidgets.QLabel):
for entry in self.linPreviewSteps: for entry in self.linPreviewSteps:
entry.setParent(None) entry.setParent(None)
zRange = np.linspace(self.minLevelSpinbox.value(), self.maxLevelSpinbox.value(), self.numLevelsSpinbox.value()) zRange = np.linspace(self.minLevelSpinbox.value(), self.maxLevelSpinbox.value(), self.numLevelsSpinbox.value())
self.linPreviewSteps = [] self.linPreviewSteps = []
for val in zRange: for val in zRange:
newLabel = QtWidgets.QLabel(str(round(val, 1))) newLabel = QtWidgets.QLabel(str(round(val, 1)))
newLabel.setEnabled(self.linChecker.isChecked()) newLabel.setEnabled(self.linChecker.isChecked())
self.linPreviewSteps.append(newLabel) self.linPreviewSteps.append(newLabel)
self.linPreviewLayout.addWidget(newLabel) self.linPreviewLayout.addWidget(newLabel)
def _updateLogPreview(self): def _updateLogPreview(self):
""" """
Updates logarithmic level preview Updates logarithmic level preview
:return: :return:
""" """
base = 2
for entry in self.logPreviewSteps: for entry in self.logPreviewSteps:
entry.setParent(None) entry.setParent(None)
zmin = self.minLevelSpinbox.value() zmin = self.minLevelSpinbox.value()
zmax = np.log2(self.maxLevelSpinbox.value()) zmax = self.maxLevelSpinbox.value()
zRange = np.logspace(zmin, zmax, self.numLevelsSpinbox.value(), base=2) diff = -(zmin-1)
zmin = math.log((zmin+diff), base)
zmax = math.log((zmax+diff), base)
zRange = np.logspace(zmin, zmax, self.numLevelsSpinbox.value(), base=base)
self.logPreviewSteps = [] self.logPreviewSteps = []
for val in zRange: for val in zRange:
val -= diff
newLabel = QtWidgets.QLabel(str(round(val, 1))) newLabel = QtWidgets.QLabel(str(round(val, 1)))
newLabel.setEnabled(self.logChecker.isChecked()) newLabel.setEnabled(self.logChecker.isChecked())
self.logPreviewSteps.append(newLabel) self.logPreviewSteps.append(newLabel)
self.logPreviewLayout.addWidget(newLabel) self.logPreviewLayout.addWidget(newLabel)
def _updateCustomPreview(self): def _updateCustomPreview(self):
""" """
Updates custom level preview Updates custom level preview
...@@ -191,24 +202,25 @@ class ZLevelSetter(QtWidgets.QLabel): ...@@ -191,24 +202,25 @@ class ZLevelSetter(QtWidgets.QLabel):
""" """
numCurrentEntries = len(self.customPreviewSteps) numCurrentEntries = len(self.customPreviewSteps)
numDesiredEntries = self.numLevelsSpinbox.value() numDesiredEntries = self.numLevelsSpinbox.value()
indicesToRemove = []
for i in range(max([numCurrentEntries, numDesiredEntries])): for i in range(max([numCurrentEntries, numDesiredEntries])):
if i < numCurrentEntries: if i < numCurrentEntries:
try: self.customPreviewSteps[i].setEnabled(self.customChecker.isChecked())
self.customPreviewSteps[i].setEnabled(self.customChecker.isChecked())
except IndexError:
print(i, self.customPreviewSteps)
elif i >= numCurrentEntries: elif i >= numCurrentEntries:
newEntry = QtWidgets.QLineEdit() newEntry = QtWidgets.QLineEdit()
newEntry.setEnabled(self.customChecker.isChecked()) newEntry.setEnabled(self.customChecker.isChecked())
self.customPreviewSteps.append(newEntry) self.customPreviewSteps.append(newEntry)
self.customPreviewLayout.addWidget(newEntry) self.customPreviewLayout.addWidget(newEntry)
if i >= numDesiredEntries: if i >= numDesiredEntries:
entry = self.customPreviewSteps[i] indicesToRemove.append(i)
entry.setParent(None)
self.customPreviewSteps.remove(entry) for i in sorted(indicesToRemove, reverse=True):
entry = self.customPreviewSteps[i]
entry.setParent(None)
self.customPreviewSteps.remove(entry)
def _updateSize(self): def _updateSize(self):
""" """
...@@ -227,7 +239,7 @@ class ZLevelSetter(QtWidgets.QLabel): ...@@ -227,7 +239,7 @@ class ZLevelSetter(QtWidgets.QLabel):
""" """
self.maxLevelSpinbox.setMinimum(self.minLevelSpinbox.value()+1) self.maxLevelSpinbox.setMinimum(self.minLevelSpinbox.value()+1)
self.maxLevelSpinbox.setMaximum(self.maxLevelSpinbox.value()+1000) self.maxLevelSpinbox.setMaximum(self.maxLevelSpinbox.value()+1000)
def _addRadioBtnsToGroup(self): def _addRadioBtnsToGroup(self):
""" """
Group Radio Buttons to have them in exclusive check mode Group Radio Buttons to have them in exclusive check mode
...@@ -236,7 +248,7 @@ class ZLevelSetter(QtWidgets.QLabel): ...@@ -236,7 +248,7 @@ class ZLevelSetter(QtWidgets.QLabel):
self.radioGroup.addButton(self.linChecker) self.radioGroup.addButton(self.linChecker)
self.radioGroup.addButton(self.logChecker) self.radioGroup.addButton(self.logChecker)
self.radioGroup.addButton(self.customChecker) self.radioGroup.addButton(self.customChecker)
def getZLevels(self): def getZLevels(self):
""" """
retrieves the z-levels according to the chosen settings retrieves the z-levels according to the chosen settings
...@@ -267,17 +279,3 @@ class ZLevelSetter(QtWidgets.QLabel): ...@@ -267,17 +279,3 @@ class ZLevelSetter(QtWidgets.QLabel):
except: except:
raise ValueError raise ValueError
return sorted(levels) return sorted(levels)
if __name__ == '__main__':
try:
del app
except:
pass
app = QtWidgets.QApplication(sys.argv)
zSetter = ZLevelSetter()
zSetter.show()
ret = app.exec_()
sys.exit(1)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment