Ejemplo n.º 1
0
def filterBS(rows):
    c = []
    rowsEl, maxRowWidth = [], 0
    for y in range(len(rows)):
        x = 0
        rowEl = []
        while x < len(rows[y]):
            d = rows[y][x]
            if not d:
                x = x + 1
                continue
            # if isinstance(d, str) and not (d[0].isascii() and d[0].isalpha()):
            #     x = x + 1
            #     continue
            el = MapDataElement.createWithXY(x, y, d)
            if el:
                c.append(el)
                rowEl.append(el)
            elif x == 0:
                d = str(d)
                for xx in range(1, len(rows[y])):
                    el = MapDataElement.createWithXY(x, y, rows[y][xx])
                    if el:
                        el.text, el.textAlign, el.textPlacement = d, 'r', 'l'
                        c.append(el)
                        rowEl.append(el)
                        rows[y][0:xx] = []
                        break
                    else:
                        d = d + str(rows[y][xx])
            elif c:
                el: MapDataElement = c[-1]
                el.text, el.textAlign, el.textPlacement = str(d), 'l', 'r'
            x = x + 1
        if rowEl:
            maxRowWidth = max(rowEl[-1].x, maxRowWidth)
            rowsEl.append(rowEl)
    maxRowWidth = maxRowWidth // 2 * 2 + 1  # keep it odd
    for row in rowsEl:
        if not row:
            continue
        prepend = (maxRowWidth - row[-1].x) // 2
        if prepend > 0:
            for el in row:
                el.x = el.x + prepend
        if row[-1].text:
            row[-1].textX = (maxRowWidth - row[-1].x) * BS
    return c
Ejemplo n.º 2
0
    def actPaste(self):
        c = []
        text = QtGui.QGuiApplication.clipboard().text()
        bad = False

        try:
            l = json.loads(text)
            if not isinstance(l, list):
                c = filterBS(parseBS(text))
            else:
                for s in l:
                    d = MapDataElement.fromdict(s)
                    if d and d.src and d.src.svgId:
                        c.append(d)
                    else:
                        bad = True
        except JSONDecodeError:
            c = filterBS(parseBS(text))

        if bad or len(c) == 0:
            QMessageBox(
                QMessageBox.Icon.Warning, TR("Paste"),
                c and TR("__paste_filter_invalid_blocks__").format(len(c))
                or TR("__paste_no_valid_blocks__")).exec_()
        len(c) and self.ghostHold(c)
Ejemplo n.º 3
0
    def __init__(self, parent):
        super().__init__(parent)

        self.data = MapData(self)
        self.scale = 2

        if FLAGS["DEBUG_fill"]:
            sources = []
            for i in range(0, 10):
                sources.append(
                    SvgSource(
                        "id" + str(i), """
                    <svg height="48" width="48">
                        <text x="8" y="24" fill="red">{}</text>
                        <rect x="8" y="8" width="32" height="32" fill="transparent" stroke="#000"></rect>
                    </svg>""".format(i).encode('utf-8'), BS, BS))
            n = FLAGS["DEBUG_fill"]
            s = math.sqrt(n)
            for i in range(0, n):
                l = int(random.random() * s + 1)
                d = random.random() * math.pi * 2
                x, y = int(l * math.cos(d)), int(l * math.sin(d))
                el = MapDataElement(sources[random.randrange(0, len(sources))])
                if random.random() > 0.9:
                    el.cascades.append(sources[random.randrange(
                        0, len(sources))])
                if random.random() > 0.8:
                    el.text = "lazy\nfox jumps"
                self.data._put(x, y, el)

        self.selector = Selector(self)
        self.dragger = Dragger(self)
        self.hover = Hover(self)
        self.ruler = Ruler(self)
        self.boxRows = self.boxCols = 0
        self.viewOrigin = [0, 0]
        self.pan(0, 0)  # fill svgBoxes
        self.setMouseTracking(True)
        self.pressPos = None
        self.pressPosPath = []
        self.pressHoldSel = False
        self.showRuler = True
        self.setFocusPolicy(QtCore.Qt.FocusPolicy.ClickFocus)
Ejemplo n.º 4
0
 def findCellUnder(self, a0: QtGui.QMouseEvent, c: QtCore.QPoint = None):
     c = a0 and a0.pos() or c
     bs = self._blocksize()
     sx, sy = self.deltaxy()
     rr = math.floor
     pt = QtCore.QPoint(
         int((c.x() - sx) / bs) * bs + sx,
         int((c.y() - sy) / bs) * bs + sy)
     x_ = (c.x() - self.viewOrigin[0]) / bs  # xy of data
     y_ = (c.y() - self.viewOrigin[1]) / bs
     x, y = rr(x_), rr(y_)
     if x_ == x or y_ == y:  # special case: cursor on edge, no cell found
         return None, pt
     d = self.data.get(x, y) or MapDataElement(x=x, y=y)
     return d, pt
Ejemplo n.º 5
0
 def load(self, fn: str):
     d: MapData = self.mapview.data
     crashfn = fn.removesuffix(".bsm") + ".crash.bsm"
     delcrash = False
     if os.path.exists(crashfn):
         ans = QMessageBox.question(self, TR('Open'),
                                    TR('__open_crash__').format(crashfn))
         if ans == QMessageBox.StandardButton.Yes:
             if fn == "":
                 fn = crashfn
             else:
                 filename = os.path.basename(fn)
                 dir = os.path.dirname(fn)
                 shutil.copy2(fn, os.path.join(dir,
                                               "." + filename + ".old"))
                 shutil.copy2(crashfn, fn)
             delcrash = True
         else:
             os.remove(crashfn)
     if not os.path.exists(fn):
         return
     with open(fn, 'rb') as f:
         try:
             fd = json.load(f)
         except JSONDecodeError as e:
             QMessageBox(QMessageBox.Icon.Critical, TR('Open'),
                         TR('__open_fail__').format(fn)).exec_()
             QtCore.qDebug("{}".format(e).encode("utf-8"))
             return
         d.clearHistory()
         d.data = {}
         for c in fd['data']:
             el = MapDataElement.fromdict(c)
             if el:
                 d.data[(el.x, el.y)] = el
         # self.mapview.pan(0, 0)
         self.mapview.center()
         self._updateCurrentFile(fn)
         self.fileMeta["author"] = fd.get("author")
         self.fileMeta["desc"] = fd.get("desc")
         self.mapview.ruler.fromdict(fd.get("rulers"))
     if delcrash:
         os.remove(crashfn)
Ejemplo n.º 6
0
 def onCopy(self, src):
     if not src:
         return
     v = json.dumps([MapDataElement(src).todict()])
     QApplication.clipboard().setText(v)
     self.findMainWin().mapview.setFocus()
Ejemplo n.º 7
0
 def ghostHoldSvgSource(self, s):
     self.mapview.ghostHold([MapDataElement(s)])