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()
def markSpellingError(self): self.spellItem = LineH(self.canvas, self.x, self.tline.yReal + self.tline.height, self.getWidth(), SPELLTHICKNESS, colours("spell"))
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()
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()