class RunDialog(QDialog): simulation_done = Signal(bool, str) def __init__(self, config_file, run_model, arguments, parent=None): QDialog.__init__(self, parent) self.setWindowFlags(Qt.Window) self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint) self.setModal(True) self.setWindowModality(Qt.WindowModal) self.setWindowTitle("Simulations - {}".format(config_file)) assert isinstance(run_model, BaseRunModel) self._run_model = run_model ert = None if isinstance(run_model, BaseRunModel): ert = run_model.ert() self._simulations_argments = arguments self.simulations_tracker = create_tracker( run_model, qtimer_cls=QTimer, event_handler=self._on_tracker_event, num_realizations=arguments["active_realizations"].count()) states = self.simulations_tracker.get_states() self.state_colors = {state.name: state.color for state in states} self.state_colors['Success'] = self.state_colors["Finished"] self.state_colors['Failure'] = self.state_colors["Failed"] self.total_progress = SimpleProgress() status_layout = QHBoxLayout() status_layout.addStretch() self.__status_label = QLabel() status_layout.addWidget(self.__status_label) status_layout.addStretch() status_widget_container = QWidget() status_widget_container.setLayout(status_layout) self.progress = Progress() self.progress.setIndeterminateColor(self.total_progress.color) for state in states: self.progress.addState(state.state, QColor(*state.color), 100.0 * state.count / state.total_count) legend_layout = QHBoxLayout() self.legends = {} for state in states: self.legends[state] = Legend("%s (%d/%d)", QColor(*state.color)) self.legends[state].updateLegend(state.name, 0, 0) legend_layout.addWidget(self.legends[state]) legend_widget_container = QWidget() legend_widget_container.setLayout(legend_layout) self.running_time = QLabel("") self.plot_tool = PlotTool(config_file) self.plot_tool.setParent(None) self.plot_button = QPushButton(self.plot_tool.getName()) self.plot_button.clicked.connect(self.plot_tool.trigger) self.plot_button.setEnabled(ert is not None) self.kill_button = QPushButton("Kill simulations") self.done_button = QPushButton("Done") self.done_button.setHidden(True) self.restart_button = QPushButton("Restart") self.restart_button.setHidden(True) self.show_details_button = QPushButton("Details") self.show_details_button.setCheckable(True) size = 20 spin_movie = resourceMovie("ide/loading.gif") spin_movie.setSpeed(60) spin_movie.setScaledSize(QSize(size, size)) spin_movie.start() self.processing_animation = QLabel() self.processing_animation.setMaximumSize(QSize(size, size)) self.processing_animation.setMinimumSize(QSize(size, size)) self.processing_animation.setMovie(spin_movie) button_layout = QHBoxLayout() button_layout.addWidget(self.processing_animation) button_layout.addWidget(self.running_time) button_layout.addStretch() button_layout.addWidget(self.show_details_button) button_layout.addWidget(self.plot_button) button_layout.addWidget(self.kill_button) button_layout.addWidget(self.done_button) button_layout.addWidget(self.restart_button) button_widget_container = QWidget() button_widget_container.setLayout(button_layout) self.detailed_progress = DetailedProgressWidget( self, self.state_colors) self.detailed_progress.setVisible(False) self.dummy_widget_container = QWidget( ) #Used to keep the other widgets from stretching layout = QVBoxLayout() layout.addWidget(self.total_progress) layout.addWidget(status_widget_container) layout.addWidget(self.progress) layout.addWidget(legend_widget_container) layout.addWidget(self.detailed_progress) layout.addWidget(self.dummy_widget_container) layout.addWidget(button_widget_container) layout.setStretch(0, 0) layout.setStretch(1, 0) layout.setStretch(2, 0) layout.setStretch(3, 0) layout.setStretch(4, 1) layout.setStretch(5, 1) layout.setStretch(6, 0) self.setLayout(layout) self.kill_button.clicked.connect(self.killJobs) self.done_button.clicked.connect(self.accept) self.restart_button.clicked.connect(self.restart_failed_realizations) self.show_details_button.clicked.connect(self.toggle_detailed_progress) self.simulation_done.connect(self._on_simulation_done) def reject(self): return def closeEvent(self, QCloseEvent): self.simulations_tracker.stop() if self._run_model.isFinished(): self.simulation_done.emit(self._run_model.hasRunFailed(), self._run_model.getFailMessage()) else: # Kill jobs if dialog is closed if self.killJobs() != QMessageBox.Yes: QCloseEvent.ignore() def startSimulation(self): self._run_model.reset() self.simulations_tracker.reset() def run(): self._run_model.startSimulations(self._simulations_argments) simulation_thread = Thread(name="ert_gui_simulation_thread") simulation_thread.setDaemon(True) simulation_thread.run = run simulation_thread.start() self.simulations_tracker.track() def killJobs(self): msg = "Are you sure you want to kill the currently running simulations?" if self._run_model.getQueueStatus().get( JobStatusType.JOB_QUEUE_UNKNOWN, 0) > 0: msg += "\n\nKilling a simulation with unknown status will not kill the realizations already submitted!" kill_job = QMessageBox.question(self, "Kill simulations?", msg, QMessageBox.Yes | QMessageBox.No) if kill_job == QMessageBox.Yes: if self._run_model.killAllSimulations(): self.reject() return kill_job @Slot(bool, str) def _on_simulation_done(self, failed, failed_msg): self.simulations_tracker.stop() self.processing_animation.hide() self.kill_button.setHidden(True) self.done_button.setHidden(False) self.restart_button.setVisible(self.has_failed_realizations()) self.restart_button.setEnabled(self._run_model.support_restart) if failed: QMessageBox.critical( self, "Simulations failed!", "The simulation failed with the following " + "error:\n\n{}".format(failed_msg)) @Slot(object) def _on_tracker_event(self, event): if isinstance(event, TickEvent): self.running_time.setText(format_running_time(event.runtime)) if isinstance(event, GeneralEvent): self.total_progress.setProgress(event.progress) self.progress.setIndeterminate(event.indeterminate) if event.indeterminate: for state in event.sim_states: self.legends[state].updateLegend(state.name, 0, 0) else: for state in event.sim_states: self.progress.updateState( state.state, 100.0 * state.count / state.total_count) self.legends[state].updateLegend(state.name, state.count, state.total_count) if isinstance(event, DetailedEvent): if not self.progress.get_indeterminate(): self.detailed_progress.set_progress(event.details, event.iteration) if isinstance(event, EndEvent): self.simulation_done.emit(event.failed, event.failed_msg) def has_failed_realizations(self): completed = self._run_model.completed_realizations_mask initial = self._run_model.initial_realizations_mask for (index, successful) in enumerate(completed): if initial[index] and not successful: return True return False def count_successful_realizations(self): """ Counts the realizations completed in the prevoius ensemble run :return: """ completed = self._run_model.completed_realizations_mask return completed.count(True) def create_mask_from_failed_realizations(self): """ Creates a BoolVector mask representing the failed realizations :return: Type BoolVector """ completed = self._run_model.completed_realizations_mask initial = self._run_model.initial_realizations_mask inverted_mask = BoolVector(default_value=False) for (index, successful) in enumerate(completed): inverted_mask[index] = initial[index] and not successful return inverted_mask def restart_failed_realizations(self): msg = QMessageBox(self) msg.setIcon(QMessageBox.Information) msg.setText( "Note that workflows will only be executed on the restarted realizations and that this might have unexpected consequences." ) msg.setWindowTitle("Restart Failed Realizations") msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel) result = msg.exec_() if result == QMessageBox.Ok: self.restart_button.setVisible(False) self.kill_button.setVisible(True) self.done_button.setVisible(False) active_realizations = self.create_mask_from_failed_realizations() self._simulations_argments[ 'active_realizations'] = active_realizations self._simulations_argments[ 'prev_successful_realizations'] = self._simulations_argments.get( 'prev_successful_realizations', 0) self._simulations_argments[ 'prev_successful_realizations'] += self.count_successful_realizations( ) self.startSimulation() def toggle_detailed_progress(self): self.detailed_progress.setVisible(not ( self.detailed_progress.isVisible())) self.dummy_widget_container.setVisible(not ( self.detailed_progress.isVisible())) self.adjustSize()
class QtPluginDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) self.installer = Installer() self.setup_ui() self.installer.set_output_widget(self.stdout_text) self.installer.process.started.connect(self._on_installer_start) self.installer.process.finished.connect(self._on_installer_done) self.refresh() def _on_installer_start(self): self.show_status_btn.setChecked(True) self.working_indicator.show() self.process_error_indicator.hide() def _on_installer_done(self, exit_code, exit_status): self.working_indicator.hide() if exit_code: self.process_error_indicator.show() else: self.show_status_btn.setChecked(False) self.refresh() self.plugin_sorter.refresh() def refresh(self): self.installed_list.clear() self.available_list.clear() # fetch installed from ...plugins import plugin_manager plugin_manager.discover() # since they might not be loaded yet already_installed = set() for plugin_name, mod_name, distname in plugin_manager.iter_available(): # not showing these in the plugin dialog if plugin_name in ('napari_plugin_engine', ): continue if distname: already_installed.add(distname) meta = standard_metadata(distname) else: meta = {} self.installed_list.addItem( ProjectInfo( normalized_name(distname or ''), meta.get('version', ''), meta.get('url', ''), meta.get('summary', ''), meta.get('author', ''), meta.get('license', ''), ), plugin_name=plugin_name, enabled=plugin_name in plugin_manager.plugins, ) # self.v_splitter.setSizes([70 * self.installed_list.count(), 10, 10]) # fetch available plugins self.worker = create_worker(iter_napari_plugin_info) def _handle_yield(project_info): if project_info.name in already_installed: self.installed_list.tag_outdated(project_info) else: self.available_list.addItem(project_info) self.worker.yielded.connect(_handle_yield) self.worker.finished.connect(self.working_indicator.hide) self.worker.finished.connect(self._update_count_in_label) self.worker.start() def setup_ui(self): self.resize(1080, 640) vlay_1 = QVBoxLayout(self) self.h_splitter = QSplitter(self) vlay_1.addWidget(self.h_splitter) self.h_splitter.setOrientation(Qt.Horizontal) self.v_splitter = QSplitter(self.h_splitter) self.v_splitter.setOrientation(Qt.Vertical) self.v_splitter.setMinimumWidth(500) self.plugin_sorter = QtPluginSorter(parent=self.h_splitter) self.plugin_sorter.layout().setContentsMargins(2, 0, 0, 0) self.plugin_sorter.hide() installed = QWidget(self.v_splitter) lay = QVBoxLayout(installed) lay.setContentsMargins(0, 2, 0, 2) lay.addWidget(QLabel("Installed Plugins")) self.installed_list = QPluginList(installed, self.installer) lay.addWidget(self.installed_list) uninstalled = QWidget(self.v_splitter) lay = QVBoxLayout(uninstalled) lay.setContentsMargins(0, 2, 0, 2) self.avail_label = QLabel("Available Plugins") lay.addWidget(self.avail_label) self.available_list = QPluginList(uninstalled, self.installer) lay.addWidget(self.available_list) self.stdout_text = QTextEdit(self.v_splitter) self.stdout_text.setReadOnly(True) self.stdout_text.setObjectName("pip_install_status") self.stdout_text.hide() buttonBox = QHBoxLayout() self.working_indicator = QLabel("loading ...", self) sp = self.working_indicator.sizePolicy() sp.setRetainSizeWhenHidden(True) self.working_indicator.setSizePolicy(sp) self.process_error_indicator = QLabel(self) self.process_error_indicator.setObjectName("error_label") self.process_error_indicator.hide() load_gif = str(Path(napari.resources.__file__).parent / "loading.gif") mov = QMovie(load_gif) mov.setScaledSize(QSize(18, 18)) self.working_indicator.setMovie(mov) mov.start() self.direct_entry_edit = QLineEdit(self) self.direct_entry_edit.installEventFilter(self) self.direct_entry_edit.setPlaceholderText( 'install by name/url, or drop file...') self.direct_entry_btn = QPushButton("Install", self) self.direct_entry_btn.clicked.connect(self._install_packages) self.show_status_btn = QPushButton("Show Status", self) self.show_status_btn.setFixedWidth(100) self.show_sorter_btn = QPushButton("<< Show Sorter", self) self.close_btn = QPushButton("Close", self) self.close_btn.clicked.connect(self.reject) buttonBox.addWidget(self.show_status_btn) buttonBox.addWidget(self.working_indicator) buttonBox.addWidget(self.direct_entry_edit) buttonBox.addWidget(self.direct_entry_btn) buttonBox.addWidget(self.process_error_indicator) buttonBox.addSpacing(60) buttonBox.addWidget(self.show_sorter_btn) buttonBox.addWidget(self.close_btn) buttonBox.setContentsMargins(0, 0, 4, 0) vlay_1.addLayout(buttonBox) self.show_status_btn.setCheckable(True) self.show_status_btn.setChecked(False) self.show_status_btn.toggled.connect(self._toggle_status) self.show_sorter_btn.setCheckable(True) self.show_sorter_btn.setChecked(False) self.show_sorter_btn.toggled.connect(self._toggle_sorter) self.v_splitter.setStretchFactor(1, 2) self.h_splitter.setStretchFactor(0, 2) def _update_count_in_label(self): count = self.available_list.count() self.avail_label.setText(f"Available Plugins ({count})") def eventFilter(self, watched, event): if event.type() == QEvent.DragEnter: # we need to accept this event explicitly to be able # to receive QDropEvents! event.accept() if event.type() == QEvent.Drop: md = event.mimeData() if md.hasUrls(): files = [url.toLocalFile() for url in md.urls()] self.direct_entry_edit.setText(files[0]) return True return super().eventFilter(watched, event) def _toggle_sorter(self, show): if show: self.show_sorter_btn.setText(">> Hide Sorter") self.plugin_sorter.show() else: self.show_sorter_btn.setText("<< Show Sorter") self.plugin_sorter.hide() def _toggle_status(self, show): if show: self.show_status_btn.setText("Hide Status") self.stdout_text.show() else: self.show_status_btn.setText("Show Status") self.stdout_text.hide() def _install_packages(self, packages: Sequence[str] = ()): if not packages: _packages = self.direct_entry_edit.text() if os.path.exists(_packages): packages = [_packages] else: packages = _packages.split() self.direct_entry_edit.clear() if packages: self.installer.install(packages)
class QtPluginSorter(QWidget): """Dialog that allows a user to change the call order of plugin hooks. A main QComboBox lets the user pick which hook specification they would like to reorder. Then a :class:`QtHookImplementationListWidget` shows the current call order for all implementations of the current hook specification. The user may then reorder them, or disable them by checking the checkbox next to each hook implementation name. Parameters ---------- plugin_manager : PluginManager, optional An instance of a PluginManager. by default, the main :class:`~napari.plugins.manager.PluginManager` instance parent : QWidget, optional Optional parent widget, by default None initial_hook : str, optional If provided the QComboBox at the top of the dialog will be set to this hook, by default None firstresult_only : bool, optional If True, only hook specifications that declare the "firstresult" option will be included. (these are hooks for which only the first non None result is returned). by default True (because it makes less sense to sort hooks where we just collect all results anyway) https://pluggy.readthedocs.io/en/latest/#first-result-only Attributes ---------- hook_combo_box : QComboBox A dropdown menu to select the current hook. hook_list : QtHookImplementationListWidget The list widget that displays (and allows sorting of) all of the hook implementations for the currently selected hook. """ NULL_OPTION = 'select hook... ' def __init__( self, plugin_manager: PluginManager = napari_plugin_manager, *, parent: Optional[QWidget] = None, initial_hook: Optional[str] = None, firstresult_only: bool = True, ): super().__init__(parent) self.plugin_manager = plugin_manager self.hook_combo_box = QComboBox() self.hook_combo_box.addItem(self.NULL_OPTION, None) # populate comboBox with all of the hooks known by the plugin manager for name, hook_caller in plugin_manager.hooks.items(): # only show hooks with specifications if not hook_caller.spec: continue if firstresult_only: # if the firstresult_only option is set # we only want to include hook_specifications that declare the # "firstresult" option as True. if not hook_caller.spec.opts.get('firstresult', False): continue self.hook_combo_box.addItem(name.replace("napari_", ""), hook_caller) self.hook_combo_box.setToolTip( "select the hook specification to reorder") self.hook_combo_box.currentIndexChanged.connect(self._on_hook_change) self.hook_list = QtHookImplementationListWidget(parent=self) title = QLabel('Plugin Sorter') title.setObjectName("h3") instructions = QLabel( 'Select a hook to rearrange, then drag and ' 'drop plugins into the desired call order.\n\n' 'Disable plugins for a specific hook by unchecking their checkbox.' ) instructions.setWordWrap(True) self.docstring = QLabel(self) self.info = QLabel(self) self.info.setObjectName("info_icon") doc_lay = QHBoxLayout() doc_lay.addWidget(self.docstring) doc_lay.setStretch(0, 1) doc_lay.addWidget(self.info) self.docstring.setWordWrap(True) self.docstring.setObjectName('small_text') self.info.hide() self.docstring.hide() layout = QVBoxLayout(self) layout.addWidget(title) layout.addWidget(instructions) layout.addWidget(self.hook_combo_box) layout.addLayout(doc_lay) layout.addWidget(self.hook_list) if initial_hook is not None: self.set_hookname(initial_hook) def set_hookname(self, hook: str): """Change the hook specification shown in the list widget. Parameters ---------- hook : str Name of the new hook specification to show. """ self.hook_combo_box.setCurrentText(hook.replace("napari_", '')) def _on_hook_change(self, index): hook_caller = self.hook_combo_box.currentData() self.hook_list.set_hook_caller(hook_caller) if hook_caller: doc = hook_caller.spec.function.__doc__ html = rst2html(doc.split("Parameters")[0].strip()) summary, fulldoc = html.split('<br>', 1) while fulldoc.startswith('<br>'): fulldoc = fulldoc[4:] self.docstring.setText(summary.strip()) self.docstring.show() self.info.show() self.info.setToolTip(fulldoc.strip()) else: self.docstring.hide() self.info.hide() self.docstring.setToolTip('') def refresh(self): self._on_hook_change(self.hook_combo_box.currentIndex())
class DimensionMDE(Dimension): binningChanged = Signal() """ MDEventWorkspace has additional properties for either number_of_bins or thickness from mantidqt.widgets.sliceviewer.dimensionwidget import DimensionMDE from qtpy.QtWidgets import QApplication app = QApplication([]) window = DimensionMDE({'minimum':-1.1, 'number_of_bins':11, 'width':0.2, 'name':'Dim0', 'units':'A'}) window.show() app.exec_() """ def __init__(self, dim_info, number=0, state=State.NONE, parent=None): # hack in a number_of_bins for MDEventWorkspace dim_info['number_of_bins'] = 1000 dim_info['width'] = (dim_info['maximum']-dim_info['minimum'])/1000 self.spinBins = QSpinBox() self.spinBins.setRange(2,9999) self.spinBins.setValue(100) self.spinBins.hide() self.spinBins.setMinimumWidth(110) self.spinThick = QDoubleSpinBox() self.spinThick.setRange(0.001,999) self.spinThick.setValue(0.1) self.spinThick.setSingleStep(0.1) self.spinThick.setDecimals(3) self.spinThick.setMinimumWidth(110) self.rebinLabel = QLabel("thick") self.rebinLabel.setMinimumWidth(44) super(DimensionMDE, self).__init__(dim_info, number, state, parent) self.spinBins.valueChanged.connect(self.binningChanged) self.spinThick.valueChanged.connect(self.valueChanged) self.layout.addWidget(self.spinBins) self.layout.addWidget(self.spinThick) self.layout.addWidget(self.rebinLabel) def get_bins(self): return int(self.spinBins.value()) def get_thickness(self): return float(self.spinThick.value()) def set_state(self, state): super(DimensionMDE, self).set_state(state) if self.state == State.X: self.spinBins.show() self.spinThick.hide() self.rebinLabel.setText('bins') elif self.state == State.Y: self.spinBins.show() self.spinThick.hide() self.rebinLabel.setText('bins') elif self.state == State.NONE: self.spinBins.hide() self.spinThick.show() self.rebinLabel.setText('thick') else: self.spinBins.hide() self.spinThick.hide() self.rebinLabel.hide()
class PreferencesWindow(PyDialog): """ +-------------+ | Preferences | +---------------------------------+ | Text Size ______ Default | | Annotation Color ______ | | Annotation Size ______ | | Picker Size ______ | | Back Color ______ | | Text Color ______ | | | | Apply OK Cancel | +---------------------------------+ """ def __init__(self, data, win_parent=None): """ Saves the data members from data and performs type checks """ PyDialog.__init__(self, data, win_parent) self._updated_preference = False self._default_font_size = data['font_size'] self._default_text_size = 14 self._default_annotation_size = 18 self._default_coord_scale = 0.05 * 100. self._default_coord_text_scale = 0.5 * 100. self._default_clipping_min = data['min_clip'] self._default_clipping_max = data['max_clip'] #self._default_annotation_size = data['annotation_size'] # int #self.default_magnify = data['magnify'] self.dim_max = data['dim_max'] self._use_gradient_background = data['use_gradient_background'] # bool self._show_corner_coord = data['show_corner_coord'] self._annotation_size = data['annotation_size'] # int #self.out_data = data self._picker_size = data['picker_size'] * 100. self._coord_scale = data['coord_scale'] * 100. self._coord_text_scale = data['coord_text_scale'] * 100. self._magnify = data['magnify'] self._text_size = data['text_size'] self._highlight_opacity = data['highlight_opacity'] self.annotation_color_float, self.annotation_color_int = _check_color( data['annotation_color']) self.background_color_float, self.background_color_int = _check_color( data['background_color']) self.background_color2_float, self.background_color2_int = _check_color( data['background_color2']) self.text_color_float, self.text_color_int = _check_color( data['text_color']) self.highlight_color_float, self.highlight_color_int = _check_color( data['highlight_color']) self._nastran_is_element_quality = data['nastran_is_element_quality'] self._nastran_is_properties = data['nastran_is_properties'] self._nastran_is_3d_bars = data['nastran_is_3d_bars'] self._nastran_is_3d_bars_update = data['nastran_is_3d_bars_update'] self._nastran_is_bar_axes = data['nastran_is_bar_axes'] self._nastran_create_coords = data['nastran_create_coords'] self.setWindowTitle('Preferences') self.create_widgets() self.create_layout() self.set_connections() self.on_font(self._default_font_size) self.on_gradient_scale() #self.show() def create_widgets(self): """creates the display window""" # Text Size self.font_size_label = QLabel("Font Size:") self.font_size_edit = QSpinBox(self) self.font_size_edit.setValue(self._default_font_size) self.font_size_edit.setRange(7, 20) self.font_size_button = QPushButton("Default") #----------------------------------------------------------------------- # Annotation Color self.annotation_color_label = QLabel("Annotation Color:") self.annotation_color_edit = QPushButtonColor( self.annotation_color_int) self.annotation_color_label.hide() self.annotation_color_edit.hide() #----------------------------------------------------------------------- # Text Color self.text_size_label = QLabel("Text Size:") self.text_size_edit = QSpinBox(self) self.text_size_edit.setValue(self._default_text_size) self.text_size_edit.setRange(7, 30) self.text_size_button = QPushButton("Default") # Text Color self.text_color_label = QLabel("Text Color:") self.text_color_edit = QPushButtonColor(self.text_color_int) #----------------------------------------------------------------------- # Highlight Color self.highlight_opacity_label = QLabel("Highlight Opacity:") self.highlight_opacity_edit = QDoubleSpinBox(self) self.highlight_opacity_edit.setValue(self._highlight_opacity) self.highlight_opacity_edit.setRange(0.1, 1.0) self.highlight_opacity_edit.setDecimals(1) self.highlight_opacity_edit.setSingleStep(0.1) self.highlight_opacity_button = QPushButton("Default") # Text Color self.highlight_color_label = QLabel("Highlight Color:") self.highlight_color_edit = QPushButtonColor(self.highlight_color_int) #----------------------------------------------------------------------- # Background Color self.background_color_label = QLabel("Btm Background Color:") self.background_color_edit = QPushButtonColor( self.background_color_int) # Background Color2 self.gradient_scale_label = QLabel("Gradient Background:") self.gradient_scale_checkbox = QCheckBox() self.gradient_scale_checkbox.setChecked(self._use_gradient_background) self.background_color2_label = QLabel("Top Background Color:") self.background_color2_edit = QPushButtonColor( self.background_color2_int) #----------------------------------------------------------------------- # Annotation Size self.annotation_size_label = QLabel("Annotation Size:") self.annotation_size_edit = QSpinBox(self) self.annotation_size_edit.setRange(1, 500) self.annotation_size_edit.setValue(self._annotation_size) self.annotation_size_button = QPushButton("Default") #----------------------------------------------------------------------- # Picker Size self.picker_size_label = QLabel("Picker Size (% of Screen):") self.picker_size_edit = QDoubleSpinBox(self) self.picker_size_edit.setRange(0., 10.) log_dim = log10(self.dim_max) decimals = int(ceil(abs(log_dim))) decimals = max(6, decimals) self.picker_size_edit.setDecimals(decimals) self.picker_size_edit.setSingleStep(10. / 5000.) self.picker_size_edit.setValue(self._picker_size) #----------------------------------------------------------------------- # Clipping Min self.clipping_min_label = QLabel("Clipping Min:") self.clipping_min_edit = QLineEdit(str(self._default_clipping_min)) self.clipping_min_button = QPushButton("Default") # Clipping Max self.clipping_max_label = QLabel("Clipping Max:") self.clipping_max_edit = QLineEdit(str(self._default_clipping_max)) self.clipping_max_button = QPushButton("Default") #----------------------------------------------------------------------- self.coord_scale_label = QLabel('Coordinate System Scale:') self.coord_scale_button = QPushButton("Default") self.coord_scale_edit = QDoubleSpinBox(self) self.coord_scale_edit.setRange(0.1, 1000.) self.coord_scale_edit.setDecimals(3) self.coord_scale_edit.setSingleStep(2.5) self.coord_scale_edit.setValue(self._coord_scale) self.coord_text_scale_label = QLabel('Coordinate System Text Scale:') self.coord_text_scale_button = QPushButton("Default") self.coord_text_scale_edit = QDoubleSpinBox(self) self.coord_text_scale_edit.setRange(0.1, 2000.) self.coord_text_scale_edit.setDecimals(3) self.coord_text_scale_edit.setSingleStep(2.5) self.coord_text_scale_edit.setValue(self._coord_text_scale) # Show corner coord self.corner_coord_label = QLabel("Show Corner Coordinate System:") self.corner_coord_checkbox = QCheckBox() self.corner_coord_checkbox.setChecked(self._show_corner_coord) #----------------------------------------------------------------------- self.magnify_label = QLabel('Screenshot Magnify:') self.magnify_edit = QSpinBox(self) self.magnify_edit.setMinimum(1) self.magnify_edit.setMaximum(10) self.magnify_edit.setValue(self._magnify) #----------------------------------------------------------------------- self.nastran_is_element_quality_checkbox = QCheckBox('Element Quality') self.nastran_is_properties_checkbox = QCheckBox('Properties') self.nastran_is_3d_bars_checkbox = QCheckBox('3D Bars') self.nastran_is_3d_bars_update_checkbox = QCheckBox('Update 3D Bars') self.nastran_create_coords_checkbox = QCheckBox('Coords') self.nastran_is_bar_axes_checkbox = QCheckBox('Bar Axes') self.nastran_is_3d_bars_checkbox.setDisabled(True) self.nastran_is_3d_bars_update_checkbox.setDisabled(True) #self.nastran_is_bar_axes_checkbox.setDisabled(True) self.nastran_is_element_quality_checkbox.setChecked( self._nastran_is_element_quality) self.nastran_is_properties_checkbox.setChecked( self._nastran_is_properties) self.nastran_is_3d_bars_checkbox.setChecked(self._nastran_is_3d_bars) #self.nastran_is_3d_bars_update_checkbox.setChecked(self._nastran_is_3d_bars_update) self.nastran_is_3d_bars_update_checkbox.setChecked(False) self.nastran_is_bar_axes_checkbox.setChecked(self._nastran_is_bar_axes) self.nastran_create_coords_checkbox.setChecked( self._nastran_create_coords) #----------------------------------------------------------------------- # closing self.apply_button = QPushButton("Apply") self.ok_button = QPushButton("OK") self.cancel_button = QPushButton("Cancel") #def create_legend_widgets(self): #""" #Creates the widgets for the legend control #Name Itailic Bold Font #==== ======= ===== ======== #Title check check pulldown #Label check check pulldown #""" #self.name_label = QLabel("Name:") #self.italic_label = QLabel("Italic:") #self.bold_label = QLabel("Bold:") #self.font_label = QLabel("Font:") #self.legend_label = QLabel("Legend:") #self.legend_title_name = QLabel("Title") #self.legend_title_italic_check = QCheckBox() #self.legend_title_bold_check = QCheckBox() #self.legend_title_font_edit = QComboBox() #self.legend_title_font_edit.addItems(['cat', 'dog', 'frog']) #self.legend_label_italic_name = QLabel("Label") #self.legend_label_italic_check = QCheckBox() #self.legend_label_bold_check = QCheckBox() #self.legend_label_font_edit = QComboBox() #self.legend_label_font_edit.addItems(['cat2', 'dog2', 'frog2']) #def create_legend_layout(self): #""" #Creates the layout for the legend control #Name Itailic Bold Font #==== ======= ===== ======== #Title check check pulldown #Label check check pulldown #""" #grid2 = QGridLayout() #grid2.addWidget(self.legend_label, 0, 0) #grid2.addWidget(self.name_label, 1, 0) #grid2.addWidget(self.italic_label, 1, 1) #grid2.addWidget(self.bold_label, 1, 2) #grid2.addWidget(self.font_label, 1, 3) #grid2.addWidget(self.legend_title_name, 2, 0) #grid2.addWidget(self.legend_title_italic_check, 2, 1) #grid2.addWidget(self.legend_title_bold_check, 2, 2) #grid2.addWidget(self.legend_title_font_edit, 2, 3) #grid2.addWidget(self.legend_label_italic_name, 3, 0) #grid2.addWidget(self.legend_label_italic_check, 3, 1) #grid2.addWidget(self.legend_label_bold_check, 3, 2) #grid2.addWidget(self.legend_label_font_edit, 3, 3) #return grid2 def create_layout(self): grid = QGridLayout() irow = 0 grid.addWidget(self.font_size_label, irow, 0) grid.addWidget(self.font_size_edit, irow, 1) grid.addWidget(self.font_size_button, irow, 2) irow += 1 grid.addWidget(self.gradient_scale_label, irow, 0) grid.addWidget(self.gradient_scale_checkbox, irow, 1) irow += 1 grid.addWidget(self.background_color2_label, irow, 0) grid.addWidget(self.background_color2_edit, irow, 1) irow += 1 grid.addWidget(self.background_color_label, irow, 0) grid.addWidget(self.background_color_edit, irow, 1) irow += 1 grid.addWidget(self.highlight_color_label, irow, 0) grid.addWidget(self.highlight_color_edit, irow, 1) irow += 1 grid.addWidget(self.highlight_opacity_label, irow, 0) grid.addWidget(self.highlight_opacity_edit, irow, 1) irow += 1 grid.addWidget(self.text_color_label, irow, 0) grid.addWidget(self.text_color_edit, irow, 1) irow += 1 grid.addWidget(self.text_size_label, irow, 0) grid.addWidget(self.text_size_edit, irow, 1) grid.addWidget(self.text_size_button, irow, 2) irow += 1 grid.addWidget(self.annotation_color_label, irow, 0) grid.addWidget(self.annotation_color_edit, irow, 1) irow += 1 grid.addWidget(self.annotation_size_label, irow, 0) grid.addWidget(self.annotation_size_edit, irow, 1) grid.addWidget(self.annotation_size_button, irow, 2) irow += 1 #grid.addWidget(self.clipping_min_label, irow, 0) #grid.addWidget(self.clipping_min_edit, irow, 1) #grid.addWidget(self.clipping_min_button, irow, 2) #irow += 1 #grid.addWidget(self.clipping_max_label, irow, 0) #grid.addWidget(self.clipping_max_edit, irow, 1) #grid.addWidget(self.clipping_max_button, irow, 2) #irow += 1 grid.addWidget(self.corner_coord_label, irow, 0) grid.addWidget(self.corner_coord_checkbox, irow, 1) irow += 1 grid.addWidget(self.coord_scale_label, irow, 0) grid.addWidget(self.coord_scale_edit, irow, 1) grid.addWidget(self.coord_scale_button, irow, 2) irow += 1 grid.addWidget(self.coord_text_scale_label, irow, 0) grid.addWidget(self.coord_text_scale_edit, irow, 1) grid.addWidget(self.coord_text_scale_button, irow, 2) irow += 1 #----------------------------------------------- grid.addWidget(self.magnify_label, irow, 0) grid.addWidget(self.magnify_edit, irow, 1) irow += 1 grid.addWidget(self.picker_size_label, irow, 0) grid.addWidget(self.picker_size_edit, irow, 1) irow += 1 #-------------------------------------------------- grid_nastran = QGridLayout() irow = 0 grid_nastran.addWidget(self.nastran_create_coords_checkbox, irow, 0) irow += 1 grid_nastran.addWidget(self.nastran_is_element_quality_checkbox, irow, 0) grid_nastran.addWidget(self.nastran_is_properties_checkbox, irow, 1) irow += 1 grid_nastran.addWidget(self.nastran_is_bar_axes_checkbox, irow, 0) irow += 1 grid_nastran.addWidget(self.nastran_is_3d_bars_checkbox, irow, 0) grid_nastran.addWidget(self.nastran_is_3d_bars_update_checkbox, irow, 1) irow += 1 #bold_font = make_font(self._default_font_size, is_bold=True) vbox_nastran = QVBoxLayout() self.nastran_label = QLabel('Nastran:') vbox_nastran.addWidget(self.nastran_label) vbox_nastran.addLayout(grid_nastran) #self.create_legend_widgets() #grid2 = self.create_legend_layout() ok_cancel_box = QHBoxLayout() ok_cancel_box.addWidget(self.apply_button) ok_cancel_box.addWidget(self.ok_button) ok_cancel_box.addWidget(self.cancel_button) vbox = QVBoxLayout() vbox.addLayout(grid) vbox.addLayout(vbox_nastran) #vbox.addStretch() #vbox.addLayout(grid2) vbox.addStretch() vbox.addLayout(ok_cancel_box) self.setLayout(vbox) def set_connections(self): """creates the actions for the menu""" self.font_size_button.clicked.connect(self.on_default_font_size) self.font_size_edit.valueChanged.connect(self.on_font) self.annotation_size_edit.editingFinished.connect( self.on_annotation_size) self.annotation_size_edit.valueChanged.connect(self.on_annotation_size) self.annotation_color_edit.clicked.connect(self.on_annotation_color) self.annotation_size_button.clicked.connect( self.on_default_annotation_size) self.background_color_edit.clicked.connect(self.on_background_color) self.background_color2_edit.clicked.connect(self.on_background_color2) self.gradient_scale_checkbox.clicked.connect(self.on_gradient_scale) self.highlight_color_edit.clicked.connect(self.on_highlight_color) self.highlight_opacity_edit.valueChanged.connect( self.on_highlight_opacity) self.text_color_edit.clicked.connect(self.on_text_color) self.text_size_edit.valueChanged.connect(self.on_text_size) self.text_size_button.clicked.connect(self.on_default_text_size) self.picker_size_edit.valueChanged.connect(self.on_picker_size) self.picker_size_edit.editingFinished.connect(self.on_picker_size) self.coord_scale_edit.valueChanged.connect(self.on_coord_scale) self.coord_scale_edit.editingFinished.connect(self.on_coord_scale) self.coord_scale_button.clicked.connect(self.on_default_coord_scale) self.corner_coord_checkbox.clicked.connect(self.on_corner_coord) self.coord_text_scale_edit.valueChanged.connect( self.on_coord_text_scale) self.coord_text_scale_edit.editingFinished.connect( self.on_coord_text_scale) self.coord_text_scale_button.clicked.connect( self.on_default_coord_text_scale) self.magnify_edit.valueChanged.connect(self.on_magnify) self.magnify_edit.editingFinished.connect(self.on_magnify) self.clipping_min_button.clicked.connect(self.on_default_clipping_min) self.clipping_max_button.clicked.connect(self.on_default_clipping_max) #------------------------------------ # format-specific self.nastran_is_element_quality_checkbox.clicked.connect( self.on_nastran_is_element_quality) self.nastran_is_properties_checkbox.clicked.connect( self.on_nastran_is_properties) self.nastran_is_3d_bars_checkbox.clicked.connect( self.on_nastran_is_3d_bars) self.nastran_is_3d_bars_update_checkbox.clicked.connect( self.on_nastran_is_3d_bars) self.nastran_is_bar_axes_checkbox.clicked.connect( self.on_nastran_is_bar_axes) self.nastran_create_coords_checkbox.clicked.connect( self.on_nastran_create_coords) #------------------------------------ self.apply_button.clicked.connect(self.on_apply) self.ok_button.clicked.connect(self.on_ok) self.cancel_button.clicked.connect(self.on_cancel) # closeEvent def on_nastran_is_element_quality(self): """set the nastran element quality preferences""" is_checked = self.nastran_is_element_quality_checkbox.isChecked() if self.win_parent is not None: self.win_parent.settings.nastran_is_element_quality = is_checked def on_nastran_is_properties(self): """set the nastran properties preferences""" is_checked = self.nastran_is_properties_checkbox.isChecked() if self.win_parent is not None: self.win_parent.settings.nastran_is_properties = is_checked def on_nastran_is_3d_bars(self): """set the nastran properties preferences""" is_checked = self.nastran_is_3d_bars_checkbox.isChecked() if self.win_parent is not None: self.win_parent.settings.nastran_is_3d_bars = is_checked def on_nastran_is_3d_bars_update(self): """set the nastran properties preferences""" is_checked = self.nastran_is_3d_bars_update_checkbox.isChecked() if self.win_parent is not None: self.win_parent.settings.nastran_is_3d_bars_update = is_checked def on_nastran_is_bar_axes(self): """set the nastran properties preferences""" is_checked = self.nastran_is_bar_axes_checkbox.isChecked() if self.win_parent is not None: self.win_parent.settings.nastran_is_bar_axes = is_checked def on_nastran_create_coords(self): """set the nastran properties preferences""" is_checked = self.nastran_create_coords_checkbox.isChecked() if self.win_parent is not None: self.win_parent.settings.nastran_create_coords = is_checked def on_font(self, value=None): """update the font for the current window""" if value is None: value = self.font_size_edit.value() font = QtGui.QFont() font.setPointSize(value) self.setFont(font) bold_font = make_font(value, is_bold=True) self.nastran_label.setFont(bold_font) def on_annotation_size(self, value=None): """update the annotation size""" if value is None: value = int(self.annotation_size_edit.text()) self._annotation_size = value #self.on_apply(force=True) #self.min_edit.setText(str(self._default_min)) #self.min_edit.setStyleSheet("QLineEdit{background: white;}") self.update_annotation_size_color() def update_annotation_size_color(self): if self.win_parent is not None: self.win_parent.settings.set_annotation_size_color( size=self._annotation_size) def on_gradient_scale(self): is_checked = self.gradient_scale_checkbox.isChecked() self.background_color2_label.setEnabled(is_checked) self.background_color2_edit.setEnabled(is_checked) if self.win_parent is not None: self.win_parent.settings.set_gradient_background( use_gradient_background=is_checked) def on_corner_coord(self): is_checked = self.corner_coord_checkbox.isChecked() if self.win_parent is not None: self.win_parent.set_corner_axis_visiblity(is_checked, render=True) def on_annotation_color(self): rgb_color_ints = self.annotation_color_int title = "Choose an annotation color" passed, rgb_color_ints, rgb_color_floats = self.on_color( self.annotation_color_edit, rgb_color_ints, title) if passed: self.annotation_color_int = rgb_color_ints self.annotation_color_float = rgb_color_floats self.update_annotation_size_color() def on_background_color(self): """ Choose a background color""" title = "Choose a primary background color" rgb_color_ints = self.background_color_int color_edit = self.background_color_edit func_name = 'set_background_color' passed, rgb_color_ints, rgb_color_floats = self._background_color( title, color_edit, rgb_color_ints, func_name) if passed: self.background_color_int = rgb_color_ints self.background_color_float = rgb_color_floats def on_background_color2(self): """ Choose a background color""" title = "Choose a secondary background color" rgb_color_ints = self.background_color2_int color_edit = self.background_color2_edit func_name = 'set_background_color2' passed, rgb_color_ints, rgb_color_floats = self._background_color( title, color_edit, rgb_color_ints, func_name) if passed: self.background_color2_int = rgb_color_ints self.background_color2_float = rgb_color_floats def on_highlight_color(self): """ Choose a highlight color""" title = "Choose a highlight color" rgb_color_ints = self.highlight_color_int color_edit = self.highlight_color_edit func_name = 'set_highlight_color' passed, rgb_color_ints, rgb_color_floats = self._background_color( title, color_edit, rgb_color_ints, func_name) if passed: self.highlight_color_int = rgb_color_ints self.highlight_color_float = rgb_color_floats def on_highlight_opacity(self, value=None): if value is None: value = self.highlight_opacity_edit.value() self._highlight_opacity = value if self.win_parent is not None: self.win_parent.settings.set_highlight_opacity(value) def _background_color(self, title, color_edit, rgb_color_ints, func_name): """helper method for ``on_background_color`` and ``on_background_color2``""" passed, rgb_color_ints, rgb_color_floats = self.on_color( color_edit, rgb_color_ints, title) if passed: if self.win_parent is not None: settings = self.win_parent.settings func_background_color = getattr(settings, func_name) func_background_color(rgb_color_floats) return passed, rgb_color_ints, rgb_color_floats def on_text_color(self): """ Choose a text color """ rgb_color_ints = self.text_color_int title = "Choose a text color" passed, rgb_color_ints, rgb_color_floats = self.on_color( self.text_color_edit, rgb_color_ints, title) if passed: self.text_color_int = rgb_color_ints self.text_color_float = rgb_color_floats if self.win_parent is not None: self.win_parent.settings.set_text_color(rgb_color_floats) def on_default_text_size(self): self.text_size_edit.setValue(self._default_text_size) self.on_text_size(self._default_text_size) def on_text_size(self, value=None): if value is None: value = self.text_size_edit.value() self._text_size = value if self.win_parent is not None: self.win_parent.settings.set_text_size(value) def on_color(self, color_edit, rgb_color_ints, title): """pops a color dialog""" col = QColorDialog.getColor(QtGui.QColor(*rgb_color_ints), self, title) if not col.isValid(): return False, rgb_color_ints, None color_float = col.getRgbF()[:3] # floats color_int = [int(colori * 255) for colori in color_float] assert isinstance(color_float[0], float), color_float assert isinstance(color_int[0], int), color_int color_edit.setStyleSheet("QPushButton {" "background-color: rgb(%s, %s, %s);" % tuple(color_int) + #"border:1px solid rgb(255, 170, 255); " "}") return True, color_int, color_float def on_picker_size(self): self._picker_size = float(self.picker_size_edit.text()) if self.win_parent is not None: self.win_parent.element_picker_size = self._picker_size / 100. #self.on_apply(force=True) def on_magnify(self, value=None): if value is None: value = self.magnify_edit.value() self._magnify = value if self.win_parent is not None: self.win_parent.settings.set_magnify(value) #--------------------------------------------------------------------------- def on_coord_scale(self, value=None): if value is None: value = self.coord_scale_edit.value() self._coord_scale = value if self.win_parent is not None: self.win_parent.settings.set_coord_scale(value / 100.) def on_default_coord_scale(self): self.coord_scale_edit.setValue(self._default_coord_scale) self.on_coord_scale(self._default_coord_scale) def on_coord_text_scale(self, value=None): if value is None: value = self.coord_text_scale_edit.value() self._coord_text_scale = value if self.win_parent is not None: self.win_parent.settings.set_coord_text_scale(value / 100.) def on_default_coord_text_scale(self): self.coord_text_scale_edit.setValue(self._default_coord_text_scale) self.on_coord_text_scale(self._default_coord_text_scale) #--------------------------------------------------------------------------- def on_default_font_size(self): self.font_size_edit.setValue(self._default_font_size) self.on_font(self._default_font_size) def on_default_annotation_size(self): self.annotation_size_edit.setValue(self._default_annotation_size) self.on_annotation_size(self._default_annotation_size) def on_default_clipping_min(self): self.clipping_min_edit.setText(str(self._default_clipping_min)) self.clipping_min_edit.setStyleSheet("QLineEdit{background: white;}") def on_default_clipping_max(self): self.clipping_max_edit.setText(str(self._default_clipping_max)) self.clipping_max_edit.setStyleSheet("QLineEdit{background: white;}") def on_validate(self): font_size_value, flag0 = check_float(self.font_size_edit) annotation_size_value, flag1 = check_float(self.annotation_size_edit) assert isinstance(self.annotation_color_float[0], float), self.annotation_color_float assert isinstance(self.annotation_color_int[0], int), self.annotation_color_int picker_size_value, flag2 = check_float(self.picker_size_edit) clipping_min_value, flag3 = check_label_float(self.clipping_min_edit) clipping_max_value, flag4 = check_label_float(self.clipping_max_edit) if all([flag0, flag1, flag2, flag3, flag4]): self._annotation_size = annotation_size_value self._picker_size = picker_size_value self.out_data['font_size'] = int(font_size_value) self.out_data['min_clip'] = min(clipping_min_value, clipping_max_value) self.out_data['max_clip'] = max(clipping_min_value, clipping_max_value) self.out_data['clicked_ok'] = True return True return False def on_apply(self, force=False): passed = self.on_validate() if (passed or force) and self.win_parent is not None: self.win_parent.settings.on_set_font_size( self.out_data['font_size']) #self.win_parent.settings.set_annotation_size_color(self._annotation_size) #self.win_parent.element_picker_size = self._picker_size / 100. if passed and self.win_parent is not None: self.win_parent.clipping_obj.apply_clipping(self.out_data) return passed def on_ok(self): passed = self.on_apply() if passed: self.close() #self.destroy() def on_cancel(self): self.out_data['close'] = True self.close()
class QtPluginDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) self.refresh_state = RefreshState.DONE self.already_installed = set() installer_type = "mamba" if running_as_constructor_app() else "pip" self.installer = Installer(installer=installer_type) self.setup_ui() self.installer.set_output_widget(self.stdout_text) self.installer.started.connect(self._on_installer_start) self.installer.finished.connect(self._on_installer_done) self.refresh() def _on_installer_start(self): self.cancel_all_btn.setVisible(True) self.working_indicator.show() self.process_error_indicator.hide() self.close_btn.setDisabled(True) def _on_installer_done(self, exit_code): self.working_indicator.hide() if exit_code: self.process_error_indicator.show() self.cancel_all_btn.setVisible(False) self.close_btn.setDisabled(False) self.refresh() def closeEvent(self, event): if self.close_btn.isEnabled(): super().closeEvent(event) event.ignore() def refresh(self): if self.refresh_state != RefreshState.DONE: self.refresh_state = RefreshState.OUTDATED return self.refresh_state = RefreshState.REFRESHING self.installed_list.clear() self.available_list.clear() # fetch installed from npe2 import PluginManager from ...plugins import plugin_manager plugin_manager.discover() # since they might not be loaded yet self.already_installed = set() def _add_to_installed(distname, enabled, npe_version=1): norm_name = normalized_name(distname or '') if distname: try: meta = metadata(distname) except PackageNotFoundError: self.refresh_state = RefreshState.OUTDATED return # a race condition has occurred and the package is uninstalled by another thread if len(meta) == 0: # will not add builtins. return self.already_installed.add(norm_name) else: meta = {} self.installed_list.addItem( PackageMetadata( metadata_version="1.0", name=norm_name, version=meta.get('version', ''), summary=meta.get('summary', ''), home_page=meta.get('url', ''), author=meta.get('author', ''), license=meta.get('license', ''), ), installed=True, enabled=enabled, npe_version=npe_version, ) pm2 = PluginManager.instance() for manifest in pm2.iter_manifests(): distname = normalized_name(manifest.name or '') if distname in self.already_installed or distname == 'napari': continue enabled = not pm2.is_disabled(manifest.name) _add_to_installed(distname, enabled, npe_version=2) for ( plugin_name, _mod_name, distname, ) in plugin_manager.iter_available(): # not showing these in the plugin dialog if plugin_name in ('napari_plugin_engine', ): continue if distname in self.already_installed: continue _add_to_installed(distname, not plugin_manager.is_blocked(plugin_name)) self.installed_label.setText( trans._( "Installed Plugins ({amount})", amount=len(self.already_installed), )) # fetch available plugins settings = get_settings() use_hub = (running_as_bundled_app() or running_as_constructor_app() or settings.plugins.plugin_api.name == "napari_hub") if use_hub: conda_forge = running_as_constructor_app() self.worker = create_worker(iter_hub_plugin_info, conda_forge=conda_forge) else: self.worker = create_worker(iter_napari_plugin_info) self.worker.yielded.connect(self._handle_yield) self.worker.finished.connect(self.working_indicator.hide) self.worker.finished.connect(self._update_count_in_label) self.worker.finished.connect(self._end_refresh) self.worker.start() def setup_ui(self): self.resize(1080, 640) vlay_1 = QVBoxLayout(self) self.h_splitter = QSplitter(self) vlay_1.addWidget(self.h_splitter) self.h_splitter.setOrientation(Qt.Horizontal) self.v_splitter = QSplitter(self.h_splitter) self.v_splitter.setOrientation(Qt.Vertical) self.v_splitter.setMinimumWidth(500) installed = QWidget(self.v_splitter) lay = QVBoxLayout(installed) lay.setContentsMargins(0, 2, 0, 2) self.installed_label = QLabel(trans._("Installed Plugins")) self.packages_filter = QLineEdit() self.packages_filter.setPlaceholderText(trans._("filter...")) self.packages_filter.setMaximumWidth(350) self.packages_filter.setClearButtonEnabled(True) mid_layout = QVBoxLayout() mid_layout.addWidget(self.packages_filter) mid_layout.addWidget(self.installed_label) lay.addLayout(mid_layout) self.installed_list = QPluginList(installed, self.installer) self.packages_filter.textChanged.connect(self.installed_list.filter) lay.addWidget(self.installed_list) uninstalled = QWidget(self.v_splitter) lay = QVBoxLayout(uninstalled) lay.setContentsMargins(0, 2, 0, 2) self.avail_label = QLabel(trans._("Available Plugins")) mid_layout = QHBoxLayout() mid_layout.addWidget(self.avail_label) mid_layout.addStretch() lay.addLayout(mid_layout) self.available_list = QPluginList(uninstalled, self.installer) self.packages_filter.textChanged.connect(self.available_list.filter) lay.addWidget(self.available_list) self.stdout_text = QTextEdit(self.v_splitter) self.stdout_text.setReadOnly(True) self.stdout_text.setObjectName("pip_install_status") self.stdout_text.hide() buttonBox = QHBoxLayout() self.working_indicator = QLabel(trans._("loading ..."), self) sp = self.working_indicator.sizePolicy() sp.setRetainSizeWhenHidden(True) self.working_indicator.setSizePolicy(sp) self.process_error_indicator = QLabel(self) self.process_error_indicator.setObjectName("error_label") self.process_error_indicator.hide() load_gif = str(Path(napari.resources.__file__).parent / "loading.gif") mov = QMovie(load_gif) mov.setScaledSize(QSize(18, 18)) self.working_indicator.setMovie(mov) mov.start() visibility_direct_entry = not running_as_constructor_app() self.direct_entry_edit = QLineEdit(self) self.direct_entry_edit.installEventFilter(self) self.direct_entry_edit.setPlaceholderText( trans._('install by name/url, or drop file...')) self.direct_entry_edit.setVisible(visibility_direct_entry) self.direct_entry_btn = QPushButton(trans._("Install"), self) self.direct_entry_btn.setVisible(visibility_direct_entry) self.direct_entry_btn.clicked.connect(self._install_packages) self.show_status_btn = QPushButton(trans._("Show Status"), self) self.show_status_btn.setFixedWidth(100) self.cancel_all_btn = QPushButton(trans._("cancel all actions"), self) self.cancel_all_btn.setObjectName("remove_button") self.cancel_all_btn.setVisible(False) self.cancel_all_btn.clicked.connect(lambda: self.installer.cancel()) self.close_btn = QPushButton(trans._("Close"), self) self.close_btn.clicked.connect(self.accept) self.close_btn.setObjectName("close_button") buttonBox.addWidget(self.show_status_btn) buttonBox.addWidget(self.working_indicator) buttonBox.addWidget(self.direct_entry_edit) buttonBox.addWidget(self.direct_entry_btn) if not visibility_direct_entry: buttonBox.addStretch() buttonBox.addWidget(self.process_error_indicator) buttonBox.addSpacing(20) buttonBox.addWidget(self.cancel_all_btn) buttonBox.addSpacing(20) buttonBox.addWidget(self.close_btn) buttonBox.setContentsMargins(0, 0, 4, 0) vlay_1.addLayout(buttonBox) self.show_status_btn.setCheckable(True) self.show_status_btn.setChecked(False) self.show_status_btn.toggled.connect(self._toggle_status) self.v_splitter.setStretchFactor(1, 2) self.h_splitter.setStretchFactor(0, 2) self.packages_filter.setFocus() def _update_count_in_label(self): count = self.available_list.count() self.avail_label.setText( trans._("Available Plugins ({count})", count=count)) def _end_refresh(self): refresh_state = self.refresh_state self.refresh_state = RefreshState.DONE if refresh_state == RefreshState.OUTDATED: self.refresh() def eventFilter(self, watched, event): if event.type() == QEvent.DragEnter: # we need to accept this event explicitly to be able # to receive QDropEvents! event.accept() if event.type() == QEvent.Drop: md = event.mimeData() if md.hasUrls(): files = [url.toLocalFile() for url in md.urls()] self.direct_entry_edit.setText(files[0]) return True return super().eventFilter(watched, event) def _toggle_status(self, show): if show: self.show_status_btn.setText(trans._("Hide Status")) self.stdout_text.show() else: self.show_status_btn.setText(trans._("Show Status")) self.stdout_text.hide() def _install_packages(self, packages: Sequence[str] = ()): if not packages: _packages = self.direct_entry_edit.text() if os.path.exists(_packages): packages = [_packages] else: packages = _packages.split() self.direct_entry_edit.clear() if packages: self.installer.install(packages) def _handle_yield(self, data: Tuple[PackageMetadata, bool]): project_info, is_available = data if project_info.name in self.already_installed: self.installed_list.tag_outdated(project_info, is_available) else: self.available_list.addItem(project_info) if not is_available: self.available_list.tag_unavailable(project_info) self.filter() def filter(self, text: str = None) -> None: """Filter by text or set current text as filter.""" if text is None: text = self.packages_filter.text() else: self.packages_filter.setText(text) self.installed_list.filter(text) self.available_list.filter(text)
class CollapseCube(QDialog): def __init__(self, data, data_collection=[], allow_preview=False, parent=None): super(CollapseCube, self).__init__(parent) self.setWindowTitle("Collapse Cube Along Spectral Axis") # Get the data_components (e.g., FLUX, DQ, ERROR etc) # Using list comprehension to keep the order of the component_ids self.data_components = [ str(x).strip() for x in data.component_ids() if not x in data.coordinate_components ] self.setWindowFlags(self.windowFlags() | Qt.Tool) self.title = "Cube Collapse" self.data = data self.data_collection = data_collection self.parent = parent self._general_description = "Collapse the data cube over the spectral range based on the mathematical operation. The nearest index or wavelength will be chosen if a specified number is out of bounds" self._custom_description = "To use the spectral viewer to define a region to collapse over, cancel this, create an ROI and then select this Collapse Cube again." self.currentAxes = None self.currentKernel = None self.createUI() def createUI(self): """ Create the popup box with the calculation input area and buttons. :return: """ boldFont = QtGui.QFont() boldFont.setBold(True) # Create data component label and input box self.widget_desc = QLabel(self._general_description) self.widget_desc.setWordWrap(True) self.widget_desc.setFixedWidth(350) self.widget_desc.setAlignment((Qt.AlignLeft | Qt.AlignTop)) hb_desc = QHBoxLayout() hb_desc.addWidget(self.widget_desc) # Create data component label and input box self.data_label = QLabel("Data:") self.data_label.setFixedWidth(100) self.data_label.setAlignment((Qt.AlignRight | Qt.AlignTop)) self.data_label.setFont(boldFont) self.data_combobox = QComboBox() self.data_combobox.addItems([ str(x).strip() for x in self.data.component_ids() if not x in self.data.coordinate_components ]) self.data_combobox.setMinimumWidth(200) hb_data = QHBoxLayout() hb_data.addWidget(self.data_label) hb_data.addWidget(self.data_combobox) # Create operation label and input box self.operation_label = QLabel("Operation:") self.operation_label.setFixedWidth(100) self.operation_label.setAlignment((Qt.AlignRight | Qt.AlignTop)) self.operation_label.setFont(boldFont) self.operation_combobox = QComboBox() self.operation_combobox.addItems(operations.keys()) self.operation_combobox.setMinimumWidth(200) hb_operation = QHBoxLayout() hb_operation.addWidget(self.operation_label) hb_operation.addWidget(self.operation_combobox) # Create region label and input box self.region_label = QLabel("region:") self.region_label.setFixedWidth(100) self.region_label.setAlignment((Qt.AlignRight | Qt.AlignTop)) self.region_label.setFont(boldFont) self.region_combobox = QComboBox() # Get the Specviz regions and add them in to the Combo box for roi in self.parent.specviz._widget.roi_bounds: self.region_combobox.addItem("Specviz ROI ({:.3}, {:.3})".format( roi[0], roi[1])) self.region_combobox.addItems( ["Custom (Wavelengths)", "Custom (Indices)"]) self.region_combobox.setMinimumWidth(200) self.region_combobox.currentIndexChanged.connect( self._region_selection_change) hb_region = QHBoxLayout() hb_region.addWidget(self.region_label) hb_region.addWidget(self.region_combobox) # Create error label self.error_label = QLabel("") self.error_label.setFixedWidth(100) self.error_label_text = QLabel("") self.error_label_text.setMinimumWidth(200) self.error_label_text.setAlignment((Qt.AlignLeft | Qt.AlignTop)) hbl_error = QHBoxLayout() hbl_error.addWidget(self.error_label) hbl_error.addWidget(self.error_label_text) # Create start label and input box self.start_label = QLabel("Start:") self.start_label.setFixedWidth(100) self.start_label.setAlignment((Qt.AlignRight | Qt.AlignTop)) self.start_label.setFont(boldFont) self.start_text = QLineEdit() self.start_text.setMinimumWidth(200) self.start_text.setAlignment((Qt.AlignLeft | Qt.AlignTop)) hb_start = QHBoxLayout() hb_start.addWidget(self.start_label) hb_start.addWidget(self.start_text) # Create end label and input box self.end_label = QLabel("End:") self.end_label.setFixedWidth(100) self.end_label.setAlignment((Qt.AlignRight | Qt.AlignTop)) self.end_label.setFont(boldFont) self.end_text = QLineEdit() self.end_text.setMinimumWidth(200) self.end_text.setAlignment((Qt.AlignLeft | Qt.AlignTop)) hb_end = QHBoxLayout() hb_end.addWidget(self.end_label) hb_end.addWidget(self.end_text) # Create Calculate and Cancel buttons self.calculateButton = QPushButton("Calculate") self.calculateButton.clicked.connect(self.calculate_callback) self.calculateButton.setDefault(True) self.cancelButton = QPushButton("Cancel") self.cancelButton.clicked.connect(self.cancel_callback) hb_buttons = QHBoxLayout() hb_buttons.addStretch(1) hb_buttons.addWidget(self.cancelButton) hb_buttons.addWidget(self.calculateButton) # # Sigma clipping # vbox_sigma_clipping = QVBoxLayout() self.sigma_description = QLabel( "Sigma clipping is implemented using <a href='http://docs.astropy.org/en/stable/api/astropy.stats.sigma_clip.html'>astropy.stats.sigma_clip</a>. Empty values will use defaults listed on the webpage, <b>but</b> if the first sigma is empty, then no clipping will be done." ) self.sigma_description.setWordWrap(True) hb_sigma = QHBoxLayout() hb_sigma.addWidget(self.sigma_description) vbox_sigma_clipping.addLayout(hb_sigma) # Create sigma self.sigma_label = QLabel("Sigma:") self.sigma_label.setFixedWidth(100) self.sigma_label.setAlignment((Qt.AlignRight | Qt.AlignTop)) self.sigma_label.setFont(boldFont) self.sigma_text = QLineEdit() self.sigma_text.setMinimumWidth(200) self.sigma_text.setAlignment((Qt.AlignLeft | Qt.AlignTop)) hb_sigma = QHBoxLayout() hb_sigma.addWidget(self.sigma_label) hb_sigma.addWidget(self.sigma_text) vbox_sigma_clipping.addLayout(hb_sigma) # Create sigma_lower self.sigma_lower_label = QLabel("Sigma Lower:") self.sigma_lower_label.setFixedWidth(100) self.sigma_lower_label.setAlignment((Qt.AlignRight | Qt.AlignTop)) self.sigma_lower_label.setFont(boldFont) self.sigma_lower_text = QLineEdit() self.sigma_lower_text.setMinimumWidth(200) self.sigma_lower_text.setAlignment((Qt.AlignLeft | Qt.AlignTop)) hb_sigma_lower = QHBoxLayout() hb_sigma_lower.addWidget(self.sigma_lower_label) hb_sigma_lower.addWidget(self.sigma_lower_text) vbox_sigma_clipping.addLayout(hb_sigma_lower) # Create sigma_upper self.sigma_upper_label = QLabel("Sigma Upper:") self.sigma_upper_label.setFixedWidth(100) self.sigma_upper_label.setAlignment((Qt.AlignRight | Qt.AlignTop)) self.sigma_upper_label.setFont(boldFont) self.sigma_upper_text = QLineEdit() self.sigma_upper_text.setMinimumWidth(200) self.sigma_upper_text.setAlignment((Qt.AlignLeft | Qt.AlignTop)) hb_sigma_upper = QHBoxLayout() hb_sigma_upper.addWidget(self.sigma_upper_label) hb_sigma_upper.addWidget(self.sigma_upper_text) vbox_sigma_clipping.addLayout(hb_sigma_upper) # Create sigma_iters self.sigma_iters_label = QLabel("Sigma Iterations:") self.sigma_iters_label.setFixedWidth(100) self.sigma_iters_label.setAlignment((Qt.AlignRight | Qt.AlignTop)) self.sigma_iters_label.setFont(boldFont) self.sigma_iters_text = QLineEdit() self.sigma_iters_text.setMinimumWidth(200) self.sigma_iters_text.setAlignment((Qt.AlignLeft | Qt.AlignTop)) hb_sigma_iters = QHBoxLayout() hb_sigma_iters.addWidget(self.sigma_iters_label) hb_sigma_iters.addWidget(self.sigma_iters_text) vbox_sigma_clipping.addLayout(hb_sigma_iters) # Add calculation and buttons to popup box vbl = QVBoxLayout() vbl.addLayout(hb_desc) vbl.addLayout(hb_data) vbl.addLayout(hb_operation) vbl.addLayout(hb_region) vbl.addLayout(hb_start) vbl.addLayout(hb_end) vbl.addLayout(vbox_sigma_clipping) vbl.addLayout(hbl_error) vbl.addLayout(hb_buttons) self.setLayout(vbl) self.setMaximumWidth(700) self.show() # Fire the callback to set the default values for everything self._region_selection_change(0) def _region_selection_change(self, index): """ Callback for a change on the region selection combo box. :param newvalue: :return: """ newvalue = self.region_combobox.currentText() # First, let's see if this is one of the custom options if 'Custom' in newvalue and 'Wavelength' in newvalue: # Custom Wavelengths self.hide_start_end(False) self.start_label.setText("Start Wavelength:") self.end_label.setText("End Wavelength:") elif 'Custom' in newvalue and 'Indices' in newvalue: # Custom indices self.hide_start_end(False) self.start_label.setText("Start Index:") self.end_label.setText("End Index:") else: # Region defined in specviz self.hide_start_end(True) # We are going to store the start and end wavelengths in the text boxes even though # they are hidden. This way we can use the text boxes later as a hidden storage container. # TODO: Should probably save the ROIs so the start and end values are more accurate. regex = r"-?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?" floating = re.findall(regex, newvalue) self.start_text.setText(floating[0]) self.end_text.setText(floating[1]) # Let's update the text on the widget if 'Custom' in newvalue: self.widget_desc.setText(self._general_description + "\n\n" + self._custom_description) else: self.widget_desc.setText(self._general_description) def hide_start_end(self, dohide): """ Show or hide the start and end indices depending if the region is defined from the specviz plot OR if we are using custom limits. :param dohide: :return: """ if dohide: self.start_label.hide() self.start_text.hide() self.end_label.hide() self.end_text.hide() else: self.start_label.show() self.start_text.show() self.end_label.show() self.end_text.show() def calculate_callback(self): """ Callback for when they hit calculate :return: """ # Grab the values of interest data_name = self.data_combobox.currentText() start_value = self.start_text.text().strip() end_value = self.end_text.text().strip() self.error_label_text.setText(' ') self.error_label_text.setStyleSheet("color: rgba(255, 0, 0, 128)") # Sanity checks first if not start_value and not end_value: self.start_label.setStyleSheet("color: rgba(255, 0, 0, 128)") self.end_label.setStyleSheet("color: rgba(255, 0, 0, 128)") self.error_label_text.setText( 'Must set at least one of start or end value') return wavelengths = np.array(self.parent._wavelengths) # If indicies, get them and check to see if the inputs are good. if 'Indices' in self.region_combobox.currentText(): if len(start_value) == 0: start_index = 0 else: try: start_index = int(start_value) except ValueError as e: self.start_label.setStyleSheet( "color: rgba(255, 0, 0, 128)") self.error_label_text.setText( 'Start value must be an integer') return if start_index < 0: start_index = 0 if len(end_value) == 0: end_index = len(wavelengths) - 1 else: try: end_index = int(end_value) except ValueError as e: self.end_label.setStyleSheet("color: rgba(255, 0, 0, 128)") self.error_label_text.setText( 'End value must be an integer') return if end_index > len(wavelengths) - 1: end_index = len(wavelengths) - 1 else: # Wavelength inputs if len(start_value) == 0: start_index = 0 else: # convert wavelength to float value try: start_value = float(start_value) except ValueError as e: self.start_label.setStyleSheet( "color: rgba(255, 0, 0, 128)") self.error_label_text.setText( 'Start value must be a floating point number') return # Look up index start_index = np.argsort(np.abs(wavelengths - start_value))[0] if len(end_value) == 0: end_index = len(wavelengths) - 1 else: # convert wavelength to float value try: end_value = float(end_value) except ValueError as e: self.end_label.setStyleSheet("color: rgba(255, 0, 0, 128)") self.error_label_text.setText( 'End value must be a floating point number') return # Look up index end_index = np.argsort(np.abs(wavelengths - end_value))[0] # Check to make sure at least one of start or end is within the range of the wavelengths. if (start_index < 0 and end_index < 0) or (start_index > len(wavelengths) and end_index > len(wavelengths)): self.start_label.setStyleSheet("color: rgba(255, 0, 0, 128)") self.end_label.setStyleSheet("color: rgba(255, 0, 0, 128)") self.error_label_text.setText( 'Can not have both start and end outside of the wavelength range.' ) return if start_index > end_index: self.start_label.setStyleSheet("color: rgba(255, 0, 0, 128)") self.end_label.setStyleSheet("color: rgba(255, 0, 0, 128)") self.error_label_text.setText( 'Start value must be less than end value') return # Check to see if the wavelength (indices) are the same. if start_index == end_index: self.start_label.setStyleSheet("color: rgba(255, 0, 0, 128)") self.end_label.setStyleSheet("color: rgba(255, 0, 0, 128)") self.error_label_text.setText( 'Can not have both start and end wavelengths be the same.') return # Set the start and end values in the text boxes -- in case they enter one way out of range then # we'll fix it. ts = start_index if 'Indices' in self.region_combobox.currentText( ) else wavelengths[start_index] self.start_text.setText('{}'.format(ts)) te = end_index if 'Indices' in self.region_combobox.currentText( ) else wavelengths[end_index] self.end_text.setText('{}'.format(te)) data_name = self.data_combobox.currentText() operation = self.operation_combobox.currentText() # Do calculation if we got this far wavelengths, new_component = collapse_cube(self.data[data_name], data_name, self.data.coords.wcs, operation, start_index, end_index) # Get the start and end wavelengths from the newly created spectral cube and use for labeling the cube. # Convert to the current units. start_wavelength = wavelengths[0].to( self.parent._units_controller._new_units) end_wavelength = wavelengths[-1].to( self.parent._units_controller._new_units) label = '{}-collapse-{} ({:0.3}, {:0.3})'.format( data_name, operation, start_wavelength, end_wavelength) # Apply sigma clipping sigma = self.sigma_text.text().strip() if len(sigma) > 0: try: sigma = float(sigma) except ValueError as e: self.sigma_label.setStyleSheet("color: rgba(255, 0, 0, 128)") self.error_label_text.setText( 'If sigma set, it must be a floating point number') return sigma_lower = self.sigma_lower_text.text().strip() if len(sigma_lower) > 0: try: sigma_lower = float(sigma_lower) except ValueError as e: self.sigma_lower_label.setStyleSheet( "color: rgba(255, 0, 0, 128)") self.error_label_text.setText( 'If sigma lower set, it must be a floating point number' ) return else: sigma_lower = None sigma_upper = self.sigma_upper_text.text().strip() if len(sigma_upper) > 0: try: sigma_upper = float(sigma_upper) except ValueError as e: self.sigma_upper_label.setStyleSheet( "color: rgba(255, 0, 0, 128)") self.error_label_text.setText( 'If sigma upper set, it must be a floating point number' ) return else: sigma_upper = None sigma_iters = self.sigma_iters_text.text().strip() if len(sigma_iters) > 0: try: sigma_iters = float(sigma_iters) except ValueError as e: self.sigma_iters_label.setStyleSheet( "color: rgba(255, 0, 0, 128)") self.error_label_text.setText( 'If sigma iters set, it must be a floating point number' ) return else: sigma_iters = None new_component = sigma_clip(new_component, sigma=sigma, sigma_lower=sigma_lower, sigma_upper=sigma_upper, iters=sigma_iters) # Add to label so it is clear which overlay/component is which if sigma: label += ' sigma={}'.format(sigma) if sigma_lower: label += ' sigma_lower={}'.format(sigma_lower) if sigma_upper: label += ' sigma_upper={}'.format(sigma_upper) if sigma_iters: label += ' sigma_iters={}'.format(sigma_iters) # Add new overlay/component to cubeviz self.parent.add_overlay(new_component, label) self.close() # Show new dialog self.final_dialog(label) def final_dialog(self, label): """ Final dialog that to show where the calculated collapsed cube was put. :param label: :return: """ final_dialog = QDialog() # Create data component label and input box widget_desc = QLabel( 'The collapsed cube was added as an overlay with label "{}"'. format(label)) widget_desc.setWordWrap(True) widget_desc.setFixedWidth(350) widget_desc.setAlignment((Qt.AlignLeft | Qt.AlignTop)) hb_desc = QHBoxLayout() hb_desc.addWidget(widget_desc) # Create Ok button okButton = QPushButton("Ok") okButton.clicked.connect(lambda: final_dialog.close()) okButton.setDefault(True) hb_buttons = QHBoxLayout() hb_buttons.addStretch(1) hb_buttons.addWidget(okButton) # Add description and buttons to popup box vbl = QVBoxLayout() vbl.addLayout(hb_desc) vbl.addLayout(hb_buttons) final_dialog.setLayout(vbl) final_dialog.setMaximumWidth(400) final_dialog.show() def cancel_callback(self, caller=0): """ Cancel callback when the person hits the cancel button :param caller: :return: """ self.close() def keyPressEvent(self, e): if e.key() == Qt.Key_Escape: self.cancel_callback()
class PyDMScaleIndicator(QFrame, TextFormatter, PyDMWidget): """ A bar-shaped indicator for scalar value with support for Channels and more from PyDM. Configurable features include indicator type (bar/pointer), scale tick marks and orientation (horizontal/vertical). Parameters ---------- parent : QWidget The parent widget for the Scale init_channel : str, optional The channel to be used by the widget. """ def __init__(self, parent=None, init_channel=None): QFrame.__init__(self, parent) PyDMWidget.__init__(self, init_channel=init_channel) self._show_value = True self._show_limits = True self.scale_indicator = QScale() self.value_label = QLabel() self.lower_label = QLabel() self.upper_label = QLabel() self.value_label.setText('<val>') self.lower_label.setText('<min>') self.upper_label.setText('<max>') self._value_position = Qt.TopEdge self._limits_from_channel = True self._user_lower_limit = 0 self._user_upper_limit = 0 self.value_label.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self.setup_widgets_for_orientation(Qt.Horizontal, False, False, self._value_position) def update_labels(self): """ Update the limits and value labels with the correct values. """ self.lower_label.setText(str(self.scale_indicator._lower_limit)) self.upper_label.setText(str(self.scale_indicator._upper_limit)) self.value_label.setText(self.format_string.format(self.scale_indicator._value)) def value_changed(self, new_value): """ Callback invoked when the Channel value is changed. Parameters ---------- new_val : int or float The new value from the channel. """ super(PyDMScaleIndicator, self).value_changed(new_value) self.scale_indicator.set_value(new_value) self.update_labels() def upperCtrlLimitChanged(self, new_limit): """ PyQT Slot for changes on the upper control limit value of the Channel This slot sends the new limit value to the ```ctrl_limit_changed``` callback. Parameters ---------- new_limit : float """ super(PyDMScaleIndicator, self).upperCtrlLimitChanged(new_limit) if self.limitsFromChannel: self.scale_indicator.set_upper_limit(new_limit) self.update_labels() def lowerCtrlLimitChanged(self, new_limit): """ PyQT Slot for changes on the lower control limit value of the Channel This slot sends the new limit value to the ```ctrl_limit_changed``` callback. Parameters ---------- new_limit : float """ super(PyDMScaleIndicator, self).lowerCtrlLimitChanged(new_limit) if self.limitsFromChannel: self.scale_indicator.set_lower_limit(new_limit) self.update_labels() def setup_widgets_for_orientation(self, new_orientation, flipped, inverted, value_position): """ Reconstruct the widget given the orientation. Parameters ---------- new_orientation : int Qt.Horizontal or Qt.Vertical flipped : bool Indicates if scale tick marks are flipped to the other side inverted : bool Indicates if scale appearance is inverted """ self.limits_layout = None self.widget_layout = None if new_orientation == Qt.Horizontal: self.limits_layout = QHBoxLayout() if not inverted: self.limits_layout.addWidget(self.lower_label) self.limits_layout.addWidget(self.upper_label) else: self.limits_layout.addWidget(self.upper_label) self.limits_layout.addWidget(self.lower_label) self.widget_layout = QGridLayout() if not flipped: if value_position == Qt.LeftEdge: self.widget_layout.addWidget(self.value_label, 0, 0) self.widget_layout.addWidget(self.scale_indicator, 0, 1) self.widget_layout.addItem(self.limits_layout, 1, 1) elif value_position == Qt.RightEdge: self.widget_layout.addWidget(self.value_label, 0, 1) self.widget_layout.addWidget(self.scale_indicator, 0, 0) self.widget_layout.addItem(self.limits_layout, 1, 0) elif value_position == Qt.TopEdge: self.widget_layout.addWidget(self.value_label, 0, 0) self.widget_layout.addWidget(self.scale_indicator, 1, 0) self.widget_layout.addItem(self.limits_layout, 2, 0) elif value_position == Qt.BottomEdge: self.widget_layout.addWidget(self.scale_indicator, 0, 0) self.widget_layout.addItem(self.limits_layout, 1, 0) self.widget_layout.addWidget(self.value_label, 2, 0) if not inverted: self.lower_label.setAlignment(Qt.AlignTop | Qt.AlignLeft) self.upper_label.setAlignment(Qt.AlignTop | Qt.AlignRight) elif inverted: self.lower_label.setAlignment(Qt.AlignTop | Qt.AlignRight) self.upper_label.setAlignment(Qt.AlignTop | Qt.AlignLeft) else: if value_position == Qt.LeftEdge: self.widget_layout.addItem(self.limits_layout, 0, 1) self.widget_layout.addWidget(self.scale_indicator, 1, 1) self.widget_layout.addWidget(self.value_label, 1, 0) elif value_position == Qt.RightEdge: self.widget_layout.addItem(self.limits_layout, 0, 0) self.widget_layout.addWidget(self.scale_indicator, 1, 0) self.widget_layout.addWidget(self.value_label, 1, 1) elif value_position == Qt.TopEdge: self.widget_layout.addWidget(self.value_label, 0, 0) self.widget_layout.addItem(self.limits_layout, 1, 0) self.widget_layout.addWidget(self.scale_indicator, 2, 0) elif value_position == Qt.BottomEdge: self.widget_layout.addItem(self.limits_layout, 0, 0) self.widget_layout.addWidget(self.scale_indicator, 1, 0) self.widget_layout.addWidget(self.value_label, 2, 0) if not inverted: self.lower_label.setAlignment(Qt.AlignBottom | Qt.AlignLeft) self.upper_label.setAlignment(Qt.AlignBottom | Qt.AlignRight) elif inverted: self.lower_label.setAlignment(Qt.AlignBottom | Qt.AlignRight) self.upper_label.setAlignment(Qt.AlignBottom | Qt.AlignLeft) elif new_orientation == Qt.Vertical: self.limits_layout = QVBoxLayout() if (value_position == Qt.RightEdge and flipped == False) or \ (value_position == Qt.LeftEdge and flipped == True): add_value_between_limits = True else: add_value_between_limits = False if not inverted: self.limits_layout.addWidget(self.upper_label) if add_value_between_limits: self.limits_layout.addWidget(self.value_label) self.limits_layout.addWidget(self.lower_label) self.lower_label.setAlignment(Qt.AlignHCenter | Qt.AlignBottom) self.upper_label.setAlignment(Qt.AlignHCenter | Qt.AlignTop) else: self.limits_layout.addWidget(self.lower_label) if add_value_between_limits: self.limits_layout.addWidget(self.value_label) self.limits_layout.addWidget(self.upper_label) self.lower_label.setAlignment(Qt.AlignHCenter | Qt.AlignTop) self.upper_label.setAlignment(Qt.AlignHCenter | Qt.AlignBottom) self.widget_layout = QGridLayout() if not flipped: if value_position == Qt.LeftEdge: self.widget_layout.addWidget(self.value_label, 0, 0) self.widget_layout.addWidget(self.scale_indicator, 0, 1) self.widget_layout.addItem(self.limits_layout, 0, 2) elif value_position == Qt.RightEdge: self.widget_layout.addWidget(self.scale_indicator, 0, 0) self.widget_layout.addItem(self.limits_layout, 0, 1) elif value_position == Qt.TopEdge: self.widget_layout.addWidget(self.value_label, 0, 0, 1, 2) self.widget_layout.addWidget(self.scale_indicator, 1, 0) self.widget_layout.addItem(self.limits_layout, 1, 1) elif value_position == Qt.BottomEdge: self.widget_layout.addWidget(self.scale_indicator, 0, 0) self.widget_layout.addItem(self.limits_layout, 0, 1) self.widget_layout.addWidget(self.value_label, 1, 0, 1, 2) if not inverted: self.lower_label.setAlignment(Qt.AlignLeft | Qt.AlignBottom) self.upper_label.setAlignment(Qt.AlignLeft | Qt.AlignTop) elif inverted: self.lower_label.setAlignment(Qt.AlignLeft | Qt.AlignTop) self.upper_label.setAlignment(Qt.AlignLeft | Qt.AlignBottom) else: if value_position == Qt.LeftEdge: self.widget_layout.addItem(self.limits_layout, 0, 1) self.widget_layout.addWidget(self.scale_indicator, 0, 2) elif value_position == Qt.RightEdge: self.widget_layout.addItem(self.limits_layout, 0, 0) self.widget_layout.addWidget(self.scale_indicator, 0, 1) self.widget_layout.addWidget(self.value_label, 0, 2) elif value_position == Qt.TopEdge: self.widget_layout.addWidget(self.value_label, 0, 0, 1, 2) self.widget_layout.addItem(self.limits_layout, 1, 0) self.widget_layout.addWidget(self.scale_indicator, 1, 1) elif value_position == Qt.BottomEdge: self.widget_layout.addItem(self.limits_layout, 0, 0) self.widget_layout.addWidget(self.scale_indicator, 0, 1) self.widget_layout.addWidget(self.value_label, 1, 0, 1, 2) if not inverted: self.lower_label.setAlignment(Qt.AlignRight | Qt.AlignBottom) self.upper_label.setAlignment(Qt.AlignRight | Qt.AlignTop) elif inverted: self.lower_label.setAlignment(Qt.AlignRight | Qt.AlignTop) self.upper_label.setAlignment(Qt.AlignRight | Qt.AlignBottom) self.value_label.setAlignment(Qt.AlignCenter) if self.layout() is not None: # Trick to remove the existing layout by re-parenting it in an empty widget. QWidget().setLayout(self.layout()) self.widget_layout.setContentsMargins(1, 1, 1, 1) self.setLayout(self.widget_layout) @Property(bool) def showValue(self): """ Whether or not the current value should be displayed on the scale. Returns ------- bool """ return self._show_value @showValue.setter def showValue(self, checked): """ Whether or not the current value should be displayed on the scale. Parameters ---------- checked : bool """ if self._show_value != bool(checked): self._show_value = checked if checked: self.value_label.show() else: self.value_label.hide() @Property(bool) def showLimits(self): """ Whether or not the high and low limits should be displayed on the scale. Returns ------- bool """ return self._show_limits @showLimits.setter def showLimits(self, checked): """ Whether or not the high and low limits should be displayed on the scale. Parameters ---------- checked : bool """ if self._show_limits != bool(checked): self._show_limits = checked if checked: self.lower_label.show() self.upper_label.show() else: self.lower_label.hide() self.upper_label.hide() @Property(bool) def showTicks(self): """ Whether or not the tick marks should be displayed on the scale. Returns ------- bool """ return self.scale_indicator.get_show_ticks() @showTicks.setter def showTicks(self, checked): """ Whether or not the tick marks should be displayed on the scale. Parameters ---------- checked : bool """ self.scale_indicator.set_show_ticks(checked) @Property(Qt.Orientation) def orientation(self): """ The scale orientation (Horizontal or Vertical) Returns ------- int Qt.Horizontal or Qt.Vertical """ return self.scale_indicator.get_orientation() @orientation.setter def orientation(self, orientation): """ The scale orientation (Horizontal or Vertical) Parameters ---------- new_orientation : int Qt.Horizontal or Qt.Vertical """ self.scale_indicator.set_orientation(orientation) self.setup_widgets_for_orientation(orientation, self.flipScale, self.invertedAppearance, self._value_position) @Property(bool) def flipScale(self): """ Whether or not the scale should be flipped. Returns ------- bool """ return self.scale_indicator.get_flip_scale() @flipScale.setter def flipScale(self, checked): """ Whether or not the scale should be flipped. Parameters ---------- checked : bool """ self.scale_indicator.set_flip_scale(checked) self.setup_widgets_for_orientation(self.orientation, checked, self.invertedAppearance, self._value_position) @Property(bool) def invertedAppearance(self): """ Whether or not the scale appearence should be inverted. Returns ------- bool """ return self.scale_indicator.get_inverted_appearance() @invertedAppearance.setter def invertedAppearance(self, inverted): """ Whether or not the scale appearence should be inverted. Parameters ---------- inverted : bool """ self.scale_indicator.set_inverted_appearance(inverted) self.setup_widgets_for_orientation(self.orientation, self.flipScale, inverted, self._value_position) @Property(bool) def barIndicator(self): """ Whether or not the scale indicator should be a bar instead of a pointer. Returns ------- bool """ return self.scale_indicator.get_bar_indicator() @barIndicator.setter def barIndicator(self, checked): """ Whether or not the scale indicator should be a bar instead of a pointer. Parameters ---------- checked : bool """ self.scale_indicator.set_bar_indicator(checked) @Property(QColor) def backgroundColor(self): """ The color of the scale background. Returns ------- QColor """ return self.scale_indicator.get_background_color() @backgroundColor.setter def backgroundColor(self, color): """ The color of the scale background. Parameters ------- color : QColor """ self.scale_indicator.set_background_color(color) @Property(QColor) def indicatorColor(self): """ The color of the scale indicator. Returns ------- QColor """ return self.scale_indicator.get_indicator_color() @indicatorColor.setter def indicatorColor(self, color): """ The color of the scale indicator. Parameters ------- color : QColor """ self.scale_indicator.set_indicator_color(color) @Property(QColor) def tickColor(self): """ The color of the scale tick marks. Returns ------- QColor """ return self.scale_indicator.get_tick_color() @tickColor.setter def tickColor(self, color): """ The color of the scale tick marks. Parameters ------- color : QColor """ self.scale_indicator.set_tick_color(color) @Property(float) def backgroundSizeRate(self): """ The rate of background height size (from top to bottom). Returns ------- float """ return self.scale_indicator.get_background_size_rate() @backgroundSizeRate.setter def backgroundSizeRate(self, rate): """ The rate of background height size (from top to bottom). Parameters ------- rate : float Between 0 and 1. """ self.scale_indicator.set_background_size_rate(rate) @Property(float) def tickSizeRate(self): """ The rate of tick marks height size (from bottom to top). Returns ------- float """ return self.scale_indicator.get_tick_size_rate() @tickSizeRate.setter def tickSizeRate(self, rate): """ The rate of tick marks height size (from bottom to top). Parameters ------- rate : float Between 0 and 1. """ self.scale_indicator.set_tick_size_rate(rate) @Property(int) def numDivisions(self): """ The number in which the scale is divided. Returns ------- int """ return self.scale_indicator.get_num_divisions() @numDivisions.setter def numDivisions(self, divisions): """ The number in which the scale is divided. Parameters ------- divisions : int The number of scale divisions. """ self.scale_indicator.set_num_divisions(divisions) @Property(int) def scaleHeight(self): """ The scale height, fixed so it do not wiggle when value label resizes. Returns ------- int """ return self.scale_indicator.get_scale_height() @scaleHeight.setter def scaleHeight(self, value): """ The scale height, fixed so it do not wiggle when value label resizes. Parameters ------- divisions : int The scale height. """ self.scale_indicator.set_scale_height(value) @Property(Qt.Edge) def valuePosition(self): """ The position of the value label (Top, Bottom, Left or Right). Returns ------- int Qt.TopEdge, Qt.BottomEdge, Qt.LeftEdge or Qt.RightEdge """ return self._value_position @valuePosition.setter def valuePosition(self, position): """ The position of the value label (Top, Bottom, Left or Right). Parameters ---------- position : int Qt.TopEdge, Qt.BottomEdge, Qt.LeftEdge or Qt.RightEdge """ self._value_position = position self.setup_widgets_for_orientation(self.orientation, self.flipScale, self.invertedAppearance, position) @Property(bool) def originAtZero(self): """ Whether or not the scale indicator should start at zero value. Applies only for bar indicator. Returns ------- bool """ return self.scale_indicator.get_origin_at_zero() @originAtZero.setter def originAtZero(self, checked): """ Whether or not the scale indicator should start at zero value. Applies only for bar indicator. Parameters ---------- checked : bool """ self.scale_indicator.set_origin_at_zero(checked) @Property(bool) def limitsFromChannel(self): """ Whether or not the scale indicator should use the limits information from the channel. Returns ------- bool """ return self._limits_from_channel @limitsFromChannel.setter def limitsFromChannel(self, checked): """ Whether or not the scale indicator should use the limits information from the channel. Parameters ---------- checked : bool True to use the limits from the Channel, False to use the user-defined values. """ if self._limits_from_channel != checked: self._limits_from_channel = checked if checked: if self._lower_ctrl_limit: self.scale_indicator.set_lower_limit(self._lower_ctrl_limit) if self._upper_ctrl_limit: self.scale_indicator.set_upper_limit(self._upper_ctrl_limit) else: self.scale_indicator.set_lower_limit(self._user_lower_limit) self.scale_indicator.set_upper_limit(self._user_upper_limit) self.update_labels() @Property(float) def userLowerLimit(self): """ The user-defined lower limit for the scale. Returns ------- float """ return self._user_lower_limit @userLowerLimit.setter def userLowerLimit(self, value): """ The user-defined lower limit for the scale. Parameters ---------- value : float The new lower limit value. """ if self._limits_from_channel: return self._user_lower_limit = value self.scale_indicator.set_lower_limit(self._user_lower_limit) self.update_labels() @Property(float) def userUpperLimit(self): """ The user-defined upper limit for the scale. Returns ------- float """ return self._user_upper_limit @userUpperLimit.setter def userUpperLimit(self, value): """ The user-defined upper limit for the scale. Parameters ---------- value : float The new upper limit value. """ if self._limits_from_channel: return self._user_upper_limit = value self.scale_indicator.set_upper_limit(self._user_upper_limit) self.update_labels()
class DimensionMDE(Dimension): binningChanged = Signal() """ MDEventWorkspace has additional properties for either number_of_bins or thickness from mantidqt.widgets.sliceviewer.dimensionwidget import DimensionMDE from qtpy.QtWidgets import QApplication app = QApplication([]) window = DimensionMDE({'minimum':-1.1, 'number_of_bins':11, 'width':0.2, 'name':'Dim0', 'units':'A'}) window.show() app.exec_() """ def __init__(self, dim_info, number=0, state=State.NONE, parent=None): # hack in a number_of_bins for MDEventWorkspace dim_info['number_of_bins'] = 1000 dim_info['width'] = (dim_info['maximum'] - dim_info['minimum']) / 1000 self.spinBins = QSpinBox() self.spinBins.setRange(2, 9999) self.spinBins.setValue(100) self.spinBins.hide() self.spinBins.setMinimumWidth(110) self.spinThick = QDoubleSpinBox() self.spinThick.setRange(0.001, 999) self.spinThick.setValue(0.1) self.spinThick.setSingleStep(0.1) self.spinThick.setDecimals(3) self.spinThick.setMinimumWidth(110) self.rebinLabel = QLabel("thick") self.rebinLabel.setMinimumWidth(44) super().__init__(dim_info, number, state, parent) self.spinBins.valueChanged.connect(self.binningChanged) self.spinThick.valueChanged.connect(self.valueChanged) self.layout.addWidget(self.spinBins) self.layout.addWidget(self.spinThick) self.layout.addWidget(self.rebinLabel) def get_bins(self): return int(self.spinBins.value()) def get_thickness(self): return float(self.spinThick.value()) def set_state(self, state): super().set_state(state) if self.state == State.X: self.spinBins.show() self.spinThick.hide() self.rebinLabel.setText('bins') elif self.state == State.Y: self.spinBins.show() self.spinThick.hide() self.rebinLabel.setText('bins') elif self.state == State.NONE: self.spinBins.hide() self.spinThick.show() self.rebinLabel.setText('thick') else: self.spinBins.hide() self.spinThick.hide() self.rebinLabel.hide()
class PyDMEmbeddedDisplay(QFrame, PyDMPrimitiveWidget): """ A QFrame capable of rendering a PyDM Display Parameters ---------- parent : QWidget The parent widget for the Label """ def __init__(self, parent=None): QFrame.__init__(self, parent) PyDMPrimitiveWidget.__init__(self) self.app = QApplication.instance() self._filename = None self._macros = None self._embedded_widget = None self._disconnect_when_hidden = True self._is_connected = False self._only_load_when_shown = True self._needs_load = True self._load_error_timer = None self._load_error = None self.layout = QVBoxLayout(self) self.err_label = QLabel(self) self.err_label.setAlignment(Qt.AlignHCenter) self.layout.addWidget(self.err_label) self.layout.setContentsMargins(0, 0, 0, 0) self.err_label.hide() def init_for_designer(self): self.setFrameShape(QFrame.Box) def minimumSizeHint(self): """ This property holds the recommended minimum size for the widget. Returns ------- QSize """ # This is totally arbitrary, I just want *some* visible nonzero size return QSize(100, 100) @Property(str) def macros(self): """ JSON-formatted string containing macro variables to pass to the embedded file. Returns ------- str """ if self._macros is None: return "" return self._macros @macros.setter def macros(self, new_macros): """ JSON-formatted string containing macro variables to pass to the embedded file. .. warning:: If the macros property is not defined before the filename property, The widget will not have any macros defined when it loads the embedded file. This behavior will be fixed soon. Parameters ---------- new_macros : str """ new_macros = str(new_macros) if new_macros != self._macros: self._macros = new_macros self._needs_load = True self.load_if_needed() @Property(str) def filename(self): """ Filename of the display to embed. Returns ------- str """ if self._filename is None: return "" return self._filename @filename.setter def filename(self, filename): """ Filename of the display to embed. Parameters ---------- filename : str """ filename = str(filename) if filename != self._filename: self._filename = filename self._needs_load = True if is_qt_designer(): if self._load_error_timer: # Kill the timer here. If new filename still causes the problem, it will be restarted self._load_error_timer.stop() self._load_error_timer = None self.clear_error_text() self.load_if_needed() def parsed_macros(self): """ Dictionary containing the key value pair for each macro specified. Returns -------- dict """ parent_display = self.find_parent_display() parent_macros = {} if parent_display: parent_macros = copy.copy(parent_display.macros()) widget_macros = macro.parse_macro_string(self.macros) parent_macros.update(widget_macros) return parent_macros def load_if_needed(self): if self._needs_load and (not self._only_load_when_shown or self.isVisible() or is_qt_designer()): self.embedded_widget = self.open_file() def open_file(self, force=False): """ Opens the widget specified in the widget's filename property. Returns ------- display : QWidget """ if (not force) and (not self._needs_load): return if not self.filename: return try: parent_display = self.find_parent_display() base_path = "" if parent_display: base_path = os.path.dirname(parent_display.loaded_file()) fname = find_file(self.filename, base_path=base_path) w = load_file(fname, macros=self.parsed_macros(), target=None) self._needs_load = False self.clear_error_text() return w except Exception as e: self._load_error = e if self._load_error_timer: self._load_error_timer.stop() self._load_error_timer = QTimer(self) self._load_error_timer.setSingleShot(True) self._load_error_timer.setTimerType(Qt.VeryCoarseTimer) self._load_error_timer.timeout.connect( self._display_designer_load_error) self._load_error_timer.start(1000) return None def clear_error_text(self): if self._load_error_timer: self._load_error_timer.stop() self.err_label.clear() self.err_label.hide() def display_error_text(self, e): self.err_label.setText( "Could not open {filename}.\nError: {err}".format( filename=self._filename, err=e)) self.err_label.show() @property def embedded_widget(self): """ The embedded widget being displayed. Returns ------- QWidget """ return self._embedded_widget @embedded_widget.setter def embedded_widget(self, new_widget): """ Defines the embedded widget to display inside the QFrame Parameters ---------- new_widget : QWidget """ should_reconnect = False if new_widget is self._embedded_widget: return if self._embedded_widget is not None: self.layout.removeWidget(self._embedded_widget) self._embedded_widget.deleteLater() self._embedded_widget = None if new_widget is not None: self._embedded_widget = new_widget self._embedded_widget.setParent(self) self.layout.addWidget(self._embedded_widget) self.err_label.hide() self._embedded_widget.show() self._is_connected = True def connect(self): """ Establish the connection between the embedded widget and the channels associated with it. """ if self._is_connected or self.embedded_widget is None: return establish_widget_connections(self.embedded_widget) self._is_connected = True def disconnect(self): """ Disconnects the embedded widget from the channels associated with it. """ if not self._is_connected or self.embedded_widget is None: return close_widget_connections(self.embedded_widget) self._is_connected = False @Property(bool) def loadWhenShown(self): """ If True, only load and display the file once the PyDMEmbeddedDisplayWidget is visible on screen. This is very useful if you have many different PyDMEmbeddedWidgets in different tabs of a QTabBar or PyDMTabBar: only the tab that the user is looking at will be loaded, which can greatly speed up the launch time of a display. If this property is changed from 'True' to 'False', and the file has not been loaded yet, it will be loaded immediately. Returns ------- bool """ return self._only_load_when_shown @loadWhenShown.setter def loadWhenShown(self, val): self._only_load_when_shown = val self.load_if_needed() @Property(bool) def disconnectWhenHidden(self): """ Disconnect from PVs when this widget is not visible. Returns ------- bool """ return self._disconnect_when_hidden @disconnectWhenHidden.setter def disconnectWhenHidden(self, disconnect_when_hidden): """ Disconnect from PVs when this widget is not visible. Parameters ---------- disconnect_when_hidden : bool """ self._disconnect_when_hidden = disconnect_when_hidden def showEvent(self, e): """ Show events are sent to widgets that become visible on the screen. Parameters ---------- event : QShowEvent """ if self._only_load_when_shown: w = self.open_file() if w: self.embedded_widget = w if self.disconnectWhenHidden: self.connect() def hideEvent(self, e): """ Hide events are sent to widgets that become invisible on the screen. Parameters ---------- event : QHideEvent """ if self.disconnectWhenHidden: self.disconnect() def _display_designer_load_error(self): self._load_error_timer = None logger.exception("Exception while opening embedded display file.", exc_info=self._load_error) if self._load_error: self.display_error_text(self._load_error)
class Dimension(QWidget): stateChanged = Signal(int) valueChanged = Signal() """ pass in dimension state: one of (State.X, State.Y, State.NONE, State.DISABLE) Can be run independently by: from mantidqt.widgets.sliceviewer.dimensionwidget import Dimension from qtpy.QtWidgets import QApplication app = QApplication([]) window = Dimension({'minimum':-1.1, 'number_of_bins':11, 'width':0.2, 'name':'Dim0', 'units':'A'}) window.show() app.exec_() """ def __init__(self, dim_info, number=0, state=State.NONE, parent=None): super().__init__(parent) self.minimum = dim_info['minimum'] self.nbins = dim_info['number_of_bins'] self.width = dim_info['width'] self.number = number self.layout = QHBoxLayout(self) self.layout.setContentsMargins(0, 2, 0, 0) self.name = QLabel(dim_info['name']) self.units = QLabel(dim_info['units']) self.x = QPushButton('X') self.x.setFixedSize(26, 26) self.x.setCheckable(True) self.x.clicked.connect(self.x_clicked) self.y = QPushButton('Y') self.y.setFixedSize(26, 26) self.y.setCheckable(True) self.y.clicked.connect(self.y_clicked) self.slider = QSlider(Qt.Horizontal) self.slider.setRange(0, self.nbins - 1) self.slider.valueChanged.connect(self.slider_changed) self.spinbox = QDoubleSpinBox() self.spinbox.setDecimals(3) self.spinbox.setRange(self.get_bin_center(0), self.get_bin_center(self.nbins - 1)) self.spinbox.setSingleStep(self.width) self.spinbox.editingFinished.connect(self.spinbox_changed) self.layout.addWidget(self.name) self.button_layout = QHBoxLayout() self.button_layout.setContentsMargins(0, 0, 0, 0) self.button_layout.setSpacing(0) self.button_layout.addWidget(self.x) self.button_layout.addWidget(self.y) self.layout.addLayout(self.button_layout) self.layout.addWidget(self.slider, stretch=1) self.layout.addStretch(0) self.layout.addWidget(self.spinbox) self.layout.addWidget(self.units) self.set_value(0) if self.nbins < 2: state = State.DISABLE self.set_state(state) def set_state(self, state): self.state = state if self.state == State.X: self.x.setChecked(True) self.y.setChecked(False) self.slider.hide() self.spinbox.hide() self.units.hide() elif self.state == State.Y: self.x.setChecked(False) self.y.setChecked(True) self.slider.hide() self.spinbox.hide() self.units.hide() elif self.state == State.NONE: self.x.setChecked(False) self.y.setChecked(False) self.slider.show() self.spinbox.show() self.units.show() else: self.x.setChecked(False) self.x.setDisabled(True) self.y.setChecked(False) self.y.setDisabled(True) self.slider.hide() self.spinbox.show() self.spinbox.setDisabled(True) self.units.show() def get_state(self): return self.state def x_clicked(self): old_state = self.state self.set_state(State.X) if self.state != old_state: self.stateChanged.emit(self.number) def y_clicked(self): old_state = self.state self.set_state(State.Y) if self.state != old_state: self.stateChanged.emit(self.number) def spinbox_changed(self): self.value = self.spinbox.value() self.update_slider() def slider_changed(self): self.value = self.get_bin_center(self.slider.value()) self.update_spinbox() self.valueChanged.emit() def get_bin_center(self, n): return (n + 0.5) * self.width + self.minimum def update_slider(self): i = (self.value - self.minimum) / self.width self.slider.setValue(int(min(max(i, 0), self.nbins - 1))) def update_spinbox(self): self.spinbox.setValue(self.value) def set_value(self, value): self.value = value self.update_slider() self.update_spinbox() def get_value(self): return self.value
class MainWindow(QWidget): def __init__(self, config: Config) -> None: """ Main window with the GUI and whatever player is being used. """ super().__init__() self.setWindowTitle('vidify') # Setting the window to stay on top if config.stay_on_top: self.setWindowFlags(Qt.WindowStaysOnTopHint) # Setting the fullscreen and window size if config.fullscreen: self.showFullScreen() else: self.resize(config.width or 800, config.height or 600) self.layout = QHBoxLayout(self) self.layout.setContentsMargins(0, 0, 0, 0) self.layout.setSpacing(0) # Loading the used fonts (Inter) font_db = QFontDatabase() for font in Res.fonts: font_db.addApplicationFont(font) # Initializing the player and saving the config object in the window. self.player = initialize_player(config.player, config) logging.info("Using %s as the player", config.player) self.config = config # The audiosync feature is optional until it's more stable. if self.config.audiosync: from vidify.audiosync import AudiosyncWorker self.audiosync = AudiosyncWorker() else: self.audiosync = None # The API initialization is more complex. For more details, please # check the flow diagram in vidify.api. First we have to check if # the API is saved in the config: try: api_data = get_api_data(config.api) except KeyError: # Otherwise, the user is prompted for an API. After choosing one, # it will be initialized from outside this function. logging.info("API not found: prompting the user") self.API_selection = APISelection() self.layout.addWidget(self.API_selection) self.API_selection.api_chosen.connect(self.on_api_selection) else: logging.info("Using %s as the API", config.api) self.initialize_api(api_data) @Slot(str) def on_api_selection(self, api_str: str) -> None: """ Method called when the API is selected with APISelection. The provided api string must be an existent entry inside the APIData enumeration. """ # Removing the widget used to obtain the API string self.layout.removeWidget(self.API_selection) self.API_selection.setParent(None) self.API_selection.hide() del self.API_selection # Saving the API in the config self.config.api = api_str # Starting the API initialization self.initialize_api(APIData[api_str]) def initialize_api(self, api_data: APIData, do_start: bool = True) -> None: """ Initializes an API with the information from APIData. """ # The API may need interaction with the user to obtain credentials # or similar data. This function will already take care of the # rest of the initialization. if api_data.gui_init_fn is not None: fn = getattr(self, api_data.gui_init_fn) fn() return mod = importlib.import_module(api_data.module) cls = getattr(mod, api_data.class_name) self.api = cls() # Some custom API initializations may not want to start the API # inside this function. if do_start: self.start(self.api.connect_api, message=api_data.connect_msg, event_loop_interval=api_data.event_loop_interval) def start(self, connect: Callable[[], None], message: Optional[str] = None, event_loop_interval: int = 1000) -> None: """ Waits for a Spotify session to be opened or for a song to play. Times out after 30 seconds to avoid infinite loops or too many API/process requests. A custom message will be shown meanwhile. If a `connect` call was succesful, the `init` function will be called with `init_args` as arguments. Otherwise, the program is closed. An event loop can also be initialized by passing `event_loop` and `event_interval`. If the former is None, nothing will be done. """ # Initializing values as attributes so that they can be accessed # from the function called with QTimer. self.conn_counter = 0 self.conn_fn = connect self.conn_attempts = 120 # 2 minutes, at 1 connection attempt/second self.event_loop_interval = event_loop_interval # Creating a label with a loading message that will be shown when the # connection attempt is successful. self.loading_label = QLabel("Loading...") self.loading_label.setFont(Fonts.title) self.loading_label.setMargin(50) self.loading_label.setAlignment(Qt.AlignCenter) self.layout.addWidget(self.loading_label) # Creating the label to wait for connection. It starts hidden, since # it's only shown if the first attempt to connect fails. self.conn_label = QLabel(message or "Waiting for connection") self.conn_label.hide() self.conn_label.setWordWrap(True) self.conn_label.setFont(Fonts.header) self.conn_label.setMargin(50) self.conn_label.setAlignment(Qt.AlignCenter) self.layout.addWidget(self.conn_label) # Creating the QTimer to check for connection every second. self.conn_timer = QTimer(self) self.conn_timer.timeout.connect(self.wait_for_connection) self.conn_timer.start(1000) @Slot() def wait_for_connection(self) -> None: """ Function called by start() to check every second if the connection has been established. """ # Saving the starting timestamp for the audiosync feature start_time = time.time() # Changing the loading message for the connection one if the first # connection attempt was unsuccessful. if self.conn_counter == 1: self.layout.removeWidget(self.loading_label) self.loading_label.hide() self.conn_label.show() # The APIs should raise `ConnectionNotReady` if the first attempt # to get metadata from Spotify was unsuccessful. logging.info("Connection attempt %d", self.conn_counter + 1) try: self.conn_fn() except ConnectionNotReady: pass else: logging.info("Succesfully connected to the API") # Stopping the timer and changing the label to the loading one. self.conn_timer.stop() self.layout.removeWidget(self.conn_label) del self.conn_timer self.layout.removeWidget(self.conn_label) self.conn_label.hide() del self.conn_label self.layout.removeWidget(self.loading_label) del self.loading_label # Loading the player and more self.setStyleSheet(f"background-color:{Colors.black};") self.layout.addWidget(self.player) self.play_video(self.api.artist, self.api.title, start_time) # Connecting to the signals generated by the API self.api.new_song_signal.connect(self.play_video) self.api.position_signal.connect(self.change_video_position) self.api.status_signal.connect(self.change_video_status) # Starting the event loop if it was initially passed as # a parameter. if self.event_loop_interval is not None: self.start_event_loop(self.api.event_loop, self.event_loop_interval) self.conn_counter += 1 # If the maximum amount of attempts is reached, the app is closed. if self.conn_counter >= self.conn_attempts: print("Timed out waiting for the connection") self.conn_timer.stop() QCoreApplication.exit(1) def start_event_loop(self, event_loop: Callable[[], None], ms: int) -> None: """ Starts a "manual" event loop with a timer every `ms` milliseconds. This is used with the SwSpotify API and the Web API to check every `ms` seconds if a change has happened, like if the song was paused. """ logging.info("Starting event loop") timer = QTimer(self) # Qt doesn't accept a method as the parameter so it's converted # to a function. if isinstance(event_loop, types.MethodType): timer.timeout.connect(lambda: event_loop()) else: timer.timeout.connect(event_loop) timer.start(ms) @Slot(bool) def change_video_status(self, is_playing: bool) -> None: """ Slot used for API updates of the video status. """ self.player.pause = not is_playing # If there is an audiosync thread running, this will pause the sound # recording and youtube downloading. try: self.audiosync.is_running = is_playing except AttributeError: pass @Slot(int) def change_video_position(self, ms: int) -> None: """ Slot used for API updates of the video position. """ if not self.config.audiosync: self.player.position = ms @Slot(str, str, float) def play_video(self, artist: str, title: str, start_time: float) -> None: """ Slot used to play a video. This is called when the API is first initialized from this GUI, and afterwards from the event loop handler whenever a new song is detected. If an error was detected when downloading the video, the default one is shown instead. """ # Checking that the artist and title are valid first of all if self.api.artist in (None, '') and self.api.title in (None, ''): logging.info("The provided artist and title are empty.") self.on_youtubedl_fail() return # This delay is used to know the elapsed time until the video # actually starts playing, used in the audiosync feature. self.timestamp = start_time # Loading the audio synchronization feature before anything else query = f"ytsearch:{format_name(artist, title)} Official Video" if self.config.audiosync: # First trying to stop the previous audiosync thread, as only # one audiosync thread can be running at once. If it wasn't # initialized, the worker is created. try: # Although inheriting from QThread and reusing the same object # may not be the standard, QThread.start() is guaranteed to # work once QThread.run() has returned. Thus, this will wait # until it's done and launch the new one. logging.info("Stopping the previous audiosync thread") self.audiosync.abort() self.audiosync.wait() except AttributeError: logging.info("Creating a new audiosync thread") from vidify.audiosync import AudiosyncWorker self.audiosync = AudiosyncWorker() self.audiosync.success.connect(self.on_audiosync_success) self.audiosync.failed.connect(self.on_audiosync_fail) logging.info("Starting the audiosync thread") self.audiosync.youtube_title = query self.audiosync.start() # Launching the thread with YouTube-DL to obtain the video URL # without blocking the GUI. logging.info("Starting the youtube-dl thread") self.youtubedl = YouTubeDLWorker(query, self.config.debug, self.config.width, self.config.height) self.yt_thread = QThread() self.youtubedl.moveToThread(self.yt_thread) self.yt_thread.started.connect(self.youtubedl.get_url) self.youtubedl.success.connect(self.on_yt_success) self.youtubedl.fail.connect(self.on_youtubedl_fail) self.youtubedl.finish.connect(self.yt_thread.exit) self.yt_thread.start() @Slot() def on_youtubedl_fail(self) -> None: """ If Youtube-dl for whatever reason failed to load the video, a fallback error video is shown, along with a message to let the user know what happened. """ self.player.start_video(Res.default_video, self.api.is_playing) print("The video wasn't found, either because of an issue with your" " internet connection or because the provided data was invalid." " For more information, enable the debug mode.") @Slot(str) def on_yt_success(self, url: str) -> None: """ Obtains the video URL from the Youtube-dl thread and starts playing the video. Also shows the lyrics if enabled. The position of the video isn't set if it's using audiosync, because this is done by the AudiosyncWorker thread. """ self.player.start_video(url, self.api.is_playing) if not self.config.audiosync: try: self.player.position = self.api.position except NotImplementedError: self.player.position = 0 # Finally, the lyrics are displayed. If the video wasn't found, an # error message is shown. if self.config.lyrics: print(get_lyrics(self.api.artist, self.api.title)) @Slot(int) def on_audiosync_success(self, lag: int) -> None: """ Slot used after the audiosync function has finished. It sets the returned lag in milliseconds on the player. This assumes that the song wasn't paused until this issue is fixed: https://github.com/vidify/audiosync/issues/12 """ logging.info("Audiosync module returned %d ms", lag) # The current API position according to what's being recorded. playback_delay = round((time.time() - self.timestamp) * 1000) \ - self.player.position lag += playback_delay # The user's custom audiosync delay. This is basically the time taken # until the module started recording (which may depend on the user # hardware and other things). Thus, it will almost always be a # negative value. lag += self.config.audiosync_calibration logging.info("Total delay is %d ms", lag) if lag > 0: self.player.position += lag elif lag < 0: # If a negative delay is larger than the current player position, # the player position is set to zero after the lag has passed # with a timer. if self.player.position < -lag: self.sync_timer = QTimer(self) self.sync_timer.singleShot( -lag, lambda: self.change_video_position(0)) else: self.player.position += lag @Slot() def on_audiosync_fail(self) -> None: logging.info("Audiosync module failed to return the lag") def init_spotify_web_api(self) -> None: """ SPOTIFY WEB API CUSTOM FUNCTION Note: the Tekore imports are done inside the functions so that Tekore isn't needed for whoever doesn't plan to use the Spotify Web API. """ from vidify.api.spotify.web import get_token from vidify.gui.api.spotify_web import SpotifyWebPrompt token = get_token(self.config.refresh_token, self.config.client_id, self.config.client_secret) if token is not None: # If the previous token was valid, the API can already start. logging.info("Reusing a previously generated token") self.start_spotify_web_api(token, save_config=False) else: # Otherwise, the credentials are obtained with the GUI. When # a valid auth token is ready, the GUI will initialize the API # automatically exactly like above. The GUI won't ask for a # redirect URI for now. logging.info("Asking the user for credentials") # The SpotifyWebPrompt handles the interaction with the user and # emits a `done` signal when it's done. self._spotify_web_prompt = SpotifyWebPrompt( self.config.client_id, self.config.client_secret, self.config.redirect_uri) self._spotify_web_prompt.done.connect(self.start_spotify_web_api) self.layout.addWidget(self._spotify_web_prompt) def start_spotify_web_api(self, token: 'RefreshingToken', save_config: bool = True) -> None: """ SPOTIFY WEB API CUSTOM FUNCTION Initializes the Web API, also saving them in the config for future usage (if `save_config` is true). """ from vidify.api.spotify.web import SpotifyWebAPI logging.info("Initializing the Spotify Web API") # Initializing the web API self.api = SpotifyWebAPI(token) api_data = APIData['SPOTIFY_WEB'] self.start(self.api.connect_api, message=api_data.connect_msg, event_loop_interval=api_data.event_loop_interval) # The obtained credentials are saved for the future if save_config: logging.info("Saving the Spotify Web API credentials") self.config.client_secret = self._spotify_web_prompt.client_secret self.config.client_id = self._spotify_web_prompt.client_id self.config.refresh_token = token.refresh_token # The credentials prompt widget is removed after saving the data. It # may not exist because start_spotify_web_api was called directly, # so errors are taken into account. try: self.layout.removeWidget(self._spotify_web_prompt) self._spotify_web_prompt.hide() del self._spotify_web_prompt except AttributeError: pass
class DimensionNonIntegrated(Dimension): binningChanged = Signal() """ A dimension that can either be sliced through or rebinned. It has additional properties for either number_of_bins or thickness from mantidqt.widgets.sliceviewer.dimensionwidget import DimensionMDE from qtpy.QtWidgets import QApplication app = QApplication([]) window = DimensionNonIntegrated({'minimum':-1.1, 'number_of_bins':11, 'width':0.2, 'name':'Dim0', 'units':'A'}) window.show() app.exec_() """ def __init__(self, dim_info, number=0, state=State.NONE, parent=None): # hack in a number_of_bins for MDEventWorkspace if dim_info['type'] == 'MDE': dim_info['number_of_bins'] = 100 dim_info['width'] = (dim_info['maximum'] - dim_info['minimum']) / 100 self.spinBins = QSpinBox() self.spinBins.setRange(2, 9999) self.spinBins.setValue(dim_info['number_of_bins']) self.spinBins.hide() self.spinBins.setMinimumWidth(110) self.spinThick = QDoubleSpinBox() self.spinThick.setRange(0.001, 999) self.spinThick.setValue(0.1) self.spinThick.setSingleStep(0.1) self.spinThick.setDecimals(3) self.spinThick.setMinimumWidth(110) self.rebinLabel = QLabel("thick") self.rebinLabel.setMinimumWidth(44) super().__init__(dim_info, number, state, parent) self.spinBins.valueChanged.connect(self.binningChanged) self.spinThick.valueChanged.connect(self.valueChanged) self.layout.addWidget(self.spinBins) self.layout.addWidget(self.spinThick) self.layout.addWidget(self.rebinLabel) def get_bins(self): return int(self.spinBins.value()) def get_thickness(self): return float(self.spinThick.value()) def set_state(self, state): super().set_state(state) if self.state == State.X: self.spinBins.show() self.spinThick.hide() self.rebinLabel.setText('bins') elif self.state == State.Y: self.spinBins.show() self.spinThick.hide() self.rebinLabel.setText('bins') elif self.state == State.NONE: self.spinBins.hide() self.spinThick.show() self.rebinLabel.setText('thick') else: self.spinBins.hide() self.spinThick.hide() self.rebinLabel.hide() def set_value(self, value): """Override the set_value for MDE, this allows the exact value to be set instead of limiting to the value of the slider. This allows when selecting a peak to go to the exact layer where the peak is. """ self.value = value # temporary disable updating value from slider change self.update_value_from_slider = False self.update_slider() self.update_value_from_slider = True self.update_spinbox()
class PyDMSlider(QFrame, TextFormatter, PyDMWritableWidget): """ A QSlider with support for Channels and more from PyDM. Parameters ---------- parent : QWidget The parent widget for the Label init_channel : str, optional The channel to be used by the widget. """ actionTriggered = Signal(int) rangeChanged = Signal(float, float) sliderMoved = Signal(float) sliderPressed = Signal() sliderReleased = Signal() valueChanged = Signal(float) def __init__(self, parent=None, init_channel=None): QFrame.__init__(self, parent) PyDMWritableWidget.__init__(self, init_channel=init_channel) self.alarmSensitiveContent = True self.alarmSensitiveBorder = False # Internal values for properties self.set_enable_state() self._show_limit_labels = True self._show_value_label = True self._user_defined_limits = False self._needs_limit_info = True self._minimum = None self._maximum = None self._user_minimum = -10.0 self._user_maximum = 10.0 self._num_steps = 101 self._orientation = Qt.Horizontal # Set up all the internal widgets that make up a PyDMSlider. # We'll add all these things to layouts when we call setup_widgets_for_orientation label_size_policy = QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Fixed) self.low_lim_label = QLabel(self) self.low_lim_label.setObjectName("lowLimLabel") self.low_lim_label.setSizePolicy(label_size_policy) self.low_lim_label.setAlignment(Qt.AlignLeft | Qt.AlignTrailing | Qt.AlignVCenter) self.value_label = QLabel(self) self.value_label.setObjectName("valueLabel") self.value_label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.high_lim_label = QLabel(self) self.high_lim_label.setObjectName("highLimLabel") self.high_lim_label.setSizePolicy(label_size_policy) self.high_lim_label.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter) self._slider = QSlider(parent=self) self._slider.setOrientation(Qt.Horizontal) self._slider.sliderMoved.connect(self.internal_slider_moved) self._slider.sliderPressed.connect(self.internal_slider_pressed) self._slider.sliderReleased.connect(self.internal_slider_released) self._slider.valueChanged.connect(self.internal_slider_value_changed) # self.vertical_layout.addWidget(self._slider) # Other internal variables and final setup steps self._slider_position_to_value_map = None self._mute_internal_slider_changes = False self.setup_widgets_for_orientation(self._orientation) self.reset_slider_limits() def init_for_designer(self): """ Method called after the constructor to tweak configurations for when using the widget with the Qt Designer """ self.value = 0.0 @Property(Qt.Orientation) def orientation(self): """ The slider orientation (Horizontal or Vertical) Returns ------- int Qt.Horizontal or Qt.Vertical """ return self._orientation @orientation.setter def orientation(self, new_orientation): """ The slider orientation (Horizontal or Vertical) Parameters ---------- new_orientation : int Qt.Horizontal or Qt.Vertical """ self._orientation = new_orientation self.setup_widgets_for_orientation(new_orientation) def setup_widgets_for_orientation(self, new_orientation): """ Reconstruct the widget given the orientation. Parameters ---------- new_orientation : int Qt.Horizontal or Qt.Vertical """ if new_orientation not in (Qt.Horizontal, Qt.Vertical): logger.error( "Invalid orientation '{0}'. The existing layout will not change." .format(new_orientation)) return layout = None if new_orientation == Qt.Horizontal: layout = QVBoxLayout() layout.setContentsMargins(4, 0, 4, 4) label_layout = QHBoxLayout() label_layout.addWidget(self.low_lim_label) label_layout.addStretch(0) label_layout.addWidget(self.value_label) label_layout.addStretch(0) label_layout.addWidget(self.high_lim_label) layout.addLayout(label_layout) self._slider.setOrientation(new_orientation) layout.addWidget(self._slider) elif new_orientation == Qt.Vertical: layout = QHBoxLayout() layout.setContentsMargins(0, 4, 4, 4) label_layout = QVBoxLayout() label_layout.addWidget(self.high_lim_label) label_layout.addStretch(0) label_layout.addWidget(self.value_label) label_layout.addStretch(0) label_layout.addWidget(self.low_lim_label) layout.addLayout(label_layout) self._slider.setOrientation(new_orientation) layout.addWidget(self._slider) if self.layout() is not None: # Trick to remove the existing layout by re-parenting it in an empty widget. QWidget().setLayout(self.layout()) self.setLayout(layout) def update_labels(self): """ Update the limits and value labels with the correct values. """ def set_label(value, label_widget): if value is None: label_widget.setText("") else: label_widget.setText(self.format_string.format(value)) set_label(self.minimum, self.low_lim_label) set_label(self.maximum, self.high_lim_label) set_label(self.value, self.value_label) def reset_slider_limits(self): """ Reset the limits and adjust the labels properly for the slider. """ if self.minimum is None or self.maximum is None: self._needs_limit_info = True self.set_enable_state() return self._needs_limit_info = False self._slider.setMinimum(0) self._slider.setMaximum(self._num_steps - 1) self._slider.setSingleStep(1) self._slider.setPageStep(10) self._slider_position_to_value_map = np.linspace(self.minimum, self.maximum, num=self._num_steps) self.update_labels() self.set_slider_to_closest_value(self.value) self.rangeChanged.emit(self.minimum, self.maximum) self.set_enable_state() def find_closest_slider_position_to_value(self, val): """ Find and returns the index for the closest position on the slider for a given value. Parameters ---------- val : float Returns ------- int """ diff = abs(self._slider_position_to_value_map - float(val)) return np.argmin(diff) def set_slider_to_closest_value(self, val): """ Set the value for the slider according to a given value. Parameters ---------- val : float """ if val is None or self._needs_limit_info: return # When we set the slider to the closest value, it may end up at a slightly # different position than val (if val is not in self._slider_position_to_value_map) # We don't want that slight difference to get broacast out and put the channel # somewhere new. For example, if the slider can only be at 0.4 or 0.5, but a # new value comes in of 0.45, its more important to keep the 0.45 than to change # it to where the slider gets set. Therefore, we mute the internal slider changes # so that its valueChanged signal doesn't cause us to emit a signal to PyDM to change # the value of the channel. self._mute_internal_slider_changes = True self._slider.setValue(self.find_closest_slider_position_to_value(val)) self._mute_internal_slider_changes = False def value_changed(self, new_val): """ Callback invoked when the Channel value is changed. Parameters ---------- new_val : int or float The new value from the channel. """ PyDMWritableWidget.value_changed(self, new_val) if hasattr(self, "value_label"): self.value_label.setText(self.format_string.format(self.value)) if not self._slider.isSliderDown(): self.set_slider_to_closest_value(self.value) def ctrl_limit_changed(self, which, new_limit): """ Callback invoked when the Channel receives new control limit values. Parameters ---------- which : str Which control limit was changed. "UPPER" or "LOWER" new_limit : float New value for the control limit """ PyDMWritableWidget.ctrl_limit_changed(self, which, new_limit) if not self.userDefinedLimits: self.reset_slider_limits() def update_format_string(self): """ Reconstruct the format string to be used when representing the output value. Returns ------- format_string : str The format string to be used including or not the precision and unit """ fs = super(PyDMSlider, self).update_format_string() self.update_labels() return fs def set_enable_state(self): """ Determines wether or not the widget must be enabled or not depending on the write access, connection state and presence of limits information """ self.setEnabled(self._write_access and self._connected and not self._needs_limit_info) @Slot(int) def internal_slider_action_triggered(self, action): self.actionTriggered.emit(action) @Slot(int) def internal_slider_moved(self, val): """ Method invoked when the slider is moved. Parameters ---------- val : float """ # The user has moved the slider, we need to update our value. # Only update the underlying value, not the self.value property, # because we don't need to reset the slider position. If we change # self.value, we can get into a loop where the position changes, which # updates the value, which changes the position again, etc etc. self.value = self._slider_position_to_value_map[val] self.sliderMoved.emit(self.value) @Slot() def internal_slider_pressed(self): """ Method invoked when the slider is pressed """ self.sliderPressed.emit() @Slot() def internal_slider_released(self): """ Method invoked when the slider is released """ self.sliderReleased.emit() @Slot(int) def internal_slider_value_changed(self, val): """ Method invoked when a new value is selected on the slider. This will cause the new value to be emitted to the signal unless `mute_internal_slider_changes` is True. Parameters ---------- val : int """ # At this point, our local copy of the value reflects the position of the # slider, now all we need to do is emit a signal to PyDM so that the data # plugin will send a put to the channel. Don't update self.value or self._value # in here, it is pointless at best, and could cause an infinite loop at worst. if not self._mute_internal_slider_changes: self.send_value_signal[float].emit(self.value) @Property(bool) def showLimitLabels(self): """ Whether or not the high and low limits should be displayed on the slider. Returns ------- bool """ return self._show_limit_labels @showLimitLabels.setter def showLimitLabels(self, checked): """ Whether or not the high and low limits should be displayed on the slider. Parameters ---------- checked : bool """ self._show_limit_labels = checked if checked: self.low_lim_label.show() self.high_lim_label.show() else: self.low_lim_label.hide() self.high_lim_label.hide() @Property(bool) def showValueLabel(self): """ Whether or not the current value should be displayed on the slider. Returns ------- bool """ return self._show_value_label @showValueLabel.setter def showValueLabel(self, checked): """ Whether or not the current value should be displayed on the slider. Parameters ---------- checked : bool """ self._show_value_label = checked if checked: self.value_label.show() else: self.value_label.hide() @Property(QSlider.TickPosition) def tickPosition(self): """ Where to draw tick marks for the slider. Returns ------- QSlider.TickPosition """ return self._slider.tickPosition() @tickPosition.setter def tickPosition(self, position): """ Where to draw tick marks for the slider. Parameter --------- position : QSlider.TickPosition """ self._slider.setTickPosition(position) @Property(bool) def userDefinedLimits(self): """ Wether or not to use limits defined by the user and not from the channel Returns ------- bool """ return self._user_defined_limits @userDefinedLimits.setter def userDefinedLimits(self, user_defined_limits): """ Wether or not to use limits defined by the user and not from the channel Parameters ---------- user_defined_limits : bool """ self._user_defined_limits = user_defined_limits self.reset_slider_limits() @Property(float) def userMinimum(self): """ Lower user defined limit value Returns ------- float """ return self._user_minimum @userMinimum.setter def userMinimum(self, new_min): """ Lower user defined limit value Parameters ---------- new_min : float """ self._user_minimum = float(new_min) if new_min is not None else None if self.userDefinedLimits: self.reset_slider_limits() @Property(float) def userMaximum(self): """ Upper user defined limit value Returns ------- float """ return self._user_maximum @userMaximum.setter def userMaximum(self, new_max): """ Upper user defined limit value Parameters ---------- new_max : float """ self._user_maximum = float(new_max) if new_max is not None else None if self.userDefinedLimits: self.reset_slider_limits() @property def minimum(self): """ The current value being used for the lower limit Returns ------- float """ if self.userDefinedLimits: return self._user_minimum return self._lower_ctrl_limit @property def maximum(self): """ The current value being used for the upper limit Returns ------- float """ if self.userDefinedLimits: return self._user_maximum return self._upper_ctrl_limit @Property(int) def num_steps(self): """ The number of steps on the slider Returns ------- int """ return self._num_steps @num_steps.setter def num_steps(self, new_steps): """ The number of steps on the slider Parameters ---------- new_steps : int """ self._num_steps = int(new_steps) self.reset_slider_limits()
class SelectSmoothing(QDialog): """ SelectSmoothing launches a GUI and executes smoothing. Any output is added to the input data as a new component. """ def __init__(self, data, parent=None, smooth_cube=None, allow_preview=False, allow_spectral_axes=False): super(SelectSmoothing, self).__init__(parent) self.setWindowFlags(self.windowFlags() | Qt.Tool) self.parent = parent self.title = "Smoothing Selection" self.data = data # Glue data to be smoothed # Check if smooth object is the caller if smooth_cube is None: self.smooth_cube = SmoothCube(data=self.data) else: self.smooth_cube = smooth_cube self.allow_spectral_axes = allow_spectral_axes self.allow_preview = allow_preview self.is_preview_active = False # Flag to show if smoothing preview is active self.abort_window = None # Small window pop up when smoothing. self.component_id = None # Glue data component to smooth over self.current_axis = None # Selected smoothing_axis self.current_kernel_type = None # Selected kernel type, a key in SmoothCube.kernel_registry self.current_kernel_name = None # Name of selected kernel self._init_selection_ui() # Format and show gui def _init_selection_ui(self): # LINE 1: Radio box spatial vs spectral axis self.axes_prompt = QLabel("Smoothing Axis:") self.axes_prompt.setMinimumWidth(150) self.spatial_radio = QRadioButton("Spatial") self.spatial_radio.setChecked(True) self.current_axis = "spatial" self.spatial_radio.toggled.connect(self.spatial_radio_checked) self.spectral_radio = QRadioButton("Spectral") self.spectral_radio.toggled.connect(self.spectral_radio_checked) # hbl is short for Horizontal Box Layout hbl1 = QHBoxLayout() hbl1.addWidget(self.axes_prompt) hbl1.addWidget(self.spatial_radio) hbl1.addWidget(self.spectral_radio) # LINE 2: Kernel Type prompt self.k_type_prompt = QLabel("Kernel Type:") self.k_type_prompt.setMinimumWidth(150) # Load kernel types + names and add to drop down self._load_options() self.combo = QComboBox() self.combo.setMinimumWidth(150) self.combo.addItems(self.options[self.current_axis]) hbl2 = QHBoxLayout() hbl2.addWidget(self.k_type_prompt) hbl2.addWidget(self.combo) # LINE 3: Kernel size self.size_prompt = QLabel(self.smooth_cube.get_kernel_size_prompt(self.current_kernel_type)) self.size_prompt.setWordWrap(True) self.size_prompt.setMinimumWidth(150) self.unit_label = QLabel(self.smooth_cube.get_kernel_unit(self.current_kernel_type)) self.k_size = QLineEdit("1") # Default Kernel size set here hbl3 = QHBoxLayout() hbl3.addWidget(self.size_prompt) hbl3.addWidget(self.k_size) hbl3.addWidget(self.unit_label) # LINE 4: Data component drop down self.component_prompt = QLabel("Data Component:") self.component_prompt.setWordWrap(True) self.component_prompt.setMinimumWidth(150) # Load component_ids and add to drop down # Add the data component labels to the drop down, with the ComponentID # set as the userData: if self.parent is not None and hasattr(self.parent, 'data_components'): labeldata = [(str(cid), cid) for cid in self.parent.data_components] else: labeldata = [(str(cid), cid) for cid in self.data.main_components()] self.component_combo = QComboBox() update_combobox(self.component_combo, labeldata) self.component_combo.setMaximumWidth(150) self.component_combo.setCurrentIndex(0) if self.allow_preview: self.component_combo.currentIndexChanged.connect(self.update_preview_button) hbl4 = QHBoxLayout() hbl4.addWidget(self.component_prompt) hbl4.addWidget(self.component_combo) # Line 5: Preview Message message = "Info: Smoothing previews are displayed on " \ "CubeViz's left and single image viewers." self.preview_message = QLabel(message) self.preview_message.setWordWrap(True) self.preview_message.hide() hbl5 = QHBoxLayout() hbl5.addWidget(self.preview_message) # LINE 6: preview ok cancel buttons self.previewButton = QPushButton("Preview Slice") self.previewButton.clicked.connect(self.call_preview) self.okButton = QPushButton("Smooth Cube") self.okButton.clicked.connect(self.call_main) self.okButton.setDefault(True) self.cancelButton = QPushButton("Cancel") self.cancelButton.clicked.connect(self.cancel) hbl6 = QHBoxLayout() hbl6.addStretch(1) if self.allow_preview: hbl6.addWidget(self.previewButton) hbl6.addWidget(self.cancelButton) hbl6.addWidget(self.okButton) # Add Lines to Vertical Layout # vbl is short for Vertical Box Layout vbl = QVBoxLayout() if self.allow_spectral_axes: vbl.addLayout(hbl1) vbl.addLayout(hbl2) vbl.addLayout(hbl3) vbl.addLayout(hbl4) vbl.addLayout(hbl5) vbl.addLayout(hbl6) self.setLayout(vbl) self.setMaximumWidth(330) # Connect kernel combo box to event handler self.combo.currentIndexChanged.connect(self.selection_changed) self.selection_changed(0) self.show() def _load_options(self): """Extract names + types of kernels from SmoothCube.kernel_registry""" kernel_registry = self.smooth_cube.get_kernel_registry() self.options = {"spatial": [], "spectral": []} for k in kernel_registry: axis = kernel_registry[k]["axis"] for a in axis: if "spatial" == a: self.options["spatial"].append(kernel_registry[k]["name"]) elif "spectral" == a: self.options["spectral"].append(kernel_registry[k]["name"]) self.options["spectral"].sort() self.options["spatial"].sort() self.current_kernel_name = self.options[self.current_axis][0] self.current_kernel_type = self.smooth_cube.name_to_kernel_type(self.options[self.current_axis][0]) def selection_changed(self, i): """ Update kernel type, units, etc... when kernel name changes in combo box. """ keys = self.options[self.current_axis] name = keys[i] self.current_kernel_name = name self.current_kernel_type = self.smooth_cube.name_to_kernel_type(name) self.unit_label.setText(self.smooth_cube.get_kernel_unit(self.current_kernel_type)) self.size_prompt.setText(self.smooth_cube.get_kernel_size_prompt(self.current_kernel_type)) def spatial_radio_checked(self): self.current_axis = "spatial" self.update_preview_button() self.combo.clear() self.combo.addItems(self.options[self.current_axis]) def spectral_radio_checked(self): self.current_axis = "spectral" self.update_preview_button() self.combo.clear() self.combo.addItems(self.options[self.current_axis]) def input_validation(self): """ Check if input will break Smoothing :return: bool: True if no errors """ red = "background-color: rgba(255, 0, 0, 128);" success = True # Check 1: k_size if self.k_size == "": self.k_size.setStyleSheet(red) success = False else: try: if self.current_kernel_type == "median": k_size = int(self.k_size.text()) else: k_size = float(self.k_size.text()) if k_size <= 0: self.k_size.setStyleSheet(red) success = False else: self.k_size.setStyleSheet("") except ValueError: if self.current_kernel_type == "median": info = QMessageBox.critical(self, "Error", "Kernel size must be integer for median") self.k_size.setStyleSheet(red) success = False return success def call_main(self): try: self.main() except Exception as e: info = QMessageBox.critical(self, "Error", str(e)) self.cancel() raise def main(self): """ Main function to process input and call smoothing function """ success = self.input_validation() if not success: return self.hide() self.abort_window = AbortWindow(self) QApplication.processEvents() # Add smoothing parameters self.smooth_cube.abort_window = self.abort_window if self.smooth_cube.parent is None and self.parent is not self.smooth_cube: self.smooth_cube.parent = self.parent if self.parent is not self.smooth_cube: self.smooth_cube.data = self.data self.smooth_cube.smoothing_axis = self.current_axis self.smooth_cube.kernel_type = self.current_kernel_type if self.current_kernel_type == "median": self.smooth_cube.kernel_size = int(self.k_size.text()) else: self.smooth_cube.kernel_size = float(self.k_size.text()) self.smooth_cube.component_id = str(self.component_combo.currentText()) self.smooth_cube.output_as_component = True if self.is_preview_active: self.parent.end_smoothing_preview() self.is_preview_active = False self.smooth_cube.multi_threading_smooth() return def update_preview_button(self): if self.parent is None or "spatial" != self.current_axis: self.previewButton.setDisabled(True) return self.previewButton.setDisabled(False) return def call_preview(self): try: self.preview() except Exception as e: info = QMessageBox.critical(self, "Error", str(e)) self.cancel() raise def preview(self): """Preview current options""" success = self.input_validation() if not success: return if self.smooth_cube.parent is None and self.parent is not self.smooth_cube: self.smooth_cube.parent = self.parent self.smooth_cube.smoothing_axis = self.current_axis self.smooth_cube.kernel_type = self.current_kernel_type if self.current_kernel_type == "median": self.smooth_cube.kernel_size = int(self.k_size.text()) else: self.smooth_cube.kernel_size = float(self.k_size.text()) preview_function = self.smooth_cube.preview_smoothing preview_title = self.smooth_cube.get_preview_title() component_id = self.component_combo.currentData() self.parent.start_smoothing_preview(preview_function, component_id, preview_title) self.is_preview_active = True self.preview_message.show() def cancel(self): self.clean_up() def clean_up(self): self.close() if self.abort_window is not None: self.abort_window.close() if self.is_preview_active: self.parent.end_smoothing_preview() self.is_preview_active = False def closeEvent(self, event): if self.is_preview_active: self.parent.end_smoothing_preview() self.is_preview_active = False def keyPressEvent(self, e): if e.key() == Qt.Key_Escape: self.clean_up()
class ShortcutEditor(QDialog): """A dialog for entering key sequences.""" def __init__(self, parent, context, name, sequence, shortcuts): super(ShortcutEditor, self).__init__(parent) self._parent = parent self.context = context self.npressed = 0 self.keys = set() self.key_modifiers = set() self.key_non_modifiers = list() self.key_text = list() self.sequence = sequence self.new_sequence = None self.edit_state = True self.shortcuts = shortcuts # Widgets self.label_info = QLabel() self.label_info.setText(_("Press the new shortcut and select 'Ok': \n" "(Press 'Tab' once to switch focus between the shortcut entry \n" "and the buttons below it)")) self.label_current_sequence = QLabel(_("Current shortcut:")) self.text_current_sequence = QLabel(sequence) self.label_new_sequence = QLabel(_("New shortcut:")) self.text_new_sequence = CustomLineEdit(self) self.text_new_sequence.setPlaceholderText(sequence) self.helper_button = HelperToolButton() self.helper_button.hide() self.label_warning = QLabel() self.label_warning.hide() bbox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) self.button_ok = bbox.button(QDialogButtonBox.Ok) self.button_cancel = bbox.button(QDialogButtonBox.Cancel) # Setup widgets self.setWindowTitle(_('Shortcut: {0}').format(name)) self.button_ok.setFocusPolicy(Qt.NoFocus) self.button_ok.setEnabled(False) self.button_cancel.setFocusPolicy(Qt.NoFocus) self.helper_button.setToolTip('') self.helper_button.setFocusPolicy(Qt.NoFocus) style = """ QToolButton { margin:1px; border: 0px solid grey; padding:0px; border-radius: 0px; }""" self.helper_button.setStyleSheet(style) self.text_new_sequence.setFocusPolicy(Qt.NoFocus) self.label_warning.setFocusPolicy(Qt.NoFocus) # Layout spacing = 5 layout_sequence = QGridLayout() layout_sequence.addWidget(self.label_info, 0, 0, 1, 3) layout_sequence.addItem(QSpacerItem(spacing, spacing), 1, 0, 1, 2) layout_sequence.addWidget(self.label_current_sequence, 2, 0) layout_sequence.addWidget(self.text_current_sequence, 2, 2) layout_sequence.addWidget(self.label_new_sequence, 3, 0) layout_sequence.addWidget(self.helper_button, 3, 1) layout_sequence.addWidget(self.text_new_sequence, 3, 2) layout_sequence.addWidget(self.label_warning, 4, 2, 1, 2) layout = QVBoxLayout() layout.addLayout(layout_sequence) layout.addSpacing(spacing) layout.addWidget(bbox) self.setLayout(layout) # Signals bbox.accepted.connect(self.accept) bbox.rejected.connect(self.reject) def keyPressEvent(self, e): """Qt override.""" key = e.key() # Check if valid keys if key not in VALID_KEYS: self.invalid_key_flag = True return self.npressed += 1 self.key_non_modifiers.append(key) self.key_modifiers.add(key) self.key_text.append(e.text()) self.invalid_key_flag = False debug_print('key {0}, npressed: {1}'.format(key, self.npressed)) if key == Qt.Key_unknown: return # The user clicked just and only the special keys # Ctrl, Shift, Alt, Meta. if (key == Qt.Key_Control or key == Qt.Key_Shift or key == Qt.Key_Alt or key == Qt.Key_Meta): return modifiers = e.modifiers() if modifiers & Qt.ShiftModifier: key += Qt.SHIFT if modifiers & Qt.ControlModifier: key += Qt.CTRL if sys.platform == 'darwin': self.npressed -= 1 debug_print('decrementing') if modifiers & Qt.AltModifier: key += Qt.ALT if modifiers & Qt.MetaModifier: key += Qt.META self.keys.add(key) def toggle_state(self): """Switch between shortcut entry and Accept/Cancel shortcut mode.""" self.edit_state = not self.edit_state if not self.edit_state: self.text_new_sequence.setEnabled(False) if self.button_ok.isEnabled(): self.button_ok.setFocus() else: self.button_cancel.setFocus() else: self.text_new_sequence.setEnabled(True) self.text_new_sequence.setFocus() def nonedit_keyrelease(self, e): """Key release event for non-edit state.""" key = e.key() if key in [Qt.Key_Escape]: self.close() return if key in [Qt.Key_Left, Qt.Key_Right, Qt.Key_Up, Qt.Key_Down]: if self.button_ok.hasFocus(): self.button_cancel.setFocus() else: self.button_ok.setFocus() def keyReleaseEvent(self, e): """Qt override.""" self.npressed -= 1 if self.npressed <= 0: key = e.key() if len(self.keys) == 1 and key == Qt.Key_Tab: self.toggle_state() return if len(self.keys) == 1 and key == Qt.Key_Escape: self.set_sequence('') self.label_warning.setText(_("Please introduce a different " "shortcut")) if len(self.keys) == 1 and key in [Qt.Key_Return, Qt.Key_Enter]: self.toggle_state() return if not self.edit_state: self.nonedit_keyrelease(e) else: debug_print('keys: {}'.format(self.keys)) if self.keys and key != Qt.Key_Escape: self.validate_sequence() self.keys = set() self.key_modifiers = set() self.key_non_modifiers = list() self.key_text = list() self.npressed = 0 def check_conflicts(self): """Check shortcuts for conflicts.""" conflicts = [] for index, shortcut in enumerate(self.shortcuts): sequence = str(shortcut.key) if sequence == self.new_sequence and \ (shortcut.context == self.context or shortcut.context == '_' or self.context == '_'): conflicts.append(shortcut) return conflicts def update_warning(self, warning_type=NO_WARNING, conflicts=[]): """Update warning label to reflect conflict status of new shortcut""" if warning_type == NO_WARNING: warn = False tip = 'This shortcut is correct!' elif warning_type == SEQUENCE_CONFLICT: template = '<i>{0}<b>{1}</b></i>' tip_title = _('The new shorcut conflicts with:') + '<br>' tip_body = '' for s in conflicts: tip_body += ' - {0}: {1}<br>'.format(s.context, s.name) tip_body = tip_body[:-4] # Removing last <br> tip = template.format(tip_title, tip_body) warn = True elif warning_type == SEQUENCE_LENGTH: # Sequences with 5 keysequences (i.e. Ctrl+1, Ctrl+2, Ctrl+3, # Ctrl+4, Ctrl+5) are invalid template = '<i>{0}</i>' tip = _('A compound sequence can have {break} a maximum of ' '4 subsequences.{break}').format(**{'break': '<br>'}) warn = True elif warning_type == INVALID_KEY: template = '<i>{0}</i>' tip = _('Invalid key entered') + '<br>' warn = True self.helper_button.show() if warn: self.label_warning.show() self.helper_button.setIcon(get_std_icon('MessageBoxWarning')) self.button_ok.setEnabled(False) else: self.helper_button.setIcon(get_std_icon('DialogApplyButton')) self.label_warning.setText(tip) def set_sequence(self, sequence): """Set the new shortcut and update buttons.""" if not sequence or self.sequence == sequence: self.button_ok.setEnabled(False) different_sequence = False else: self.button_ok.setEnabled(True) different_sequence = True self.text_new_sequence.setText(sequence) self.new_sequence = sequence conflicts = self.check_conflicts() if conflicts and different_sequence: warning_type = SEQUENCE_CONFLICT else: warning_type = NO_WARNING self.update_warning(warning_type=warning_type, conflicts=conflicts) def validate_sequence(self): """Provide additional checks for accepting or rejecting shortcuts.""" if self.invalid_key_flag: self.update_warning(warning_type=INVALID_KEY) return for mod in MODIFIERS: non_mod = set(self.key_non_modifiers) non_mod.discard(mod) if mod in self.key_non_modifiers: self.key_non_modifiers.remove(mod) self.key_modifiers = self.key_modifiers - non_mod while u'' in self.key_text: self.key_text.remove(u'') self.key_text = [k.upper() for k in self.key_text] # Fix Backtab, Tab issue if os.name == 'nt': if Qt.Key_Backtab in self.key_non_modifiers: idx = self.key_non_modifiers.index(Qt.Key_Backtab) self.key_non_modifiers[idx] = Qt.Key_Tab if len(self.key_modifiers) == 0: # Filter single key allowed if self.key_non_modifiers[0] not in VALID_SINGLE_KEYS: return # Filter elif len(self.key_non_modifiers) > 1: return # QKeySequence accepts a maximum of 4 different sequences if len(self.keys) > 4: # Update warning self.update_warning(warning_type=SEQUENCE_LENGTH) return keys = [] for i in range(len(self.keys)): key_seq = 0 for m in self.key_modifiers: key_seq += MODIFIERS[m] key_seq += self.key_non_modifiers[i] keys.append(key_seq) sequence = QKeySequence(*keys) self.set_sequence(sequence.toString())
class RunDialog(QDialog): simulation_done = Signal(bool, str) simulation_termination_request = Signal() def __init__(self, config_file, run_model, simulation_arguments, parent=None): QDialog.__init__(self, parent) self.setWindowFlags(Qt.Window) self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint) self.setModal(True) self.setWindowModality(Qt.WindowModal) self.setWindowTitle("Simulations - {}".format(config_file)) self._snapshot_model = SnapshotModel(self) self._run_model = run_model self._isDetailedDialog = False self._minimum_width = 1200 ert = None if isinstance(run_model, BaseRunModel): ert = run_model.ert() self._simulations_argments = simulation_arguments self._ticker = QTimer(self) self._ticker.timeout.connect(self._on_ticker) progress_proxy_model = ProgressProxyModel(self._snapshot_model, parent=self) self._total_progress_label = QLabel( _TOTAL_PROGRESS_TEMPLATE.format( total_progress=0, phase_name=run_model.getPhaseName()), self, ) self._total_progress_bar = QProgressBar(self) self._total_progress_bar.setRange(0, 100) self._total_progress_bar.setTextVisible(False) self._iteration_progress_label = QLabel(self) self._progress_view = ProgressView(self) self._progress_view.setModel(progress_proxy_model) self._progress_view.setIndeterminate(True) legend_view = LegendView(self) legend_view.setModel(progress_proxy_model) self._tab_widget = QTabWidget(self) self._tab_widget.currentChanged.connect(self._current_tab_changed) self._snapshot_model.rowsInserted.connect(self.on_new_iteration) self._job_label = QLabel(self) self._job_model = JobListProxyModel(self, 0, 0, 0, 0) self._job_model.setSourceModel(self._snapshot_model) self._job_view = QTableView(self) self._job_view.setVerticalScrollMode(QAbstractItemView.ScrollPerItem) self._job_view.setSelectionBehavior(QAbstractItemView.SelectRows) self._job_view.setSelectionMode(QAbstractItemView.SingleSelection) self._job_view.clicked.connect(self._job_clicked) self._open_files = {} self._job_view.setModel(self._job_model) self.running_time = QLabel("") self.plot_tool = PlotTool(config_file) self.plot_tool.setParent(self) self.plot_button = QPushButton(self.plot_tool.getName()) self.plot_button.clicked.connect(self.plot_tool.trigger) self.plot_button.setEnabled(ert is not None) self.kill_button = QPushButton("Kill Simulations") self.done_button = QPushButton("Done") self.done_button.setHidden(True) self.restart_button = QPushButton("Restart") self.restart_button.setHidden(True) self.show_details_button = QPushButton("Show Details") self.show_details_button.setCheckable(True) size = 20 spin_movie = resourceMovie("ide/loading.gif") spin_movie.setSpeed(60) spin_movie.setScaledSize(QSize(size, size)) spin_movie.start() self.processing_animation = QLabel() self.processing_animation.setMaximumSize(QSize(size, size)) self.processing_animation.setMinimumSize(QSize(size, size)) self.processing_animation.setMovie(spin_movie) button_layout = QHBoxLayout() button_layout.addWidget(self.processing_animation) button_layout.addWidget(self.running_time) button_layout.addStretch() button_layout.addWidget(self.show_details_button) button_layout.addWidget(self.plot_button) button_layout.addWidget(self.kill_button) button_layout.addWidget(self.done_button) button_layout.addWidget(self.restart_button) button_widget_container = QWidget() button_widget_container.setLayout(button_layout) layout = QVBoxLayout() layout.addWidget(self._total_progress_label) layout.addWidget(self._total_progress_bar) layout.addWidget(self._iteration_progress_label) layout.addWidget(self._progress_view) layout.addWidget(legend_view) layout.addWidget(self._tab_widget) layout.addWidget(self._job_label) layout.addWidget(self._job_view) layout.addWidget(button_widget_container) self.setLayout(layout) self.kill_button.clicked.connect(self.killJobs) self.done_button.clicked.connect(self.accept) self.restart_button.clicked.connect(self.restart_failed_realizations) self.show_details_button.clicked.connect(self.toggle_detailed_progress) self.simulation_done.connect(self._on_simulation_done) self.setMinimumWidth(self._minimum_width) self._setSimpleDialog() def _current_tab_changed(self, index: int): # Clear the selection in the other tabs for i in range(0, self._tab_widget.count()): if i != index: self._tab_widget.widget(i).clearSelection() def _setSimpleDialog(self) -> None: self._isDetailedDialog = False self._tab_widget.setVisible(False) self._job_label.setVisible(False) self._job_view.setVisible(False) self.show_details_button.setText("Show Details") def _setDetailedDialog(self) -> None: self._isDetailedDialog = True self._tab_widget.setVisible(True) self._job_label.setVisible(True) self._job_view.setVisible(True) self.show_details_button.setText("Hide Details") @Slot(QModelIndex, int, int) def on_new_iteration(self, parent: QModelIndex, start: int, end: int) -> None: if not parent.isValid(): index = self._snapshot_model.index(start, 0, parent) iter_row = start self._iteration_progress_label.setText( f"Progress for iteration {index.internalPointer().id}") widget = RealizationWidget(iter_row) widget.setSnapshotModel(self._snapshot_model) widget.currentChanged.connect(self._select_real) self._tab_widget.addTab( widget, f"Realizations for iteration {index.internalPointer().id}") @Slot(QModelIndex) def _job_clicked(self, index): if not index.isValid(): return selected_file = index.data(FileRole) if selected_file and selected_file not in self._open_files: job_name = index.siblingAtColumn(0).data() viewer = FileDialog( selected_file, job_name, index.row(), index.model().get_real(), index.model().get_iter(), self, ) self._open_files[selected_file] = viewer def remove_file(): """ We have sometimes seen this fail because the selected file is not in open file, without being able to reproduce the exception. """ try: self._open_files.pop(selected_file) except KeyError: logger = logging.getLogger(__name__) logger.exception( f"Failed to pop: {selected_file} from {self._open_files}" ) viewer.finished.connect(remove_file) elif selected_file in self._open_files: self._open_files[selected_file].raise_() @Slot(QModelIndex) def _select_real(self, index): step = 0 stage = 0 real = index.row() iter_ = index.model().get_iter() self._job_model.set_step(iter_, real, stage, step) self._job_label.setText( f"Realization id {index.data(RealIens)} in iteration {iter_}") self._job_view.horizontalHeader().setSectionResizeMode( QHeaderView.Stretch) def reject(self): return def closeEvent(self, QCloseEvent): if self._run_model.isFinished(): self.simulation_done.emit(self._run_model.hasRunFailed(), self._run_model.getFailMessage()) else: # Kill jobs if dialog is closed if self.killJobs() != QMessageBox.Yes: QCloseEvent.ignore() def startSimulation(self): self._run_model.reset() self._snapshot_model.reset() self._tab_widget.clear() def run(): asyncio.set_event_loop(asyncio.new_event_loop()) self._run_model.startSimulations(self._simulations_argments) simulation_thread = Thread(name="ert_gui_simulation_thread") simulation_thread.setDaemon(True) simulation_thread.run = run simulation_thread.start() self._ticker.start(1000) tracker = create_tracker( self._run_model, num_realizations=self._simulations_argments["active_realizations"]. count(), ee_config=self._simulations_argments.get("ee_config", None), ) worker = TrackerWorker(tracker) worker_thread = QThread() worker.done.connect(worker_thread.quit) worker.consumed_event.connect(self._on_tracker_event) worker.moveToThread(worker_thread) self.simulation_done.connect(worker.stop) self._worker = worker self._worker_thread = worker_thread worker_thread.started.connect(worker.consume_and_emit) self._worker_thread.start() def killJobs(self): msg = "Are you sure you want to kill the currently running simulations?" if self._run_model.getQueueStatus().get( JobStatusType.JOB_QUEUE_UNKNOWN, 0) > 0: msg += "\n\nKilling a simulation with unknown status will not kill the realizations already submitted!" kill_job = QMessageBox.question(self, "Kill simulations?", msg, QMessageBox.Yes | QMessageBox.No) if kill_job == QMessageBox.Yes: # Normally this slot would be invoked by the signal/slot system, # but the worker is busy tracking the evaluation. self._worker.request_termination() self.reject() return kill_job @Slot(bool, str) def _on_simulation_done(self, failed, failed_msg): self.processing_animation.hide() self.kill_button.setHidden(True) self.done_button.setHidden(False) self.restart_button.setVisible(self.has_failed_realizations()) self.restart_button.setEnabled(self._run_model.support_restart) self._total_progress_bar.setValue(100) self._total_progress_label.setText( _TOTAL_PROGRESS_TEMPLATE.format( total_progress=100, phase_name=self._run_model.getPhaseName())) if failed: QMessageBox.critical( self, "Simulations failed!", f"The simulation failed with the following error:\n\n{failed_msg}", ) @Slot() def _on_ticker(self): runtime = self._run_model.get_runtime() self.running_time.setText(format_running_time(runtime)) @Slot(object) def _on_tracker_event(self, event): if isinstance(event, EndEvent): self.simulation_done.emit(event.failed, event.failed_msg) self._worker.stop() self._ticker.stop() elif isinstance(event, FullSnapshotEvent): if event.snapshot is not None: self._snapshot_model._add_snapshot(event.snapshot, event.iteration) self._progress_view.setIndeterminate(event.indeterminate) progress = int(event.progress * 100) self._total_progress_bar.setValue(progress) self._total_progress_label.setText( _TOTAL_PROGRESS_TEMPLATE.format(total_progress=progress, phase_name=event.phase_name)) elif isinstance(event, SnapshotUpdateEvent): if event.partial_snapshot is not None: self._snapshot_model._add_partial_snapshot( event.partial_snapshot, event.iteration) self._progress_view.setIndeterminate(event.indeterminate) progress = int(event.progress * 100) self._total_progress_bar.setValue(progress) self._total_progress_label.setText( _TOTAL_PROGRESS_TEMPLATE.format(total_progress=progress, phase_name=event.phase_name)) def has_failed_realizations(self): completed = self._run_model.completed_realizations_mask initial = self._run_model.initial_realizations_mask for (index, successful) in enumerate(completed): if initial[index] and not successful: return True return False def count_successful_realizations(self): """ Counts the realizations completed in the prevoius ensemble run :return: """ completed = self._run_model.completed_realizations_mask return completed.count(True) def create_mask_from_failed_realizations(self): """ Creates a BoolVector mask representing the failed realizations :return: Type BoolVector """ completed = self._run_model.completed_realizations_mask initial = self._run_model.initial_realizations_mask inverted_mask = BoolVector(default_value=False) for (index, successful) in enumerate(completed): inverted_mask[index] = initial[index] and not successful return inverted_mask def restart_failed_realizations(self): msg = QMessageBox(self) msg.setIcon(QMessageBox.Information) msg.setText( "Note that workflows will only be executed on the restarted realizations and that this might have unexpected consequences." ) msg.setWindowTitle("Restart Failed Realizations") msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel) result = msg.exec_() if result == QMessageBox.Ok: self.restart_button.setVisible(False) self.kill_button.setVisible(True) self.done_button.setVisible(False) active_realizations = self.create_mask_from_failed_realizations() self._simulations_argments[ "active_realizations"] = active_realizations self._simulations_argments[ "prev_successful_realizations"] = self._simulations_argments.get( "prev_successful_realizations", 0) self._simulations_argments[ "prev_successful_realizations"] += self.count_successful_realizations( ) self.startSimulation() @Slot() def toggle_detailed_progress(self): if self._isDetailedDialog: self._setSimpleDialog() else: self._setDetailedDialog() self.adjustSize()
class PyDMEmbeddedDisplay(QFrame, PyDMPrimitiveWidget): """ A QFrame capable of rendering a PyDM Display Parameters ---------- parent : QWidget The parent widget for the Label """ def __init__(self, parent=None): QFrame.__init__(self, parent) PyDMPrimitiveWidget.__init__(self) self.app = QApplication.instance() self._filename = None self._macros = None self._embedded_widget = None self._disconnect_when_hidden = True self._is_connected = False self._only_load_when_shown = True self._needs_load = True self.base_path = "" self.base_macros = {} if is_pydm_app(): self.base_path = self.app.directory_stack[-1] self.base_macros = self.app.macro_stack[-1] self.layout = QVBoxLayout(self) self.err_label = QLabel(self) self.err_label.setAlignment(Qt.AlignHCenter) self.layout.addWidget(self.err_label) self.layout.setContentsMargins(0, 0, 0, 0) self.err_label.hide() if not is_pydm_app(): self.setFrameShape(QFrame.Box) else: self.setFrameShape(QFrame.NoFrame) def minimumSizeHint(self): """ This property holds the recommended minimum size for the widget. Returns ------- QSize """ # This is totally arbitrary, I just want *some* visible nonzero size return QSize(100, 100) @Property(str) def macros(self): """ JSON-formatted string containing macro variables to pass to the embedded file. Returns ------- str """ if self._macros is None: return "" return self._macros @macros.setter def macros(self, new_macros): """ JSON-formatted string containing macro variables to pass to the embedded file. .. warning:: If the macros property is not defined before the filename property, The widget will not have any macros defined when it loads the embedded file. This behavior will be fixed soon. Parameters ---------- new_macros : str """ new_macros = str(new_macros) if new_macros != self._macros: self._macros = new_macros self._needs_load = True self.load_if_needed() @Property(str) def filename(self): """ Filename of the display to embed. Returns ------- str """ if self._filename is None: return "" return self._filename @filename.setter def filename(self, filename): """ Filename of the display to embed. Parameters ---------- filename : str """ filename = str(filename) if filename != self._filename: self._filename = filename self._needs_load = True self.load_if_needed() def parsed_macros(self): """ Dictionary containing the key value pair for each macro specified. Returns -------- dict """ m = macro.find_base_macros(self) m.update(macro.parse_macro_string(self.macros)) return m def load_if_needed(self): if (not self._only_load_when_shown ) or self.isVisible() or is_qt_designer(): self.embedded_widget = self.open_file() def open_file(self, force=False): """ Opens the widget specified in the widget's filename property. Returns ------- display : QWidget """ if (not force) and (not self._needs_load): return if not self.filename: return # Expand user (~ or ~user) and environment variables. fname = os.path.expanduser(os.path.expandvars(self.filename)) if self.base_path: full_fname = os.path.join(self.base_path, fname) if not is_pydm_app(): (filename, extension) = os.path.splitext(full_fname) if extension == ".ui": loadfunc = load_ui_file elif extension == ".py": loadfunc = load_py_file try: w = loadfunc(full_fname, macros=self.parsed_macros()) self._needs_load = False self.clear_error_text() return w except Exception as e: logger.exception( "Exception while opening embedded display file.") self.display_error_text(e) return None # If you get this far, you are running inside a PyDMApplication, load # using that system. try: if os.path.isabs(full_fname) and os.path.exists(full_fname): w = self.app.open_file(full_fname, macros=self.parsed_macros()) else: w = self.app.open_relative(fname, self, macros=self.parsed_macros()) self._needs_load = False self.clear_error_text() return w except (ValueError, IOError) as e: self.display_error_text(e) def clear_error_text(self): self.err_label.clear() self.err_label.hide() def display_error_text(self, e): self.err_label.setText( "Could not open {filename}.\nError: {err}".format( filename=self._filename, err=e)) self.err_label.show() @property def embedded_widget(self): """ The embedded widget being displayed. Returns ------- QWidget """ return self._embedded_widget @embedded_widget.setter def embedded_widget(self, new_widget): """ Defines the embedded widget to display inside the QFrame Parameters ---------- new_widget : QWidget """ should_reconnect = False if new_widget is self._embedded_widget: return if self._embedded_widget is not None: self.layout.removeWidget(self._embedded_widget) self._embedded_widget.deleteLater() self._embedded_widget = None if new_widget is not None: self._embedded_widget = new_widget self._embedded_widget.setParent(self) self.layout.addWidget(self._embedded_widget) self.err_label.hide() self._embedded_widget.show() self._is_connected = True def connect(self): """ Establish the connection between the embedded widget and the channels associated with it. """ if self._is_connected or self.embedded_widget is None: return establish_widget_connections(self.embedded_widget) def disconnect(self): """ Disconnects the embedded widget from the channels associated with it. """ if not self._is_connected or self.embedded_widget is None: return close_widget_connections(self.embedded_widget) @Property(bool) def loadWhenShown(self): """ If True, only load and display the file once the PyDMEmbeddedDisplayWidget is visible on screen. This is very useful if you have many different PyDMEmbeddedWidgets in different tabs of a QTabBar or PyDMTabBar: only the tab that the user is looking at will be loaded, which can greatly speed up the launch time of a display. If this property is changed from 'True' to 'False', and the file has not been loaded yet, it will be loaded immediately. Returns ------- bool """ return self._only_load_when_shown @loadWhenShown.setter def loadWhenShown(self, val): self._only_load_when_shown = val self.load_if_needed() @Property(bool) def disconnectWhenHidden(self): """ Disconnect from PVs when this widget is not visible. Returns ------- bool """ return self._disconnect_when_hidden @disconnectWhenHidden.setter def disconnectWhenHidden(self, disconnect_when_hidden): """ Disconnect from PVs when this widget is not visible. Parameters ---------- disconnect_when_hidden : bool """ self._disconnect_when_hidden = disconnect_when_hidden def showEvent(self, e): """ Show events are sent to widgets that become visible on the screen. Parameters ---------- event : QShowEvent """ if self._only_load_when_shown: w = self.open_file() if w: self.embedded_widget = w if self.disconnectWhenHidden: self.connect() def hideEvent(self, e): """ Hide events are sent to widgets that become invisible on the screen. Parameters ---------- event : QHideEvent """ if self.disconnectWhenHidden: self.disconnect()
class AnimationWindow(PyDialog): """ +-------------------+ | Animation | +-------------------------+ | icase ______ | | scale ______ Default | | time ______ Default | | | | nframes ______ Default | | resolu. ______ Default | | Dir ______ Browse | | iFrame ______ | | | | Animations: | | o Scale, Phase, Time | | | | x delete images | | x repeat | # TODO: change to an integer | x make gif | | | | Step, RunAll | | Close | +-------------------------+ TODO: add key-frame support """ def __init__(self, data, win_parent=None, fringe_cases=None, is_gui_parent=False): PyDialog.__init__(self, data, win_parent) # is the parent the gui? self.is_gui_parent = False self.fringe_cases = fringe_cases self.set_font_size(data['font_size']) self.istep = 0 self._animate_type = 'time' self._updated_animation = False self._active_deformation = 0. self._icase_fringe = data['icase_fringe'] self._icase_disp = data['icase_disp'] self._icase_vector = data['icase_vector'] self._default_title = data['title'] self._default_time = data['time'] self._default_fps = data['frames/sec'] self._default_resolution = data['resolution'] self._scale = data['scale'] self._default_scale = data['default_scale'] self._default_is_scale = data['is_scale'] self._arrow_scale = data['arrow_scale'] self._default_arrow_scale = data['default_arrow_scale'] self._phase = data['phase'] self._default_phase = data['default_phase'] self._default_dirname = data['dirname'] self._default_gif_name = os.path.join(self._default_dirname, data['title'] + '.gif') self.animation_types = [ 'Animate Scale', ] #'Animate Phase', #'Animate Time', #'Animate Frequency Sweep' #] self.setWindowTitle('Animate Model') self.create_widgets() self.create_layout() self.set_connections() self.is_gui = False self.gui = None if hasattr(self.win_parent, '_updated_legend'): self.win_parent.is_animate_open = True self.is_gui = True self.gui = self.win_parent.win_parent if is_gui_parent: self.is_gui = True self.gui = self.win_parent self.on_update_min_max_defaults() def create_widgets(self): """creates the menu objects""" self.box_scale = QGroupBox('Animate Scale') self.box_time = QGroupBox('Animate Time') icase_max = 1000 # TODO: update 1000 self.checkbox_fringe = QCheckBox('Animate') self.checkbox_fringe.setToolTip( 'Animate the fringe in addition to the deflection') #self.checkbox_disp = QCheckBox('Animate') self.checkbox_fringe.setEnabled(False) self.icase_fringe_label = QLabel("iCase (Fringe):") self.icase_fringe_edit = QSpinBox(self) self.icase_fringe_edit.setRange(0, icase_max) self.icase_fringe_edit.setSingleStep(1) if self._icase_fringe is not None: self.icase_fringe_edit.setValue(self._icase_fringe) self.icase_fringe_edit.setToolTip( 'Case Number for the Scale/Phase Animation Type.\n' 'Defaults to the result you had shown when you clicked "Create Animation".\n' 'iCase can be seen by clicking "Apply" on a result.') self.icase_disp_label = QLabel("iCase (Disp):") self.icase_disp_edit = QSpinBox(self) self.icase_disp_edit.setRange(1, icase_max) self.icase_disp_edit.setSingleStep(1) if self._icase_disp is not None: self.icase_disp_edit.setValue(self._icase_disp) self.checkbox_vector = QCheckBox('Animate') self.checkbox_vector.setToolTip( 'Animate the vector in addition to the deflection') self.checkbox_vector.hide() #self.checkbox_disp = QCheckBox('Animate') self.icase_vector_label = QLabel("iCase (Vector):") self.icase_vector_edit = QSpinBox(self) self.icase_vector_edit.setRange(1, icase_max) self.icase_vector_edit.setSingleStep(1) if self._icase_vector is not None: self.icase_vector_edit.setValue(self._icase_vector) self.scale_label = QLabel("True Scale:") self.scale_edit = QLineEdit(str(self._scale)) self.scale_button = QPushButton("Default") self.scale_edit.setToolTip('Scale factor of the "deflection"') self.scale_button.setToolTip('Sets the scale factor of the gif to %s' % self._scale) self.arrow_scale_label = QLabel("Arrow Scale:") self.arrow_scale_edit = QLineEdit(str(self._scale)) self.arrow_scale_button = QPushButton("Default") self.arrow_scale_edit.setToolTip('Scale factor of the "arrows"') self.arrow_scale_button.setToolTip('Sets the arrow scale factor of the gif to %s' % ( self._arrow_scale)) self.arrow_scale_label.setVisible(False) self.arrow_scale_edit.setVisible(False) self.arrow_scale_button.setVisible(False) self.time_label = QLabel("Total Time (sec):") self.time_edit = QDoubleSpinBox(self) self.time_edit.setValue(self._default_time) self.time_edit.setRange(0.1, 5. * 60.) self.time_edit.setDecimals(2) self.time_edit.setSingleStep(0.1) self.time_button = QPushButton("Default") self.time_edit.setToolTip("Total time of the gif") self.time_button.setToolTip('Sets the total time of the gif to %.2f' % self._default_time) self.fps_label = QLabel("Frames/Second:") self.fps_edit = QSpinBox(self) self.fps_edit.setRange(1, 60) self.fps_edit.setSingleStep(1) self.fps_edit.setValue(self._default_fps) self.fps_button = QPushButton("Default") self.fps_edit.setToolTip("A higher FPS is smoother, but may not play well for large gifs") self.fps_button.setToolTip('Sets the FPS to %s' % self._default_fps) self.resolution_label = QLabel("Resolution Scale:") self.resolution_edit = QSpinBox(self) self.resolution_edit.setRange(1, 5) self.resolution_edit.setSingleStep(1) self.resolution_edit.setValue(self._default_resolution) self.resolution_button = QPushButton("Default") self.resolution_edit.setToolTip('Scales the window resolution by an integer factor') self.resolution_button.setToolTip('Sets the resolution to %s' % self._default_resolution) #----------------- # Time plot self.fringe_label = QLabel("Fringe") self.icase_fringe_start_edit = QSpinBox(self) self.icase_fringe_start_edit.setRange(0, icase_max) self.icase_fringe_start_edit.setSingleStep(1) self.icase_fringe_start_edit.setValue(self._icase_fringe) self.icase_fringe_start_button = QPushButton("Default") self.icase_fringe_end_edit = QSpinBox(self) self.icase_fringe_end_edit.setRange(0, icase_max) self.icase_fringe_end_edit.setSingleStep(1) self.icase_fringe_end_edit.setValue(self._icase_fringe) self.icase_fringe_end_button = QPushButton("Default") self.icase_fringe_delta_edit = QSpinBox(self) self.icase_fringe_delta_edit.setRange(1, icase_max) self.icase_fringe_delta_edit.setSingleStep(1) self.icase_fringe_delta_edit.setValue(1) self.icase_fringe_delta_button = QPushButton("Default") self.displacement_label = QLabel("Displacement") self.icase_start = QLabel("iCase Start:") self.icase_disp_start_edit = QSpinBox(self) self.icase_disp_start_edit.setRange(0, icase_max) self.icase_disp_start_edit.setSingleStep(1) self.icase_disp_start_edit.setValue(self._icase_fringe) self.icase_disp_start_button = QPushButton("Default") self.icase_end_label = QLabel("iCase End:") self.icase_disp_end_edit = QSpinBox(self) self.icase_disp_end_edit.setRange(0, icase_max) self.icase_disp_end_edit.setSingleStep(1) self.icase_disp_end_edit.setValue(self._icase_fringe) self.icase_disp_end_button = QPushButton("Default") self.icase_delta_label = QLabel("iCase Delta:") self.icase_disp_delta_edit = QSpinBox(self) self.icase_disp_delta_edit.setRange(1, icase_max) self.icase_disp_delta_edit.setSingleStep(1) self.icase_disp_delta_edit.setValue(1) self.icase_disp_delta_button = QPushButton("Default") self.min_value_enable = QCheckBox() self.min_value_label = QLabel("Min Value:") self.min_value_edit = QLineEdit('') #self.min_value_edit.setRange(1, 1000) #self.min_value_edit.setSingleStep(1) #self.min_value_edit.setValue(1) self.min_value_button = QPushButton("Default") self.max_value_enable = QCheckBox() self.max_value_label = QLabel("Max Value:") self.max_value_edit = QLineEdit('') #self.min_value_edit.setRange(1, 1000) # TODO: update 1000 #self.min_value_edit.setSingleStep(1) #self.min_value_edit.setValue(1) self.max_value_button = QPushButton("Default") # TODO: enable this (uncomment) ------------------------------------------ #self.min_value_enable.hide() #self.min_value.hide() #self.min_value_edit.hide() #self.min_value_button.hide() #self.max_value_enable.hide() #self.max_value.hide() #self.max_value_edit.hide() #self.max_value_button.hide() # TODO: enable this (uncomment) ------------------------------------------ self.icase_disp_start_edit.setToolTip('The first frame of the animation') self.icase_disp_end_edit.setToolTip( 'The last frame of the animation\n' 'Assumes icase_start + nframes * icase_delta = icase_end') self.icase_disp_delta_edit.setToolTip( 'The frame step size (to skip non-consecutive results).\n' 'Frame skipping can be used to:\n' " - skip across results that you don't want to plot\n" ' - adjust the FPS') self.min_value_edit.setToolTip('Min value of the legend (not supported)') self.max_value_edit.setToolTip('Max value of the legend (not supported)') #'time' : 0., #'default_time' : 0, #'icase_start' : 10, #'icase_delta' : 3, #'min_value' : 0., #'max_value' : 1000., self.browse_folder_label = QLabel('Output Directory:') self.browse_folder_edit = QLineEdit(str(self._default_dirname)) self.browse_folder_button = QPushButton('Browse...') self.browse_folder_edit.setToolTip('Location to save the png/gif files') self.gif_label = QLabel("Gif Filename:") self.gif_edit = QLineEdit(str(self._default_title + '.gif')) self.gif_button = QPushButton('Default') self.gif_edit.setToolTip('Name of the gif') self.gif_button.setToolTip('Sets the name of the gif to %s.gif' % self._default_title) # scale / phase if 1: # pragma: no cover self.animate_scale_radio = QRadioButton("Animate Scale") self.animate_phase_radio = QRadioButton("Animate Phase") self.animate_time_radio = QRadioButton("Animate Time") self.animate_freq_sweeep_radio = QRadioButton("Animate Frequency Sweep") self.animate_scale_radio.setToolTip( 'Animates the scale factor based on the "Animation Type"') self.animate_time_radio.setToolTip('Animates the time/load/mode step') self.animate_scale_radio.setChecked(self._default_is_scale) self.animate_phase_radio.setChecked(not self._default_is_scale) self.animate_time_radio.setChecked(False) msg = 'Scale : Animates the scale factor based on the "Animation Profile"\n' if self._default_phase is None: self.animate_phase_radio.setDisabled(True) self.animate_phase_radio.setToolTip('Animates the phase angle ' '(only for complex results)') msg += 'Phase : Animates the phase angle (only for complex results)\n' else: self.animate_phase_radio.setToolTip("Animates the phase angle") msg += 'Phase : Animates the phase angle\n' msg += ( 'Time : Animates the time/load/mode step\n' 'Freq Sweep : Animates a complex result across a range of frequencies ' '(not supported)\n' ) self.animate_freq_sweeep_radio.setDisabled(True) self.animate_freq_sweeep_radio.setToolTip( 'Animates a complex result across a range of frequencies (not supported)') else: msg = 'Scale : Animates the scale factor based on the "Animation Profile"\n' if self._default_phase is None: #self.animate_phase_radio.setDisabled(True) #self.animate_phase_radio.setToolTip('Animates the phase angle ' #'(only for complex results)') msg += 'Phase : Animates the phase angle (only for complex results)\n' else: #self.animate_phase_radio.setToolTip("Animates the phase angle") msg += 'Phase : Animates the phase angle\n' msg += ( 'Time : Animates the time/load/mode step\n' 'Freq Sweep : Animates a complex result across a range of frequencies ' '(not supported)\n' ) self.animation_type = QLabel("Animation Type:") animation_type = OrderedDict() #scale_msg = 'Scale\n' #phase_msg = 'Phase\n' #time_msg = 'Time\n' #animation_types = [ #('Animate Scale', scale_msg), #('Animate Phase', phase_msg), #('Animate Time', time_msg), ##'Animate Frequency Sweep' #] if self._phase is not None: self.animation_types.append('Animate Phase') self.animation_types.append('Animate Time') self.animation_profile_label = QLabel("Animation Profile:") self.animation_profile_edit = QComboBox() for animation_profile in ANIMATION_PROFILES: self.animation_profile_edit.addItem(animation_profile) self.animation_profile_edit.setToolTip('The profile for a scaled GIF') self.animation_type_edit = QComboBox() # TODO: add a tooltip for each item for animation_type in self.animation_types: self.animation_type_edit.addItem(animation_type) #self.animation_type_edit.setToolTip('The profile for a scaled GIF') self.animation_type_edit.setToolTip(msg.rstrip()) self.csv_profile_label = QLabel("CSV profile:") self.csv_profile_edit = QLineEdit() self.csv_profile_browse_button = QPushButton('Browse') self.csv_profile_edit.setToolTip( 'The path to the CSV file of (Scale1, Scale2, Scale3, ...)') #widget = QWidget(self) #horizontal_vertical_group = QButtonGroup(widget) #horizontal_vertical_group.addButton(self.animate_scale_radio) #horizontal_vertical_group.addButton(self.animate_phase_radio) #horizontal_vertical_group.addButton(self.animate_time_radio) #horizontal_vertical_group.addButton(self.animate_freq_sweeep_radio) # animate in gui self.animate_in_gui_checkbox = QCheckBox("Animate In GUI?") self.animate_in_gui_checkbox.setChecked(True) # make images self.make_images_checkbox = QCheckBox("Make images?") self.make_images_checkbox.setChecked(True) # make images self.overwrite_images_checkbox = QCheckBox("Overwrite images?") self.overwrite_images_checkbox.setChecked(True) # delete images when finished self.delete_images_checkbox = QCheckBox("Delete images when finished?") self.delete_images_checkbox.setChecked(True) # endless loop self.repeat_checkbox = QCheckBox("Repeat?") self.repeat_checkbox.setChecked(True) self.repeat_checkbox.setToolTip("Repeating creates an infinitely looping gif") # endless loop self.make_gif_checkbox = QCheckBox("Make Gif?") if IS_IMAGEIO: self.make_gif_checkbox.setChecked(True) else: self.make_gif_checkbox.setChecked(False) self.make_gif_checkbox.setEnabled(False) self.make_gif_checkbox.setToolTip('imageio is not available; install it') # bottom buttons self.step_button = QPushButton("Step") self.wipe_button = QPushButton("Wipe Deformed Shape") self.stop_button = QPushButton("Stop") self.run_button = QPushButton("Run") self.step_button.setToolTip('Steps through the animation (for testing)') self.wipe_button.setToolTip('Removes the existing "deflecton" from the animation') self.stop_button.setToolTip('Stops the animation') self.run_button.setToolTip('Creates the animation') self.step_button.hide() self.wipe_button.hide() self.wipe_button.setEnabled(False) #self.wipe_button.hide() self.stop_button.setEnabled(False) self.cancel_button = QPushButton("Close") #self.set_grid_time(enabled=False) #self.set_grid_scale(enabled=self._default_is_scale) if self._default_phase: self.on_animate_phase(force=True) set_combo_box_text(self.animation_type_edit, 'Animate Phase') else: self.on_animate_scale(force=True) def set_connections(self): """creates the actions for the menu""" self.checkbox_vector.clicked.connect(self.on_checkbox_vector) self.scale_button.clicked.connect(self.on_default_scale) self.arrow_scale_button.clicked.connect(self.on_default_arrow_scale) self.time_button.clicked.connect(self.on_default_time) self.fps_button.clicked.connect(self.on_default_fps) self.resolution_button.clicked.connect(self.on_default_resolution) self.browse_folder_button.clicked.connect(self.on_browse_folder) self.csv_profile_browse_button.clicked.connect(self.on_browse_csv) self.gif_button.clicked.connect(self.on_default_title) self.step_button.clicked.connect(self.on_step) self.wipe_button.clicked.connect(self.on_wipe) self.stop_button.clicked.connect(self.on_stop) self.run_button.clicked.connect(self.on_run) self.min_value_enable.clicked.connect(self.on_min_value_enable) self.max_value_enable.clicked.connect(self.on_max_value_enable) self.min_value_button.clicked.connect(self.on_min_value_default) self.max_value_button.clicked.connect(self.on_max_value_default) self.icase_disp_start_button.clicked.connect(self.on_update_min_max_defaults) #self.animate_scale_radio.clicked.connect(self.on_animate_scale) #self.animate_phase_radio.clicked.connect(self.on_animate_phase) #self.animate_time_radio.clicked.connect(self.on_animate_time) self.animation_type_edit.currentIndexChanged.connect(self.on_animate) #self.animate_freq_sweeep_radio self.cancel_button.clicked.connect(self.on_cancel) self.animate_in_gui_checkbox.clicked.connect(self.on_animate_in_gui) self.animate_in_gui_checkbox.setChecked(True) self.on_animate_in_gui() def on_checkbox_vector(self): is_enabled = self.checkbox_vector.isEnabled() is_checked = self.checkbox_vector.isChecked() enable_edit = is_enabled and is_checked self.icase_vector_label.setEnabled(is_checked) self.icase_vector_edit.setEnabled(enable_edit) def on_animate_in_gui(self): animate_in_gui = self.animate_in_gui_checkbox.isChecked() enable = not animate_in_gui if HIDE_WHEN_INACTIVE: self.make_images_checkbox.setVisible(enable) self.delete_images_checkbox.setVisible(enable) self.make_gif_checkbox.setVisible(enable) self.repeat_checkbox.setVisible(enable) self.resolution_button.setVisible(enable) self.resolution_label.setVisible(enable) self.resolution_edit.setVisible(enable) self.gif_label.setVisible(enable) self.gif_edit.setVisible(enable) self.gif_button.setVisible(enable) self.browse_folder_label.setVisible(enable) self.browse_folder_button.setVisible(enable) self.browse_folder_edit.setVisible(enable) self.step_button.setEnabled(enable) self.make_images_checkbox.setEnabled(enable) self.delete_images_checkbox.setEnabled(enable) self.make_gif_checkbox.setEnabled(enable) self.repeat_checkbox.setEnabled(enable) self.resolution_button.setEnabled(enable) self.resolution_edit.setEnabled(enable) self.gif_edit.setEnabled(enable) self.gif_button.setEnabled(enable) self.browse_folder_button.setEnabled(enable) self.browse_folder_edit.setEnabled(enable) self.step_button.setEnabled(enable) #wipe_button def on_animate(self, value): """ animate pulldown Parameters ---------- value : int index in animation_types """ #animation_types = ['Animate Scale', 'Animate Phase', 'Animate Time', #'Animate Frequency Sweep'] animation_type = self.animation_types[value] if animation_type == 'Animate Scale': self.on_animate_scale() elif animation_type == 'Animate Phase': self.on_animate_phase() elif animation_type == 'Animate Time': self.on_animate_time() else: raise NotImplementedError('value = ', value) def on_animate_time(self, force=False): """enables the secondary input""" #print('on_animate_time') if self._animate_type == 'scale' or force: self.set_grid_scale(False, 'time') self.set_grid_time(True, 'time') self._animate_type = 'time' def on_animate_scale(self, force=False): """enables the secondary input""" #print('on_animate_scale') self.set_grid_scale(True, 'scale') if self._animate_type == 'time' or force: self.set_grid_time(False, 'scale') self._animate_type = 'scale' def on_animate_phase(self, force=False): """enables the secondary input""" #print('on_animate_phase') if self._animate_type == 'scale' or force: self.set_grid_scale(False, 'phase') if self._animate_type == 'time' or force: self.set_grid_time(False, 'phase') self._animate_type = 'phase' def set_grid_scale(self, enabled=True, word=''): """enables/disables the secondary input""" #print('%s-set_grid_scale; enabled = %r' % (word, enabled)) if HIDE_WHEN_INACTIVE: self.box_scale.setVisible(enabled) self.animation_profile_label.setVisible(enabled) self.animation_profile_edit.setVisible(enabled) #self.min_value_enable.setVisible(enabled) #self.max_value_enable.setVisible(enabled) self.animation_profile_label.setEnabled(enabled) self.animation_profile_edit.setEnabled(enabled) # TODO: doesn't work... #self.csv_profile.setEnabled(enabled) #self.csv_profile_edit.setEnabled(enabled) #self.csv_profile_button.setEnabled(enabled) self.min_value_enable.setEnabled(enabled) self.max_value_enable.setEnabled(enabled) self.on_min_value_enable() self.on_max_value_enable() def set_grid_time(self, enabled=True, word=''): """enables/disables the secondary input""" #print('%s-set_grid_time; enabled = %r' % (word, enabled)) if HIDE_WHEN_INACTIVE: self.box_time.setVisible(enabled) self.checkbox_fringe.setVisible(not enabled) self.icase_fringe_label.setVisible(not enabled) self.icase_fringe_edit.setVisible(not enabled) self.icase_disp_label.setVisible(not enabled) self.icase_disp_edit.setVisible(not enabled) self.icase_vector_label.setVisible(not enabled) self.icase_vector_edit.setVisible(not enabled) #self.icase_fringe_delta_edit.setVisible(enabled) self.icase_disp_delta_edit.setVisible(enabled) self.icase_disp_delta_edit.setVisible(enabled) self.fps_label.setVisible(enabled) self.fps_edit.setVisible(enabled) self.fps_button.setVisible(enabled) self.displacement_label.setEnabled(enabled) self.fringe_label.setEnabled(enabled) self.icase_start.setEnabled(enabled) self.icase_disp_start_edit.setEnabled(enabled) self.icase_disp_start_button.setEnabled(enabled) self.icase_fringe_start_edit.setEnabled(enabled) self.icase_fringe_start_button.setEnabled(enabled) self.icase_end_label.setEnabled(enabled) self.icase_disp_end_edit.setEnabled(enabled) self.icase_disp_end_button.setEnabled(enabled) self.icase_fringe_end_edit.setEnabled(enabled) self.icase_fringe_end_button.setEnabled(enabled) self.icase_delta_label.setEnabled(enabled) self.icase_disp_delta_edit.setEnabled(enabled) self.icase_disp_delta_button.setEnabled(enabled) self.icase_fringe_delta_edit.setEnabled(enabled) self.icase_fringe_delta_button.setEnabled(enabled) #----------------------------------------------------------------------- is_min_enabled = self.min_value_enable.isChecked() self.min_value_label.setEnabled(is_min_enabled) self.min_value_edit.setEnabled(is_min_enabled) self.min_value_button.setEnabled(is_min_enabled) is_max_enabled = self.max_value_enable.isChecked() self.max_value_label.setEnabled(is_max_enabled) self.max_value_edit.setEnabled(is_max_enabled) self.max_value_button.setEnabled(is_max_enabled) self.min_value_enable.setEnabled(enabled) self.on_min_value_enable() #self.min_value.setEnabled(enabled) #self.min_value_edit.setEnabled(enabled) #self.min_value_button.setEnabled(enabled) self.max_value_enable.setEnabled(enabled) self.on_max_value_enable() #self.max_value.setEnabled(enabled) #self.max_value_edit.setEnabled(enabled) #self.max_value_button.setEnabled(enabled) self.icase_fringe_label.setEnabled(not enabled) self.icase_fringe_edit.setEnabled(not enabled) self.checkbox_fringe.setEnabled(not enabled) self.icase_disp_label.setEnabled(not enabled) self.icase_disp_edit.setEnabled(not enabled) self.icase_vector_label.setEnabled(not enabled) self.icase_vector_edit.setEnabled(not enabled) self.checkbox_vector.setEnabled(not enabled) self.on_checkbox_vector() self.fps_label.setEnabled(not enabled) self.fps_edit.setEnabled(not enabled) self.fps_button.setEnabled(not enabled) def on_min_value_enable(self): """ The min edit value box is enabled when we switch to time and the box is checked """ is_min_enabled = self.min_value_enable.isChecked() and self.min_value_enable.isEnabled() self.min_value_label.setEnabled(is_min_enabled) self.min_value_edit.setEnabled(is_min_enabled) self.min_value_button.setEnabled(is_min_enabled) def on_max_value_enable(self): """ The max edit value box is enabled when we switch to time and the box is checked """ is_max_enabled = self.max_value_enable.isChecked() and self.max_value_enable.isEnabled() self.max_value_label.setEnabled(is_max_enabled) self.max_value_edit.setEnabled(is_max_enabled) self.max_value_button.setEnabled(is_max_enabled) def on_update_min_max_defaults(self): """ When the icase is changed, the min/max value default message is changed """ icase = self.icase_disp_start_edit.value() min_value, max_value = self.get_min_max(icase) self.min_value_button.setToolTip('Sets the min value to %g' % min_value) self.max_value_button.setToolTip('Sets the max value to %g' % max_value) def on_min_value_default(self): """When min default icase is pressued, update the value""" icase = self.icase_disp_start_edit.value() min_value = self.get_min_max(icase)[0] self.min_value_edit.setText(str(min_value)) self.min_value_edit.setStyleSheet("QLineEdit{background: white;}") def on_max_value_default(self): """When max default icase is pressued, update the value""" icase = self.icase_disp_start_edit.value() max_value = self.get_min_max(icase)[1] self.max_value_edit.setText(str(max_value)) self.max_value_edit.setStyleSheet("QLineEdit{background: white;}") def on_browse_folder(self): """opens a folder dialog""" dirname = getexistingdirectory(parent=self, caption='Select a Directory', basedir='', options=QFileDialog.ShowDirsOnly) if not dirname: return self.browse_folder_edit.setText(dirname) def on_browse_csv(self): """opens a file dialog""" default_filename = '' file_types = 'Delimited Text (*.txt; *.dat; *.csv)' dirname = open_file_dialog(self, 'Select a CSV File', default_filename, file_types) if not dirname: return self.csv_profile_browse_button.setText(dirname) def on_default_title(self): """sets the default gif name""" self.gif_edit.setText(self._default_title + '.gif') def on_default_scale(self): """sets the default displacement scale factor""" if self.is_gui: icase_disp = self.icase_disp_edit.value() out = self.gui.legend_obj.get_legend_disp( icase_disp) unused_scale, unused_phase, default_scale, unused_default_phase = out else: default_scale = self._default_scale self.scale_edit.setText(str(default_scale)) self.scale_edit.setStyleSheet("QLineEdit{background: white;}") def on_default_arrow_scale(self): """sets the default arrow scale factor""" if self.is_gui: icase_vector = self.icase_vector_edit.value() out = self.gui.legend_obj.get_legend_vector(icase_vector) unused_arrow_scale, default_arrow_scale = out else: default_arrow_scale = self._default_arrow_scale self.arrow_scale_edit.setText(str(default_arrow_scale)) self.arrow_scale_edit.setStyleSheet("QLineEdit{background: white;}") def on_default_time(self): """sets the default gif time""" self.time_edit.setValue(self._default_time) def on_default_fps(self): """sets the default FPS""" self.fps_edit.setValue(self._default_fps) def on_default_resolution(self): """sets the default image resolution scale factor""" self.resolution_edit.setValue(self._default_resolution) def create_layout(self): """displays the menu objects""" grid = QGridLayout() irow = 0 grid.addWidget(self.icase_fringe_label, irow, 0) grid.addWidget(self.icase_fringe_edit, irow, 1) grid.addWidget(self.checkbox_fringe, irow, 2) irow += 1 grid.addWidget(self.icase_disp_label, irow, 0) grid.addWidget(self.icase_disp_edit, irow, 1) #grid.addWidget(self.checkbox_disp, irow, 2) irow += 1 grid.addWidget(self.icase_vector_label, irow, 0) grid.addWidget(self.icase_vector_edit, irow, 1) grid.addWidget(self.checkbox_vector, irow, 2) irow += 1 grid.addWidget(self.scale_label, irow, 0) grid.addWidget(self.scale_edit, irow, 1) grid.addWidget(self.scale_button, irow, 2) irow += 1 grid.addWidget(self.arrow_scale_label, irow, 0) grid.addWidget(self.arrow_scale_edit, irow, 1) grid.addWidget(self.arrow_scale_button, irow, 2) irow += 1 grid.addWidget(self.time_label, irow, 0) grid.addWidget(self.time_edit, irow, 1) grid.addWidget(self.time_button, irow, 2) irow += 1 # spacer spacer = QLabel('') grid.addWidget(self.fps_label, irow, 0) grid.addWidget(self.fps_edit, irow, 1) grid.addWidget(self.fps_button, irow, 2) irow += 1 grid.addWidget(self.animation_type, irow, 0) grid.addWidget(self.animation_type_edit, irow, 1) irow += 1 grid.addWidget(spacer, irow, 0) irow += 1 #---------- #Time grid_time = QGridLayout() jrow = 0 self.fringe_label.setAlignment(Qt.AlignCenter) self.displacement_label.setAlignment(Qt.AlignCenter) if not IS_TIME_FRINGE: self.fringe_label.hide() self.icase_fringe_delta_edit.hide() self.icase_fringe_start_edit.hide() self.icase_fringe_end_edit.hide() self.icase_fringe_delta_button.hide() grid_time.addWidget(self.displacement_label, jrow, 1) grid_time.addWidget(self.fringe_label, jrow, 2) jrow += 1 grid_time.addWidget(self.icase_start, jrow, 0) grid_time.addWidget(self.icase_disp_start_edit, jrow, 1) grid_time.addWidget(self.icase_fringe_start_edit, jrow, 2) #grid_time.addWidget(self.icase_disp_start_button, jrow, 2) jrow += 1 grid_time.addWidget(self.icase_end_label, jrow, 0) grid_time.addWidget(self.icase_disp_end_edit, jrow, 1) grid_time.addWidget(self.icase_fringe_end_edit, jrow, 2) #grid_time.addWidget(self.icase_end_button, jrow, 2) jrow += 1 grid_time.addWidget(self.icase_delta_label, jrow, 0) grid_time.addWidget(self.icase_disp_delta_edit, jrow, 1) grid_time.addWidget(self.icase_fringe_delta_edit, jrow, 2) #grid_time.addWidget(self.icase_delta_button, jrow, 2) jrow += 1 hbox_min = QHBoxLayout() hbox_min.addWidget(self.min_value_enable) hbox_min.addWidget(self.min_value_label) grid_time.addLayout(hbox_min, jrow, 0) grid_time.addWidget(self.min_value_edit, jrow, 1) grid_time.addWidget(self.min_value_button, jrow, 2) jrow += 1 hbox_max = QHBoxLayout() hbox_max.addWidget(self.max_value_enable) hbox_max.addWidget(self.max_value_label) grid_time.addLayout(hbox_max, jrow, 0) grid_time.addWidget(self.max_value_edit, jrow, 1) grid_time.addWidget(self.max_value_button, jrow, 2) jrow += 1 grid_time.addWidget(spacer, jrow, 0) jrow += 1 #-------------- grid_scale = QGridLayout() grid_scale.addWidget(self.animation_profile_label, 0, 0) grid_scale.addWidget(self.animation_profile_edit, 0, 1) #grid_scale.addWidget(self.csv_profile, 1, 0) #grid_scale.addWidget(self.csv_profile_edit, 1, 1) #grid_scale.addWidget(self.csv_profile_browse_button, 1, 2) self.csv_profile = QLabel("CSV profile:") self.csv_profile_edit = QLineEdit() self.csv_profile_button = QPushButton('Browse') #box_time = QVBoxLayout() # TODO: It's super annoying that the animate time box doesn't # line up with the previous box self.box_scale.setLayout(grid_scale) self.box_time.setLayout(grid_time) #---------- grid2 = QGridLayout() irow = 0 #grid2.addWidget(self.animate_scale_radio, 8, 0) #grid2.addWidget(self.animate_phase_radio, 8, 1) #grid2.addWidget(self.animate_time_radio, 8, 2) #grid2.addWidget(self.animate_freq_sweeep_radio, 8, 3) grid2.addWidget(self.animate_in_gui_checkbox, irow, 0) irow += 1 grid2.addWidget(self.resolution_label, irow, 0) grid2.addWidget(self.resolution_edit, irow, 1) grid2.addWidget(self.resolution_button, irow, 2) irow += 1 grid2.addWidget(self.browse_folder_label, irow, 0) grid2.addWidget(self.browse_folder_edit, irow, 1) grid2.addWidget(self.browse_folder_button, irow, 2) irow += 1 grid2.addWidget(self.gif_label, irow, 0) grid2.addWidget(self.gif_edit, irow, 1) grid2.addWidget(self.gif_button, irow, 2) irow += 1 grid2.addWidget(self.make_images_checkbox, irow, 0) #grid2.addWidget(self.overwrite_images_checkbox, irow, 0) grid2.addWidget(self.delete_images_checkbox, irow, 1) grid2.addWidget(self.make_gif_checkbox, irow, 2) irow += 1 grid2.addWidget(self.repeat_checkbox, irow, 0) irow += 1 grid2.addWidget(spacer, irow, 0) grid_hbox = QHBoxLayout() grid_hbox.addWidget(spacer) grid_hbox.addLayout(grid2) grid_hbox.addWidget(spacer) # bottom buttons step_run_box = QHBoxLayout() step_run_box.addWidget(self.step_button) step_run_box.addWidget(self.wipe_button) step_run_box.addWidget(self.stop_button) step_run_box.addWidget(self.run_button) ok_cancel_box = QHBoxLayout() ok_cancel_box.addWidget(self.cancel_button) vbox = QVBoxLayout() vbox.addLayout(grid) vbox.addWidget(self.box_scale) vbox.addWidget(self.box_time) #vbox.addLayout(checkboxes) vbox.addLayout(grid_hbox) vbox.addStretch() vbox.addLayout(step_run_box) vbox.addLayout(ok_cancel_box) if IS_RESULTS_SELECTOR and self.fringe_cases: cases = get_cases_from_tree(self.fringe_cases) parent = self name = 'main' data = self.fringe_cases choices = cases results_widget = ResultsWindow(parent, name, data, choices, include_clear=False, include_delete=False) vbox_results = QVBoxLayout() results_widget_label = QLabel('Results:') vbox_results.addWidget(results_widget_label) vbox_results.addWidget(results_widget) hbox_main = QHBoxLayout() hbox_main.addLayout(vbox) hbox_main.addLayout(vbox_results) self.setLayout(hbox_main) else: self.setLayout(vbox) def on_fringe(self, icase): """sets the icase fringe""" self.icase_fringe_edit.setValue(icase) def on_disp(self, icase): """sets the icase disp""" self.icase_disp_edit.setValue(icase) def on_vector(self, icase): """sets the icase vector""" self.icase_vector_edit.setValue(icase) def on_clear_results(self): """sink for the right click menu""" pass def on_step(self): """click the Step button""" passed, validate_out = self.on_validate() if passed: try: self._make_gif(validate_out, istep=self.istep) self.istep += 1 except IndexError: self._make_gif(validate_out, istep=0) self.istep += 1 self.wipe_button.setEnabled(True) def on_wipe(self): """click the Wipe button""" passed, validate_out = self.on_validate(wipe=True) if passed: self.istep = 0 self._make_gif(validate_out, istep=self.istep) self.wipe_button.setEnabled(False) self.stop_button.setEnabled(False) def on_stop(self): """click the Stop button""" #passed, validate_out = self.on_validate() #if passed: #self._make_gif(validate_out, stop_animation=True) if self.is_gui: self.gui.stop_animation() self.wipe_button.setEnabled(True) self.stop_button.setEnabled(False) def on_run(self): """click the Run button""" self.istep = 0 self.wipe_button.setEnabled(False) self.stop_button.setEnabled(True) passed, validate_out = self.on_validate() if passed: self._make_gif(validate_out, istep=None) return passed def _make_gif(self, validate_out, istep=None, stop_animation=False): """interface for making the gif""" (icase_fringe, icase_disp, icase_vector, scale, time, fps, animate_in_gui, magnify, output_dir, gifbase, min_value, max_value) = validate_out fps = int(fps) gif_filename = None if not stop_animation and not animate_in_gui and gifbase is not None: if gifbase.lower().endswith('.gif'): gifbase = gifbase[:-4] gif_filename = os.path.join(output_dir, gifbase + '.gif') animate_fringe = self.checkbox_fringe.isChecked() animate_vector = self.checkbox_vector.isChecked() animate_scale = self.animate_scale_radio.isChecked() animate_phase = self.animate_phase_radio.isChecked() animate_time = self.animate_time_radio.isChecked() if not self.checkbox_vector.isEnabled(): icase_vector = None animate_scale = False animate_phase = False animate_time = False if self._animate_type == 'scale': animate_scale = True elif self._animate_type == 'phase': animate_phase = True elif self._animate_type == 'time': animate_time = True else: raise NotImplementedError(self._animate_type) make_images = self.make_images_checkbox.isChecked() delete_images = self.delete_images_checkbox.isChecked() make_gif = self.make_gif_checkbox.isChecked() animation_profile = str(self.animation_profile_edit.currentText()) icase_disp_start = self.icase_disp_start_edit.value() icase_disp_end = self.icase_disp_end_edit.value() icase_disp_delta = self.icase_disp_delta_edit.value() bool_repeat = self.repeat_checkbox.isChecked() # TODO: change this to an integer if bool_repeat: nrepeat = 0 else: nrepeat = 1 #self.out_data['is_shown'] = self.show_radio.isChecked() #icase = self._icase if self.is_gui: self.gui.make_gif( gif_filename, scale, istep=istep, animate_scale=animate_scale, animate_phase=animate_phase, animate_time=animate_time, icase_fringe=icase_fringe, icase_disp=icase_disp, icase_vector=icase_vector, animate_fringe=animate_fringe, animate_vector=animate_vector, icase_start=icase_disp_start, icase_end=icase_disp_end, icase_delta=icase_disp_delta, time=time, animation_profile=animation_profile, nrepeat=nrepeat, fps=fps, magnify=magnify, make_images=make_images, delete_images=delete_images, make_gif=make_gif, stop_animation=stop_animation, animate_in_gui=animate_in_gui, min_value=min_value, max_value=max_value, ) self.out_data['clicked_ok'] = True self.out_data['close'] = True def get_min_max(self, icase): if self.is_gui: (obj, (i, name)) = self.gui.result_cases[icase] min_value, max_value = obj.get_min_max(i, name) else: return 0., 1.0 return min_value, max_value def on_validate(self, wipe=False): """checks to see if the input is valid""" # requires no special validation icase_fringe, flag0 = check_int(self.icase_fringe_edit) icase_disp, unused_flaga = check_int(self.icase_disp_edit) icase_vector, unused_flagb = check_int(self.icase_vector_edit) #icase_disp = self._icase_disp #icase_vector = self._icase_vector scale, flag1 = check_float(self.scale_edit) time, flag2 = check_float(self.time_edit) fps, flag3 = check_float(self.fps_edit) min_value = max_value = None flag4 = flag5 = True if self.min_value_edit.isEnabled(): min_value, flag4 = check_float(self.min_value_edit) if self.max_value_edit.isEnabled(): max_value, flag5 = check_float(self.max_value_edit) if wipe: animate_in_gui = False scale = 0. flag1 = True else: animate_in_gui = self.animate_in_gui_checkbox.isChecked() if scale == 0.0: self.scale_edit.setStyleSheet("QLineEdit{background: red;}") flag1 = False if animate_in_gui or wipe: passed = all([flag0, flag1, flag2, flag3, flag4, flag5]) magnify, output_dir, gifbase = None, None, None else: magnify, flag6 = check_int(self.resolution_edit) output_dir, flag7 = check_path(self.browse_folder_edit) gifbase, flag8 = check_name_str(self.gif_edit) passed = all([flag0, flag1, flag2, flag3, flag4, flag5, flag6, flag7, flag8]) return passed, (icase_fringe, icase_disp, icase_vector, scale, time, fps, animate_in_gui, magnify, output_dir, gifbase, min_value, max_value) #def on_ok(self): #"""click the OK button""" #passed = self.on_apply() #if passed: #self.win_parent._animation_window_shown = False #self.close() ##self.destroy() def on_cancel(self): """click the Cancel button""" self.on_stop() self.out_data['close'] = True self.close()
class ProjectDialog(QDialog): """Project creation dialog.""" sig_project_creation_requested = Signal(str, str, object) """ This signal is emitted to request the Projects plugin the creation of a project. Parameters ---------- project_path: str Location of project. project_type: str Type of project as defined by project types. project_packages: object Package to install. Currently not in use. """ def __init__(self, parent, project_types): """Project creation dialog.""" super(ProjectDialog, self).__init__(parent=parent) self.plugin = parent self._project_types = project_types self.project_data = {} self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint) self.project_name = None self.location = get_home_dir() # Widgets projects_url = "http://docs.spyder-ide.org/current/panes/projects.html" self.description_label = QLabel( _("Select a new or existing directory to create a new Spyder " "project in it. To learn more about projects, take a look at " "our <a href=\"{0}\">documentation</a>.").format(projects_url)) self.description_label.setOpenExternalLinks(True) self.description_label.setWordWrap(True) self.groupbox = QGroupBox() self.radio_new_dir = QRadioButton(_("New directory")) self.radio_from_dir = QRadioButton(_("Existing directory")) self.label_project_name = QLabel(_('Project name')) self.label_location = QLabel(_('Location')) self.label_project_type = QLabel(_('Project type')) self.text_project_name = QLineEdit() self.text_location = QLineEdit(get_home_dir()) self.combo_project_type = QComboBox() self.label_information = QLabel("") self.label_information.hide() self.button_select_location = create_toolbutton( self, triggered=self.select_location, icon=ima.icon('DirOpenIcon'), tip=_("Select directory")) self.button_cancel = QPushButton(_('Cancel')) self.button_create = QPushButton(_('Create')) self.bbox = QDialogButtonBox(Qt.Horizontal) self.bbox.addButton(self.button_cancel, QDialogButtonBox.ActionRole) self.bbox.addButton(self.button_create, QDialogButtonBox.ActionRole) # Widget setup self.radio_new_dir.setChecked(True) self.text_location.setEnabled(True) self.text_location.setReadOnly(True) self.button_cancel.setDefault(True) self.button_cancel.setAutoDefault(True) self.button_create.setEnabled(False) for (id_, name) in [(pt_id, pt.get_name()) for pt_id, pt in project_types.items()]: self.combo_project_type.addItem(name, id_) self.setWindowTitle(_('Create new project')) # Layouts layout_top = QHBoxLayout() layout_top.addWidget(self.radio_new_dir) layout_top.addSpacing(15) layout_top.addWidget(self.radio_from_dir) layout_top.addSpacing(200) self.groupbox.setLayout(layout_top) layout_grid = QGridLayout() layout_grid.addWidget(self.label_project_name, 0, 0) layout_grid.addWidget(self.text_project_name, 0, 1, 1, 2) layout_grid.addWidget(self.label_location, 1, 0) layout_grid.addWidget(self.text_location, 1, 1) layout_grid.addWidget(self.button_select_location, 1, 2) layout_grid.addWidget(self.label_project_type, 2, 0) layout_grid.addWidget(self.combo_project_type, 2, 1, 1, 2) layout_grid.addWidget(self.label_information, 3, 0, 1, 3) layout = QVBoxLayout() layout.addWidget(self.description_label) layout.addSpacing(3) layout.addWidget(self.groupbox) layout.addSpacing(8) layout.addLayout(layout_grid) layout.addSpacing(8) layout.addWidget(self.bbox) layout.setSizeConstraint(layout.SetFixedSize) self.setLayout(layout) # Signals and slots self.button_create.clicked.connect(self.create_project) self.button_cancel.clicked.connect(self.close) self.radio_from_dir.clicked.connect(self.update_location) self.radio_new_dir.clicked.connect(self.update_location) self.text_project_name.textChanged.connect(self.update_location) def select_location(self): """Select directory.""" location = osp.normpath( getexistingdirectory( self, _("Select directory"), self.location, )) if location and location != '.': if is_writable(location): self.location = location self.text_project_name.setText(osp.basename(location)) self.update_location() def update_location(self, text=''): """Update text of location and validate it.""" msg = '' path_validation = False path = self.location name = self.text_project_name.text().strip() # Setup self.text_project_name.setEnabled(self.radio_new_dir.isChecked()) self.label_information.setText('') self.label_information.hide() if name and self.radio_new_dir.isChecked(): # Allow to create projects only on new directories. path = osp.join(self.location, name) path_validation = not osp.isdir(path) if not path_validation: msg = _("This directory already exists!") elif self.radio_from_dir.isChecked(): # Allow to create projects in current directories that are not # Spyder projects. path = self.location path_validation = not osp.isdir(osp.join(path, '.spyproject')) if not path_validation: msg = _("This directory is already a Spyder project!") # Set path in text_location self.text_location.setText(path) # Validate project name with the method from the currently selected # project. project_type_id = self.combo_project_type.currentData() validate_func = self._project_types[project_type_id].validate_name project_name_validation, project_msg = validate_func(path, name) if not project_name_validation: if msg: msg = msg + '\n\n' + project_msg else: msg = project_msg # Set message if msg: self.label_information.show() self.label_information.setText('\n' + msg) # Allow to create project if validation was successful validated = path_validation and project_name_validation self.button_create.setEnabled(validated) # Set default state of buttons according to validation # Fixes spyder-ide/spyder#16745 if validated: self.button_create.setDefault(True) self.button_create.setAutoDefault(True) else: self.button_cancel.setDefault(True) self.button_cancel.setAutoDefault(True) def create_project(self): """Create project.""" self.project_data = { "root_path": self.text_location.text(), "project_type": self.combo_project_type.currentData(), } self.sig_project_creation_requested.emit( self.text_location.text(), self.combo_project_type.currentData(), [], ) self.accept()
class PyDMEmbeddedDisplay(QFrame, PyDMPrimitiveWidget): """ A QFrame capable of rendering a PyDM Display Parameters ---------- parent : QWidget The parent widget for the Label """ def __init__(self, parent=None): QFrame.__init__(self, parent) PyDMPrimitiveWidget.__init__(self) self.app = QApplication.instance() self._filename = None self._macros = None self._embedded_widget = None self._disconnect_when_hidden = True self._is_connected = False self.layout = QVBoxLayout(self) self.err_label = QLabel(self) self.err_label.setAlignment(Qt.AlignHCenter) self.layout.addWidget(self.err_label) self.layout.setContentsMargins(0, 0, 0, 0) self.err_label.hide() if not is_pydm_app(): self.setFrameShape(QFrame.Box) else: self.setFrameShape(QFrame.NoFrame) def minimumSizeHint(self): """ This property holds the recommended minimum size for the widget. Returns ------- QSize """ # This is totally arbitrary, I just want *some* visible nonzero size return QSize(100, 100) @Property(str) def macros(self): """ JSON-formatted string containing macro variables to pass to the embedded file. Returns ------- str """ if self._macros is None: return "" return self._macros @macros.setter def macros(self, new_macros): """ JSON-formatted string containing macro variables to pass to the embedded file. .. warning:: If the macros property is not defined before the filename property, The widget will not have any macros defined when it loads the embedded file. This behavior will be fixed soon. Parameters ---------- new_macros : str """ self._macros = str(new_macros) @Property(str) def filename(self): """ Filename of the display to embed. Returns ------- str """ if self._filename is None: return "" return self._filename @filename.setter def filename(self, filename): """ Filename of the display to embed. Parameters ---------- filename : str """ filename = str(filename) if filename != self._filename: self._filename = filename # If we aren't in a PyDMApplication (usually that means we are in Qt Designer), # don't try to load the file, just show text with the filename. if not is_pydm_app(): self.err_label.setText(self._filename) self.err_label.show() return try: self.embedded_widget = self.open_file() except ValueError as e: self.err_label.setText( "Could not parse macro string.\nError: {}".format(e)) self.err_label.show() except IOError as e: self.err_label.setText( "Could not open {filename}.\nError: {err}".format( filename=self._filename, err=e)) self.err_label.show() def parsed_macros(self): """ Dictionary containing the key value pair for each macro specified. Returns -------- dict """ return parse_macro_string(self.macros) def open_file(self): """ Opens the widget specified in the widget's filename property. Returns ------- display : QWidget """ # Expand user (~ or ~user) and environment variables. fname = os.path.expanduser(os.path.expandvars(self.filename)) if os.path.isabs(fname): return self.app.open_file(fname, macros=self.parsed_macros()) else: return self.app.open_relative(fname, self, macros=self.parsed_macros()) @property def embedded_widget(self): """ The embedded widget being displayed. Returns ------- QWidget """ return self._embedded_widget @embedded_widget.setter def embedded_widget(self, new_widget): """ Defines the embedded widget to display inside the QFrame Parameters ---------- new_widget : QWidget """ should_reconnect = False if new_widget is self._embedded_widget: return if self._embedded_widget is not None: self.layout.removeWidget(self._embedded_widget) self._embedded_widget.deleteLater() self._embedded_widget = None self._embedded_widget = new_widget self._embedded_widget.setParent(self) self.layout.addWidget(self._embedded_widget) self.err_label.hide() self._embedded_widget.show() self._is_connected = True def connect(self): """ Establish the connection between the embedded widget widgets and the channels associated with it. """ if self._is_connected or self.embedded_widget is None: return establish_widget_connections(self.embedded_widget) def disconnect(self): """ Disconnects the embedded widget widgets from the channels associated with it. """ if not self._is_connected or self.embedded_widget is None: return close_widget_connections(self.embedded_widget) @Property(bool) def disconnectWhenHidden(self): """ Disconnect from PVs when this widget is not visible. Returns ------- bool """ return self._disconnect_when_hidden @disconnectWhenHidden.setter def disconnectWhenHidden(self, disconnect_when_hidden): """ Disconnect from PVs when this widget is not visible. Parameters ---------- disconnect_when_hidden : bool """ self._disconnect_when_hidden = disconnect_when_hidden def showEvent(self, e): """ Show events are sent to widgets that become visible on the screen. Parameters ---------- event : QShowEvent """ if self.disconnectWhenHidden: self.connect() def hideEvent(self, e): """ Hide events are sent to widgets that become invisible on the screen. Parameters ---------- event : QHideEvent """ if self.disconnectWhenHidden: self.disconnect()
class QtPluginDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) self.installer = Installer() self.setup_ui() self.installer.set_output_widget(self.stdout_text) self.installer.process.started.connect(self._on_installer_start) self.installer.process.finished.connect(self._on_installer_done) self.refresh() def _on_installer_start(self): self.show_status_btn.setChecked(True) self.working_indicator.show() self.process_error_indicator.hide() def _on_installer_done(self, exit_code, exit_status): self.working_indicator.hide() if exit_code: self.process_error_indicator.show() else: self.show_status_btn.setChecked(False) self.refresh() self.plugin_sorter.refresh() def refresh(self): self.installed_list.clear() self.available_list.clear() # fetch installed from ...plugins import plugin_manager plugin_manager.discover() # since they might not be loaded yet already_installed = set() for plugin_name, mod_name, distname in plugin_manager.iter_available(): # not showing these in the plugin dialog if plugin_name in ('napari_plugin_engine', ): continue if distname: already_installed.add(distname) meta = standard_metadata(distname) else: meta = {} self.installed_list.addItem( ProjectInfo( normalized_name(distname or ''), meta.get('version', ''), meta.get('url', ''), meta.get('summary', ''), meta.get('author', ''), meta.get('license', ''), ), plugin_name=plugin_name, enabled=plugin_name in plugin_manager.plugins, ) # self.v_splitter.setSizes([70 * self.installed_list.count(), 10, 10]) # fetch available plugins self.worker = create_worker(iter_napari_plugin_info) def _handle_yield(project_info): if project_info.name in already_installed: self.installed_list.tag_outdated(project_info) else: self.available_list.addItem(project_info) self.worker.yielded.connect(_handle_yield) self.worker.finished.connect(self.working_indicator.hide) self.worker.start() def setup_ui(self): self.resize(1080, 640) vlay_1 = QVBoxLayout(self) self.h_splitter = QSplitter(self) vlay_1.addWidget(self.h_splitter) self.h_splitter.setOrientation(Qt.Horizontal) self.v_splitter = QSplitter(self.h_splitter) self.v_splitter.setOrientation(Qt.Vertical) self.v_splitter.setMinimumWidth(500) self.plugin_sorter = QtPluginSorter(parent=self.h_splitter) self.plugin_sorter.layout().setContentsMargins(2, 0, 0, 0) self.plugin_sorter.hide() installed = QWidget(self.v_splitter) lay = QVBoxLayout(installed) lay.setContentsMargins(0, 2, 0, 2) lay.addWidget(QLabel("Installed Plugins")) self.installed_list = QPluginList(installed, self.installer) lay.addWidget(self.installed_list) uninstalled = QWidget(self.v_splitter) lay = QVBoxLayout(uninstalled) lay.setContentsMargins(0, 2, 0, 2) lay.addWidget(QLabel("Available Plugin Packages")) self.available_list = QPluginList(uninstalled, self.installer) lay.addWidget(self.available_list) self.stdout_text = QTextEdit(self.v_splitter) self.stdout_text.setReadOnly(True) self.stdout_text.setObjectName("pip_install_status") self.stdout_text.hide() buttonBox = QHBoxLayout() self.working_indicator = QLabel("loading ...", self) self.process_error_indicator = QLabel(self) self.process_error_indicator.setObjectName("error_label") self.process_error_indicator.hide() load_gif = str(Path(napari.resources.__file__).parent / "loading.gif") mov = QMovie(load_gif) mov.setScaledSize(QSize(18, 18)) self.working_indicator.setMovie(mov) mov.start() self.show_status_btn = QPushButton("Show Status", self) self.show_status_btn.setFixedWidth(100) self.show_sorter_btn = QPushButton("<< Show Sorter", self) self.close_btn = QPushButton("Close", self) self.close_btn.clicked.connect(self.reject) buttonBox.addWidget(self.show_status_btn) buttonBox.addWidget(self.working_indicator) buttonBox.addWidget(self.process_error_indicator) buttonBox.addStretch() buttonBox.addWidget(self.show_sorter_btn) buttonBox.addWidget(self.close_btn) buttonBox.setContentsMargins(0, 0, 4, 0) vlay_1.addLayout(buttonBox) self.show_status_btn.setCheckable(True) self.show_status_btn.setChecked(False) self.show_status_btn.toggled.connect(self._toggle_status) self.show_sorter_btn.setCheckable(True) self.show_sorter_btn.setChecked(False) self.show_sorter_btn.toggled.connect(self._toggle_sorter) self.v_splitter.setStretchFactor(1, 2) self.h_splitter.setStretchFactor(0, 2) def _toggle_sorter(self, show): if show: self.show_sorter_btn.setText(">> Hide Sorter") self.plugin_sorter.show() else: self.show_sorter_btn.setText("<< Show Sorter") self.plugin_sorter.hide() def _toggle_status(self, show): if show: self.show_status_btn.setText("Hide Status") self.stdout_text.show() else: self.show_status_btn.setText("Show Status") self.stdout_text.hide()
class PyDMSlider(QFrame, TextFormatter, PyDMWritableWidget): """ A QSlider with support for Channels and more from PyDM. Parameters ---------- parent : QWidget The parent widget for the Label init_channel : str, optional The channel to be used by the widget. """ actionTriggered = Signal(int) rangeChanged = Signal(float, float) sliderMoved = Signal(float) sliderPressed = Signal() sliderReleased = Signal() valueChanged = Signal(float) def __init__(self, parent=None, init_channel=None): QFrame.__init__(self, parent) PyDMWritableWidget.__init__(self, init_channel=init_channel) self.alarmSensitiveContent = True self.alarmSensitiveBorder = False # Internal values for properties self._show_limit_labels = True self._show_value_label = True self._user_defined_limits = False self._needs_limit_info = True self._minimum = None self._maximum = None self._user_minimum = -10.0 self._user_maximum = 10.0 self._num_steps = 101 self._orientation = Qt.Horizontal # Set up all the internal widgets that make up a PyDMSlider. # We'll add all these things to layouts when we call setup_widgets_for_orientation label_size_policy = QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Fixed) self.low_lim_label = QLabel(self) self.low_lim_label.setObjectName("lowLimLabel") self.low_lim_label.setSizePolicy(label_size_policy) self.low_lim_label.setAlignment(Qt.AlignLeft | Qt.AlignTrailing | Qt.AlignVCenter) self.value_label = QLabel(self) self.value_label.setObjectName("valueLabel") self.value_label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.high_lim_label = QLabel(self) self.high_lim_label.setObjectName("highLimLabel") self.high_lim_label.setSizePolicy(label_size_policy) self.high_lim_label.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter) self._slider = QSlider(parent=self) self._slider.setOrientation(Qt.Horizontal) self._slider.sliderMoved.connect(self.internal_slider_moved) self._slider.sliderPressed.connect(self.internal_slider_pressed) self._slider.sliderReleased.connect(self.internal_slider_released) self._slider.valueChanged.connect(self.internal_slider_value_changed) # self.vertical_layout.addWidget(self._slider) # Other internal variables and final setup steps self._slider_position_to_value_map = None self._mute_internal_slider_changes = False self.setup_widgets_for_orientation(self._orientation) self.reset_slider_limits() def init_for_designer(self): """ Method called after the constructor to tweak configurations for when using the widget with the Qt Designer """ self.value = 0.0 @Property(Qt.Orientation) def orientation(self): """ The slider orientation (Horizontal or Vertical) Returns ------- int Qt.Horizontal or Qt.Vertical """ return self._orientation @orientation.setter def orientation(self, new_orientation): """ The slider orientation (Horizontal or Vertical) Parameters ---------- new_orientation : int Qt.Horizontal or Qt.Vertical """ self._orientation = new_orientation self.setup_widgets_for_orientation(new_orientation) def setup_widgets_for_orientation(self, new_orientation): """ Reconstruct the widget given the orientation. Parameters ---------- new_orientation : int Qt.Horizontal or Qt.Vertical """ if new_orientation not in (Qt.Horizontal, Qt.Vertical): logger.error("Invalid orientation '{0}'. The existing layout will not change.".format(new_orientation)) return layout = None if new_orientation == Qt.Horizontal: layout = QVBoxLayout() layout.setContentsMargins(4, 0, 4, 4) label_layout = QHBoxLayout() label_layout.addWidget(self.low_lim_label) label_layout.addStretch(0) label_layout.addWidget(self.value_label) label_layout.addStretch(0) label_layout.addWidget(self.high_lim_label) layout.addLayout(label_layout) self._slider.setOrientation(new_orientation) layout.addWidget(self._slider) elif new_orientation == Qt.Vertical: layout = QHBoxLayout() layout.setContentsMargins(0, 4, 4, 4) label_layout = QVBoxLayout() label_layout.addWidget(self.high_lim_label) label_layout.addStretch(0) label_layout.addWidget(self.value_label) label_layout.addStretch(0) label_layout.addWidget(self.low_lim_label) layout.addLayout(label_layout) self._slider.setOrientation(new_orientation) layout.addWidget(self._slider) if self.layout() is not None: # Trick to remove the existing layout by re-parenting it in an empty widget. QWidget().setLayout(self.layout()) self.setLayout(layout) def update_labels(self): """ Update the limits and value labels with the correct values. """ def set_label(value, label_widget): if value is None: label_widget.setText("") else: label_widget.setText(self.format_string.format(value)) set_label(self.minimum, self.low_lim_label) set_label(self.maximum, self.high_lim_label) set_label(self.value, self.value_label) def reset_slider_limits(self): """ Reset the limits and adjust the labels properly for the slider. """ if self.minimum is None or self.maximum is None: self._needs_limit_info = True self.set_enable_state() return self._needs_limit_info = False self._slider.setMinimum(0) self._slider.setMaximum(self._num_steps - 1) self._slider.setSingleStep(1) self._slider.setPageStep(10) self._slider_position_to_value_map = np.linspace(self.minimum, self.maximum, num=self._num_steps) self.update_labels() self.set_slider_to_closest_value(self.value) self.rangeChanged.emit(self.minimum, self.maximum) self.set_enable_state() def find_closest_slider_position_to_value(self, val): """ Find and returns the index for the closest position on the slider for a given value. Parameters ---------- val : float Returns ------- int """ diff = abs(self._slider_position_to_value_map - float(val)) return np.argmin(diff) def set_slider_to_closest_value(self, val): """ Set the value for the slider according to a given value. Parameters ---------- val : float """ if val is None or self._needs_limit_info: return # When we set the slider to the closest value, it may end up at a slightly # different position than val (if val is not in self._slider_position_to_value_map) # We don't want that slight difference to get broacast out and put the channel # somewhere new. For example, if the slider can only be at 0.4 or 0.5, but a # new value comes in of 0.45, its more important to keep the 0.45 than to change # it to where the slider gets set. Therefore, we mute the internal slider changes # so that its valueChanged signal doesn't cause us to emit a signal to PyDM to change # the value of the channel. self._mute_internal_slider_changes = True self._slider.setValue(self.find_closest_slider_position_to_value(val)) self._mute_internal_slider_changes = False def value_changed(self, new_val): """ Callback invoked when the Channel value is changed. Parameters ---------- new_val : int or float The new value from the channel. """ PyDMWritableWidget.value_changed(self, new_val) if hasattr(self, "value_label"): self.value_label.setText(self.format_string.format(self.value)) if not self._slider.isSliderDown(): self.set_slider_to_closest_value(self.value) def ctrl_limit_changed(self, which, new_limit): """ Callback invoked when the Channel receives new control limit values. Parameters ---------- which : str Which control limit was changed. "UPPER" or "LOWER" new_limit : float New value for the control limit """ PyDMWritableWidget.ctrl_limit_changed(self, which, new_limit) if not self.userDefinedLimits: self.reset_slider_limits() def update_format_string(self): """ Reconstruct the format string to be used when representing the output value. Returns ------- format_string : str The format string to be used including or not the precision and unit """ fs = super(PyDMSlider, self).update_format_string() self.update_labels() return fs def set_enable_state(self): """ Determines wether or not the widget must be enabled or not depending on the write access, connection state and presence of limits information """ # Even though by documentation disabling parent QFrame (self), should disable internal # slider, in practice it still remains interactive (PyQt 5.12.1). Disabling explicitly, solves # the problem. should_enable = self._write_access and self._connected and not self._needs_limit_info self.setEnabled(should_enable) self._slider.setEnabled(should_enable) @Slot(int) def internal_slider_action_triggered(self, action): self.actionTriggered.emit(action) @Slot(int) def internal_slider_moved(self, val): """ Method invoked when the slider is moved. Parameters ---------- val : float """ # Avoid potential crash if limits are undefined if self._slider_position_to_value_map is None: return # The user has moved the slider, we need to update our value. # Only update the underlying value, not the self.value property, # because we don't need to reset the slider position. If we change # self.value, we can get into a loop where the position changes, which # updates the value, which changes the position again, etc etc. self.value = self._slider_position_to_value_map[val] self.sliderMoved.emit(self.value) @Slot() def internal_slider_pressed(self): """ Method invoked when the slider is pressed """ self.sliderPressed.emit() @Slot() def internal_slider_released(self): """ Method invoked when the slider is released """ self.sliderReleased.emit() @Slot(int) def internal_slider_value_changed(self, val): """ Method invoked when a new value is selected on the slider. This will cause the new value to be emitted to the signal unless `mute_internal_slider_changes` is True. Parameters ---------- val : int """ # At this point, our local copy of the value reflects the position of the # slider, now all we need to do is emit a signal to PyDM so that the data # plugin will send a put to the channel. Don't update self.value or self._value # in here, it is pointless at best, and could cause an infinite loop at worst. if not self._mute_internal_slider_changes: self.send_value_signal[float].emit(self.value) @Property(bool) def showLimitLabels(self): """ Whether or not the high and low limits should be displayed on the slider. Returns ------- bool """ return self._show_limit_labels @showLimitLabels.setter def showLimitLabels(self, checked): """ Whether or not the high and low limits should be displayed on the slider. Parameters ---------- checked : bool """ self._show_limit_labels = checked if checked: self.low_lim_label.show() self.high_lim_label.show() else: self.low_lim_label.hide() self.high_lim_label.hide() @Property(bool) def showValueLabel(self): """ Whether or not the current value should be displayed on the slider. Returns ------- bool """ return self._show_value_label @showValueLabel.setter def showValueLabel(self, checked): """ Whether or not the current value should be displayed on the slider. Parameters ---------- checked : bool """ self._show_value_label = checked if checked: self.value_label.show() else: self.value_label.hide() @Property(QSlider.TickPosition) def tickPosition(self): """ Where to draw tick marks for the slider. Returns ------- QSlider.TickPosition """ return self._slider.tickPosition() @tickPosition.setter def tickPosition(self, position): """ Where to draw tick marks for the slider. Parameter --------- position : QSlider.TickPosition """ self._slider.setTickPosition(position) @Property(bool) def userDefinedLimits(self): """ Wether or not to use limits defined by the user and not from the channel Returns ------- bool """ return self._user_defined_limits @userDefinedLimits.setter def userDefinedLimits(self, user_defined_limits): """ Wether or not to use limits defined by the user and not from the channel Parameters ---------- user_defined_limits : bool """ self._user_defined_limits = user_defined_limits self.reset_slider_limits() @Property(float) def userMinimum(self): """ Lower user defined limit value Returns ------- float """ return self._user_minimum @userMinimum.setter def userMinimum(self, new_min): """ Lower user defined limit value Parameters ---------- new_min : float """ self._user_minimum = float(new_min) if new_min is not None else None if self.userDefinedLimits: self.reset_slider_limits() @Property(float) def userMaximum(self): """ Upper user defined limit value Returns ------- float """ return self._user_maximum @userMaximum.setter def userMaximum(self, new_max): """ Upper user defined limit value Parameters ---------- new_max : float """ self._user_maximum = float(new_max) if new_max is not None else None if self.userDefinedLimits: self.reset_slider_limits() @property def minimum(self): """ The current value being used for the lower limit Returns ------- float """ if self.userDefinedLimits: return self._user_minimum return self._lower_ctrl_limit @property def maximum(self): """ The current value being used for the upper limit Returns ------- float """ if self.userDefinedLimits: return self._user_maximum return self._upper_ctrl_limit @Property(int) def num_steps(self): """ The number of steps on the slider Returns ------- int """ return self._num_steps @num_steps.setter def num_steps(self, new_steps): """ The number of steps on the slider Parameters ---------- new_steps : int """ self._num_steps = int(new_steps) self.reset_slider_limits()
class Dimension(QWidget): stateChanged = Signal(int) valueChanged = Signal() """ pass in dimension state: one of (State.X, State.Y, State.NONE, State.DISBALE) Can be run independently by: from mantidqt.widgets.sliceviewer.dimensionwidget import Dimension from qtpy.QtWidgets import QApplication app = QApplication([]) window = Dimension({'minimum':-1.1, 'number_of_bins':11, 'width':0.2, 'name':'Dim0', 'units':'A'}) window.show() app.exec_() """ def __init__(self, dim_info, number=0, state=State.NONE, parent=None): super(Dimension, self).__init__(parent) self.minimum = dim_info['minimum'] self.nbins = dim_info['number_of_bins'] self.width = dim_info['width'] self.number = number self.layout = QHBoxLayout(self) self.name = QLabel(dim_info['name']) self.units = QLabel(dim_info['units']) self.x = QPushButton('X') self.x.setFixedSize(32,32) self.x.setCheckable(True) self.x.clicked.connect(self.x_clicked) self.y = QPushButton('Y') self.y.setFixedSize(32,32) self.y.setCheckable(True) self.y.clicked.connect(self.y_clicked) self.slider = QSlider(Qt.Horizontal) self.slider.setRange(0, self.nbins-1) self.slider.valueChanged.connect(self.slider_changed) self.spinbox = QDoubleSpinBox() self.spinbox.setDecimals(3) self.spinbox.setRange(self.get_bin_center(0), self.get_bin_center(self.nbins-1)) self.spinbox.setSingleStep(self.width) self.spinbox.valueChanged.connect(self.spinbox_changed) self.layout.addWidget(self.name) self.layout.addWidget(self.x) self.layout.addWidget(self.y) self.layout.addWidget(self.slider, stretch=1) self.layout.addStretch(0) self.layout.addWidget(self.spinbox) self.layout.addWidget(self.units) self.set_value(0) if self.nbins < 2: state = State.DISABLE self.set_state(state) def set_state(self, state): self.state = state if self.state == State.X: self.x.setChecked(True) self.y.setChecked(False) self.slider.hide() self.spinbox.hide() self.units.hide() elif self.state == State.Y: self.x.setChecked(False) self.y.setChecked(True) self.slider.hide() self.spinbox.hide() self.units.hide() elif self.state == State.NONE: self.x.setChecked(False) self.y.setChecked(False) self.slider.show() self.spinbox.show() self.units.show() else: self.x.setChecked(False) self.x.setDisabled(True) self.y.setChecked(False) self.y.setDisabled(True) self.slider.hide() self.spinbox.show() self.spinbox.setDisabled(True) self.units.show() def get_state(self): return self.state def x_clicked(self): old_state = self.state self.set_state(State.X) if self.state != old_state: self.stateChanged.emit(self.number) def y_clicked(self): old_state = self.state self.set_state(State.Y) if self.state != old_state: self.stateChanged.emit(self.number) def spinbox_changed(self): self.value = self.spinbox.value() self.update_slider() self.valueChanged.emit() def slider_changed(self): self.value = self.get_bin_center(self.slider.value()) self.update_spinbox() self.valueChanged.emit() def get_bin_center(self, n): return (n+0.5)*self.width+self.minimum def update_slider(self): i = (self.value-self.minimum)/self.width self.slider.setValue(int(min(max(i, 0), self.nbins-1))) def update_spinbox(self): self.spinbox.setValue(self.value) def set_value(self, value): self.value = value self.update_slider() self.update_spinbox() def get_value(self): return self.value
class ColorbarWidget(QWidget): colorbarChanged = Signal() # The parent should simply redraw their canvas def __init__(self, parent=None): super(ColorbarWidget, self).__init__(parent) self.setWindowTitle("Colorbar") self.setMaximumWidth(100) self.cmap = QComboBox() self.cmap.addItems(sorted(cm.cmap_d.keys())) self.cmap.currentIndexChanged.connect(self.cmap_index_changed) self.cmin = QLineEdit() self.cmin_value = 0 self.cmin.setMaximumWidth(100) self.cmin.editingFinished.connect(self.clim_changed) self.cmin_layout = QHBoxLayout() self.cmin_layout.addStretch() self.cmin_layout.addWidget(self.cmin) self.cmin_layout.addStretch() self.cmax = QLineEdit() self.cmax_value = 1 self.cmax.setMaximumWidth(100) self.cmax.editingFinished.connect(self.clim_changed) self.cmin.setValidator(QDoubleValidator()) self.cmax.setValidator(QDoubleValidator()) self.cmax_layout = QHBoxLayout() self.cmax_layout.addStretch() self.cmax_layout.addWidget(self.cmax) self.cmax_layout.addStretch() self.norm_layout = QHBoxLayout() self.norm = QComboBox() self.norm.addItems(NORM_OPTS) self.norm.currentIndexChanged.connect(self.norm_changed) self.powerscale = QLineEdit() self.powerscale_value = 2 self.powerscale.setText("2") self.powerscale.setValidator(QDoubleValidator(0.001, 100, 3)) self.powerscale.setMaximumWidth(50) self.powerscale.editingFinished.connect(self.norm_changed) self.powerscale.hide() self.powerscale_label = QLabel("n=") self.powerscale_label.hide() self.norm_layout.addStretch() self.norm_layout.addWidget(self.norm) self.norm_layout.addStretch() self.norm_layout.addWidget(self.powerscale_label) self.norm_layout.addWidget(self.powerscale) self.autoscale = QCheckBox("Autoscaling") self.autoscale.setChecked(True) self.autoscale.stateChanged.connect(self.update_clim) self.canvas = FigureCanvas(Figure()) if parent: # Set facecolor to match parent self.canvas.figure.set_facecolor( parent.palette().window().color().getRgbF()) self.ax = self.canvas.figure.add_axes([0.0, 0.02, 0.2, 0.97]) # layout self.layout = QVBoxLayout(self) self.layout.setContentsMargins(0, 0, 0, 0) self.layout.setSpacing(2) self.layout.addWidget(self.cmap) self.layout.addLayout(self.cmax_layout) self.layout.addWidget(self.canvas, stretch=1) self.layout.addLayout(self.cmin_layout) self.layout.addLayout(self.norm_layout) self.layout.addWidget(self.autoscale) def set_mappable(self, mappable): """ When a new plot is created this method should be called with the new mappable """ self.ax.clear() try: # Use current cmap cmap = get_current_cmap(self.colorbar) except AttributeError: # else use default cmap = ConfigService.getString("plots.images.Colormap") self.colorbar = Colorbar(ax=self.ax, mappable=mappable) self.cmin_value, self.cmax_value = mappable.get_clim() self.update_clim_text() self.cmap_changed(cmap) mappable_cmap = get_current_cmap(mappable) self.cmap.setCurrentIndex( sorted(cm.cmap_d.keys()).index(mappable_cmap.name)) self.redraw() def cmap_index_changed(self): self.cmap_changed(self.cmap.currentText()) def cmap_changed(self, name): self.colorbar.mappable.set_cmap(name) if mpl_version_info() >= (3, 1): self.colorbar.update_normal(self.colorbar.mappable) else: self.colorbar.set_cmap(name) self.redraw() def mappable_changed(self): """ Updates the colormap and min/max values of the colorbar when the plot changes via settings. """ mappable_cmap = get_current_cmap(self.colorbar.mappable) low, high = self.colorbar.mappable.get_clim() self.cmin_value = low self.cmax_value = high self.update_clim_text() self.cmap.setCurrentIndex( sorted(cm.cmap_d.keys()).index(mappable_cmap.name)) self.redraw() def norm_changed(self): """ Called when a different normalization is selected """ idx = self.norm.currentIndex() if NORM_OPTS[idx] == 'Power': self.powerscale.show() self.powerscale_label.show() else: self.powerscale.hide() self.powerscale_label.hide() self.colorbar.mappable.set_norm(self.get_norm()) self.set_mappable(self.colorbar.mappable) def get_norm(self): """ This will create a matplotlib.colors.Normalize from selected idx, limits and powerscale """ idx = self.norm.currentIndex() if self.autoscale.isChecked(): cmin = cmax = None else: cmin = self.cmin_value cmax = self.cmax_value if NORM_OPTS[idx] == 'Power': if self.powerscale.hasAcceptableInput(): self.powerscale_value = float(self.powerscale.text()) return PowerNorm(gamma=self.powerscale_value, vmin=cmin, vmax=cmax) elif NORM_OPTS[idx] == "SymmetricLog10": return SymLogNorm( 1e-8 if cmin is None else max(1e-8, abs(cmin) * 1e-3), vmin=cmin, vmax=cmax) else: return Normalize(vmin=cmin, vmax=cmax) def clim_changed(self): """ Called when either the min or max is changed. Will unset the autoscale. """ self.autoscale.blockSignals(True) self.autoscale.setChecked(False) self.autoscale.blockSignals(False) self.update_clim() def update_clim(self): """ This will update the clim of the plot based on min, max, and autoscale """ if self.autoscale.isChecked(): data = self.colorbar.mappable.get_array() try: try: self.cmin_value = data[~data.mask].min() self.cmax_value = data[~data.mask].max() except (AttributeError, IndexError): self.cmin_value = np.nanmin(data) self.cmax_value = np.nanmax(data) except (ValueError, RuntimeWarning): # all values mask pass self.update_clim_text() else: if self.cmin.hasAcceptableInput(): cmin = float(self.cmin.text()) if cmin < self.cmax_value: self.cmin_value = cmin else: # reset values back self.update_clim_text() if self.cmax.hasAcceptableInput(): cmax = float(self.cmax.text()) if cmax > self.cmin_value: self.cmax_value = cmax else: # reset values back self.update_clim_text() self.colorbar.mappable.set_clim(self.cmin_value, self.cmax_value) self.redraw() def update_clim_text(self): """ Update displayed limit values based on stored ones """ self.cmin.setText("{:.4}".format(self.cmin_value)) self.cmax.setText("{:.4}".format(self.cmax_value)) def redraw(self): """ Redraws the colobar and emits signal to cause the parent to redraw """ self.colorbar.update_ticks() self.colorbar.draw_all() self.canvas.draw_idle() self.colorbarChanged.emit()
class HelpWidget(PluginMainWidget): ENABLE_SPINNER = True # Signals sig_item_found = Signal() """This signal is emitted when an item is found.""" sig_render_started = Signal() """This signal is emitted to inform a help text rendering has started.""" sig_render_finished = Signal() """This signal is emitted to inform a help text rendering has finished.""" def __init__(self, name=None, plugin=None, parent=None): super().__init__(name, plugin, parent) # Attributes self._starting_up = True self._current_color_scheme = None self._last_texts = [None, None] self._last_editor_doc = None self._last_console_cb = None self._last_editor_cb = None self.css_path = self.get_conf('css_path') self.no_docs = _("No documentation available") self.docstring = True # TODO: What is this used for? # Widgets self._sphinx_thread = SphinxThread( html_text_no_doc=warning(self.no_docs, css_path=self.css_path), css_path=self.css_path, ) self.shell = None self.internal_console = None self.internal_shell = None self.plain_text = PlainText(self) self.rich_text = RichText(self) self.source_label = QLabel(_("Source")) self.source_combo = QComboBox(self) self.object_label = QLabel(_("Object")) self.object_combo = ObjectComboBox(self) self.object_edit = QLineEdit(self) # Setup self.object_edit.setReadOnly(True) self.object_combo.setMaxCount(self.get_conf('max_history_entries')) self.object_combo.setItemText(0, '') self.plain_text.set_wrap_mode(self.get_conf('wrap')) self.source_combo.addItems([_("Console"), _("Editor")]) if (not programs.is_module_installed('rope') and not programs.is_module_installed('jedi', '>=0.11.0')): self.source_combo.hide() self.source_label.hide() # Layout self.stack_layout = layout = QStackedLayout() layout.addWidget(self.rich_text) layout.addWidget(self.plain_text) self.setLayout(layout) # Signals self._sphinx_thread.html_ready.connect( self._on_sphinx_thread_html_ready) self._sphinx_thread.error_msg.connect(self._on_sphinx_thread_error_msg) self.object_combo.valid.connect(self.force_refresh) self.rich_text.sig_link_clicked.connect(self.handle_link_clicks) self.source_combo.currentIndexChanged.connect( lambda x: self.source_changed()) self.sig_render_started.connect(self.start_spinner) self.sig_render_finished.connect(self.stop_spinner) # --- PluginMainWidget API # ------------------------------------------------------------------------ def get_title(self): return _('Help') def setup(self): self.wrap_action = self.create_action( name=HelpWidgetActions.ToggleWrap, text=_("Wrap lines"), toggled=True, initial=self.get_conf('wrap'), option='wrap') self.copy_action = self.create_action( name=HelpWidgetActions.CopyAction, text=_("Copy"), triggered=lambda value: self.plain_text.copy(), register_shortcut=False, ) self.select_all_action = self.create_action( name=HelpWidgetActions.SelectAll, text=_("Select All"), triggered=lambda value: self.plain_text.select_all(), register_shortcut=False, ) self.auto_import_action = self.create_action( name=HelpWidgetActions.ToggleAutomaticImport, text=_("Automatic import"), toggled=True, initial=self.get_conf('automatic_import'), option='automatic_import') self.show_source_action = self.create_action( name=HelpWidgetActions.ToggleShowSource, text=_("Show Source"), toggled=True, option='show_source') self.rich_text_action = self.create_action( name=HelpWidgetActions.ToggleRichMode, text=_("Rich Text"), toggled=True, initial=self.get_conf('rich_mode'), option='rich_mode') self.plain_text_action = self.create_action( name=HelpWidgetActions.TogglePlainMode, text=_("Plain Text"), toggled=True, initial=self.get_conf('plain_mode'), option='plain_mode') self.locked_action = self.create_action( name=HelpWidgetActions.ToggleLocked, text=_("Lock/Unlock"), toggled=True, icon=self.create_icon('lock_open'), initial=self.get_conf('locked'), option='locked') self.home_action = self.create_action( name=HelpWidgetActions.Home, text=_("Home"), triggered=self.show_intro_message, icon=self.create_icon('home'), ) # Add the help actions to an exclusive QActionGroup help_actions = QActionGroup(self) help_actions.setExclusive(True) help_actions.addAction(self.plain_text_action) help_actions.addAction(self.rich_text_action) # Menu menu = self.get_options_menu() for item in [ self.rich_text_action, self.plain_text_action, self.show_source_action ]: self.add_item_to_menu( item, menu=menu, section=HelpWidgetOptionsMenuSections.Display, ) self.add_item_to_menu( self.auto_import_action, menu=menu, section=HelpWidgetOptionsMenuSections.Other, ) # Plain text menu self._plain_text_context_menu = self.create_menu( "plain_text_context_menu") self.add_item_to_menu( self.copy_action, self._plain_text_context_menu, section="copy_section", ) self.add_item_to_menu( self.select_all_action, self._plain_text_context_menu, section="select_section", ) self.add_item_to_menu( self.wrap_action, self._plain_text_context_menu, section="wrap_section", ) # Toolbar toolbar = self.get_main_toolbar() for item in [ self.source_label, self.source_combo, self.object_label, self.object_combo, self.object_edit, self.home_action, self.locked_action ]: self.add_item_to_toolbar( item, toolbar=toolbar, section=HelpWidgetMainToolbarSections.Main, ) self.source_changed() self.switch_to_rich_text() self.show_intro_message() # Signals self.plain_text.sig_custom_context_menu_requested.connect( self._show_plain_text_context_menu) def _should_display_welcome_page(self): """Determine if the help welcome page should be displayed.""" return (self._last_editor_doc is None or self._last_console_cb is None or self._last_editor_cb is None) @on_conf_change(option='wrap') def on_wrap_option_update(self, value): self.plain_text.set_wrap_mode(value) @on_conf_change(option='locked') def on_lock_update(self, value): if value: icon = self.create_icon('lock') tip = _("Unlock") else: icon = self.create_icon('lock_open') tip = _("Lock") action = self.get_action(HelpWidgetActions.ToggleLocked) action.setIcon(icon) action.setToolTip(tip) @on_conf_change(option='automatic_import') def on_automatic_import_update(self, value): self.object_combo.validate_current_text() if self._should_display_welcome_page(): self.show_intro_message() else: self.force_refresh() @on_conf_change(option='rich_mode') def on_rich_mode_update(self, value): if value: # Plain Text OFF / Rich text ON self.docstring = not value self.stack_layout.setCurrentWidget(self.rich_text) self.get_action( HelpWidgetActions.ToggleShowSource).setChecked(False) else: # Plain Text ON / Rich text OFF self.docstring = value self.stack_layout.setCurrentWidget(self.plain_text) if self._should_display_welcome_page(): self.show_intro_message() else: self.force_refresh() @on_conf_change(option='show_source') def on_show_source_update(self, value): if value: self.switch_to_plain_text() self.get_action(HelpWidgetActions.ToggleRichMode).setChecked(False) self.docstring = not value if self._should_display_welcome_page(): self.show_intro_message() else: self.force_refresh() def update_actions(self): for __, action in self.get_actions().items(): # IMPORTANT: Since we are defining the main actions in here # and the context is WidgetWithChildrenShortcut we need to # assign the same actions to the children widgets in order # for shortcuts to work for widget in [ self.plain_text, self.rich_text, self.source_combo, self.object_combo, self.object_edit ]: if action not in widget.actions(): widget.addAction(action) def get_focus_widget(self): self.object_combo.lineEdit().selectAll() return self.object_combo # --- Private API # ------------------------------------------------------------------------ @Slot(QPoint) def _show_plain_text_context_menu(self, point): point = self.plain_text.mapToGlobal(point) self._plain_text_context_menu.popup(point) def _on_sphinx_thread_html_ready(self, html_text): """ Set our sphinx documentation based on thread result. Parameters ---------- html_text: str Html results text. """ self._sphinx_thread.wait() self.set_rich_text_html(html_text, QUrl.fromLocalFile(self.css_path)) self.sig_render_finished.emit() self.stop_spinner() def _on_sphinx_thread_error_msg(self, error_msg): """ Display error message on Sphinx rich text failure. Parameters ---------- error_msg: str Error message text. """ self._sphinx_thread.wait() self.plain_text_action.setChecked(True) sphinx_ver = programs.get_module_version('sphinx') QMessageBox.critical( self, _('Help'), _("The following error occurred when calling " "<b>Sphinx %s</b>. <br>Incompatible Sphinx " "version or doc string decoding failed." "<br><br>Error message:<br>%s") % (sphinx_ver, error_msg), ) self.sig_render_finished.emit() # --- Public API # ------------------------------------------------------------------------ def source_is_console(self): """Return True if source is Console.""" return self.source_combo.currentIndex() == 0 def switch_to_editor_source(self): """Switch to editor view of the help viewer.""" self.source_combo.setCurrentIndex(1) def switch_to_console_source(self): """Switch to console view of the help viewer.""" self.source_combo.setCurrentIndex(0) def source_changed(self): """Handle a source (plain/rich) change.""" is_console = self.source_is_console() if is_console: self.object_combo.show() self.object_edit.hide() else: # Editor self.object_combo.hide() self.object_edit.show() self.get_action( HelpWidgetActions.ToggleShowSource).setEnabled(is_console) self.get_action( HelpWidgetActions.ToggleAutomaticImport).setEnabled(is_console) self.restore_text() def save_text(self, callback): """ Save help text. Parameters ---------- callback: callable Method to call on save. """ if self.source_is_console(): self._last_console_cb = callback else: self._last_editor_cb = callback def restore_text(self): """Restore last text using callback.""" if self.source_is_console(): cb = self._last_console_cb else: cb = self._last_editor_cb if cb is None: if self.get_conf('plain_mode'): self.switch_to_plain_text() else: self.switch_to_rich_text() else: func = cb[0] args = cb[1:] func(*args) if get_meth_class_inst(func) is self.rich_text: self.switch_to_rich_text() else: self.switch_to_plain_text() @property def find_widget(self): """Show find widget.""" if self.get_conf('plain_mode'): return self.plain_text.find_widget else: return self.rich_text.find_widget def switch_to_plain_text(self): """Switch to plain text mode.""" self.get_action(HelpWidgetActions.TogglePlainMode).setChecked(True) def switch_to_rich_text(self): """Switch to rich text mode.""" self.get_action(HelpWidgetActions.ToggleRichMode).setChecked(True) def set_plain_text(self, text, is_code): """ Set plain text docs. Parameters ---------- text: str Text content. is_code: bool True if it is code text. Notes ----- Text is coming from utils.dochelpers.getdoc """ if type(text) is dict: name = text['name'] if name: rst_title = ''.join([ '=' * len(name), '\n', name, '\n', '=' * len(name), '\n\n' ]) else: rst_title = '' try: if text['argspec']: definition = ''.join( ['Definition: ', name, text['argspec'], '\n\n']) else: definition = '' if text['note']: note = ''.join(['Type: ', text['note'], '\n\n----\n\n']) else: note = '' except TypeError: definition = self.no_docs note = '' full_text = ''.join( [rst_title, definition, note, text['docstring']]) else: full_text = text self.plain_text.set_text(full_text, is_code) self.save_text([self.plain_text.set_text, full_text, is_code]) def set_rich_text_html(self, html_text, base_url): """ Set rich text. Parameters ---------- html_text: str Html string. base_url: str Location of stylesheets and images to load in the page. """ self.rich_text.set_html(html_text, base_url) self.save_text([self.rich_text.set_html, html_text, base_url]) def show_loading_message(self): """Create html page to show while the documentation is generated.""" self.sig_render_started.emit() loading_message = _("Retrieving documentation") loading_img = get_image_path('loading_sprites') if os.name == 'nt': loading_img = loading_img.replace('\\', '/') self.set_rich_text_html( loading(loading_message, loading_img, css_path=self.css_path), QUrl.fromLocalFile(self.css_path), ) def show_intro_message(self): """Show message on Help with the right shortcuts.""" intro_message_eq = _("Here you can get help of any object by pressing " "%s in front of it, either on the Editor or the " "Console.%s") intro_message_dif = _( "Here you can get help of any object by pressing " "%s in front of it on the Editor, or %s in front " "of it on the Console.%s") intro_message_common = _( "Help can also be shown automatically after writing " "a left parenthesis next to an object. You can " "activate this behavior in %s.") prefs = _("Preferences > Help") shortcut_editor = self.get_conf('editor/inspect current object', section='shortcuts') shortcut_console = self.get_conf('console/inspect current object', section='shortcuts') if sys.platform == 'darwin': shortcut_editor = shortcut_editor.replace('Ctrl', 'Cmd') shortcut_console = shortcut_console.replace('Ctrl', 'Cmd') if self.get_conf('rich_mode'): title = _("Usage") tutorial_message = _("New to Spyder? Read our") tutorial = _("tutorial") if shortcut_editor == shortcut_console: intro_message = (intro_message_eq + intro_message_common) % ( "<b>" + shortcut_editor + "</b>", "<br><br>", "<i>" + prefs + "</i>") else: intro_message = (intro_message_dif + intro_message_common) % ( "<b>" + shortcut_editor + "</b>", "<b>" + shortcut_console + "</b>", "<br><br>", "<i>" + prefs + "</i>") self.set_rich_text_html( usage(title, intro_message, tutorial_message, tutorial, css_path=self.css_path), QUrl.fromLocalFile(self.css_path)) else: install_sphinx = "\n\n%s" % _("Please consider installing Sphinx " "to get documentation rendered in " "rich text.") if shortcut_editor == shortcut_console: intro_message = (intro_message_eq + intro_message_common) % ( shortcut_editor, "\n\n", prefs) else: intro_message = (intro_message_dif + intro_message_common) % ( shortcut_editor, shortcut_console, "\n\n", prefs) intro_message += install_sphinx self.set_plain_text(intro_message, is_code=False) def show_rich_text(self, text, collapse=False, img_path=''): """ Show text in rich mode. Parameters ---------- text: str Plain text to display. collapse: bool, optional Show collapsable sections as collapsed/expanded. Default is False. img_path: str, optional Path to folder with additional images needed to correctly display the rich text help. Default is ''. """ self.switch_to_rich_text() context = generate_context(collapse=collapse, img_path=img_path, css_path=self.css_path) self.render_sphinx_doc(text, context) def show_plain_text(self, text): """ Show text in plain mode. Parameters ---------- text: str Plain text to display. """ self.switch_to_plain_text() self.set_plain_text(text, is_code=False) @Slot() def show_tutorial(self): """Show the Spyder tutorial.""" tutorial_path = get_module_source_path('spyder.plugins.help.utils') tutorial = os.path.join(tutorial_path, 'tutorial.rst') with open(tutorial, 'r') as fh: text = fh.read() self.show_rich_text(text, collapse=True) def handle_link_clicks(self, url): """ Handle how url links should be opened. Parameters ---------- url: QUrl QUrl object containing the link to open. """ url = to_text_string(url.toString()) if url == "spy://tutorial": self.show_tutorial() elif url.startswith('http'): start_file(url) else: self.rich_text.load_url(url) @Slot() @Slot(bool) @Slot(bool, bool) def force_refresh(self, valid=True, editing=True): """ Force a refresh/rerender of the help viewer content. Parameters ---------- valid: bool, optional Default is True. editing: bool, optional Default is True. """ if valid: if self.source_is_console(): self.set_object_text(None, force_refresh=True) elif self._last_editor_doc is not None: self.set_editor_doc(self._last_editor_doc, force_refresh=True) def set_object_text(self, text, force_refresh=False, ignore_unknown=False): """ Set object's name in Help's combobox. Parameters ---------- text: str Object name. force_refresh: bool, optional Force a refresh with the rendering. ignore_unknown: bool, optional Ignore not found object names. See Also -------- :py:meth:spyder.widgets.mixins.GetHelpMixin.show_object_info """ if self.get_conf('locked') and not force_refresh: return self.switch_to_console_source() add_to_combo = True if text is None: text = to_text_string(self.object_combo.currentText()) add_to_combo = False found = self.show_help(text, ignore_unknown=ignore_unknown) if ignore_unknown and not found: return if add_to_combo: self.object_combo.add_text(text) if found: self.sig_item_found.emit() index = self.source_combo.currentIndex() self._last_texts[index] = text def set_editor_doc(self, help_data, force_refresh=False): """ Set content for help data sent from the editor. Parameters ---------- help_data: dict Dictionary with editor introspection information. force_refresh: bool, optional Force a refresh with the rendering. Examples -------- >>> help_data = { 'obj_text': str, 'name': str, 'argspec': str, 'note': str, 'docstring': str, 'path': str, } """ if self.get_conf('locked') and not force_refresh: return self.switch_to_editor_source() self._last_editor_doc = help_data self.object_edit.setText(help_data['obj_text']) if self.get_conf('rich_mode'): self.render_sphinx_doc(help_data) else: self.set_plain_text(help_data, is_code=False) index = self.source_combo.currentIndex() self._last_texts[index] = help_data['docstring'] def set_shell(self, shell): """ Bind to shell. Parameters ---------- shell: object internal shell or ipython console shell """ self.shell = shell def get_shell(self): """ Return shell which is currently bound to Help. """ if self.shell is None: self.shell = self.internal_shell return self.shell def render_sphinx_doc(self, help_data, context=None, css_path=CSS_PATH): """ Transform help_data dictionary to HTML and show it. Parameters ---------- help_data: str or dict Dictionary with editor introspection information. context: dict Sphinx context. css_path: str Path to CSS file for styling. """ if isinstance(help_data, dict): path = help_data.pop('path', '') dname = os.path.dirname(path) else: dname = '' # Math rendering option could have changed self._sphinx_thread.render(help_data, context, self.get_conf('math'), dname, css_path=self.css_path) self.show_loading_message() def show_help(self, obj_text, ignore_unknown=False): """ Show help for an object's name. Parameters ---------- obj_text: str Object's name. ignore_unknown: bool, optional Ignore unknown object's name. """ # TODO: This method makes active use of the shells. It would be better # to use signals and pass information this way for better decoupling. shell = self.get_shell() if shell is None: return obj_text = to_text_string(obj_text) if not shell.is_defined(obj_text): if (self.get_conf('automatic_import') and self.internal_shell.is_defined(obj_text, force_import=True)): shell = self.internal_shell else: shell = None doc = None source_text = None if shell is not None: doc = shell.get_doc(obj_text) source_text = shell.get_source(obj_text) is_code = False if self.get_conf('rich_mode'): self.render_sphinx_doc(doc, css_path=self.css_path) return doc is not None elif self.docstring: hlp_text = doc if hlp_text is None: hlp_text = source_text if hlp_text is None: return False else: hlp_text = source_text if hlp_text is None: hlp_text = doc if hlp_text is None: hlp_text = _("No source code available.") if ignore_unknown: return False else: is_code = True self.set_plain_text(hlp_text, is_code=is_code) return True def set_rich_text_font(self, font, fixed_font): """ Set rich text mode font. Parameters ---------- fixed_font: QFont The current rich text font to use. """ self.rich_text.set_font(font, fixed_font=fixed_font) def set_plain_text_font(self, font, color_scheme=None): """ Set plain text mode font. Parameters ---------- font: QFont The current plain text font to use. color_scheme: str The selected color scheme. """ if color_scheme is None: color_scheme = self._current_color_scheme self.plain_text.set_font(font, color_scheme=color_scheme) def set_plain_text_color_scheme(self, color_scheme): """ Set plain text mode color scheme. Parameters ---------- color_scheme: str The selected color scheme. """ self._current_color_scheme = color_scheme self.plain_text.set_color_scheme(color_scheme) def set_history(self, history): """ Set list of strings on object combo box. Parameters ---------- history: list List of strings of objects. """ self.object_combo.addItems(history) def get_history(self): """ Return list of strings on object combo box. """ history = [] for index in range(self.object_combo.count()): history.append(to_text_string(self.object_combo.itemText(index))) return history def set_internal_console(self, console): """ Set the internal console shell. Parameters ---------- console: :py:class:spyder.plugins.console.plugin.Console Console plugin. """ self.internal_console = console self.internal_shell = console.get_widget().shell
class ShortcutEditor(QDialog): """A dialog for entering key sequences.""" def __init__(self, parent, context, name, sequence, shortcuts): super(ShortcutEditor, self).__init__(parent) self._parent = parent self.context = context self.npressed = 0 self.keys = set() self.key_modifiers = set() self.key_non_modifiers = list() self.key_text = list() self.sequence = sequence self.new_sequence = None self.edit_state = True self.shortcuts = shortcuts # Widgets self.label_info = QLabel() self.label_info.setText(_("Press the new shortcut and select 'Ok': \n" "(Press 'Tab' once to switch focus between the shortcut entry \n" "and the buttons below it)")) self.label_current_sequence = QLabel(_("Current shortcut:")) self.text_current_sequence = QLabel(sequence) self.label_new_sequence = QLabel(_("New shortcut:")) self.text_new_sequence = CustomLineEdit(self) self.text_new_sequence.setPlaceholderText(sequence) self.helper_button = HelperToolButton() self.helper_button.hide() self.label_warning = QLabel() self.label_warning.hide() bbox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) self.button_ok = bbox.button(QDialogButtonBox.Ok) self.button_cancel = bbox.button(QDialogButtonBox.Cancel) # Setup widgets self.setWindowTitle(_('Shortcut: {0}').format(name)) self.button_ok.setFocusPolicy(Qt.NoFocus) self.button_ok.setEnabled(False) self.button_cancel.setFocusPolicy(Qt.NoFocus) self.helper_button.setToolTip('') self.helper_button.setFocusPolicy(Qt.NoFocus) style = """ QToolButton { margin:1px; border: 0px solid grey; padding:0px; border-radius: 0px; }""" self.helper_button.setStyleSheet(style) self.text_new_sequence.setFocusPolicy(Qt.NoFocus) self.label_warning.setFocusPolicy(Qt.NoFocus) # Layout spacing = 5 layout_sequence = QGridLayout() layout_sequence.addWidget(self.label_info, 0, 0, 1, 3) layout_sequence.addItem(QSpacerItem(spacing, spacing), 1, 0, 1, 2) layout_sequence.addWidget(self.label_current_sequence, 2, 0) layout_sequence.addWidget(self.text_current_sequence, 2, 2) layout_sequence.addWidget(self.label_new_sequence, 3, 0) layout_sequence.addWidget(self.helper_button, 3, 1) layout_sequence.addWidget(self.text_new_sequence, 3, 2) layout_sequence.addWidget(self.label_warning, 4, 2, 1, 2) layout = QVBoxLayout() layout.addLayout(layout_sequence) layout.addSpacing(spacing) layout.addWidget(bbox) self.setLayout(layout) # Signals bbox.accepted.connect(self.accept) bbox.rejected.connect(self.reject) @Slot() def reject(self): """Slot for rejected signal.""" # Added for issue #5426. Due to the focusPolicy of Qt.NoFocus for the # buttons, if the cancel button was clicked without first setting focus # to the button, it would cause a seg fault crash. self.button_cancel.setFocus() super().reject() @Slot() def accept(self): """Slot for accepted signal.""" # Added for issue #5426. Due to the focusPolicy of Qt.NoFocus for the # buttons, if the ok button was clicked without first setting focus to # the button, it would cause a seg fault crash. self.button_ok.setFocus() super().accept() def keyPressEvent(self, e): """Qt override.""" key = e.key() # Check if valid keys if key not in VALID_KEYS: self.invalid_key_flag = True return self.npressed += 1 self.key_non_modifiers.append(key) self.key_modifiers.add(key) self.key_text.append(e.text()) self.invalid_key_flag = False debug_print('key {0}, npressed: {1}'.format(key, self.npressed)) if key == Qt.Key_unknown: return # The user clicked just and only the special keys # Ctrl, Shift, Alt, Meta. if (key == Qt.Key_Control or key == Qt.Key_Shift or key == Qt.Key_Alt or key == Qt.Key_Meta): return modifiers = e.modifiers() if modifiers & Qt.ShiftModifier: key += Qt.SHIFT if modifiers & Qt.ControlModifier: key += Qt.CTRL if sys.platform == 'darwin': self.npressed -= 1 debug_print('decrementing') if modifiers & Qt.AltModifier: key += Qt.ALT if modifiers & Qt.MetaModifier: key += Qt.META self.keys.add(key) def toggle_state(self): """Switch between shortcut entry and Accept/Cancel shortcut mode.""" self.edit_state = not self.edit_state if not self.edit_state: self.text_new_sequence.setEnabled(False) if self.button_ok.isEnabled(): self.button_ok.setFocus() else: self.button_cancel.setFocus() else: self.text_new_sequence.setEnabled(True) self.text_new_sequence.setFocus() def nonedit_keyrelease(self, e): """Key release event for non-edit state.""" key = e.key() if key in [Qt.Key_Escape]: self.close() return if key in [Qt.Key_Left, Qt.Key_Right, Qt.Key_Up, Qt.Key_Down]: if self.button_ok.hasFocus(): self.button_cancel.setFocus() else: self.button_ok.setFocus() def keyReleaseEvent(self, e): """Qt override.""" self.npressed -= 1 if self.npressed <= 0: key = e.key() if len(self.keys) == 1 and key == Qt.Key_Tab: self.toggle_state() return if len(self.keys) == 1 and key == Qt.Key_Escape: self.set_sequence('') self.label_warning.setText(_("Please introduce a different " "shortcut")) if len(self.keys) == 1 and key in [Qt.Key_Return, Qt.Key_Enter]: self.toggle_state() return if not self.edit_state: self.nonedit_keyrelease(e) else: debug_print('keys: {}'.format(self.keys)) if self.keys and key != Qt.Key_Escape: self.validate_sequence() self.keys = set() self.key_modifiers = set() self.key_non_modifiers = list() self.key_text = list() self.npressed = 0 def check_conflicts(self): """Check shortcuts for conflicts.""" conflicts = [] for index, shortcut in enumerate(self.shortcuts): sequence = str(shortcut.key) if sequence == self.new_sequence and \ (shortcut.context == self.context or shortcut.context == '_' or self.context == '_'): conflicts.append(shortcut) return conflicts def update_warning(self, warning_type=NO_WARNING, conflicts=[]): """Update warning label to reflect conflict status of new shortcut""" if warning_type == NO_WARNING: warn = False tip = 'This shortcut is correct!' elif warning_type == SEQUENCE_CONFLICT: template = '<i>{0}<b>{1}</b></i>' tip_title = _('The new shortcut conflicts with:') + '<br>' tip_body = '' for s in conflicts: tip_body += ' - {0}: {1}<br>'.format(s.context, s.name) tip_body = tip_body[:-4] # Removing last <br> tip = template.format(tip_title, tip_body) warn = True elif warning_type == IN_BLACKLIST: template = '<i>{0}<b>{1}</b></i>' tip_title = _('Forbidden key sequence!') + '<br>' tip_body = '' use = BLACKLIST[self.new_sequence] if use is not None: tip_body = use tip = template.format(tip_title, tip_body) warn = True elif warning_type == SHIFT_BLACKLIST: template = '<i>{0}<b>{1}</b></i>' tip_title = _('Forbidden key sequence!') + '<br>' tip_body = '' use = BLACKLIST['Shift'] if use is not None: tip_body = use tip = template.format(tip_title, tip_body) warn = True elif warning_type == SEQUENCE_LENGTH: # Sequences with 5 keysequences (i.e. Ctrl+1, Ctrl+2, Ctrl+3, # Ctrl+4, Ctrl+5) are invalid template = '<i>{0}</i>' tip = _('A compound sequence can have {break} a maximum of ' '4 subsequences.{break}').format(**{'break': '<br>'}) warn = True elif warning_type == INVALID_KEY: template = '<i>{0}</i>' tip = _('Invalid key entered') + '<br>' warn = True self.helper_button.show() if warn: self.label_warning.show() self.helper_button.setIcon(get_std_icon('MessageBoxWarning')) self.button_ok.setEnabled(False) else: self.helper_button.setIcon(get_std_icon('DialogApplyButton')) self.label_warning.setText(tip) def set_sequence(self, sequence): """Set the new shortcut and update buttons.""" if not sequence or self.sequence == sequence: self.button_ok.setEnabled(False) different_sequence = False else: self.button_ok.setEnabled(True) different_sequence = True if sys.platform == 'darwin': if 'Meta+Ctrl' in sequence: shown_sequence = sequence.replace('Meta+Ctrl', 'Ctrl+Cmd') elif 'Ctrl+Meta' in sequence: shown_sequence = sequence.replace('Ctrl+Meta', 'Cmd+Ctrl') elif 'Ctrl' in sequence: shown_sequence = sequence.replace('Ctrl', 'Cmd') elif 'Meta' in sequence: shown_sequence = sequence.replace('Meta', 'Ctrl') else: shown_sequence = sequence else: shown_sequence = sequence self.text_new_sequence.setText(shown_sequence) self.new_sequence = sequence conflicts = self.check_conflicts() blacklist = self.new_sequence in BLACKLIST individual_keys = self.new_sequence.split('+') if conflicts and different_sequence: warning_type = SEQUENCE_CONFLICT elif blacklist: warning_type = IN_BLACKLIST elif len(individual_keys) == 2 and individual_keys[0] == 'Shift': warning_type = SHIFT_BLACKLIST else: warning_type = NO_WARNING self.update_warning(warning_type=warning_type, conflicts=conflicts) def validate_sequence(self): """Provide additional checks for accepting or rejecting shortcuts.""" if self.invalid_key_flag: self.update_warning(warning_type=INVALID_KEY) return for mod in MODIFIERS: non_mod = set(self.key_non_modifiers) non_mod.discard(mod) if mod in self.key_non_modifiers: self.key_non_modifiers.remove(mod) self.key_modifiers = self.key_modifiers - non_mod while u'' in self.key_text: self.key_text.remove(u'') self.key_text = [k.upper() for k in self.key_text] # Fix Backtab, Tab issue if Qt.Key_Backtab in self.key_non_modifiers: idx = self.key_non_modifiers.index(Qt.Key_Backtab) self.key_non_modifiers[idx] = Qt.Key_Tab if len(self.key_modifiers) == 0: # Filter single key allowed if self.key_non_modifiers[0] not in VALID_SINGLE_KEYS: return # Filter elif len(self.key_non_modifiers) > 1: return # QKeySequence accepts a maximum of 4 different sequences if len(self.keys) > 4: # Update warning self.update_warning(warning_type=SEQUENCE_LENGTH) return keys = [] for i in range(len(self.keys)): key_seq = 0 for m in self.key_modifiers: key_seq += MODIFIERS[m] key_seq += self.key_non_modifiers[i] keys.append(key_seq) sequence = QKeySequence(*keys) self.set_sequence(sequence.toString())
def __init__(self, parent=None, css_path=CSS_PATH): SpyderPluginWidget.__init__(self, parent) self.internal_shell = None self.console = None self.css_path = css_path self.no_doc_string = _("No documentation available") self._last_console_cb = None self._last_editor_cb = None self.plain_text = PlainText(self) self.rich_text = RichText(self) color_scheme = self.get_color_scheme() self.set_plain_text_font(self.get_font(), color_scheme) self.plain_text.editor.toggle_wrap_mode(self.get_option('wrap')) # Add entries to read-only editor context-menu self.wrap_action = create_action(self, _("Wrap lines"), toggled=self.toggle_wrap_mode) self.wrap_action.setChecked(self.get_option('wrap')) self.plain_text.editor.readonly_menu.addSeparator() add_actions(self.plain_text.editor.readonly_menu, (self.wrap_action, )) self.set_rich_text_font(self.get_font(rich_text=True)) self.shell = None # locked = disable link with Console self.locked = False self._last_texts = [None, None] self._last_editor_doc = None # Object name layout_edit = QHBoxLayout() layout_edit.setContentsMargins(0, 0, 0, 0) txt = _("Source") if sys.platform == 'darwin': source_label = QLabel(" " + txt) else: source_label = QLabel(txt) layout_edit.addWidget(source_label) self.source_combo = QComboBox(self) self.source_combo.addItems([_("Console"), _("Editor")]) self.source_combo.currentIndexChanged.connect(self.source_changed) if (not programs.is_module_installed('rope') and not programs.is_module_installed('jedi', '>=0.11.0')): self.source_combo.hide() source_label.hide() layout_edit.addWidget(self.source_combo) layout_edit.addSpacing(10) layout_edit.addWidget(QLabel(_("Object"))) self.combo = ObjectComboBox(self) layout_edit.addWidget(self.combo) self.object_edit = QLineEdit(self) self.object_edit.setReadOnly(True) layout_edit.addWidget(self.object_edit) self.combo.setMaxCount(self.get_option('max_history_entries')) self.combo.addItems(self.load_history()) self.combo.setItemText(0, '') self.combo.valid.connect(self.force_refresh) # Plain text docstring option self.docstring = True self.rich_help = self.get_option('rich_mode', True) self.plain_text_action = create_action(self, _("Plain Text"), toggled=self.toggle_plain_text) # Source code option self.show_source_action = create_action( self, _("Show Source"), toggled=self.toggle_show_source) # Rich text option self.rich_text_action = create_action(self, _("Rich Text"), toggled=self.toggle_rich_text) # Add the help actions to an exclusive QActionGroup help_actions = QActionGroup(self) help_actions.setExclusive(True) help_actions.addAction(self.plain_text_action) help_actions.addAction(self.rich_text_action) # Automatic import option self.auto_import_action = create_action( self, _("Automatic import"), toggled=self.toggle_auto_import) auto_import_state = self.get_option('automatic_import') self.auto_import_action.setChecked(auto_import_state) # Lock checkbox self.locked_button = create_toolbutton(self, triggered=self.toggle_locked) layout_edit.addWidget(self.locked_button) self._update_lock_icon() # Option menu layout_edit.addWidget(self.options_button) if self.rich_help: self.switch_to_rich_text() else: self.switch_to_plain_text() self.plain_text_action.setChecked(not self.rich_help) self.rich_text_action.setChecked(self.rich_help) self.source_changed() # Main layout layout = create_plugin_layout(layout_edit) # we have two main widgets, but only one of them is shown at a time layout.addWidget(self.plain_text) layout.addWidget(self.rich_text) self.setLayout(layout) # Add worker thread for handling rich text rendering self._sphinx_thread = SphinxThread(html_text_no_doc=warning( self.no_doc_string, css_path=self.css_path), css_path=self.css_path) self._sphinx_thread.html_ready.connect( self._on_sphinx_thread_html_ready) self._sphinx_thread.error_msg.connect(self._on_sphinx_thread_error_msg) # Handle internal and external links view = self.rich_text.webview if not WEBENGINE: view.page().setLinkDelegationPolicy( QWebEnginePage.DelegateAllLinks) view.linkClicked.connect(self.handle_link_clicks) self._starting_up = True
class EditGeometryProperties(PyDialog): force = True def __init__(self, data, win_parent=None): """ +------------------+ | Edit Actor Props | +------------------+------+ | Name1 | | Name2 | | Name3 | | Name4 | | | | Active_Name main | | Color box | | Line_Width 2 | | Point_Size 2 | | Bar_Scale 2 | | Opacity 0.5 | | Show/Hide | | | | Apply OK Cancel | +-------------------------+ """ PyDialog.__init__(self, data, win_parent) self.set_font_size(data['font_size']) del self.out_data['font_size'] self.setWindowTitle('Edit Geometry Properties') self.allow_update = True #default #self.win_parent = win_parent #self.out_data = data self.keys = sorted(data.keys()) self.keys = data.keys() keys = self.keys items = list(keys) #nrows = len(keys) active_key = 'main' if 'main' not in items: active_key = items[0] self.active_key = active_key header_labels = ['Groups'] table_model = Model(items, header_labels, self) view = SingleChoiceQTableView(self) #Call your custom QTableView here view.setModel(table_model) #if qt_version == 4: #view.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) self.table = view #self.opacity_edit.valueChanged.connect(self.on_opacity) #mListWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(itemClicked(QListWidgetItem*))); #self.table.itemClicked.connect(self.table.mouseDoubleClickEvent) actor_obj = data[self.active_key] if isinstance(actor_obj, CoordProperties): opacity = 1.0 representation = 'coord' show = actor_obj.is_visible color = None line_width = 0 point_size = 0 bar_scale = 0 name = 'Coord' else: name = actor_obj.name line_width = actor_obj.line_width point_size = actor_obj.point_size bar_scale = actor_obj.bar_scale opacity = actor_obj.opacity color = actor_obj.color show = actor_obj.is_visible representation = actor_obj.representation self.representation = representation # table header = self.table.horizontalHeader() header.setStretchLastSection(True) self._default_is_apply = False self.name = QLabel("Name:") self.name_edit = QLineEdit(str(name)) self.name_edit.setDisabled(True) self.color = QLabel("Color:") self.color_edit = QPushButton() #self.color_edit.setFlat(True) if color is not None: qcolor = QtGui.QColor() qcolor.setRgb(*color) #print('color =%s' % str(color)) palette = QtGui.QPalette( self.color_edit.palette()) # make a copy of the palette #palette.setColor(QtGui.QPalette.Active, QtGui.QPalette.Base, \ #qcolor) palette.setColor(QtGui.QPalette.Background, QtGui.QColor('blue')) # ButtonText self.color_edit.setPalette(palette) self.color_edit.setStyleSheet( "QPushButton {" "background-color: rgb(%s, %s, %s);" % tuple(color) + #"border:1px solid rgb(255, 170, 255); " "}") self.representation_label = QLabel('Representation:') self.checkbox_wire = QCheckBox('Wireframe') self.checkbox_surf = QCheckBox('Surface/Solid') #print('representation = %s' % self.representation) #self.check_point = QCheckBox() self.use_slider = True self.is_opacity_edit_active = False self.is_opacity_edit_slider_active = False self.is_line_width_edit_active = False self.is_line_width_edit_slider_active = False self.is_point_size_edit_active = False self.is_point_size_edit_slider_active = False self.is_bar_scale_edit_active = False self.is_bar_scale_edit_slider_active = False self.opacity = QLabel("Opacity:") self.opacity_edit = QDoubleSpinBox(self) self.opacity_edit.setRange(0.1, 1.0) self.opacity_edit.setDecimals(1) self.opacity_edit.setSingleStep(0.1) self.opacity_edit.setValue(opacity) if self.use_slider: self.opacity_slider_edit = QSlider(QtCore.Qt.Horizontal) self.opacity_slider_edit.setRange(1, 10) self.opacity_slider_edit.setValue(opacity * 10) self.opacity_slider_edit.setTickInterval(1) self.opacity_slider_edit.setTickPosition(QSlider.TicksBelow) self.line_width = QLabel("Line Width:") self.line_width_edit = QSpinBox(self) self.line_width_edit.setRange(1, MAX_LINE_WIDTH) self.line_width_edit.setSingleStep(1) self.line_width_edit.setValue(line_width) if self.use_slider: self.line_width_slider_edit = QSlider(QtCore.Qt.Horizontal) self.line_width_slider_edit.setRange(1, MAX_LINE_WIDTH) self.line_width_slider_edit.setValue(line_width) self.line_width_slider_edit.setTickInterval(1) self.line_width_slider_edit.setTickPosition(QSlider.TicksBelow) if self.representation in ['point', 'surface']: self.line_width.setEnabled(False) self.line_width_edit.setEnabled(False) self.line_width_slider_edit.setEnabled(False) self.point_size = QLabel("Point Size:") self.point_size_edit = QSpinBox(self) self.point_size_edit.setRange(1, MAX_POINT_SIZE) self.point_size_edit.setSingleStep(1) self.point_size_edit.setValue(point_size) self.point_size.setVisible(False) self.point_size_edit.setVisible(False) if self.use_slider: self.point_size_slider_edit = QSlider(QtCore.Qt.Horizontal) self.point_size_slider_edit.setRange(1, MAX_POINT_SIZE) self.point_size_slider_edit.setValue(point_size) self.point_size_slider_edit.setTickInterval(1) self.point_size_slider_edit.setTickPosition(QSlider.TicksBelow) self.point_size_slider_edit.setVisible(False) if self.representation in ['wire', 'surface']: self.point_size.setEnabled(False) self.point_size_edit.setEnabled(False) if self.use_slider: self.point_size_slider_edit.setEnabled(False) self.bar_scale = QLabel("Bar Scale:") self.bar_scale_edit = QDoubleSpinBox(self) #self.bar_scale_edit.setRange(0.01, 1.0) # was 0.1 #self.bar_scale_edit.setRange(0.05, 5.0) self.bar_scale_edit.setDecimals(1) #self.bar_scale_edit.setSingleStep(bar_scale / 10.) self.bar_scale_edit.setSingleStep(0.1) self.bar_scale_edit.setValue(bar_scale) #if self.use_slider: #self.bar_scale_slider_edit = QSlider(QtCore.Qt.Horizontal) #self.bar_scale_slider_edit.setRange(1, 100) # 1/0.05 = 100/5.0 #self.bar_scale_slider_edit.setValue(opacity * 0.05) #self.bar_scale_slider_edit.setTickInterval(10) #self.bar_scale_slider_edit.setTickPosition(QSlider.TicksBelow) if self.representation != 'bar': self.bar_scale.setEnabled(False) self.bar_scale_edit.setEnabled(False) self.bar_scale.setVisible(False) self.bar_scale_edit.setVisible(False) #self.bar_scale_slider_edit.setVisible(False) #self.bar_scale_slider_edit.setEnabled(False) # show/hide self.checkbox_show = QCheckBox("Show") self.checkbox_hide = QCheckBox("Hide") self.checkbox_show.setChecked(show) self.checkbox_hide.setChecked(not show) if name == 'main': self.color.setEnabled(False) self.color_edit.setEnabled(False) self.point_size.setEnabled(False) self.point_size_edit.setEnabled(False) if self.use_slider: self.point_size_slider_edit.setEnabled(False) self.cancel_button = QPushButton("Close") self.create_layout() self.set_connections() if isinstance(actor_obj, CoordProperties): self.color_edit.hide() self.color.hide() self.opacity.hide() self.opacity_edit.hide() self.opacity_slider_edit.hide() self.line_width.hide() self.line_width_edit.hide() self.line_width_slider_edit.hide() def on_delete(self, irow): """deletes an actor based on the row number""" if irow == 0: # main return nkeys = len(self.keys) if nkeys in [0, 1]: return name = self.keys[irow] nrows = nkeys - 1 self.keys.pop(irow) header_labels = ['Groups'] table_model = Model(self.keys, header_labels, self) self.table.setModel(table_model) if len(self.keys) == 0: self.update() self.set_as_null() return if irow == nrows: irow -= 1 new_name = self.keys[irow] self.update_active_name(new_name) if self.is_gui: self.win_parent.delete_actor(name) def set_as_null(self): """sets the null case""" self.name.setVisible(False) self.name_edit.setVisible(False) self.color.setVisible(False) self.color_edit.setVisible(False) self.line_width.setVisible(False) self.line_width_edit.setVisible(False) self.point_size.setVisible(False) self.point_size_edit.setVisible(False) self.bar_scale.setVisible(False) self.bar_scale_edit.setVisible(False) self.opacity.setVisible(False) self.opacity_edit.setVisible(False) self.opacity_slider_edit.setVisible(False) self.point_size_slider_edit.setVisible(False) self.line_width_slider_edit.setVisible(False) self.checkbox_show.setVisible(False) self.checkbox_hide.setVisible(False) def on_update_geometry_properties_window(self, data): """Not Implemented""" return #new_keys = sorted(data.keys()) #if self.active_key in new_keys: #i = new_keys.index(self.active_key) #else: #i = 0 #self.table.update_data(new_keys) #self.out_data = data #self.update_active_key(i) def update_active_key(self, index): """ Parameters ---------- index : PyQt4.QtCore.QModelIndex the index of the list Internal Parameters ------------------- name : str the name of obj obj : CoordProperties, AltGeometry the storage object for things like line_width, point_size, etc. """ name = str(index.data()) #print('name = %r' % name) #i = self.keys.index(self.active_key) self.update_active_name(name) def update_active_name(self, name): self.active_key = name self.name_edit.setText(name) obj = self.out_data[name] if isinstance(obj, CoordProperties): opacity = 1.0 representation = 'coord' is_visible = obj.is_visible elif isinstance(obj, AltGeometry): line_width = obj.line_width point_size = obj.point_size bar_scale = obj.bar_scale opacity = obj.opacity representation = obj.representation is_visible = obj.is_visible self.color_edit.setStyleSheet( "QPushButton {" "background-color: rgb(%s, %s, %s);" % tuple(obj.color) + #"border:1px solid rgb(255, 170, 255); " "}") self.allow_update = False self.force = False self.line_width_edit.setValue(line_width) self.point_size_edit.setValue(point_size) self.bar_scale_edit.setValue(bar_scale) self.force = True self.allow_update = True else: raise NotImplementedError(obj) #allowed_representations = [ #'main', 'surface', 'coord', 'toggle', 'wire', 'point', 'bar'] if self.representation != representation: self.representation = representation #if representation not in allowed_representations: #msg = 'name=%r; representation=%r is invalid\nrepresentations=%r' % ( #name, representation, allowed_representations) if self.representation == 'coord': self.color.setVisible(False) self.color_edit.setVisible(False) self.line_width.setVisible(False) self.line_width_edit.setVisible(False) self.point_size.setVisible(False) self.point_size_edit.setVisible(False) self.bar_scale.setVisible(False) self.bar_scale_edit.setVisible(False) self.opacity.setVisible(False) self.opacity_edit.setVisible(False) if self.use_slider: self.opacity_slider_edit.setVisible(False) self.point_size_slider_edit.setVisible(False) self.line_width_slider_edit.setVisible(False) #self.bar_scale_slider_edit.setVisible(False) else: self.color.setVisible(True) self.color_edit.setVisible(True) self.line_width.setVisible(True) self.line_width_edit.setVisible(True) self.point_size.setVisible(True) self.point_size_edit.setVisible(True) self.bar_scale.setVisible(True) #self.bar_scale_edit.setVisible(True) self.opacity.setVisible(True) self.opacity_edit.setVisible(True) if self.use_slider: self.opacity_slider_edit.setVisible(True) self.line_width_slider_edit.setVisible(True) self.point_size_slider_edit.setVisible(True) #self.bar_scale_slider_edit.setVisible(True) if name == 'main': self.color.setEnabled(False) self.color_edit.setEnabled(False) self.point_size.setEnabled(False) self.point_size_edit.setEnabled(False) self.line_width.setEnabled(True) self.line_width_edit.setEnabled(True) self.bar_scale.setEnabled(False) self.bar_scale_edit.setEnabled(False) show_points = False show_line_width = True show_bar_scale = False if self.use_slider: self.line_width_slider_edit.setEnabled(True) #self.bar_scale_slider_edit.setVisible(False) else: self.color.setEnabled(True) self.color_edit.setEnabled(True) show_points = False if self.representation in ['point', 'wire+point']: show_points = True show_line_width = False if self.representation in [ 'wire', 'wire+point', 'wire+surf', 'bar', 'toggle' ]: show_line_width = True if representation == 'bar': show_bar_scale = True else: show_bar_scale = False #self.bar_scale_button.setVisible(show_bar_scale) #self.bar_scale_edit.setSingleStep(bar_scale / 10.) #if self.use_slider: #self.bar_scale_slider_edit.setEnabled(False) self.point_size.setEnabled(show_points) self.point_size_edit.setEnabled(show_points) self.point_size.setVisible(show_points) self.point_size_edit.setVisible(show_points) self.line_width.setEnabled(show_line_width) self.line_width_edit.setEnabled(show_line_width) self.bar_scale.setEnabled(show_bar_scale) self.bar_scale_edit.setEnabled(show_bar_scale) self.bar_scale.setVisible(show_bar_scale) self.bar_scale_edit.setVisible(show_bar_scale) if self.use_slider: self.point_size_slider_edit.setEnabled(show_points) self.point_size_slider_edit.setVisible(show_points) self.line_width_slider_edit.setEnabled(show_line_width) #if self.representation in ['wire', 'surface']: self.opacity_edit.setValue(opacity) #if self.use_slider: #self.opacity_slider_edit.setValue(opacity*10) self.checkbox_show.setChecked(is_visible) self.checkbox_hide.setChecked(not is_visible) passed = self.on_validate() #self.on_apply(force=True) # TODO: was turned on...do I want this??? #self.allow_update = True def create_layout(self): ok_cancel_box = QHBoxLayout() ok_cancel_box.addWidget(self.cancel_button) grid = QGridLayout() irow = 0 grid.addWidget(self.name, irow, 0) grid.addWidget(self.name_edit, irow, 1) irow += 1 grid.addWidget(self.color, irow, 0) grid.addWidget(self.color_edit, irow, 1) irow += 1 grid.addWidget(self.opacity, irow, 0) if self.use_slider: grid.addWidget(self.opacity_edit, irow, 2) grid.addWidget(self.opacity_slider_edit, irow, 1) else: grid.addWidget(self.opacity_edit, irow, 1) irow += 1 grid.addWidget(self.line_width, irow, 0) if self.use_slider: grid.addWidget(self.line_width_edit, irow, 2) grid.addWidget(self.line_width_slider_edit, irow, 1) else: grid.addWidget(self.line_width_edit, irow, 1) irow += 1 grid.addWidget(self.point_size, irow, 0) if self.use_slider: grid.addWidget(self.point_size_edit, irow, 2) grid.addWidget(self.point_size_slider_edit, irow, 1) else: grid.addWidget(self.point_size_edit, irow, 1) irow += 1 grid.addWidget(self.bar_scale, irow, 0) if self.use_slider and 0: grid.addWidget(self.bar_scale_edit, irow, 2) grid.addWidget(self.bar_scale_slider_edit, irow, 1) else: grid.addWidget(self.bar_scale_edit, irow, 1) irow += 1 wire_surf_checkboxes = QButtonGroup(self) wire_surf_checkboxes.addButton(self.checkbox_wire) wire_surf_checkboxes.addButton(self.checkbox_surf) checkboxs = QButtonGroup(self) checkboxs.addButton(self.checkbox_show) checkboxs.addButton(self.checkbox_hide) vbox = QVBoxLayout() vbox.addWidget(self.table, stretch=1) vbox.addLayout(grid) vbox1 = QVBoxLayout() vbox1.addWidget(self.checkbox_wire) vbox1.addWidget(self.checkbox_surf) vbox2 = QVBoxLayout() vbox2.addWidget(self.checkbox_show) vbox2.addWidget(self.checkbox_hide) #vbox.addLayout(vbox1) vbox.addLayout(vbox2) vbox.addStretch() #vbox.addWidget(self.check_apply) vbox.addLayout(ok_cancel_box) self.setLayout(vbox) def set_connections(self): self.opacity_edit.valueChanged.connect(self.on_opacity) self.line_width_edit.valueChanged.connect(self.on_line_width) self.point_size_edit.valueChanged.connect(self.on_point_size) self.bar_scale_edit.valueChanged.connect(self.on_bar_scale) if self.use_slider: self.opacity_slider_edit.valueChanged.connect( self.on_opacity_slider) self.line_width_slider_edit.valueChanged.connect( self.on_line_width_slider) self.point_size_slider_edit.valueChanged.connect( self.on_point_size_slider) #self.bar_scale_slider_edit.valueChanged.connect(self.on_bar_scale_slider) # self.connect(self.opacity_edit, QtCore.SIGNAL('clicked()'), self.on_opacity) # self.connect(self.line_width, QtCore.SIGNAL('clicked()'), self.on_line_width) # self.connect(self.point_size, QtCore.SIGNAL('clicked()'), self.on_point_size) if qt_version == 4: self.connect(self, QtCore.SIGNAL('triggered()'), self.closeEvent) self.color_edit.clicked.connect(self.on_color) self.checkbox_show.clicked.connect(self.on_show) self.checkbox_hide.clicked.connect(self.on_hide) self.cancel_button.clicked.connect(self.on_cancel) # closeEvent def keyPressEvent(self, event): if event.key() == QtCore.Qt.Key_Escape: self.close() def closeEvent(self, event): self.on_cancel() def on_color(self): """called when the user clicks on the color box""" name = self.active_key obj = self.out_data[name] rgb_color_ints = obj.color msg = name col = QColorDialog.getColor(QtGui.QColor(*rgb_color_ints), self, "Choose a %s color" % msg) if col.isValid(): color_float = col.getRgbF()[:3] obj.color = color_float color_int = [int(colori * 255) for colori in color_float] self.color_edit.setStyleSheet( "QPushButton {" "background-color: rgb(%s, %s, %s);" % tuple(color_int) + #"border:1px solid rgb(255, 170, 255); " "}") self.on_apply(force=self.force) #print(self.allow_update) def on_show(self): """shows the actor""" name = self.active_key is_checked = self.checkbox_show.isChecked() self.out_data[name].is_visible = is_checked self.on_apply(force=self.force) def on_hide(self): """hides the actor""" name = self.active_key is_checked = self.checkbox_hide.isChecked() self.out_data[name].is_visible = not is_checked self.on_apply(force=self.force) def on_line_width(self): """increases/decreases the wireframe (for solid bodies) or the bar thickness""" self.is_line_width_edit_active = True name = self.active_key line_width = self.line_width_edit.value() self.out_data[name].line_width = line_width if not self.is_line_width_edit_slider_active: if self.use_slider: self.line_width_slider_edit.setValue(line_width) self.is_line_width_edit_active = False self.on_apply(force=self.force) self.is_line_width_edit_active = False def on_line_width_slider(self): """increases/decreases the wireframe (for solid bodies) or the bar thickness""" self.is_line_width_edit_slider_active = True #name = self.active_key line_width = self.line_width_slider_edit.value() if not self.is_line_width_edit_active: self.line_width_edit.setValue(line_width) self.is_line_width_edit_slider_active = False def on_point_size(self): """increases/decreases the point size""" self.is_point_size_edit_active = True name = self.active_key point_size = self.point_size_edit.value() self.out_data[name].point_size = point_size if not self.is_point_size_edit_slider_active: if self.use_slider: self.point_size_slider_edit.setValue(point_size) self.is_point_size_edit_active = False self.on_apply(force=self.force) self.is_point_size_edit_active = False def on_point_size_slider(self): """increases/decreases the point size""" self.is_point_size_edit_slider_active = True #name = self.active_key point_size = self.point_size_slider_edit.value() if not self.is_point_size_edit_active: self.point_size_edit.setValue(point_size) self.is_point_size_edit_slider_active = False def on_bar_scale(self): """ Vectors start at some xyz coordinate and can increase in length. Increases/decreases the length scale factor. """ self.is_bar_scale_edit_active = True name = self.active_key float_bar_scale = self.bar_scale_edit.value() self.out_data[name].bar_scale = float_bar_scale if not self.is_bar_scale_edit_slider_active: #int_bar_scale = int(round(float_bar_scale * 20, 0)) #if self.use_slider: #self.bar_scale_slider_edit.setValue(int_bar_scale) self.is_bar_scale_edit_active = False self.on_apply(force=self.force) self.is_bar_scale_edit_active = False def on_bar_scale_slider(self): """ Vectors start at some xyz coordinate and can increase in length. Increases/decreases the length scale factor. """ self.is_bar_scale_edit_slider_active = True #name = self.active_key int_bar_scale = self.bar_scale_slider_edit.value() if not self.is_bar_scale_edit_active: float_bar_scale = int_bar_scale / 20. self.bar_scale_edit.setValue(float_bar_scale) self.is_bar_scale_edit_slider_active = False def on_opacity(self): """ opacity = 1.0 (solid/opaque) opacity = 0.0 (invisible) """ self.is_opacity_edit_active = True name = self.active_key float_opacity = self.opacity_edit.value() self.out_data[name].opacity = float_opacity if not self.is_opacity_edit_slider_active: int_opacity = int(round(float_opacity * 10, 0)) if self.use_slider: self.opacity_slider_edit.setValue(int_opacity) self.is_opacity_edit_active = False self.on_apply(force=self.force) self.is_opacity_edit_active = False def on_opacity_slider(self): """ opacity = 1.0 (solid/opaque) opacity = 0.0 (invisible) """ self.is_opacity_edit_slider_active = True #name = self.active_key int_opacity = self.opacity_slider_edit.value() if not self.is_opacity_edit_active: float_opacity = int_opacity / 10. self.opacity_edit.setValue(float_opacity) self.is_opacity_edit_slider_active = False def on_validate(self): self.out_data['clicked_ok'] = True self.out_data['clicked_cancel'] = False old_obj = self.out_data[self.active_key] old_obj.line_width = self.line_width_edit.value() old_obj.point_size = self.point_size_edit.value() old_obj.bar_scale = self.bar_scale_edit.value() old_obj.opacity = self.opacity_edit.value() #old_obj.color = self.color_edit old_obj.is_visible = self.checkbox_show.isChecked() return True #name_value, flag0 = self.check_name(self.name_edit) #ox_value, flag1 = check_float(self.transparency_edit) #if flag0 and flag1: #self.out_data['clicked_ok'] = True #return True #return False @property def is_gui(self): return hasattr(self.win_parent, 'on_update_geometry_properties') def on_apply(self, force=False): passed = self.on_validate() #print("passed=%s force=%s allow=%s" % (passed, force, self.allow_update)) if (passed or force) and self.allow_update and self.is_gui: #print('obj = %s' % self.out_data[self.active_key]) self.win_parent.on_update_geometry_properties(self.out_data, name=self.active_key) return passed def on_cancel(self): passed = self.on_apply(force=True) if passed: self.close()
class LegendPropertiesWindow(PyDialog): """ +-------------------+ | Legend Properties | +-----------------------+ | Title ______ Default | | Min ______ Default | | Max ______ Default | | Format ______ Default | | Scale ______ Default | | Phase ______ Default | | Number of Colors ____ | | Number of Labels ____ | | Label Size ____ | (TODO) | ColorMap ____ | (TODO) | | | x Min/Max (Blue->Red) | | o Max/Min (Red->Blue) | | | | x Vertical/Horizontal | | x Show/Hide | | | | Animate | | Apply OK Cancel | +-----------------------+ """ def __init__(self, data, win_parent=None): PyDialog.__init__(self, data, win_parent) self.is_gui = win_parent is not None self._updated_legend = False self.external_call = True #if win_parent is None: self._icase_fringe = data['icase_fringe'] self._icase_disp = data['icase_disp'] self._icase_vector = data['icase_vector'] #else: #self._icase_fringe = data['icase'] #self._icase_disp = data['icase'] #self._icase_vector = data['icase'] self._default_icase_fringe = self._icase_fringe self._default_icase_disp = self._icase_disp self._default_icase_vector = self._icase_vector #print('*icase_fringe=%s icase_disp=%s icase_vector=%s' % ( #self._default_icase_fringe, self._default_icase_disp, self._default_icase_vector)) self._default_name = data['name'] self._default_min = data['min_value'] self._default_max = data['max_value'] self._default_scale = data['default_scale'] self._scale = data['scale'] #if win_parent is None: self._default_arrow_scale = data['default_arrow_scale'] self._arrow_scale = data['arrow_scale'] #else: #self._default_arrow_scale = data['default_scale'] #self._arrow_scale = data['scale'] self._default_phase = data['default_phase'] self._phase = data['phase'] self._default_format = data['default_format'] self._format = data['format'] self._default_labelsize = data['default_labelsize'] self._labelsize = data['labelsize'] self._default_nlabels = data['default_nlabels'] self._nlabels = data['nlabels'] self._default_ncolors = data['default_ncolors'] self._ncolors = data['ncolors'] self._default_colormap = data['default_colormap'] self._colormap = data['colormap'] self._default_is_low_to_high = data['is_low_to_high'] self._default_is_discrete = data['is_discrete'] self._default_is_horizontal = data['is_horizontal'] self._default_is_shown = data['is_shown'] self._is_fringe = data['is_fringe'] self._update_defaults_to_blank() self.setWindowTitle('Legend Properties') self.create_widgets() self.create_layout() self.set_connections() self.set_font_size(data['font_size']) def _update_defaults_to_blank(self): """Changes the default (None) to a blank string""" if self._default_colormap is None: self._default_colormap = 'jet' if self._default_labelsize is None: self._default_labelsize = '' if self._default_ncolors is None: self._default_ncolors = '' if self._default_nlabels is None: self._default_nlabels = '' if self._colormap is None: self._colormap = 'jet' if self._labelsize is None: self._labelsize = '' if self._ncolors is None: self._ncolors = '' if self._nlabels is None: self._nlabels = '' def update_legend(self, icase_fringe, icase_disp, icase_vector, name, min_value, max_value, data_format, nlabels, labelsize, ncolors, colormap, is_fringe, scale, phase, arrow_scale, default_title, default_min_value, default_max_value, default_data_format, default_nlabels, default_labelsize, default_ncolors, default_colormap, default_scale, default_phase, default_arrow_scale, font_size=8, external_call=False): """ We need to update the legend if there's been a result change request """ self.external_call = external_call self.set_font_size(font_size) update_fringe = False update_disp = False update_vector = False #print('update_legend; fringe=%s disp=%s vector=%s' % ( #icase_fringe, icase_disp, icase_vector)) #print('update_legend; default: fringe=%s disp=%s vector=%s' % ( # self._default_icase_fringe, self._default_icase_disp, self._default_icase_vector)) if icase_fringe != self._default_icase_fringe: self._icase_fringe = icase_fringe self._default_icase_fringe = icase_fringe update_fringe = True #is_fringe = icase_fringe is not None is_disp = icase_disp is not None is_vector = icase_vector is not None if icase_disp != self._default_icase_disp: assert isinstance( scale, float_types), 'scale=%r type=%s' % (scale, type(scale)) #assert isinstance(default_scale, float), 'default_scale=%r' % default_scale self._icase_disp = icase_disp self._default_icase_disp = icase_disp self._default_scale = default_scale self._default_phase = default_phase update_disp = True if icase_vector != self._default_icase_vector: assert isinstance( arrow_scale, float_types), 'arrow_scale=%r type=%s' % (arrow_scale, type(scale)) #assert isinstance(default_arrow_scale, float), 'default_arrow_scale=%r' % default_arrow_scale self._icase_vector = icase_vector self._default_icase_vector = icase_vector self._default_arrow_scale = default_arrow_scale update_vector = True #print('*update_legend; default: fringe=%s disp=%s vector=%s' % ( # self._default_icase_fringe, self._default_icase_disp, self._default_icase_vector)) #print('update_fringe=%s update_disp=%s update_vector=%s' % ( # update_fringe, update_disp, update_vector)) #print('is_fringe=%s is_disp=%s is_vector=%s' % ( # is_fringe, is_disp, is_vector)) if update_fringe: self._icase_fringe = icase_fringe self._default_icase_fringe = icase_fringe self._default_name = default_title self._default_min = default_min_value self._default_max = default_max_value self._default_format = default_data_format #self._default_is_low_to_high = is_low_to_high self._default_is_discrete = True #self._default_is_horizontal = is_horizontal_scalar_bar self._default_nlabels = default_nlabels self._default_labelsize = default_labelsize self._default_ncolors = default_ncolors self._default_colormap = default_colormap self._is_fringe = is_fringe if colormap is None: colormap = 'jet' if labelsize is None: labelsize = '' if ncolors is None: ncolors = '' if nlabels is None: nlabels = '' self._update_defaults_to_blank() #----------------------------------------------------------------------- update = update_fringe or update_disp or update_vector if update: #self.scale_label.setEnabled(is_disp) #self.scale_edit.setEnabled(is_disp) #self.scale_button.setEnabled(is_disp) self.scale_label.setVisible(is_disp) self.scale_edit.setVisible(is_disp) self.scale_button.setVisible(is_disp) is_complex_disp = self._default_phase is not None self.phase_label.setVisible(is_complex_disp) self.phase_edit.setVisible(is_complex_disp) self.phase_button.setVisible(is_complex_disp) self._scale = set_cell_to_blank_if_value_is_none( self.scale_edit, scale) self._phase = set_cell_to_blank_if_value_is_none( self.phase_edit, phase) if self._default_icase_disp is None: # or self._default_icase_vector is None: self.animate_button.setEnabled(False) self.animate_button.setToolTip(ANIMATE_TOOLTIP_OFF) else: self.animate_button.setEnabled(True) self.animate_button.setToolTip(ANIMATE_TOOLTIP_ON) #----------------------------------------------------------------------- if update: #self.arrow_scale_label.setEnabled(is_vector) #self.arrow_scale_edit.setEnabled(is_vector) #self.arrow_scale_button.setEnabled(is_vector) self.arrow_scale_label.setVisible(is_vector) self.arrow_scale_edit.setVisible(is_vector) self.arrow_scale_button.setVisible(is_vector) self._arrow_scale = set_cell_to_blank_if_value_is_none( self.arrow_scale_edit, arrow_scale) #----------------------------------------------------------------------- if update_fringe: #self.on_default_name() #self.on_default_min() #self.on_default_max() #self.on_default_format() #self.on_default_scale() # reset defaults self.name_edit.setText(name) self.name_edit.setStyleSheet("QLineEdit{background: white;}") self.min_edit.setText(str(min_value)) self.min_edit.setStyleSheet("QLineEdit{background: white;}") self.max_edit.setText(str(max_value)) self.max_edit.setStyleSheet("QLineEdit{background: white;}") self.format_edit.setText(str(data_format)) self.format_edit.setStyleSheet("QLineEdit{background: white;}") self.nlabels_edit.setText(str(nlabels)) self.nlabels_edit.setStyleSheet("QLineEdit{background: white;}") self.labelsize_edit.setText(str(labelsize)) self.labelsize_edit.setStyleSheet("QLineEdit{background: white;}") self.ncolors_edit.setText(str(ncolors)) self.ncolors_edit.setStyleSheet("QLineEdit{background: white;}") self.colormap_edit.setCurrentIndex( colormap_keys.index(str(colormap))) self._set_legend_fringe(self._is_fringe) if update: self.on_apply() self.external_call = True #print('---------------------------------') def clear_disp(self): """hides dispacement blocks""" self._icase_disp = None self._default_icase_disp = None self.scale_label.setVisible(False) self.scale_edit.setVisible(False) self.scale_button.setVisible(False) self.phase_label.setVisible(False) self.phase_edit.setVisible(False) self.phase_button.setVisible(False) def clear_vector(self): """hides vector blocks""" self._icase_vector = None self._default_icase_vector = None self.arrow_scale_label.setVisible(False) self.arrow_scale_edit.setVisible(False) self.arrow_scale_button.setVisible(False) def clear(self): """hides fringe, displacemnt, and vector blocks""" self._icase_fringe = None self._default_icase_fringe = None self._set_legend_fringe(False) self.clear_disp() self.clear_vector() def _set_legend_fringe(self, is_fringe): """ Show/hide buttons if we dont have a result. This is used for normals. A result can still exist (i.e., icase_fringe is not None). """ # lots of hacking for the Normal vectors self._is_fringe = is_fringe #self._default_icase_fringe = None enable = True if not is_fringe: enable = False show_name = self._icase_fringe is not None self.name_label.setVisible(show_name) self.name_edit.setVisible(show_name) self.name_button.setVisible(show_name) self.max_label.setVisible(enable) self.min_label.setVisible(enable) self.max_edit.setVisible(enable) self.min_edit.setVisible(enable) self.max_button.setVisible(enable) self.min_button.setVisible(enable) self.show_radio.setVisible(enable) self.hide_radio.setVisible(enable) self.low_to_high_radio.setVisible(enable) self.high_to_low_radio.setVisible(enable) self.format_label.setVisible(enable) self.format_edit.setVisible(enable) self.format_edit.setVisible(enable) self.format_button.setVisible(enable) self.nlabels_label.setVisible(enable) self.nlabels_edit.setVisible(enable) self.nlabels_button.setVisible(enable) self.ncolors_label.setVisible(enable) self.ncolors_edit.setVisible(enable) self.ncolors_button.setVisible(enable) self.grid2_title.setVisible(enable) self.vertical_radio.setVisible(enable) self.horizontal_radio.setVisible(enable) self.colormap_label.setVisible(enable) self.colormap_edit.setVisible(enable) self.colormap_button.setVisible(enable) def create_widgets(self): """creates the menu objects""" # Name self.name_label = QLabel("Title:") self.name_edit = QLineEdit(str(self._default_name)) self.name_button = QPushButton("Default") # Min self.min_label = QLabel("Min:") self.min_edit = QLineEdit(str(self._default_min)) self.min_button = QPushButton("Default") # Max self.max_label = QLabel("Max:") self.max_edit = QLineEdit(str(self._default_max)) self.max_button = QPushButton("Default") #--------------------------------------- # Format self.format_label = QLabel("Format (e.g. %.3f, %g, %.6e):") self.format_edit = QLineEdit(str(self._format)) self.format_button = QPushButton("Default") #--------------------------------------- # Scale self.scale_label = QLabel("True Scale:") self.scale_edit = QLineEdit(str(self._scale)) self.scale_button = QPushButton("Default") if self._icase_disp is None: self.scale_label.setVisible(False) self.scale_edit.setVisible(False) self.scale_button.setVisible(False) # Phase self.phase_label = QLabel("Phase (deg):") self.phase_edit = QLineEdit(str(self._phase)) self.phase_button = QPushButton("Default") if self._icase_disp is None or self._default_phase is None: self.phase_label.setVisible(False) self.phase_edit.setVisible(False) self.phase_button.setVisible(False) self.phase_edit.setText('0.0') #--------------------------------------- self.arrow_scale_label = QLabel("Arrow Scale:") self.arrow_scale_edit = QLineEdit(str(self._arrow_scale)) self.arrow_scale_button = QPushButton("Default") if self._icase_vector is None: self.arrow_scale_label.setVisible(False) self.arrow_scale_edit.setVisible(False) self.arrow_scale_button.setVisible(False) #tip = QtGui.QToolTip() #tip.setTe #self.format_edit.toolTip(tip) #--------------------------------------- # nlabels self.nlabels_label = QLabel("Number of Labels:") self.nlabels_edit = QLineEdit(str(self._nlabels)) self.nlabels_button = QPushButton("Default") self.labelsize_label = QLabel("Label Size:") self.labelsize_edit = QLineEdit(str(self._labelsize)) self.labelsize_button = QPushButton("Default") self.ncolors_label = QLabel("Number of Colors:") self.ncolors_edit = QLineEdit(str(self._ncolors)) self.ncolors_button = QPushButton("Default") self.colormap_label = QLabel("Color Map:") self.colormap_edit = QComboBox(self) self.colormap_button = QPushButton("Default") for key in colormap_keys: self.colormap_edit.addItem(key) self.colormap_edit.setCurrentIndex(colormap_keys.index(self._colormap)) # -------------------------------------------------------------- # the header self.grid2_title = QLabel("Color Scale:") # red/blue or blue/red self.low_to_high_radio = QRadioButton('Low -> High') self.high_to_low_radio = QRadioButton('High -> Low') widget = QWidget(self) low_to_high_group = QButtonGroup(widget) low_to_high_group.addButton(self.low_to_high_radio) low_to_high_group.addButton(self.high_to_low_radio) self.low_to_high_radio.setChecked(self._default_is_low_to_high) self.high_to_low_radio.setChecked(not self._default_is_low_to_high) # horizontal / vertical self.horizontal_radio = QRadioButton("Horizontal") self.vertical_radio = QRadioButton("Vertical") widget = QWidget(self) horizontal_vertical_group = QButtonGroup(widget) horizontal_vertical_group.addButton(self.horizontal_radio) horizontal_vertical_group.addButton(self.vertical_radio) self.horizontal_radio.setChecked(self._default_is_horizontal) self.vertical_radio.setChecked(not self._default_is_horizontal) # on / off self.show_radio = QRadioButton("Show") self.hide_radio = QRadioButton("Hide") widget = QWidget(self) show_hide_group = QButtonGroup(widget) show_hide_group.addButton(self.show_radio) show_hide_group.addButton(self.hide_radio) self.show_radio.setChecked(self._default_is_shown) self.hide_radio.setChecked(not self._default_is_shown) # -------------------------------------------------------------- if self._icase_fringe is None: self.name_label.setVisible(False) self.name_edit.setVisible(False) self.name_button.setVisible(False) if not self._is_fringe: self.max_label.hide() self.min_label.hide() self.max_edit.hide() self.min_edit.hide() self.max_button.hide() self.min_button.hide() self.format_label.hide() self.format_edit.hide() self.format_button.hide() self.nlabels_label.hide() self.nlabels_edit.hide() self.nlabels_button.hide() self.ncolors_label.hide() self.ncolors_edit.hide() self.ncolors_button.hide() self.grid2_title.hide() self.vertical_radio.hide() self.horizontal_radio.hide() self.show_radio.hide() self.hide_radio.hide() self.low_to_high_radio.hide() self.high_to_low_radio.hide() self.colormap_label.hide() self.colormap_edit.hide() self.colormap_button.hide() self.animate_button = QPushButton('Create Animation') #self.advanced_button = QPushButton('Advanced') if self._default_icase_disp is None: # or self._default_icase_vector is None: self.animate_button.setEnabled(False) self.animate_button.setToolTip(ANIMATE_TOOLTIP_OFF) else: self.animate_button.setEnabled(True) self.animate_button.setToolTip(ANIMATE_TOOLTIP_ON) # closing self.apply_button = QPushButton("Apply") self.ok_button = QPushButton("OK") self.cancel_button = QPushButton("Cancel") def create_layout(self): """displays the menu objects""" grid = QGridLayout() grid.addWidget(self.name_label, 0, 0) grid.addWidget(self.name_edit, 0, 1) grid.addWidget(self.name_button, 0, 2) grid.addWidget(self.min_label, 1, 0) grid.addWidget(self.min_edit, 1, 1) grid.addWidget(self.min_button, 1, 2) grid.addWidget(self.max_label, 2, 0) grid.addWidget(self.max_edit, 2, 1) grid.addWidget(self.max_button, 2, 2) grid.addWidget(self.format_label, 3, 0) grid.addWidget(self.format_edit, 3, 1) grid.addWidget(self.format_button, 3, 2) grid.addWidget(self.scale_label, 4, 0) grid.addWidget(self.scale_edit, 4, 1) grid.addWidget(self.scale_button, 4, 2) grid.addWidget(self.phase_label, 6, 0) grid.addWidget(self.phase_edit, 6, 1) grid.addWidget(self.phase_button, 6, 2) grid.addWidget(self.arrow_scale_label, 5, 0) grid.addWidget(self.arrow_scale_edit, 5, 1) grid.addWidget(self.arrow_scale_button, 5, 2) grid.addWidget(self.nlabels_label, 7, 0) grid.addWidget(self.nlabels_edit, 7, 1) grid.addWidget(self.nlabels_button, 7, 2) #grid.addWidget(self.labelsize_label, 6, 0) #grid.addWidget(self.labelsize_edit, 6, 1) #grid.addWidget(self.labelsize_button, 6, 2) grid.addWidget(self.ncolors_label, 8, 0) grid.addWidget(self.ncolors_edit, 8, 1) grid.addWidget(self.ncolors_button, 8, 2) grid.addWidget(self.colormap_label, 9, 0) grid.addWidget(self.colormap_edit, 9, 1) grid.addWidget(self.colormap_button, 9, 2) ok_cancel_box = QHBoxLayout() ok_cancel_box.addWidget(self.apply_button) ok_cancel_box.addWidget(self.ok_button) ok_cancel_box.addWidget(self.cancel_button) grid2 = QGridLayout() grid2.addWidget(self.grid2_title, 0, 0) grid2.addWidget(self.low_to_high_radio, 1, 0) grid2.addWidget(self.high_to_low_radio, 2, 0) grid2.addWidget(self.vertical_radio, 1, 1) grid2.addWidget(self.horizontal_radio, 2, 1) grid2.addWidget(self.show_radio, 1, 2) grid2.addWidget(self.hide_radio, 2, 2) grid2.addWidget(self.animate_button, 3, 1) #grid2.setSpacing(0) vbox = QVBoxLayout() vbox.addLayout(grid) #vbox.addLayout(checkboxes) vbox.addLayout(grid2) vbox.addStretch() vbox.addLayout(ok_cancel_box) self.setLayout(vbox) def set_connections(self): """creates the actions for the buttons""" self.name_button.clicked.connect(self.on_default_name) self.min_button.clicked.connect(self.on_default_min) self.max_button.clicked.connect(self.on_default_max) self.format_button.clicked.connect(self.on_default_format) self.scale_button.clicked.connect(self.on_default_scale) self.arrow_scale_button.clicked.connect(self.on_default_arrow_scale) self.phase_button.clicked.connect(self.on_default_phase) self.nlabels_button.clicked.connect(self.on_default_nlabels) self.labelsize_button.clicked.connect(self.on_default_labelsize) self.ncolors_button.clicked.connect(self.on_default_ncolors) self.colormap_button.clicked.connect(self.on_default_colormap) self.animate_button.clicked.connect(self.on_animate) self.show_radio.clicked.connect(self.on_show_hide) self.hide_radio.clicked.connect(self.on_show_hide) self.apply_button.clicked.connect(self.on_apply) self.ok_button.clicked.connect(self.on_ok) self.cancel_button.clicked.connect(self.on_cancel) if qt_version == 4: self.connect(self, QtCore.SIGNAL('triggered()'), self.closeEvent) #self.colormap_edit.activated[str].connect(self.onActivated) #else: # closeEvent??? def set_font_size(self, font_size): """ Updates the font size of the objects Parameters ---------- font_size : int the font size """ if self.font_size == font_size: return self.font_size = font_size font = QFont() font.setPointSize(font_size) self.setFont(font) self.name_edit.setFont(font) self.min_edit.setFont(font) self.max_edit.setFont(font) self.format_edit.setFont(font) self.scale_edit.setFont(font) self.phase_edit.setFont(font) self.nlabels_edit.setFont(font) self.labelsize_edit.setFont(font) self.ncolors_edit.setFont(font) def on_animate(self): """opens the animation window""" name, flag0 = check_name(self.name_edit) if not flag0: return scale, flag1 = check_float(self.scale_edit) if not flag1: scale = self._scale data = { 'font_size': self.out_data['font_size'], 'icase_fringe': self._icase_fringe, 'icase_disp': self._icase_disp, 'icase_vector': self._icase_vector, 'name': name, 'time': 2, 'frames/sec': 30, 'resolution': 1, 'iframe': 0, 'scale': scale, 'default_scale': self._default_scale, 'arrow_scale': scale, 'default_arrow_scale': self._default_arrow_scale, 'is_scale': self._default_phase is None, 'phase': self._phase, 'default_phase': self._default_phase, 'dirname': os.path.abspath(os.getcwd()), 'clicked_ok': False, 'close': False, } self.win_parent.legend_obj.set_animation_window(data) def on_default_name(self): """action when user clicks 'Default' for name""" name = str(self._default_name) self.name_edit.setText(name) self.name_edit.setStyleSheet("QLineEdit{background: white;}") def on_default_min(self): """action when user clicks 'Default' for min value""" self.min_edit.setText(str(self._default_min)) self.min_edit.setStyleSheet("QLineEdit{background: white;}") def on_default_max(self): """action when user clicks 'Default' for max value""" self.max_edit.setText(str(self._default_max)) self.max_edit.setStyleSheet("QLineEdit{background: white;}") def on_default_format(self): """action when user clicks 'Default' for the number format""" self.format_edit.setText(str(self._default_format)) self.format_edit.setStyleSheet("QLineEdit{background: white;}") def on_default_scale(self): """action when user clicks 'Default' for scale factor""" self.scale_edit.setText(str(self._default_scale)) self.scale_edit.setStyleSheet("QLineEdit{background: white;}") def on_default_arrow_scale(self): """action when user clicks 'Default' for arrow_scale factor""" self.arrow_scale_edit.setText(str(self._default_arrow_scale)) self.arrow_scale_edit.setStyleSheet("QLineEdit{background: white;}") def on_default_phase(self): """action when user clicks 'Default' for phase angle""" self.phase_edit.setText(str(self._default_phase)) self.phase_edit.setStyleSheet("QLineEdit{background: white;}") def on_default_ncolors(self): """action when user clicks 'Default' for number of colors""" self.ncolors_edit.setText(str(self._default_ncolors)) self.ncolors_edit.setStyleSheet("QLineEdit{background: white;}") def on_default_colormap(self): """action when user clicks 'Default' for the color map""" self.colormap_edit.setCurrentIndex( colormap_keys.index(self._default_colormap)) def on_default_nlabels(self): """action when user clicks 'Default' for number of labels""" self.nlabels_edit.setStyleSheet("QLineEdit{background: white;}") self.nlabels_edit.setText(str(self._default_nlabels)) def on_default_labelsize(self): """action when user clicks 'Default' for number of labelsize""" self.labelsize_edit.setText(str(self._default_labelsize)) self.labelsize_edit.setStyleSheet("QLineEdit{background: white;}") def on_show_hide(self): """action when user clicks the 'Show/Hide' radio button""" #self.colormap_edit.setCurrentIndex(colormap_keys.index(self._default_colormap)) is_shown = self.show_radio.isChecked() self.nlabels_edit.setEnabled(is_shown) self.nlabels_button.setEnabled(is_shown) self.ncolors_edit.setEnabled(is_shown) self.ncolors_button.setEnabled(is_shown) self.high_to_low_radio.setEnabled(is_shown) self.low_to_high_radio.setEnabled(is_shown) self.colormap_edit.setEnabled(is_shown) self.colormap_button.setEnabled(is_shown) self.vertical_radio.setEnabled(is_shown) self.horizontal_radio.setEnabled(is_shown) def show_legend(self): """shows the legend""" self._set_legend(True) def hide_legend(self): """hides the legend""" self._set_legend(False) def _set_legend(self, is_shown): """shows/hides the legend""" self.show_radio.setChecked(is_shown) self.hide_radio.setChecked(not is_shown) #if self.is_gui: #self.win_parent.scalarBar.SetVisibility(is_shown) def on_validate(self): """checks to see if the ``on_apply`` method can be called""" show_name = self._icase_fringe is not None flag_name = True name_value = '' if show_name: name_value, flag_name = check_name(self.name_edit) flag_fringe = True min_value = max_value = format_value = nlabels = ncolors = labelsize = colormap = None if self._icase_fringe is not None: min_value, flag1 = check_float(self.min_edit) max_value, flag2 = check_float(self.max_edit) format_value, flag3 = check_format(self.format_edit) nlabels, flag4 = check_positive_int_or_blank(self.nlabels_edit) ncolors, flag5 = check_positive_int_or_blank(self.ncolors_edit) labelsize, flag6 = check_positive_int_or_blank(self.labelsize_edit) colormap = str(self.colormap_edit.currentText()) if 'i' in format_value: format_value = '%i' flag_fringe = all([flag1, flag2, flag3, flag4, flag5, flag6]) flag_disp = True scale = phase = None if self._icase_disp is not None: scale, flag1 = check_float(self.scale_edit) phase, flag2 = check_float(self.phase_edit) assert isinstance(scale, float), scale flag_disp = all([flag1, flag2]) flag_vector = True arrow_scale = None if self._icase_vector is not None: arrow_scale, flag_vector = check_float(self.arrow_scale_edit) if all([flag_name, flag_fringe, flag_disp, flag_vector]): self.out_data['name'] = name_value self.out_data['min_value'] = min_value self.out_data['max_value'] = max_value self.out_data['format'] = format_value self.out_data['scale'] = scale self.out_data['phase'] = phase self.out_data['arrow_scale'] = arrow_scale self.out_data['nlabels'] = nlabels self.out_data['ncolors'] = ncolors self.out_data['labelsize'] = labelsize self.out_data['colormap'] = colormap self.out_data['is_low_to_high'] = self.low_to_high_radio.isChecked( ) self.out_data['is_horizontal'] = self.horizontal_radio.isChecked() self.out_data['is_shown'] = self.show_radio.isChecked() self.out_data['clicked_ok'] = True self.out_data['close'] = True #print('self.out_data = ', self.out_data) #print("name = %r" % self.name_edit.text()) #print("min = %r" % self.min_edit.text()) #print("max = %r" % self.max_edit.text()) #print("format = %r" % self.format_edit.text()) return True return False def on_apply(self): """applies the current values""" passed = self.on_validate() if passed and self.external_call: self.win_parent.legend_obj._apply_legend(self.out_data) self.external_call = True return passed def on_ok(self): """applies the current values and closes the window""" passed = self.on_apply() if passed: self.close() #self.destroy() def on_cancel(self): """closes the windows and reverts the legend""" self.out_data['close'] = True self.close()
class QtImageControls(QtBaseImageControls): """Qt view and controls for the napari Image layer. Parameters ---------- layer : napari.layers.Image An instance of a napari Image layer. Attributes ---------- attenuationSlider : qtpy.QtWidgets.QSlider Slider controlling attenuation rate for `attenuated_mip` mode. attenuationLabel : qtpy.QtWidgets.QLabel Label for the attenuation slider widget. grid_layout : qtpy.QtWidgets.QGridLayout Layout of Qt widget controls for the layer. interpComboBox : qtpy.QtWidgets.QComboBox Dropdown menu to select the interpolation mode for image display. interpLabel : qtpy.QtWidgets.QLabel Label for the interpolation dropdown menu. isoThresholdSlider : qtpy.QtWidgets.QSlider Slider controlling the isosurface threshold value for rendering. isoThresholdLabel : qtpy.QtWidgets.QLabel Label for the isosurface threshold slider widget. layer : napari.layers.Image An instance of a napari Image layer. renderComboBox : qtpy.QtWidgets.QComboBox Dropdown menu to select the rendering mode for image display. renderLabel : qtpy.QtWidgets.QLabel Label for the rendering mode dropdown menu. """ def __init__(self, layer): super().__init__(layer) self.layer.events.interpolation.connect(self._on_interpolation_change) self.layer.events.rendering.connect(self._on_rendering_change) self.layer.events.iso_threshold.connect(self._on_iso_threshold_change) self.layer.events.attenuation.connect(self._on_attenuation_change) self.layer.events._ndisplay.connect(self._on_ndisplay_change) self.interpComboBox = QComboBox(self) self.interpComboBox.activated[str].connect(self.changeInterpolation) self.interpLabel = QLabel(trans._('interpolation:')) renderComboBox = QComboBox(self) rendering_options = [i.value for i in ImageRendering] renderComboBox.addItems(rendering_options) index = renderComboBox.findText(self.layer.rendering, Qt.MatchFixedString) renderComboBox.setCurrentIndex(index) renderComboBox.activated[str].connect(self.changeRendering) self.renderComboBox = renderComboBox self.renderLabel = QLabel(trans._('rendering:')) sld = QSlider(Qt.Horizontal, parent=self) sld.setFocusPolicy(Qt.NoFocus) sld.setMinimum(0) sld.setMaximum(100) sld.setSingleStep(1) sld.setValue(int(self.layer.iso_threshold * 100)) sld.valueChanged.connect(self.changeIsoThreshold) self.isoThresholdSlider = sld self.isoThresholdLabel = QLabel(trans._('iso threshold:')) sld = QSlider(Qt.Horizontal, parent=self) sld.setFocusPolicy(Qt.NoFocus) sld.setMinimum(0) sld.setMaximum(100) sld.setSingleStep(1) sld.setValue(int(self.layer.attenuation * 200)) sld.valueChanged.connect(self.changeAttenuation) self.attenuationSlider = sld self.attenuationLabel = QLabel(trans._('attenuation:')) self._on_ndisplay_change() colormap_layout = QHBoxLayout() if hasattr(self.layer, 'rgb') and self.layer.rgb: colormap_layout.addWidget(QLabel("RGB")) self.colormapComboBox.setVisible(False) self.colorbarLabel.setVisible(False) else: colormap_layout.addWidget(self.colorbarLabel) colormap_layout.addWidget(self.colormapComboBox) colormap_layout.addStretch(1) # grid_layout created in QtLayerControls # addWidget(widget, row, column, [row_span, column_span]) self.grid_layout.addWidget(QLabel(trans._('opacity:')), 0, 0) self.grid_layout.addWidget(self.opacitySlider, 0, 1) self.grid_layout.addWidget(QLabel(trans._('contrast limits:')), 1, 0) self.grid_layout.addWidget(self.contrastLimitsSlider, 1, 1) self.grid_layout.addWidget(QLabel(trans._('auto-contrast:')), 2, 0) self.grid_layout.addWidget(self.autoScaleBar, 2, 1) self.grid_layout.addWidget(QLabel(trans._('gamma:')), 3, 0) self.grid_layout.addWidget(self.gammaSlider, 3, 1) self.grid_layout.addWidget(QLabel(trans._('colormap:')), 4, 0) self.grid_layout.addLayout(colormap_layout, 4, 1) self.grid_layout.addWidget(QLabel(trans._('blending:')), 5, 0) self.grid_layout.addWidget(self.blendComboBox, 5, 1) self.grid_layout.addWidget(self.interpLabel, 6, 0) self.grid_layout.addWidget(self.interpComboBox, 6, 1) self.grid_layout.addWidget(self.renderLabel, 7, 0) self.grid_layout.addWidget(self.renderComboBox, 7, 1) self.grid_layout.addWidget(self.isoThresholdLabel, 8, 0) self.grid_layout.addWidget(self.isoThresholdSlider, 8, 1) self.grid_layout.addWidget(self.attenuationLabel, 9, 0) self.grid_layout.addWidget(self.attenuationSlider, 9, 1) self.grid_layout.setRowStretch(9, 1) self.grid_layout.setColumnStretch(1, 1) self.grid_layout.setSpacing(4) def changeInterpolation(self, text): """Change interpolation mode for image display. Parameters ---------- text : str Interpolation mode used by vispy. Must be one of our supported modes: 'bessel', 'bicubic', 'bilinear', 'blackman', 'catrom', 'gaussian', 'hamming', 'hanning', 'hermite', 'kaiser', 'lanczos', 'mitchell', 'nearest', 'spline16', 'spline36' """ self.layer.interpolation = text def changeRendering(self, text): """Change rendering mode for image display. Parameters ---------- text : str Rendering mode used by vispy. Selects a preset rendering mode in vispy that determines how volume is displayed: * translucent: voxel colors are blended along the view ray until the result is opaque. * mip: maximum intensity projection. Cast a ray and display the maximum value that was encountered. * additive: voxel colors are added along the view ray until the result is saturated. * iso: isosurface. Cast a ray until a certain threshold is encountered. At that location, lighning calculations are performed to give the visual appearance of a surface. * attenuated_mip: attenuated maximum intensity projection. Cast a ray and attenuate values based on integral of encountered values, display the maximum value that was encountered after attenuation. This will make nearer objects appear more prominent. """ self.layer.rendering = text self._toggle_rendering_parameter_visbility() def changeIsoThreshold(self, value): """Change isosurface threshold on the layer model. Parameters ---------- value : float Threshold for isosurface. """ with self.layer.events.blocker(self._on_iso_threshold_change): self.layer.iso_threshold = value / 100 def _on_iso_threshold_change(self): """Receive layer model isosurface change event and update the slider.""" with self.layer.events.iso_threshold.blocker(): self.isoThresholdSlider.setValue( int(self.layer.iso_threshold * 100)) def changeAttenuation(self, value): """Change attenuation rate for attenuated maximum intensity projection. Parameters ---------- value : Float Attenuation rate for attenuated maximum intensity projection. """ with self.layer.events.blocker(self._on_attenuation_change): self.layer.attenuation = value / 200 def _on_attenuation_change(self): """Receive layer model attenuation change event and update the slider.""" with self.layer.events.attenuation.blocker(): self.attenuationSlider.setValue(int(self.layer.attenuation * 200)) def _on_interpolation_change(self, event): """Receive layer interpolation change event and update dropdown menu. Parameters ---------- event : napari.utils.event.Event The napari event that triggered this method. """ interp_string = event.value.value with self.layer.events.interpolation.blocker(): if self.interpComboBox.findText(interp_string) == -1: self.interpComboBox.addItem(interp_string) self.interpComboBox.setCurrentText(interp_string) def _on_rendering_change(self): """Receive layer model rendering change event and update dropdown menu.""" with self.layer.events.rendering.blocker(): index = self.renderComboBox.findText(self.layer.rendering, Qt.MatchFixedString) self.renderComboBox.setCurrentIndex(index) self._toggle_rendering_parameter_visbility() def _toggle_rendering_parameter_visbility(self): """Hide isosurface rendering parameters if they aren't needed.""" rendering = ImageRendering(self.layer.rendering) if rendering == ImageRendering.ISO: self.isoThresholdSlider.show() self.isoThresholdLabel.show() else: self.isoThresholdSlider.hide() self.isoThresholdLabel.hide() if rendering == ImageRendering.ATTENUATED_MIP: self.attenuationSlider.show() self.attenuationLabel.show() else: self.attenuationSlider.hide() self.attenuationLabel.hide() def _update_interpolation_combo(self): self.interpComboBox.clear() interp_names = (Interpolation3D.keys() if self.layer._ndisplay == 3 else [i.value for i in Interpolation.view_subset()]) self.interpComboBox.addItems(interp_names) index = self.interpComboBox.findText(self.layer.interpolation, Qt.MatchFixedString) self.interpComboBox.setCurrentIndex(index) def _on_ndisplay_change(self): """Toggle between 2D and 3D visualization modes.""" self._update_interpolation_combo() if self.layer._ndisplay == 2: self.isoThresholdSlider.hide() self.isoThresholdLabel.hide() self.attenuationSlider.hide() self.attenuationLabel.hide() self.renderComboBox.hide() self.renderLabel.hide() else: self.renderComboBox.show() self.renderLabel.show() self._toggle_rendering_parameter_visbility()
class TimeChartDisplay(Display): def __init__(self, parent=None, args=[], macros=None, show_pv_add_panel=True, config_file=None): """ Create all the widgets, including any child dialogs. Parameters ---------- parent : QWidget The parent widget of the charting display args : list The command parameters macros : str Macros to modify the UI parameters at runtime show_pv_add_panel : bool Whether or not to show the PV add panel on top of the graph """ super(TimeChartDisplay, self).__init__(parent=parent, args=args, macros=macros) self.legend_font = None self.channel_map = dict() self.setWindowTitle("TimeChart Tool") self.main_layout = QVBoxLayout() self.body_layout = QVBoxLayout() self.pv_add_panel = QFrame() self.pv_add_panel.setVisible(show_pv_add_panel) self.pv_add_panel.setMaximumHeight(50) self.pv_layout = QHBoxLayout() self.pv_name_line_edt = QLineEdit() self.pv_name_line_edt.setAcceptDrops(True) self.pv_name_line_edt.returnPressed.connect(self.add_curve) self.pv_protocol_cmb = QComboBox() self.pv_protocol_cmb.addItems(["ca://", "archive://"]) self.pv_protocol_cmb.setEnabled(False) self.pv_connect_push_btn = QPushButton("Connect") self.pv_connect_push_btn.clicked.connect(self.add_curve) self.tab_panel = QTabWidget() self.tab_panel.setMinimumWidth(350) self.tab_panel.setMaximumWidth(350) self.curve_settings_tab = QWidget() self.data_settings_tab = QWidget() self.chart_settings_tab = QWidget() self.charting_layout = QHBoxLayout() self.chart = PyDMTimePlot(plot_by_timestamps=False) self.chart.setDownsampling(ds=False, auto=False, mode=None) self.chart.plot_redrawn_signal.connect(self.update_curve_data) self.chart.setBufferSize(DEFAULT_BUFFER_SIZE) self.chart.setPlotTitle("Time Plot") self.splitter = QSplitter() self.curve_settings_layout = QVBoxLayout() self.curve_settings_layout.setAlignment(Qt.AlignTop) self.curve_settings_layout.setSizeConstraint(QLayout.SetMinAndMaxSize) self.curve_settings_layout.setSpacing(5) self.crosshair_settings_layout = QVBoxLayout() self.crosshair_settings_layout.setAlignment(Qt.AlignTop) self.crosshair_settings_layout.setSpacing(5) self.enable_crosshair_chk = QCheckBox("Crosshair") self.crosshair_coord_lbl = QLabel() self.crosshair_coord_lbl.setWordWrap(True) self.curve_settings_inner_frame = QFrame() self.curve_settings_inner_frame.setLayout(self.curve_settings_layout) self.curve_settings_scroll = QScrollArea() self.curve_settings_scroll.setVerticalScrollBarPolicy( Qt.ScrollBarAsNeeded) self.curve_settings_scroll.setHorizontalScrollBarPolicy( Qt.ScrollBarAlwaysOff) self.curve_settings_scroll.setWidget(self.curve_settings_inner_frame) self.curve_settings_scroll.setWidgetResizable(True) self.enable_crosshair_chk.setChecked(False) self.enable_crosshair_chk.clicked.connect( self.handle_enable_crosshair_checkbox_clicked) self.enable_crosshair_chk.clicked.emit(False) self.curves_tab_layout = QHBoxLayout() self.curves_tab_layout.addWidget(self.curve_settings_scroll) self.data_tab_layout = QVBoxLayout() self.data_tab_layout.setAlignment(Qt.AlignTop) self.data_tab_layout.setSpacing(5) self.chart_settings_layout = QVBoxLayout() self.chart_settings_layout.setAlignment(Qt.AlignTop) self.chart_settings_layout.setSpacing(5) self.chart_layout = QVBoxLayout() self.chart_layout.setSpacing(10) self.chart_panel = QWidget() self.chart_panel.setMinimumHeight(400) self.chart_control_layout = QHBoxLayout() self.chart_control_layout.setAlignment(Qt.AlignHCenter) self.chart_control_layout.setSpacing(10) self.zoom_x_layout = QVBoxLayout() self.zoom_x_layout.setAlignment(Qt.AlignTop) self.zoom_x_layout.setSpacing(5) self.plus_icon = IconFont().icon("plus", color=QColor("green")) self.minus_icon = IconFont().icon("minus", color=QColor("red")) self.view_all_icon = IconFont().icon("globe", color=QColor("blue")) self.reset_icon = IconFont().icon("circle-o-notch", color=QColor("green")) self.zoom_in_x_btn = QPushButton("X Zoom") self.zoom_in_x_btn.setIcon(self.plus_icon) self.zoom_in_x_btn.clicked.connect( partial(self.handle_zoom_in_btn_clicked, "x", True)) self.zoom_in_x_btn.setEnabled(False) self.zoom_out_x_btn = QPushButton("X Zoom") self.zoom_out_x_btn.setIcon(self.minus_icon) self.zoom_out_x_btn.clicked.connect( partial(self.handle_zoom_in_btn_clicked, "x", False)) self.zoom_out_x_btn.setEnabled(False) self.zoom_y_layout = QVBoxLayout() self.zoom_y_layout.setAlignment(Qt.AlignTop) self.zoom_y_layout.setSpacing(5) self.zoom_in_y_btn = QPushButton("Y Zoom") self.zoom_in_y_btn.setIcon(self.plus_icon) self.zoom_in_y_btn.clicked.connect( partial(self.handle_zoom_in_btn_clicked, "y", True)) self.zoom_in_y_btn.setEnabled(False) self.zoom_out_y_btn = QPushButton("Y Zoom") self.zoom_out_y_btn.setIcon(self.minus_icon) self.zoom_out_y_btn.clicked.connect( partial(self.handle_zoom_in_btn_clicked, "y", False)) self.zoom_out_y_btn.setEnabled(False) self.view_all_btn = QPushButton("View All") self.view_all_btn.setIcon(self.view_all_icon) self.view_all_btn.clicked.connect(self.handle_view_all_button_clicked) self.view_all_btn.setEnabled(False) self.view_all_reset_chart_layout = QVBoxLayout() self.view_all_reset_chart_layout.setAlignment(Qt.AlignTop) self.view_all_reset_chart_layout.setSpacing(5) self.pause_chart_layout = QVBoxLayout() self.pause_chart_layout.setAlignment(Qt.AlignTop) self.pause_chart_layout.setSpacing(5) self.reset_chart_btn = QPushButton("Reset") self.reset_chart_btn.setIcon(self.reset_icon) self.reset_chart_btn.clicked.connect( self.handle_reset_chart_btn_clicked) self.reset_chart_btn.setEnabled(False) self.pause_icon = IconFont().icon("pause", color=QColor("red")) self.play_icon = IconFont().icon("play", color=QColor("green")) self.pause_chart_btn = QPushButton() self.pause_chart_btn.setIcon(self.pause_icon) self.pause_chart_btn.clicked.connect( self.handle_pause_chart_btn_clicked) self.title_settings_layout = QVBoxLayout() self.title_settings_layout.setAlignment(Qt.AlignTop) self.title_settings_layout.setSpacing(5) self.title_settings_grpbx = QGroupBox("Title and Legend") self.title_settings_grpbx.setMaximumHeight(120) self.import_export_data_layout = QVBoxLayout() self.import_export_data_layout.setAlignment(Qt.AlignTop) self.import_export_data_layout.setSpacing(5) self.import_data_btn = QPushButton("Import...") self.import_data_btn.clicked.connect( self.handle_import_data_btn_clicked) self.export_data_btn = QPushButton("Export...") self.export_data_btn.clicked.connect( self.handle_export_data_btn_clicked) self.chart_title_layout = QHBoxLayout() self.chart_title_layout.setSpacing(10) self.chart_title_lbl = QLabel(text="Graph Title") self.chart_title_line_edt = QLineEdit() self.chart_title_line_edt.setText(self.chart.getPlotTitle()) self.chart_title_line_edt.textChanged.connect( self.handle_title_text_changed) self.chart_title_font_btn = QPushButton() self.chart_title_font_btn.setFixedHeight(24) self.chart_title_font_btn.setFixedWidth(24) self.chart_title_font_btn.setIcon(IconFont().icon("font")) self.chart_title_font_btn.clicked.connect( partial(self.handle_chart_font_changed, "title")) self.chart_change_axis_settings_btn = QPushButton( text="Change Axis Settings...") self.chart_change_axis_settings_btn.clicked.connect( self.handle_change_axis_settings_clicked) self.update_datetime_timer = QTimer(self) self.update_datetime_timer.timeout.connect( self.handle_update_datetime_timer_timeout) self.chart_sync_mode_layout = QVBoxLayout() self.chart_sync_mode_layout.setSpacing(5) self.chart_sync_mode_grpbx = QGroupBox("Data Sampling Mode") self.chart_sync_mode_grpbx.setMaximumHeight(100) self.chart_sync_mode_sync_radio = QRadioButton("Synchronous") self.chart_sync_mode_async_radio = QRadioButton("Asynchronous") self.chart_sync_mode_async_radio.setChecked(True) self.graph_drawing_settings_layout = QVBoxLayout() self.graph_drawing_settings_layout.setAlignment(Qt.AlignVCenter) self.chart_interval_layout = QFormLayout() self.chart_redraw_rate_lbl = QLabel("Redraw Rate (Hz)") self.chart_redraw_rate_spin = QSpinBox() self.chart_redraw_rate_spin.setRange(MIN_REDRAW_RATE_HZ, MAX_REDRAW_RATE_HZ) self.chart_redraw_rate_spin.setValue(DEFAULT_REDRAW_RATE_HZ) self.chart_redraw_rate_spin.editingFinished.connect( self.handle_redraw_rate_changed) self.chart_data_sampling_rate_lbl = QLabel("Data Sampling Rate (Hz)") self.chart_data_async_sampling_rate_spin = QSpinBox() self.chart_data_async_sampling_rate_spin.setRange( MIN_DATA_SAMPLING_RATE_HZ, MAX_DATA_SAMPLING_RATE_HZ) self.chart_data_async_sampling_rate_spin.setValue( DEFAULT_DATA_SAMPLING_RATE_HZ) self.chart_data_async_sampling_rate_spin.editingFinished.connect( self.handle_data_sampling_rate_changed) self.chart_data_sampling_rate_lbl.hide() self.chart_data_async_sampling_rate_spin.hide() self.chart_limit_time_span_layout = QHBoxLayout() self.chart_limit_time_span_layout.setSpacing(5) self.limit_time_plan_text = "Limit Time Span" self.chart_limit_time_span_chk = QCheckBox(self.limit_time_plan_text) self.chart_limit_time_span_chk.hide() self.chart_limit_time_span_lbl = QLabel("H:MM:SS") self.chart_limit_time_span_hours_spin_box = QSpinBox() self.chart_limit_time_span_hours_spin_box.setMaximum(999) self.chart_limit_time_span_minutes_spin_box = QSpinBox() self.chart_limit_time_span_minutes_spin_box.setMaximum(59) self.chart_limit_time_span_seconds_spin_box = QSpinBox() self.chart_limit_time_span_seconds_spin_box.setMaximum(59) self.chart_limit_time_span_activate_btn = QPushButton("Apply") self.chart_limit_time_span_activate_btn.setDisabled(True) self.chart_ring_buffer_layout = QFormLayout() self.chart_ring_buffer_size_lbl = QLabel("Ring Buffer Size") self.chart_ring_buffer_size_edt = QLineEdit() self.chart_ring_buffer_size_edt.returnPressed.connect( self.handle_buffer_size_changed) self.chart_ring_buffer_size_edt.setText(str(DEFAULT_BUFFER_SIZE)) self.show_legend_chk = QCheckBox("Show Legend") self.show_legend_chk.setChecked(self.chart.showLegend) self.show_legend_chk.clicked.connect( self.handle_show_legend_checkbox_clicked) self.legend_font_btn = QPushButton() self.legend_font_btn.setFixedHeight(24) self.legend_font_btn.setFixedWidth(24) self.legend_font_btn.setIcon(IconFont().icon("font")) self.legend_font_btn.clicked.connect( partial(self.handle_chart_font_changed, "legend")) self.graph_background_color_layout = QFormLayout() self.axis_grid_color_layout = QFormLayout() self.background_color_lbl = QLabel("Graph Background Color ") self.background_color_btn = QPushButton() self.background_color_btn.setStyleSheet( "background-color: " + self.chart.getBackgroundColor().name()) self.background_color_btn.setContentsMargins(10, 0, 5, 5) self.background_color_btn.setMaximumWidth(20) self.background_color_btn.clicked.connect( self.handle_background_color_button_clicked) self.axis_settings_layout = QVBoxLayout() self.axis_settings_layout.setSpacing(10) self.show_x_grid_chk = QCheckBox("Show x Grid") self.show_x_grid_chk.setChecked(self.chart.showXGrid) self.show_x_grid_chk.clicked.connect( self.handle_show_x_grid_checkbox_clicked) self.show_y_grid_chk = QCheckBox("Show y Grid") self.show_y_grid_chk.setChecked(self.chart.showYGrid) self.show_y_grid_chk.clicked.connect( self.handle_show_y_grid_checkbox_clicked) self.axis_color_lbl = QLabel("Axis and Grid Color") self.axis_color_btn = QPushButton() self.axis_color_btn.setStyleSheet("background-color: " + DEFAULT_CHART_AXIS_COLOR.name()) self.axis_color_btn.setContentsMargins(10, 0, 5, 5) self.axis_color_btn.setMaximumWidth(20) self.axis_color_btn.clicked.connect( self.handle_axis_color_button_clicked) self.grid_opacity_lbl = QLabel("Grid Opacity") self.grid_opacity_lbl.setEnabled(False) self.grid_opacity_slr = QSlider(Qt.Horizontal) self.grid_opacity_slr.setFocusPolicy(Qt.StrongFocus) self.grid_opacity_slr.setRange(0, 10) self.grid_opacity_slr.setValue(5) self.grid_opacity_slr.setTickInterval(1) self.grid_opacity_slr.setSingleStep(1) self.grid_opacity_slr.setTickPosition(QSlider.TicksBelow) self.grid_opacity_slr.valueChanged.connect( self.handle_grid_opacity_slider_mouse_release) self.grid_opacity_slr.setEnabled(False) self.reset_data_settings_btn = QPushButton("Reset Data Settings") self.reset_data_settings_btn.clicked.connect( self.handle_reset_data_settings_btn_clicked) self.reset_chart_settings_btn = QPushButton("Reset Chart Settings") self.reset_chart_settings_btn.clicked.connect( self.handle_reset_chart_settings_btn_clicked) self.curve_checkbox_panel = QWidget() self.graph_drawing_settings_grpbx = QGroupBox("Graph Intervals") self.graph_drawing_settings_grpbx.setAlignment(Qt.AlignTop) self.axis_settings_grpbx = QGroupBox("Graph Appearance") self.app = QApplication.instance() self.setup_ui() self.curve_settings_disp = None self.axis_settings_disp = None self.chart_data_export_disp = None self.chart_data_import_disp = None self.grid_alpha = 5 self.time_span_limit_hours = None self.time_span_limit_minutes = None self.time_span_limit_seconds = None self.data_sampling_mode = ASYNC_DATA_SAMPLING # If there is an imported config file, let's start TimeChart with the imported configuration data if config_file: importer = SettingsImporter(self) importer.import_settings(config_file) def ui_filepath(self): """ The path to the UI file created by Qt Designer, if applicable. """ # No UI file is being used return None def ui_filename(self): """ The name the UI file created by Qt Designer, if applicable. """ # No UI file is being used return None def setup_ui(self): """ Initialize the widgets and layouts. """ self.setLayout(self.main_layout) self.pv_layout.addWidget(self.pv_protocol_cmb) self.pv_layout.addWidget(self.pv_name_line_edt) self.pv_layout.addWidget(self.pv_connect_push_btn) self.pv_add_panel.setLayout(self.pv_layout) QTimer.singleShot(0, self.pv_name_line_edt.setFocus) self.curve_settings_tab.setLayout(self.curves_tab_layout) self.chart_settings_tab.setLayout(self.chart_settings_layout) self.setup_chart_settings_layout() self.data_settings_tab.setLayout(self.data_tab_layout) self.setup_data_tab_layout() self.tab_panel.addTab(self.curve_settings_tab, "Curves") self.tab_panel.addTab(self.data_settings_tab, "Data") self.tab_panel.addTab(self.chart_settings_tab, "Graph") self.crosshair_settings_layout.addWidget(self.enable_crosshair_chk) self.crosshair_settings_layout.addWidget(self.crosshair_coord_lbl) self.zoom_x_layout.addWidget(self.zoom_in_x_btn) self.zoom_x_layout.addWidget(self.zoom_out_x_btn) self.zoom_y_layout.addWidget(self.zoom_in_y_btn) self.zoom_y_layout.addWidget(self.zoom_out_y_btn) self.view_all_reset_chart_layout.addWidget(self.reset_chart_btn) self.view_all_reset_chart_layout.addWidget(self.view_all_btn) self.pause_chart_layout.addWidget(self.pause_chart_btn) self.import_export_data_layout.addWidget(self.import_data_btn) self.import_export_data_layout.addWidget(self.export_data_btn) self.chart_control_layout.addLayout(self.zoom_x_layout) self.chart_control_layout.addLayout(self.zoom_y_layout) self.chart_control_layout.addLayout(self.view_all_reset_chart_layout) self.chart_control_layout.addLayout(self.pause_chart_layout) self.chart_control_layout.addLayout(self.crosshair_settings_layout) self.chart_control_layout.addLayout(self.import_export_data_layout) self.chart_control_layout.insertSpacing(5, 30) self.chart_layout.addWidget(self.chart) self.chart_layout.addLayout(self.chart_control_layout) self.chart_panel.setLayout(self.chart_layout) self.splitter.addWidget(self.chart_panel) self.splitter.addWidget(self.tab_panel) self.splitter.setSizes([1, 0]) self.splitter.setHandleWidth(10) self.splitter.setStretchFactor(0, 0) self.splitter.setStretchFactor(1, 1) self.charting_layout.addWidget(self.splitter) self.body_layout.addWidget(self.pv_add_panel) self.body_layout.addLayout(self.charting_layout) self.body_layout.setSpacing(0) self.body_layout.setContentsMargins(0, 0, 0, 0) self.main_layout.addLayout(self.body_layout) self.enable_chart_control_buttons(False) handle = self.splitter.handle(1) layout = QVBoxLayout() layout.setContentsMargins(0, 0, 0, 0) button = QToolButton(handle) button.setArrowType(Qt.LeftArrow) button.clicked.connect(lambda: self.handle_splitter_button(True)) layout.addWidget(button) button = QToolButton(handle) button.setArrowType(Qt.RightArrow) button.clicked.connect(lambda: self.handle_splitter_button(False)) layout.addWidget(button) handle.setLayout(layout) def handle_splitter_button(self, left=True): if left: self.splitter.setSizes([1, 1]) else: self.splitter.setSizes([1, 0]) def change_legend_font(self, font): if font is None: return self.legend_font = font items = self.chart.plotItem.legend.items for i in items: i[1].item.setFont(font) i[1].resizeEvent(None) i[1].updateGeometry() def change_title_font(self, font): current_text = self.chart.plotItem.titleLabel.text args = { "family": font.family, "size": "{}pt".format(font.pointSize()), "bold": font.bold(), "italic": font.italic(), } self.chart.plotItem.titleLabel.setText(current_text, **args) def handle_chart_font_changed(self, target): if target not in ("title", "legend"): return dialog = QFontDialog(self) dialog.setOption(QFontDialog.DontUseNativeDialog, True) if target == "title": dialog.fontSelected.connect(self.change_title_font) else: dialog.fontSelected.connect(self.change_legend_font) dialog.open() def setup_data_tab_layout(self): self.chart_sync_mode_sync_radio.toggled.connect( partial(self.handle_sync_mode_radio_toggle, self.chart_sync_mode_sync_radio)) self.chart_sync_mode_async_radio.toggled.connect( partial(self.handle_sync_mode_radio_toggle, self.chart_sync_mode_async_radio)) self.chart_sync_mode_layout.addWidget(self.chart_sync_mode_sync_radio) self.chart_sync_mode_layout.addWidget(self.chart_sync_mode_async_radio) self.chart_sync_mode_grpbx.setLayout(self.chart_sync_mode_layout) self.data_tab_layout.addWidget(self.chart_sync_mode_grpbx) self.chart_limit_time_span_layout.addWidget( self.chart_limit_time_span_lbl) self.chart_limit_time_span_layout.addWidget( self.chart_limit_time_span_hours_spin_box) self.chart_limit_time_span_layout.addWidget( self.chart_limit_time_span_minutes_spin_box) self.chart_limit_time_span_layout.addWidget( self.chart_limit_time_span_seconds_spin_box) self.chart_limit_time_span_layout.addWidget( self.chart_limit_time_span_activate_btn) self.chart_limit_time_span_lbl.hide() self.chart_limit_time_span_hours_spin_box.hide() self.chart_limit_time_span_minutes_spin_box.hide() self.chart_limit_time_span_seconds_spin_box.hide() self.chart_limit_time_span_activate_btn.hide() self.chart_limit_time_span_hours_spin_box.valueChanged.connect( self.handle_time_span_changed) self.chart_limit_time_span_minutes_spin_box.valueChanged.connect( self.handle_time_span_changed) self.chart_limit_time_span_seconds_spin_box.valueChanged.connect( self.handle_time_span_changed) self.chart_limit_time_span_chk.clicked.connect( self.handle_limit_time_span_checkbox_clicked) self.chart_limit_time_span_activate_btn.clicked.connect( self.handle_chart_limit_time_span_activate_btn_clicked) self.chart_interval_layout.addRow(self.chart_redraw_rate_lbl, self.chart_redraw_rate_spin) self.chart_interval_layout.addRow( self.chart_data_sampling_rate_lbl, self.chart_data_async_sampling_rate_spin) self.graph_drawing_settings_layout.addLayout( self.chart_interval_layout) self.graph_drawing_settings_layout.addWidget( self.chart_limit_time_span_chk) self.graph_drawing_settings_layout.addLayout( self.chart_limit_time_span_layout) self.chart_ring_buffer_layout.addRow(self.chart_ring_buffer_size_lbl, self.chart_ring_buffer_size_edt) self.graph_drawing_settings_layout.addLayout( self.chart_ring_buffer_layout) self.graph_drawing_settings_grpbx.setLayout( self.graph_drawing_settings_layout) self.data_tab_layout.addWidget(self.graph_drawing_settings_grpbx) self.chart_sync_mode_async_radio.toggled.emit(True) self.data_tab_layout.addWidget(self.reset_data_settings_btn) def setup_chart_settings_layout(self): self.chart_title_layout.addWidget(self.chart_title_lbl) self.chart_title_layout.addWidget(self.chart_title_line_edt) self.chart_title_layout.addWidget(self.chart_title_font_btn) self.title_settings_layout.addLayout(self.chart_title_layout) legend_layout = QHBoxLayout() legend_layout.addWidget(self.show_legend_chk) legend_layout.addWidget(self.legend_font_btn) self.title_settings_layout.addLayout(legend_layout) self.title_settings_layout.addWidget( self.chart_change_axis_settings_btn) self.title_settings_grpbx.setLayout(self.title_settings_layout) self.chart_settings_layout.addWidget(self.title_settings_grpbx) self.graph_background_color_layout.addRow(self.background_color_lbl, self.background_color_btn) self.axis_settings_layout.addLayout(self.graph_background_color_layout) self.axis_grid_color_layout.addRow(self.axis_color_lbl, self.axis_color_btn) self.axis_settings_layout.addLayout(self.axis_grid_color_layout) self.axis_settings_layout.addWidget(self.show_x_grid_chk) self.axis_settings_layout.addWidget(self.show_y_grid_chk) self.axis_settings_layout.addWidget(self.grid_opacity_lbl) self.axis_settings_layout.addWidget(self.grid_opacity_slr) self.axis_settings_grpbx.setLayout(self.axis_settings_layout) self.chart_settings_layout.addWidget(self.axis_settings_grpbx) self.chart_settings_layout.addWidget(self.reset_chart_settings_btn) self.update_datetime_timer.start(1000) def add_curve(self): """ Add a new curve to the chart. """ pv_name = self._get_full_pv_name(self.pv_name_line_edt.text()) if pv_name and len(pv_name): color = random_color() for k, v in self.channel_map.items(): if color == v.color: color = random_color() self.add_y_channel(pv_name=pv_name, curve_name=pv_name, color=color) self.handle_splitter_button(left=True) def show_mouse_coordinates(self, x, y): self.crosshair_coord_lbl.clear() self.crosshair_coord_lbl.setText("x = {0:.3f}\ny = {1:.3f}".format( x, y)) def handle_enable_crosshair_checkbox_clicked(self, is_checked): self.chart.enableCrosshair(is_checked) self.crosshair_coord_lbl.setVisible(is_checked) self.chart.crosshair_position_updated.connect( self.show_mouse_coordinates) def add_y_channel(self, pv_name, curve_name, color, line_style=Qt.SolidLine, line_width=2, symbol=None, symbol_size=None): if pv_name in self.channel_map: logger.error("'{0}' has already been added.".format(pv_name)) return curve = self.chart.addYChannel(y_channel=pv_name, name=curve_name, color=color, lineStyle=line_style, lineWidth=line_width, symbol=symbol, symbolSize=symbol_size) if self.show_legend_chk.isChecked(): self.change_legend_font(self.legend_font) self.channel_map[pv_name] = curve self.generate_pv_controls(pv_name, color) self.enable_chart_control_buttons() self.app.add_connection(curve.channel) def generate_pv_controls(self, pv_name, curve_color): """ Generate a set of widgets to manage the appearance of a curve. The set of widgets includes: 1. A checkbox which shows the curve on the chart if checked, and hide the curve if not checked 2. Two buttons -- Modify... and Remove. Modify... will bring up the Curve Settings dialog. Remove will delete the curve from the chart This set of widgets will be hidden initially, until the first curve is plotted. Parameters ---------- pv_name: str The name of the PV the current curve is being plotted for curve_color : QColor The color of the curve to paint for the checkbox label to help the user track the curve to the checkbox """ individual_curve_layout = QVBoxLayout() size_policy = QSizePolicy() size_policy.setVerticalPolicy(QSizePolicy.Fixed) size_policy.setHorizontalPolicy(QSizePolicy.Fixed) individual_curve_grpbx = QGroupBox() individual_curve_grpbx.setMinimumWidth(300) individual_curve_grpbx.setMinimumHeight(120) individual_curve_grpbx.setAlignment(Qt.AlignTop) individual_curve_grpbx.setSizePolicy(size_policy) individual_curve_grpbx.setObjectName(pv_name + "_grb") individual_curve_grpbx.setLayout(individual_curve_layout) checkbox = QCheckBox(parent=individual_curve_grpbx) checkbox.setObjectName(pv_name + "_chb") palette = checkbox.palette() palette.setColor(QPalette.Active, QPalette.WindowText, curve_color) checkbox.setPalette(palette) display_name = pv_name.split("://")[1] if len(display_name) > MAX_DISPLAY_PV_NAME_LENGTH: # Only display max allowed number of characters of the PV Name display_name = display_name[ :int(MAX_DISPLAY_PV_NAME_LENGTH / 2) - 1] + "..." + \ display_name[ -int(MAX_DISPLAY_PV_NAME_LENGTH / 2) + 2:] checkbox.setText(display_name) data_text = QLabel(parent=individual_curve_grpbx) data_text.setWordWrap(True) data_text.setObjectName(pv_name + "_lbl") data_text.setPalette(palette) checkbox.setChecked(True) checkbox.clicked.connect( partial(self.handle_curve_chkbox_toggled, checkbox)) modify_curve_btn = QPushButton("Modify...", parent=individual_curve_grpbx) modify_curve_btn.setObjectName(pv_name + "_btn_modify") modify_curve_btn.setMaximumWidth(80) modify_curve_btn.clicked.connect( partial(self.display_curve_settings_dialog, pv_name)) focus_curve_btn = QPushButton("Focus", parent=individual_curve_grpbx) focus_curve_btn.setObjectName(pv_name + "_btn_focus") focus_curve_btn.setMaximumWidth(80) focus_curve_btn.clicked.connect(partial(self.focus_curve, pv_name)) # annotate_curve_btn = QPushButton("Annotate...", # parent=individual_curve_grpbx) # annotate_curve_btn.setObjectName(pv_name+"_btn_ann") # annotate_curve_btn.setMaximumWidth(80) # annotate_curve_btn.clicked.connect( # partial(self.annotate_curve, pv_name)) remove_curve_btn = QPushButton("Remove", parent=individual_curve_grpbx) remove_curve_btn.setObjectName(pv_name + "_btn_remove") remove_curve_btn.setMaximumWidth(80) remove_curve_btn.clicked.connect(partial(self.remove_curve, pv_name)) curve_btn_layout = QHBoxLayout() curve_btn_layout.setSpacing(5) curve_btn_layout.addWidget(modify_curve_btn) curve_btn_layout.addWidget(focus_curve_btn) # curve_btn_layout.addWidget(annotate_curve_btn) curve_btn_layout.addWidget(remove_curve_btn) individual_curve_layout.addWidget(checkbox) individual_curve_layout.addWidget(data_text) individual_curve_layout.addLayout(curve_btn_layout) self.curve_settings_layout.addWidget(individual_curve_grpbx) self.tab_panel.setCurrentIndex(0) def handle_curve_chkbox_toggled(self, checkbox): """ Handle a checkbox's checked and unchecked events. If a checkbox is checked, find the curve from the channel map. If found, re-draw the curve with its previous appearance settings. If a checkbox is unchecked, remove the curve from the chart, but keep the cached data in the channel map. Parameters ---------- checkbox : QCheckBox The current checkbox being toggled """ pv_name = self._get_full_pv_name(checkbox.text()) if checkbox.isChecked(): curve = self.channel_map.get(pv_name, None) if curve: self.chart.addLegendItem(curve, pv_name, self.show_legend_chk.isChecked()) curve.show() self.change_legend_font(self.legend_font) else: curve = self.chart.findCurve(pv_name) if curve: curve.hide() self.chart.removeLegendItem(pv_name) def display_curve_settings_dialog(self, pv_name): """ Bring up the Curve Settings dialog to modify the appearance of a curve. Parameters ---------- pv_name : str The name of the PV the curve is being plotted for """ self.curve_settings_disp = CurveSettingsDisplay(self, pv_name) self.curve_settings_disp.show() def focus_curve(self, pv_name): curve = self.chart.findCurve(pv_name) if curve: self.chart.plotItem.setYRange(curve.minY, curve.maxY, padding=0) def annotate_curve(self, pv_name): curve = self.chart.findCurve(pv_name) if curve: annot = TextItem( html= '<div style="text-align: center"><span style="color: #FFF;">This is the' '</span><br><span style="color: #FF0; font-size: 16pt;">PEAK</span></div>', anchor=(-0.3, 0.5), border='w', fill=(0, 0, 255, 100)) annot = TextItem("test", anchor=(-0.3, 0.5)) self.chart.annotateCurve(curve, annot) def remove_curve(self, pv_name): """ Remove a curve from the chart permanently. This will also clear the channel map cache from retaining the removed curve's appearance settings. Parameters ---------- pv_name : str The name of the PV the curve is being plotted for """ curve = self.chart.findCurve(pv_name) if curve: self.app.remove_connection(curve.channel) self.chart.removeYChannel(curve) del self.channel_map[pv_name] self.chart.removeLegendItem(pv_name) widget = self.findChild(QGroupBox, pv_name + "_grb") if widget: widget.deleteLater() widget = None if len(self.chart.getCurves()) < 1: self.enable_chart_control_buttons(False) self.show_legend_chk.setChecked(False) def handle_title_text_changed(self, new_text): self.chart.setPlotTitle(new_text) def handle_change_axis_settings_clicked(self): self.axis_settings_disp = AxisSettingsDisplay(self) self.axis_settings_disp.show() def handle_limit_time_span_checkbox_clicked(self, is_checked): self.chart_limit_time_span_lbl.setVisible(is_checked) self.chart_limit_time_span_hours_spin_box.setVisible(is_checked) self.chart_limit_time_span_minutes_spin_box.setVisible(is_checked) self.chart_limit_time_span_seconds_spin_box.setVisible(is_checked) self.chart_limit_time_span_activate_btn.setVisible(is_checked) self.chart_ring_buffer_size_lbl.setDisabled(is_checked) self.chart_ring_buffer_size_edt.setDisabled(is_checked) if not is_checked: self.chart_limit_time_span_chk.setText(self.limit_time_plan_text) def handle_time_span_changed(self, new_val): self.time_span_limit_hours = self.chart_limit_time_span_hours_spin_box.value( ) self.time_span_limit_minutes = self.chart_limit_time_span_minutes_spin_box.value( ) self.time_span_limit_seconds = self.chart_limit_time_span_seconds_spin_box.value( ) status = self.time_span_limit_hours > 0 or self.time_span_limit_minutes > 0 or self.time_span_limit_seconds > 0 self.chart_limit_time_span_activate_btn.setEnabled(status) def handle_chart_limit_time_span_activate_btn_clicked(self): timeout_milliseconds = (self.time_span_limit_hours * 3600 + self.time_span_limit_minutes * 60 + self.time_span_limit_seconds) * 1000 self.chart.setTimeSpan(timeout_milliseconds / 1000.0) self.chart_ring_buffer_size_edt.setText(str( self.chart.getBufferSize())) def handle_buffer_size_changed(self): try: new_buffer_size = int(self.chart_ring_buffer_size_edt.text()) if new_buffer_size and int(new_buffer_size) >= MINIMUM_BUFFER_SIZE: self.chart.setBufferSize(new_buffer_size) except ValueError: display_message_box(QMessageBox.Critical, "Invalid Values", "Only integer values are accepted.") def handle_redraw_rate_changed(self): self.chart.maxRedrawRate = self.chart_redraw_rate_spin.value() def handle_data_sampling_rate_changed(self): # The chart expects the value in milliseconds sampling_rate_seconds = 1.0 / self.chart_data_async_sampling_rate_spin.value( ) buffer_size = self.chart.getBufferSize() self.chart.setUpdateInterval(sampling_rate_seconds) if self.chart.getBufferSize() < buffer_size: self.chart.setBufferSize(buffer_size) self.chart_ring_buffer_size_edt.setText(str( self.chart.getBufferSize())) def handle_background_color_button_clicked(self): selected_color = QColorDialog.getColor() self.chart.setBackgroundColor(selected_color) self.background_color_btn.setStyleSheet("background-color: " + selected_color.name()) def handle_axis_color_button_clicked(self): selected_color = QColorDialog.getColor() self.chart.setAxisColor(selected_color) self.axis_color_btn.setStyleSheet("background-color: " + selected_color.name()) def handle_grid_opacity_slider_mouse_release(self): self.grid_alpha = float(self.grid_opacity_slr.value()) / 10.0 self.chart.setShowXGrid(self.show_x_grid_chk.isChecked(), self.grid_alpha) self.chart.setShowYGrid(self.show_y_grid_chk.isChecked(), self.grid_alpha) def handle_show_x_grid_checkbox_clicked(self, is_checked): self.chart.setShowXGrid(is_checked, self.grid_alpha) self.grid_opacity_lbl.setEnabled(is_checked or self.show_y_grid_chk.isChecked()) self.grid_opacity_slr.setEnabled(is_checked or self.show_y_grid_chk.isChecked()) def handle_show_y_grid_checkbox_clicked(self, is_checked): self.chart.setShowYGrid(is_checked, self.grid_alpha) self.grid_opacity_lbl.setEnabled(is_checked or self.show_x_grid_chk.isChecked()) self.grid_opacity_slr.setEnabled(is_checked or self.show_x_grid_chk.isChecked()) def handle_show_legend_checkbox_clicked(self, is_checked): self.chart.setShowLegend(is_checked) def handle_export_data_btn_clicked(self): self.chart_data_export_disp = ChartDataExportDisplay(self) self.chart_data_export_disp.show() def handle_import_data_btn_clicked(self): open_file_info = QFileDialog.getOpenFileName(self, caption="Open File", filter="*." + IMPORT_FILE_FORMAT) open_file_name = open_file_info[0] if open_file_name: importer = SettingsImporter(self) importer.import_settings(open_file_name) def handle_sync_mode_radio_toggle(self, radio_btn): if radio_btn.isChecked(): if radio_btn.text() == "Synchronous": self.data_sampling_mode = SYNC_DATA_SAMPLING self.chart_data_sampling_rate_lbl.hide() self.chart_data_async_sampling_rate_spin.hide() self.chart.resetTimeSpan() self.chart_limit_time_span_chk.setChecked(False) self.chart_limit_time_span_chk.clicked.emit(False) self.chart_limit_time_span_chk.hide() self.chart.setUpdatesAsynchronously(False) elif radio_btn.text() == "Asynchronous": self.data_sampling_mode = ASYNC_DATA_SAMPLING self.chart_data_sampling_rate_lbl.show() self.chart_data_async_sampling_rate_spin.show() self.chart_limit_time_span_chk.show() self.chart.setUpdatesAsynchronously(True) self.app.establish_widget_connections(self) def handle_zoom_in_btn_clicked(self, axis, is_zoom_in): scale_factor = 0.5 if not is_zoom_in: scale_factor += 1.0 if axis == "x": self.chart.getViewBox().scaleBy(x=scale_factor) elif axis == "y": self.chart.getViewBox().scaleBy(y=scale_factor) def handle_view_all_button_clicked(self): self.chart.plotItem.getViewBox().autoRange() def handle_pause_chart_btn_clicked(self): if self.chart.pausePlotting(): self.pause_chart_btn.setIcon(self.pause_icon) else: self.pause_chart_btn.setIcon(self.play_icon) def handle_reset_chart_btn_clicked(self): self.chart.getViewBox().setXRange(DEFAULT_X_MIN, 0) self.chart.resetAutoRangeY() @Slot() def handle_reset_chart_settings_btn_clicked(self): self.chart.setBackgroundColor(DEFAULT_CHART_BACKGROUND_COLOR) self.background_color_btn.setStyleSheet( "background-color: " + DEFAULT_CHART_BACKGROUND_COLOR.name()) self.chart.setAxisColor(DEFAULT_CHART_AXIS_COLOR) self.axis_color_btn.setStyleSheet("background-color: " + DEFAULT_CHART_AXIS_COLOR.name()) self.grid_opacity_slr.setValue(5) self.show_x_grid_chk.setChecked(False) self.show_x_grid_chk.clicked.emit(False) self.show_y_grid_chk.setChecked(False) self.show_y_grid_chk.clicked.emit(False) self.show_legend_chk.setChecked(False) self.chart.setShowXGrid(False) self.chart.setShowYGrid(False) self.chart.setShowLegend(False) @Slot() def handle_reset_data_settings_btn_clicked(self): self.chart_ring_buffer_size_edt.setText(str(DEFAULT_BUFFER_SIZE)) self.chart_redraw_rate_spin.setValue(DEFAULT_REDRAW_RATE_HZ) self.chart_data_async_sampling_rate_spin.setValue( DEFAULT_DATA_SAMPLING_RATE_HZ) self.chart_data_sampling_rate_lbl.hide() self.chart_data_async_sampling_rate_spin.hide() self.chart_sync_mode_async_radio.setChecked(True) self.chart_sync_mode_async_radio.toggled.emit(True) self.chart_limit_time_span_chk.setChecked(False) self.chart_limit_time_span_chk.setText(self.limit_time_plan_text) self.chart_limit_time_span_chk.clicked.emit(False) self.chart.setUpdatesAsynchronously(True) self.chart.resetTimeSpan() self.chart.resetUpdateInterval() self.chart.setBufferSize(DEFAULT_BUFFER_SIZE) def enable_chart_control_buttons(self, enabled=True): self.zoom_in_x_btn.setEnabled(enabled) self.zoom_out_x_btn.setEnabled(enabled) self.zoom_in_y_btn.setEnabled(enabled) self.zoom_out_y_btn.setEnabled(enabled) self.view_all_btn.setEnabled(enabled) self.reset_chart_btn.setEnabled(enabled) self.pause_chart_btn.setIcon(self.pause_icon) self.pause_chart_btn.setEnabled(enabled) self.export_data_btn.setEnabled(enabled) def _get_full_pv_name(self, pv_name): """ Append the protocol to the PV Name. Parameters ---------- pv_name : str The name of the PV the curve is being plotted for """ if pv_name and "://" not in pv_name: pv_name = ''.join([self.pv_protocol_cmb.currentText(), pv_name]) return pv_name def handle_update_datetime_timer_timeout(self): current_label = self.chart.getBottomAxisLabel() new_label = "Current Time: " + TimeChartDisplay.get_current_datetime() if X_AXIS_LABEL_SEPARATOR in current_label: current_label = current_label[current_label. find(X_AXIS_LABEL_SEPARATOR) + len(X_AXIS_LABEL_SEPARATOR):] new_label += X_AXIS_LABEL_SEPARATOR + current_label self.chart.setLabel("bottom", text=new_label) def update_curve_data(self, curve): """ Determine if the PV is active. If not, disable the related PV controls. If the PV is active, update the PV controls' states. Parameters ---------- curve : PlotItem A PlotItem, i.e. a plot, to draw on the chart. """ pv_name = curve.address min_y = curve.minY if curve.minY else 0 max_y = curve.maxY if curve.maxY else 0 current_y = curve.data_buffer[1, -1] connected = not np.isnan(current_y) grb = self.findChild(QGroupBox, pv_name + "_grb") lbl = grb.findChild(QLabel, pv_name + "_lbl") lbl.setText("(yMin = {0:.3f}, yMax = {1:.3f}) y = {2:.3f}".format( min_y, max_y, current_y)) chb = grb.findChild(QCheckBox, pv_name + "_chb") if connected and chb.isEnabled(): return chb.setEnabled(connected) btn_modify = grb.findChild(QPushButton, pv_name + "_btn_modify") btn_modify.setEnabled(connected) btn_focus = grb.findChild(QPushButton, pv_name + "_btn_modify") btn_focus.setEnabled(connected) # btn_ann = grb.findChild(QPushButton, pv_name + "_btn_ann") # btn_ann.setEnabled(connected) @staticmethod def get_current_datetime(): current_date = datetime.datetime.now().strftime("%b %d, %Y") current_time = datetime.datetime.now().strftime("%H:%M:%S") current_datetime = current_time + ' (' + current_date + ')' return current_datetime @property def gridAlpha(self): return self.grid_alpha
class SelectSmoothing(QDialog): """ SelectSmoothing launches a GUI and executes smoothing. Any output is added to the input data as a new component. """ def __init__(self, data, parent=None, smooth_cube=None, allow_preview=False, allow_spectral_axes=False): super(SelectSmoothing, self).__init__(parent) self.setWindowFlags(self.windowFlags() | Qt.Tool) self.parent = parent self.title = "Smoothing Selection" self.data = data # Glue data to be smoothed # Check if smooth object is the caller if smooth_cube is None: self.smooth_cube = SmoothCube(data=self.data) else: self.smooth_cube = smooth_cube self.allow_spectral_axes = allow_spectral_axes self.allow_preview = allow_preview self.is_preview_active = False # Flag to show if smoothing preview is active self.abort_window = None # Small window pop up when smoothing. self.component_id = None # Glue data component to smooth over self.current_axis = None # Selected smoothing_axis self.current_kernel_type = None # Selected kernel type, a key in SmoothCube.kernel_registry self.current_kernel_name = None # Name of selected kernel self._init_selection_ui() # Format and show gui def _init_selection_ui(self): # LINE 1: Radio box spatial vs spectral axis self.axes_prompt = QLabel("Smoothing Axis:") self.axes_prompt.setMinimumWidth(150) self.spatial_radio = QRadioButton("Spatial") self.spatial_radio.setChecked(True) self.current_axis = "spatial" self.spatial_radio.toggled.connect(self.spatial_radio_checked) self.spectral_radio = QRadioButton("Spectral") self.spectral_radio.toggled.connect(self.spectral_radio_checked) # hbl is short for Horizontal Box Layout hbl1 = QHBoxLayout() hbl1.addWidget(self.axes_prompt) hbl1.addWidget(self.spatial_radio) hbl1.addWidget(self.spectral_radio) # LINE 2: Kernel Type prompt self.k_type_prompt = QLabel("Kernel Type:") self.k_type_prompt.setMinimumWidth(150) # Load kernel types + names and add to drop down self._load_options() self.combo = QComboBox() self.combo.setMinimumWidth(150) self.combo.addItems(self.options[self.current_axis]) hbl2 = QHBoxLayout() hbl2.addWidget(self.k_type_prompt) hbl2.addWidget(self.combo) # LINE 3: Kernel size self.size_prompt = QLabel( self.smooth_cube.get_kernel_size_prompt(self.current_kernel_type)) self.size_prompt.setWordWrap(True) self.size_prompt.setMinimumWidth(150) self.unit_label = QLabel( self.smooth_cube.get_kernel_unit(self.current_kernel_type)) self.k_size = QLineEdit("1") # Default Kernel size set here hbl3 = QHBoxLayout() hbl3.addWidget(self.size_prompt) hbl3.addWidget(self.k_size) hbl3.addWidget(self.unit_label) # LINE 4: Data component drop down self.component_prompt = QLabel("Data Component:") self.component_prompt.setWordWrap(True) self.component_prompt.setMinimumWidth(150) # Load component_ids and add to drop down # Add the data component labels to the drop down, with the ComponentID # set as the userData: if self.parent is not None and hasattr(self.parent, 'data_components'): labeldata = [(str(cid), cid) for cid in self.parent.data_components] else: labeldata = [(str(cid), cid) for cid in self.data.main_components()] self.component_combo = QComboBox() update_combobox(self.component_combo, labeldata) self.component_combo.setMaximumWidth(150) self.component_combo.setCurrentIndex(0) if self.allow_preview: self.component_combo.currentIndexChanged.connect( self.update_preview_button) hbl4 = QHBoxLayout() hbl4.addWidget(self.component_prompt) hbl4.addWidget(self.component_combo) # Line 5: Preview Message message = "Info: Smoothing previews are displayed on " \ "CubeViz's left and single image viewers." self.preview_message = QLabel(message) self.preview_message.setWordWrap(True) self.preview_message.hide() hbl5 = QHBoxLayout() hbl5.addWidget(self.preview_message) # LINE 6: preview ok cancel buttons self.previewButton = QPushButton("Preview Slice") self.previewButton.clicked.connect(self.call_preview) self.okButton = QPushButton("Smooth Cube") self.okButton.clicked.connect(self.call_main) self.okButton.setDefault(True) self.cancelButton = QPushButton("Cancel") self.cancelButton.clicked.connect(self.cancel) hbl6 = QHBoxLayout() hbl6.addStretch(1) if self.allow_preview: hbl6.addWidget(self.previewButton) hbl6.addWidget(self.cancelButton) hbl6.addWidget(self.okButton) # Add Lines to Vertical Layout # vbl is short for Vertical Box Layout vbl = QVBoxLayout() if self.allow_spectral_axes: vbl.addLayout(hbl1) vbl.addLayout(hbl2) vbl.addLayout(hbl3) vbl.addLayout(hbl4) vbl.addLayout(hbl5) vbl.addLayout(hbl6) self.setLayout(vbl) self.setMaximumWidth(330) # Connect kernel combo box to event handler self.combo.currentIndexChanged.connect(self.selection_changed) self.selection_changed(0) self.show() def _load_options(self): """Extract names + types of kernels from SmoothCube.kernel_registry""" kernel_registry = self.smooth_cube.get_kernel_registry() self.options = {"spatial": [], "spectral": []} for k in kernel_registry: axis = kernel_registry[k]["axis"] for a in axis: if "spatial" == a: self.options["spatial"].append(kernel_registry[k]["name"]) elif "spectral" == a: self.options["spectral"].append(kernel_registry[k]["name"]) self.options["spectral"].sort() self.options["spatial"].sort() self.current_kernel_name = self.options[self.current_axis][0] self.current_kernel_type = self.smooth_cube.name_to_kernel_type( self.options[self.current_axis][0]) def selection_changed(self, i): """ Update kernel type, units, etc... when kernel name changes in combo box. """ keys = self.options[self.current_axis] name = keys[i] self.current_kernel_name = name self.current_kernel_type = self.smooth_cube.name_to_kernel_type(name) self.unit_label.setText( self.smooth_cube.get_kernel_unit(self.current_kernel_type)) self.size_prompt.setText( self.smooth_cube.get_kernel_size_prompt(self.current_kernel_type)) def spatial_radio_checked(self): self.current_axis = "spatial" self.update_preview_button() self.combo.clear() self.combo.addItems(self.options[self.current_axis]) def spectral_radio_checked(self): self.current_axis = "spectral" self.update_preview_button() self.combo.clear() self.combo.addItems(self.options[self.current_axis]) def input_validation(self): """ Check if input will break Smoothing :return: bool: True if no errors """ red = "background-color: rgba(255, 0, 0, 128);" success = True # Check 1: k_size if self.k_size == "": self.k_size.setStyleSheet(red) success = False else: try: if self.current_kernel_type == "median": k_size = int(self.k_size.text()) else: k_size = float(self.k_size.text()) if k_size <= 0: self.k_size.setStyleSheet(red) success = False else: self.k_size.setStyleSheet("") except ValueError: if self.current_kernel_type == "median": info = QMessageBox.critical( self, "Error", "Kernel size must be integer for median") self.k_size.setStyleSheet(red) success = False return success def call_main(self): try: self.main() except Exception as e: info = QMessageBox.critical(self, "Error", str(e)) self.cancel() raise def main(self): """ Main function to process input and call smoothing function """ success = self.input_validation() if not success: return self.hide() self.abort_window = AbortWindow(self) QApplication.processEvents() # Add smoothing parameters self.smooth_cube.abort_window = self.abort_window if self.smooth_cube.parent is None and self.parent is not self.smooth_cube: self.smooth_cube.parent = self.parent if self.parent is not self.smooth_cube: self.smooth_cube.data = self.data self.smooth_cube.smoothing_axis = self.current_axis self.smooth_cube.kernel_type = self.current_kernel_type if self.current_kernel_type == "median": self.smooth_cube.kernel_size = int(self.k_size.text()) else: self.smooth_cube.kernel_size = float(self.k_size.text()) self.smooth_cube.component_id = str(self.component_combo.currentText()) self.smooth_cube.output_as_component = True if self.is_preview_active: self.parent.end_smoothing_preview() self.is_preview_active = False self.smooth_cube.multi_threading_smooth() return def update_preview_button(self): if self.parent is None or "spatial" != self.current_axis: self.previewButton.setDisabled(True) return self.previewButton.setDisabled(False) return def call_preview(self): try: self.preview() except Exception as e: info = QMessageBox.critical(self, "Error", str(e)) self.cancel() raise def preview(self): """Preview current options""" success = self.input_validation() if not success: return if self.smooth_cube.parent is None and self.parent is not self.smooth_cube: self.smooth_cube.parent = self.parent self.smooth_cube.smoothing_axis = self.current_axis self.smooth_cube.kernel_type = self.current_kernel_type if self.current_kernel_type == "median": self.smooth_cube.kernel_size = int(self.k_size.text()) else: self.smooth_cube.kernel_size = float(self.k_size.text()) preview_function = self.smooth_cube.preview_smoothing preview_title = self.smooth_cube.get_preview_title() component_id = self.component_combo.currentData() self.parent.start_smoothing_preview(preview_function, component_id, preview_title) self.is_preview_active = True self.preview_message.show() def cancel(self): self.clean_up() def clean_up(self): self.close() if self.abort_window is not None: self.abort_window.close() if self.is_preview_active: self.parent.end_smoothing_preview() self.is_preview_active = False def closeEvent(self, event): if self.is_preview_active: self.parent.end_smoothing_preview() self.is_preview_active = False def keyPressEvent(self, e): if e.key() == Qt.Key_Escape: self.clean_up()
def __init__(self, parent): if PYQT5: SpyderPluginWidget.__init__(self, parent, main = parent) else: SpyderPluginWidget.__init__(self, parent) self.internal_shell = None # Initialize plugin self.initialize_plugin() self.no_doc_string = _("No further documentation available") self._last_console_cb = None self._last_editor_cb = None self.plain_text = PlainText(self) self.rich_text = RichText(self) color_scheme = self.get_color_scheme() self.set_plain_text_font(self.get_plugin_font(), color_scheme) self.plain_text.editor.toggle_wrap_mode(self.get_option('wrap')) # Add entries to read-only editor context-menu self.wrap_action = create_action(self, _("Wrap lines"), toggled=self.toggle_wrap_mode) self.wrap_action.setChecked(self.get_option('wrap')) self.plain_text.editor.readonly_menu.addSeparator() add_actions(self.plain_text.editor.readonly_menu, (self.wrap_action,)) self.set_rich_text_font(self.get_plugin_font('rich_text')) self.shell = None self.external_console = None # locked = disable link with Console self.locked = False self._last_texts = [None, None] self._last_editor_doc = None # Object name layout_edit = QHBoxLayout() layout_edit.setContentsMargins(0, 0, 0, 0) txt = _("Source") if sys.platform == 'darwin': source_label = QLabel(" " + txt) else: source_label = QLabel(txt) layout_edit.addWidget(source_label) self.source_combo = QComboBox(self) self.source_combo.addItems([_("Console"), _("Editor")]) self.source_combo.currentIndexChanged.connect(self.source_changed) if (not programs.is_module_installed('rope') and not programs.is_module_installed('jedi', '>=0.8.1')): self.source_combo.hide() source_label.hide() layout_edit.addWidget(self.source_combo) layout_edit.addSpacing(10) layout_edit.addWidget(QLabel(_("Object"))) self.combo = ObjectComboBox(self) layout_edit.addWidget(self.combo) self.object_edit = QLineEdit(self) self.object_edit.setReadOnly(True) layout_edit.addWidget(self.object_edit) self.combo.setMaxCount(self.get_option('max_history_entries')) self.combo.addItems( self.load_history() ) self.combo.setItemText(0, '') self.combo.valid.connect(lambda valid: self.force_refresh()) # Plain text docstring option self.docstring = True self.rich_help = self.get_option('rich_mode', True) self.plain_text_action = create_action(self, _("Plain Text"), toggled=self.toggle_plain_text) # Source code option self.show_source_action = create_action(self, _("Show Source"), toggled=self.toggle_show_source) # Rich text option self.rich_text_action = create_action(self, _("Rich Text"), toggled=self.toggle_rich_text) # Add the help actions to an exclusive QActionGroup help_actions = QActionGroup(self) help_actions.setExclusive(True) help_actions.addAction(self.plain_text_action) help_actions.addAction(self.rich_text_action) # Automatic import option self.auto_import_action = create_action(self, _("Automatic import"), toggled=self.toggle_auto_import) auto_import_state = self.get_option('automatic_import') self.auto_import_action.setChecked(auto_import_state) # Lock checkbox self.locked_button = create_toolbutton(self, triggered=self.toggle_locked) layout_edit.addWidget(self.locked_button) self._update_lock_icon() # Option menu options_button = create_toolbutton(self, text=_('Options'), icon=ima.icon('tooloptions')) options_button.setPopupMode(QToolButton.InstantPopup) menu = QMenu(self) add_actions(menu, [self.rich_text_action, self.plain_text_action, self.show_source_action, None, self.auto_import_action]) options_button.setMenu(menu) layout_edit.addWidget(options_button) if self.rich_help: self.switch_to_rich_text() else: self.switch_to_plain_text() self.plain_text_action.setChecked(not self.rich_help) self.rich_text_action.setChecked(self.rich_help) self.source_changed() # Main layout layout = QVBoxLayout() layout.setContentsMargins(0, 0, 0, 0) layout.addLayout(layout_edit) layout.addWidget(self.plain_text) layout.addWidget(self.rich_text) self.setLayout(layout) # Add worker thread for handling rich text rendering self._sphinx_thread = SphinxThread( html_text_no_doc=warning(self.no_doc_string)) self._sphinx_thread.html_ready.connect( self._on_sphinx_thread_html_ready) self._sphinx_thread.error_msg.connect(self._on_sphinx_thread_error_msg) # Handle internal and external links view = self.rich_text.webview if not WEBENGINE: view.page().setLinkDelegationPolicy(QWebEnginePage.DelegateAllLinks) view.linkClicked.connect(self.handle_link_clicks) self._starting_up = True
class QtCustomTitleBar(QLabel): """A widget to be used as the titleBar in the QtViewerDockWidget. Keeps vertical size minimal, has a hand cursor and styles (in stylesheet) for hover. Close and float buttons. Parameters ---------- parent : QDockWidget The QtViewerDockWidget to which this titlebar belongs title : str A string to put in the titlebar. vertical : bool Whether this titlebar is oriented vertically or not. """ def __init__(self, parent, title: str = '', vertical=False): super().__init__(parent) self.setObjectName("QtCustomTitleBar") self.setProperty('vertical', str(vertical)) self.vertical = vertical self.setToolTip(trans._('drag to move. double-click to float')) line = QFrame(self) line.setObjectName("QtCustomTitleBarLine") add_close = False try: # if the plugins menu is already created, check to see if this is a plugin # dock widget. If it is, then add the close button option to the title bar. actions = [ action.text() for action in self.parent()._qt_viewer.viewer.window.plugins_menu.actions() ] if self.parent().name in actions: add_close = True self.close_button = QPushButton(self) self.close_button.setToolTip(trans._('close this panel')) self.close_button.setObjectName("QTitleBarCloseButton") self.close_button.setCursor(Qt.ArrowCursor) self.close_button.clicked.connect( lambda: self.parent().destroyOnClose() ) else: add_close = False except AttributeError: pass self.hide_button = QPushButton(self) self.hide_button.setToolTip(trans._('hide this panel')) self.hide_button.setObjectName("QTitleBarHideButton") self.hide_button.setCursor(Qt.ArrowCursor) self.hide_button.clicked.connect(lambda: self.parent().close()) self.float_button = QPushButton(self) self.float_button.setToolTip(trans._('float this panel')) self.float_button.setObjectName("QTitleBarFloatButton") self.float_button.setCursor(Qt.ArrowCursor) self.float_button.clicked.connect( lambda: self.parent().setFloating(not self.parent().isFloating()) ) self.title = QLabel(title, self) self.title.setSizePolicy( QSizePolicy(QSizePolicy.Policy.Maximum, QSizePolicy.Policy.Maximum) ) if vertical: layout = QVBoxLayout() layout.setSpacing(4) layout.setContentsMargins(0, 8, 0, 8) line.setFixedWidth(1) if add_close: layout.addWidget(self.close_button, 0, Qt.AlignHCenter) layout.addWidget(self.hide_button, 0, Qt.AlignHCenter) layout.addWidget(self.float_button, 0, Qt.AlignHCenter) layout.addWidget(line, 0, Qt.AlignHCenter) self.title.hide() else: layout = QHBoxLayout() layout.setSpacing(4) layout.setContentsMargins(8, 1, 8, 0) line.setFixedHeight(1) if add_close: layout.addWidget(self.close_button) layout.addWidget(self.hide_button) layout.addWidget(self.float_button) layout.addWidget(line) layout.addWidget(self.title) self.setLayout(layout) self.setCursor(Qt.OpenHandCursor) def sizeHint(self): # this seems to be the correct way to set the height of the titlebar szh = super().sizeHint() if self.vertical: szh.setWidth(20) else: szh.setHeight(20) return szh
class ColorbarWidget(QWidget): colorbarChanged = Signal() # The parent should simply redraw their canvas def __init__(self, parent=None): super(ColorbarWidget, self).__init__(parent) self.setWindowTitle("Colorbar") self.setMaximumWidth(200) self.dval = QDoubleValidator() self.cmin = QLineEdit() self.cmin_value = 0 self.cmin.setMaximumWidth(100) self.cmin.editingFinished.connect(self.clim_changed) self.cmin_layout = QHBoxLayout() self.cmin_layout.addStretch() self.cmin_layout.addWidget(self.cmin) self.cmin_layout.addStretch() self.cmax = QLineEdit() self.cmax_value = 1 self.cmax.setMaximumWidth(100) self.cmax.editingFinished.connect(self.clim_changed) self.cmin.setValidator(self.dval) self.cmax.setValidator(self.dval) self.cmax_layout = QHBoxLayout() self.cmax_layout.addStretch() self.cmax_layout.addWidget(self.cmax) self.cmax_layout.addStretch() self.norm_layout = QHBoxLayout() self.norm = QComboBox() self.norm.addItems(NORM_OPTS) self.norm.currentIndexChanged.connect(self.norm_changed) self.powerscale = QLineEdit() self.powerscale_value = 2 self.powerscale.setText("2") self.powerscale.setValidator(QDoubleValidator(0.001,100,3)) self.powerscale.setMaximumWidth(50) self.powerscale.editingFinished.connect(self.norm_changed) self.powerscale.hide() self.powerscale_label = QLabel("n=") self.powerscale_label.hide() self.norm_layout.addStretch() self.norm_layout.addWidget(self.norm) self.norm_layout.addStretch() self.norm_layout.addWidget(self.powerscale_label) self.norm_layout.addWidget(self.powerscale) self.autoscale = QCheckBox("Autoscaling") self.autoscale.setChecked(True) self.autoscale.stateChanged.connect(self.update_clim) self.canvas = FigureCanvas(Figure()) if parent: # Set facecolor to match parent self.canvas.figure.set_facecolor(parent.palette().window().color().getRgbF()) self.ax = self.canvas.figure.add_axes([0.4,0.05,0.2,0.9]) # layout self.layout = QVBoxLayout(self) self.layout.addLayout(self.cmax_layout) self.layout.addWidget(self.canvas, stretch=1) self.layout.addLayout(self.cmin_layout) self.layout.addLayout(self.norm_layout) self.layout.addWidget(self.autoscale) def set_mappable(self, mappable): """ When a new plot is created this method should be called with the new mappable """ self.ax.clear() self.colorbar = Colorbar(ax=self.ax, mappable=mappable) self.cmin_value, self.cmax_value = self.colorbar.get_clim() self.update_clim_text() self.redraw() def norm_changed(self): """ Called when a different normalization is selected """ idx = self.norm.currentIndex() if NORM_OPTS[idx] == 'Power': self.powerscale.show() self.powerscale_label.show() else: self.powerscale.hide() self.powerscale_label.hide() self.colorbar.mappable.set_norm(self.get_norm()) self.colorbarChanged.emit() def get_norm(self): """ This will create a matplotlib.colors.Normalize from selected idx, limits and powerscale """ idx = self.norm.currentIndex() if self.autoscale.isChecked(): cmin = cmax = None else: cmin = self.cmin_value cmax = self.cmax_value if NORM_OPTS[idx] == 'Power': if self.powerscale.hasAcceptableInput(): self.powerscale_value = float(self.powerscale.text()) return PowerNorm(gamma=self.powerscale_value, vmin=cmin, vmax=cmax) elif NORM_OPTS[idx] == "SymmetricLog10": return SymLogNorm(1e-8 if cmin is None else max(1e-8, abs(cmin)*1e-3), vmin=cmin, vmax=cmax) else: return Normalize(vmin=cmin, vmax=cmax) def clim_changed(self): """ Called when either the min or max is changed. Will unset the autoscale. """ self.autoscale.blockSignals(True) self.autoscale.setChecked(False) self.autoscale.blockSignals(False) self.update_clim() def update_clim(self): """ This will update the clim of the plot based on min, max, and autoscale """ if self.autoscale.isChecked(): data = self.colorbar.mappable.get_array() try: try: self.cmin_value = data[~data.mask].min() self.cmax_value = data[~data.mask].max() except AttributeError: self.cmin_value = np.nanmin(data) self.cmax_value = np.nanmax(data) except (ValueError, RuntimeWarning): # all values mask pass self.update_clim_text() else: if self.cmin.hasAcceptableInput(): self.cmin_value = float(self.cmin.text()) if self.cmax.hasAcceptableInput(): self.cmax_value = float(self.cmax.text()) self.colorbar.set_clim(self.cmin_value, self.cmax_value) self.redraw() def update_clim_text(self): """ Update displayed limit values based on stored ones """ self.cmin.setText("{:.4}".format(self.cmin_value)) self.cmax.setText("{:.4}".format(self.cmax_value)) def redraw(self): """ Redraws the colobar and emits signal to cause the parent to redraw """ self.colorbar.update_ticks() self.colorbar.draw_all() self.canvas.draw_idle() self.colorbarChanged.emit()
class MainConsole(QWidget): """ Interpreter with interactive console. All console output will be redirected to this command line. It provides normal REPL functionality and additionally access to editor components such as the session object and nodes (by right-click on them). The input field below can also expand to a text edit to take whole code blocks. """ instance = None def __init__( self, window_theme, history: int = 100, # max lines in history buffer blockcount: int = 5000, # max lines in output buffer ): super(MainConsole, self).__init__() self.session = None # set by MainWindow self.window_theme = window_theme self.init_ui(history, blockcount) def init_ui(self, history, blockcount): self.content_layout = QGridLayout(self) self.content_layout.setContentsMargins(0, 0, 0, 0) # self.content_layout.setSpacing(0) self.input_layout = QGridLayout() self.input_layout.setContentsMargins(0, 0, 0, 0) self.input_layout.setSpacing(0) # display for output self.out_display = ConsoleDisplay(blockcount) self.content_layout.addWidget(self.out_display, 1, 0, 1, 2) # colors to differentiate input, output and stderr self.inpfmt = self.out_display.currentCharFormat() self.inpfmt.setForeground( QBrush(QColor(self.window_theme.colors['primaryColor']))) self.outfmt = QTextCharFormat(self.inpfmt) self.outfmt.setForeground( QBrush(QColor(self.window_theme.colors['secondaryTextColor']))) self.errfmt = QTextCharFormat(self.inpfmt) self.errfmt.setForeground( QBrush(QColor(self.window_theme.colors['danger']))) # display input prompt left besides input edit self.prompt_label = QLabel('> ', self) self.prompt_label.setFixedWidth(15) self.input_layout.addWidget(self.prompt_label, 0, 0, 1, 1) self.prompt_label.hide() # command "text edit" for large code input self.inptextedit = ConsoleInputTextEdit(self.window_theme) self.input_layout.addWidget(self.inptextedit, 1, 0, 2, 2) self.inptextedit.hide() # command line self.inpedit = ConsoleInputLineEdit(self.inptextedit, max_history=history) self.inpedit.returned.connect(self.push) self.input_layout.addWidget(self.inpedit, 0, 1, 1, 1) self.content_layout.addLayout(self.input_layout, 2, 0, 1, 2) self.interp = None self.reset_interpreter() self.buffer = [] self.num_added_object_contexts = 0 def setprompt(self, text: str): # self.prompt_label.setText(text) ... def add_obj_context(self, context_obj): """adds an object to the current context by initializing a new interpreter with a new context""" old_context = {} if self.interp is None else self.interp.locals name = 'obj' + (str(self.num_added_object_contexts + 1) if self.num_added_object_contexts > 0 else '') new_context = {name: context_obj} context = {**old_context, **new_context} # merge dicts self.interp = code.InteractiveConsole(context) print('added as ' + name) self.num_added_object_contexts += 1 def reset_interpreter(self): """Initializes a new plain interpreter""" # CONTEXT session = self.session def reset(): self.reset_interpreter() context = {**locals()} # ------- self.num_added_object_contexts = 0 # self.reset_scope_button.hide() self.interp = code.InteractiveConsole(context) def push(self, commands: str) -> None: """execute entered command which may span multiple lines when code was pasted""" if commands == 'clear': self.out_display.clear() else: lines = commands.split('\n') # usually just one entry # clean and print commands for line in lines: # # remove '> ' and '.' prefixes which may remain from copy&paste # if re.match('^[\>\.] ', line): # line = line[2:] # print input self.writeoutput( # self.prompt_label.text() + line, self.inpfmt) # prepare for multi-line input # self.setprompt('. ') self.buffer.append(line) # merge commands source = '\n'.join(self.buffer) more = self.interp.runsource(source, '<console>') if more: # self.setprompt('> ') if self.prompt_label.isHidden(): self.prompt_label.show() # add leading space for next input leading_space = re.match(r"\s*", self.buffer[-1]).group() self.inpedit.next_line = leading_space else: # no more input required self.prompt_label.hide() self.buffer = [] # reset buffer def write(self, line: str) -> None: """capture stdout and print to outdisplay""" if len(line) != 1 or ord(line[0]) != 10: self.writeoutput(line.rstrip()) # , self.outfmt) def errorwrite(self, line: str) -> None: """capture stderr and print to outdisplay""" self.writeoutput(line, self.errfmt) def writeoutput(self, line: str, fmt: QTextCharFormat = None) -> None: """prints to outdisplay""" if fmt: self.out_display.setCurrentCharFormat(fmt) self.out_display.appendPlainText(line.rstrip()) self.out_display.setCurrentCharFormat(self.outfmt)