def shadowTemplatePixmap(color, length): """ Returns 1 pixel wide, `length` pixels long linear-gradient. Args: color (QColor): shadow color length (int): length of cast shadow """ key = "InnerShadowTemplate " + \ color.name() + " " + \ str(length) # get cached template shadowPixmap = QPixmapCache.find(key) if shadowPixmap: return shadowPixmap shadowPixmap = QPixmap(1, length) shadowPixmap.fill(Qt.transparent) grad = QLinearGradient(0, 0, 0, length) grad.setColorAt(0, color) grad.setColorAt(1, Qt.transparent) painter = QPainter() painter.begin(shadowPixmap) painter.fillRect(shadowPixmap.rect(), grad) painter.end() # cache template QPixmapCache.insert(key, shadowPixmap) return shadowPixmap
def pixmap(self, size, mode, state): # type: (QSize, QIcon.Mode, QIcon.State) -> QPixmap if not self.__generator.isValid(): return QPixmap() dsize = self.__generator.defaultSize() # type: QSize if not dsize.isNull(): dsize.scale(size, Qt.KeepAspectRatio) size = dsize key = "{}.SVGIconEngine/{}/{}x{}".format(__name__, self.__cache_id, size.width(), size.height()) pm = QPixmapCache.find(key) if pm is None or pm.isNull(): pm = QPixmap(size) pm.fill(Qt.transparent) painter = QPainter(pm) try: self.__generator.render( painter, QRectF(0, 0, size.width(), size.height())) finally: painter.end() QPixmapCache.insert(key, pm) style = QApplication.style() if style is not None: opt = QStyleOption() opt.palette = QApplication.palette() pm = style.generatedIconPixmap(mode, pm, opt) return pm
def pixmap(self, size, mode, state): # type: (QSize, QIcon.Mode, QIcon.State) -> QPixmap if not self.__generator.isValid(): return QPixmap() dsize = self.__generator.defaultSize() # type: QSize if not dsize.isNull(): dsize.scale(size, Qt.KeepAspectRatio) size = dsize key = "{}.SVGIconEngine/{}/{}x{}".format( __name__, self.__cache_id, size.width(), size.height() ) pm = QPixmapCache.find(key) if pm is None or pm.isNull(): pm = QPixmap(size) pm.fill(Qt.transparent) painter = QPainter(pm) try: self.__generator.render( painter, QRectF(0, 0, size.width(), size.height())) finally: painter.end() QPixmapCache.insert(key, pm) style = QApplication.style() if style is not None: opt = QStyleOption() opt.palette = QApplication.palette() pm = style.generatedIconPixmap(mode, pm, opt) return pm
def innerShadowPixmap(color, size, pos, length=5): """ Args: color (QColor): shadow color size (QSize): size of pixmap pos (int): shadow position int flag, use bitwise operations 1 - top 2 - right 4 - bottom 8 - left length (int): length of cast shadow """ key = "InnerShadow " + \ color.name() + " " + \ str(size) + " " + \ str(pos) + " " + \ str(length) # get cached shadow if it exists finalShadow = QPixmapCache.find(key) if finalShadow: return finalShadow shadowTemplate = shadowTemplatePixmap(color, length) finalShadow = QPixmap(size) finalShadow.fill(Qt.transparent) shadowPainter = QPainter(finalShadow) shadowPainter.setCompositionMode(QPainter.CompositionMode_Darken) # top/bottom rect targetRect = QRect(0, 0, size.width(), length) # shadow on top if pos & 1: shadowPainter.drawPixmap(targetRect, shadowTemplate, shadowTemplate.rect()) # shadow on bottom if pos & 4: shadowPainter.save() shadowPainter.translate(QPointF(0, size.height())) shadowPainter.scale(1, -1) shadowPainter.drawPixmap(targetRect, shadowTemplate, shadowTemplate.rect()) shadowPainter.restore() # left/right rect targetRect = QRect(0, 0, size.height(), shadowTemplate.rect().height()) # shadow on the right if pos & 2: shadowPainter.save() shadowPainter.translate(QPointF(size.width(), 0)) shadowPainter.rotate(90) shadowPainter.drawPixmap(targetRect, shadowTemplate, shadowTemplate.rect()) shadowPainter.restore() # shadow on left if pos & 8: shadowPainter.save() shadowPainter.translate(0, size.height()) shadowPainter.rotate(-90) shadowPainter.drawPixmap(targetRect, shadowTemplate, shadowTemplate.rect()) shadowPainter.restore() shadowPainter.end() # cache shadow QPixmapCache.insert(key, finalShadow) return finalShadow
def innerGlowBackgroundPixmap(color, size, radius=5): """ Draws radial gradient pixmap, then uses that to draw a rounded-corner gradient rectangle pixmap. Args: color (QColor): used as outer color (lightness 245 used for inner) size (QSize): size of output pixmap radius (int): radius of inner glow rounded corners """ key = "InnerGlowBackground " + \ color.name() + " " + \ str(radius) bg = QPixmapCache.find(key) if bg: return bg # set background colors for gradient color = color.toHsl() light_color = color.fromHsl(color.hslHue(), color.hslSaturation(), 245) dark_color = color # initialize radial gradient center = QPoint(radius, radius) pixRect = QRect(0, 0, radius * 2, radius * 2) gradientPixmap = QPixmap(radius * 2, radius * 2) gradientPixmap.fill(dark_color) # draw radial gradient pixmap pixPainter = QPainter(gradientPixmap) pixPainter.setPen(Qt.NoPen) gradient = QRadialGradient(center, radius - 1) gradient.setColorAt(0, light_color) gradient.setColorAt(1, dark_color) pixPainter.setBrush(gradient) pixPainter.drawRect(pixRect) pixPainter.end() # set tl and br to the gradient's square-shaped rect tl = QPoint(0, 0) br = QPoint(size.width(), size.height()) # fragments of radial gradient pixmap to create rounded gradient outline rectangle frags = [ # top-left corner QPainter.PixmapFragment.create( QPointF(tl.x() + radius / 2, tl.y() + radius / 2), QRectF(0, 0, radius, radius) ), # top-mid 'linear gradient' QPainter.PixmapFragment.create( QPointF(tl.x() + (br.x() - tl.x()) / 2, tl.y() + radius / 2), QRectF(radius, 0, 1, radius), scaleX=(br.x() - tl.x() - 2 * radius) ), # top-right corner QPainter.PixmapFragment.create( QPointF(br.x() - radius / 2, tl.y() + radius / 2), QRectF(radius, 0, radius, radius) ), # left-mid 'linear gradient' QPainter.PixmapFragment.create( QPointF(tl.x() + radius / 2, tl.y() + (br.y() - tl.y()) / 2), QRectF(0, radius, radius, 1), scaleY=(br.y() - tl.y() - 2 * radius) ), # mid solid QPainter.PixmapFragment.create( QPointF(tl.x() + (br.x() - tl.x()) / 2, tl.y() + (br.y() - tl.y()) / 2), QRectF(radius, radius, 1, 1), scaleX=(br.x() - tl.x() - 2 * radius), scaleY=(br.y() - tl.y() - 2 * radius) ), # right-mid 'linear gradient' QPainter.PixmapFragment.create( QPointF(br.x() - radius / 2, tl.y() + (br.y() - tl.y()) / 2), QRectF(radius, radius, radius, 1), scaleY=(br.y() - tl.y() - 2 * radius) ), # bottom-left corner QPainter.PixmapFragment.create( QPointF(tl.x() + radius / 2, br.y() - radius / 2), QRectF(0, radius, radius, radius) ), # bottom-mid 'linear gradient' QPainter.PixmapFragment.create( QPointF(tl.x() + (br.x() - tl.x()) / 2, br.y() - radius / 2), QRectF(radius, radius, 1, radius), scaleX=(br.x() - tl.x() - 2 * radius) ), # bottom-right corner QPainter.PixmapFragment.create( QPointF(br.x() - radius / 2, br.y() - radius / 2), QRectF(radius, radius, radius, radius) ), ] # draw icon background to pixmap outPix = QPixmap(size.width(), size.height()) outPainter = QPainter(outPix) outPainter.setPen(Qt.NoPen) outPainter.drawPixmapFragments(frags, gradientPixmap, QPainter.PixmapFragmentHints(QPainter.OpaqueHint)) outPainter.end() QPixmapCache.insert(key, outPix) return outPix
def __init__(self, parent=None, select=SELECTNONE): QWidget.__init__(self) OWComponent.__init__(self, parent) self.parent = parent self.selection_type = select self.saving_enabled = hasattr(self.parent, "save_graph") self.clear_data(init=True) self.plotview = pg.PlotWidget(background="w", viewBox=InteractiveViewBox(self)) self.plot = self.plotview.getPlotItem() self.plot.setDownsampling(auto=True, mode="peak") self.markings = [] self.vLine = pg.InfiniteLine(angle=90, movable=False) self.hLine = pg.InfiniteLine(angle=0, movable=False) self.proxy = pg.SignalProxy(self.plot.scene().sigMouseMoved, rateLimit=20, slot=self.mouseMoved, delay=0.1) self.plot.scene().sigMouseMoved.connect(self.plot.vb.mouseMovedEvent) self.plot.vb.sigRangeChanged.connect(self.resized) self.pen_mouse = pg.mkPen(color=(0, 0, 255), width=2) self.pen_normal = defaultdict(lambda: pg.mkPen(color=(200, 200, 200, 127), width=1)) self.pen_subset = defaultdict(lambda: pg.mkPen(color=(0, 0, 0, 127), width=1)) self.pen_selected = defaultdict(lambda: pg.mkPen(color=(0, 0, 0, 127), width=2, style=Qt.DotLine)) self.label = pg.TextItem("", anchor=(1, 0)) self.label.setText("", color=(0, 0, 0)) self.discrete_palette = None QPixmapCache.setCacheLimit(max(QPixmapCache.cacheLimit(), 100 * 1024)) self.curves_cont = PlotCurvesItem() self.important_decimals = 4, 4 self.MOUSE_RADIUS = 20 self.clear_graph() #interface settings self.location = True #show current position self.markclosest = True #mark self.crosshair = True self.crosshair_hidden = True self.viewtype = INDIVIDUAL layout = QVBoxLayout() self.setLayout(layout) self.layout().setContentsMargins(0, 0, 0, 0) self.layout().addWidget(self.plotview) actions = [] zoom_in = QAction( "Zoom in", self, triggered=self.plot.vb.set_mode_zooming ) zoom_in.setShortcuts([Qt.Key_Z, QKeySequence(QKeySequence.ZoomIn)]) actions.append(zoom_in) zoom_fit = QAction( "Zoom to fit", self, triggered=lambda x: (self.plot.vb.autoRange(), self.plot.vb.set_mode_panning()) ) zoom_fit.setShortcuts([Qt.Key_Backspace, QKeySequence(Qt.ControlModifier | Qt.Key_0)]) actions.append(zoom_fit) rescale_y = QAction( "Rescale Y to fit", self, shortcut=Qt.Key_D, triggered=self.rescale_current_view_y ) actions.append(rescale_y) view_individual = QAction( "Show individual", self, shortcut=Qt.Key_I, triggered=lambda x: self.show_individual() ) actions.append(view_individual) view_average = QAction( "Show averages", self, shortcut=Qt.Key_A, triggered=lambda x: self.show_average() ) actions.append(view_average) self.show_grid = False self.show_grid_a = QAction( "Show grid", self, shortcut=Qt.Key_G, checkable=True, triggered=self.grid_changed ) actions.append(self.show_grid_a) self.invertX_menu = QAction( "Invert X", self, shortcut=Qt.Key_X, checkable=True, triggered=self.invertX_changed ) actions.append(self.invertX_menu) if self.selection_type == SELECTMANY: select_curves = QAction( "Select (line)", self, triggered=self.plot.vb.set_mode_select, ) select_curves.setShortcuts([Qt.Key_S]) actions.append(select_curves) if self.saving_enabled: save_graph = QAction( "Save graph", self, triggered=self.save_graph, ) save_graph.setShortcuts([QKeySequence(Qt.ControlModifier | Qt.Key_S)]) actions.append(save_graph) range_menu = MenuFocus("Define view range", self) range_action = QWidgetAction(self) layout = QGridLayout() range_box = gui.widgetBox(self, margin=5, orientation=layout) range_box.setFocusPolicy(Qt.TabFocus) self.range_e_x1 = lineEditFloatOrNone(None, self, "range_x1", label="e") range_box.setFocusProxy(self.range_e_x1) self.range_e_x2 = lineEditFloatOrNone(None, self, "range_x2", label="e") layout.addWidget(QLabel("X"), 0, 0, Qt.AlignRight) layout.addWidget(self.range_e_x1, 0, 1) layout.addWidget(QLabel("-"), 0, 2) layout.addWidget(self.range_e_x2, 0, 3) self.range_e_y1 = lineEditFloatOrNone(None, self, "range_y1", label="e") self.range_e_y2 = lineEditFloatOrNone(None, self, "range_y2", label="e") layout.addWidget(QLabel("Y"), 1, 0, Qt.AlignRight) layout.addWidget(self.range_e_y1, 1, 1) layout.addWidget(QLabel("-"), 1, 2) layout.addWidget(self.range_e_y2, 1, 3) b = gui.button(None, self, "Apply", callback=self.set_limits) layout.addWidget(b, 2, 3, Qt.AlignRight) range_action.setDefaultWidget(range_box) range_menu.addAction(range_action) layout = QGridLayout() self.plotview.setLayout(layout) self.button = QPushButton("View", self.plotview) self.button.setAutoDefault(False) layout.setRowStretch(1, 1) layout.setColumnStretch(1, 1) layout.addWidget(self.button, 0, 0) view_menu = MenuFocus(self) self.button.setMenu(view_menu) view_menu.addActions(actions) view_menu.addMenu(range_menu) self.addActions(actions) choose_color_action = QWidgetAction(self) choose_color_box = gui.hBox(self) choose_color_box.setFocusPolicy(Qt.TabFocus) model = VariableListModel() self.attrs = [] model.wrap(self.attrs) label = gui.label(choose_color_box, self, "Color by") label.setAlignment(Qt.AlignRight | Qt.AlignVCenter) self.attrCombo = gui.comboBox( choose_color_box, self, value="color_attr", contentsLength=12, callback=self.change_color_attr) self.attrCombo.setModel(model) choose_color_box.setFocusProxy(self.attrCombo) choose_color_action.setDefaultWidget(choose_color_box) view_menu.addAction(choose_color_action) labels_action = QWidgetAction(self) layout = QGridLayout() labels_box = gui.widgetBox(self, margin=0, orientation=layout) t = gui.lineEdit(None, self, "label_title", label="Title:", callback=self.labels_changed, callbackOnType=self.labels_changed) layout.addWidget(QLabel("Title:"), 0, 0, Qt.AlignRight) layout.addWidget(t, 0, 1) t = gui.lineEdit(None, self, "label_xaxis", label="X-axis:", callback=self.labels_changed, callbackOnType=self.labels_changed) layout.addWidget(QLabel("X-axis:"), 1, 0, Qt.AlignRight) layout.addWidget(t, 1, 1) t = gui.lineEdit(None, self, "label_yaxis", label="Y-axis:", callback=self.labels_changed, callbackOnType=self.labels_changed) layout.addWidget(QLabel("Y-axis:"), 2, 0, Qt.AlignRight) layout.addWidget(t, 2, 1) labels_action.setDefaultWidget(labels_box) view_menu.addAction(labels_action) self.labels_changed() # apply saved labels self.invertX_apply() self.plot.vb.set_mode_panning() self.reports = {} # current reports self.viewhelpers_show()
def __init__(self, parent: OWWidget, select=SELECTNONE): QWidget.__init__(self) OWComponent.__init__(self, parent) self.parent = parent self.selection_type = select self.select_at_least_1 = False self.saving_enabled = hasattr(self.parent, "save_graph") self.clear_data() self.subset = None # current subset input, an array of indices self.subset_indices = None # boolean index array with indices in self.data self.plotview = pg.PlotWidget(background="w", viewBox=InteractiveViewBoxC(self)) self.plot = self.plotview.getPlotItem() self.plot.setDownsampling(auto=True, mode="peak") self.markings = [] self.vLine = pg.InfiniteLine(angle=90, movable=False) self.hLine = pg.InfiniteLine(angle=0, movable=False) self.proxy = pg.SignalProxy(self.plot.scene().sigMouseMoved, rateLimit=20, slot=self.mouseMoved, delay=0.1) self.plot.scene().sigMouseMoved.connect(self.plot.vb.mouseMovedEvent) self.plot.vb.sigRangeChanged.connect(self.resized) self.plot.vb.sigResized.connect(self.resized) self.pen_mouse = pg.mkPen(color=(0, 0, 255), width=2) self.pen_normal = defaultdict(lambda: pg.mkPen(color=(200, 200, 200, 127), width=1)) self.pen_subset = defaultdict(lambda: pg.mkPen(color=(0, 0, 0, 127), width=1)) self.pen_selected = defaultdict(lambda: pg.mkPen(color=(0, 0, 0, 127), width=2, style=Qt.DotLine)) self.label = pg.TextItem("", anchor=(1, 0)) self.label.setText("", color=(0, 0, 0)) self.discrete_palette = None QPixmapCache.setCacheLimit(max(QPixmapCache.cacheLimit(), 100 * 1024)) self.curves_cont = PlotCurvesItem() self.important_decimals = 4, 4 self.plot.scene().installEventFilter( HelpEventDelegate(self.help_event, self)) # whether to rescale at next update self.rescale_next = True self.MOUSE_RADIUS = 20 self.clear_graph() # interface settings self.location = True # show current position self.markclosest = True # mark self.crosshair = True self.crosshair_hidden = True layout = QVBoxLayout() self.setLayout(layout) self.layout().setContentsMargins(0, 0, 0, 0) self.layout().addWidget(self.plotview) # prepare interface according to the new context self.parent.contextAboutToBeOpened.connect(lambda x: self.init_interface_data(x[0])) actions = [] resample_curves = QAction( "Resample curves", self, shortcut=Qt.Key_R, triggered=lambda x: self.resample_curves(self.sample_seed+1) ) resample_curves.setShortcutContext(Qt.WidgetWithChildrenShortcut) actions.append(resample_curves) reset_curves = QAction( "Resampling reset", self, shortcut=QKeySequence(Qt.ControlModifier | Qt.Key_R), triggered=lambda x: self.resample_curves(0) ) reset_curves.setShortcutContext(Qt.WidgetWithChildrenShortcut) actions.append(reset_curves) zoom_in = QAction( "Zoom in", self, triggered=self.plot.vb.set_mode_zooming ) zoom_in.setShortcuts([Qt.Key_Z, QKeySequence(QKeySequence.ZoomIn)]) zoom_in.setShortcutContext(Qt.WidgetWithChildrenShortcut) actions.append(zoom_in) zoom_fit = QAction( "Zoom to fit", self, triggered=lambda x: (self.plot.vb.autoRange(), self.plot.vb.set_mode_panning()) ) zoom_fit.setShortcuts([Qt.Key_Backspace, QKeySequence(Qt.ControlModifier | Qt.Key_0)]) zoom_fit.setShortcutContext(Qt.WidgetWithChildrenShortcut) actions.append(zoom_fit) rescale_y = QAction( "Rescale Y to fit", self, shortcut=Qt.Key_D, triggered=self.rescale_current_view_y ) rescale_y.setShortcutContext(Qt.WidgetWithChildrenShortcut) actions.append(rescale_y) self.view_average_menu = QAction( "Show averages", self, shortcut=Qt.Key_A, checkable=True, triggered=lambda x: self.viewtype_changed() ) self.view_average_menu.setShortcutContext(Qt.WidgetWithChildrenShortcut) actions.append(self.view_average_menu) self.show_grid = False self.show_grid_a = QAction( "Show grid", self, shortcut=Qt.Key_G, checkable=True, triggered=self.grid_changed ) self.show_grid_a.setShortcutContext(Qt.WidgetWithChildrenShortcut) actions.append(self.show_grid_a) self.invertX_menu = QAction( "Invert X", self, shortcut=Qt.Key_X, checkable=True, triggered=self.invertX_changed ) self.invertX_menu.setShortcutContext(Qt.WidgetWithChildrenShortcut) actions.append(self.invertX_menu) if self.selection_type == SELECTMANY: select_curves = QAction( "Select (line)", self, triggered=self.line_select_start, ) select_curves.setShortcuts([Qt.Key_S]) select_curves.setShortcutContext(Qt.WidgetWithChildrenShortcut) actions.append(select_curves) if self.saving_enabled: save_graph = QAction( "Save graph", self, triggered=self.save_graph, ) save_graph.setShortcuts([QKeySequence(Qt.ControlModifier | Qt.Key_S)]) save_graph.setShortcutContext(Qt.WidgetWithChildrenShortcut) actions.append(save_graph) range_menu = MenuFocus("Define view range", self) range_action = QWidgetAction(self) layout = QGridLayout() range_box = gui.widgetBox(self, margin=5, orientation=layout) range_box.setFocusPolicy(Qt.TabFocus) self.range_e_x1 = lineEditFloatOrNone(None, self, "range_x1", label="e") range_box.setFocusProxy(self.range_e_x1) self.range_e_x2 = lineEditFloatOrNone(None, self, "range_x2", label="e") layout.addWidget(QLabel("X"), 0, 0, Qt.AlignRight) layout.addWidget(self.range_e_x1, 0, 1) layout.addWidget(QLabel("-"), 0, 2) layout.addWidget(self.range_e_x2, 0, 3) self.range_e_y1 = lineEditFloatOrNone(None, self, "range_y1", label="e") self.range_e_y2 = lineEditFloatOrNone(None, self, "range_y2", label="e") layout.addWidget(QLabel("Y"), 1, 0, Qt.AlignRight) layout.addWidget(self.range_e_y1, 1, 1) layout.addWidget(QLabel("-"), 1, 2) layout.addWidget(self.range_e_y2, 1, 3) b = gui.button(None, self, "Apply", callback=self.set_limits) layout.addWidget(b, 2, 3, Qt.AlignRight) range_action.setDefaultWidget(range_box) range_menu.addAction(range_action) layout = QGridLayout() self.plotview.setLayout(layout) self.button = QPushButton("View", self.plotview) self.button.setAutoDefault(False) layout.setRowStretch(1, 1) layout.setColumnStretch(1, 1) layout.addWidget(self.button, 0, 0) view_menu = MenuFocus(self) self.button.setMenu(view_menu) view_menu.addActions(actions) view_menu.addMenu(range_menu) self.addActions(actions) choose_color_action = QWidgetAction(self) choose_color_box = gui.hBox(self) choose_color_box.setFocusPolicy(Qt.TabFocus) label = gui.label(choose_color_box, self, "Color by") label.setAlignment(Qt.AlignRight | Qt.AlignVCenter) self.feature_color_model = DomainModel(DomainModel.METAS | DomainModel.CLASSES, valid_types=(DiscreteVariable,), placeholder="None") self.feature_color_combo = gui.comboBox( choose_color_box, self, "feature_color", callback=self.update_view, model=self.feature_color_model, valueType=str) choose_color_box.setFocusProxy(self.feature_color_combo) choose_color_action.setDefaultWidget(choose_color_box) view_menu.addAction(choose_color_action) cycle_colors = QShortcut(Qt.Key_C, self, self.cycle_color_attr, context=Qt.WidgetWithChildrenShortcut) labels_action = QWidgetAction(self) layout = QGridLayout() labels_box = gui.widgetBox(self, margin=0, orientation=layout) t = gui.lineEdit(None, self, "label_title", label="Title:", callback=self.labels_changed, callbackOnType=self.labels_changed) layout.addWidget(QLabel("Title:"), 0, 0, Qt.AlignRight) layout.addWidget(t, 0, 1) t = gui.lineEdit(None, self, "label_xaxis", label="X-axis:", callback=self.labels_changed, callbackOnType=self.labels_changed) layout.addWidget(QLabel("X-axis:"), 1, 0, Qt.AlignRight) layout.addWidget(t, 1, 1) t = gui.lineEdit(None, self, "label_yaxis", label="Y-axis:", callback=self.labels_changed, callbackOnType=self.labels_changed) layout.addWidget(QLabel("Y-axis:"), 2, 0, Qt.AlignRight) layout.addWidget(t, 2, 1) labels_action.setDefaultWidget(labels_box) view_menu.addAction(labels_action) self.labels_changed() # apply saved labels self.invertX_apply() self.plot.vb.set_mode_panning() self.reports = {} # current reports self.viewhelpers_show()