Beispiel #1
0
    def play(self, resume=False):
        self.status = "Switching to Player..."

        import player

        player.app = app
        struct, cells_by_id, columns_by_id = save(self.scene, resume=resume)

        window = player.MainWindow(playtest=True)
        window.setWindowModality(qt.ApplicationModal)
        window.setWindowState(self.windowState())
        window.setGeometry(self.geometry())

        windowcloseevent = window.closeEvent

        def closeevent(e):
            windowcloseevent(e)
            for it in window.scene.all(player.Cell):
                cells_by_id[
                    it.id].revealed_resume = it.kind is not Cell.unknown

        window.closeEvent = closeevent

        window.load(struct)
        window.show()
        app.processEvents()
        window.view.setSceneRect(self.view.sceneRect())
        window.view.setTransform(self.view.transform())
        window.view.horizontalScrollBar().setValue(
            self.view.horizontalScrollBar().value())
        delta = window.view.mapTo(window.central_widget, QPoint(0, 0))
        window.view.verticalScrollBar().setValue(
            self.view.verticalScrollBar().value() + delta.y())

        self.status = "Done", 1
Beispiel #2
0
 def first_visible_row(self):
     geom = self.viewport().geometry()
     for y in range(geom.top(), (self.spacing() * 2) + geom.top(), 5):
         for x in range(geom.left(), (self.spacing() * 2) + geom.left(), 5):
             ans = self.indexAt(QPoint(x, y)).row()
             if ans > -1:
                 return ans
