[PySide] QGraphicsTextItem

Ofek ofek at outlook.com
Sun Oct 7 00:26:16 CEST 2012


Hi,

 

Thank you for replying so quickly.

I looked at the code, but I'm looking for a way to use the built-in
functions in Qt, rather than computing it manually.

 

As I can't possibly get a proper pageCount() from QTextEdit  (keeps
returning 1) and because it is easier to add multiple QGraphicsTextItem to a
scene, I chose to go with the QGraphics approach.

 

This is what I have so far:

http://pastebin.com/jwfH40JP

 

A few things aren't working right now: I can't seem to set the TextItem flag
from the MultiPage class, only through the class itself.

I also can't focus the TextItem with the mouse or navigate with the
keyboard.

Also line count doesn't work for lines that are just bigger from the page
width (meaning, without \n)

 

Those are all problems I have for being new to both Python and Qt, and will
be solved.

The real question here is if you believe that this approach is good (using
Qt's functions, and the use of QGraphicsTextItem) to get, eventually, a
working word processor.

 

 

Thanks again

 

 

 

 

 

 

From: Zfox Atis [mailto:zfoxatis at gmail.com] 
Sent: Saturday, October 06, 2012 01:12 PM
To: Ofek
Cc: pyside at qt-project.org
Subject: Re: [PySide] QGraphicsTextItem

 

I also like things trying.(this works but is not the final) Maybe it will
give you some ideas:
(http://pastebin.com/S2mj1ZS2 )

class ReportPage(QFrame):
    ''' report rootclass
        create multi-page finance report: dynamic height of the header and
footer'''
    def __init__(self):
        super(ReportPage,self).__init__()

        self.printer=QPrinter()
        self.printer.setPaperSize(QPrinter.A4)
        self.printer.setPageMargins(40,30,40,10,QPrinter.Point)
        self.rootEditorTextEdit=QTextEdit()  
        
        self.createReport()
        self.reportGui()

    def reportGui(self):pass # ...create printpreview widget

    def createReport(self):
  
        self.doc=self.rootEditorTextEdit.document()
        self.doc.setUseDesignMetrics(True)
        self.doc.setPageSize(QSizeF(793,1123)) #(A/4)
        self.doc.setDocumentMargin(0)
        
        self.rootTextCursor = QTextCursor(self.doc)

        self.pageBreakTextBlockFormat=QTextBlockFormat()
 
self.pageBreakTextBlockFormat.setPageBreakPolicy(QTextFormat.PageBreak_Alway
sBefore)

        # document fragments:
        self.firstPageHeader=None
        self.docTitle=None
        self.docPrologText=None
        self.notFirstPageHeader=None
        self.docDetailHeader=None 
        self.detailitems=[]
        self.detailSummary=None
        self.docEndText=None
        self.docEndFooter=None
        self.pageFooter=None
        
        self.reportPartDefinition()
        self.reportCompilation()
        
    def reportPartDefinition(self):pass #child class create to document
fragments 
   
        
    def reportCompilation(self):
        self.max_page_height=self.printer.pageRect().height()-10 # -10 point
repair counting
        try:
            self.page_footer_textHeight=self.pageFooter.textHeight() 
        except: 
            self.page_footer_textHeight=0
        
        #print(self.status())
        
        if self.firstPageHeader:
 
self.rootTextCursor.insertFragment(QTextDocumentFragment(self.firstPageHeade
r))

        if self.docTitle:
 
self.rootTextCursor.insertFragment(QTextDocumentFragment(self.docTitle))

        if self.docPrologText:
 
self.rootTextCursor.insertFragment(QTextDocumentFragment(self.docPrologText)
)
            
        if self.docDetailHeader:
            if
self.currentPageFreeHeightExceptFooter()<self.docDetailHeader.textHeight():
                self.pageBreak()
 
self.rootTextCursor.insertFragment(QTextDocumentFragment(self.docDetailHeade
r))
        
        while len(self.detailitems):
            self.currentitem=self.detailitems.pop(0)
            if
self.currentPageFreeHeightExceptFooter()<self.currentitem.textHeight():
                self.detailPageBreak()
 
self.rootTextCursor.insertFragment(QTextDocumentFragment(self.currentitem))

        if self.detailSummary:
            if
self.currentPageFreeHeightExceptFooter()<self.detailSummary.textHeight():
                self.detailPageBreak()
 
self.rootTextCursor.insertFragment(QTextDocumentFragment(self.detailSummary)
)

        if self.docEndText:
            if
self.currentPageFreeHeightExceptFooter()<self.docEndText.textHeight():
                self.pageBreak()
 
self.rootTextCursor.insertFragment(QTextDocumentFragment(self.docEndText))

        if self.docEndFooter:
            if  self.currentPageFreeHeight()<self.docEndFooter.textHeight():
                self.pageBreak()
            print(self.currentPage())  # last page number
 
self.rootTextCursor.insertBlock(MyTextBlockFormat(self.currentPageFreeHeight
()-self.docEndFooter.textHeight()-18)) 
 
self.rootTextCursor.insertFragment(QTextDocumentFragment(self.docEndFooter))
  
    def pageBreak(self):
        if self.pageFooter:
 
self.rootTextCursor.insertBlock(MyTextBlockFormat(self.currentPageFreeHeight
ExceptFooter()))
 
self.rootTextCursor.insertFragment(QTextDocumentFragment(self.pageFooter))
        print(self.currentPage()) # current page number
        self.rootTextCursor.setBlockFormat(self.pageBreakTextBlockFormat)
        if self.notFirstPageHeader:
 
self.rootTextCursor.insertFragment(QTextDocumentFragment(self.notFirstPageHe
ader))
    
    def detailPageBreak(self):
        self.pageBreak()
        if self.docDetailHeader:
 
self.rootTextCursor.insertFragment(QTextDocumentFragment(self.docDetailHeade
r))
    
    def currentPageFreeHeight(self):
        return
self.max_page_height-18-self.doc.size().height()%self.max_page_height
    # repair counting -18 point (Qt BUG?) blank QDocument size.height=18
point
    # and fragmentdocuments after insertion Qt don't counting rootdocument
into the heights of 18 points
    
    def currentPageFreeHeightExceptFooter(self):
        return self.currentPageFreeHeight()-self.page_footer_textHeight
    
    def currentPage(self): #calculating currentpage number
 
currentpagecount,fragmentpagesize=divmod(self.doc.size().height(),self.max_p
age_height)
        if fragmentpagesize>0:
            currentpagecount+=1
        return currentpagecount

class FragmentTextDocument(QTextDocument):
    '''document fragments rootclass:
        -firstPageHeader
        -docTitle
        -docPrologText
        -notFirstPageHeader
        -docDetailHeader 
        -detailitems items
        -detailSummary
        -docEndText
        -docEndFooter
        -pageFooter'''
    
    def __init__(self,**kwargs):
        super(FragmentTextDocument,self).__init__()
        self.textdict=kwargs

        self.setUseDesignMetrics(True)
        self.setPageSize(QSizeF(793,1123))
        self.setDocumentMargin(0)
        
        self.fragmentTextEdit=QTextEdit()
        self.fragmentTextCursor = self.fragmentTextEdit.textCursor() 
        self.fragmentTextCursor.movePosition(QTextCursor.Start)
        
        self.createFormats()
        self.detailText()
        self.setHtml(self.fragmentTextEdit.toHtml())

    def createFormats(self): pass #common formats
        
       
    def detailText(self):pass #child class create document
    
    
    def textHeight(self):
        return self.size().height()+2




2012/10/6 Ofek <ofek at outlook.com>

Hey,

 

I am trying to produce a text editor similar to word (paginated) in PySide.

The way I decided to tackle this is having a QGraphicsScene with
QGraphicsTextItem for each page, and to code the focus by interaction.

Now all I am trying to do now is have a box representing an A4 page, and
handling textchanged events using the
QGraphicsTextItem.document().pageCount() function.

I bumped into several issues:

1.	How can I properly define the size of the page given the QSizeF is
not using any units? Will defining this right guarantee a true return value
from pageCount()?
2.	If I define a pageSize at all, QGraphicsTextItem stops expanding.
How can I set a fixed size so It'll look like a page?

 

Documentation for Qt is extremely light and I could not figure anything out
of it.

 

Thanks!


_______________________________________________
PySide mailing list
PySide at qt-project.org
http://lists.qt-project.org/mailman/listinfo/pyside

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20121007/82fa01a3/attachment.html>


More information about the PySide mailing list