PDA

View Full Version : QT4 DomXPath / DomElementContainer find tag.



patrik08
16th June 2007, 12:37
On QTextBrowser is not possibel to make a standard <a href=\"#\" target=\"xx\"> link...
target will remove after reload text ...!

So i search a iterate to handle this, my popup dialog link write a link so:
http://www.qtcentre.org/#target=_blank
After i rewrite this on php xml...

Now i found DomElementContainer from http://blog.wysota.eu.org/

i test so... und it can not find link... founf 0 . why wo i mistake?




const char *xml = "<test><tag>content 1</tag><tag>co<a href=\"#test\" target=\"xx\">ntent</a> 2</tag> "
"<othertag>wrongcontent</othertag>"
"<tag>content 3</tag></test>";

int main(){
QDomDocument doc;
doc.setContent(QString(xml));
DomElementContainer c(doc, "a");
int link = 0;
foreach(QDomElement e, c) {
link++;
}

qDebug() << "### total link found -> " << link;

return 0;
}


on php i rewrite so....





function Rewrite_QT_Tag($xhtml) {
/* rewrite not support target on QTextedit tag <a> */
/* samble : <a href="http://www.qtcentre.org/#target=_blank">
<span style=" text-decoration: underline; color:#0000ff;">QtCentre</span>
</a>*/
$dom = new XML();
$dom->loadXML($xhtml);
$xp = new DomXPath($dom);
$xp ->registerNamespace('default','http://www.w3.org/1999/xhtml');
$result = $xp->query("//default:a");
foreach ($result as $node) {
$attuale = $node->getAttribute("href");
/* get text from qt span and insert text node.... */
$testodentro ="";
while($node->hasChildNodes()) {
/* get span text inside <a><span>text link</span></a>*/
$testodentro .=$node->childNodes->item(0)->nodeValue;
$node->removeChild($node->childNodes->item(0));
}
$testodentro = eregi_replace("\n"," ",$testodentro);
$testo = $dom->createTextNode($testodentro);
$node->appendChild($testo);

/* rewrite target */
if (eregi("#target=",$attuale)) {
$params = explode("#target=", $attuale);
$urigo = $params[0];
$node->setAttribute("href",$urigo);
/**/
if (eregi("^(http|https)+(:\/\/)+[a-z0-9_-]+\.+[a-z0-9_-]", $urigo )) {
$node->setAttribute("target","_blank");
$node->setAttribute("class","Link_External");
} else if (eregi('mailto:',$urigo)) {
$node->setAttribute("class","Link_Mail");
} else {
$node->setAttribute("target",$params[1]);
$node->setAttribute("class","Link_Internal");
}
/**/
} else if (eregi("^(http|https)+(:\/\/)+[a-z0-9_-]+\.+[a-z0-9_-]", $attuale )) {

$node->setAttribute("target","_blank");
$node->setAttribute("class","Link_External");
} else {
continue;
}
}
/* end link rewrite */
$xhtml = $dom->saveXML();
return $xhtml;
}

wysota
18th June 2007, 17:35
On QTextBrowser is not possibel to make a standard <a href=\"#\" target=\"xx\"> link...
target will remove after reload text ...!

I think you must have done something wrong. Qt Assistant is based around QTextBrowser and it uses anchors for method and property names and that seems to work fine...

Is this code you pasted in your post somehow relevant to the question?

BTW. The container doesn't work for you, because it iterates direct children of the element which it was given and in the code you pasted "a" is a child of "tag", so you'd have to do:

foreach(QDomElement elem, DomElementContainer(doc, "tag"))
foreach(QDomElement subelem, DomElementContainer(elem, "a"))
links++;

patrik08
18th June 2007, 23:53
I think you must have done something wrong. Qt Assistant is based around QTextBrowser and it uses anchors for method and property names and that seems to work fine...


Yes on ready document read only its work ... My problem i have 3 case on QTextBrowser
View modus / Edit modus Wysiwyg / Source edit modus /



BTW. The container doesn't work for you, because it iterates direct children of the element which it was given and in the code you pasted "a" is a child of "tag", so you'd have to do:

