Example #1
0
 def placeText(self, text, font, x, y, alignLeft):
     """Used for subject headers and teacher names, to place
     them on the canvas.
     """
     ti = TextItem(self.canvas, text)
     ti.setFont(font)
     if not alignLeft:       # Right alignment
         x = self.layoutInfo.mainWidth - x - font.getWidth(text)
     ti.setPos(x, y)
     return ti
    def printout(self, filepath=None, pages=None):
        """Print the selected pages ('None' => all pages !).
        If filepath is given the output will be to a file, otherwise
        the default printer.
#Or should I use the print dialog?
        """
        printer = Printer(filename=filepath)
        first = True    # to control 'newPage'

        npages = len(self.pages)    # total number of pages
        if (pages == None):
            pages = range(1, npages+1)

        for pn in pages:
            p = self.pages[pn-1]

            # add header/footer (class, subject, ...)
            text = u"%s - %s  (%s)" % (
                    argSub(_("Class %1"), (self.className,)),
                    self.subjectName,
                    argSub(_("page %1 of %2"), (unicode(pn), unicode(npages))))
            ti = TextItem(p, text)
            ti.setFont(self.titleFont)
            ti.setPos(XTITLE, YTITLE - self.titleSpace)

            LineH(p, XTITLE, 0.0,
                    self.titleFont.getWidth(text),
                    UNDERLINEWIDTH,
                    getColour(u"#606060"))

            if not first:
                printer.newPage()

            printer.render(p.gScene)

            first = False

        printer.end()
Example #3
0
 def placeText(self, text, font, x, y, alignLeft):
     """Used for subject headers and teacher names, to place
     them on the canvas.
     """
     ti = TextItem(self.canvas, text)
     ti.setFont(font)
     if not alignLeft:  # Right alignment
         x = self.layoutInfo.mainWidth - x - font.getWidth(text)
     ti.setPos(x, y)
     return ti
Example #4
0
    def renderItem(self, idict, page=u"-"):
        """Render the item described by the given dictionary to the
        current page.
        """
        x = float(idict[u"x"])
        y = float(idict[u"y"])
        object = idict[u"object"]

        try:
            table, item = object.split(u"/")
            odict = self.layoutDict[table][item]
            if (table == u"lines"):
                self.renderLine(odict, x, y)

            elif (table == u"text"):
                t = self.textSub(odict[u"text"])
                ti = TextItem(self.page, t)
                ti.setFont(self.db.layoutInfo.getFont(odict[u"font"]))
                ti.setPos(x, y)

            elif (table == u"images"):
                file = odict[u"file"]
                data = self.db.getBFile(u"imagefiles/%s" % file)

                image = Image(self.page, data)
                image.setPos(x, y)
                image.setSize(float(odict[u"vsize"]))

            elif (table == u"blocks"):
                self.renderSubjects(odict, x, y)

            else:
                raise

        except:
            print_exc()

            warning(_("Invalid item for print rendering on page %1:\n  %2"),
                    (page, object))
Example #5
0
    def printout(self, filepath=None, pages=None):
        """Print the selected pages ('None' => all pages !).
        If filepath is given the output will be to a file, otherwise
        the default printer.
#Or should I use the print dialog?
        """
        printer = Printer(filename=filepath)
        first = True  # to control 'newPage'

        npages = len(self.pages)  # total number of pages
        if (pages == None):
            pages = range(1, npages + 1)

        for pn in pages:
            p = self.pages[pn - 1]

            # add header/footer (class, subject, ...)
            text = u"%s - %s  (%s)" % (argSub(
                _("Class %1"), (self.className, )), self.subjectName,
                                       argSub(_("page %1 of %2"),
                                              (unicode(pn), unicode(npages))))
            ti = TextItem(p, text)
            ti.setFont(self.titleFont)
            ti.setPos(XTITLE, YTITLE - self.titleSpace)

            LineH(p, XTITLE, 0.0, self.titleFont.getWidth(text),
                  UNDERLINEWIDTH, getColour(u"#606060"))

            if not first:
                printer.newPage()

            printer.render(p.gScene)

            first = False

        printer.end()
