zlevelsetter.py 9.47 KB
Newer Older
1 2 3 4 5 6
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 25 09:39:14 2019

@author: brandt
"""
Josef Brandt's avatar
Josef Brandt committed
7
from PyQt5 import QtWidgets, QtCore
8 9 10 11 12 13 14 15 16
import sys
import numpy as np


class ZLevelSetter(QtWidgets.QLabel):
    """
    Allows refining settings for the focus-z-stacking
    :return:
    """
Josef Brandt's avatar
Josef Brandt committed
17 18 19

    sizeChanged = QtCore.pyqtSignal()

20 21 22 23 24 25 26 27 28 29 30
    def __init__(self):
        super(ZLevelSetter, self).__init__()
        layout = QtWidgets.QVBoxLayout()
        self.setLayout(layout)
        
        self.radioGroup = QtWidgets.QButtonGroup()
        
        layout.addWidget(self._createRangeGroupBox())
        layout.addLayout(self._createLevelSelectionLayout())

        self._addRadioBtnsToGroup()
Josef Brandt's avatar
Josef Brandt committed
31
        self._updateLevels()
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

    def _createRangeGroupBox(self):
        """
        Creates Widgets for setting range of z-levels
        :return:
        """
        rangeGroupBox = QtWidgets.QGroupBox('Select Range of z-steps')
        rangesLayout = QtWidgets.QHBoxLayout()
        rangeGroupBox.setLayout(rangesLayout)
        rangeGroupBox.setFixedHeight(60)
        
        self.minLevelSpinbox = QtWidgets.QSpinBox()
        self.minLevelSpinbox.setMinimum(-1000)
        self.minLevelSpinbox.setMaximum(1000)
        self.minLevelSpinbox.setValue(0)
        self.minLevelSpinbox.valueChanged.connect(self._minLevelBoxChanged)
        self.maxLevelSpinbox = QtWidgets.QSpinBox()
        self.maxLevelSpinbox.setValue(50)
        self._minLevelBoxChanged()
        
        self.numLevelsSpinbox = QtWidgets.QSpinBox()
        self.numLevelsSpinbox.setMinimum(1)
        self.numLevelsSpinbox.setMaximum(100)
        self.numLevelsSpinbox.setValue(5)
        
        for box in [self.minLevelSpinbox, self.maxLevelSpinbox, self.numLevelsSpinbox]:
            box.valueChanged.connect(self._updateLevels)
        
Josef Brandt's avatar
Josef Brandt committed
60
        rangesLayout.addWidget(QtWidgets.QLabel('z_min (µm): '))
61
        rangesLayout.addWidget(self.minLevelSpinbox)
Josef Brandt's avatar
Josef Brandt committed
62
        rangesLayout.addWidget(QtWidgets.QLabel(', z_max (µm): '))
63
        rangesLayout.addWidget(self.maxLevelSpinbox)
Josef Brandt's avatar
Josef Brandt committed
64
        rangesLayout.addWidget(QtWidgets.QLabel(', Num. steps: '))
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        rangesLayout.addWidget(self.numLevelsSpinbox)
        return rangeGroupBox
    
    def _createLevelSelectionLayout(self):
        """
        Creates the Parent Group for selecting mode of level distribution
        :return:
        """
        modeLayout = QtWidgets.QHBoxLayout()
        modeLayout.addWidget(self._createLinGroup())
        modeLayout.addWidget(self._createLogGroup())
        modeLayout.addWidget(self._createCustomGroup())
        return modeLayout
    
    def _createLinGroup(self):
        """
        Creates Widget group for linear level distribution
        :return:
        """
        linGroup = QtWidgets.QGroupBox()
        linLayout = QtWidgets.QVBoxLayout()
        linGroup.setLayout(linLayout)
        linGroup.sizePolicy().setVerticalPolicy(QtWidgets.QSizePolicy.Expanding)
        
        self.linChecker = QtWidgets.QRadioButton('Linear')
        self.linChecker.setChecked(True)
        self.linChecker.toggled.connect(self._updateLevels)
        linLayout.addWidget(self.linChecker)
        
        self.linPreviewLayout = QtWidgets.QVBoxLayout()
        linLayout.addLayout(self.linPreviewLayout)
        self.linPreviewSteps = []
        self._updateLinPreview()
        return linGroup
    
    def _createLogGroup(self):
        """
        Creates Widget group for logarithmic level distribution
        :return:
        """
        logGroup = QtWidgets.QGroupBox()
        logLayout = QtWidgets.QVBoxLayout()
        logGroup.setLayout(logLayout)
        logGroup.sizePolicy().setVerticalPolicy(QtWidgets.QSizePolicy.Expanding)
        
        self.logChecker = QtWidgets.QRadioButton('Logarithmic')
        self.logChecker.setChecked(False)
        self.logChecker.toggled.connect(self._updateLevels)
        logLayout.addWidget(self.logChecker)
        
        self.logPreviewLayout = QtWidgets.QVBoxLayout()
        logLayout.addLayout(self.logPreviewLayout)
        
        self.logPreviewSteps = []
        self._updateLogPreview()
        return logGroup
    
    def _createCustomGroup(self):
        """
        Creates Widget group for custom level distribution
        :return:
        """
        customGroup = QtWidgets.QGroupBox()
        customLayout = QtWidgets.QVBoxLayout()
        customGroup.setLayout(customLayout)
        customGroup.sizePolicy().setVerticalPolicy(QtWidgets.QSizePolicy.Expanding)
        
        self.customChecker = QtWidgets.QRadioButton('Custom')
        self.customChecker.setChecked(False)
        self.customChecker.toggled.connect(self._updateLevels)
        customLayout.addWidget(self.customChecker)
        
        self.customPreviewLayout = QtWidgets.QVBoxLayout()
        customLayout.addLayout(self.customPreviewLayout)
        self.customPreviewSteps = []
        self._updateCustomPreview()
        return customGroup
    
    def _updateLevels(self):
        """
        Updates the previewed levels for all modes
        :return:
        """
        self._updateLinPreview()
        self._updateLogPreview()
        self._updateCustomPreview()
Josef Brandt's avatar
Josef Brandt committed
151
        self._updateSize()
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        
    def _updateLinPreview(self):
        """
        Updates linear level preview
        :return:
        """
        for entry in self.linPreviewSteps:
            entry.setParent(None)
        zRange = np.linspace(self.minLevelSpinbox.value(), self.maxLevelSpinbox.value(), self.numLevelsSpinbox.value())
        
        self.linPreviewSteps = []
        for val in zRange:
            newLabel = QtWidgets.QLabel(str(round(val, 1)))
            newLabel.setEnabled(self.linChecker.isChecked())
            self.linPreviewSteps.append(newLabel)
            self.linPreviewLayout.addWidget(newLabel)
            
    def _updateLogPreview(self):
        """
        Updates logarithmic level preview
        :return:
        """
        for entry in self.logPreviewSteps:
            entry.setParent(None)
        zmin = self.minLevelSpinbox.value()
        zmax = np.log2(self.maxLevelSpinbox.value())
        zRange = np.logspace(zmin, zmax, self.numLevelsSpinbox.value(), base=2)

        self.logPreviewSteps = []
        for val in zRange:
            newLabel = QtWidgets.QLabel(str(round(val, 1)))
            newLabel.setEnabled(self.logChecker.isChecked())
            self.logPreviewSteps.append(newLabel)
            self.logPreviewLayout.addWidget(newLabel)
    
    def _updateCustomPreview(self):
        """
        Updates custom level preview
        :return:
        """
        numCurrentEntries = len(self.customPreviewSteps)
        numDesiredEntries = self.numLevelsSpinbox.value()
        
        for i in range(max([numCurrentEntries, numDesiredEntries])):
            if i < numCurrentEntries:
                try:
                    self.customPreviewSteps[i].setEnabled(self.customChecker.isChecked())
                except IndexError:
                    print(i, self.customPreviewSteps)
                
            elif i >= numCurrentEntries:
                newEntry = QtWidgets.QLineEdit()
                newEntry.setEnabled(self.customChecker.isChecked())
                self.customPreviewSteps.append(newEntry)
                self.customPreviewLayout.addWidget(newEntry)
            
            if i >= numDesiredEntries:
                entry = self.customPreviewSteps[i]
                entry.setParent(None)
                self.customPreviewSteps.remove(entry)

Josef Brandt's avatar
Josef Brandt committed
213 214 215 216 217 218 219 220 221 222
    def _updateSize(self):
        """
        Updates the size so that all entries are well visible
        :return:
        """
        width = 400
        height = 100 + self.numLevelsSpinbox.value()*30
        self.setFixedSize(width, height)
        self.sizeChanged.emit()

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    def _minLevelBoxChanged(self):
        """
        Adjusts maxLevelSpinbox to show always values larger then minLevelSpinbox
        :return:
        """
        self.maxLevelSpinbox.setMinimum(self.minLevelSpinbox.value()+1)
        self.maxLevelSpinbox.setMaximum(self.maxLevelSpinbox.value()+1000)
            
    def _addRadioBtnsToGroup(self):
        """
        Group Radio Buttons to have them in exclusive check mode
        :return:
        """
        self.radioGroup.addButton(self.linChecker)
        self.radioGroup.addButton(self.logChecker)
        self.radioGroup.addButton(self.customChecker)
        
    def getZLevels(self):
        """
        retrieves the z-levels according to the chosen settings
        :return:
        """
        zLevels = []
        if self.linChecker.isChecked():
            zLevels = [float(entry.text()) for entry in self.linPreviewSteps]
        elif self.logChecker.isChecked():
            zLevels = [float(entry.text()) for entry in self.logPreviewSteps]
        elif self.customChecker.isChecked():
            try:
                zLevels = self._getCustomLevels()
Josef Brandt's avatar
Josef Brandt committed
253
            except ValueError:
254
                QtWidgets.QMessageBox.critical(self, 'Error', 'Invalid entry in custom level selection')
Josef Brandt's avatar
Josef Brandt committed
255
                raise ValueError
256 257 258 259 260 261 262 263 264 265 266 267
        return np.array(zLevels)

    def _getCustomLevels(self):
        """
        Reads out the custom level range
        :return:
        """
        levels = []
        for lineEdit in self.customPreviewSteps:
            try:
                levels.append(float(lineEdit.text()))
            except:
Josef Brandt's avatar
Josef Brandt committed
268
                raise ValueError
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
        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)