foreach(QDomElement elem, DomElementContainer(doc, "tag"))
foreach(QDomElement subelem, DomElementContainer(elem, "a"))
links++;

Is here a way to make this container Xpath similar?
like:
/body/p[0]/a/span

wysota
19th June 2007, 00:01
Yes on ready document read only its work ... My problem i have 3 case on QTextBrowser
View modus / Edit modus Wysiwyg / Source edit modus /
Still I don't see a problem. Especially given a fact that QTextBrowser is read-only, so you're surely not using it for modes other than "view".


Is here a way to make this container Xpath similar?
like:
/body/p[0]/a/span

If you implement an XPath parser, then I don't see a problem. I don't need such complicated things. My most common use of XML trees is iterating over subtrees, so such a simple container is enough.

patrik08
19th June 2007, 00:21
Still I don't see a problem. Especially given a fact that QTextBrowser is read-only, so you're surely not using it for modes other than "view".


QTextBrowser -> QTextEdit
WysiwygText->setReadOnly(false);

People edit text and view
http://www.qt-apps.org/content/show.php/XHTML+Wysiwyg+Qeditor?content=59493

and work..



void Base_Edit::ModusView( bool src )
{
if (src) {
tabWidget->setCurrentIndex(1); /* QTextBrowser view + edit */
} else {
tabWidget->setCurrentIndex(0); /* QTextEdit source edit */
}
}

wysota
19th June 2007, 00:51
Mmmm.... Write more clearly you should. Understand you I do not...

patrik08
19th June 2007, 01:03
Mmmm.... Write more clearly you should. Understand you I do not...

Why You say "QTextBrowser is read-only"!?

setReadOnly(false); and the gui edit text ... table image like frontpage from MS$

I am the first one that I write them within (QTextBrowser)?

IMO: steep by steep i learn en_GB or US? after I find courage to write in the wiki without
cin->getline(This article or section needs a rewrite!.)

wysota
19th June 2007, 01:20
Why You say "QTextBrowser is read-only"!?
Because it is read only... That's why it's called a browser and not an editor. Quoting the docs:

This class extends QTextEdit (in read-only mode), adding some navigation functionality so that users can follow links in hypertext documents.


I am the first one that I write them within (QTextBrowser)?
Could be. Others have read the docs :)


IMO: steep by steep i learn en_GB or US?
Hmmm... What? :) Oh... you mean that step by step you learn to speak English? Maybe... I don't know. I think people (me too) are sometimes having trouble understanding what you say and according to me your main problem is that you don't use full sentences but instead you use arrows (->), dots (...), etc. This doesn't really help us (at least me) understand you. If you used full sentences, even ones containing mistakes, it would be easier to guess what you mean.


after I find courage to write in the wiki without
cin->getline(This article or section needs a rewrite!.)
A suggestion for a rewrite may be caused not only by the language but also by the contents which may be unclear or seem irrelevant to the rest of the text.

patrik08
19th June 2007, 12:16
Because it is read only... That's why it's called a browser and not an editor.
Could be. Others have read the docs :)


Me to and i found only one to display html page + link css image and open link on firefox . :rolleyes:

How you projekt a Wysiwyg xhtml editor without QTextBrowser ? :)
To make a preview inside you need this QTextBrowser.
QTextEdit dont dispay href link and open it. Boring event maybe? and make clickable.
Otherwise you must open a new window to display and test links...

Now i found a way to emulate XPath ....

Now the problem is to contruct a new QDomNodeList and append note to return on public code...

QDomNodeList not having append(node) !



#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <QtCore>
#include <QDomDocument>