Beispiel #3
0
 def last_visible_row(self):
     geom = self.viewport().geometry()
     for y in range(geom.bottom(), geom.bottom() - 2 * self.spacing(), -5):
         for x in range(geom.left(), (self.spacing()*2) + geom.left(), 5):
             ans = self.indexAt(QPoint(x, y)).row()
             if ans > -1:
                 item_width = self.delegate.item_size.width() + 2*self.spacing()
                 return ans + (geom.width() // item_width)
Beispiel #4
0
 def number_of_columns(self):
     # Number of columns currently visible in the grid
     if self._ncols is None:
         dpr = self.device_pixel_ratio
         width = int(dpr * self.delegate.cover_size.width())
         height = int(dpr * self.delegate.cover_size.height())
         step = max(10, self.spacing())
         for y in range(step, 2 * height, step):
             for x in range(step, 2 * width, step):
                 i = self.indexAt(QPoint(x, y))
                 if i.isValid():
                     for x in range(self.viewport().width() - step, self.viewport().width() - width, -step):
                         j = self.indexAt(QPoint(x, y))
                         if j.isValid():
                             self._ncols = j.row() - i.row() + 1
                             return self._ncols
     return self._ncols
Beispiel #5
0
 def delayed():
     window.load(struct)
     window.view.setSceneRect(self.view.sceneRect())
     window.view.setTransform(self.view.transform())
     window.view.horizontalScrollBar().setValue(self.view.horizontalScrollBar().value())
     delta = window.view.mapTo(window.central_widget, QPoint(0, 0))
     window.view.verticalScrollBar().setValue(self.view.verticalScrollBar().value()+delta.y())
     self.status = "Done", 1
Beispiel #6
0
    def do_layout(self, rect, apply_geometry=False):
        left, top, right, bottom = self.getContentsMargins()
        erect = rect.adjusted(left, top, -right, -bottom)
        x, y = erect.x(), erect.y()

        line_height = 0

        def layout_spacing(wid, horizontal=True):
            ans = self.smart_spacing(horizontal)
            if ans != -1:
                return ans
            if wid is None:
                return 0
            return wid.style().layoutSpacing(
                QSizePolicy.ControlType.PushButton,
                QSizePolicy.ControlType.PushButton, Qt.Orientation.Horizontal
                if horizontal else Qt.Orientation.Vertical)

        lines, current_line = [], []
        gmap = {}
        for item in self.items:
            isz, wid = item.sizeHint(), item.widget()
            hs, vs = layout_spacing(wid), layout_spacing(wid, False)

            next_x = x + isz.width() + hs
            if next_x - hs > erect.right() and line_height > 0:
                x = erect.x()
                y = y + line_height + vs
                next_x = x + isz.width() + hs
                lines.append((line_height, current_line))
                current_line = []
                line_height = 0
            if apply_geometry:
                gmap[item] = x, y, isz
            x = next_x
            line_height = max(line_height, isz.height())
            current_line.append((item, isz.height()))

        lines.append((line_height, current_line))

        if apply_geometry:
            for line_height, items in lines:
                for item, item_height in items:
                    x, wy, isz = gmap[item]
                    if item_height < line_height:
                        wy += (line_height - item_height) // 2
                    item.setGeometry(QRect(QPoint(x, wy), isz))

        return y + line_height - rect.y() + bottom
Beispiel #7
0
def drag_data(self):
    m = self.model()
    db = m.db
    selected = self.get_selected_ids()
    ids = ' '.join(map(str, selected))
    md = QMimeData()
    md.setData('application/calibre+from_library', ids.encode('utf-8'))
    fmt = prefs['output_format']

    def url_for_id(i):
        try:
            ans = db.format_path(i, fmt, index_is_id=True)
        except:
            ans = None
        if ans is None:
            fmts = db.formats(i, index_is_id=True)
            if fmts:
                fmts = fmts.split(',')
            else:
                fmts = []
            for f in fmts:
                try:
                    ans = db.format_path(i, f, index_is_id=True)
                except:
                    ans = None
        if ans is None:
            ans = db.abspath(i, index_is_id=True)
        return QUrl.fromLocalFile(ans)

    md.setUrls([url_for_id(i) for i in selected])
    drag = QDrag(self)
    col = self.selectionModel().currentIndex().column()
    try:
        md.column_name = self.column_map[col]
    except AttributeError:
        md.column_name = 'title'
    drag.setMimeData(md)
    cover = self.drag_icon(m.cover(self.currentIndex().row()),
                           len(selected) > 1)
    drag.setHotSpot(QPoint(-15, -15))
    drag.setPixmap(cover)
    return drag
Beispiel #8
0
    def popup(self, select_first=True):
        if self.disable_popup:
            return
        p = self
        m = p.model()
        widget = self.parent()
        if widget is None:
            return
        screen = widget.screen().availableGeometry()
        h = (p.sizeHintForRow(0) * min(self.max_visible_items, m.rowCount()) +
             3) + 3
        hsb = p.horizontalScrollBar()
        if hsb and hsb.isVisible():
            h += hsb.sizeHint().height()

        rh = widget.height()
        pos = widget.mapToGlobal(QPoint(0, widget.height() - 2))
        w = min(widget.width(), screen.width())

        if (pos.x() + w) > (screen.x() + screen.width()):
            pos.setX(screen.x() + screen.width() - w)
        if pos.x() < screen.x():
            pos.setX(screen.x())

        top = pos.y() - rh - screen.top() + 2
        bottom = screen.bottom() - pos.y()
        h = max(h, p.minimumHeight())
        if h > bottom:
            h = min(max(top, bottom), h)

            if top > bottom:
                pos.setY(pos.y() - h - rh + 2)

        p.setGeometry(pos.x(), pos.y(), w, h)

        if (tweaks['preselect_first_completion'] and select_first
                and not self.currentIndex().isValid()
                and self.model().rowCount() > 0):
            self.setCurrentIndex(self.model().index(0))

        if not p.isVisible():
            p.show()
Beispiel #9
0
 def point_at(self, frac):
     return (self.path.pointAtPercent(frac).toPoint() -
             QPoint(self.rect().center().x(), self.height()))
Beispiel #10
0
    def do_layout(self, rect, apply_geometry=False):
        x, y = rect.x(), rect.y()

        line_height = 0

        def layout_spacing(wid, horizontal=True):
            ans = self.smart_spacing(horizontal)
            if ans != -1:
                return ans
            return wid.style().layoutSpacing(
                QSizePolicy.ControlType.ToolButton,
                QSizePolicy.ControlType.ToolButton, Qt.Orientation.Horizontal
                if horizontal else Qt.Orientation.Vertical)

        lines, current_line = [], []
        gmap = {}
        if apply_geometry:
            for item in self.items:
                if isinstance(item, Separator):
                    item.setGeometry(0, 0, 0, 0)

        def commit_line():
            while current_line and isinstance(current_line[-1], Separator):
                current_line.pop()
            if current_line:
                lines.append((line_height, current_line))

        for wid in self.items:
            if not wid.isVisible() or (not current_line
                                       and isinstance(wid, Separator)):
                continue
            isz = wid.sizeHint()
            hs, vs = layout_spacing(wid), layout_spacing(wid, False)

            next_x = x + isz.width() + hs
            if next_x - hs > rect.right() and line_height > 0:
                if isinstance(wid, Separator):
                    continue
                x = rect.x()
                y = y + line_height + vs
                next_x = x + isz.width() + hs
                commit_line()
                current_line = []
                line_height = 0
            if apply_geometry:
                gmap[wid] = x, y, isz
            x = next_x
            line_height = max(line_height, isz.height())
            current_line.append(wid)

        commit_line()

        if apply_geometry:
            self.applied_geometry = rect
            for line_height, items in lines:
                for wid in items:
                    x, wy, isz = gmap[wid]
                    if isz.height() < line_height:
                        wy += (line_height - isz.height()) // 2
                    if wid.isVisible():
                        wid.setGeometry(QRect(QPoint(x, wy), isz))

        return y + line_height - rect.y()
Beispiel #11
0
    def do_layout(self, rect, apply_geometry=False):
        x, y = rect.x(), rect.y()

        line_height = 0

        def layout_spacing(wid, horizontal=True):
            ans = self.smart_spacing(horizontal)
            if ans != -1:
                return ans
            return wid.style().layoutSpacing(
                QSizePolicy.ControlType.ToolButton,
                QSizePolicy.ControlType.ToolButton, Qt.Orientation.Horizontal
                if horizontal else Qt.Orientation.Vertical)

        lines, current_line = [], []
        gmap = {}
        if apply_geometry:
            for item in self.items:
                if isinstance(item, Separator):
                    item.setGeometry(0, 0, 0, 0)

        def commit_line():
            while current_line and isinstance(current_line[-1], Separator):
                current_line.pop()
            if current_line:
                lines.append((line_height, current_line))

        groups = []
        current_group = Group(self.parent())
        for wid in self.items:
            if not wid.isVisible() or (not current_group
                                       and isinstance(wid, Separator)):
                continue
            if isinstance(wid, Separator):
                groups.append(current_group)
                current_group = Group(self.parent(), wid)
            else:
                current_group.add_widget(wid)
        if current_group:
            groups.append(current_group)
        x = rect.x()
        y = 0
        line_height = 0
        vs = 0
        for group in groups:
            if current_line and x + group.width >= rect.right():
                commit_line()
                current_line = []
                x = rect.x()
                y += group.height
                group.leading_separator = None
                line_height = 0
            if group.leading_separator:
                current_line.append(group.leading_separator)
                sz = group.leading_separator.sizeHint()
                gmap[group.leading_separator] = x, y, sz
                x += sz.width() + group.layout_spacing(group.leading_separator)
            for item in group.items:
                wid = item.widget
                if not vs:
                    vs = group.layout_spacing(wid, False)
                if apply_geometry:
                    gmap[wid] = x, y, item.sz
                x += item.width + group.layout_spacing(wid)
                current_line.append(wid)
            line_height = group.height

        commit_line()

        if apply_geometry:
            self.applied_geometry = rect
            for line_height, items in lines:
                for wid in items:
                    x, wy, isz = gmap[wid]
                    if isz.height() < line_height:
                        wy += (line_height - isz.height()) // 2
                    if wid.isVisible():
                        wid.setGeometry(QRect(QPoint(x, wy), isz))

        return y + line_height - rect.y()
Beispiel #12
0
    def paintEvent(self, ev):
        offset = QPoint(0, 0)
        p = QPainter(self)
        p.setClipRect(ev.rect())
        bottom = self.rect().bottom()

        if self.results:
            for i, (prefix, full, text) in enumerate(self.results):
                size = prefix.size()
                if offset.y() + size.height() > bottom:
                    break
                self.max_result = i
                offset.setX(0)
                if i in (self.current_result, self.mouse_hover_result):
                    p.save()
                    if i != self.current_result:
                        p.setPen(Qt.PenStyle.DotLine)
                    p.drawLine(offset, QPoint(self.width(), offset.y()))
                    p.restore()
                offset.setY(offset.y() + self.MARGIN // 2)
                p.drawStaticText(offset, prefix)
                offset.setX(self.maxwidth + 5)
                p.drawStaticText(offset, self.divider)
                offset.setX(offset.x() + int(ceil(self.divider.size().width())))
                p.drawStaticText(offset, full)
                offset.setY(int(offset.y() + size.height() + self.MARGIN // 2))
                if i in (self.current_result, self.mouse_hover_result):
                    offset.setX(0)
                    p.save()
                    if i != self.current_result:
                        p.setPen(Qt.PenStyle.DotLine)
                    p.drawLine(offset, QPoint(self.width(), offset.y()))
                    p.restore()
        else:
            p.drawText(self.rect(), Qt.AlignmentFlag.AlignCenter, _('No results found'))

        p.end()
Beispiel #13
0
 def _show_menu(self, x, y):
     m = self.contextMenu()
     if m is not None:
         m.exec_(QPoint(x, y))