[webkit-qt] [webkit-help] How to get the layout infomation use qtwebkit?
achellies
achellies at 163.com
Wed Feb 23 17:00:48 PST 2011
The following codes is used to get the position for each DOM node.
Document* doc = frame()->document();
if (doc && doc->firstChild()) {
Vector<WebCore::IntRect> leafNodesRect;
walkThroughRecrusionNode(leafNodesRect, doc->firstChild());
}
void WebView::getLeafNodeRect(Vector<WebCore::IntRect>& leafNodesRect, WebCore::Node* node)
{
if (node && node->renderer()) {
WebCore::IntRect baseRect = node->getRect();
if (node->renderer() && node->renderer()->enclosingBox())
baseRect = node->renderer()->enclosingBox()->absoluteContentBox();
if (node->renderer()->isText()
|| node->renderer()->isTextArea()
|| node->renderer()->isTextField()
|| node->renderer()->isRubyText()
#if defined(ENABLE_SVG) && ENABLE_SVG
|| node->renderer()->isSVGText()
|| node->renderer()->isSVGInlineText()
#endif
) {
node->renderer()->absoluteRects(leafNodesRect, baseRect.x(), baseRect.y());
}
else if (node->renderer()->isImage()
|| node->renderer()->isVideo()
|| node->renderer()->isTable()
#if defined(ENABLE_SVG) && ENABLE_SVG
|| node->renderer()->isSVGPath()
|| node->renderer()->isSVGImage()
#endif
){
leafNodesRect.append(baseRect);
}
else
leafNodesRect.append(baseRect);
}
return;
}
void WebView::walkThroughRecrusionNode(Vector<WebCore::IntRect>& leafNodesRect, WebCore::Node* node)
{
if (node) {
for (; node; node = node->nextSibling()) {
if (node->hasChildNodes()) {
if (node->renderer() && node->renderer()->isImage()) {
WebCore::IntRect parentRect = node->getRect();
leafNodesRect.append(parentRect);
} else {
walkThroughRecrusionNode(leafNodesRect, node->firstChild());
}
} else {
getLeafNodeRect(leafNodesRect, node);
}
}
}
return;
}
在 2011-02-23,"周鹏" <zhoupeng19 at 126.com> 写道:
webkit-qt,你好!
Hi guys!
I want to get the page's layout infomation.eg:if there is a image in the page,i want to know the coordinates of the image in the page,the background color, for text,i want to know it's font,size,and it's coordinates...
here is my code:
//traversal a dom tree
void traversal(QWebElement node)
{
...
QRect rect= node.geometry();
printf("Start: X-%d,Y-%d End: X-%d,Y-%d\n", ss.topLeft().x(), ss.topLeft().y(), ss.bottomRight().x(), ss.bottomRight().y() );
...
}
int main(int argc, char *argv[])
{
...
QString content(htmlSource);
QUrl url("file:///home/rmss/zp/");
QApplication a(argc, argv);
QWebView *view = new QWebView;
QWebSettings * settings = QWebSettings::globalSettings();
//settings->setAttribute(QWebSettings::AutoLoadImages, false);
settings->setAttribute(QWebSettings::JavascriptEnabled, false);
settings->setAttribute(QWebSettings::JavaEnabled, false);
settings->setAttribute(QWebSettings::PluginsEnabled, false);
settings->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
settings->setDefaultTextEncoding("GB2312");
view->setHtml(content, url);
view->show();
QWebPage* page = view->page();
QWebFrame* frame = page->mainFrame();
QString str = frame->renderTreeDump();
QWebElement document = frame->documentElement();
QWebElement body = document.findFirst("body");
if(body .isNull())
{
printf("failed!\n");
return -1;
}
QString info = eee.styleProperty("background-color", QWebElement::ComputedStyle);//get background color
traversal(body );
return 0;
}
I use "geometry" to get the location of every node,but it's not work correctly,and this program can't process page which use external css. And please tell me how to get the font,size of text.I have not much time to read the qtwebkit code because my boss want me to fininsh this job in two days. So. please tell me how to solove these problem,and if possible,give me some demo.
Apologize for my poor english!
周鹏
zhoupeng19 at 126.com
2011-02-23
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-qt/attachments/20110224/4a1bad61/attachment.html>
More information about the webkit-qt
mailing list