def __init__( self, text: str, settings: NotificationSettings = NotificationSettings(), parent: Optional[QWidget] = None, **kwargs, ): super().__init__(text, parent=parent, **kwargs) self._settings = settings self.setFrameStyle(QFrame.Shape.Panel) self.setLineWidth(2) self.setWindowFlags(Qt.WindowType.ToolTip) self.setContentsMargins(10, 10, 10, 10) palette = QPalette() palette.setColor(QPalette.ColorRole.Window, QColor(self._settings.bg_color)) palette.setColor(QPalette.ColorRole.WindowText, QColor(self._settings.fg_color)) self.setPalette(palette) if parent and self._settings.focus_behavior != FocusBehavior.always_on_top: app: "AnkiApp" = QApplication.instance( ) # type: ignore[assignment] app.focusChanged.connect(self._on_app_focus_changed)
def tooltip(msg, period=3000, parent=None): global _tooltipTimer, _tooltipLabel class CustomLabel(QLabel): silentlyClose = True def mousePressEvent(self, evt): evt.accept() self.hide() closeTooltip() aw = parent or aqt.mw.app.activeWindow() or aqt.mw lab = CustomLabel("""\ <table cellpadding=10> <tr> <td>%s</td> </tr> </table>""" % msg, aw) lab.setFrameStyle(QFrame.Panel) lab.setLineWidth(2) lab.setWindowFlags(Qt.ToolTip) p = QPalette() p.setColor(QPalette.Window, QColor("#feffc4")) p.setColor(QPalette.WindowText, QColor("#000000")) lab.setPalette(p) lab.move( aw.mapToGlobal(QPoint(0, -100 + aw.height()))) lab.show() _tooltipTimer = aqt.mw.progress.timer( period, closeTooltip, False, requiresCollection=False) _tooltipLabel = lab
def _getBrush(self): if not self.ankiword.noteId: return None gradient = QLinearGradient(0, 0, 1, 0) gradient.setCoordinateMode(QGradient.ObjectBoundingMode) gradient.setColorAt(0, QColor.fromRgbF(1, 1, 1, 0.0)) gradient.setColorAt(1, QColor.fromRgbF(0, 1, 0, 0.5)) return QBrush(gradient)
def _updateButtonColor(self, color): """Generate color preview pixmap and place it on button""" pixmap = QPixmap(128, 18) qcolour = QColor(0, 0, 0) qcolour.setNamedColor(color) pixmap.fill(qcolour) self.setIcon(QIcon(pixmap)) self.setIconSize(QSize(128, 18)) self.color = color
def paint(self, painter, option, widget=None): """ Paint the node in the graphic view. """ painter.setBrush(QBrush(QColor(255, 0, 0, 100))) painter.setPen(QPen(QColor(0, 0, 0), 1.0, Qt.SolidLine)) painter.drawRect(self.rect()) painter.setRenderHint(QPainter.Antialiasing) painter.setBrush(QBrush(QColor(255, 0, 0, 255))) painter.setPen(QPen(QColor(0, 0, 0, 255), 1.0, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)) for handle, rect in self.handles.items(): if self.handleSelected is None or handle == self.handleSelected: painter.drawEllipse(rect)
def icon_from_resources(self, path: Union[str, ColoredIcon]) -> QIcon: "Fetch icon from Qt resources, and invert if in night mode." if self.night_mode: cache = self._icon_cache_light else: cache = self._icon_cache_dark if isinstance(path, str): key = path else: key = f"{path.path}-{path.color}" icon = cache.get(key) if icon: return icon if isinstance(path, str): # default black/white icon = QIcon(path) if self.night_mode: img = icon.pixmap(self._icon_size, self._icon_size).toImage() img.invertPixels() icon = QIcon(QPixmap(img)) else: # specified colours icon = QIcon(path.path) pixmap = icon.pixmap(16) painter = QPainter(pixmap) painter.setCompositionMode(QPainter.CompositionMode_SourceIn) painter.fillRect(pixmap.rect(), QColor(path.current_color(self.night_mode))) painter.end() icon = QIcon(pixmap) return icon return cache.setdefault(path, icon)
def onChangeCol(self): new = QColorDialog.getColor(QColor(self.fcolour), None) # native dialog doesn't refocus us for some reason self.parentWindow.activateWindow() if new.isValid(): self.fcolour = new.name() self.onColourChanged() self._wrapWithColour(self.fcolour)
def placeFood(self, painter: QPainter): if self.foodPlaced is False: self.foodX = randrange(24) * 12 self.foodY = randrange(2, 24) * 12 if not [self.foodX, self.foodY] in self.snakeArray: self.foodPlaced = True painter.setBrush(QColor("#ffdd55")) painter.drawRect(self.foodX, self.foodY, 12, 12)
def _chooseColor(self): qcolour = QColor(self.color) dialog = QColorDialog(qcolour, parent=self) color = dialog.getColor() if not color.isValid(): return False color = color.name() self._updateButtonColor(color)
def main(): # Query system colours. p = QPalette() textcolour = p.color(QPalette.Text) basecolour = p.color(QPalette.Base) # Inject background colour into the browser. def tree_colour_hook(self): p = self.form.tree.palette() p.setColor(QPalette.Base, basecolour) self.form.tree.setPalette(p) browser.Browser.setupTree = wrap(browser.Browser.setupTree, tree_colour_hook) # Change suspend and mark colours. coloursuspended = QColor() coloursuspended.setNamedColor(browser.COLOUR_SUSPENDED) colourmarked = QColor() colourmarked.setNamedColor(browser.COLOUR_MARKED) lightness_blacklist = [textcolour.lightness()] hue_blacklist = [basecolour.hue(), textcolour.hue()] lightness_preference = max(basecolour.lightness(), 40) for colour in [coloursuspended, colourmarked]: (h, s, l, a) = colour.getHsl() new_lightness = get_new_lightness(lightness_blacklist, lightness_preference) # print("Considering {0} with preference {2} choose lightness {1}\n".format( # lightness_blacklist, new_lightness, lightness_preference)) new_hue = get_new_hue(hue_blacklist, h) # print("Considering {0} with preference {2} choose hue {1}\n".format( # hue_blacklist, new_hue, h)) hue_blacklist.append(new_hue) colour.setHsl(new_hue, s, new_lightness, a) browser.COLOUR_SUSPENDED = coloursuspended.name() browser.COLOUR_MARKED = colourmarked.name() # Inject colouring into the web view. editor._html = re.sub( "(\\.fname\s*\\{)", "\\1 color: {0};".format(textcolour.name()), editor._html) # Fix the default text colour for type answer edit elements. reviewer.Reviewer._css = re.sub( "(#typeans\s*\\{)", "\\1 color: {0};".format(textcolour.name()), reviewer.Reviewer._css)
def _apply_palette(self, app: QApplication) -> None: set_macos_dark_mode(self.night_mode) if not self.night_mode: app.setStyle(QStyleFactory.create(self._default_style)) # type: ignore app.setPalette(self.default_palette) return if not self.macos_dark_mode(): app.setStyle(QStyleFactory.create("fusion")) # type: ignore palette = QPalette() text_fg = self.qcolor(colors.TEXT_FG) palette.setColor(QPalette.ColorRole.WindowText, text_fg) palette.setColor(QPalette.ColorRole.ToolTipText, text_fg) palette.setColor(QPalette.ColorRole.Text, text_fg) palette.setColor(QPalette.ColorRole.ButtonText, text_fg) hlbg = self.qcolor(colors.HIGHLIGHT_BG) hlbg.setAlpha(64) palette.setColor( QPalette.ColorRole.HighlightedText, self.qcolor(colors.HIGHLIGHT_FG) ) palette.setColor(QPalette.ColorRole.Highlight, hlbg) window_bg = self.qcolor(colors.WINDOW_BG) palette.setColor(QPalette.ColorRole.Window, window_bg) palette.setColor(QPalette.ColorRole.AlternateBase, window_bg) palette.setColor(QPalette.ColorRole.Button, QColor("#454545")) frame_bg = self.qcolor(colors.FRAME_BG) palette.setColor(QPalette.ColorRole.Base, frame_bg) palette.setColor(QPalette.ColorRole.ToolTipBase, frame_bg) disabled_color = self.qcolor(colors.DISABLED) palette.setColor(QPalette.ColorRole.PlaceholderText, disabled_color) palette.setColor( QPalette.ColorGroup.Disabled, QPalette.ColorRole.Text, disabled_color ) palette.setColor( QPalette.ColorGroup.Disabled, QPalette.ColorRole.ButtonText, disabled_color ) palette.setColor( QPalette.ColorGroup.Disabled, QPalette.ColorRole.HighlightedText, disabled_color, ) palette.setColor(QPalette.ColorRole.Link, self.qcolor(colors.LINK)) palette.setColor(QPalette.ColorRole.BrightText, Qt.GlobalColor.red) app.setPalette(palette)
def _apply_palette(self, app: QApplication) -> None: if not self.night_mode: return if not self.macos_dark_mode(): app.setStyle(QStyleFactory.create("fusion")) # type: ignore palette = QPalette() text_fg = self.qcolor("text-fg") palette.setColor(QPalette.WindowText, text_fg) palette.setColor(QPalette.ToolTipText, text_fg) palette.setColor(QPalette.Text, text_fg) palette.setColor(QPalette.ButtonText, text_fg) hlbg = self.qcolor("highlight-bg") hlbg.setAlpha(64) palette.setColor(QPalette.HighlightedText, self.qcolor("highlight-fg")) palette.setColor(QPalette.Highlight, hlbg) window_bg = self.qcolor("window-bg") palette.setColor(QPalette.Window, window_bg) palette.setColor(QPalette.AlternateBase, window_bg) palette.setColor(QPalette.Button, QColor(colors.get("fusion-button-base-bg"))) frame_bg = self.qcolor("frame-bg") palette.setColor(QPalette.Base, frame_bg) palette.setColor(QPalette.ToolTipBase, frame_bg) disabled_color = self.qcolor("disabled") palette.setColor(QPalette.Disabled, QPalette.Text, disabled_color) palette.setColor(QPalette.Disabled, QPalette.ButtonText, disabled_color) palette.setColor(QPalette.Disabled, QPalette.HighlightedText, disabled_color) palette.setColor(QPalette.Link, self.qcolor("link")) palette.setColor(QPalette.BrightText, Qt.red) app.setPalette(palette)
def gameOver(self, event: QPaintEvent, painter: QPainter): info = "" if self.score > self.highscore: self.lives += 1 self.highscore = self.score info = "\n\nNew high score! 1 life replenished." font_size = 10 if not is_mac else 12 painter.setPen(QColor(0, 34, 3)) painter.setFont(QFont("Decorative", font_size)) if self.lives > 0: msg = "GAME OVER{}\n\nPress space to play again".format(info) else: self.setCursor(Qt.CursorShape.PointingHandCursor) msg = ("GAME OVER\n\nYou're out of lives for today,\n" "but tomorrow is another day :)\n\n" "Tip: Get more lives by\nkeeping up with your reviews!\n\n" "Pro-Tip: Pledge your support on Patreon\n" "and get access to other secret\n" "features and add-ons :)" "\n\nClick here to go to\n" "patreon.com/glutanimate") painter.drawText(event.rect(), Qt.AlignmentFlag.AlignCenter, msg)
def qcolor(self, key: str) -> QColor: """Get a color defined in _vars.scss as a QColor.""" return QColor(self.str_color(key))
def scoreText(self, event: QPaintEvent, painter: QPainter): painter.setPen(QColor("#ffffff")) painter.setFont(QFont("Decorative", 10)) painter.drawText(4, 17, "LIVES: " + str(self.lives)) painter.drawText(120, 17, "SCORE: " + str(self.score)) painter.drawText(230, 17, "BEST: " + str(self.highscore))
def scoreBoard(self, painter: QPainter): painter.setPen(Qt.PenStyle.NoPen) painter.setBrush(QColor("#3e7a78")) painter.drawRect(0, 0, 300, 24)
def drawSnake(self, painter: QPainter): painter.setPen(Qt.PenStyle.NoPen) painter.setBrush(QColor("#ffffff")) for i in self.snakeArray: painter.drawRect(i[0], i[1], 12, 12)
def qcolor(self, colors: tuple[str, str]) -> QColor: return QColor(self.color(colors))
def _getWindowColor(self): if isMac: # standard palette does not return correct window color on macOS return QColor("#ececec") return self.style().standardPalette().color(QPalette.Window)