def current_cursor(self, cursor: AnimatedCursor): self._cur = cursor self._current_frame = -1 self._is_ani = False self.__animation_timer.stop() if cursor is not None and (len(cursor) > 0): self._cur.normalize([(self._size, self._size)]) self._imgs = [ toqpixmap(cur[(self._size, self._size)].image) for cur, delay in cursor ] self._delays = [delay for cur, delay in cursor] if len(cursor) > 1: self._is_ani = True else: self._imgs = [ QtGui.QPixmap( QtGui.QImage( bytes(4 * self._size**2), self._size, self._size, 4, QtGui.QImage.Format_ARGB32, )) ] self._delays = [0] self.move_step()
def paintEvent(self, event: QtGui.QPaintEvent): self._core_painter.begin(self) if self._hotspot_preview_loc is not None: self._core_painter.setPen(QtGui.QColor(0, 0, 0, 0)) colors = [(0, 0, 255, 100), (0, 255, 0, 200), (255, 0, 0, 255)] widths = [20, 8, 3] for color, width in zip(colors, widths): self._core_painter.setBrush(QtGui.QBrush(QtGui.QColor(*color))) self._core_painter.drawEllipse( QtCore.QPoint(*self._hotspot_preview_loc), width, width) self._core_painter.end()
def __init__(self, parent, cursor: AnimatedCursor): super().__init__(parent) self._core_painter = QtGui.QPainter() self._pixmaps = [ toqpixmap(sub_cur[self.CURSOR_SIZE].image) for sub_cur, delay in cursor ] self._hotspots = [ sub_cur[self.CURSOR_SIZE].hotspot for sub_cur, delay in cursor ] self._delays = [delay for sub_cur, delay in cursor] self._animation_timer = QtCore.QTimer() self._animation_timer.setSingleShot(True) self._animation_timer.timeout.connect(self.moveStep) self._current_frame = -1 self._main_layout = QtWidgets.QVBoxLayout() self._example_label = QtWidgets.QLabel( "Hover over me to see the cursor! Click to see the hotspot.") self._main_layout.addWidget(self._example_label) self.setLayout(self._main_layout) self._hotspot_preview_loc = None self._pressed = False self.moveStep()
def __init__(self, parent=None, cursor: AnimatedCursor = None, size=None, *args, **kwargs): super().__init__(parent, *args, **kwargs) self._cur = None self._current_frame = None self._is_ani = None self.__painter = QtGui.QPainter() self.__animation_timer = QtCore.QTimer() self.__animation_timer.setSingleShot(True) self.__animation_timer.timeout.connect(self.move_step) self._imgs = None self._delays = None self._pressed = False self._size = self.DEF_SIZE if (size is None) else int(size) self.current_cursor = cursor self.setMinimumSize(self._imgs[self._current_frame].size()) self.setSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) self.resize(self._imgs[self._current_frame].size())
def __init__(self, parent=None, cursor: AnimatedCursor = None, frame=0, *args, **kwargs): super().__init__(parent, *args, **kwargs) self._frame = 0 self._frame_img = None self._pressed = False if (cursor is None) or (len(cursor) == 0): tmp_img = Image.fromarray( np.zeros(self.VIEW_SIZE + (4, ), dtype=np.uint8)) tmp_cursor = Cursor([CursorIcon(tmp_img, 0, 0)]) self._cursor = AnimatedCursor([tmp_cursor], [100]) else: self._cursor = cursor self._cursor.normalize([self.VIEW_SIZE]) self.__painter = QtGui.QPainter() self.frame = frame self.setCursor(QtCore.Qt.CrossCursor) self.setMinimumSize(QtCore.QSize(*self.VIEW_SIZE)) self.setSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) self.resize(self.minimumSize())
def moveStep(self): self._current_frame = (self._current_frame + 1) % len(self._delays) if len(self._pixmaps) > 0: self.setCursor( QtGui.QCursor( self._pixmaps[self._current_frame], *self._hotspots[self._current_frame], )) if len(self._pixmaps) > 1: self._animation_timer.setInterval( self._delays[self._current_frame]) self._animation_timer.start()
def dropEvent(self, event: QtGui.QDropEvent): if event.mimeData().hasUrls(): cursor = None working_path = None for cur_path in event.mimeData().urls(): if cur_path.isLocalFile(): path = cur_path.path(options=QtCore.QUrl.FullyDecoded) if isinstance( pathlib.PurePath(), pathlib.PureWindowsPath) and path.startswith("/"): path = path[1:] with open(path, "rb") as f: try: cursor = load_cursor(f) working_path = path except ValueError as e: print(e) else: try: req = urlopen(cur_path.url()) data = BytesIO(req.read()) data.seek(0) cursor = load_cursor(data) working_path = None except (URLError, ValueError) as e: print(e) if cursor is not None: self.current_cursor = cursor self._current_file = working_path event.acceptProposedAction() return if event.mimeData().hasImage(): mem_img = BytesIO() buffer = QtCore.QBuffer() image = QtGui.QImage(event.mimeData().imageData()) image.save(buffer, "PNG") mem_img.write(buffer) mem_img.seek(0) self.current_cursor = load_cursor(mem_img) self._current_file = None event.acceptProposedAction() return
def paintEvent(self, event: QtGui.QPaintEvent): self.__painter.begin(self) self.__painter.setPen(QtGui.QColor("black")) self.__painter.setBrush(QtGui.QColor("red")) self.__painter.drawPixmap(0, 0, self._frame_img) hotspot = QtCore.QPoint(*self.hotspot) self.__painter.setPen(QtGui.QColor(0, 0, 0, 150)) self.__painter.setBrush(QtGui.QColor(255, 0, 0, 100)) self.__painter.drawEllipse(hotspot, 4, 4) self.__painter.setPen(QtGui.QColor(0, 0, 255, 255)) self.__painter.setBrush(QtGui.QColor(0, 0, 255, 255)) self.__painter.drawEllipse(hotspot, 1, 1) self.__painter.end()
def __init__(self, parent=None, *args, **kwargs): super().__init__(parent, *args, **kwargs) mem_icon = BytesIO(base64.b64decode(ICON)) self.setWindowTitle("Cursor Theme Builder") self.setWindowIcon(QtGui.QIcon(toqpixmap(Image.open(mem_icon)))) self._open_build_project = None self._metadata = {"author": None, "licence": None} self._main_layout = QtWidgets.QVBoxLayout(self) self._scroll_pane = QtWidgets.QScrollArea() self._scroll_pane.setVerticalScrollBarPolicy( QtCore.Qt.ScrollBarAsNeeded) self._scroll_pane.setHorizontalScrollBarPolicy( QtCore.Qt.ScrollBarAlwaysOff) self._scroll_pane.setWidgetResizable(True) self._inner_pane_area = QtWidgets.QWidget() self._flow_layout = FlowLayout() self._cursor_selectors = {} for cursor_name in sorted(CursorThemeBuilder.DEFAULT_CURSORS): self._cursor_selectors[cursor_name] = CursorSelectWidget( label_text=cursor_name) self._flow_layout.addWidget(self._cursor_selectors[cursor_name]) self._edit_metadata = QtWidgets.QPushButton("Edit Artist Info") self._main_layout.addWidget(self._edit_metadata) self._inner_pane_area.setLayout(self._flow_layout) self._scroll_pane.setWidget(self._inner_pane_area) self._button_layout = QtWidgets.QHBoxLayout() self._main_layout.addWidget(self._scroll_pane) self._build_button = QtWidgets.QPushButton("Build Project") self._create_proj_btn = QtWidgets.QPushButton("Save Project") self._load_proj_btn = QtWidgets.QPushButton("Load Project") self._update_proj_btn = QtWidgets.QPushButton("Update Project") self._build_in_place = QtWidgets.QPushButton("Build in Place") self._clear_btn = QtWidgets.QPushButton("New Project") self._update_proj_btn.setEnabled(False) self._build_in_place.setEnabled(False) self._button_layout.addWidget(self._build_button) self._button_layout.addWidget(self._create_proj_btn) self._button_layout.addWidget(self._load_proj_btn) self._button_layout.addWidget(self._update_proj_btn) self._button_layout.addWidget(self._build_in_place) self._button_layout.addWidget(self._clear_btn) self._main_layout.addLayout(self._button_layout) self.setLayout(self._main_layout) # self.setMinimumSize(self.sizeHint()) self._build_button.clicked.connect(self.build_project) self._create_proj_btn.clicked.connect(self.create_project) self._load_proj_btn.clicked.connect(self.load_project) self._update_proj_btn.clicked.connect(self.update_project) self._build_in_place.clicked.connect(self.build_in_place) self._clear_btn.clicked.connect(self.clear_ui) self._edit_metadata.clicked.connect(self._chg_metadata)