Example #6
0
    def __init__(self, db, subject, pupils):
        layoutInfo = db.layoutInfo

        self.subjectName = db.getSubjectName(subject)
        self.className = db.classObject.className
        self.titleFont = layoutInfo.titleFont
        self.titleSpace = self.titleFont.lineSpacing + TITLEGAP

        docinfo = layoutInfo.layoutDict[u"document"]
        width = float(docinfo[u"pageWidth"])
        height = float(docinfo[u"pageHeight"])
        y0 = float(docinfo[u"topSpace"])
        x0 = (width - layoutInfo.mainWidth) / 2
        lineWidth = layoutInfo.lineWidth
        textSpace = float(docinfo[u"textAreaHeight"])

        page = None  # current page
        self.pages = []  # list of pages

        for p in pupils:
            rs = RSubject(p, subject, db)
            pupilName = db.getPupilName(p)

            # Take the existing frames and re-render these in the
            # available area, one frame at a time
            lix = 0  # line index
            nlines = len(rs.tlines)
            frameNo = 0  # frame number

            for frame in rs.frames:
                frameNo += 1

                # Get all the lines from this frame
                lines = []
                while (lix < nlines):
                    line = rs.tlines[lix]
                    if (line.frame != frame):
                        break
                    lines.append(line)
                    lix += 1

                if not lines:
                    # Don't show empty frames
                    break

                # will they fit on the current page?
                if (not page) or self.noFit(textSpace - yoff - self.titleSpace,
                                            lines):
                    # if not, end the page and get a new one
                    if (len(self.pages) % 2):
                        xoff = -XSHIFT
                    else:
                        xoff = XSHIFT
                    page = Page(x0 + xoff, y0, width, height, lineWidth)
                    self.pages.append(page)

                    # set yoff
                    yoff = GAP

                # Render the collected lines

                #  - pupil name + frame number
                text = pupilName
                if (frameNo > 1):
                    if (frameNo < len(rs.frames)):
                        text += u" (%d)" % frameNo
                    else:
                        # overflow frame
                        text = u"     (%s [X])" % text
                ti = TextItem(page, text)
                ti.setFont(self.titleFont)
                ti.setPos(0.0, yoff)

                yoff += self.titleSpace - lines[0].y

                for l in lines:
                    l.render0(page, yoff)

                # reset yoff to after last line
                ##yoff += lines[-1].getYafter() - lines[0].y
                yoff += lines[-1].getYafter()

                # gap at end
                yoff += GAP
    def __init__(self, db, subject, pupils):
        layoutInfo = db.layoutInfo

        self.subjectName = db.getSubjectName(subject)
        self.className = db.classObject.className
        self.titleFont = layoutInfo.titleFont
        self.titleSpace = self.titleFont.lineSpacing + TITLEGAP

        docinfo = layoutInfo.layoutDict[u"document"]
        width = float(docinfo[u"pageWidth"])
        height = float(docinfo[u"pageHeight"])
        y0 = float(docinfo[u"topSpace"])
        x0 = (width - layoutInfo.mainWidth) / 2
        lineWidth = layoutInfo.lineWidth
        textSpace = float(docinfo[u"textAreaHeight"])

        page = None         # current page
        self.pages = []     # list of pages

        for p in pupils:
            rs = RSubject(p, subject, db)
            pupilName = db.getPupilName(p)

            # Take the existing frames and re-render these in the
            # available area, one frame at a time
            lix = 0     # line index
            nlines = len(rs.tlines)
            frameNo = 0         # frame number

            for frame in rs.frames:
                frameNo += 1

                # Get all the lines from this frame
                lines = []
                while  (lix < nlines):
                    line = rs.tlines[lix]
                    if (line.frame != frame):
                        break
                    lines.append(line)
                    lix += 1

                if not lines:
                    # Don't show empty frames
                    break

                # will they fit on the current page?
                if (not page) or self.noFit(textSpace - yoff - self.titleSpace,
                        lines):
                    # if not, end the page and get a new one
                    if (len(self.pages) % 2):
                        xoff = -XSHIFT
                    else:
                        xoff = XSHIFT
                    page = Page(x0 + xoff, y0, width, height, lineWidth)
                    self.pages.append(page)

                    # set yoff
                    yoff = GAP

                # Render the collected lines

                #  - pupil name + frame number
                text = pupilName
                if (frameNo > 1):
                    if (frameNo < len(rs.frames)):
                        text += u" (%d)" % frameNo
                    else:
                        # overflow frame
                        text = u"     (%s [X])" % text
                ti = TextItem(page, text)
                ti.setFont(self.titleFont)
                ti.setPos(0.0, yoff)

                yoff += self.titleSpace - lines[0].y

                for l in lines:
                    l.render0(page, yoff)

                # reset yoff to after last line
                ##yoff += lines[-1].getYafter() - lines[0].y
                yoff += lines[-1].getYafter()

                # gap at end
                yoff += GAP
