PDA

View Full Version : Highlight text issue



ejoshva
27th March 2015, 11:17
I am trying to highlight the selected text. Finding issue, when the same word(s) found before that line when FINDTEXT() is used
Since trigger action is set as START_OF_DOCUMENT

Kindly suggest an alternative to highlight text on the webview.

The code for the highlight text using find text is as below



void webview::findtextpos(QString txt, QString paraid)
{
page()->triggerAction(QWebPage::MoveToStartOfDocument, true);
page()->setContentEditable(true);
//qDebug() << txt;
this->findText(txt);

QVariant tmpVar = this->page()->inputMethodQuery(Qt::ImMicroFocus);
qDebug() << tmpVar;
QRect tmpRect = tmpVar.toRect();
QPoint pt;
pt.setX(tmpRect.x());
pt.setY(tmpRect.y());
QWebHitTestResult hitRes = page()->mainFrame()->hitTestContent(pt);
QWebElement htmlElement = hitRes.enclosingBlockElement();
QString id = htmlElement.attribute("id");
if(id == paraid)
{
QColor m_Color;
m_Color.setNamedColor("yellow");
QString clrName = m_Color.name();
QLatin1String cmd = QLatin1String("HiliteColor");
QString js = QString(QLatin1String("document.execCommand(\"%1\",null, \"%2\")")).arg(cmd).arg(clrName);
QVariant va = page()->mainFrame()->evaluateJavaScript(js);
page()->triggerAction(QWebPage::MoveToNextChar, true);
QVariant tmpVar = page()->inputMethodQuery(Qt::ImMicroFocus);
QRect tmpRect = tmpVar.toRect();
QPoint pt;
pt.setX(tmpRect.x());
pt.setY(tmpRect.y());
textpos = pt;
return;
}

else
{
while(page()->MoveToEndOfDocument)
{
page()->triggerAction(QWebPage::MoveToNextChar, true);
findtextpos(txt,paraid);
}
}

page()->setContentEditable(false);

}

void webview::applyhighlight(QString str, QString color)
{

hglist.clear();
// hglist.append(ldbook->getBookTitle());
findText(str);
QColor m_Color;
m_Color.setNamedColor(color);
QString clrName = m_Color.name();
QLatin1String cmd = QLatin1String("HiliteColor");
QString js = QString(QLatin1String("document.execCommand(\"%1\",null, \"%2\")")).arg(cmd).arg(clrName);
QVariant va = page()->mainFrame()->evaluateJavaScript(js);
QVariant tmpVar = page()->inputMethodQuery(Qt::ImMicroFocus);
QRect tmpRect = tmpVar.toRect();
QPoint pt;
pt.setX(tmpRect.x());
pt.setY(tmpRect.y());
page()->inputMethodQuery(Qt::ImMicroFocus).toRect().center ();
QWebHitTestResult hitRes = page()->mainFrame()->hitTestContent(pt);
QWebElement htmlElement = hitRes.enclosingBlockElement();
QString id = htmlElement.attribute("id");
QStringList pno = id.split("\_");
if(hglist.isEmpty())
hglist<<str;

hglist.append(htmlElement.attribute("id"));
hglist.append(color);
hglist.append("");
hglist.append("");
hglist.append(htmlElement.toOuterXml());
storedbhighlights();
page()->triggerAction(QWebPage::MoveToNextChar);

}



Added after 1 36 minutes:

Issue cropping up here is that say for example the word "THE" is highlighted in the 5 the line. But this word is also found in all the other previous 4 lines and multiple times as well. Now I am not able to identify the indented text to highlight.
I don't have para id as well.
How do I do this?

<Just an idea> Is it possible to get the index of the word from the start of the page. So that I can't again find this index and restore the highlight.

wysota
27th March 2015, 22:12
Sorry, what is the question and how is the code snippet related to it?

ejoshva
28th March 2015, 01:57
I want to highlight the selected word on the page displayed in QGraphicsWebView.

Currently I am doing the highlight using findText().
But the issue I am facing is that if there is more than one occurrence of the particular word, only first occurrence is highlighted and not the intended.
For example : I am highlighting "1940". In that particular 1940 is found 4 times. I intend to highlight the 3rd occurrence. But only the 1st occurrence is highlighted since before findtext(), page()->triggerAction(Qt::MoveToStartOfDocument).
When I restore the highlight after navigating to next page and coming back also finding the same issue.

I just got a plain idea as to loop through whole page and find all occurrence of selected word using findText() and match the current mouseReleasePoint and match against the location of each occurrence and highlight at the exact matching location.
Not sure how to implement this. Since I am not able to find the location of each occurrence of the word.

rough coding


QPoint webview::findTextInPage(QString searchText, QPoint hitMatchPoint, QString color)
{
QPoint pt;
page()->setContentEditable(true);
page()->triggerAction(QWebPage::MoveToStartOfDocument);

while (findText(searchText))
{
QVariant tmpVar = page()->inputMethodQuery(Qt::ImMicroFocus);
QRect tmpRect = tmpVar.toRect();

pt.setX(tmpRect.x());
pt.setY(tmpRect.y());

if( pt == hitMatchPoint)
{
QColor m_Color;
m_Color.setNamedColor(color);
QString clrName = m_Color.name();
QLatin1String cmd = QLatin1String("HiliteColor");
QString js = QString(QLatin1String("document.execCommand(\"%1\",null, \"%2\")")).arg(cmd).arg(clrName);
QVariant va = page()->mainFrame()->evaluateJavaScript(js);
}

qDebug()<<"hitMatchPoint : "<<hitMatchPoint;
qDebug()<<" Impact Point : "<<pt;
}
page()->setContentEditable(false);
return pt;
}

wysota
28th March 2015, 06:15
I want to highlight the selected word on the page displayed in QGraphicsWebView.

Why don't you use QGraphicsWebView::findText()? It has a flag to highlight all findings.

ejoshva
30th March 2015, 06:13
I am using that only. But when it encounters any regular expression like /[^\n]* ?=()
it's not getting highlighted.

Eg : when this below is selected for highlight, findText doesn't work.
372–5

Eg : when below 3 lines are selected to be highlighted, highlight doesn't work

As you work through this topic, look for information that will help you to answer these questions:
How did societies change during the period between ancient and early modern times?
How did beliefs and values influence societies in this period?


Kindly let me how to include all the regular expression in the findtext()

jefftee
30th March 2015, 06:21
Is your regular expression a typo? Doesn't look like a valid regular expression to me and certainly won't match any of the 3 lines you listed.

wysota
30th March 2015, 06:23
I don't think findText supports regular expressions.

ejoshva
30th March 2015, 06:52
If findText() doesn't support, is there any other function which I can make use of?


Is your regular expression a typo? Doesn't look like a valid regular expression to me and certainly won't match any of the 3 lines you listed.
What I meant was, if any of those in the regex appears in the selected text then the findText doesn't work

jefftee
30th March 2015, 19:45
I was hung up on the fact that your regex didn't seem valid to me but as wysota pointed out, it doesn't matter because findText doesn't support QRegExp or QRegularExpression.

I am not aware of an alternative unfortunately, sorry!

ejoshva
31st March 2015, 05:02
For highlight, found something as QSyntaxHighlighter. Checking on it now.
http://doc.qt.io/qt-5/qtwidgets-richtext-syntaxhighlighter-example.html

wysota
31st March 2015, 06:51
QSyntaxHighlighter will not work with a web view.

ejoshva
31st March 2015, 07:27
:( . Then any other alternative

ejoshva
26th May 2015, 07:04
Still couldn't find any solution for this highlight word issue :(