PDA

View Full Version : Paint QTextDocument splitted by pages 1/5 and edit txt



patrik08
18th July 2008, 22:21
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...




/* M_PageSize DDPAGE; = container
from margin and multi page size */
void BaseText::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
const QRectF rectalldoc = boundingRect(); /* all page rect call to recalc pagetotal */
const int PageS = DocPageSumme;
QTextDocument *_d = document();
for (int i = 0; i < PageS; ++i) {
painter->save();
const QRectF innrect = DDPAGE.PageInternal(i); /* rect to place one page */
painter->translate(innrect.topLeft().x(), innrect.topLeft().y() - (i * DDPAGE.hight()) );
_d->drawContents(painter,QRectF(0, i * DDPAGE.hight() ,_d->size().rwidth(), DDPAGE.hight() ));
painter->restore();
}
}

patrik08
30th July 2008, 17:23
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...




void ScribePage::paint(QPainter * painter ,
const QStyleOptionGraphicsItem *option , QWidget *widget )
{
/* QTextDocument = _d */
PageTotal = _d->pageCount();
const int PageSumm = qBound (1,_d->pageCount(),MaximumPages);
QTextFrame *Tframe = _d->rootFrame();
root_format = Tframe->frameFormat();
const QRectF ActiveBlock = CurrentBlockRect();
/* discovery qtextcursor living Current_Page_Nr */

painter->save();
painter->setPen( Qt::NoPen );
painter->setBrush(Qt::lightGray);
painter->drawRect(boundingRect());
painter->restore();
for (int i = 0; i < PageSumm; ++i) {
const QPointF topleft = PageIndexTopLeft(i); /* page top left point */
DrawPage(i,painter,i); /* active page from cursor !!
selectAll must draw cursor each page*/
}

}

void ScribePage::DrawPage( const int index ,
QPainter * painter , const int cursorpage )
{
const QPointF topleft = PageIndexTopLeft(index);
QAbstractTextDocumentLayout::PaintContext CTX;
CTX.palette.setColor(QPalette::Text, Qt::black);
const QRectF body = QRectF(0, topleft.y() ,Page_Edit_Rect.width(),
Page_Edit_Rect.height()); /* on view */
QRectF view(0, index * body.height(), body.width(), body.height() ); /* on doc */

if (index != cursorpage || !Edit_On()) {
painter->save();
painter->translate(body.left(), body.top() - index * body.height());
painter->setClipRect(view);
CTX.clip = view;
_d->documentLayout()->draw(painter,CTX);
painter->restore();
return;
}

/* draw cursor active page no !edit no print only display edit! */
painter->save();
painter->translate(body.left(), body.top() - index * body.height());
painter->setClipRect(view);
CTX.clip = view;
QColor BackHightlight("#0072ab");
BackHightlight.setAlpha(180);
CTX.palette.setColor(QPalette::Text, Qt::black);
CTX.palette.setColor(QPalette::Highlight,BackHight light);
CTX.palette.setColor(QPalette::HighlightedText,Qt: :white);
CTX.selections;
CTX.clip = view;
CTX.cursorPosition = -1;
/* play cursor */

if (cursortime) { /* blink intervall by timer */
CTX.cursorPosition = C_cursor.position();
}
if ( C_cursor.hasSelection()) {
QAbstractTextDocumentLayout::Selection Internal_selection;
Internal_selection.cursor = C_cursor;

QPalette::ColorGroup cg = cursorIsFocusIndicator ? QPalette::Active : QPalette::Inactive;
Internal_selection.format.setBackground(CTX.palett e.brush(cg, QPalette::Highlight));
Internal_selection.format.setForeground(CTX.palett e.brush(cg, QPalette::HighlightedText));
Internal_selection.format.setProperty(QTextFormat: :FullWidthSelection, true);
CTX.selections.append(Internal_selection);

}
_d->documentLayout()->draw(painter,CTX);
painter->restore();
}


Results ....

http://ppk.ciz.ch/MiniScribus/sceeenBeginMiniscribus2.png

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

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?

patrik08
1st August 2008, 09:28
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...



bool TextProcessor::StartDragOperation()
{
DragFill = false;
if (!C_cursor.hasSelection()) {
return DragFill;
}
QMimeData *data = createMimeDataFromSelection();
if (data) {
QApplication::clipboard()->setMimeData(data);
QDrag *drag = new QDrag(Gwi); /* QWidget *Gwi; from event */
drag->setMimeData(data);
drag->setHotSpot(QPoint(-25,-25));
/* try to make a QPixmap from mime html fragment or image drag drop */
const QPixmap playdragicon = ImagefromMime(data);
if (!playdragicon.isNull()) {
drag->setPixmap(playdragicon);
}
if (drag->exec(Qt::CopyAction |
Qt::MoveAction,
Qt::CopyAction) == Qt::MoveAction) {
DragFill = true;
emit q_startDrag(PointPositionOnDoc); /* pos from events */
}
}
return DragFill;
}

