Double click selects words

BUGS: 187347
FIXED-IN: 4.9,0
This commit is contained in:
Albert Astals Cid 2012-03-25 23:48:07 +02:00
parent 592c0c1611
commit 9d805df013
5 changed files with 120 additions and 1 deletions

View file

@ -217,6 +217,14 @@ bool Page::hasTextPage() const
return d->m_text != 0;
}
RegularAreaRect * Page::wordAt( const NormalizedPoint &p, QString *word ) const
{
if ( d->m_text )
return d->m_text->wordAt( p, word );
return 0;
}
RegularAreaRect * Page::textArea ( TextSelection * selection ) const
{
if ( d->m_text )

View file

@ -204,6 +204,14 @@ class OKULAR_EXPORT Page
* @since 0.14 (KDE 4.8)
*/
TextEntity::List words( const RegularAreaRect * rect, TextPage::TextAreaInclusionBehaviour b ) const;
/**
* Returns the area and text of the word at the given point
* Note that ownership of the returned area belongs to the caller.
* @see TextPage::wordAt()
* @since 0.15 (KDE 4.9)
*/
RegularAreaRect * wordAt( const NormalizedPoint &p, QString *word = 0 ) const;
/**
* Returns the rectangular area of the given @p selection.

View file

@ -1958,3 +1958,84 @@ TextEntity::List TextPage::words(const RegularAreaRect *area, TextAreaInclusionB
}
return ret;
}
RegularAreaRect * TextPage::wordAt( const NormalizedPoint &p, QString *word ) const
{
TextList::ConstIterator itBegin = d->m_words.constBegin(), itEnd = d->m_words.constEnd();
TextList::ConstIterator it = itBegin;
TextList::ConstIterator posIt = itEnd;
for ( ; it != itEnd; ++it )
{
if ( (*it)->area.contains( p.x, p.y ) )
{
posIt = it;
break;
}
}
QString text;
if ( posIt != itEnd )
{
if ( (*posIt)->text().simplified().isEmpty() )
{
return NULL;
}
// Find the first TinyTextEntity of the word
while ( posIt != itBegin )
{
--posIt;
const QString itText = (*posIt)->text();
if ( itText.right(1).at(0).isSpace() )
{
if (itText.endsWith("-\n"))
{
// Is an hyphenated word
// continue searching the start of the word back
continue;
}
if (itText == "\n" && posIt != itBegin )
{
--posIt;
if ((*posIt)->text().endsWith("-")) {
// Is an hyphenated word
// continue searching the start of the word back
continue;
}
++posIt;
}
++posIt;
break;
}
}
RegularAreaRect *ret = new RegularAreaRect();
for ( ; posIt != itEnd; ++posIt )
{
const QString itText = (*posIt)->text();
if ( itText.simplified().isEmpty() )
{
break;
}
ret->appendShape( (*posIt)->area );
text += (*posIt)->text();
if (itText.right(1).at(0).isSpace())
{
if (!text.endsWith("-\n"))
{
break;
}
}
}
if (word)
{
*word = text;
}
return ret;
}
else
{
return NULL;
}
}

View file

@ -20,6 +20,7 @@ class QMatrix;
namespace Okular {
class NormalizedPoint;
class NormalizedRect;
class Page;
class PagePrivate;
@ -171,6 +172,13 @@ class OKULAR_EXPORT TextPage
*/
TextEntity::List words( const RegularAreaRect * rect, TextAreaInclusionBehaviour b ) const;
/**
* Returns the area and text of the word at the given point
* Note that ownership of the returned area belongs to the caller.
* @since 0.15 (KDE 4.9)
*/
RegularAreaRect * wordAt( const NormalizedPoint &p, QString *word = 0 ) const;
/**
* Returns the rectangular area of the given @p selection.
*/

View file

@ -2803,9 +2803,23 @@ void PageView::mouseDoubleClickEvent( QMouseEvent * e )
if ( pageItem )
{
// find out normalized mouse coords inside current item
const QRect & itemRect = pageItem->uncroppedGeometry();
double nX = pageItem->absToPageX(eventPos.x());
double nY = pageItem->absToPageY(eventPos.y());
if ( Okular::Settings::mouseMode() == Okular::Settings::EnumMouseMode::TextSelect ) {
textSelectionClear();
Okular::RegularAreaRect *wordRect = pageItem->page()->wordAt( Okular::NormalizedPoint( nX, nY ) );
if ( wordRect )
{
// TODO words with hyphens across pages
d->document->setPageTextSelection( pageItem->pageNumber(), wordRect, palette().color( QPalette::Active, QPalette::Highlight ) );
d->pagesWithTextSelection << pageItem->pageNumber();
return;
}
}
const QRect & itemRect = pageItem->uncroppedGeometry();
Okular::Annotation * ann = 0;
const Okular::ObjectRect * orect = pageItem->page()->objectRect( Okular::ObjectRect::OAnnotation, nX, nY, itemRect.width(), itemRect.height() );
if ( orect )