Ejemplo n.º 1
0
void WBWebTrapWebView::trapElementAtPos(const QPoint& pos)
{
    QWebHitTestResult htr = page()->currentFrame()->hitTestContent(pos);

    if (!htr.pixmap().isNull())
    {
        emit pixmapCaptured(htr.pixmap(), false);
    }
    else if (mCurrentContentType == ObjectOrEmbed)
    {
        QString embedSelector = QString("document.elementFromPoint(%1, %2)").arg(pos.x()).arg(pos.y());
        QVariant tagName = page()->currentFrame()->evaluateJavaScript(embedSelector + ".tagName");

        QVariant innerHTML = page()->currentFrame()->evaluateJavaScript(embedSelector + ".innerHTML");
        qDebug() << "innerHTML" << innerHTML;

        if (tagName.toString().toLower() == "object")
        {
            embedSelector = QString("document.elementFromPoint(%1, %2).getElementsByTagName('object')[0]")
                .arg(pos.x()).arg(pos.y());
        }

        QString querySource = embedSelector + ".src";
        QVariant resSource = page()->currentFrame()->evaluateJavaScript(querySource);
        qDebug() << "resSource" << resSource;
        QString source = resSource.toString();

        QString queryType = embedSelector + ".type";
        QVariant resType = page()->currentFrame()->evaluateJavaScript(queryType);
        QString type = resType.toString();
        qDebug() << "resType" << resType;

        if (source.trimmed().length() > 0)
        {
            emit objectCaptured(QUrl(page()->currentFrame()->url().toString() + "/" + source), type,
                    htr.boundingRect().width(), htr.boundingRect().height());

            UBApplication::boardController->downloadURL(QUrl(source), QPointF(0.0, 0.0));
            UBApplication::applicationController->showBoard();
        }
    }
    else if (mCurrentContentType == Input)
    {
        QString embedCode = potentialEmbedCodeAtPos(pos);

        if (embedCode.length() > 0)
        {
            emit embedCodeCaptured(embedCode);
        }
    }
    else if (mCurrentContentType == ElementByQuery)
    {
        webElementCaptured(page()->currentFrame()->url(), mElementQuery);
    }
}
Ejemplo n.º 2
0
/*!
    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;
}
Ejemplo n.º 3
0
void WBWebTrapWebView::highliteElementAtPos ( const QPoint& pos)
{
    mCurrentContentType = Unknown;

    if(page() && page()->currentFrame())
    {
        QWebHitTestResult htr = page()->currentFrame()->hitTestContent (pos);
        QRect pageHtr = htr.boundingRect().translated(htr.frame()->pos());

        QRect updateRect = mWebViewElementRect.united(pageHtr);
        updateRect = updateRect.adjusted(-8, -8, 8, 8);

        mDomElementRect = htr.boundingRect();

        if (!htr.pixmap().isNull())
        {
            mCurrentContentType = Image;
            qDebug() << "found pixmap at " << htr.boundingRect();
        }
        else
        {
            QWebElement element = htr.element();
            QString tagName = element.tagName().toLower();

            if (tagName == "object"
                || tagName == "embed")
            {
                mCurrentContentType = ObjectOrEmbed;
            }
            else if ((tagName == "input") || (tagName == "textarea"))
            {
                QString ec = potentialEmbedCodeAtPos(pos);

                if (ec.length() > 0)
                {
                    qDebug() << "found input data \n\n" << ec;
                    mCurrentContentType = Input;
                }
            }
            else
            {
                QString tagName = htr.element().tagName();
                QString id =  htr.element().attribute("id", "");
                QWebElement el = htr.element();

                QString idSelector = tagName + "#" + id;
                bool idSuccess = (el == el.document().findFirst(idSelector));

                if (idSuccess)
                {
                    mElementQuery = idSelector;
                    mCurrentContentType = ElementByQuery;
                }
                else
                {
                    //bool isValid = true;

                    QWebElement elParent = el.parent();
                    QWebElement currentEl = el;
                    QString path = tagName;
                    QStringList pathElements;

                    do
                    {
                        QWebElement someSibling = elParent.firstChild();

                        int index = 0;
                        bool foundIndex = false;

                        do
                        {
                            if (someSibling.tagName() == currentEl.tagName())
                            {
                                if (someSibling == currentEl)
                                {
                                    foundIndex = true;
                                }
                                else
                                    index++;
                            }

                            someSibling = someSibling.nextSibling();
                        }
                        while(!someSibling.isNull() && !foundIndex);

                        QString part;

                        if (index > 0)
                            part = QString("%1:nth-child(%2)").arg(currentEl.tagName()).arg(index);
                        else
                            part = currentEl.tagName();

                        pathElements.insert(0, part);

                        currentEl = elParent;
                        elParent = elParent.parent();

                    } while(!elParent.isNull());

                    //QString idSelector = tagName + "#" + id;
                    QString treeSelector =  pathElements.join(" > ");

                    mElementQuery = treeSelector;
                    mCurrentContentType = ElementByQuery;

                    //bool treeSuccess = (el == el.document().findFirst(treeSelector));

                    //qDebug() << "----------------------------";
                    //qDebug() << idSuccess << idSelector;
                    //qDebug() << treeSuccess << treeSelector;
                }
            }
        }

        update(updateRect);
    }
}