Example #8
0
 def setCanvas(self, canvas):
     """Allocate a graphical item for the word on the given canvas.
     """
     self.canvas = canvas
     self.textItem = TextItem(canvas, self.string)
     self.textItem.setFont(self.tline.para.font)
Example #9
0
class TWord:
    """Manages / encapsulates a single text word in a report.
    It must contain the text string, and eventually it will
    contain the graphics object. It provides a method (getWidth) to
    obtain the display/print width of the word.
    After rendering it also contains the x-coordinate relative to the
    beginning of its display line. The units are mm.
    In addition the x-offsets of the characters within the word are
    calculated on demand and cached in the variable 'offsets'.
    """
    def __init__(self, string):
        """string: the text - it should be unicode.
        (the text can be changed later)
        """
        self.x = None
        self.tline = None
        self.textItem = None    # initially there is no graphical item
        self.canvas = None
        self.spellingError = False
        self.spellItem = None
        self.setString(string)

    def setString(self, string):
        """Change the text of the word.
        """
        self.string = string
        self.offsets = None
        self.width = None
        if autoSpellCheck:
            self.spellingError = not checkSpelling.check(string)
            if self.spellItem and (not self.spellingError):
                self.spellItem.remove()
                self.spellItem = None

        if self.textItem:
            self.textItem.setText(self.string)

    def getWidth(self):
        """Get the display/print width of this word.
        """
        if (self.width == None):
            self.width = self.tline.para.font.getWidth(self.string)
        return self.width

    def setPos(self, x):
        """Place the word on the canvas at the given x-coordinate.
        The y-coordinate comes from the Textline.
        """
        self.x = x
        y = self.tline.yReal
        self.textItem.setPos(x, y)

        if self.spellItem:
            self.spellItem.setPos(x, y + self.tline.height, self.getWidth())

        elif self.spellingError:
            self.markSpellingError()

    def markSpellingError(self):
        self.spellItem = LineH(self.canvas, self.x,
                self.tline.yReal + self.tline.height,
                self.getWidth(), SPELLTHICKNESS, colours("spell"))


    def setCanvas(self, canvas):
        """Allocate a graphical item for the word on the given canvas.
        """
        self.canvas = canvas
        self.textItem = TextItem(canvas, self.string)
        self.textItem.setFont(self.tline.para.font)

    def delete(self):
        """The destructor method.
        As TWord objects form circular references with TextLine objects,
        I provide this function to break the cycles, so as not
        to rely on the garbage collector.
        Also the graphical items need removing from the editor view.
        """
        self.tline = None
        self.textItem.remove()
        del(self.textItem)
        del(self.canvas)
        if self.spellItem:
            self.spellItem.remove()
            del(self.spellItem)

    def getX(self, charIx=0):
        """Return the x-coordinate (not offset!) of the character at the
        given index.
        """
        if (charIx == 0):
            x = 0.0
        else:
            x = self.getOffsets()[charIx - 1]
        return self.x + x

    def getOffsets(self):
        """Return a list of character x-offsets, starting at the second
        character (the first character always has offset 0.0).
        """
        if (self.offsets == None):
            dlfont = self.tline.para.font
            self.offsets = []
            k = 1
            while (k <= len(self.string)):
                self.offsets.append(dlfont.getWidth(self.string[:k]))
                k += 1
        return self.offsets

    def setTLine(self, ntline):
        """Tell the TWord object when it is allocated to a new
        TLine instance.
        """
        # Check whether the style of the word has changed
        if (not self.tline) or (self.tline.para.font != ntline.para.font):
            self.offsets = None
            self.width = None
            if self.textItem:
                self.textItem.setFont(ntline.para.font)
        self.tline = ntline

    def getXYH(self, cx):
        """Get editor view coordinates of a given character offset within
        this word.
        Returns a 3-tuple (x, y, lineHeight)
        """
        x = self.getX(cx)
        tl = self.tline
        return (x, tl.y + tl.frame.yF, tl.height)

    def spellUncheck(self):
        """Clears spell-checking data from word.
        """
        self.spellingError = False
        if self.spellItem:
            self.spellItem.remove()
            self.spellItem = None

    def spellCheck(self):
        """Check spelling of the word.
        """
        self.spellingError = not checkSpelling.check(self.string)
        if self.spellingError:
            self.markSpellingError()
