/*! Returns the area of the largest element at position (\a x,\a y) that is no larger than \a maxWidth by \a maxHeight pixels. May return an area larger in the case when no smaller element is at the position. */ QRect QDeclarativeWebView::elementAreaAt(int x, int y, int maxWidth, int maxHeight) const { QWebHitTestResult hit = page()->mainFrame()->hitTestContent(QPoint(x, y)); QRect hitRect = hit.boundingRect(); QWebElement element = hit.enclosingBlockElement(); if (maxWidth <= 0) maxWidth = INT_MAX; if (maxHeight <= 0) maxHeight = INT_MAX; while (!element.parent().isNull() && element.geometry().width() <= maxWidth && element.geometry().height() <= maxHeight) { hitRect = element.geometry(); element = element.parent(); } return hitRect; }
QList<CachedHandler> ChromeDOM::getCachedHandlers(const QString &elementId, const QRectF & ownerArea) { QWebElement snippet = getElementById(elementId); QList <QWebElement> controls = snippet.findAll(".GinebraCached").toList(); QList <CachedHandler> handlers; for (int i = 0; i < controls.size(); i++){ QWebElement elem = controls.at(i); //Element rectangle relative to snippet, so we can handle mouse events relative to snippet //qDebug() << "====> Owner X: " << ownerArea.x() << " Owner Width: " << ownerArea.width() << " Elem X: " << elem.geometry().x() << " Elem Width: " << elem.geometry().width(); QRectF elemRect(elem.geometry().x() - ownerArea.x(), elem.geometry().y() - ownerArea.y(), elem.geometry().width(), elem.geometry().height()); //NB: For now we handle only onclick from cache. Should add at least long-press too. CachedHandler handler(elem.attribute("id"), elem.attribute("data-GinebraOnClick"), elemRect, m_chrome, elem.attribute("data-GinebraTargetView")); //qDebug() << "Cached handler" << handler.elementId() << ": " << handler.script() << ": " << handler.rect(); handlers.append(handler); } return handlers; }
QRectF WebContentAnimationItem::findZoomableRectForPoint(const QPointF& point) { QPointF zoomPoint = m_webView->mapFromParent(point); QWebHitTestResult hitResult = m_webView->page()->mainFrame()->hitTestContent(zoomPoint.toPoint()); QWebElement targetElement = hitResult.enclosingBlockElement(); while (!targetElement.isNull() && targetElement.geometry().width() < MinDoubleClickZoomTargetWidth) targetElement = targetElement.parent(); if (!targetElement.isNull()) { QRectF elementRect = targetElement.geometry(); qreal overMinWidth = elementRect.width() - ZoomableContentMinWidth; if (overMinWidth < 0) elementRect.adjust(overMinWidth / 2, 0, -overMinWidth / 2, 0); zoomPoint.setX(elementRect.x()); QRectF resultRect(zoomPoint, elementRect.size()); return QRectF(m_webView->mapToParent(resultRect.topLeft()), m_webView->mapToParent(resultRect.bottomRight())); } return QRectF(); }
ChromeSnippet *ChromeDOM::getSnippet(const QString &docElementId, QGraphicsItem* parent) { Q_UNUSED(parent) ChromeSnippet * snippet = 0; QWebElement doc = m_page->mainFrame()->documentElement(); QWebElement element = doc.findFirst("#" + docElementId); QRect rect = element.geometry(); //TODO: This may not be accurate since final heights may not have been computed at this point!! m_height += rect.height(); // qDebug() << "Snippet: ID: " << docElementId << " Owner Area: " << rect << " Element Rect: " << element.geometry(); if (!rect.isNull()) { QString className = element.attribute("data-GinebraNativeClass", "__NO_CLASS__"); if (className == "__NO_CLASS__") { if (element.attribute("data-GinebraContainer", "false") == "true") { snippet = new WebChromeContainerSnippet(docElementId, m_chrome, element); snippet->setChromeWidget(new ChromeItem(snippet)); } else { snippet = new WebChromeSnippet(docElementId, m_chrome, element); m_renderer->addRenderItem((static_cast<WebChromeSnippet*> (snippet))->item()); } } else { snippet = nativeSnippetForClassName(className, docElementId, element); //TODO: Is the following still needed? QGraphicsWidget * widget = snippet->widget(); //Have snippet determine its own size when in anchor layout. Again, these will not //necessarily be accurate at this point. widget->resize(rect.width(), rect.height()); widget->setPreferredSize(rect.width(), rect.height()); widget->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred)); //Have snippet determine its own location when NOT in anchor layout widget->setPos(rect.x(), rect.y()); } QWebElement parentElem; if (!(parentElem = findChromeParent(element)).isNull()) { snippet->setParentId(parentElem.attribute("id")); } //Set auto-layout attributes snippet->setAnchor(element.attribute("data-GinebraAnchor", "AnchorNone"), false); snippet->setAnchorOffset(element.attribute("data-GinebraAnchorOffset", "0").toInt()); snippet->setInitiallyVisible(element.attribute("data-GinebraVisible", "false") == "true"); snippet->setHidesContent(element.attribute("data-GinebraHidesContent", "false") == "true"); } return snippet; }
void XPathInspector::setActiveElement(const QWebElement & elem, bool select) { if (elem.isNull()) return; selectElements(false); //if (!activeElement.isNull()) // activeElement.setStyleProperty("border", activeOldStyle); selectedElements.clear(); selectedElements.append(elem); activeElement = elem; QString path = PhpBrowser::getxpath(elem); //activeOldStyle = activeElement.styleProperty("border", QWebElement::ComputedStyle); //activeElement.setStyleProperty("border", "2px dashed red"); selectElements(true); QString attrsStr = ""; QStringList attrs = activeElement.attributeNames(); for (int i=0; i<attrs.size(); i++) { attrsStr += attrs.at(i) + " = \"" + elem.attribute(attrs.at(i)) +"\"\n"; } attrsStr += "rect: x:"+QString::number(elem.geometry().topLeft().x())+", y:"+QString::number(elem.geometry().topLeft().y())+ ", w:"+QString::number(elem.geometry().width())+", h:"+QString::number(elem.geometry().height())+"\n"; edit->setText(path); edit2->setPlainText(attrsStr); if (select) { deselectTreeItems(); findTreeItemAndSelect(elem); } }
void AlertWebApp::updateContentRect() { QWebFrame* frame = page()->page()->mainFrame(); QWebElement el = frame->findFirstElement("[x-palm-popup-content]"); QRect r; if (!el.isNull()) { r = el.geometry(); r.setLeft(MAX(0, r.left())); r.setRight(MIN(r.right(), (int) m_windowWidth)); r.setTop(MAX(0, r.top())); r.setBottom(MIN(r.bottom(), (int) m_windowHeight)); } m_channel->sendAsyncMessage(new ViewHost_Alert_SetContentRect(routingId(), r.left(), r.right(), r.top(), r.bottom())); }
bool PhpWebView::findElementByCoord(QWebElement & root, QPoint & point, QList<WebElementStruct> & list) { QWebElement child = root.lastChild(); bool isfind = false; while (!child.isNull()) { if (findElementByCoord(child, point, list)) isfind = true; child = child.previousSibling(); } QString display = root.styleProperty("display", QWebElement::ComputedStyle); if (display != "none" && root.geometry().contains(point) && !isfind) { list.append(createWebElementStruct(root, list.count())); return true; } else return false; }
extern QVariantMap toMap(QWebElement el, QStringList css_attrs) { QVariantMap map; map["isNull"] = false; map["classes"] = QVariant(el.classes()); map["tagName"] = QVariant(el.tagName()); QRect rect = el.geometry(); QVariantMap geo; geo["width"] = rect.width(); geo["height"] = rect.height(); geo["x"] = rect.x(); geo["y"] = rect.y(); map["geometry"] = QVariant(geo); QVariantMap attrs; QStringList attributes = el.attributeNames(); foreach(QString name,attributes) { attrs[name] = el.attribute(name); }
void QgsMapTip::showMapTip( QgsMapLayer *pLayer, QgsPoint & mapPosition, QPoint & thePixelPosition, QgsMapCanvas *pMapCanvas ) { // Do the search using the active layer and the preferred label field for the // layer. The label field must be defined in the layer configuration // file/database. The code required to do this is similar to identify, except // we only want the first qualifying feature and we will only display the // field defined as the label field in the layer configuration file/database // Show the maptip on the canvas QString tipText, lastTipText, tipHtml, bodyStyle, containerStyle, backgroundColor, borderColor; delete mWidget; mWidget = new QWidget( pMapCanvas ); mWebView = new QgsWebView( mWidget ); #if WITH_QTWEBKIT mWebView->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );//Handle link clicks by yourself mWebView->setContextMenuPolicy( Qt::NoContextMenu ); //No context menu is allowed if you don't need it connect( mWebView, SIGNAL( linkClicked( QUrl ) ), this, SLOT( onLinkClicked( QUrl ) ) ); #endif mWebView->page()->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true ); mWebView->page()->settings()->setAttribute( QWebSettings::JavascriptEnabled, true ); QHBoxLayout* layout = new QHBoxLayout; layout->addWidget( mWebView ); mWidget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); mWidget->setLayout( layout ); //assure the map tip is never larger than half the map canvas const int MAX_WIDTH = pMapCanvas->geometry().width() / 2; const int MAX_HEIGHT = pMapCanvas->geometry().height() / 2; mWidget->setMaximumSize( MAX_WIDTH, MAX_HEIGHT ); // start with 0 size, // the content will automatically make it grow up to MaximumSize mWidget->resize( 0, 0 ); backgroundColor = mWidget->palette().base().color().name(); borderColor = mWidget->palette().shadow().color().name(); mWidget->setStyleSheet( QString( ".QWidget{" "border: 1px solid %1;" "background-color: %2;}" ).arg( borderColor, backgroundColor ) ); tipText = fetchFeature( pLayer, mapPosition, pMapCanvas ); mMapTipVisible = !tipText.isEmpty(); if ( !mMapTipVisible ) { clear(); return; } if ( tipText == lastTipText ) { return; } bodyStyle = QString( "background-color: %1;" "margin: 0;" ).arg( backgroundColor ); containerStyle = QString( "display: inline-block;" "margin: 0px" ); tipHtml = QString( "<html>" "<body style='%1'>" "<div id='QgsWebViewContainer' style='%2'>%3</div>" "</body>" "</html>" ).arg( bodyStyle, containerStyle, tipText ); mWidget->move( thePixelPosition.x(), thePixelPosition.y() ); mWebView->setHtml( tipHtml ); lastTipText = tipText; mWidget->show(); #if WITH_QTWEBKIT int scrollbarWidth = mWebView->page()->mainFrame()->scrollBarGeometry( Qt::Vertical ).width(); int scrollbarHeight = mWebView->page()->mainFrame()->scrollBarGeometry( Qt::Horizontal ).height(); if ( scrollbarWidth > 0 || scrollbarHeight > 0 ) { // Get the content size QWebElement container = mWebView->page()->mainFrame()->findFirstElement( QStringLiteral( "#QgsWebViewContainer" ) ); int width = container.geometry().width() + 5 + scrollbarWidth; int height = container.geometry().height() + 5 + scrollbarHeight; mWidget->resize( width, height ); } #endif }
int PhpWebView::click(const QString & xpath, bool samewnd) { QWebElement elem = getElementByXPath(QString(xpath)); if (elem.isNull()) return 0; elem.setFocus(); if (samewnd) elem.removeAttribute("target"); QString js = "var node = this; var x = node.offsetLeft; var y = node.offsetTop; "; js += "var w = node.offsetWidth/2; "; js += "var h = node.offsetHeight/2; "; js += "while (node.offsetParent != null) { "; js += " node = node.offsetParent; "; js += " x += node.offsetLeft; "; js += " y += node.offsetTop; "; js += "} "; js += "[x+w, y+h]; "; QList<QVariant> vlist = elem.evaluateJavaScript(js).toList(); QPoint point; point.setX(vlist.at(0).toInt()); point.setY(vlist.at(1).toInt()); QRect elGeom = elem.geometry(); QPoint elPoint = elGeom.center(); int elX = point.x(); //elPoint.x(); int elY = point.y(); //elPoint.y(); int webWidth = width(); int webHeight = height(); int pixelsToScrollRight=0; int pixelsToScrollDown=0; if (elX > webWidth) pixelsToScrollRight = elX-webWidth+elGeom.width()/2+50; //the +10 part if for the page to scroll a bit further if (elY > webHeight) pixelsToScrollDown = elY-webHeight+elGeom.height()/2+50; //the +10 part if for the page to scroll a bit further /*pixelsToScrollRight = elX-elGeom.width()/2-50; pixelsToScrollDown = elY-elGeom.height()/2-50; if (pixelsToScrollRight < 0) pixelsToScrollRight = 0; if (pixelsToScrollRight > page()->mainFrame()->scrollBarMaximum(Qt::Horizontal)) pixelsToScrollRight = page()->mainFrame()->scrollBarMaximum(Qt::Horizontal); if (pixelsToScrollDown < 0) pixelsToScrollDown = 0; if (pixelsToScrollDown > page()->mainFrame()->scrollBarMaximum(Qt::Vertical)) pixelsToScrollDown = page()->mainFrame()->scrollBarMaximum(Qt::Vertical);*/ int oldHoriz = page()->mainFrame()->scrollBarValue(Qt::Horizontal); int oldVert = page()->mainFrame()->scrollBarValue(Qt::Vertical); page()->mainFrame()->setScrollBarValue(Qt::Horizontal, pixelsToScrollRight); page()->mainFrame()->setScrollBarValue(Qt::Vertical, pixelsToScrollDown); QPoint pointToClick(elX-pixelsToScrollRight, elY-pixelsToScrollDown); QEventLoop loop; isNewViewCreated = false; isNewViewBegin = false; //QMouseEvent *pressEvent = new QMouseEvent(QMouseEvent::MouseButtonPress, pointToClick, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); //QApplication::postEvent(browser, pressEvent); //QApplication::processEvents(); //QMouseEvent releaseEvent(QMouseEvent::MouseButtonRelease,pointToClick,Qt::LeftButton,Qt::LeftButton,Qt::NoModifier); //QApplication::sendEvent(browser, &releaseEvent); QString js2 = "var e = document.createEvent('MouseEvents');"; //js2 += "e.initEvent( 'click', true, true );"; //js2 += "e.initMouseEvent('click', true, true, window, 0, 0, 0, "+QString::number(elX)+", "+QString::number(elY)+", false, false, false, false, 0, null);"; js2 += "e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);"; js2 += "this.dispatchEvent(e);"; elem.evaluateJavaScript(js2); QTimer::singleShot(1000, &loop, SLOT(quit())); loop.exec(); while (browser && isNewViewBegin && !isNewViewCreated) loop.processEvents(); //page()->mainFrame()->setScrollBarValue(Qt::Horizontal, oldHoriz); //page()->mainFrame()->setScrollBarValue(Qt::Vertical, oldVert); return 1; }