class List_Tag
{
public:
List_Tag( QDomDocument doc , const QString FindTagName , const QString AttributeName )
{
t = FindTagName;
a = AttributeName;

std::cout << "Start Job search tag ->" << qPrintable(FindTagName) << std::endl;
QDomNode n = doc.firstChild();
if (n.hasChildNodes()) {
Grep_Tag(n);
}
}
protected:
QString t;
QString a;
QString value;
QString tagname;
QDomNode node;
private:
void Grep_Tag( QDomNode n )
{
QDomNodeList list = n.childNodes();
for (int i = 0; i < list.length(); ++i) {
node = list.item(i);
tagname = node.nodeName();
std::cout << "Found tag loop->" << qPrintable(tagname) << std::endl;
if (tagname == t && node.hasAttributes()) {
value = node.toElement().attributeNode(a).value();
if (!value.isEmpty()) {
std::cout << "Result->" << qPrintable(tagname) << " attribute->" << qPrintable(value) << std::endl;
}
} else if (tagname == t && a.isEmpty()) {
std::cout << "Result->" << qPrintable(tagname) << " and Empty attribute." << std::endl;
}
if (node.hasChildNodes()) {
Grep_Tag(list.item(i));
}
}
}

};

int main(int argc, char *argv[]) {
QCoreApplication a( argc, argv );

const char *xml = "<test><tag>content 1</tag><tag>content 2</tag> "
"<othertag>wrongcontent<p>text line <a href=\"/somelink\">link1</a></p></othertag>"
"<tag>content 3 <div><p>text other <a href=\"/otherlink\">link2</a></p></div></tag></test>";
QDomDocument doc;
bool ok = doc.setContent(QString(xml));
if (ok) {
List_Tag links(doc,"a","href");
List_Tag paras(doc,"p","");
}
std::cout << "Main end..................." << std::endl;
return a.exec();
}

////////#include "main.moc"

wysota
19th June 2007, 12:29
Me to and i found only one to display html page + link css image and open link on firefox . :rolleyes:
Sorry, I don't understand what you mean.


How you projekt a Wysiwyg xhtml editor without QTextBrowser ? :)
To make a preview inside you need this QTextBrowser.
QTextEdit dont dispay href link and open it. Boring event maybe? and make clickable.
Otherwise you must open a new window to display and test links...
I'd probably use QGraphicsView instead of QTextBrowser. If you want a wysiwyg editor, you have to be able to manipulate objects and QGraphicsView seems perfect for that.


Now the problem is to contruct a new QDomNodeList and append note to return on public code...

Your code is very cluttered with unnecessary debugging output. You should remove such lines before posting them here.

From what I understand you wish to emulate "getElementsByName", right? Why not use QDomElement::elementsByTagName( const QString & tagname ) then?

patrik08
19th June 2007, 13:09
I'd probably use QGraphicsView instead of QTextBrowser. If you want a wysiwyg editor, you have to be able to manipulate objects and QGraphicsView seems perfect for that.


Exact , fortunately you understand me.

QGraphicsView , this is my weakness i not found enought sample or demo , wiki or so , to learn this. :o

My target is to construct a editor like Scribus , only on xml way. At end by using relax NG its can write KDE docbook file or xml , and all xml semantic. Only swap the relax file, and other format can write.


My actual XHTML editor to home page CMS is Bitflux editor ,
http://cvsdemo.bitfluxeditor.org/examples/inlineXHTML/index.html test it
run only on Firefox and other Gecko Browser..
The big advantage from this XML editor is:
1 - http://en.wikipedia.org/wiki/RELAX_NG Relax NG ( http://www.w3.org/TR/xhtml2/xhtml20_relax.html )
2 - I can set a 100% corporate identity from clients and the person editor can not mistake page why i set a schema to hadle this. If i say param is blue and red, only this color can write.
3 - Write all xml format rss and more like apache fop pdf excel latex ( at end only xslt) http://bitfluxeditor.org/

Example relax ng shema to xhtml:
http://cvsdemo.bitfluxeditor.org/examples/inlineXHTML/relaxng.xml

Have you QGraphicsView sample or how much i must pay for that?
I understand only draw on paintevent and pixmap.

wysota
19th June 2007, 15:23
Have you QGraphicsView sample or how much i must pay for that?
I understand only draw on paintevent and pixmap.

Have you tried the example graphics view applications that come with Qt?

patrik08
20th June 2007, 00:06
Have you tried the example graphics view applications that come with Qt?