Example #10
0
 def setCanvas(self, canvas):
     """Allocate a graphical item for the word on the given canvas.
     """
     self.canvas = canvas
     self.textItem = TextItem(canvas, self.string)
     self.textItem.setFont(self.tline.para.font)
Example #11
0
class TWord:
    """Manages / encapsulates a single text word in a report.
    It must contain the text string, and eventually it will
    contain the graphics object. It provides a method (getWidth) to
    obtain the display/print width of the word.
    After rendering it also contains the x-coordinate relative to the
    beginning of its display line. The units are mm.
    In addition the x-offsets of the characters within the word are
    calculated on demand and cached in the variable 'offsets'.
    """
    def __init__(self, string):
        """string: the text - it should be unicode.
        (the text can be changed later)
        """
        self.x = None
        self.tline = None
        self.textItem = None  # initially there is no graphical item
        self.canvas = None
        self.spellingError = False
        self.spellItem = None
        self.setString(string)

    def setString(self, string):
        """Change the text of the word.
        """
        self.string = string
        self.offsets = None
        self.width = None
        if autoSpellCheck:
            self.spellingError = not checkSpelling.check(string)
            if self.spellItem and (not self.spellingError):
                self.spellItem.remove()
                self.spellItem = None

        if self.textItem:
            self.textItem.setText(self.string)

    def getWidth(self):
        """Get the display/print width of this word.
        """
        if (self.width == None):
            self.width = self.tline.para.font.getWidth(self.string)
        return self.width

    def setPos(self, x):
        """Place the word on the canvas at the given x-coordinate.
        The y-coordinate comes from the Textline.
        """
        self.x = x
        y = self.tline.yReal
        self.textItem.setPos(x, y)

        if self.spellItem:
            self.spellItem.setPos(x, y + self.tline.height, self.getWidth())

        elif self.spellingError:
            self.markSpellingError()

    def markSpellingError(self):
        self.spellItem = LineH(self.canvas, self.x,
                               self.tline.yReal + self.tline.height,
                               self.getWidth(), SPELLTHICKNESS,
                               colours("spell"))

    def setCanvas(self, canvas):
        """Allocate a graphical item for the word on the given canvas.
        """
        self.canvas = canvas
        self.textItem = TextItem(canvas, self.string)
        self.textItem.setFont(self.tline.para.font)

    def delete(self):
        """The destructor method.
        As TWord objects form circular references with TextLine objects,
        I provide this function to break the cycles, so as not
        to rely on the garbage collector.
        Also the graphical items need removing from the editor view.
        """
        self.tline = None
        self.textItem.remove()
        del (self.textItem)
        del (self.canvas)
        if self.spellItem:
            self.spellItem.remove()
            del (self.spellItem)

    def getX(self, charIx=0):
        """Return the x-coordinate (not offset!) of the character at the
        given index.
        """
        if (charIx == 0):
            x = 0.0
        else:
            x = self.getOffsets()[charIx - 1]
        return self.x + x

    def getOffsets(self):
        """Return a list of character x-offsets, starting at the second
        character (the first character always has offset 0.0).
        """
        if (self.offsets == None):
            dlfont = self.tline.para.font
            self.offsets = []
            k = 1
            while (k <= len(self.string)):
                self.offsets.append(dlfont.getWidth(self.string[:k]))
                k += 1
        return self.offsets

    def setTLine(self, ntline):
        """Tell the TWord object when it is allocated to a new
        TLine instance.
        """
        # Check whether the style of the word has changed
        if (not self.tline) or (self.tline.para.font != ntline.para.font):
            self.offsets = None
            self.width = None
            if self.textItem:
                self.textItem.setFont(ntline.para.font)
        self.tline = ntline

    def getXYH(self, cx):
        """Get editor view coordinates of a given character offset within
        this word.
        Returns a 3-tuple (x, y, lineHeight)
        """
        x = self.getX(cx)
        tl = self.tline
        return (x, tl.y + tl.frame.yF, tl.height)

    def spellUncheck(self):
        """Clears spell-checking data from word.
        """
        self.spellingError = False
        if self.spellItem:
            self.spellItem.remove()
            self.spellItem = None

    def spellCheck(self):
        """Check spelling of the word.
        """
        self.spellingError = not checkSpelling.check(self.string)
        if self.spellingError:
            self.markSpellingError()