class MaskRadiiPreviewDialog(MaskPreviewDialog): def _createPreview(self, frame): """ Should be implemented by subclasses to create the items preview. """ from pyworkflow.gui.matplotlib_image import MaskPreview if self.innerRadius is None: self.innerRadius = 0 if self.outerRadius is None or self.outerRadius == -1 or self.outerRadius > self.dim_par/2: self.outerRadius = int(self.dim_par/2) self.preview = MaskPreview(frame, self.dim, label=self.previewLabel, outerRadius=int(self.outerRadius)*self.ratio, innerRadius=self.innerRadius*self.ratio) self.preview.grid(row=0, column=0) def _createControls(self, frame): self.radiusSliderOut = LabelSlider(frame, 'Outer radius', from_=0, to=int(self.dim_par/2), value=self.outerRadius, step=1, callback=lambda a, b, c:self.updateRadius(self.radiusSliderOut, self.radiusSliderIn)) self.radiusSliderOut.grid(row=0, column=0, padx=5, pady=5) self.radiusSliderIn = LabelSlider(frame, 'Inner radius', from_=0, to=int(self.dim_par/2), value=self.innerRadius, step=1, callback=lambda a, b, c:self.updateRadius(self.radiusSliderOut, self.radiusSliderIn)) self.radiusSliderIn.grid(row=1, column=0, padx=5, pady=5) def updateRadius(self, radiusSliderOut, radiusSliderIn): self.preview.updateMask(outerRadius=radiusSliderOut.get() * self.ratio, innerRadius=radiusSliderIn.get() * self.ratio) def getRadius(self, radiusSlider): return int(radiusSlider.get())
class MaskPreviewDialog(ImagePreviewDialog): def _beforePreview(self): self.dim = 256 self.samplingRate = 1 self.unit = getattr(self, 'unit', UNIT_PIXEL) if self.unit != UNIT_PIXEL: self.samplingRate = self.firstItem.getSamplingRate() self.dim_par = self.firstItem.getDim()[0] * self.samplingRate self.ratio = self.dim / float(self.dim_par) self.previewLabel = 'Central slice' def _createPreview(self, frame): """ Should be implemented by subclasses to create the items preview. """ from pyworkflow.gui.matplotlib_image import MaskPreview if self.maskRadius == -1: self.iniRadius = self.dim_par / 2 else: self.iniRadius = self.maskRadius self.preview = MaskPreview(frame, self.dim, label=self.previewLabel, outerRadius=self.iniRadius * self.ratio) self.preview.grid(row=0, column=0) def _createControls(self, frame): self.addRadiusBox(frame) def addRadiusBox(self, parent): self.radiusSlider = LabelSlider( parent, 'Outer radius (%s)' % self.unit, from_=0, to=int(self.dim_par / 2), value=self.iniRadius, step=self.samplingRate, callback=lambda a, b, c: self.updateRadius()) self.radiusSlider.grid(row=0, column=0, padx=5, pady=5) def updateRadius(self): self.preview.updateMask(self.radiusSlider.get() * self.ratio) def getRadius(self): return int(self.radiusSlider.get())
class MaskRadiiPreviewDialog(MaskPreviewDialog): def _createPreview(self, frame): """ Should be implemented by subclasses to create the items preview. """ from pyworkflow.gui.matplotlib_image import MaskPreview if self.innerRadius is None: self.innerRadius = 0 if self.outerRadius is None or self.outerRadius == -1 or self.outerRadius > self.dim_par / 2: self.outerRadius = int(self.dim_par / 2) self.preview = MaskPreview(frame, self.dim, label=self.previewLabel, outerRadius=int(self.outerRadius) * self.ratio, innerRadius=self.innerRadius * self.ratio) self.preview.grid(row=0, column=0) def _createControls(self, frame): self.radiusSliderOut = LabelSlider( frame, 'Outer radius', from_=0, to=int(self.dim_par / 2), value=self.outerRadius, step=1, callback=lambda a, b, c: self.updateRadius(self.radiusSliderOut, self.radiusSliderIn)) self.radiusSliderOut.grid(row=0, column=0, padx=5, pady=5) self.radiusSliderIn = LabelSlider( frame, 'Inner radius', from_=0, to=int(self.dim_par / 2), value=self.innerRadius, step=1, callback=lambda a, b, c: self.updateRadius(self.radiusSliderOut, self.radiusSliderIn)) self.radiusSliderIn.grid(row=1, column=0, padx=5, pady=5) def updateRadius(self, radiusSliderOut, radiusSliderIn): self.preview.updateMask(outerRadius=radiusSliderOut.get() * self.ratio, innerRadius=radiusSliderIn.get() * self.ratio) def getRadius(self, radiusSlider): return int(radiusSlider.get())
class MaskPreviewDialog(ImagePreviewDialog): def _beforePreview(self): self.dim = 256 self.samplingRate = 1 self.unit = getattr(self, 'unit', UNIT_PIXEL) if self.unit != UNIT_PIXEL: self.samplingRate = self.firstItem.getSamplingRate() self.dim_par = self.firstItem.getDim()[0] * self.samplingRate self.ratio = self.dim / float(self.dim_par) self.previewLabel = 'Central slice' def _createPreview(self, frame): """ Should be implemented by subclasses to create the items preview. """ from pyworkflow.gui.matplotlib_image import MaskPreview if self.maskRadius == -1: self.iniRadius = self.dim_par/2 else: self.iniRadius = self.maskRadius self.preview = MaskPreview(frame, self.dim, label=self.previewLabel, outerRadius=self.iniRadius*self.ratio) self.preview.grid(row=0, column=0) def _createControls(self, frame): self.addRadiusBox(frame) def addRadiusBox(self, parent): self.radiusSlider = LabelSlider(parent, 'Outer radius (%s)' % self.unit, from_=0, to=int(self.dim_par/2), value=self.iniRadius, step=self.samplingRate, callback=lambda a, b, c:self.updateRadius()) self.radiusSlider.grid(row=0, column=0, padx=5, pady=5) def updateRadius(self): self.preview.updateMask(self.radiusSlider.get() * self.ratio) def getRadius(self): return int(self.radiusSlider.get())
class MaskPreviewDialog(ImagePreviewDialog): def _beforePreview(self): self.dim = 256 self.unit = getattr(self, 'unit', emcts.UNIT_PIXEL) self.samplingRate = self.firstItem.getSamplingRate() self.dim_par = self.firstItem.getDim()[0] self.ratio = self.dim / float(self.dim_par) self.previewLabel = 'Central slice' def _createPreview(self, frame): """ Should be implemented by subclasses to create the items preview. """ # Insert the corresponding explanation text self.expText.updateExpText(emcts.CIRCLE_MASK_WIZ_MSG) from pyworkflow.gui.matplotlib_image import MaskPreview if self.maskRadius == -1: self.iniRadius = self.dim_par / 2 else: self.iniRadius = self.maskRadius if self.unit == emcts.UNIT_ANGSTROM: self.iniRadius = self.iniRadius / self.samplingRate listeners = { "<Button-4>": self.makeBigger, "<Button-5>": self.makeSmaller } self.preview = MaskPreview(frame, self.dim, label=self.previewLabel, listenersDict=listeners) self.preview.grid(row=1, column=0) def makeBigger(self, event): new_val = self.iniRadius + 1 if new_val <= self.dim_par / 2: # Don't make the mask bigger unless the is equal or lower than the max self.iniRadius = self.iniRadius + 1 self.manageMaskVals() def makeSmaller(self, event): new_val = self.iniRadius - 1 if new_val >= 1: # Don't make the mask smaller unless the radius is equal or greater than 1 self.iniRadius = self.iniRadius - 1 self.manageMaskVals() def manageMaskVals(self): # Set the ring slider in case it comes from the mouse wheel self.setRadius(self.radiusSlider, self.iniRadius) # Show values self.showValues(self.hfVar, self.radiusSlider) # Update mask self.preview.updateMask(self.iniRadius * self.ratio) def _createControls(self, frame): self.addRadiusBox(frame) self.hfVar = tk.StringVar() self.hfLabel = tk.Label(frame, textvariable=self.hfVar, width=20) self.hfLabel.grid(row=0, column=1, sticky='NSE', padx=5, pady=5) self.manageMaskVals() def addRadiusBox(self, parent): self.radiusSlider = LabelSlider( parent, 'Mask radius', from_=1, to=int(self.dim_par / 2), value=self.iniRadius, step=1, length=150, showvalue=0, callback=lambda a, b, c: self.updateSliderRadius()) self.radiusSlider.grid(row=0, column=0, sticky='NEWS') def updateSliderRadius(self): new_val = self.getRadius(self.radiusSlider) # Carry out the action only if the slider is clicked & dragged (prevent from minimal variations when the mouse # is on the slider but not clicking on it) if abs(new_val - self.iniRadius) >= 1: self.iniRadius = new_val self.manageMaskVals() def showValues(self, var2set, radiusSlider): """ Show the values selected for the inner and outer radius. If the units are angstroms (sampling_rate = 1, it will show only one value to avoid redundancies """ pixVal = self.getRadius(radiusSlider) if self.samplingRate == 1: var2set.set('{:6.1f} {}'.format(pixVal, emcts.UNIT_PIXEL)) else: var2set.set('{:5.0f} pix | {:6.1f} {}'.format( pixVal, self.getRadiusAngstroms(radiusSlider), emcts.UNIT_ANGSTROM_SYMBOL)) @staticmethod def getRadius(radiusSlider): return radiusSlider.get() @staticmethod def setRadius(radiusSlider, val): radiusSlider.slider.set(val) def getRadiusAngstroms(self, radiusSlider): return convertPixToLength(self.samplingRate, radiusSlider.get())
class MaskRadiiPreviewDialog(MaskPreviewDialog): def _createPreview(self, frame): """ Should be implemented by subclasses to create the items preview. """ # Insert the corresponding explanation text self.expText.updateExpText(emcts.RING_MASK_WIZ_MSG) from pyworkflow.gui.matplotlib_image import MaskPreview if self.innerRadius is None: self.innerRadius = 1 if self.outerRadius is None or self.outerRadius == -1 or self.outerRadius > self.dim_par / 2: self.outerRadius = int(self.dim_par / 2) if self.unit == emcts.UNIT_ANGSTROM: self.innerRadius = self.innerRadius / self.samplingRate self.outerRadius = self.innerRadius / self.samplingRate listeners = { "<Button-4>": self.makeBigger, "<Button-5>": self.makeSmaller, "<Up>": self.upKeyPress, "<Down>": self.downKeyPress } self.preview = MaskPreview(frame, self.dim, label=self.previewLabel, listenersDict=listeners) self.preview.grid(row=1, column=0) def upKeyPress(self, event): self.isInnerRad = False self.highlightOuterSlider() def downKeyPress(self, event): self.isInnerRad = True self.highlightInnerSlider() def highlightOuterSlider(self): self.radiusSliderOut.highlightLabel() self.radiusSliderIn.removeHighlightFromLabel() def highlightInnerSlider(self): self.radiusSliderIn.highlightLabel() self.radiusSliderOut.removeHighlightFromLabel() def makeBigger(self, event): self.isMakingBigger = True if self.isInnerRad: self.innerRadius = self.innerRadius + self.step else: new_val = self.outerRadius + self.step if new_val <= int( self.dim_par / 2 ): # Don't make the mask bigger unless the is equal or lower than the max self.outerRadius = new_val self.manageMaskVals() def makeSmaller(self, event): self.isMakingBigger = False if self.isInnerRad: new_val = self.innerRadius - self.step if new_val >= 0: self.innerRadius = new_val else: self.outerRadius = self.outerRadius - self.step self.manageMaskVals() def manageMaskVals(self): if self.isMakingBigger: if self.isInnerRad and self.innerRadius >= self.outerRadius: # Inner ring can't be bigger than outer ring # Subtract one step to go back to the nearest lower value self.innerRadius = self.outerRadius - self.step else: if not self.isInnerRad and self.outerRadius <= self.innerRadius: # Outer ring can't be smaller than inner # ring # Add one step to go back to the nearest higher value self.outerRadius = self.innerRadius + self.step # Set the ring sliders in case it comes from the mouse wheel self.setRadius(self.radiusSliderIn, self.innerRadius) self.setRadius(self.radiusSliderOut, self.outerRadius) # Show values self.showValues(self.orVar, self.radiusSliderOut) self.showValues(self.irVar, self.radiusSliderIn) # Update mask self.preview.updateMask(self.outerRadius * self.ratio, self.innerRadius * self.ratio) def _createControls(self, frame): self.step = 1 to = int(self.dim_par / 2) self.radiusSliderOut = LabelSlider( frame, 'Outer radius', from_=1, to=to, value=self.outerRadius, step=self.step, callback=lambda a, b, c: self.updateSliderOuterRadius(), length=150, showvalue=0) self.radiusSliderOut.highlightLabel() self.orVar = tk.StringVar() self.orLabel = tk.Label(frame, textvariable=self.orVar, width=20) self.radiusSliderIn = LabelSlider( frame, 'Inner radius', from_=1, to=to, value=self.innerRadius, step=self.step, callback=lambda a, b, c: self.updateSliderInnerRadius(), length=150, showvalue=0) self.irVar = tk.StringVar() self.irLabel = tk.Label(frame, textvariable=self.irVar, width=20) # Pack and configure outer radius slider self.radiusSliderOut.grid(row=0, column=0, sticky='NEWS') self.radiusSliderOut.columnconfigure(0, weight=1) self.orLabel.grid(row=0, column=1, sticky='NSE', padx=5, pady=5) self.orLabel.columnconfigure(0, weight=1) # Pack and configure inner radius slider self.radiusSliderIn.grid(row=1, column=0, sticky='NEWS') self.radiusSliderIn.columnconfigure(0, weight=1) self.irLabel.grid(row=1, column=1, sticky='NSE', padx=5, pady=5) self.irLabel.columnconfigure(0, weight=1) # Update both mask and sliders with the initial values self.manageMaskVals() def updateSliderOuterRadius(self): new_val = self.getRadius(self.radiusSliderOut) # Carry out the action only if the slider is clicked & dragged (prevent from minimal variations when the mouse # is on the slider but not clicking on it) if abs(new_val - self.outerRadius) >= self.step: self.highlightOuterSlider() self.isInnerRad = False self.isMakingBigger = False # Check if the user is making the ring bigger if new_val > self.outerRadius: self.isMakingBigger = True self.outerRadius = new_val self.manageMaskVals() def updateSliderInnerRadius(self): new_val = self.getRadius(self.radiusSliderIn) # Highlight only if the slider is clicked & dragged (prevent from minimal variations when the mouse is on the # slider but not clicking on it) if abs(new_val - self.innerRadius) >= self.step: self.highlightInnerSlider() self.isInnerRad = True self.isMakingBigger = False # Check if the user is making the ring bigger if new_val > self.innerRadius: self.isMakingBigger = True self.innerRadius = new_val self.manageMaskVals()