Yes i look on example/diagramscene on particular,

QGraphicsTextItem , and how i can paste external QMimeData text/html and image included... ? inside a textitem...

wysota
20th June 2007, 00:09
the text item has a QTextDocument associated with it.

patrik08
20th June 2007, 00:38
the text item has a QTextDocument associated with it.

QTextDocument the same as QTextEdit :( high mountain from span tag.

I like to make my Own xml schema on relax NG:
( http://cvsdemo.bitfluxeditor.org/examples/inlineXHTML/schema/xhtml/xhtml-strict.rng.xml )

to transform xslt to other format fop,xhtml,docbook,latex,rss,scribus,ecc..
Only change the relax ng file and i write on other format.. by past i accept only allowed tag, or transform tidy && xslt.

<para/> xslt <p/>
<bold/> xslt <b/>
<italic/> xslt <i/>
<textblock/> xslt <div/ style="x:y">
<section/> xslt <div/> && xslt <section/>

Is this possibel? wo i must beginn to subclass?

wysota
20th June 2007, 05:17
Start with stopping treating QTextDocument as HTML.

patrik08
20th June 2007, 10:45
Start with stopping treating QTextDocument as HTML.

Cann you explain more exact? "treating" http://dictionary.reference.com/ dont have it.. :(

Doc say...

http://doc.trolltech.com/4.0/qabstracttextdocumentlayout.html


The QAbstractTextDocumentLayout class is an abstract base class used to implement custom layouts for QTextDocuments. More...

#include <QAbstractTextDocumentLayout>

My question is here on QAbstractTextDocumentLayout if i can manipulate tag and reimplement as xml other tag:

example xml from scribus....

http://tuxmas.lug-bk.de/tuxmas.sla


An here comming xml return...




QString QTextDocument::toHtml ( const QByteArray & encoding = QByteArray() ) const
Returns a string containing an HTML representation of the document.
The encoding parameter specifies the value for the charset attribute in the html header. For example if 'utf-8' is specified then the beginning of the generated html will look like this:
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body>...
If no encoding is specified then no such meta information is generated.
If you later on convert the returned html string into a byte array for transmission over a network or when saving to disk you should specify the encoding you're going to use for the conversion to a byte array here.
See also Supported HTML Subset.




Or better write a :

QString QTextDocument::toNewXML ( const QByteArray & encoding = QByteArray() ) const

a total new one ..? an append the 100% relax ng schema <grammar define ....

If QTextDocument can transform to xml format from scribus all my problem is solved!
and on xslt can make docbook latex word pdf openoffice the scribus format can handle forms coordinate color rgb or cyan... so much choise.

The html paragraph tag dont return point X,Y or height x width

wysota
23rd June 2007, 13:06
I meant to start treating QTextDocument as a set of blocks of blocks of blocks of.... formatted text and not as html. Stop reading html tags and dig into the API of the framework.

patrik08
24th June 2007, 11:00
I meant to start treating QTextDocument as a set of blocks of blocks of blocks of.... formatted text and not as html. Stop reading html tags and dig into the API of the framework.

Imo: Ok.. now i have more time; i move a subclass from 100 ip to a new provider .. a battle to become correct TTL running and bandswitch.

QTextBlock is only on /src/gui/text/ qtextobject.cpp && qtextobject.cpp or is here more place?
the are only objekt to transform on what I want?
On this case i must only subclass QTextDocument and append my import/export tag , and begin document
void QGraphicsTextItem::setDocument ( QTextDocument * document )

ist this mistake or correct?

wysota
24th June 2007, 11:48
Read a bit about the whole framework: http://doc.trolltech.com/latest/richtext.html

patrik08
24th June 2007, 12:23
Read a bit about the whole framework: http://doc.trolltech.com/latest/richtext.html

"Rich Text Processing" this is a chapter wo not exist on Mr. Jasmin Blanchette Book German book!
Only two line about QTextBrowser same case on QMimeData.
I try to digest it in English :eek: tanks. if the are not sample or wiki .

Practical & theory would have to be in balance.