Esempio n. 1
0
def basic_cache_logic(f,
                      pklfile: File,
                      *args,
                      log_level=LogLevel.PROGRESS,
                      **kwargs):
    if pklfile.exists:
        log_level.handle(f"loading {f.__name__} result from cache")
        return pklfile.load(silent=True), True
    else:
        key = log_level.handle(
            f"no cached result for {f.__name__}, running ...",
            attempt_one_line=True)
        r = f(*args, **kwargs)
        pklfile.save(r, silent=True)
        log_level.handle(f"completed and cached", one_line_key=key)
        return r, False
Esempio n. 2
0
class HelloWindow(QMainWindow):
    # https://stackoverflow.com/questions/43126721/detect-resizing-in-widget-window-resized-signal
    resized = QtCore.pyqtSignal()

    def resizeEvent(self, event):
        self.resized.emit()
        return super(HelloWindow, self).resizeEvent(event)

    moved = QtCore.pyqtSignal(str)

    def fakeMoveEventGenerator(self):
        while True:
            sleep(5)
            self.moved.emit('dummy')

    def __init__(self,
                 title,
                 label,
                 fullscreen=True,
                 always_on_top=False,
                 always_on_bottom=False,
                 no_title_bar=False,
                 pathWrap=False,
                 readOnly=True,
                 bind_file=None):
        # QMainWindow.__init__(self)
        # super().__init__(flags, *args, **kwargs)
        flags = []
        args = ()
        kwargs = {}

        flags = 0
        flags = None
        if no_title_bar:
            if flags is None:
                flags = QtCore.Qt.CustomizeWindowHint
            else:
                flags = flags | QtCore.Qt.CustomizeWindowHint
            # pass
        if always_on_top:
            if flags is None:
                flags = QtCore.Qt.WindowStaysOnTopHint
            else:
                flags = flags | QtCore.Qt.WindowStaysOnTopHint
            # flags = flags + QtCore.Qt.WindowStaysOnTopHint
            # pass
            # super().__init__(None, QtCore.Qt.WindowStaysOnTopHint)
            # super(w).__init__(None, QtCore.Qt.X11BypassWindowManagerHint)
        # else:

        if always_on_bottom:
            if flags is None:
                flags = QtCore.Qt.WindowStaysOnBottomHint
            else:
                flags = flags | QtCore.Qt.WindowStaysOnBottomHint

        if flags is not None:
            super().__init__(None, flags)
        else:
            super().__init__(None)
        # self.setWindowFlags(QtCore.Qt.WindowFlags(QtCore.Qt.CustomizeWindowHint,QtCore.Qt.WindowStaysOnTopHint))
        # super().__init__(None,  QtCore.Qt.WindowStaysOnTopHint)

        # self.setMinimumSize(QSize(640, 480))
        # set stylesheet
        # CAUSED BUTTONS TO NOT WORK
        # file = QFile("/Users/matt/Desktop/BreezeStyleSheets/dark.qss")

        # file = QFile(":/dark.qss")
        # file.open(QFile.ReadOnly | QFile.Text)
        # stream = QTextStream(file)
        # CAUSED BUTTONS TO NOT WORK
        # self.setStyleSheet(stream.readAll())

        self.setWindowTitle(title)

        self._init_cw()

        if label is not None:
            if not readOnly:
                pathWrap = True
            if pathWrap:
                self.label = self.textPathWrap(label, readOnly)
            else:
                self.label = self.text(label)
        else:
            self.label = None

        if bind_file is None:
            w, h = screen_sizes()

            self.setFixedSize(w, h)

            if fullscreen:
                self.showFullScreen()
            else:
                self.setFixedSize(w, h - 45)
        else:
            self.stage_file = File(bind_file, quiet=True)
            if self.stage_file.exists:
                self.load_geometry()
            else:
                w, h = screen_sizes()

                # self.setFixedSize(w, h)

                if fullscreen:
                    self.showFullScreen()
                else:
                    self.setFixedSize(w, h - 45)
            self.bindGeometry()

    def saveWinGeometry(self):
        self.stage_file.save({
            "x": self.frameGeometry().x(),
            "y": self.frameGeometry().y(),
            "width": self.frameGeometry().width(),
            "height": self.frameGeometry().height()
        })

    def bindGeometry(self):
        self.resized.connect(self.saveWinGeometry)
        self.moved.connect(self.saveWinGeometry)
        Thread(target=self.fakeMoveEventGenerator, daemon=True).start()

    def load_geometry(self):
        geo = self.stage_file.load()
        self.move(geo['x'], geo['y'])
        self.setFixedSize(geo['width'], geo['height'])

    def _init_cw(self):
        centralWidget = MQWidget(self)
        self.setCentralWidget(centralWidget)

        self.cw = centralWidget

        self.gridLayout = centralWidget.gridLayout

    def clear(self):
        try_delete(self.cw, self)
        self._init_cw()

    def text(self, txt="", data=None, click_handler=None):
        return self.cw.text(txt, data, click_handler)

    def textPathWrap(self, txt="", readOnly=True):
        return self.cw.textPathWrap(txt, readOnly)

    def button(self, text, fun):
        return self.cw.button(text, fun)

    def input(self, initial="") -> QLineEdit:
        return self.cw.input(initial)

    def pane(self, returnBox=False, vertical=True):
        return self.cw.pane(returnBox, vertical=vertical)

    def screen(self):
        return QtWidgets.QApplication.screenAt(QPoint(self.x(), self.y()))

    def current_screen_sizes(self):
        screen_height = self.screen().availableGeometry().height()
        screen_width = self.screen().availableGeometry().width()
        half_width = screen_width / 2
        half_height = screen_height / 2
        return screen_height, screen_width, half_width, half_height

    def move_left(self):
        screen_height, screen_width, half_width, half_height = self.current_screen_sizes(
        )
        self.move(0, 0)
        self.setFixedSize(half_width, screen_height)

    def move_right(self):
        screen_height, screen_width, half_width, half_height = self.current_screen_sizes(
        )
        self.move(half_width, 0)
        self.setFixedSize(half_width, screen_height)

    def move_down(self):
        screen_height, screen_width, half_width, half_height = self.current_screen_sizes(
        )
        self.move(0, half_height)
        self.setFixedSize(screen_width, half_height)

    def move_up(self):
        screen_height, screen_width, half_width, half_height = self.current_screen_sizes(
        )
        self.move(0, 0)
        self.setFixedSize(screen_width, half_height)

    def maximize(self):
        screen_height, screen_width, half_width, half_height = self.current_screen_sizes(
        )
        self.move(0, 0)
        self.setFixedSize(screen_width, screen_height)