class _AmplitudeRangeDialog(qt.QDialog): """QDialog asking for the amplitude range to display.""" sigRangeChanged = qt.Signal(tuple) """Signal emitted when the range has changed. It provides the new range as a 2-tuple: (max, delta) """ def __init__(self, parent=None, amplitudeRange=None, displayedRange=(None, 2)): super(_AmplitudeRangeDialog, self).__init__(parent) self.setWindowTitle('Set Displayed Amplitude Range') if amplitudeRange is not None: amplitudeRange = min(amplitudeRange), max(amplitudeRange) self._amplitudeRange = amplitudeRange self._defaultDisplayedRange = displayedRange layout = qt.QFormLayout() self.setLayout(layout) if self._amplitudeRange is not None: min_, max_ = self._amplitudeRange layout.addRow( qt.QLabel('Data Amplitude Range: [%g, %g]' % (min_, max_))) self._maxLineEdit = FloatEdit(parent=self) self._maxLineEdit.validator().setBottom(0.) self._maxLineEdit.setAlignment(qt.Qt.AlignRight) self._maxLineEdit.editingFinished.connect(self._rangeUpdated) layout.addRow('Displayed Max.:', self._maxLineEdit) self._autoscale = qt.QCheckBox('autoscale') self._autoscale.toggled.connect(self._autoscaleCheckBoxToggled) layout.addRow('', self._autoscale) self._deltaLineEdit = FloatEdit(parent=self) self._deltaLineEdit.validator().setBottom(1.) self._deltaLineEdit.setAlignment(qt.Qt.AlignRight) self._deltaLineEdit.editingFinished.connect(self._rangeUpdated) layout.addRow('Displayed delta (log10 unit):', self._deltaLineEdit) buttons = qt.QDialogButtonBox(self) buttons.addButton(qt.QDialogButtonBox.Ok) buttons.addButton(qt.QDialogButtonBox.Cancel) buttons.accepted.connect(self.accept) buttons.rejected.connect(self.reject) layout.addRow(buttons) # Set dialog from default values self._resetDialogToDefault() self.rejected.connect(self._handleRejected) def _resetDialogToDefault(self): """Set Widgets of the dialog from range information """ max_, delta = self._defaultDisplayedRange if max_ is not None: # Not in autoscale displayedMax = max_ elif self._amplitudeRange is not None: # Autoscale with data displayedMax = self._amplitudeRange[1] else: # Autoscale without data displayedMax = '' if displayedMax == "": self._maxLineEdit.setText("") else: self._maxLineEdit.setValue(displayedMax) self._maxLineEdit.setEnabled(max_ is not None) self._deltaLineEdit.setValue(delta) self._autoscale.setChecked(self._defaultDisplayedRange[0] is None) def getRangeInfo(self): """Returns the current range as a 2-tuple (max, delta (in log10))""" if self._autoscale.isChecked(): max_ = None else: maxStr = self._maxLineEdit.text() max_ = self._maxLineEdit.value() if maxStr else None return max_, self._deltaLineEdit.value() if self._deltaLineEdit.text() else 2 def _handleRejected(self): """Reset range info to default when rejected""" self._resetDialogToDefault() self._rangeUpdated() def _rangeUpdated(self): """Handle QLineEdit editing finised""" self.sigRangeChanged.emit(self.getRangeInfo()) def _autoscaleCheckBoxToggled(self, checked): """Handle autoscale checkbox state changes""" if checked: # Use default values if self._amplitudeRange is None: max_ = '' else: max_ = self._amplitudeRange[1] if max_ == "": self._maxLineEdit.setText("") else: self._maxLineEdit.setValue(max_) self._maxLineEdit.setEnabled(not checked) self._rangeUpdated()
class PrintGeometryWidget(qt.QWidget): """Widget to specify the size and aspect ratio of an item before sending it to the print preview dialog. Use methods :meth:`setPrintGeometry` and :meth:`getPrintGeometry` to interact with the widget. """ def __init__(self, parent=None): super(PrintGeometryWidget, self).__init__(parent) self.mainLayout = qt.QGridLayout(self) self.mainLayout.setContentsMargins(0, 0, 0, 0) self.mainLayout.setSpacing(2) hbox = qt.QWidget(self) hboxLayout = qt.QHBoxLayout(hbox) hboxLayout.setContentsMargins(0, 0, 0, 0) hboxLayout.setSpacing(2) label = qt.QLabel(self) label.setText("Units") label.setAlignment(qt.Qt.AlignCenter) self._pageButton = qt.QRadioButton() self._pageButton.setText("Page") self._inchButton = qt.QRadioButton() self._inchButton.setText("Inches") self._cmButton = qt.QRadioButton() self._cmButton.setText("Centimeters") self._buttonGroup = qt.QButtonGroup(self) self._buttonGroup.addButton(self._pageButton) self._buttonGroup.addButton(self._inchButton) self._buttonGroup.addButton(self._cmButton) self._buttonGroup.setExclusive(True) # units self.mainLayout.addWidget(label, 0, 0, 1, 4) hboxLayout.addWidget(self._pageButton) hboxLayout.addWidget(self._inchButton) hboxLayout.addWidget(self._cmButton) self.mainLayout.addWidget(hbox, 1, 0, 1, 4) self._pageButton.setChecked(True) # xOffset label = qt.QLabel(self) label.setText("X Offset:") self.mainLayout.addWidget(label, 2, 0) self._xOffset = FloatEdit(self, 0.1) self.mainLayout.addWidget(self._xOffset, 2, 1) # yOffset label = qt.QLabel(self) label.setText("Y Offset:") self.mainLayout.addWidget(label, 2, 2) self._yOffset = FloatEdit(self, 0.1) self.mainLayout.addWidget(self._yOffset, 2, 3) # width label = qt.QLabel(self) label.setText("Width:") self.mainLayout.addWidget(label, 3, 0) self._width = FloatEdit(self, 0.9) self.mainLayout.addWidget(self._width, 3, 1) # height label = qt.QLabel(self) label.setText("Height:") self.mainLayout.addWidget(label, 3, 2) self._height = FloatEdit(self, 0.9) self.mainLayout.addWidget(self._height, 3, 3) # aspect ratio self._aspect = qt.QCheckBox(self) self._aspect.setText("Keep screen aspect ratio") self._aspect.setChecked(True) self.mainLayout.addWidget(self._aspect, 4, 1, 1, 2) def getPrintGeometry(self): """Return the print geometry dictionary. See :meth:`setPrintGeometry` for documentation about the print geometry dictionary.""" ddict = {} if self._inchButton.isChecked(): ddict['units'] = "inches" elif self._cmButton.isChecked(): ddict['units'] = "centimeters" else: ddict['units'] = "page" ddict['xOffset'] = self._xOffset.value() ddict['yOffset'] = self._yOffset.value() ddict['width'] = self._width.value() ddict['height'] = self._height.value() if self._aspect.isChecked(): ddict['keepAspectRatio'] = True else: ddict['keepAspectRatio'] = False return ddict def setPrintGeometry(self, geometry=None): """Set the print geometry. The geometry parameters must be provided as a dictionary with the following keys: - *"xOffset"* (float) - *"yOffset"* (float) - *"width"* (float) - *"height"* (float) - *"units"*: possible values *"page", "inch", "cm"* - *"keepAspectRatio"*: *True* or *False* If *units* is *"page"*, the values should be floats in [0, 1.] and are interpreted as a fraction of the page width or height. :param dict geometry: Geometry parameters, as a dictionary.""" if geometry is None: geometry = {} oldDict = self.getPrintGeometry() for key in [ "units", "xOffset", "yOffset", "width", "height", "keepAspectRatio" ]: geometry[key] = geometry.get(key, oldDict[key]) if geometry['units'].lower().startswith("inc"): self._inchButton.setChecked(True) elif geometry['units'].lower().startswith("c"): self._cmButton.setChecked(True) else: self._pageButton.setChecked(True) self._xOffset.setText("%s" % float(geometry['xOffset'])) self._yOffset.setText("%s" % float(geometry['yOffset'])) self._width.setText("%s" % float(geometry['width'])) self._height.setText("%s" % float(geometry['height'])) if geometry['keepAspectRatio']: self._aspect.setChecked(True) else: self._aspect.setChecked(False)
class PrintGeometryWidget(qt.QWidget): """Widget to specify the size and aspect ratio of an item before sending it to the print preview dialog. Use methods :meth:`setPrintGeometry` and :meth:`getPrintGeometry` to interact with the widget. """ def __init__(self, parent=None): super(PrintGeometryWidget, self).__init__(parent) self.mainLayout = qt.QGridLayout(self) self.mainLayout.setContentsMargins(0, 0, 0, 0) self.mainLayout.setSpacing(2) hbox = qt.QWidget(self) hboxLayout = qt.QHBoxLayout(hbox) hboxLayout.setContentsMargins(0, 0, 0, 0) hboxLayout.setSpacing(2) label = qt.QLabel(self) label.setText("Units") label.setAlignment(qt.Qt.AlignCenter) self._pageButton = qt.QRadioButton() self._pageButton.setText("Page") self._inchButton = qt.QRadioButton() self._inchButton.setText("Inches") self._cmButton = qt.QRadioButton() self._cmButton.setText("Centimeters") self._buttonGroup = qt.QButtonGroup(self) self._buttonGroup.addButton(self._pageButton) self._buttonGroup.addButton(self._inchButton) self._buttonGroup.addButton(self._cmButton) self._buttonGroup.setExclusive(True) # units self.mainLayout.addWidget(label, 0, 0, 1, 4) hboxLayout.addWidget(self._pageButton) hboxLayout.addWidget(self._inchButton) hboxLayout.addWidget(self._cmButton) self.mainLayout.addWidget(hbox, 1, 0, 1, 4) self._pageButton.setChecked(True) # xOffset label = qt.QLabel(self) label.setText("X Offset:") self.mainLayout.addWidget(label, 2, 0) self._xOffset = FloatEdit(self, 0.1) self.mainLayout.addWidget(self._xOffset, 2, 1) # yOffset label = qt.QLabel(self) label.setText("Y Offset:") self.mainLayout.addWidget(label, 2, 2) self._yOffset = FloatEdit(self, 0.1) self.mainLayout.addWidget(self._yOffset, 2, 3) # width label = qt.QLabel(self) label.setText("Width:") self.mainLayout.addWidget(label, 3, 0) self._width = FloatEdit(self, 0.9) self.mainLayout.addWidget(self._width, 3, 1) # height label = qt.QLabel(self) label.setText("Height:") self.mainLayout.addWidget(label, 3, 2) self._height = FloatEdit(self, 0.9) self.mainLayout.addWidget(self._height, 3, 3) # aspect ratio self._aspect = qt.QCheckBox(self) self._aspect.setText("Keep screen aspect ratio") self._aspect.setChecked(True) self.mainLayout.addWidget(self._aspect, 4, 1, 1, 2) def getPrintGeometry(self): """Return the print geometry dictionary. See :meth:`setPrintGeometry` for documentation about the print geometry dictionary.""" ddict = {} if self._inchButton.isChecked(): ddict['units'] = "inches" elif self._cmButton.isChecked(): ddict['units'] = "centimeters" else: ddict['units'] = "page" ddict['xOffset'] = self._xOffset.value() ddict['yOffset'] = self._yOffset.value() ddict['width'] = self._width.value() ddict['height'] = self._height.value() if self._aspect.isChecked(): ddict['keepAspectRatio'] = True else: ddict['keepAspectRatio'] = False return ddict def setPrintGeometry(self, geometry=None): """Set the print geometry. The geometry parameters must be provided as a dictionary with the following keys: - *"xOffset"* (float) - *"yOffset"* (float) - *"width"* (float) - *"height"* (float) - *"units"*: possible values *"page", "inch", "cm"* - *"keepAspectRatio"*: *True* or *False* If *units* is *"page"*, the values should be floats in [0, 1.] and are interpreted as a fraction of the page width or height. :param dict geometry: Geometry parameters, as a dictionary.""" if geometry is None: geometry = {} oldDict = self.getPrintGeometry() for key in ["units", "xOffset", "yOffset", "width", "height", "keepAspectRatio"]: geometry[key] = geometry.get(key, oldDict[key]) if geometry['units'].lower().startswith("inc"): self._inchButton.setChecked(True) elif geometry['units'].lower().startswith("c"): self._cmButton.setChecked(True) else: self._pageButton.setChecked(True) self._xOffset.setText("%s" % float(geometry['xOffset'])) self._yOffset.setText("%s" % float(geometry['yOffset'])) self._width.setText("%s" % float(geometry['width'])) self._height.setText("%s" % float(geometry['height'])) if geometry['keepAspectRatio']: self._aspect.setChecked(True) else: self._aspect.setChecked(False)