QMimeData *TextProcessor::createMimeDataFromSelection()
{
QTextCharFormat base = C_cursor.charFormat();
QString txt;

if (C_cursor.hasSelection()) {
txt = C_cursor.selectedText();
}

QTextImageFormat pico = base.toImageFormat();
if (pico.isValid()) {
QVariant xx = pico.property(_IMAGE_PICS_ITEM_);
if (!xx.isNull()) {
SPics pic = xx.value<SPics>();
QList<SPics> li;
li.append(pic);
QString Sdd = SaveImageGroup(li);
QMimeData *mimeData = new QMimeData;
mimeData->setData("application/x-picslists",Sdd.toUtf8());
return mimeData;
}
}

const QTextDocumentFragment fragment(C_cursor);
if ( fragment.isEmpty() && txt.size() > 0 ) {
QMimeData *xm = new QMimeData();
xm->setText(txt);
return xm;
} else if (fragment.isEmpty() && txt.isEmpty() ) {
QMimeData *xm = new QMimeData();
xm->setText(QString("...Selection...Error..."));
return xm;
} else {
return new QTextEditMimeData(fragment);
/////QTextDocumentFragment::fromHtml(md->html())
}
}

patrik08
12th August 2008, 13:58
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





#include <QtGui>

int main( int argc, char ** argv )
{
QApplication app( argc, argv );

QTextDocument d("This is some text at Page #Page# .");
QTextEdit t;
t.setDocument(&d);
t.selectAll();

const qreal spacingnow = t.textCursor().charFormat().fontLetterSpacing();
bool ok;
qreal space = QInputDialog::getDouble(0, QObject::tr("Font Letter Spacing"),
QObject::tr("Space:"),spacingnow,90,2000, 2, &ok);
if (space > 0 && ok) {
QTextCursor c = t.textCursor();
QTextCharFormat format = c.charFormat();
qDebug() << "This is some text at Page #Page# ." << space;
format.setFontLetterSpacing(space);
c.setCharFormat(format);
}

t.show();
QTextDocument * doc = d.clone();
for (QTextBlock srcBlock = d.firstBlock(), dstBlock = doc->firstBlock();
srcBlock.isValid() && dstBlock.isValid();
srcBlock = srcBlock.next(), dstBlock = dstBlock.next()) {
dstBlock.layout()->setAdditionalFormats(srcBlock.layout()->additionalFormats());
}

QTextCursor cu(doc);
const int PageNr = 1;

/* search _PAGE_NUMERATION_ #Page# and replace nr. */
qDebug() << "Replace #Page# as number " << PageNr << "only on clone doc";

QTextCursor bcu = doc->find("#Page#",cu,QTextDocument::FindWholeWords);
if (!bcu.isNull ()) {
if (bcu.hasSelection()) {
QTextCharFormat format = bcu.charFormat(); /* take from selection */
QString remtxt = bcu.selectedText();
for (int i = 0; i < remtxt.size(); ++i) {
bcu.deleteChar();
}
bcu.insertText(QString("%1").arg(PageNr),format);
bcu.clearSelection();
}
}


QTextFrame *Tframe = doc->rootFrame();
const QRectF stxt = doc->documentLayout()->frameBoundingRect(Tframe);
QPicture img;
img.setBoundingRect(stxt.toRect());

QPainter painter;
painter.begin(&img);
painter.setRenderHint(QPainter::TextAntialiasing);
painter.setBrush(QColor(Qt::white));
painter.setPen(Qt::NoPen);
painter.drawRect(stxt);

QAbstractTextDocumentLayout::PaintContext CTX;
painter.setClipRect(stxt);
CTX.cursorPosition = -1;
CTX.clip = stxt;
doc->documentLayout()->draw(&painter,CTX);
painter.end();



QTextEdit t2;
t2.setDocument(doc);
t2.show();

QLabel la;
la.setPicture (img);
la.show();

return app.exec();
}

netuno
12th August 2008, 15:35
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.

patrik08
12th August 2008, 19:55
Ok is a bug! QTextDocument drawing as image without fontLetterSpacing only qtextedit play correct.

Toniy
6th March 2017, 12:14
patrik08, is there any complete source code nowadays for this editor to get familiar with?