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);
    }
}
Exemplo n.º 2
0
void csWebView::SendToCGLX()
{
    //printf("Hello at x: %d y: %d\n", hit_pos.x(),  hit_pos.y());
    QWebFrame *tframe=page()->currentFrame ();
    QWebHitTestResult	hitresult = tframe->hitTestContent ( hit_pos );
    if(!hitresult.isNull())
    {
        QPixmap img = hitresult.pixmap();
        if(!img.isNull())
        {
            //printf("Width: %d Height: %d Link: %s\n",img.width(), img.height(),hitresult.imageUrl().toString().toStdString().c_str());
            emit send_image(img.width(), img.height() , hitresult.imageUrl());
        }
    }
}
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);
    }
}
Exemplo n.º 4
0
	void CustomWebView::contextMenuEvent (QGraphicsSceneContextMenuEvent *e)
	{
		QPointer<QMenu> menu (new QMenu ());
		QWebHitTestResult r = page ()->
			mainFrame ()->hitTestContent (e->pos ().toPoint ());

		IHookProxy_ptr proxy (new Util::DefaultHookProxy ());

		emit hookWebViewContextMenu (proxy, this, e, r,
				menu, WVSStart);

		if (!r.linkUrl ().isEmpty ())
		{
			QUrl url = r.linkUrl ();
			QString text = r.linkText ();

			if (XmlSettingsManager::Instance ()->
					property ("TryToDetectRSSLinks").toBool ())
			{
				bool hasAtom = text.contains ("Atom");
				bool hasRSS = text.contains ("RSS");

				if (hasAtom || hasRSS)
				{
					LeechCraft::Entity e;
					if (hasAtom)
					{
						e.Additional_ ["UserVisibleName"] = "Atom";
						e.Mime_ = "application/atom+xml";
					}
					else
					{
						e.Additional_ ["UserVisibleName"] = "RSS";
						e.Mime_ = "application/rss+xml";
					}

					e.Entity_ = url;
					e.Parameters_ = LeechCraft::FromUserInitiated |
						LeechCraft::OnlyHandle;

					bool ch = false;
					emit couldHandle (e, &ch);
					if (ch)
					{
						QList<QVariant> datalist;
						datalist << url
							<< e.Mime_;
						menu->addAction (tr ("Subscribe"),
								this,
								SLOT (subscribeToLink ()))->setData (datalist);
						menu->addSeparator ();
					}
				}
			}

			menu->addAction (tr ("Open &here"),
					this, SLOT (openLinkHere ()))->setData (url);
			menu->addAction (tr ("Open in new &tab"),
					this, SLOT (openLinkInNewTab ()))->setData (url);
			menu->addSeparator ();
			menu->addAction (tr ("&Save link..."),
					this, SLOT (saveLink ()));

			QList<QVariant> datalist;
			datalist << url
				<< text;
			menu->addAction (tr ("&Bookmark link..."),
					this, SLOT (bookmarkLink ()))->setData (datalist);

			menu->addSeparator ();
			if (!page ()->selectedText ().isEmpty ())
				menu->addAction (pageAction (QWebPage::Copy));
			menu->addAction (tr ("&Copy link"),
					this, SLOT (copyLink ()));
			if (page ()->settings ()->testAttribute (QWebSettings::DeveloperExtrasEnabled))
				menu->addAction (pageAction (QWebPage::InspectElement));
		}
		else if (page ()->selectedText ().contains (UrlInText))
		{
			menu->addAction (tr ("Open as link"),
					this, SLOT (openLinkInNewTab ()))->
							setData (page ()->selectedText ());
		}

		emit hookWebViewContextMenu (proxy, this, e, r,
				menu, WVSAfterLink);

		if (!r.imageUrl ().isEmpty ())
		{
			if (!menu->isEmpty ())
				menu->addSeparator ();
			menu->addAction (tr ("Open image here"),
					this, SLOT (openImageHere ()))->setData (r.imageUrl ());
			menu->addAction (tr ("Open image in new tab"),
					this, SLOT (openImageInNewTab ()));
			menu->addSeparator ();
			menu->addAction (tr ("Save image..."),
					this, SLOT (saveImage ()));

			QAction *spx = menu->addAction (tr ("Save pixmap..."),
					this, SLOT (savePixmap ()));
			spx->setToolTip (tr ("Saves the rendered pixmap without redownloading."));
			spx->setProperty ("Poshuku/OrigPX", r.pixmap ());
			spx->setProperty ("Poshuku/OrigURL", r.imageUrl ());

			menu->addAction (tr ("Copy image"),
					this, SLOT (copyImage ()));
			menu->addAction (tr ("Copy image location"),
					this, SLOT (copyImageLocation ()))->setData (r.imageUrl ());
		}

		emit hookWebViewContextMenu (proxy, this, e, r,
				menu, WVSAfterImage);

		bool hasSelected = !page ()->selectedText ().isEmpty ();
		if (hasSelected)
		{
			if (!menu->isEmpty ())
				menu->addSeparator ();
			menu->addAction (pageAction (QWebPage::Copy));
		}

		if (r.isContentEditable ())
			menu->addAction (pageAction (QWebPage::Paste));

		if (hasSelected)
		{
			Browser_->Find_->setData (page ()->selectedText ());
			menu->addAction (Browser_->Find_);
			menu->addAction (tr ("Search..."),
					this, SLOT (searchSelectedText ()));
		}

		emit hookWebViewContextMenu (proxy, this, e, r,
				menu, WVSAfterSelectedText);

		if (menu->isEmpty ())
			menu = page ()->createStandardContextMenu ();

		if (!menu->isEmpty ())
			menu->addSeparator ();

		menu->addAction (Browser_->Add2Favorites_);
		menu->addSeparator ();
		menu->addAction (Browser_->Print_);
		menu->addAction (Browser_->PrintPreview_);
		menu->addSeparator ();
		menu->addAction (Browser_->ViewSources_);
		menu->addAction (Browser_->SavePage_);
		menu->addSeparator ();
		menu->addAction (pageAction (QWebPage::ReloadAndBypassCache));
		menu->addAction (Browser_->ReloadPeriodically_);
		menu->addAction (Browser_->NotifyWhenFinished_);
		menu->addSeparator ();
		menu->addAction (Browser_->ChangeEncoding_->menuAction ());

		emit hookWebViewContextMenu (proxy, this, e, r,
				menu, WVSAfterFinish);

		if (!menu->isEmpty ())
			menu->exec (Browser_->GetGraphicsView ()->viewport ()->
					mapToGlobal (e->pos ().toPoint ()));
		else
			QGraphicsWebView::contextMenuEvent (e);

		if (menu)
			delete menu;
	}