class StatusBar(QStatusBar): """Configuration of the status bar .. note:: For more information about the QStatusBar' methods and attributes used here, please refer to the `QStatusBar documentation <http://pyqt.sourceforge.net/Docs/PyQt4/qstatusbar.html>`_ """ def __init__(self, getIcon, *args, **kwargs): QStatusBar.__init__(self, *args, **kwargs) self.ActionIcon = QLabel(u"") self.ActionInfo = QLabel(u"") self.ActionInfo.setFixedWidth(200) self.StreamInfo = QLabel(u"") self.addPermanentWidget(self.ActionIcon) self.addPermanentWidget(self.ActionInfo) openIcon = QLabel(u"") openIcon.setPixmap(getIcon("arrow").pixmap(QSize(16, 16))) self.addPermanentWidget(openIcon) self.addPermanentWidget(self.StreamInfo, 1) self.getIcon = getIcon self._stdout = sys.stdout self._txtStream = io.StringIO() sys.stdout = self._txtStream def close(self): sys.stdout = self._stdout self._txtStream.close() QStatusBar.close(self) # set text information on the panel def setInfo(self, txt="", icon_name=None): if icon_name is not None: self.ActionIcon.setPixmap( self.getIcon(icon_name).pixmap(QSize(16, 16))) self.ActionInfo.setText(txt) self.repaint() # display stream in the panel def refreshInfo(self): self._txtStream.seek(0) val = self._txtStream.getvalue() if len(val) == 0: return self._txtStream.truncate(0) self._stdout.write(val) self.StreamInfo.setText(val.split('\n')[-2]) self.repaint()
class TestPropertiesWidget(QWidget): def __init__(self, parent): QWidget.__init__(self, parent) font = QFont(get_family(MONOSPACE), 10, QFont.Normal) info_icon = QLabel() icon = get_std_icon('MessageBoxInformation').pixmap(24, 24) info_icon.setPixmap(icon) info_icon.setFixedWidth(32) info_icon.setAlignment(Qt.AlignTop) self.desc_label = QLabel() self.desc_label.setWordWrap(True) self.desc_label.setAlignment(Qt.AlignTop) self.desc_label.setFont(font) group_desc = QGroupBox(_("Description"), self) layout = QHBoxLayout() layout.addWidget(info_icon) layout.addWidget(self.desc_label) group_desc.setLayout(layout) self.editor = CodeEditor(self) self.editor.setup_editor(linenumbers=True, font=font) self.editor.setReadOnly(True) group_code = QGroupBox(_("Source code"), self) layout = QVBoxLayout() layout.addWidget(self.editor) group_code.setLayout(layout) self.run_button = QPushButton(get_icon("apply.png"), _("Run this script"), self) self.quit_button = QPushButton(get_icon("exit.png"), _("Quit"), self) hlayout = QHBoxLayout() hlayout.addWidget(self.run_button) hlayout.addStretch() hlayout.addWidget(self.quit_button) vlayout = QVBoxLayout() vlayout.addWidget(group_desc) vlayout.addWidget(group_code) vlayout.addLayout(hlayout) self.setLayout(vlayout) def set_item(self, test): self.desc_label.setText(test.get_description()) self.editor.set_text_from_file(test.filename)
class TestPropertiesWidget(QWidget): def __init__(self, parent): QWidget.__init__(self, parent) font = QFont(get_family(MONOSPACE), 10, QFont.Normal) info_icon = QLabel() icon = get_std_icon('MessageBoxInformation').pixmap(24, 24) info_icon.setPixmap(icon) info_icon.setFixedWidth(32) info_icon.setAlignment(Qt.AlignTop) self.desc_label = QLabel() self.desc_label.setWordWrap(True) self.desc_label.setAlignment(Qt.AlignTop) self.desc_label.setFont(font) group_desc = QGroupBox(_("Description"), self) layout = QHBoxLayout() layout.addWidget(info_icon) layout.addWidget(self.desc_label) group_desc.setLayout(layout) self.editor = CodeEditor(self) self.editor.setup_editor(linenumbers=True, font=font) self.editor.setReadOnly(True) group_code = QGroupBox(_("Source code"), self) layout = QVBoxLayout() layout.addWidget(self.editor) group_code.setLayout(layout) self.run_button = QPushButton(get_icon("apply.png"), _("Run this script"), self) self.quit_button = QPushButton(get_icon("exit.png"), _("Quit"), self) hlayout = QHBoxLayout() hlayout.addWidget(self.run_button) hlayout.addStretch() hlayout.addWidget(self.quit_button) vlayout = QVBoxLayout() vlayout.addWidget(group_desc) vlayout.addWidget(group_code) vlayout.addLayout(hlayout) self.setLayout(vlayout) def set_item(self, test): self.desc_label.setText(test.get_description()) self.editor.set_text_from_file(test.filename)
class DataSetShowWidget(AbstractDataSetWidget): """Read-only base widget""" READ_ONLY = True def __init__(self, item, parent_layout): AbstractDataSetWidget.__init__(self, item, parent_layout) self.group = QLabel() wordwrap = item.get_prop_value("display", "wordwrap", False) self.group.setWordWrap(wordwrap) self.group.setToolTip(item.get_help()) self.group.setStyleSheet( LABEL_CSS ) self.group.setTextInteractionFlags(Qt.TextSelectableByMouse) #self.group.setEnabled(False) def get(self): """Override AbstractDataSetWidget method""" self.set_state() text = self.item.get_string_value() self.group.setText(text) def set(self): """Read only...""" pass
class DataSetShowWidget(AbstractDataSetWidget): """Read-only base widget""" READ_ONLY = True def __init__(self, item, parent_layout): AbstractDataSetWidget.__init__(self, item, parent_layout) self.group = QLabel() wordwrap = item.get_prop_value("display", "wordwrap", False) self.group.setWordWrap(wordwrap) self.group.setToolTip(item.get_help()) self.group.setStyleSheet( LABEL_CSS ) self.group.setTextInteractionFlags(Qt.TextSelectableByMouse) #self.group.setEnabled(False) def get(self): """Override AbstractDataSetWidget method""" self.set_state() text = self.item.get_string_value() self.group.setText(text) def set(self): """Read only...""" pass
class PropertiesWidget(QWidget): def __init__(self, parent): QWidget.__init__(self, parent) font = QFont(get_family(MONOSPACE), 10, QFont.Normal) info_icon = QLabel() icon = get_std_icon('MessageBoxInformation').pixmap(24, 24) info_icon.setPixmap(icon) info_icon.setFixedWidth(32) info_icon.setAlignment(Qt.AlignTop) self.service_status_label = QLabel() self.service_status_label.setWordWrap(True) self.service_status_label.setAlignment(Qt.AlignTop) self.service_status_label.setFont(font) self.desc_label = QLabel() self.desc_label.setWordWrap(True) self.desc_label.setAlignment(Qt.AlignTop) self.desc_label.setFont(font) self.group_desc = QGroupBox("Description", self) layout = QHBoxLayout() layout.addWidget(info_icon) layout.addWidget(self.desc_label) layout.addStretch() layout.addWidget(self.service_status_label) self.group_desc.setLayout(layout) self.editor = CodeEditor(self) self.editor.setup_editor(linenumbers=True, font=font) self.editor.setReadOnly(False) self.group_code = QGroupBox("Source code", self) layout = QVBoxLayout() layout.addWidget(self.editor) self.group_code.setLayout(layout) self.enable_button = QPushButton(get_icon("apply.png"), "Enable", self) self.save_button = QPushButton(get_icon("filesave.png"), "Save", self) self.disable_button = QPushButton(get_icon("delete.png"), "Disable", self) self.refresh_button = QPushButton(get_icon("restart.png"), "Refresh", self) hlayout = QHBoxLayout() hlayout.addWidget(self.save_button) hlayout.addWidget(self.enable_button) hlayout.addWidget(self.disable_button) hlayout.addWidget(self.refresh_button) vlayout = QVBoxLayout() vlayout.addWidget(self.group_desc) vlayout.addWidget(self.group_code) self.html_window = HTMLWindow() vlayout.addWidget(self.html_window) vlayout.addLayout(hlayout) self.setLayout(vlayout) self.current_file = None def set_status(self): self.refresh_button.setEnabled(True) self.disable_button.setEnabled(False) self.enable_button.setEnabled(False) self.save_button.setEnabled(False) def set_item(self, check): self.refresh_button.setEnabled(False) self.save_button.setEnabled(True) self.current_file = check self.desc_label.setText(check.get_description()) self.editor.set_text_from_file(check.file_path) check.content = self.editor.toPlainText().__str__() if check.enabled: self.disable_button.setEnabled(True) self.enable_button.setEnabled(False) else: self.disable_button.setEnabled(False) self.enable_button.setEnabled(True) def set_datadog_conf(self, datadog_conf): self.save_button.setEnabled(True) self.refresh_button.setEnabled(False) self.current_file = datadog_conf self.desc_label.setText(datadog_conf.get_description()) self.editor.set_text_from_file(datadog_conf.file_path) datadog_conf.content = self.editor.toPlainText().__str__() self.disable_button.setEnabled(False) self.enable_button.setEnabled(False) datadog_conf.check_api_key(self.editor) def set_log_file(self, log_file): self.save_button.setEnabled(False) self.refresh_button.setEnabled(True) self.disable_button.setEnabled(False) self.enable_button.setEnabled(False) try: self.current_file = log_file self.desc_label.setText(log_file.get_description()) self.editor.set_text_from_file(log_file.file_path) log_file.content = self.editor.toPlainText().__str__() self.editor.go_to_line(len(log_file.content.splitlines())) except Exception: self.editor.set_text("Log file not found")
class PropertiesWidget(QWidget): def __init__(self, parent): QWidget.__init__(self, parent) font = QFont(get_family(MONOSPACE), 10, QFont.Normal) info_icon = QLabel() icon = get_std_icon('MessageBoxInformation').pixmap(24, 24) info_icon.setPixmap(icon) info_icon.setFixedWidth(32) info_icon.setAlignment(Qt.AlignTop) self.service_status_label = QLabel() self.service_status_label.setWordWrap(True) self.service_status_label.setAlignment(Qt.AlignTop) self.service_status_label.setFont(font) self.desc_label = QLabel() self.desc_label.setWordWrap(True) self.desc_label.setAlignment(Qt.AlignTop) self.desc_label.setFont(font) group_desc = QGroupBox("Description", self) layout = QHBoxLayout() layout.addWidget(info_icon) layout.addWidget(self.desc_label) layout.addStretch() layout.addWidget(self.service_status_label ) group_desc.setLayout(layout) self.editor = CodeEditor(self) self.editor.setup_editor(linenumbers=True, font=font) self.editor.setReadOnly(False) group_code = QGroupBox("Source code", self) layout = QVBoxLayout() layout.addWidget(self.editor) group_code.setLayout(layout) self.enable_button = QPushButton(get_icon("apply.png"), "Enable", self) self.save_button = QPushButton(get_icon("filesave.png"), "Save", self) self.edit_datadog_conf_button = QPushButton(get_icon("edit.png"), "Edit agent settings", self) self.disable_button = QPushButton(get_icon("delete.png"), "Disable", self) self.view_log_button = QPushButton(get_icon("txt.png"), "View log", self) self.menu_button = QPushButton(get_icon("settings.png"), "Manager", self) hlayout = QHBoxLayout() hlayout.addWidget(self.save_button) hlayout.addStretch() hlayout.addWidget(self.enable_button) hlayout.addStretch() hlayout.addWidget(self.disable_button) hlayout.addStretch() hlayout.addWidget(self.edit_datadog_conf_button) hlayout.addStretch() hlayout.addWidget(self.view_log_button) hlayout.addStretch() hlayout.addWidget(self.menu_button) vlayout = QVBoxLayout() vlayout.addWidget(group_desc) vlayout.addWidget(group_code) vlayout.addLayout(hlayout) self.setLayout(vlayout) self.current_file = None def set_item(self, check): self.current_file = check self.desc_label.setText(check.get_description()) self.editor.set_text_from_file(check.file_path) check.content = self.editor.toPlainText().__str__() if check.enabled: self.disable_button.setEnabled(True) self.enable_button.setEnabled(False) else: self.disable_button.setEnabled(False) self.enable_button.setEnabled(True) def set_datadog_conf(self, datadog_conf): self.current_file = datadog_conf self.desc_label.setText(datadog_conf.get_description()) self.editor.set_text_from_file(datadog_conf.file_path) datadog_conf.content = self.editor.toPlainText().__str__() self.disable_button.setEnabled(False) self.enable_button.setEnabled(False) datadog_conf.check_api_key(self.editor) def set_log_file(self, log_file): self.current_file = log_file self.desc_label.setText(log_file.get_description()) self.editor.set_text_from_file(log_file.file_path) log_file.content = self.editor.toPlainText().__str__() self.disable_button.setEnabled(False) self.enable_button.setEnabled(False) self.editor.go_to_line(len(log_file.content.splitlines()))
class FitParam(object): def __init__(self, name, value, min, max, logscale=False, steps=5000, format='%.3f', size_offset=0, unit=''): self.name = name self.value = value self.min = min self.max = max self.logscale = logscale self.steps = steps self.format = format self.unit = unit self.prefix_label = None self.lineedit = None self.unit_label = None self.slider = None self.button = None self._widgets = [] self._size_offset = size_offset self._refresh_callback = None self.dataset = FitParamDataSet(title=_("Curve fitting parameter")) def copy(self): """Return a copy of this fitparam""" return self.__class__(self.name, self.value, self.min, self.max, self.logscale, self.steps, self.format, self._size_offset, self.unit) def create_widgets(self, parent, refresh_callback): self._refresh_callback = refresh_callback self.prefix_label = QLabel() font = self.prefix_label.font() font.setPointSize(font.pointSize()+self._size_offset) self.prefix_label.setFont(font) self.button = QPushButton() self.button.setIcon(get_icon('settings.png')) self.button.setToolTip( _("Edit '%s' fit parameter properties") % self.name) self.button.clicked.connect(lambda: self.edit_param(parent)) self.lineedit = QLineEdit() self.lineedit.editingFinished.connect(self.line_editing_finished) self.unit_label = QLabel(self.unit) self.slider = QSlider() self.slider.setOrientation(Qt.Horizontal) self.slider.setRange(0, self.steps-1) self.slider.valueChanged.connect(self.slider_value_changed) self.update(refresh=False) self.add_widgets([self.prefix_label, self.lineedit, self.unit_label, self.slider, self.button]) def add_widgets(self, widgets): self._widgets += widgets def get_widgets(self): return self._widgets def set_scale(self, state): self.logscale = state > 0 self.update_slider_value() def set_text(self, fmt=None): style = "<span style=\'color: #444444\'><b>%s</b></span>" self.prefix_label.setText(style % self.name) if self.value is None: value_str = '' else: if fmt is None: fmt = self.format value_str = fmt % self.value self.lineedit.setText(value_str) self.lineedit.setDisabled( self.value == self.min and self.max == self.min) def line_editing_finished(self): try: self.value = float(self.lineedit.text()) except ValueError: self.set_text() self.update_slider_value() self._refresh_callback() def slider_value_changed(self, int_value): if self.logscale: total_delta = np.log10(1+self.max-self.min) self.value = self.min+10**(total_delta*int_value/(self.steps-1))-1 else: total_delta = self.max-self.min self.value = self.min+total_delta*int_value/(self.steps-1) self.set_text() self._refresh_callback() def update_slider_value(self): if (self.value is None or self.min is None or self.max is None): self.slider.setEnabled(False) if self.slider.parent() and self.slider.parent().isVisible(): self.slider.show() elif self.value == self.min and self.max == self.min: self.slider.hide() else: self.slider.setEnabled(True) if self.slider.parent() and self.slider.parent().isVisible(): self.slider.show() if self.logscale: value_delta = max([np.log10(1+self.value-self.min), 0.]) total_delta = np.log10(1+self.max-self.min) else: value_delta = self.value-self.min total_delta = self.max-self.min intval = int(self.steps*value_delta/total_delta) self.slider.blockSignals(True) self.slider.setValue(intval) self.slider.blockSignals(False) def edit_param(self, parent): update_dataset(self.dataset, self) if self.dataset.edit(parent=parent): restore_dataset(self.dataset, self) if self.value > self.max: self.max = self.value if self.value < self.min: self.min = self.value self.update() def update(self, refresh=True): self.unit_label.setText(self.unit) self.slider.setRange(0, self.steps-1) self.update_slider_value() self.set_text() if refresh: self._refresh_callback()
class ResizeDialog(QDialog): def __init__(self, parent, new_size, old_size, text=""): QDialog.__init__(self, parent) self.keep_original_size = False self.width, self.height = new_size self.old_width, self.old_height = old_size self.ratio = self.width/self.height layout = QVBoxLayout() self.setLayout(layout) formlayout = QFormLayout() layout.addLayout(formlayout) if text: label = QLabel(text) label.setAlignment(Qt.AlignHCenter) formlayout.addRow(label) self.w_edit = w_edit = QLineEdit(self) w_valid = QIntValidator(w_edit) w_valid.setBottom(1) w_edit.setValidator(w_valid) self.h_edit = h_edit = QLineEdit(self) h_valid = QIntValidator(h_edit) h_valid.setBottom(1) h_edit.setValidator(h_valid) zbox = QCheckBox(_("Original size"), self) formlayout.addRow(_("Width (pixels)"), w_edit) formlayout.addRow(_("Height (pixels)"), h_edit) formlayout.addRow('', zbox) formlayout.addRow(_("Original size:"), QLabel("%d x %d" % old_size)) self.z_label = QLabel() formlayout.addRow(_("Zoom factor:"), self.z_label) # Button box self.bbox = bbox = QDialogButtonBox(QDialogButtonBox.Ok| QDialogButtonBox.Cancel) self.connect(bbox, SIGNAL("accepted()"), SLOT("accept()")) self.connect(bbox, SIGNAL("rejected()"), SLOT("reject()")) layout.addWidget(bbox) self.w_edit.setText(str(self.width)) self.h_edit.setText(str(self.height)) self.update_widgets() self.setWindowTitle(_("Resize")) self.connect(w_edit, SIGNAL("textChanged(QString)"), self.width_changed) self.connect(h_edit, SIGNAL("textChanged(QString)"), self.height_changed) self.connect(zbox, SIGNAL("toggled(bool)"), self.toggled_no_zoom) def update_widgets(self): valid = True for edit in (self.w_edit, self.h_edit): if not is_edit_valid(edit): valid = False self.bbox.button(QDialogButtonBox.Ok).setEnabled(valid) self.z_label.setText("%d %s" % (100*self.width/self.old_width, '%')) def width_changed(self, text): if is_edit_valid(self.sender()): self.width = int(text) self.height = int(self.width/self.ratio) self.h_edit.blockSignals(True) self.h_edit.setText(str(self.height)) self.h_edit.blockSignals(False) self.update_widgets() def height_changed(self, text): if is_edit_valid(self.sender()): self.height = int(text) self.width = int(self.ratio*self.height) self.w_edit.blockSignals(True) self.w_edit.setText(str(self.width)) self.w_edit.blockSignals(False) self.update_widgets() def toggled_no_zoom(self, state): self.keep_original_size = state if state: self.z_label.setText("100 %") self.bbox.button(QDialogButtonBox.Ok).setEnabled(True) else: self.update_widgets() for widget in (self.w_edit, self.h_edit): widget.setDisabled(state) def get_zoom(self): if self.keep_original_size: return 1 elif self.width > self.height: return self.width/self.old_width else: return self.height/self.old_height
class FitParam(object): def __init__(self, name, value, min, max, logscale=False, steps=5000, format='%.3f', size_offset=0, unit=''): self.name = name self.value = value self.min = min self.max = max self.logscale = logscale self.steps = steps self.format = format self.unit = unit self.prefix_label = None self.lineedit = None self.unit_label = None self.slider = None self.button = None self._widgets = [] self._size_offset = size_offset self._refresh_callback = None self.dataset = FitParamDataSet(title=_("Curve fitting parameter")) def copy(self): """Return a copy of this fitparam""" return self.__class__(self.name, self.value, self.min, self.max, self.logscale, self.steps, self.format, self._size_offset, self.unit) def create_widgets(self, parent, refresh_callback): self._refresh_callback = refresh_callback self.prefix_label = QLabel() font = self.prefix_label.font() font.setPointSize(font.pointSize() + self._size_offset) self.prefix_label.setFont(font) self.button = QPushButton() self.button.setIcon(get_icon('settings.png')) self.button.setToolTip( _("Edit '%s' fit parameter properties") % self.name) self.button.clicked.connect(lambda: self.edit_param(parent)) self.lineedit = QLineEdit() self.lineedit.editingFinished.connect(self.line_editing_finished) self.unit_label = QLabel(self.unit) self.slider = QSlider() self.slider.setOrientation(Qt.Horizontal) self.slider.setRange(0, self.steps - 1) self.slider.valueChanged.connect(self.slider_value_changed) self.update(refresh=False) self.add_widgets([ self.prefix_label, self.lineedit, self.unit_label, self.slider, self.button ]) def add_widgets(self, widgets): self._widgets += widgets def get_widgets(self): return self._widgets def set_scale(self, state): self.logscale = state > 0 self.update_slider_value() def set_text(self, fmt=None): style = "<span style=\'color: #444444\'><b>%s</b></span>" self.prefix_label.setText(style % self.name) if self.value is None: value_str = '' else: if fmt is None: fmt = self.format value_str = fmt % self.value self.lineedit.setText(value_str) self.lineedit.setDisabled(self.value == self.min and self.max == self.min) def line_editing_finished(self): try: self.value = float(self.lineedit.text()) except ValueError: self.set_text() self.update_slider_value() self._refresh_callback() def slider_value_changed(self, int_value): if self.logscale: total_delta = np.log10(1 + self.max - self.min) self.value = self.min + 10**(total_delta * int_value / (self.steps - 1)) - 1 else: total_delta = self.max - self.min self.value = self.min + total_delta * int_value / (self.steps - 1) self.set_text() self._refresh_callback() def update_slider_value(self): if (self.value is None or self.min is None or self.max is None): self.slider.setEnabled(False) if self.slider.parent() and self.slider.parent().isVisible(): self.slider.show() elif self.value == self.min and self.max == self.min: self.slider.hide() else: self.slider.setEnabled(True) if self.slider.parent() and self.slider.parent().isVisible(): self.slider.show() if self.logscale: value_delta = max([np.log10(1 + self.value - self.min), 0.]) total_delta = np.log10(1 + self.max - self.min) else: value_delta = self.value - self.min total_delta = self.max - self.min intval = int(self.steps * value_delta / total_delta) self.slider.blockSignals(True) self.slider.setValue(intval) self.slider.blockSignals(False) def edit_param(self, parent): update_dataset(self.dataset, self) if self.dataset.edit(parent=parent): restore_dataset(self.dataset, self) if self.value > self.max: self.max = self.value if self.value < self.min: self.min = self.value self.update() def update(self, refresh=True): self.unit_label.setText(self.unit) self.slider.setRange(0, self.steps - 1) self.update_slider_value() self.set_text() if refresh: self._refresh_callback()
class PiStageUi(QSplitter): stageConnected = Signal( ) # gets emitted if stage was sucessfully connected stopScan = Signal() xAxeChanged = Signal(object, object) updateCurrPos = Signal(object) def __init__(self, parent): #super(ObjectFT, self).__init__(Qt.Vertical, parent) super().__init__(parent) self.stage = None self.offset = 0. # offset from 0 where t0 is (mm) self.newOff = 0. self.stageRange = (0, 0) layoutWidget = QWidget() layout = QGridLayout() layoutWidget.setLayout(layout) # put layout together self.openStageBtn = QPushButton("Open stage") self.initStageBtn = QPushButton("Init stage") #absolute move #current position self.currentPos = QLabel('') #self.currentPos.setValidator(QDoubleValidator()) #relative move (mm) self.deltaMove_mm = QLineEdit() self.deltaMove_mm.setText('0') self.deltaMove_mm.setValidator(QDoubleValidator()) self.deltaMovePlus_mm = QPushButton('+') self.deltaMoveMinus_mm = QPushButton('-') #relative move (fs) self.deltaMove_fs = QLineEdit() self.deltaMovePlus_fs = QPushButton('+') self.deltaMoveMinus_fs = QPushButton('-') #velocity self.velocityLabel = QLabel('Velocity:') self.velocity = QSlider(Qt.Horizontal) self.velocity.setMinimum(0) self.velocity.setMaximum( 2000) # unit in µm; TODO: try to get max vel. from controller # scan from (fs) self.scanFrom = QLineEdit() self.scanFrom.setText('-100') self.scanFrom.setValidator(QIntValidator()) # scan to (fs) self.scanTo = QLineEdit() self.scanTo.setText('100') self.scanTo.setValidator(QIntValidator()) # scan stepsize (fs) self.scanStep = QLineEdit() self.scanStep.setText('10') self.scanStep.setValidator(QDoubleValidator()) # center here button self.centerBtn = QPushButton('Center here') self.centerBtn.setToolTip('Center scan at current stage position') self.startScanBtn = QPushButton("Start scan") self.stopScanBtn = QPushButton("Stop scan") self.niceBtn = QPushButton('Make it nice') # spacer line hLine = QFrame() hLine.setFrameStyle(QFrame.HLine) hLine.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Expanding) # put layout together layout.addWidget(self.openStageBtn, 0, 0) layout.addWidget(self.initStageBtn, 0, 1) layout.addWidget(QLabel("Current pos (mm):"), 1, 0) layout.addWidget(self.currentPos, 1, 1) layout.addWidget(self.velocityLabel, 2, 0) layout.addWidget(self.velocity, 3, 0, 1, 2) layout.addWidget(QLabel('Move relative (mm)'), 4, 0) layout.addWidget(self.deltaMove_mm, 5, 0, 1, 2) layout.addWidget(self.deltaMoveMinus_mm, 6, 0) layout.addWidget(self.deltaMovePlus_mm, 6, 1) layout.addWidget(QLabel('Move relative (fs)'), 7, 0) layout.addWidget(self.deltaMove_fs, 8, 0, 1, 2) layout.addWidget(self.deltaMoveMinus_fs, 9, 0) layout.addWidget(self.deltaMovePlus_fs, 9, 1) layout.addWidget(hLine, 10, 0, 1, 2) layout.addWidget(QLabel('Scan from (fs)'), 11, 0) layout.addWidget(self.scanFrom, 11, 1) layout.addWidget(QLabel('Scan to (fs)'), 12, 0) layout.addWidget(self.scanTo, 12, 1) layout.addWidget(QLabel('Stepsize (fs)'), 13, 0) layout.addWidget(self.scanStep, 13, 1) layout.addWidget(self.startScanBtn, 14, 0) layout.addWidget(self.stopScanBtn, 14, 1) layout.addWidget(self.centerBtn, 15, 1) layout.addWidget(self.niceBtn, 16, 1) layout.setRowStretch(17, 10) layout.setColumnStretch(2, 10) self.addWidget(layoutWidget) # make button and stuff functional self.openStageBtn.released.connect(self.connectStage) self.initStageBtn.released.connect(self.initStage) self.scanFrom.returnPressed.connect(self._xAxeChanged) self.scanTo.returnPressed.connect(self._xAxeChanged) self.centerBtn.released.connect(self._centerHere) self.deltaMovePlus_mm.released.connect( lambda x=1: self.moveRel_mm(float(self.deltaMove_mm.text()))) self.deltaMoveMinus_mm.released.connect( lambda x=-1: self.moveRel_mm(float(self.deltaMove_mm.text()), x)) ################ # thread for updating position #self.currPosThr = GenericThread(self.__getCurrPos) self.updateCurrPos.connect(self.__updateCurrPos) self.currPos_thread = QThread() # create the QThread self.currPos_thread.start() # This causes my_worker.run() to eventually execute in my_thread: self.currPos_worker = GenericWorker(self.__getCurrPos) self.currPos_worker.moveToThread(self.currPos_thread) # my_worker.finished.connect(self.xxx) #self.threadPool.append(my_thread) #self.my_worker = my_worker def connectStage(self): gcs = GCSDevice() try: gcs.InterfaceSetupDlg() msg = QMessageBox() msg.setIcon(QMessageBox.Information) msg.setText(gcs.qIDN()) msg.exec_() self.stage = gcs self.openStageBtn.setEnabled(False) except: msg = QMessageBox() msg.setIcon(QMessageBox.Critical) msg.setText('Could not connect stage') msg.exec_() def initStage(self): # TODO put this in thread and show egg clock if self.stage is not None: ## Create and display the splash screen #splash_pix = QPixmap('icons/piController.png') #splash = QSplashScreen(splash_pix, Qt.WindowStaysOnTopHint) #splash.setMask(splash_pix.mask()) #splash.show() # TODO: give choice to select stage pitools.startup(self.stage, stages='M-112.1DG-NEW', refmode='FNL') #splash.close() # TODO: show dialog for waiting self.velocityLabel.setText('Velocity: {:f}mm/s'.format( self.stage.qVEL()['1'])) self.velocity.setValue(int(1000 * self.stage.qVEL()['1'])) self.stageConnected.emit() self._xAxeChanged() self.currentPos.setText('{:.7f}'.format(self.stage.qPOS()['1'])) self.__startCurrPosThr() self.stageRange = (self.stage.qTMN()['1'], self.stage.qTMX()['1']) self.scanStep.validator().setBottom(0) self.initStageBtn.setEnabled(False) else: msg = QMessageBox() msg.setIcon(QMessageBox.Critical) msg.setText('No stage connected') msg.exec_() def gotoPos_mm(self, x): '''Move stage to absolute position in mm''' if self.stageRange[0] <= x <= self.stageRange[1]: self.stage.MOV(self.stage.axes, x) while not pitools.ontarget(self.stage, '1')['1']: time.sleep(0.05) else: print('Requested postition', x, 'outside of range', self.stageRange) def gotoPos_fs(self, x): '''Move stage to absolute position in mm''' self.gotoPos_mm(self._calcAbsPos(x)) def moveRel_mm(self, x=0, sign=1): '''Moves stage relative to current position''' # TODO raise message if outside of range currPos = float(self.currentPos.text()) if self.stageRange[0] <= sign * x + currPos <= self.stageRange[1]: self.stage.MVR(self.stage.axes, sign * x) else: print('Requested postition', x, 'outside of range', self.stageRange) def moveRel_fs(self, x=0, sign=1): '''Moves stage relative to current position; expexts fs''' # TODO raise message if outside of range self.moveRel_mm(self._calcAbsPos(x), sign) def _calcAbsPos(self, x): '''Calculate absolute position on stage from given femtosecond value gets x in fs and returns position mm''' return (x * fsDelay) + self.offset def getDelays_mm(self): '''expects fs and returns mm''' von = self._calcAbsPos(float(self.scanFrom.text())) bis = self._calcAbsPos(float(self.scanTo.text())) #stepSize = int(self.scanStep.text()) stepSize = float(self.scanStep.text()) * fsDelay return np.linspace(von, bis, (np.abs(von) + bis) / stepSize) def getDelays_fs(self): '''expects fs and returns mm''' von = float(self.scanFrom.text()) bis = float(self.scanTo.text()) #stepSize = int(self.scanStep.text()) stepSize = float(self.scanStep.text()) return np.linspace(von, bis, (np.abs(von) + bis) / stepSize) def _xAxeChanged(self): self.xAxeChanged.emit(int(self.scanFrom.text()), int(self.scanTo.text())) def setCenter(self): '''Slot which recieves the new center position in fs and sets offset in mm ''' if self.newOff != 0: self.offset += (self.newOff * fsDelay) print('offset', self.offset, self.newOff) self.newOff = 0. def newOffset(self, newOffset): self.newOff = newOffset def _centerHere(self): self.offset = self.stage.qPOS()['1'] def __startCurrPosThr(self): self.stopCurrPosThr = False #self.currPosThr.start() self.currPos_worker.start.emit() def __stopCurrPosThr(self): self.stopCurrPosThr = True while (self.currPosThr.isRunning()): time.sleep(0.03) def __getCurrPos(self): oldPos = self.stage.qPOS()['1'] while not self.stopCurrPosThr: newPos = self.stage.qPOS()['1'] if oldPos != newPos: oldPos = newPos self.updateCurrPos.emit(newPos) time.sleep(0.5) def __updateCurrPos(self, newPos): self.currentPos.setText('{:.7f}'.format(newPos))
class MaestroUi(QSplitter): connected = Signal() # gets emitted if stage was sucessfully connected newPlotData = Signal(object) updateAvgTxt = Signal(object) def __init__(self, parent): #super(ObjectFT, self).__init__(Qt.Vertical, parent) super().__init__(parent) self.meter = None self.collectData = True # bool for data collection thread self.avgData = Queue() # need data for averaging and set for holding all self.measure = False self.runDataThr = True self.measureData = [] self.startTime = None layoutWidget = QWidget() layout = QGridLayout() layoutWidget.setLayout(layout) ############## # gui elements self.ipEdit = QLineEdit() rx = QRegExp("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$") self.ipEdit.setValidator(QRegExpValidator(rx)) self.ipEdit.setText('127.0.0.1') self.portEdit = QLineEdit() self.portEdit.setValidator(QIntValidator(1, 65535, self)) self.portEdit.setText('5000') self.connectBtn = QPushButton('Connect') self.avgSpin = QSpinBox() self.avgSpin.setValue(1) self.avgSpin.setRange(1, 10000) self.currValDisp = QLabel('0.0') self.startMeasBtn = QPushButton('Start aq') self.stopMeasBtn = QPushButton('Stop aq') ############## # put layout together layout.addWidget(QLabel('IP Address:'), 0, 0) layout.addWidget(self.ipEdit, 1, 0) layout.addWidget(QLabel('Port:'), 0, 1) layout.addWidget(self.portEdit, 1, 1) layout.addWidget(self.connectBtn, 2, 1) layout.addWidget(QLabel('Averages'), 4, 0) layout.addWidget(self.avgSpin, 5, 0) layout.addWidget(self.currValDisp, 5, 1) layout.addWidget(self.startMeasBtn, 6, 0) layout.addWidget(self.stopMeasBtn, 6, 1) layout.setRowStretch(7, 10) self.addWidget(layoutWidget) ############## # Network stuff self.tcpClient = QtNetwork.QTcpSocket() self.tcpClient.readyRead.connect(self.__getSocketData) self.tcpClient.error.connect(lambda x: print(x)) ############## # make button and stuff functional self.connectBtn.released.connect(self.connectMeter) self.avgSpin.valueChanged.connect(self.changeAverage) self.startMeasBtn.released.connect(self._startMeasure) self.stopMeasBtn.released.connect(self._stopMeasure) ############## # thread for getting data from socket self.updateAvgTxt.connect(self.__updateAvgTxt) self.dataAq_Thr = QThread() self.dataAq_Thr.start() self.dataAq_worker = GenericWorker(self.__getData) self.dataAq_worker.moveToThread(self.dataAq_Thr) def connectMeter(self): print('connected') self.tcpClient.connectToHost(self.ipEdit.text(), int(self.portEdit.text())) self.tcpClient.write('start\n'.encode()) self.dataAq_worker.start.emit() def _startMeasure(self): self.measure = True self.measureData = [] # reinitialize measure data array time.sleep(0.1) # time to wait for first data to arrive self.startTime = datetime.now() # datetime object def _stopMeasure(self): self.measure = False #@Slot def __updateAvgTxt(self, text): ''' update current value label ''' self.currValDisp.setText(text) def changeAverage(self): shape = int(self.avgSpin.value()) self.dispData = np.zeros(shape) def __getData(self): ''' Function run in thread ''' while self.runDataThr: tmpData = np.array(int(self.avgSpin.text())*[[datetime.now(), 0]]) for i in range(len(tmpData)): tmpData[i] = self.avgData.get() if self.measure: self.measureData.append( (tmpData[i,0].isoformat().encode(), (tmpData[i,0]-self.startTime).total_seconds(), tmpData[i,1])) #print('mean', tmpData.mean()) self.updateAvgTxt.emit(str(tmpData[:,1].mean())) if self.measure: self.newPlotData.emit(np.float_(np.asarray(self.measureData)[:,1:])) self.avgData.task_done() #@Slot() def __getSocketData(self): ''' to be called if network buffer has more data push data to queue ''' self.avgData.put([datetime.now(), float(self.tcpClient.readLine(1024).decode().rstrip())]) def closeEvent(self, event): if self.tcpClient.isOpen(): self.RunDataThr = False self.tcpClient.write('stop\n'.encode()) time.sleep(0.1) self.tcpClient.close() print(self.tcpClient.isOpen()) #if self.console is not None: # self.console.exit_interpreter() event.accept()
class ResizeDialog(QDialog): def __init__(self, parent, new_size, old_size, text="", keep_original_size=False): QDialog.__init__(self, parent) intfunc = lambda tup: [int(val) for val in tup] if intfunc(new_size) == intfunc(old_size): self.keep_original_size = True else: self.keep_original_size = keep_original_size self.width, self.height = new_size self.old_width, self.old_height = old_size self.ratio = self.width/self.height layout = QVBoxLayout() self.setLayout(layout) formlayout = QFormLayout() layout.addLayout(formlayout) if text: label = QLabel(text) label.setAlignment(Qt.AlignHCenter) formlayout.addRow(label) self.w_edit = w_edit = QLineEdit(self) w_valid = QIntValidator(w_edit) w_valid.setBottom(1) w_edit.setValidator(w_valid) self.h_edit = h_edit = QLineEdit(self) h_valid = QIntValidator(h_edit) h_valid.setBottom(1) h_edit.setValidator(h_valid) zbox = QCheckBox(_("Original size"), self) formlayout.addRow(_("Width (pixels)"), w_edit) formlayout.addRow(_("Height (pixels)"), h_edit) formlayout.addRow('', zbox) formlayout.addRow(_("Original size:"), QLabel("%d x %d" % old_size)) self.z_label = QLabel() formlayout.addRow(_("Zoom factor:"), self.z_label) # Button box self.bbox = bbox = QDialogButtonBox(QDialogButtonBox.Ok| QDialogButtonBox.Cancel) bbox.accepted.connect(self.accept) bbox.rejected.connect(self.reject) layout.addWidget(bbox) self.w_edit.setText(str(self.width)) self.h_edit.setText(str(self.height)) self.update_widgets() self.setWindowTitle(_("Resize")) w_edit.textChanged.connect(self.width_changed) h_edit.textChanged.connect(self.height_changed) zbox.toggled.connect(self.toggled_no_zoom) zbox.setChecked(self.keep_original_size) def update_widgets(self): valid = True for edit in (self.w_edit, self.h_edit): if not is_edit_valid(edit): valid = False self.bbox.button(QDialogButtonBox.Ok).setEnabled(valid) self.z_label.setText("%d %s" % (100*self.width/self.old_width, '%')) def width_changed(self, text): if is_edit_valid(self.sender()): self.width = int(text) self.height = int(self.width/self.ratio) self.h_edit.blockSignals(True) self.h_edit.setText(str(self.height)) self.h_edit.blockSignals(False) self.update_widgets() def height_changed(self, text): if is_edit_valid(self.sender()): self.height = int(text) self.width = int(self.ratio*self.height) self.w_edit.blockSignals(True) self.w_edit.setText(str(self.width)) self.w_edit.blockSignals(False) self.update_widgets() def toggled_no_zoom(self, state): self.keep_original_size = state if state: self.z_label.setText("100 %") self.bbox.button(QDialogButtonBox.Ok).setEnabled(True) else: self.update_widgets() for widget in (self.w_edit, self.h_edit): widget.setDisabled(state) def get_zoom(self): if self.keep_original_size: return 1 elif self.width > self.height: return self.width/self.old_width else: return self.height/self.old_height