Commit 5bcd5ea4 authored by Enrico Ros's avatar Enrico Ros
Browse files

Quick implementation of text selection (note: text is printed to stderr):

- KPDFPage: added a method to retrieve the text that is inside a rect from
    the embedded TextPage.
- PageWidget: (GFX) added methods to set the 1st (placed on click) and 2nd
    (placed on drag) corners of the selection area.
- PageView: A) set first corner on click, B) second on moves, C) retrieve
    text inside the rectangle on release.

svn path=/branches/kpdf_experiments/kdegraphics/kpdf/; revision=352751
parent e058e608
......@@ -55,7 +55,15 @@ bool KPDFPage::hasPixmap( int id, int width, int height ) const
bool KPDFPage::hasSearchPage() const
{
return (m_text != 0);
return ( m_text != 0 );
}
QString KPDFPage::getTextInRect( const QRect & rect ) const
{
if ( !m_text )
return QString();
GString * text = m_text->getText( rect.left(), rect.top(), rect.right(), rect.bottom() );
return QString( text->getCString() );
}
bool KPDFPage::hasLink( int mouseX, int mouseY ) const
......
......@@ -44,6 +44,7 @@ public:
bool isBookmarked() const { return m_bookmarking; }
bool hasPixmap( int id, int width, int height ) const;
bool hasSearchPage() const;
QString getTextInRect( const QRect & rect ) const;
bool hasLink( int mouseX, int mouseY ) const;
const KPDFLink * getLink( int mouseX, int mouseY ) const;
void drawPixmap( int id, QPainter * p, const QRect & rect, int width, int height ) const;
......
......@@ -56,6 +56,7 @@ public:
QPoint mouseGrabPos;
QPoint mouseStartPos;
bool mouseOnLink;
PageWidget * mouseSelectionWidget;
// other stuff
QTimer *delayTimer;
......@@ -97,6 +98,7 @@ PageView::PageView( QWidget *parent, KPDFDocument *document )
d->zoomFactor = 1.0;
d->mouseMode = MouseNormal;
d->mouseOnLink = false;
d->mouseSelectionWidget = 0;
d->delayTimer = 0;
d->scrollTimer = 0;
d->scrollIncrement = 0;
......@@ -292,7 +294,19 @@ void PageView::contentsMousePressEvent( QMouseEvent * e )
emit rightClick();
break;
case MouseSelection: // ? set 1st corner of the selection rect ?
case MouseSelection: // set first corner of the selection rect
if ( leftButton )
{
if ( d->mouseSelectionWidget )
d->mouseSelectionWidget->clearSelection();
d->mouseSelectionWidget = 0;
PageWidget * page = pickPageOnPoint( e->x(), e->y() );
if ( page )
{
page->setBeginCorner( e->x() - childX( page ), e->y() - childY( page ) );
d->mouseSelectionWidget = page;
}
}
break;
case MouseEdit: // ? place the beginning of [tool] ?
......@@ -300,6 +314,7 @@ void PageView::contentsMousePressEvent( QMouseEvent * e )
}
}
#include <kdebug.h>
void PageView::contentsMouseReleaseEvent( QMouseEvent * e )
{
bool leftButton = e->button() & LeftButton,
......@@ -356,7 +371,14 @@ void PageView::contentsMouseReleaseEvent( QMouseEvent * e )
}
break;
case MouseSelection: // ? d->page->setPixmapOverlaySelection( QRect ) ?
case MouseSelection: // get text from the page
if ( leftButton && d->mouseSelectionWidget )
{
kdWarning() << d->mouseSelectionWidget->selectedText() << endl;
d->mouseSelectionWidget->clearSelection();
d->mouseSelectionWidget = 0;
}
break;
case MouseEdit: // ? apply [tool] ?
break;
......@@ -390,7 +412,13 @@ void PageView::contentsMouseMoveEvent( QMouseEvent * e )
}
break;
case MouseSelection: // ? update selection contour ?
case MouseSelection: // set selection's second corner
if ( leftButton && d->mouseSelectionWidget )
// continue selecting on current page
d->mouseSelectionWidget->setEndCorner(
e->x() - childX( d->mouseSelectionWidget ),
e->y() - childY( d->mouseSelectionWidget ) );
break;
case MouseEdit: // ? update graphics ?
break;
......
......@@ -131,12 +131,49 @@ void ThumbnailWidget::paintEvent( QPaintEvent * e )
/** PageWidget **/
PageWidget::PageWidget( QWidget *parent, const KPDFPage *page )
: PixmapWidget( parent, page )
: PixmapWidget( parent, page ), m_selBeginX( -1 ), m_selBeginY( -1 )
{
// keep bottom equal to right margin
setPixmapMargins( 1, 1, 4, 4 );
}
void PageWidget::clearSelection()
{
if ( !m_selectionRect.isNull() )
update( m_selectionRect );
m_selBeginX = -1;
m_selBeginY = -1;
m_selectionRect = QRect();
}
void PageWidget::setBeginCorner( int x, int y )
{
if ( !m_selectionRect.isNull() )
update( m_selectionRect );
m_selBeginX = x;
m_selBeginY = y;
m_selectionRect.setCoords( x, y, 1, 1 );
update( m_selectionRect );
}
void PageWidget::setEndCorner( int x, int y )
{
QRect newRect( m_selBeginX, m_selBeginY, x - m_selBeginX, y - m_selBeginY );
newRect = newRect.normalize().intersect( QRect( m_marginLeft, m_marginTop, m_pixmapWidth, m_pixmapHeight ) );
if ( newRect != m_selectionRect )
{
if ( !m_selectionRect.isNull() )
update( m_selectionRect );
m_selectionRect = newRect;
update( m_selectionRect );
}
}
QString PageWidget::selectedText() const
{
return m_page->getTextInRect( m_selectionRect );
}
void PageWidget::paintEvent( QPaintEvent * e )
{
QRect clip = e->rect();
......@@ -183,5 +220,13 @@ void PageWidget::paintEvent( QPaintEvent * e )
#endif
p.translate( -m_marginLeft, -m_marginTop );
// draw selection rect (uses opaque selection)
if ( !m_selectionRect.isNull() )
{
p.setPen( palette().active().highlight().dark(110) );
//COOL p.setBrush( QBrush( palette().active().highlight(), Qt::Dense4Pattern ) );
p.drawRect( m_selectionRect );
}
p.end();
}
......@@ -12,6 +12,7 @@
#define _PIXMAPWIDGET_H_
#include <qwidget.h>
#include <qrect.h>
class KPDFPage;
......@@ -73,7 +74,7 @@ private:
/**
* @short PageView's editable page. Renders page, outline and overlay GFX.
* @short PageView's editable page. Renders page, selection and overlay GFX.
* ...
*/
class PageWidget : public PixmapWidget
......@@ -81,8 +82,18 @@ class PageWidget : public PixmapWidget
public:
PageWidget( QWidget * parent, const KPDFPage * page );
void clearSelection();
void setBeginCorner( int x, int y );
void setEndCorner( int x, int y );
QString selectedText() const;
protected:
void paintEvent(QPaintEvent *);
private:
int m_selBeginX;
int m_selBeginY;
QRect m_selectionRect;
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment