class MarkerControl(StraditizerControlBase, QWidget): """Widget to control the appearance of the marks This widget controls the appearance of the :class:`straditize.cross_mark.CrossMarks` instances in the :attr:`~straditize.straditizer.Straditizer.marks` attribute of the :attr:`~straditize.widgets.StraditizerControlBase.straditizer`""" _toolbar = None @property def marks(self): """The :class:`~straditize.cross_mark.CrossMarks` of the straditizer """ return chain(self.straditizer.marks, self.straditizer.magni_marks) @docstrings.dedent def __init__(self, straditizer_widgets, item, *args, **kwargs): """ Parameters ---------- %(StraditizerControlBase.init_straditizercontrol.parameters)s """ super(MarkerControl, self).__init__(*args, **kwargs) self.init_straditizercontrol(straditizer_widgets, item) vbox = QVBoxLayout() # auto hide button box_hide = QGridLayout() self.cb_auto_hide = QCheckBox('Auto-hide') self.cb_show_connected = QCheckBox('Show additionals') self.cb_drag_hline = QCheckBox('Drag in y-direction') self.cb_drag_vline = QCheckBox('Drag in x-direction') self.cb_selectable_hline = QCheckBox('Horizontal lines selectable') self.cb_selectable_vline = QCheckBox('Vertical lines selectable') self.cb_show_hlines = QCheckBox('Show horizontal lines') self.cb_show_vlines = QCheckBox('Show vertical lines') box_hide.addWidget(self.cb_auto_hide, 0, 0) box_hide.addWidget(self.cb_show_connected, 0, 1) box_hide.addWidget(self.cb_show_vlines, 1, 0) box_hide.addWidget(self.cb_show_hlines, 1, 1) box_hide.addWidget(self.cb_drag_hline, 2, 0) box_hide.addWidget(self.cb_drag_vline, 2, 1) box_hide.addWidget(self.cb_selectable_hline, 3, 0) box_hide.addWidget(self.cb_selectable_vline, 3, 1) vbox.addLayout(box_hide) style_box = QGridLayout() style_box.addWidget(QLabel('Unselected:'), 0, 1) selected_label = QLabel('Selected:') style_box.addWidget(selected_label, 0, 2) max_width = selected_label.sizeHint().width() # line color self.lbl_color_select = ColorLabel() self.lbl_color_unselect = ColorLabel() self.lbl_color_select.setMaximumWidth(max_width) self.lbl_color_unselect.setMaximumWidth(max_width) style_box.addWidget(QLabel('Line color:'), 1, 0) style_box.addWidget(self.lbl_color_unselect, 1, 1) style_box.addWidget(self.lbl_color_select, 1, 2) # line width self.txt_line_width = QLineEdit() self.txt_line_width_select = QLineEdit() validator = QDoubleValidator() validator.setBottom(0) self.txt_line_width.setValidator(validator) self.txt_line_width_select.setValidator(validator) style_box.addWidget(QLabel('Line width:'), 2, 0) style_box.addWidget(self.txt_line_width, 2, 1) style_box.addWidget(self.txt_line_width_select, 2, 2) vbox.addLayout(style_box) # line style hbox_line_style = QHBoxLayout() hbox_line_style.addWidget(QLabel('Line style')) self.combo_line_style = QComboBox() hbox_line_style.addWidget(self.combo_line_style) vbox.addLayout(hbox_line_style) self.fill_linestyles() self.combo_line_style.setSizeAdjustPolicy(QComboBox.AdjustToContents) # marker style hbox_marker_style = QHBoxLayout() hbox_marker_style.addWidget(QLabel('Marker size')) self.txt_marker_size = QLineEdit() self.txt_marker_size.setMinimumWidth(40) self.txt_marker_size.setText(str(mpl.rcParams['lines.markersize'])) validator = QDoubleValidator() validator.setBottom(0) self.txt_marker_size.setValidator(validator) hbox_marker_style.addWidget(self.txt_marker_size) hbox_marker_style.addWidget(QLabel('Marker style')) self.combo_marker_style = QComboBox() hbox_marker_style.addWidget(self.combo_marker_style) vbox.addLayout(hbox_marker_style) self.setLayout(vbox) self.widgets2disable = [ self.lbl_color_select, self.lbl_color_unselect, self.cb_auto_hide, self.txt_line_width, self.txt_line_width_select, self.combo_line_style, self.cb_show_connected, self.txt_marker_size, self.combo_marker_style, self.cb_drag_vline, self.cb_drag_hline, self.cb_selectable_vline, self.cb_selectable_hline, self.cb_show_hlines, self.cb_show_vlines ] self.fill_markerstyles() self.tb_actions = [] # --------------------------------------------------------------------- # ---------------------------- connections ---------------------------- # --------------------------------------------------------------------- self.lbl_color_select.color_changed.connect(self.change_select_colors) self.lbl_color_unselect.color_changed.connect( self.change_unselect_colors) self.txt_line_width.textChanged.connect(self.change_line_widths) self.txt_line_width_select.textChanged.connect( self.change_selection_line_widths) self.cb_auto_hide.stateChanged.connect(self.change_auto_hide) self.combo_marker_style.currentIndexChanged.connect( self.change_marker_style) self.combo_line_style.currentIndexChanged.connect( self.change_line_style) self.txt_marker_size.textChanged.connect(self.change_marker_size) self.cb_show_connected.stateChanged.connect( self.change_show_connected_artists) self.cb_drag_hline.stateChanged.connect(self.change_hline_draggable) self.cb_drag_vline.stateChanged.connect(self.change_vline_draggable) self.cb_selectable_hline.stateChanged.connect( self.change_hline_selectable) self.cb_selectable_vline.stateChanged.connect( self.change_vline_selectable) self.cb_show_vlines.stateChanged.connect(self.change_show_vlines) self.cb_show_hlines.stateChanged.connect(self.change_show_hlines) def draw_figs(self): """Draw the figures of the :attr:`marks`""" for canvas in {m.fig.canvas for m in self.marks}: canvas.draw_idle() def fill_linestyles(self): """Fill the :attr:`combo_line_style` combobox""" self.line_styles = [('-', 'solid'), ('--', 'dashed'), ('-.', 'dashdot'), (':', 'dotted'), ('None', None, '', ' ')] self.combo_line_style.addItems([t[0] for t in self.line_styles]) def fill_markerstyles(self): """Fill the :attr:`combo_marker_style` combobox""" self.marker_styles = [('point', (".", )), ('pixel', (",", )), ('circle', ("o", )), ('triangle down', ("v", "1")), ('triangle up', ("^", "2")), ('triangle left', ("<", "3")), ('triangle right', (">", "4")), ('octagon', ("8", )), ('square', ("s", )), ('pentagon', ("p", )), ('plus (filled)', ("P", )), ('star', ("*", )), ('hexagon1', ("h", )), ('hexagon2', ("H", )), ('plus', ("+", )), ('x', ("x", )), ('x (filled)', ("X", )), ('diamond', ("D", )), ('thin diamond', ("d", )), ('vline', ("|", )), ('hline', ("_", )), ('no marker', ('None', None, '', ' '))] self.combo_marker_style.addItems([ '%s (%s)' % (key.capitalize(), ','.join(map(str, filter(None, t)))) for key, t in self.marker_styles ]) def set_marker_item(self, marker): """Switch the :attr:`combo_marker_style` to the given `marker` Parameters ---------- marker: str A matplotlib marker string""" for i, (key, t) in enumerate(self.marker_styles): if marker in t: block = self.combo_marker_style.blockSignals(True) self.combo_marker_style.setCurrentIndex(i) self.combo_marker_style.blockSignals(block) break def set_line_style_item(self, ls): """Switch the :attr:`combo_line_style` to the given linestyle Parameters ---------- ls: str The matplotlib linestyle string """ for i, t in enumerate(self.line_styles): if ls in t: block = self.combo_line_style.blockSignals(True) self.combo_line_style.setCurrentIndex(i) self.combo_line_style.blockSignals(block) break def should_be_enabled(self, w): """Check if a widget `w` should be enabled or disabled Parameters ---------- w: PyQt5.QtWidgets.QWidget The widget to (potentially) enable Returns ------- bool True if the :attr:`straditizer` of this instance has marks. Otherwise False""" return self.straditizer is not None and bool(self.straditizer.marks) def change_select_colors(self, color): """Change the selection color of the marks Change the selection color of the marks to the given color. Parameters ---------- color: PyQt5.QtGui.QColor or a matplotlib color The color to use """ if isinstance(color, QtGui.QColor): color = np.array(color.getRgb()) / 255. for mark in self.marks: key = 'color' if 'color' in mark._select_props else 'c' mark._select_props[key] = color mark._unselect_props[key] = mark.hline.get_c() self.draw_figs() def change_unselect_colors(self, color): """Change the :attr:`straditize.cross_mark.CrossMark.cunselect` color Change the unselection color of the marks to the given color. Parameters ---------- color: PyQt5.QtGui.QColor or a matplotlib color The color to use """ if isinstance(color, QtGui.QColor): color = np.array(color.getRgb()) / 255. for mark in self.marks: key = 'color' if 'color' in mark._unselect_props else 'c' mark._unselect_props[key] = color for l in chain(mark.hlines, mark.vlines, mark.line_connections): l.set_color(color) self.draw_figs() def change_line_widths(self, lw): """Change the linewidth of the marks Parameters ---------- lw: float The line width to use """ lw = float(lw or 0) for mark in self.marks: key = 'lw' if 'lw' in mark._unselect_props else 'linewidth' mark._unselect_props[key] = lw if not mark.auto_hide: for l in chain(mark.hlines, mark.vlines, mark.line_connections): l.set_lw(lw) self.draw_figs() def change_selection_line_widths(self, lw): """Change the linewidth for selected marks Parameters ---------- lw: float The linewidth for selected marks""" lw = float(lw or 0) for mark in self.marks: key = 'lw' if 'lw' in mark._select_props else 'linewidth' mark._select_props[key] = lw def change_auto_hide(self, auto_hide): """Toggle the :attr:`~straditize.cross_mark.CrossMark.auto_hide` This method disables or enables the :attr:`~straditize.cross_mark.CrossMark.auto_hide` of the marks Parameters ---------- auto_hide: bool or PyQt5.QtGui.Qt.Checked or PyQt5.QtGui.Qt.Unchecked The value to use for the auto_hide. :data:`PyQt5.QtGui.Qt.Checked` is equivalent to ``True`` """ if auto_hide is Qt.Checked: auto_hide = True elif auto_hide is Qt.Unchecked: auto_hide = False for mark in self.marks: mark.auto_hide = auto_hide if auto_hide: for l in chain(mark.hlines, mark.vlines, mark.line_connections): l.set_lw(0) else: lw = mark._unselect_props.get( 'lw', mark._unselect_props.get('linewidth')) for l in chain(mark.hlines, mark.vlines, mark.line_connections): l.set_lw(lw) self.draw_figs() def change_show_connected_artists(self, show): """Change the visibility of connected artists Parameters ---------- show: bool The visibility for the :meth:`straditize.cross_mark.CrossMarks.set_connected_artists_visible` method""" if show is Qt.Checked: show = True elif show is Qt.Unchecked: show = False for mark in self.marks: mark.set_connected_artists_visible(show) self.draw_figs() def change_line_style(self, i): """Change the line style of the marks Parameters ---------- i: int The index of the line style in the :attr:`line_styles` attribute to use """ ls = self.line_styles[i][0] for mark in self.marks: for l in chain(mark.hlines, mark.vlines, mark.line_connections): l.set_ls(ls) self.draw_figs() def change_marker_style(self, i): """Change the marker style of the marks Parameters ---------- i: int The index of the marker style in the :attr:`marker_styles` attribute to use """ marker = self.marker_styles[i][1][0] for mark in self.marks: for l in chain(mark.hlines, mark.vlines, mark.line_connections): l.set_marker(marker) self.draw_figs() def change_marker_size(self, markersize): """Change the size of the markers Parameters ---------- markersize: float The size of the marker to use""" markersize = float(markersize or 0) for mark in self.marks: for l in chain(mark.hlines, mark.vlines, mark.line_connections): l.set_markersize(markersize) self.draw_figs() def fill_from_mark(self, mark): """Set the widgets of this :class:`MarkerControl` from a mark This method sets the color labels, combo boxes, check boxes and text edits to match the properties of the given `mark` Parameters ---------- mark: straditize.cross_mark.CrossMark The mark to use the attributes from """ line = mark.hline try: cselect = mark._select_props['c'] except KeyError: try: cselect = mark._select_props['color'] except KeyError: cselect = mark.hline.get_c() lw_key = 'lw' if 'lw' in mark._unselect_props else 'linewidth' self.set_line_style_item(line.get_linestyle()) self.set_marker_item(line.get_marker()) self.txt_line_width.setText(str(mark._unselect_props.get(lw_key, 0))) self.txt_line_width_select.setText( str(mark._select_props.get(lw_key, 0))) self.txt_marker_size.setText(str(line.get_markersize())) self.lbl_color_select._set_color(cselect) self.lbl_color_unselect._set_color(mark.hline.get_c()) self.cb_auto_hide.setChecked(mark.auto_hide) self.cb_show_connected.setChecked(mark.show_connected_artists) try: mark.x except NotImplementedError: self.cb_drag_vline.setEnabled(False) self.cb_show_vlines.setEnabled(False) self.cb_selectable_vline.setEnabled(False) else: self.cb_drag_vline.setEnabled(True) self.cb_show_vlines.setEnabled(True) self.cb_selectable_vline.setEnabled(True) try: mark.y except NotImplementedError: self.cb_drag_hline.setEnabled(False) self.cb_show_hlines.setEnabled(False) self.cb_selectable_hline.setEnabled(False) else: self.cb_drag_hline.setEnabled(True) self.cb_show_hlines.setEnabled(True) self.cb_selectable_hline.setEnabled(True) self.cb_drag_hline.setChecked('h' in mark.draggable) self.cb_drag_vline.setChecked('v' in mark.draggable) self.cb_show_hlines.setChecked(not mark.hide_horizontal) self.cb_show_vlines.setChecked(not mark.hide_vertical) self.cb_selectable_hline.setChecked('h' in mark.selectable) self.cb_selectable_vline.setChecked('v' in mark.selectable) @property def line_props(self): """The properties of the lines as a :class:`dict`""" return { 'ls': self.combo_line_style.currentText(), 'marker': self.marker_styles[self.combo_marker_style.currentIndex()][1][0], 'lw': float(self.txt_line_width.text().strip() or 0), 'markersize': float(self.txt_marker_size.text().strip() or 0), 'c': self.lbl_color_unselect.color.getRgbF(), } @property def select_props(self): """The properties of selected marks as a :class:`dict`""" return { 'c': self.lbl_color_select.color.getRgbF(), 'lw': float(self.txt_line_width_select.text().strip() or 0) } def update_mark(self, mark): """Update the properties of a mark to match the settings Parameters ---------- mark: straditize.cross_mark.CrossMarks The mark to update """ if len(self.straditizer.marks) < 2: return # line properties props = self.line_props mark._unselect_props.update(props) mark._select_props.update(self.select_props) # auto_hide auto_hide = self.cb_auto_hide.isChecked() mark.auto_hide = auto_hide if auto_hide: props['lw'] = 0 # show_connected show_connected = self.cb_show_connected.isChecked() mark.set_connected_artists_visible(show_connected) # drag hline drag_hline = self.cb_drag_hline.isChecked() if drag_hline and 'h' not in mark.draggable: mark.draggable.append('h') elif not drag_hline and 'h' in mark.draggable: mark.draggable.remove('h') # drag vline drag_vline = self.cb_drag_vline.isChecked() if drag_vline and 'v' not in mark.draggable: mark.draggable.append('v') elif not drag_vline and 'v' in mark.draggable: mark.draggable.remove('v') # select hline select_hline = self.cb_selectable_hline.isChecked() if select_hline and 'h' not in mark.selectable: mark.selectable.append('h') elif not select_hline and 'h' in mark.selectable: mark.selectable.remove('h') # select hline select_vline = self.cb_selectable_vline.isChecked() if select_vline and 'v' not in mark.selectable: mark.selectable.append('v') elif not select_vline and 'v' in mark.selectable: mark.selectable.remove('v') show_horizontal = self.cb_show_hlines.isChecked() mark.hide_horizontal = ~show_horizontal show_vertical = self.cb_show_vlines.isChecked() mark.hide_vertical = ~show_vertical for l in chain(mark.hlines, mark.vlines): l.update(props) for l in mark.hlines: l.set_visible(show_horizontal) for l in mark.vlines: l.set_visible(show_vertical) def change_hline_draggable(self, state): """Enable or disable the dragging of horizontal lines Parameters ---------- state: Qt.Checked or Qt.Unchecked If Qt.Checked, the horizontal lines can be dragged and dropped""" if state == Qt.Checked: for mark in self.marks: mark.draggable = np.unique(np.r_[['h'], mark.draggable]) else: for mark in self.marks: mark.draggable = np.array(list(set(mark.draggable) - {'h'})) def change_hline_selectable(self, state): """Enable or disable the selection of horizontal lines Parameters ---------- state: Qt.Checked or Qt.Unchecked If Qt.Checked, the horizontal lines can be selected""" if state == Qt.Checked: for mark in self.marks: mark.selectable = np.unique(np.r_[['h'], mark.selectable]) else: for mark in self.marks: mark.selectable = np.array(list(set(mark.selectable) - {'h'})) def change_vline_draggable(self, state): """Enable or disable the dragging of vertical lines Parameters ---------- state: Qt.Checked or Qt.Unchecked If Qt.Checked, the vertical lines can be dragged and dropped""" if state == Qt.Checked: for mark in self.marks: mark.draggable = np.unique(np.r_[['v'], mark.draggable]) else: for mark in self.marks: mark.draggable = np.array(list(set(mark.draggable) - {'v'})) def change_vline_selectable(self, state): """Enable or disable the selection of vertical lines Parameters ---------- state: Qt.Checked or Qt.Unchecked If Qt.Checked, the vertical lines can be selected""" if state == Qt.Checked: for mark in self.marks: mark.selectable = np.unique(np.r_[['v'], mark.selectable]) else: for mark in self.marks: mark.selectable = np.array(list(set(mark.selectable) - {'v'})) def change_show_hlines(self, state): """Enable of disable the visibility of horizontal lines Parameters ---------- state: Qt.Checked or Qt.Unchecked If Qt.Checked, all horizontal lines are hidden""" if state == Qt.Checked: for mark in self.marks: mark.hide_horizontal = False mark.set_visible(True) else: for mark in self.marks: mark.hide_horizontal = True mark.set_visible(True) self.draw_figs() def change_show_vlines(self, state): """Enable of disable the visibility of vertical lines Parameters ---------- state: Qt.Checked or Qt.Unchecked If Qt.Checked, all vertical lines are hidden""" if state == Qt.Checked: for mark in self.marks: mark.hide_vertical = False mark.set_visible(True) else: for mark in self.marks: mark.hide_vertical = True mark.set_visible(True) self.draw_figs() def enable_or_disable_widgets(self, b): """Renabled to use the :meth:`refresh` method """ self.refresh() def fill_after_adding(self, mark): if not self._filled: self.refresh() def go_to_right_mark(self): """Move the plot to the next right cross mark""" ax = self.straditizer.marks[0].ax if ax.xaxis_inverted(): return self.go_to_smaller_x_mark(min(ax.get_xlim())) else: return self.go_to_greater_x_mark(max(ax.get_xlim())) def go_to_left_mark(self): """Move the plot to the previous left cross mark""" ax = self.straditizer.marks[0].ax if ax.xaxis_inverted(): return self.go_to_greater_x_mark(max(ax.get_xlim())) else: return self.go_to_smaller_x_mark(min(ax.get_xlim())) def go_to_greater_x_mark(self, x): """Move the plot to the next mark with a x-position greater than `x` Parameters ---------- x: float The reference x-position that shall be smaller than the new centered mark""" def is_visible(mark): return (np.searchsorted(np.sort(xlim), mark.xa) == 1).any() and (np.searchsorted( np.sort(ylim), mark.ya) == 1).any() ax = next(self.marks).ax xlim = np.asarray(ax.get_xlim()) ylim = np.asarray(ax.get_ylim()) marks = self.straditizer.marks if len(marks[0].xa) > 1: # get the mark in the center yc = ylim.mean() try: mark = min(filter(is_visible, marks), key=lambda m: np.abs(m.ya - yc).min()) except ValueError: # empty sequence mark = min(marks, key=lambda m: np.abs(m.ya - yc).min()) # if all edges are visible already, we return if (np.searchsorted(xlim, mark.xa) == 1).all(): return mask = mark.xa > x if not mask.any(): # already on the right side return i = mark.xa[mask].argmin() x = mark.xa[mask][i] dx = np.diff(xlim) / 2. ax.set_xlim(x - dx, x + dx) ax.set_ylim(*ax.get_ylim()) else: distances = ((mark.xa > x).any() and (mark.xa[mark.xa > x] - x).min() for mark in marks) try: dist, mark = min((t for t in zip(distances, marks) if t[0]), key=lambda t: t[0]) except ValueError: # empty sequence return mask = mark.xa > x i = mark.xa[mask].argmin() x = mark.xa[mask][i] j = np.abs(mark.ya - ylim.mean()).argmin() y = mark.ya[j] dx = np.diff(xlim) / 2. ax.set_xlim(x - dx, x + dx) dy = np.diff(ylim) / 2. ax.set_ylim(y - dy, y + dy) self.straditizer.draw_figure() def go_to_smaller_x_mark(self, x): """Move the plot to the next mark with a x-position smaller than `x` Parameters ---------- x: float The reference x-position that shall be greater than the new centered mark""" def is_visible(mark): return (np.searchsorted(np.sort(xlim), mark.xa) == 1).any() and (np.searchsorted( np.sort(ylim), mark.ya) == 1).any() ax = next(self.marks).ax xlim = np.asarray(ax.get_xlim()) ylim = np.asarray(ax.get_ylim()) marks = self.straditizer.marks if len(marks[0].xa) > 1: # get the mark in the center yc = ylim.mean() try: mark = min(filter(is_visible, marks), key=lambda m: np.abs(m.ya - yc).min()) except ValueError: # empty sequence mark = min(marks, key=lambda m: np.abs(m.ya - yc).min()) # if all edges are visible already, we return if (np.searchsorted(xlim, mark.xa) == 1).all(): return mask = mark.xa < x if not mask.any(): # already on the right side return i = mark.xa[mask].argmin() x = mark.xa[mask][i] dx = np.diff(xlim) / 2. ax.set_xlim(x - dx, x + dx) ax.set_ylim(*ax.get_ylim()) else: distances = ((mark.xa < x).any() and (mark.xa[mark.xa < x] - x).min() for mark in marks) try: dist, mark = min((t for t in zip(distances, marks) if t[0]), key=lambda t: t[0]) except ValueError: # empty sequence return mask = mark.xa < x i = mark.xa[mask].argmin() x = mark.xa[mask][i] j = np.abs(mark.ya - ylim.mean()).argmin() y = mark.ya[j] dx = np.diff(xlim) / 2. ax.set_xlim(x - dx, x + dx) dy = np.diff(ylim) / 2. ax.set_ylim(y - dy, y + dy) self.straditizer.draw_figure() def go_to_upper_mark(self): """Go to the next mark above the current y-limits""" ax = self.straditizer.marks[0].ax if ax.xaxis_inverted(): return self.go_to_greater_y_mark(max(ax.get_ylim())) else: return self.go_to_smaller_y_mark(min(ax.get_ylim())) def go_to_lower_mark(self): """Go to the next mark below the current y-limits""" ax = self.straditizer.marks[0].ax if ax.xaxis_inverted(): return self.go_to_smaller_y_mark(min(ax.get_ylim())) else: return self.go_to_greater_y_mark(max(ax.get_ylim())) def go_to_greater_y_mark(self, y): """Move the plot to the next mark with a y-position greater than `y` Parameters ---------- y: float The reference y-position that shall be smaller than the new centered mark""" def is_visible(mark): return (np.searchsorted(np.sort(xlim), mark.xa) == 1).any() and (np.searchsorted( np.sort(ylim), mark.ya) == 1).any() ax = next(self.marks).ax xlim = np.asarray(ax.get_xlim()) ylim = np.asarray(ax.get_ylim()) marks = self.straditizer.marks if len(marks[0].ya) > 1: # get the mark in the center xc = xlim.mean() try: mark = min(filter(is_visible, marks), key=lambda m: np.abs(m.xa - xc).min()) except ValueError: # empty sequence mark = min(marks, key=lambda m: np.abs(m.xa - xc).min()) # if all edges are visible already, we return if (np.searchsorted(ylim, mark.ya) == 1).all(): return mask = mark.ya > y if not mask.any(): # already on the right side return i = mark.ya[mask].argmin() y = mark.ya[mask][i] dy = np.diff(ylim) / 2. ax.set_ylim(y - dy, y + dy) else: distances = ((mark.ya > y).any() and (mark.xa[mark.ya > y] - y).min() for mark in marks) try: dist, mark = min((t for t in zip(distances, marks) if t[0]), key=lambda t: t[0]) except ValueError: # empty sequence return mask = mark.ya > y i = mark.ya[mask].argmin() y = mark.ya[mask][i] j = np.abs(mark.xa - xlim.mean()).argmin() x = mark.xa[j] dx = np.diff(xlim) / 2. ax.set_xlim(x - dx, x + dx) dy = np.diff(ylim) / 2. ax.set_ylim(y - dy, y + dy) self.straditizer.draw_figure() def go_to_smaller_y_mark(self, y): """Move the plot to the next mark with a y-position smaller than `x` Parameters ---------- y: float The reference y-position that shall be smaller than the new centered mark""" def is_visible(mark): return (np.searchsorted(np.sort(xlim), mark.xa) == 1).any() and (np.searchsorted( np.sort(ylim), mark.ya) == 1).any() ax = next(self.marks).ax xlim = np.asarray(ax.get_xlim()) ylim = np.asarray(ax.get_ylim()) marks = self.straditizer.marks if len(marks[0].ya) > 1: # get the mark in the center xc = xlim.mean() try: mark = min(filter(is_visible, marks), key=lambda m: np.abs(m.xa - xc).min()) except ValueError: # empty sequence mark = min(marks, key=lambda m: np.abs(m.xa - xc).min()) # if all edges are visible already, we return if (np.searchsorted(ylim, mark.ya) == 1).all(): return mask = mark.ya < y if not mask.any(): # already on the right side return i = mark.ya[mask].argmin() y = mark.ya[mask][i] dy = np.diff(ylim) / 2. ax.set_ylim(y - dy, y + dy) else: distances = ((mark.ya < y).any() and (mark.ya[mark.ya < y] - y).min() for mark in marks) try: dist, mark = min((t for t in zip(distances, marks) if t[0]), key=lambda t: t[0]) except ValueError: # empty sequence return mask = mark.ya < y i = mark.ya[mask].argmin() y = mark.ya[mask][i] j = np.abs(mark.xa - xlim.mean()).argmin() x = mark.xa[j] dx = np.diff(xlim) / 2. ax.set_xlim(x - dx, x + dx) dy = np.diff(ylim) / 2. ax.set_ylim(y - dy, y + dy) self.straditizer.draw_figure() def add_toolbar_widgets(self, mark): """Add the navigation actions to the toolbar""" tb = self.straditizer.marks[0].ax.figure.canvas.toolbar if not isinstance(tb, QToolBar): return if self.tb_actions: self.remove_actions() self.tb_actions.append(tb.addSeparator()) try: mark.x except NotImplementedError: add_right = False else: a = tb.addAction(QIcon(get_icon('left_mark.png')), 'left mark', self.go_to_left_mark) a.setToolTip('Move to the next cross mark on the left') self.tb_actions.append(a) add_right = True try: mark.y except NotImplementedError: pass else: a = tb.addAction(QIcon(get_icon('upper_mark.png')), 'upper mark', self.go_to_upper_mark) a.setToolTip('Move to the next cross mark above') self.tb_actions.append(a) a = tb.addAction(QIcon(get_icon('lower_mark.png')), 'lower mark', self.go_to_lower_mark) a.setToolTip('Move to the next cross mark below') self.tb_actions.append(a) if add_right: a = tb.addAction(QIcon(get_icon('right_mark.png')), 'right mark', self.go_to_right_mark) a.setToolTip('Move to the next cross mark on the right') self.tb_actions.append(a) self._toolbar = tb def remove_actions(self): """Remove the navigation actions from the toolbar""" if self._toolbar is None: return tb = self._toolbar for a in self.tb_actions: tb.removeAction(a) self.tb_actions.clear() def refresh(self): """Reimplemented to also set the properties of this widget """ super(MarkerControl, self).refresh() if self.straditizer is not None and self.straditizer.marks: self._filled = True mark = self.straditizer.marks[0] self.fill_from_mark(mark) self.add_toolbar_widgets(mark) else: self.remove_actions() self._filled = False if self.straditizer is not None: self.straditizer.mark_added.connect(self.fill_after_adding) self.straditizer.mark_added.connect(self.update_mark)
class StraditizerWidgets(QWidget, DockMixin): """A widget that contains widgets to control the straditization in a GUI This widget is the basis of the straditize GUI and implemented as a plugin into the psyplot gui. The open straditizers are handled in the :attr:`_straditizer` attribute. The central parts of this widget are - The combobox to manage the open straditizers - The QTreeWidget in the :attr:`tree` attribute that contains all the controls to interface the straditizer - the tutorial area - the :guilabel:`Apply` and :guilabel:`Cancel` button""" #: Boolean that is True if all dialogs should be answered with `Yes` always_yes = False #: The QTreeWidget that contains the different widgets for the digitization tree = None #: The apply button apply_button = None #: The cancel button cancel_button = None #: The button to edit the straditizer attributes attrs_button = None #: The button to start a tutorial tutorial_button = None #: An :class:`InfoButton` to display the docs info_button = None #: A QComboBox to select the current straditizer stradi_combo = None #: A button to open a new straditizer btn_open_stradi = None #: A button to close the current straditizer btn_close_stradi = None #: A button to reload the last autosaved state btn_reload_autosaved = None #: The :class:`straditize.widgets.progress_widget.ProgressWidget` to #: display the progress of the straditization progress_widget = None #: The :class:`straditize.widgets.data.DigitizingControl` to interface #: the :straditize.straditizer.Straditizer.data_reader` digitizer = None #: The :class:`straditize.widgets.colnames.ColumnNamesManager` to interface #: the :straditize.straditizer.Straditizer.colnames_reader` colnames_manager = None #: The :class:`straditize.widgets.axes_translations.AxesTranslations` to #: handle the y- and x-axis conversions axes_translations = None #: The :class:`straditize.widgets.image_correction.ImageRescaler` class to #: rescale the image image_rescaler = None #: The :class:`straditize.widgets.image_correction.ImageRotator` class to #: rotate the image image_rotator = None #: The :class:`straditize.widgets.plots.PlotControl` to display additional #: information on the diagram plot_control = None #: The :class:`straditize.widgets.marker_control.MarkerControl` to modify #: the appearance of the :class:`~straditize.straditizer.Straditizer.marks` #: of the current straditizer marker_control = None #: The :class:`straditize.widgets.selection_toolbar.SelectionToolbar` to #: select features in the stratigraphic diagram selection_toolbar = None #: The :class:`straditize.straditizer.Straditizer` instance straditizer = None #: open straditizers _straditizers = [] #: The :class:`straditize.widgets.tutorial.Tutorial` class tutorial = None dock_position = Qt.LeftDockWidgetArea #: Auto-saved straditizers autosaved = [] hidden = True title = 'Stratigraphic diagram digitization' window_layout_action = None open_external = QtCore.pyqtSignal(list) def __init__(self, *args, **kwargs): from straditize.widgets.menu_actions import StraditizerMenuActions from straditize.widgets.progress_widget import ProgressWidget from straditize.widgets.data import DigitizingControl from straditize.widgets.selection_toolbar import SelectionToolbar from straditize.widgets.marker_control import MarkerControl from straditize.widgets.plots import PlotControl from straditize.widgets.axes_translations import AxesTranslations from straditize.widgets.image_correction import (ImageRotator, ImageRescaler) from straditize.widgets.colnames import ColumnNamesManager self._straditizers = [] super(StraditizerWidgets, self).__init__(*args, **kwargs) self.tree = QTreeWidget(parent=self) self.tree.setSelectionMode(QTreeWidget.NoSelection) self.refresh_button = QToolButton(self) self.refresh_button.setIcon(QIcon(get_psy_icon('refresh.png'))) self.refresh_button.setToolTip('Refresh from the straditizer') self.apply_button = EnableButton('Apply', parent=self) self.cancel_button = EnableButton('Cancel', parent=self) self.attrs_button = QPushButton('Attributes', parent=self) self.tutorial_button = QPushButton('Tutorial', parent=self) self.tutorial_button.setCheckable(True) self.error_msg = PyErrorMessage(self) self.stradi_combo = QComboBox() self.btn_open_stradi = QToolButton() self.btn_open_stradi.setIcon(QIcon(get_psy_icon('run_arrow.png'))) self.btn_close_stradi = QToolButton() self.btn_close_stradi.setIcon(QIcon(get_psy_icon('invalid.png'))) self.btn_reload_autosaved = QPushButton("Reload") self.btn_reload_autosaved.setToolTip( "Close the straditizer and reload the last autosaved project") # --------------------------------------------------------------------- # --------------------------- Tree widgets ---------------------------- # --------------------------------------------------------------------- self.tree.setHeaderLabels(['', '']) self.tree.setColumnCount(2) self.progress_item = QTreeWidgetItem(0) self.progress_item.setText(0, 'ToDo list') self.progress_widget = ProgressWidget(self, self.progress_item) self.menu_actions_item = QTreeWidgetItem(0) self.menu_actions_item.setText(0, 'Images import/export') self.tree.addTopLevelItem(self.menu_actions_item) self.menu_actions = StraditizerMenuActions(self) self.digitizer_item = item = QTreeWidgetItem(0) item.setText(0, 'Digitization control') self.digitizer = DigitizingControl(self, item) self.col_names_item = item = QTreeWidgetItem(0) item.setText(0, 'Column names') self.colnames_manager = ColumnNamesManager(self, item) self.add_info_button(item, 'column_names.rst') self.axes_translations_item = item = QTreeWidgetItem(0) item.setText(0, 'Axes translations') self.axes_translations = AxesTranslations(self, item) self.image_transform_item = item = QTreeWidgetItem(0) item.setText(0, 'Transform source image') self.image_rescaler = ImageRescaler(self, item) self.image_rotator_item = item = QTreeWidgetItem(0) item.setText(0, 'Rotate image') self.image_rotator = ImageRotator(self) self.image_transform_item.addChild(item) self.image_rotator.setup_children(item) self.plot_control_item = item = QTreeWidgetItem(0) item.setText(0, 'Plot control') self.plot_control = PlotControl(self, item) self.add_info_button(item, 'plot_control.rst') self.marker_control_item = item = QTreeWidgetItem(0) item.setText(0, 'Marker control') self.marker_control = MarkerControl(self, item) self.add_info_button(item, 'marker_control.rst') # --------------------------------------------------------------------- # ----------------------------- Toolbars ------------------------------ # --------------------------------------------------------------------- self.selection_toolbar = SelectionToolbar(self, 'Selection toolbar') # --------------------------------------------------------------------- # ----------------------------- InfoButton ---------------------------- # --------------------------------------------------------------------- self.info_button = InfoButton(self, get_doc_file('straditize.rst')) # --------------------------------------------------------------------- # --------------------------- Layouts --------------------------------- # --------------------------------------------------------------------- stradi_box = QHBoxLayout() stradi_box.addWidget(self.stradi_combo, 1) stradi_box.addWidget(self.btn_open_stradi) stradi_box.addWidget(self.btn_close_stradi) attrs_box = QHBoxLayout() attrs_box.addWidget(self.attrs_button) attrs_box.addStretch(0) attrs_box.addWidget(self.tutorial_button) btn_box = QHBoxLayout() btn_box.addWidget(self.refresh_button) btn_box.addWidget(self.info_button) btn_box.addStretch(0) btn_box.addWidget(self.apply_button) btn_box.addWidget(self.cancel_button) reload_box = QHBoxLayout() reload_box.addWidget(self.btn_reload_autosaved) reload_box.addStretch(0) vbox = QVBoxLayout() vbox.addLayout(stradi_box) vbox.addWidget(self.tree) vbox.addLayout(attrs_box) vbox.addLayout(btn_box) vbox.addLayout(reload_box) self.setLayout(vbox) self.apply_button.setEnabled(False) self.cancel_button.setEnabled(False) self.tree.expandItem(self.progress_item) self.tree.expandItem(self.digitizer_item) # --------------------------------------------------------------------- # --------------------------- Connections ----------------------------- # --------------------------------------------------------------------- self.stradi_combo.currentIndexChanged.connect(self.set_current_stradi) self.refresh_button.clicked.connect(self.refresh) self.attrs_button.clicked.connect(self.edit_attrs) self.tutorial_button.clicked.connect(self.start_tutorial) self.open_external.connect(self._create_straditizer_from_args) self.btn_open_stradi.clicked.connect( self.menu_actions.open_straditizer) self.btn_close_stradi.clicked.connect(self.close_straditizer) self.btn_reload_autosaved.clicked.connect(self.reload_autosaved) self.refresh() header = self.tree.header() header.setStretchLastSection(False) header.setSectionResizeMode(0, QHeaderView.Stretch) def disable_apply_button(self): """Method that is called when the :attr:`cancel_button` is clicked""" for w in [self.apply_button, self.cancel_button]: try: w.clicked.disconnect() except TypeError: pass w.setEnabled(False) self.apply_button.setText('Apply') self.cancel_button.setText('Cancel') self.refresh_button.setEnabled(True) def switch_to_straditizer_layout(self): """Switch to the straditizer layout This method makes this widget visible and stacks it with the psyplot content widget""" mainwindow = self.dock.parent() mainwindow.figures_tree.hide_plugin() mainwindow.ds_tree.hide_plugin() mainwindow.fmt_widget.hide_plugin() self.show_plugin() mainwindow.tabifyDockWidget(mainwindow.project_content.dock, self.dock) hsize = self.marker_control.sizeHint().width() + 50 self.menu_actions.setup_shortcuts(mainwindow) if with_qt5: mainwindow.resizeDocks([self.dock], [hsize], Qt.Horizontal) self.tree.resizeColumnToContents(0) self.tree.resizeColumnToContents(1) self.info_button.click() def to_dock(self, main, *args, **kwargs): ret = super(StraditizerWidgets, self).to_dock(main, *args, **kwargs) if self.menu_actions.window_layout_action is None: main.window_layouts_menu.addAction(self.window_layout_action) main.callbacks['straditize'] = self.open_external.emit main.addToolBar(self.selection_toolbar) self.dock.toggleViewAction().triggered.connect( self.show_or_hide_toolbar) self.menu_actions.setup_menu_actions(main) self.menu_actions.setup_children(self.menu_actions_item) try: main.open_file_options['Straditize project'] = \ self.create_straditizer_from_args except AttributeError: # psyplot-gui <= 1.1.0 pass return ret def show_or_hide_toolbar(self): """Show or hide the toolbar depending on the visibility of this widget """ self.selection_toolbar.setVisible(self.is_shown) def _create_straditizer_from_args(self, args): """A method that is called when the :attr:`psyplot_gui.main.mainwindow` receives a 'straditize' callback""" self.create_straditizer_from_args(*args) def create_straditizer_from_args(self, fnames, project=None, xlim=None, ylim=None, full=False, reader_type='area'): """Create a straditizer from the given file name This method is called when the :attr:`psyplot_gui.main.mainwindow` receives a 'straditize' callback""" fname = fnames[0] if fname is not None: self.menu_actions.open_straditizer(fname) stradi = self.straditizer if stradi is None: return if xlim is not None: stradi.data_xlim = xlim if ylim is not None: stradi.data_ylim = ylim if xlim is not None or ylim is not None or full: if stradi.data_xlim is None: stradi.data_xlim = [0, np.shape(stradi.image)[1]] if stradi.data_ylim is None: stradi.data_ylim = [0, np.shape(stradi.image)[0]] stradi.init_reader(reader_type) stradi.data_reader.digitize() self.refresh() if not self.is_shown: self.switch_to_straditizer_layout() return fname is not None def start_tutorial(self, state, tutorial_cls=None): """Start or stop the tutorial Parameters ---------- state: bool If False, the tutorial is stopped. Otherwise it is started tutorial_cls: straditize.widgets.tutorial.beginner.Tutorial The tutorial class to use. If None, it will be asked in a QInputDialog""" if self.tutorial is not None or not state: self.tutorial.close() self.tutorial_button.setText('Tutorial') elif state: if tutorial_cls is None: tutorial_cls, ok = QInputDialog.getItem( self, 'Start tutorial', "Select the tutorial type", ["Beginner", "Advanced (Hoya del Castillo)"], editable=False) if not ok: self.tutorial_button.blockSignals(True) self.tutorial_button.setChecked(False) self.tutorial_button.blockSignals(False) return if tutorial_cls == 'Beginner': from straditize.widgets.tutorial import Tutorial else: from straditize.widgets.tutorial import ( HoyaDelCastilloTutorial as Tutorial) else: Tutorial = tutorial_cls self.tutorial = Tutorial(self) self.tutorial_button.setText('Stop tutorial') def edit_attrs(self): """Edit the attributes of the current straditizer This creates a new dataframe editor to edit the :attr:`straditize.straditizer.Straditizer.attrs` meta informations""" def add_attr(key): model = editor.table.model() n = len(attrs) model.insertRow(n) model.setData(model.index(n, 0), key) model.setData(model.index(n, 1), '', change_type=six.text_type) from psyplot_gui.main import mainwindow from straditize.straditizer import common_attributes attrs = self.straditizer.attrs editor = mainwindow.new_data_frame_editor(attrs, 'Straditizer attributes') editor.table.resizeColumnToContents(1) editor.table.horizontalHeader().setVisible(False) editor.table.frozen_table_view.horizontalHeader().setVisible(False) combo = QComboBox() combo.addItems([''] + common_attributes) combo.currentTextChanged.connect(add_attr) hbox = QHBoxLayout() hbox.addWidget(QLabel('Common attributes:')) hbox.addWidget(combo) hbox.addStretch(0) editor.layout().insertLayout(1, hbox) return editor, combo def refresh(self): """Refresh from the straditizer""" for i, stradi in enumerate(self._straditizers): self.stradi_combo.setItemText( i, self.get_attr(stradi, 'project_file') or self.get_attr(stradi, 'image_file') or '') # toggle visibility of close button and attributes button enable = self.straditizer is not None self.btn_close_stradi.setVisible(enable) self.attrs_button.setEnabled(enable) # refresh controls self.menu_actions.refresh() self.progress_widget.refresh() self.digitizer.refresh() self.selection_toolbar.refresh() self.plot_control.refresh() self.marker_control.refresh() self.axes_translations.refresh() if self.tutorial is not None: self.tutorial.refresh() self.image_rotator.refresh() self.image_rescaler.refresh() self.colnames_manager.refresh() self.btn_reload_autosaved.setEnabled(bool(self.autosaved)) def get_attr(self, stradi, attr): try: return stradi.get_attr(attr) except KeyError: pass docstrings.delete_params('InfoButton.parameters', 'parent') @docstrings.get_sectionsf('StraditizerWidgets.add_info_button') @docstrings.with_indent(8) def add_info_button(self, child, fname=None, rst=None, name=None, connections=[]): """Add an infobutton to the :attr:`tree` widget Parameters ---------- child: QTreeWidgetItem The item to which to add the infobutton %(InfoButton.parameters.no_parent)s connections: list of QPushButtons Buttons that should be clicked when the info button is clicked""" button = InfoButton(self, fname=fname, rst=rst, name=name) self.tree.setItemWidget(child, 1, button) for btn in connections: btn.clicked.connect(button.click) return button def raise_figures(self): """Raise the figures of the current straditizer in the GUI""" from psyplot_gui.main import mainwindow if mainwindow.figures and self.straditizer: dock = self.straditizer.ax.figure.canvas.manager.window dock.widget().show_plugin() dock.raise_() if self.straditizer.magni is not None: dock = self.straditizer.magni.ax.figure.canvas.manager.window dock.widget().show_plugin() dock.raise_() def set_current_stradi(self, i): """Set the i-th straditizer to the current one""" if not self._straditizers: return self.straditizer = self._straditizers[i] self.menu_actions.set_stradi_in_console() block = self.stradi_combo.blockSignals(True) self.stradi_combo.setCurrentIndex(i) self.stradi_combo.blockSignals(block) self.raise_figures() self.refresh() self.autosaved.clear() def _close_stradi(self, stradi): """Close the given straditizer and all it's figures""" is_current = stradi is self.straditizer if is_current: self.selection_toolbar.disconnect() stradi.close() try: i = self._straditizers.index(stradi) except ValueError: pass else: del self._straditizers[i] self.stradi_combo.removeItem(i) if is_current and self._straditizers: self.stradi_combo.setCurrentIndex(0) elif not self._straditizers: self.straditizer = None self.refresh() self.digitizer.digitize_item.takeChildren() self.digitizer.btn_digitize.setChecked(False) self.digitizer.btn_digitize.setCheckable(False) self.digitizer.toggle_txt_tolerance('') def close_straditizer(self): """Close the current straditizer""" self._close_stradi(self.straditizer) def close_all_straditizers(self): """Close all straditizers""" self.selection_toolbar.disconnect() for stradi in self._straditizers: stradi.close() self._straditizers.clear() self.straditizer = None self.stradi_combo.clear() self.digitizer.digitize_item.takeChildren() self.digitizer.btn_digitize.setChecked(False) self.digitizer.btn_digitize.setCheckable(False) self.digitizer.toggle_txt_tolerance('') self.refresh() def add_straditizer(self, stradi): """Add a straditizer to the list of open straditizers""" if stradi and stradi not in self._straditizers: self._straditizers.append(stradi) self.stradi_combo.addItem(' ') self.set_current_stradi(len(self._straditizers) - 1) def reset_control(self): """Reset the GUI of straditize""" if getattr(self.selection_toolbar, '_pattern_selection', None): self.selection_toolbar._pattern_selection.remove_plugin() del self.selection_toolbar._pattern_selection if getattr(self.digitizer, '_samples_editor', None): self.digitizer._close_samples_fig() tb = self.selection_toolbar tb.set_label_wand_mode() tb.set_rect_select_mode() tb.new_select_action.setChecked(True) tb.select_action.setChecked(False) tb.wand_action.setChecked(False) self.disable_apply_button() self.close_all_straditizers() self.colnames_manager.reset_control() def autosave(self): """Autosave the current straditizer""" self.autosaved = [self.straditizer.to_dataset().copy(True)] + \ self.autosaved[:4] def reload_autosaved(self): """Reload the autosaved straditizer and close the old one""" from straditize.straditizer import Straditizer if not self.autosaved: return answer = QMessageBox.question( self, 'Reload autosave', 'Shall I reload the last autosaved stage? This will close the ' 'current figures.') if answer == QMessageBox.Yes: self.close_straditizer() stradi = Straditizer.from_dataset(self.autosaved.pop(0)) self.menu_actions.finish_loading(stradi)
class HelpExplorer(QWidget, DockMixin): """A widget for showing the documentation. It behaves somewhat similar to spyders object inspector plugin and can show restructured text either as html (if sphinx is installed) or as plain text. It furthermore has a browser to show html content Warnings -------- The :class:`HelpBrowser` class is known to crash under PyQt4 when new web page domains are loaded. Hence you should disable the browsing to different remote websites and even disable intersphinx""" #: The viewer classes used by the help explorer. :class:`HelpExplorer` #: instances replace this attribute with the corresponding HelpMixin #: instance viewers = OrderedDict([('HTML help', UrlHelp), ('Plain text', TextHelp)]) def __init__(self, *args, **kwargs): super(HelpExplorer, self).__init__(*args, **kwargs) self.vbox = vbox = QVBoxLayout() self.combo = QComboBox(parent=self) vbox.addWidget(self.combo) self.viewers = OrderedDict([ (key, cls(parent=self)) for key, cls in six.iteritems(self.viewers) ]) for key, ini in six.iteritems(self.viewers): self.combo.addItem(key) ini.hide() vbox.addWidget(ini) self.viewer = next(six.itervalues(self.viewers)) self.viewer.show() self.combo.currentIndexChanged[str].connect(self.set_viewer) self.setLayout(vbox) def set_viewer(self, name): """Sets the current documentation viewer Parameters ---------- name: str or object A string must be one of the :attr:`viewers` attribute. An object can be one of the values in the :attr:`viewers` attribute""" if isstring(name) and asstring(name) not in self.viewers: raise ValueError("Don't have a viewer named %s" % (name, )) elif not isstring(name): viewer = name else: name = asstring(name) viewer = self.viewers[name] self.viewer.hide() self.viewer = viewer self.viewer.show() if (isstring(name) and not self.combo.currentText() == name): self.combo.setCurrentIndex(list(self.viewers).index(name)) @docstrings.dedent def show_help(self, obj, oname='', files=None): """ Show the documentaion of the given object We first try to use the current viewer based upon it's :attr:`HelpMixin.can_document_object` attribute. If this does not work, we check the other viewers Parameters ---------- %(HelpMixin.show_help.parameters)s""" oname = asstring(oname) ret = None if self.viewer.can_document_object: try: ret = self.viewer.show_help(obj, oname=oname, files=files) except Exception: logger.debug("Could not document %s with %s viewer!", oname, self.combo.currentText(), exc_info=True) else: curr_i = self.combo.currentIndex() for i, (viewername, viewer) in enumerate(six.iteritems(self.viewers)): if i != curr_i and viewer.can_document_object: self.set_viewer(viewername) self.combo.blockSignals(True) self.combo.setCurrentIndex(i) self.combo.blockSignals(False) try: ret = viewer.show_help(obj, oname=oname, files=files) except Exception: logger.debug("Could not document %s with %s viewer!", oname, viewername, exc_info=True) if ret: self.parent().raise_() return ret @docstrings.dedent def show_rst(self, text, oname='', files=None): """ Show restructured text We first try to use the current viewer based upon it's :attr:`HelpMixin.can_show_rst` attribute. If this does not work, we check the other viewers Parameters ---------- %(HelpMixin.show_rst.parameters)s""" ret = None if self.viewer.can_show_rst: ret = self.viewer.show_rst(text, oname=oname, files=files) else: for viewer in six.itervalues(self.viewers): if viewer.can_show_rst: self.set_viewer(viewer) ret = viewer.show_rst(text, oname=oname, files=files) break if ret: self.parent().raise_() return ret @docstrings.dedent def show_intro(self, text=''): """ Show an intro text We first try to use the current viewer based upon it's :attr:`HelpMixin.can_show_rst` attribute. If this does not work, we check the other viewers Parameters ---------- %(HelpMixin.show_intro.parameters)s""" found = False for i, viewer in enumerate(six.itervalues(self.viewers)): viewer.show_intro(text) if not found and viewer.can_show_rst: if i: self.set_viewer(viewer) found = True def close(self, *args, **kwargs): self.viewers['HTML help'].close(*args, **kwargs) return super(HelpExplorer, self).close(*args, **kwargs)
class HelpExplorer(QWidget, DockMixin): """A widget for showing the documentation. It behaves somewhat similar to spyders object inspector plugin and can show restructured text either as html (if sphinx is installed) or as plain text. It furthermore has a browser to show html content Warnings -------- The :class:`HelpBrowser` class is known to crash under PyQt4 when new web page domains are loaded. Hence you should disable the browsing to different remote websites and even disable intersphinx""" #: The viewer classes used by the help explorer. :class:`HelpExplorer` #: instances replace this attribute with the corresponding HelpMixin #: instance viewers = OrderedDict([('HTML help', UrlHelp), ('Plain text', TextHelp)]) def __init__(self, *args, **kwargs): super(HelpExplorer, self).__init__(*args, **kwargs) self.vbox = vbox = QVBoxLayout() self.combo = QComboBox(parent=self) vbox.addWidget(self.combo) self.viewers = OrderedDict( [(key, cls(parent=self)) for key, cls in six.iteritems( self.viewers)]) for key, ini in six.iteritems(self.viewers): self.combo.addItem(key) ini.hide() vbox.addWidget(ini) self.viewer = next(six.itervalues(self.viewers)) self.viewer.show() self.combo.currentIndexChanged[str].connect(self.set_viewer) self.setLayout(vbox) def set_viewer(self, name): """Sets the current documentation viewer Parameters ---------- name: str or object A string must be one of the :attr:`viewers` attribute. An object can be one of the values in the :attr:`viewers` attribute""" if isstring(name) and asstring(name) not in self.viewers: raise ValueError("Don't have a viewer named %s" % (name, )) elif not isstring(name): viewer = name else: name = asstring(name) viewer = self.viewers[name] self.viewer.hide() self.viewer = viewer self.viewer.show() if (isstring(name) and not self.combo.currentText() == name): self.combo.setCurrentIndex(list(self.viewers).index(name)) @docstrings.dedent def show_help(self, obj, oname='', files=None): """ Show the documentaion of the given object We first try to use the current viewer based upon it's :attr:`HelpMixin.can_document_object` attribute. If this does not work, we check the other viewers Parameters ---------- %(HelpMixin.show_help.parameters)s""" oname = asstring(oname) ret = None if self.viewer.can_document_object: try: ret = self.viewer.show_help(obj, oname=oname, files=files) except Exception: logger.debug("Could not document %s with %s viewer!", oname, self.combo.currentText(), exc_info=True) else: curr_i = self.combo.currentIndex() for i, (viewername, viewer) in enumerate( six.iteritems(self.viewers)): if i != curr_i and viewer.can_document_object: self.set_viewer(viewername) self.combo.blockSignals(True) self.combo.setCurrentIndex(i) self.combo.blockSignals(False) try: ret = viewer.show_help(obj, oname=oname, files=files) except Exception: logger.debug("Could not document %s with %s viewer!", oname, viewername, exc_info=True) if ret: self.parent().raise_() return ret @docstrings.dedent def show_rst(self, text, oname='', files=None): """ Show restructured text We first try to use the current viewer based upon it's :attr:`HelpMixin.can_show_rst` attribute. If this does not work, we check the other viewers Parameters ---------- %(HelpMixin.show_rst.parameters)s""" ret = None if self.viewer.can_show_rst: ret = self.viewer.show_rst(text, oname=oname, files=files) else: for viewer in six.itervalues(self.viewers): if viewer.can_show_rst: self.set_viewer(viewer) ret = viewer.show_rst(text, oname=oname, files=files) break if ret: self.parent().raise_() return ret @docstrings.dedent def show_intro(self, text=''): """ Show an intro text We first try to use the current viewer based upon it's :attr:`HelpMixin.can_show_rst` attribute. If this does not work, we check the other viewers Parameters ---------- %(HelpMixin.show_intro.parameters)s""" found = False for i, viewer in enumerate(six.itervalues(self.viewers)): viewer.show_intro(text) if not found and viewer.can_show_rst: if i: self.set_viewer(viewer) found = True def close(self, *args, **kwargs): self.viewers['HTML help'].close(*args, **kwargs) return super(HelpExplorer, self).close(*args, **kwargs)