PDA

View Full Version : QTextBlockFormat to QTextImageFormat problem on QTextDocument



patrik08
13th July 2007, 23:45
How i can center a image on QTextDocument? / set aligment? set border?

if i use toImageFormat() from QTextBlockFormat the image is not visible... why?

can i subclass QTextImageFormat and paint differents..? is this possibel?





/* http://www.zvon.org/xxl/xslfoReference/Output/index.html */
bool FopFormat::ImageParserTag( const QDomElement &e )
{
QPixmap pixm;
qreal wi = ConvertUnit( e.attribute( "width" ));
qreal hi = ConvertUnit( e.attribute( "height" ));
bool having = false;
QTextBlockFormat pf;

/* search special alignment on fo:external-graphic */
QDomNamedNodeMap attlist = e.attributes();
for (int i=0; i<attlist.count(); i++){
QDomNode nod = attlist.item(i);
if (nod.nodeName().toLower() == "text-align") {
if (nod.nodeValue().toLower() == "center" ) {
pf.setAlignment( Qt::AlignCenter );
} else if (nod.nodeValue().toLower() == "left" || nod.nodeValue().toLower() == "start" || nod.nodeValue().toLower() == "inside" ) {
pf.setAlignment( Qt::AlignLeft );
} else if (nod.nodeValue().toLower() == "right" || nod.nodeValue().toLower() == "end" ) {
pf.setAlignment( Qt::AlignRight );
} else if (nod.nodeValue().toLower() == "justify") {
pf.setAlignment( Qt::AlignJustify );
} else if (nod.nodeValue().toLower() == "inherit") {
/* align same as parent before */
pf.setAlignment( Qt::AlignAbsolute );
}
}
}

QTextImageFormat format; /////////// = pf.toImageFormat(); /* destroy imageformat */

const QString href = e.attribute( "src" );
if (!href.endsWith( ".svg" )) {
QFile file(href);
if (file.exists()) {
pixm.load(href);
QFileInfo fi(href);
Tdoc->addResource( QTextDocument::ImageResource, QUrl(href), QPixmap(QString(":%1").arg(fi.baseName())) );
having = true;
}
}
if (having) {
format.setAnchor(true);
format.setAnchorHref ( href );
if (wi > 0) {
format.setWidth( wi );
} else {
format.setWidth( pixm.width() );
}
if (hi > 0) {
format.setHeight( hi );
} else {
format.setHeight( pixm.height() );
}
format.setName( href );
format.setToolTip(QString("Image %1 %2x%3").arg(href).arg(wi).arg(hi));
Tcursor->insertImage( format );
}
return having;
}