Results 1 to 7 of 7

Thread: Paint QTextDocument splitted by pages 1/5 and edit txt

  1. #1
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Paint QTextDocument splitted by pages 1/5 and edit txt

    I try to (draw) in split mode a QTextDocument on each page to edit text & display pages, (like openoffice)
    the spitt is only a QGraphicsItem paint after i retranslate
    all keybord / mouse event to correct QPointF, only to textapi.

    But i can not controll the cutting blockelement ...
    the page 1 & 2 is ok after the cutting paint go crazy.

    I suppose i must write a QPaintEngine like QTextDocument[::]print() and PageBreakFlags must run i suppose ?
    Or is here other way?

    How i subclass QPaintEngine to binding to QGraphicsItem[::]paint() ?
    and respect PageBreakFlags policy...


    Qt Code:
    1. /* M_PageSize DDPAGE; = container
    2.   from margin and multi page size */
    3. void BaseText::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    4. QWidget *widget)
    5. {
    6. const QRectF rectalldoc = boundingRect(); /* all page rect call to recalc pagetotal */
    7. const int PageS = DocPageSumme;
    8. QTextDocument *_d = document();
    9. for (int i = 0; i < PageS; ++i) {
    10. painter->save();
    11. const QRectF innrect = DDPAGE.PageInternal(i); /* rect to place one page */
    12. painter->translate(innrect.topLeft().x(), innrect.topLeft().y() - (i * DDPAGE.hight()) );
    13. _d->drawContents(painter,QRectF(0, i * DDPAGE.hight() ,_d->size().rwidth(), DDPAGE.hight() ));
    14. painter->restore();
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Paint QTextDocument splitted by pages 1/5 and edit txt

    I solved self i paint page like a normal printer, each page having a space to next.

    I take a QGraphicsRectItem and i draw inline QTextDocument and blink textcursor
    After if pageCount() change i set a new Qrect to item and scene

    Only QT 4.4 or 4.5 Can Make its , i suppose QTextDocumentFragment update...

    Qt Code:
    1. void ScribePage::paint(QPainter * painter ,
    2. const QStyleOptionGraphicsItem *option , QWidget *widget )
    3. {
    4. /* QTextDocument = _d */
    5. PageTotal = _d->pageCount();
    6. const int PageSumm = qBound (1,_d->pageCount(),MaximumPages);
    7. QTextFrame *Tframe = _d->rootFrame();
    8. root_format = Tframe->frameFormat();
    9. const QRectF ActiveBlock = CurrentBlockRect();
    10. /* discovery qtextcursor living Current_Page_Nr */
    11.  
    12. painter->save();
    13. painter->setPen( Qt::NoPen );
    14. painter->setBrush(Qt::lightGray);
    15. painter->drawRect(boundingRect());
    16. painter->restore();
    17. for (int i = 0; i < PageSumm; ++i) {
    18. const QPointF topleft = PageIndexTopLeft(i); /* page top left point */
    19. DrawPage(i,painter,i); /* active page from cursor !!
    20.   selectAll must draw cursor each page*/
    21. }
    22.  
    23. }
    24.  
    25. void ScribePage::DrawPage( const int index ,
    26. QPainter * painter , const int cursorpage )
    27. {
    28. const QPointF topleft = PageIndexTopLeft(index);
    29. QAbstractTextDocumentLayout::PaintContext CTX;
    30. CTX.palette.setColor(QPalette::Text, Qt::black);
    31. const QRectF body = QRectF(0, topleft.y() ,Page_Edit_Rect.width(),
    32. Page_Edit_Rect.height()); /* on view */
    33. QRectF view(0, index * body.height(), body.width(), body.height() ); /* on doc */
    34.  
    35. if (index != cursorpage || !Edit_On()) {
    36. painter->save();
    37. painter->translate(body.left(), body.top() - index * body.height());
    38. painter->setClipRect(view);
    39. CTX.clip = view;
    40. _d->documentLayout()->draw(painter,CTX);
    41. painter->restore();
    42. return;
    43. }
    44.  
    45. /* draw cursor active page no !edit no print only display edit! */
    46. painter->save();
    47. painter->translate(body.left(), body.top() - index * body.height());
    48. painter->setClipRect(view);
    49. CTX.clip = view;
    50. QColor BackHightlight("#0072ab");
    51. BackHightlight.setAlpha(180);
    52. CTX.palette.setColor(QPalette::Text, Qt::black);
    53. CTX.palette.setColor(QPalette::Highlight,BackHightlight);
    54. CTX.palette.setColor(QPalette::HighlightedText,Qt::white);
    55. CTX.selections;
    56. CTX.clip = view;
    57. CTX.cursorPosition = -1;
    58. /* play cursor */
    59.  
    60. if (cursortime) { /* blink intervall by timer */
    61. CTX.cursorPosition = C_cursor.position();
    62. }
    63. if ( C_cursor.hasSelection()) {
    64. QAbstractTextDocumentLayout::Selection Internal_selection;
    65. Internal_selection.cursor = C_cursor;
    66.  
    67. QPalette::ColorGroup cg = cursorIsFocusIndicator ? QPalette::Active : QPalette::Inactive;
    68. Internal_selection.format.setBackground(CTX.palette.brush(cg, QPalette::Highlight));
    69. Internal_selection.format.setForeground(CTX.palette.brush(cg, QPalette::HighlightedText));
    70. Internal_selection.format.setProperty(QTextFormat::FullWidthSelection, true);
    71. CTX.selections.append(Internal_selection);
    72.  
    73. }
    74. _d->documentLayout()->draw(painter,CTX);
    75. painter->restore();
    76. }
    To copy to clipboard, switch view to plain text mode 

    Results ....



    QTextControl can not make. i write my own handler... a lot of work and day...
    svn
    http://fop-miniscribus.googlecode.co...perSizeScroll/

    The xsl-fo read write separate routine can make Bookmark PDF save as RTF , html and open from OpenOffice xslt convert to html

    My question: I ask you to help me to insert 150 QAction, Have you time?

  3. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Paint QTextDocument splitted by pages 1/5 and edit txt

    Now i have a problem on a start drag if i set a setPixmap on QDrag it cut image on topleft 50x50 why this?
    What is not ok?

    IMO i can not printscreen mouse and drag...

    Qt Code:
    1. bool TextProcessor::StartDragOperation()
    2. {
    3. DragFill = false;
    4. if (!C_cursor.hasSelection()) {
    5. return DragFill;
    6. }
    7. QMimeData *data = createMimeDataFromSelection();
    8. if (data) {
    9. QApplication::clipboard()->setMimeData(data);
    10. QDrag *drag = new QDrag(Gwi); /* QWidget *Gwi; from event */
    11. drag->setMimeData(data);
    12. drag->setHotSpot(QPoint(-25,-25));
    13. /* try to make a QPixmap from mime html fragment or image drag drop */
    14. const QPixmap playdragicon = ImagefromMime(data);
    15. if (!playdragicon.isNull()) {
    16. drag->setPixmap(playdragicon);
    17. }
    18. if (drag->exec(Qt::CopyAction |
    19. Qt::MoveAction,
    20. Qt::CopyAction) == Qt::MoveAction) {
    21. DragFill = true;
    22. emit q_startDrag(PointPositionOnDoc); /* pos from events */
    23. }
    24. }
    25. return DragFill;
    26. }
    27.  
    28. QMimeData *TextProcessor::createMimeDataFromSelection()
    29. {
    30. QTextCharFormat base = C_cursor.charFormat();
    31. QString txt;
    32.  
    33. if (C_cursor.hasSelection()) {
    34. txt = C_cursor.selectedText();
    35. }
    36.  
    37. QTextImageFormat pico = base.toImageFormat();
    38. if (pico.isValid()) {
    39. QVariant xx = pico.property(_IMAGE_PICS_ITEM_);
    40. if (!xx.isNull()) {
    41. SPics pic = xx.value<SPics>();
    42. QList<SPics> li;
    43. li.append(pic);
    44. QString Sdd = SaveImageGroup(li);
    45. QMimeData *mimeData = new QMimeData;
    46. mimeData->setData("application/x-picslists",Sdd.toUtf8());
    47. return mimeData;
    48. }
    49. }
    50.  
    51. const QTextDocumentFragment fragment(C_cursor);
    52. if ( fragment.isEmpty() && txt.size() > 0 ) {
    53. QMimeData *xm = new QMimeData();
    54. xm->setText(txt);
    55. return xm;
    56. } else if (fragment.isEmpty() && txt.isEmpty() ) {
    57. QMimeData *xm = new QMimeData();
    58. xm->setText(QString("...Selection...Error..."));
    59. return xm;
    60. } else {
    61. return new QTextEditMimeData(fragment);
    62. /////QTextDocumentFragment::fromHtml(md->html())
    63. }
    64. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Paint QTextDocument splitted by pages 1/5 and edit txt

    I have other Problem on QTextDocument

    QTextDocument can not draw fontLetterSpacing 200% or more
    Can any body pleas try this code piece on qt4.4 and confirm is a bug or not...
    Only QTextEdit draw it correct ... but i need QPicture to replay paint from cache



    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main( int argc, char ** argv )
    4. {
    5. QApplication app( argc, argv );
    6.  
    7. QTextDocument d("This is some text at Page #Page# .");
    8. t.setDocument(&d);
    9. t.selectAll();
    10.  
    11. const qreal spacingnow = t.textCursor().charFormat().fontLetterSpacing();
    12. bool ok;
    13. qreal space = QInputDialog::getDouble(0, QObject::tr("Font Letter Spacing"),
    14. QObject::tr("Space:"),spacingnow,90,2000, 2, &ok);
    15. if (space > 0 && ok) {
    16. QTextCursor c = t.textCursor();
    17. QTextCharFormat format = c.charFormat();
    18. qDebug() << "This is some text at Page #Page# ." << space;
    19. format.setFontLetterSpacing(space);
    20. c.setCharFormat(format);
    21. }
    22.  
    23. t.show();
    24. QTextDocument * doc = d.clone();
    25. for (QTextBlock srcBlock = d.firstBlock(), dstBlock = doc->firstBlock();
    26. srcBlock.isValid() && dstBlock.isValid();
    27. srcBlock = srcBlock.next(), dstBlock = dstBlock.next()) {
    28. dstBlock.layout()->setAdditionalFormats(srcBlock.layout()->additionalFormats());
    29. }
    30.  
    31. QTextCursor cu(doc);
    32. const int PageNr = 1;
    33.  
    34. /* search _PAGE_NUMERATION_ #Page# and replace nr. */
    35. qDebug() << "Replace #Page# as number " << PageNr << "only on clone doc";
    36.  
    37. QTextCursor bcu = doc->find("#Page#",cu,QTextDocument::FindWholeWords);
    38. if (!bcu.isNull ()) {
    39. if (bcu.hasSelection()) {
    40. QTextCharFormat format = bcu.charFormat(); /* take from selection */
    41. QString remtxt = bcu.selectedText();
    42. for (int i = 0; i < remtxt.size(); ++i) {
    43. bcu.deleteChar();
    44. }
    45. bcu.insertText(QString("%1").arg(PageNr),format);
    46. bcu.clearSelection();
    47. }
    48. }
    49.  
    50.  
    51. QTextFrame *Tframe = doc->rootFrame();
    52. const QRectF stxt = doc->documentLayout()->frameBoundingRect(Tframe);
    53. QPicture img;
    54. img.setBoundingRect(stxt.toRect());
    55.  
    56. QPainter painter;
    57. painter.begin(&img);
    58. painter.setRenderHint(QPainter::TextAntialiasing);
    59. painter.setBrush(QColor(Qt::white));
    60. painter.setPen(Qt::NoPen);
    61. painter.drawRect(stxt);
    62.  
    63. QAbstractTextDocumentLayout::PaintContext CTX;
    64. painter.setClipRect(stxt);
    65. CTX.cursorPosition = -1;
    66. CTX.clip = stxt;
    67. doc->documentLayout()->draw(&painter,CTX);
    68. painter.end();
    69.  
    70.  
    71.  
    72. t2.setDocument(doc);
    73. t2.show();
    74.  
    75. QLabel la;
    76. la.setPicture (img);
    77. la.show();
    78.  
    79. return app.exec();
    80. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Aug 2008
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Paint QTextDocument splitted by pages 1/5 and edit txt

    Hi I compiled your fontLetterSpacing code (Using Qt 4.4.1 and msvc 2008) and it seems I got the same result as you. Check out the image attached to this message for the results.
    Attached Images Attached Images

  6. #6
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Paint QTextDocument splitted by pages 1/5 and edit txt

    Ok is a bug! QTextDocument drawing as image without fontLetterSpacing only qtextedit play correct.

  7. #7
    Join Date
    Jul 2015
    Posts
    22
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Paint QTextDocument splitted by pages 1/5 and edit txt

    patrik08, is there any complete source code nowadays for this editor to get familiar with?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.