Commit 5b0c32e4 authored by Enrico Ros's avatar Enrico Ros

Misc fixes and cleanups and tidy up the code (KPDFPageRect -> ObjectRect).

svn path=/trunk/kdegraphics/kpdf/; revision=387779
parent 2a3f35ac
......@@ -663,7 +663,7 @@ void PDFGenerator::customEvent( QCustomEvent * event )
PixmapRequest * request = static_cast< PixmapRequest * >( event->data() );
QImage * outImage = generatorThread->takeImage();
TextPage * outTextPage = generatorThread->takeTextPage();
QValueList< KPDFPageRect * > outRects = generatorThread->takeRects();
QValueList< ObjectRect * > outRects = generatorThread->takeRects();
request->page->setPixmap( request->id, new QPixmap( *outImage ) );
delete outImage;
......@@ -694,7 +694,7 @@ struct PPGThreadPrivate
// internal temp stored items. don't delete this.
QImage * m_image;
TextPage * m_textPage;
QValueList< KPDFPageRect * > m_rects;
QValueList< ObjectRect * > m_rects;
bool m_rectsTaken;
};
......@@ -716,7 +716,7 @@ PDFPixmapGeneratorThread::~PDFPixmapGeneratorThread()
delete d->m_textPage;
if ( !d->m_rectsTaken && d->m_rects.count() )
{
QValueList< KPDFPageRect * >::iterator it = d->m_rects.begin(), end = d->m_rects.end();
QValueList< ObjectRect * >::iterator it = d->m_rects.begin(), end = d->m_rects.end();
for ( ; it != end; ++it )
delete *it;
}
......@@ -780,7 +780,7 @@ TextPage * PDFPixmapGeneratorThread::takeTextPage() const
return tp;
}
QValueList< KPDFPageRect * > PDFPixmapGeneratorThread::takeRects() const
QValueList< ObjectRect * > PDFPixmapGeneratorThread::takeRects() const
{
d->m_rectsTaken = true;
return d->m_rects;
......
......@@ -22,7 +22,8 @@
class PDFDoc;
class GList;
class TextPage;
class KPDFPageRect;
class ObjectRect;
class KPDFOutputDev;
class PDFPixmapGeneratorThread;
......@@ -118,7 +119,7 @@ class PDFPixmapGeneratorThread : public QThread
// methods for getting contents from the GUI thread
QImage * takeImage() const;
TextPage * takeTextPage() const;
QValueList< KPDFPageRect * > takeRects() const;
QValueList< ObjectRect * > takeRects() const;
private:
// can't be called from the outside (but from startGeneration)
......
......@@ -91,11 +91,11 @@ TextPage * KPDFOutputDev::takeTextPage()
return text;
}
QValueList< KPDFPageRect * > KPDFOutputDev::takeRects()
QValueList< ObjectRect * > KPDFOutputDev::takeRects()
{
if ( m_rects.isEmpty() )
return m_rects;
QValueList< KPDFPageRect * > rectsCopy( m_rects );
QValueList< ObjectRect * > rectsCopy( m_rects );
m_rects.clear();
return rectsCopy;
}
......@@ -168,10 +168,9 @@ void KPDFOutputDev::drawLink( Link * link, Catalog * catalog )
nt = (double)top / (double)m_pixmapHeight,
nr = (double)right / (double)m_pixmapWidth,
nb = (double)bottom / (double)m_pixmapHeight;
// create the rect using normalized coords
KPDFPageRect * rect = new KPDFPageRect( nl, nt, nr, nb );
// attach the link object to the rect and add it to the vector container
rect->setPointer( l, KPDFPageRect::Link );
// create the rect using normalized coords and attach the KPDFLink to it
ObjectRect * rect = new ObjectRect( nl, nt, nr, nb, ObjectRect::Link, l );
// add the ObjectRect to the vector container
m_rects.push_back( rect );
}
}
......@@ -229,11 +228,10 @@ void KPDFOutputDev::drawImage( GfxState *state, Object *ref, Stream *str,
nt = (double)top / (double)m_pixmapHeight,
nr = (double)(left + width) / (double)m_pixmapWidth,
nb = (double)(top + height) / (double)m_pixmapHeight;
// create the rect using normalized coords
KPDFPageRect * r = new KPDFPageRect( nl, nt, nr, nb );
r->setPointer( 0, KPDFPageRect::Image );
// add the rect to the vector container
m_rects.push_back( r );
// create the rect using normalized coords and set it of KPDFImage type
ObjectRect * rect = new ObjectRect( nl, nt, nr, nb, ObjectRect::Image, 0 );
// add the ObjectRect to the vector container
m_rects.push_back( rect );
}
}
SplashOutputDev::drawImage( state, ref, str, _width, _height, colorMap, maskColors, inlineImg );
......@@ -246,7 +244,7 @@ void KPDFOutputDev::clear()
// delete rects
if ( m_rects.count() )
{
QValueList< KPDFPageRect * >::iterator it = m_rects.begin(), end = m_rects.end();
QValueList< ObjectRect * >::iterator it = m_rects.begin(), end = m_rects.end();
for ( ; it != end; ++it )
delete *it;
m_rects.clear();
......
......@@ -25,8 +25,8 @@
class QPixmap;
class TextPage;
class KPDFPageRect;
class KPDFLink;
class ObjectRect;
class DocumentViewport;
/**
......@@ -56,7 +56,7 @@ class KPDFOutputDev : public SplashOutputDev
QPixmap * takePixmap();
QImage * takeImage();
TextPage * takeTextPage();
QValueList< KPDFPageRect * > takeRects();
QValueList< ObjectRect * > takeRects();
/** inherited from OutputDev */
// Start a page.
......@@ -95,7 +95,7 @@ class KPDFOutputDev : public SplashOutputDev
QPixmap * m_pixmap;
QImage * m_image;
TextPage * m_text; // text page generated on demand
QValueList< KPDFPageRect * > m_rects; // pageRects (links/images)
QValueList< ObjectRect * > m_rects; // objectRects (links/images)
};
......
......@@ -73,7 +73,7 @@ bool KPDFPage::hasRect( double x, double y ) const
{
if ( m_rects.count() < 1 )
return false;
QValueList< KPDFPageRect * >::const_iterator it = m_rects.begin(), end = m_rects.end();
QValueList< ObjectRect * >::const_iterator it = m_rects.begin(), end = m_rects.end();
for ( ; it != end; ++it )
if ( (*it)->contains( x, y ) )
return true;
......@@ -173,9 +173,9 @@ const QString KPDFPage::getText( const NormalizedRect & rect ) const
return result;
}
const KPDFPageRect * KPDFPage::getRect( double x, double y ) const
const ObjectRect * KPDFPage::getRect( double x, double y ) const
{
QValueList< KPDFPageRect * >::const_iterator it = m_rects.begin(), end = m_rects.end();
QValueList< ObjectRect * >::const_iterator it = m_rects.begin(), end = m_rects.end();
for ( ; it != end; ++it )
if ( (*it)->contains( x, y ) )
return *it;
......@@ -206,9 +206,9 @@ void KPDFPage::setBookmark( bool state )
m_bookmarked = state;
}
void KPDFPage::setRects( const QValueList< KPDFPageRect * > rects )
void KPDFPage::setRects( const QValueList< ObjectRect * > rects )
{
QValueList< KPDFPageRect * >::iterator it = m_rects.begin(), end = m_rects.end();
QValueList< ObjectRect * >::iterator it = m_rects.begin(), end = m_rects.end();
for ( ; it != end; ++it )
delete *it;
m_rects = rects;
......@@ -254,7 +254,7 @@ void KPDFPage::deletePixmapsAndRects()
delete *it;
m_pixmaps.clear();
// delete PageRects
QValueList< KPDFPageRect * >::iterator rIt = m_rects.begin(), rEnd = m_rects.end();
QValueList< ObjectRect * >::iterator rIt = m_rects.begin(), rEnd = m_rects.end();
for ( ; rIt != rEnd; ++rIt )
delete *rIt;
m_rects.clear();
......@@ -306,55 +306,32 @@ bool NormalizedRect::intersects( double l, double t, double r, double b ) const
return (l < right) && (r > left) && (t < bottom) && (b > top);
}
/** class KPDFPageRect **/
KPDFPageRect::KPDFPageRect( double l, double t, double r, double b )
// assign coordinates swapping them if negative width or height
: NormalizedRect( r > l ? l : r, b > t ? t : b, r > l ? r : l, b > t ? b : t ),
m_pointerType( NoPointer ), m_pointer( 0 )
{
}
KPDFPageRect::~KPDFPageRect()
{
deletePointer();
}
QRect KPDFPageRect::geometry( int width, int height ) const
QRect NormalizedRect::geometry( int xScale, int yScale ) const
{
int l = (int)( left * width ),
t = (int)( top * height ),
r = (int)( right * width ),
b = (int)( bottom * height );
return QRect( l, t, r - l, b - t );
int l = (int)( left * xScale ),
t = (int)( top * yScale ),
r = (int)( right * xScale ),
b = (int)( bottom * yScale );
return QRect( l, t, r - l + 1, b - t + 1 );
}
void KPDFPageRect::setPointer( void * object, enum PointerType pType )
{
deletePointer();
m_pointer = object;
m_pointerType = pType;
}
KPDFPageRect::PointerType KPDFPageRect::pointerType() const
{
return m_pointerType;
}
/** class ObjectRect **/
const void * KPDFPageRect::pointer() const
ObjectRect::ObjectRect( double l, double t, double r, double b, ObjectType type, void * pnt )
// assign coordinates swapping them if negative width or height
: NormalizedRect( r > l ? l : r, b > t ? t : b, r > l ? r : l, b > t ? b : t ),
m_objectType( type ), m_pointer( pnt )
{
return m_pointer;
}
void KPDFPageRect::deletePointer()
ObjectRect::~ObjectRect()
{
if ( !m_pointer )
return;
if ( m_pointerType == Link )
if ( m_objectType == Link )
delete static_cast<KPDFLink*>( m_pointer );
else
kdDebug() << "Object deletion not implemented for type '"
<< m_pointerType << "' ." << endl;
kdDebug() << "Object deletion not implemented for type '" << m_objectType << "' ." << endl;
}
......@@ -16,9 +16,9 @@
class QPixmap;
class QRect;
class TextPage;
class KPDFPageRect;
class KPDFPageTransition;
class NormalizedRect;
class ObjectRect;
class HighlightRect;
/**
......@@ -54,14 +54,14 @@ class KPDFPage
NormalizedRect * findText( const QString & text, bool keepCase, NormalizedRect * last = 0 ) const;
const QString getText( const NormalizedRect & rect ) const;
const KPDFPageRect * getRect( double x, double y ) const;
const ObjectRect * getRect( double x, double y ) const;
const KPDFPageTransition * getTransition() const;
// oprations: set/delete contents (by KPDFDocument)
void setPixmap( int p_id, QPixmap * pixmap );
void setSearchPage( TextPage * text );
void setBookmark( bool state );
void setRects( const QValueList< KPDFPageRect * > rects );
void setRects( const QValueList< ObjectRect * > rects );
void setHighlight( int s_id, NormalizedRect * &r, const QColor & color );
void setTransition( const KPDFPageTransition * transition );
void deletePixmap( int p_id );
......@@ -76,7 +76,7 @@ class KPDFPage
QMap< int, QPixmap * > m_pixmaps;
TextPage * m_text;
QValueList< KPDFPageRect * > m_rects;
QValueList< ObjectRect * > m_rects;
QValueList< HighlightRect * > m_highlights;
const KPDFPageTransition * m_transition;
};
......@@ -97,44 +97,36 @@ class NormalizedRect
bool contains( double x, double y ) const;
bool intersects( const NormalizedRect & normRect ) const;
bool intersects( double l, double t, double r, double b ) const;
QRect geometry( int xScale, int yScale ) const;
};
/**
* @short A rect on the page that may contain an object.
*
* This class describes a rect (geometrical coordinates) and may hold a
* pointer to an associated object. An object is reparented to this class
* and deleted when this class is deleted.
* @short NormalizedRect that contains a reference to an object.
*
* Objects are stored and read as 'void pointers' so you have to perform
* the cast on the code that handles the object using information provided
* by pointerType().
* These rects contains a pointer to a kpdf object (such as a link or something
* like that). The pointer is read and stored as 'void pointer' so cast is
* performed by accessors based on the value returned by objectType(). Objects
* are reparented to this class.
*
* Type / Class correspondency tab:
* - NoPointer : '' : no object is stored
* - Link : class KPDFLink : description of a link
* - Image : class KPDFImage : description of an image
* - Image : class KPDFImage : description of an image (n/a)
*/
class KPDFPageRect : public NormalizedRect
class ObjectRect : public NormalizedRect
{
public:
KPDFPageRect( double left, double top, double right, double bottom );
~KPDFPageRect();
// expand NormalizedRect to given width and height
QRect geometry( int width, int height ) const;
// set a pointer to data associated to this rect
enum PointerType { NoPointer, Link, Image };
void setPointer( void * object, enum PointerType pType );
enum ObjectType { Link, Image };
ObjectRect( double left, double top, double right, double bottom,
ObjectType t, void * object );
~ObjectRect();
// query type and get a const pointer to the stored object
PointerType pointerType() const;
const void * pointer() const;
inline ObjectType objectType() const { return m_objectType; }
inline const void * pointer() const { return m_pointer; }
private:
void deletePointer();
PointerType m_pointerType;
ObjectType m_objectType;
void * m_pointer;
};
......
......@@ -681,7 +681,7 @@ void Part::slotShowMenu(const KPDFPage *page, const QPoint &point)
}
/*
//Albert says: I have not ported this as i don't see it does anything
if ( d->mouseOnRect ) // and rect->pointerType() == KDPFPageRect::Image ...
if ( d->mouseOnRect ) // and rect->objectType() == ObjectRect::Image ...
{
m_popup->insertItem( SmallIcon("filesave"), i18n("Save Image..."), 4 );
m_popup->setItemEnabled( 4, false );
......
......@@ -19,7 +19,7 @@
#include "pagepainter.h"
#include "core/page.h"
#include "conf/settings.h"
#include <kdebug.h>
void PagePainter::paintPageOnPainter( const KPDFPage * page, int id, int flags,
QPainter * destPainter, const QRect & limits, int width, int height )
{
......@@ -74,9 +74,9 @@ void PagePainter::paintPageOnPainter( const KPDFPage * page, int id, int flags,
{
// precalc normalized 'limits rect' for intersection
double nXMin = (double)limits.left() / (double)width,
nXMax = (double)(limits.right() + 0) / (double)width,
nXMax = (double)limits.right() / (double)width,
nYMin = (double)limits.top() / (double)height,
nYMax = (double)(limits.bottom() + 0) / (double)height;
nYMax = (double)limits.bottom() / (double)height;
// if no rect intersects limits, disable paintHighlights
paintHighlights = false;
QValueList< HighlightRect * >::const_iterator hIt = page->m_highlights.begin(), hEnd = page->m_highlights.end();
......@@ -165,8 +165,7 @@ void PagePainter::paintPageOnPainter( const KPDFPage * page, int id, int flags,
for ( ; hIt != hEnd; ++hIt )
{
HighlightRect * r = *hIt;
QRect highlightRect( (int)(r->left * width), (int)(r->top * height),
(int)((r->right - r->left) * width) + 1, (int)((r->bottom - r->top) * height) + 1 );
QRect highlightRect = r->geometry( width, height );
if ( highlightRect.isValid() && highlightRect.intersects( limits ) )
{
// find out the rect to highlight on pixmap
......@@ -207,12 +206,12 @@ void PagePainter::paintPageOnPainter( const KPDFPage * page, int id, int flags,
QRect limitsEnlarged = limits;
limitsEnlarged.addCoords( -2, -2, 2, 2 );
// draw rects that are inside the 'limits' paint region as opaque rects
QValueList< KPDFPageRect * >::const_iterator lIt = page->m_rects.begin(), lEnd = page->m_rects.end();
QValueList< ObjectRect * >::const_iterator lIt = page->m_rects.begin(), lEnd = page->m_rects.end();
for ( ; lIt != lEnd; ++lIt )
{
KPDFPageRect * rect = *lIt;
if ( (enhanceLinks && rect->pointerType() == KPDFPageRect::Link) ||
(enhanceImages && rect->pointerType() == KPDFPageRect::Image) )
ObjectRect * rect = *lIt;
if ( (enhanceLinks && rect->objectType() == ObjectRect::Link) ||
(enhanceImages && rect->objectType() == ObjectRect::Image) )
{
QRect rectGeometry = rect->geometry( width, height );
if ( rectGeometry.intersects( limitsEnlarged ) )
......
......@@ -876,18 +876,18 @@ void PageView::contentsMouseReleaseEvent( QMouseEvent * e )
{
double nX = (double)(e->x() - pageItem->geometry().left()) / (double)pageItem->width(),
nY = (double)(e->y() - pageItem->geometry().top()) / (double)pageItem->height();
const KPDFPageRect * rect = pageItem->page()->getRect( nX, nY );
const ObjectRect * rect = pageItem->page()->getRect( nX, nY );
if ( rect )
{
// handle click over a link
if ( rect->pointerType() == KPDFPageRect::Link )
{
const KPDFLink * link = static_cast< const KPDFLink * >( rect->pointer() );
d->document->processLink( link );
}
// handle click over an image
if ( rect->pointerType() == KPDFPageRect::Image )
// handle click over a link/image
switch ( rect->objectType() )
{
case ObjectRect::Link:{
const KPDFLink * link = static_cast< const KPDFLink * >( rect->pointer() );
d->document->processLink( link );
}break;
case ObjectRect::Image:
break;
}
}
else
......@@ -1436,11 +1436,10 @@ void PageView::updateCursor( const QPoint &p )
double nX = (double)(p.x() - pageItem->geometry().left()) / (double)pageItem->width(),
nY = (double)(p.y() - pageItem->geometry().top()) / (double)pageItem->height();
// check if over a KPDFPageRect
const KPDFPageRect * r = pageItem->page()->getRect( nX, nY );
bool onLink = r && r->pointerType() == KPDFPageRect::Link;
d->mouseOnRect = onLink;
if ( onLink )
// if over a ObjectRect (of type Link) change cursor to hand
const ObjectRect * r = pageItem->page()->getRect( nX, nY );
d->mouseOnRect = r && r->objectType() == ObjectRect::Link;
if ( d->mouseOnRect )
setCursor( pointingHandCursor );
else
setCursor( arrowCursor );
......
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