示例#1
0
    def do_layout(self, rect, test_only):
        x = rect.x()
        y = rect.y()
        line_height = 0

        for item in self.itemList:
            wid = item.widget()
            space_x = self.spacing() + wid.style().layoutSpacing(
                QtWidgets.QSizePolicy.PushButton,
                QtWidgets.QSizePolicy.PushButton,
                QtCore.Qt.Horizontal,
            )
            space_y = self.spacing() + wid.style().layoutSpacing(
                QtWidgets.QSizePolicy.PushButton,
                QtWidgets.QSizePolicy.PushButton,
                QtCore.Qt.Vertical,
            )
            next_x = x + item.sizeHint().width() + space_x
            if next_x - space_x > rect.right() and line_height > 0:
                x = rect.x()
                y = y + line_height + space_y
                next_x = x + item.sizeHint().width() + space_x
                line_height = 0

            if not test_only:
                item.setGeometry(
                    QtCore.QRect(QtCore.QPoint(x, y), item.sizeHint()))

            x = next_x
            line_height = max(line_height, item.sizeHint().height())

        return y + line_height - rect.y()
示例#2
0
    def minimumSize(self):
        size = QtCore.QSize()

        for item in self.itemList:
            size = size.expandedTo(item.minimumSize())

        size += QtCore.QSize(2 * self.contentsMargins().top(),
                             2 * self.contentsMargins().top())
        return size
    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()
示例#4
0
    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())
示例#5
0
    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 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 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 _run_script(self, script: str, **kwargs) -> str:
        loop = QtCore.QEventLoop()
        message = (False, "Could not load browser...")

        def on_dump(no_error, msg):
            nonlocal message
            message = (no_error, msg)
            loop.quit()

        self._page.on_dump = on_dump
        self._page.runJavaScript(script.format(**kwargs), 0)
        loop.exec_()

        if not message[0]:
            raise ValueError(f"Error while running script: {message[1]}")

        return str(message[1])
示例#9
0
    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()
示例#10
0
 def sizeHint(self) -> QtCore.QSize:
     return QtCore.QSize(self.DEF_SIZE, self.DEF_SIZE)
示例#11
0
 def sizeHint(self) -> QtCore.QSize:
     return QtCore.QSize(*self.VIEW_SIZE)
示例#12
0
 def heightForWidth(self, width):
     height = self.do_layout(QtCore.QRect(0, 0, width, 0), True)
     return height