From 229f3cf1deb240ad48bfc7477d492299a0b34158 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Sun, 25 Mar 2007 22:36:41 +0000 Subject: [PATCH] add a private function for the nearest Page pixmap, for the PagePainter usage; this way, the pixmap structure can be finally hidden in the private class svn path=/trunk/playground/graphics/okular/; revision=646502 --- core/page.cpp | 60 ++++++++++++++++++++++++++++++++++------------ core/page.h | 10 ++------ core/page_p.h | 9 +++++++ ui/pagepainter.cpp | 24 +------------------ 4 files changed, 57 insertions(+), 46 deletions(-) diff --git a/core/page.cpp b/core/page.cpp index 19350a584..ab0efcf29 100644 --- a/core/page.cpp +++ b/core/page.cpp @@ -71,18 +71,18 @@ PagePrivate::~PagePrivate() void PagePrivate::imageRotationDone( RotationJob * job ) { - QMap< int, Page::PixmapObject >::iterator it = m_page->m_pixmaps.find( job->id() ); - if ( it != m_page->m_pixmaps.end() ) + QMap< int, PixmapObject >::iterator it = m_pixmaps.find( job->id() ); + if ( it != m_pixmaps.end() ) { - Page::PixmapObject &object = it.value(); + PixmapObject &object = it.value(); (*object.m_pixmap) = QPixmap::fromImage( job->image() ); object.m_rotation = job->rotation(); } else { - Page::PixmapObject object; + PixmapObject object; object.m_pixmap = new QPixmap( QPixmap::fromImage( job->image() ) ); object.m_rotation = job->rotation(); - m_page->m_pixmaps.insert( job->id(), object ); + m_pixmaps.insert( job->id(), object ); } } @@ -165,8 +165,8 @@ double Page::ratio() const bool Page::hasPixmap( int id, int width, int height ) const { - QMap< int, PixmapObject >::const_iterator it = m_pixmaps.find( id ); - if ( it == m_pixmaps.end() ) + QMap< int, PagePrivate::PixmapObject >::const_iterator it = d->m_pixmaps.find( id ); + if ( it == d->m_pixmaps.end() ) return false; if ( width == -1 || height == -1 ) @@ -276,11 +276,11 @@ void Page::rotateAt( Rotation orientation ) /** * Rotate the images of the page. */ - QMapIterator< int, PixmapObject > it( m_pixmaps ); + QMapIterator< int, PagePrivate::PixmapObject > it( d->m_pixmaps ); while ( it.hasNext() ) { it.next(); - const PixmapObject &object = it.value(); + const PagePrivate::PixmapObject &object = it.value(); RotationJob *job = new RotationJob( object.m_pixmap->toImage(), object.m_rotation, d->m_rotation, it.key() ); job->setPage( d ); @@ -354,14 +354,14 @@ const QLinkedList< FormField * > Page::formFields() const void Page::setPixmap( int id, QPixmap *pixmap ) { if ( d->m_rotation == Rotation0 ) { - QMap< int, PixmapObject >::iterator it = m_pixmaps.find( id ); - if ( it != m_pixmaps.end() ) + QMap< int, PagePrivate::PixmapObject >::iterator it = d->m_pixmaps.find( id ); + if ( it != d->m_pixmaps.end() ) { delete it.value().m_pixmap; } else { - it = m_pixmaps.insert( id, PixmapObject() ); + it = d->m_pixmaps.insert( id, PagePrivate::PixmapObject() ); } it.value().m_pixmap = pixmap; it.value().m_rotation = d->m_rotation; @@ -565,19 +565,19 @@ void Page::setFormFields( const QLinkedList< FormField * >& fields ) void Page::deletePixmap( int id ) { - PixmapObject object = m_pixmaps.take( id ); + PagePrivate::PixmapObject object = d->m_pixmaps.take( id ); delete object.m_pixmap; } void Page::deletePixmaps() { - QMapIterator< int, PixmapObject > it( m_pixmaps ); + QMapIterator< int, PagePrivate::PixmapObject > it( d->m_pixmaps ); while ( it.hasNext() ) { it.next(); delete it.value().m_pixmap; } - m_pixmaps.clear(); + d->m_pixmaps.clear(); } void Page::deleteRects() @@ -731,3 +731,33 @@ void Page::saveLocalContents( QDomNode & parentNode, QDomDocument & document ) c if ( pageElement.hasChildNodes() ) parentNode.appendChild( pageElement ); } + +const QPixmap * Page::_o_nearestPixmap( int pixID, int w, int h ) const +{ + Q_UNUSED( h ) + + const QPixmap * pixmap = 0; + + // if a pixmap is present for given id, use it + QMap< int, PagePrivate::PixmapObject >::const_iterator itPixmap = d->m_pixmaps.find( pixID ); + if ( itPixmap != d->m_pixmaps.end() ) + pixmap = itPixmap.value().m_pixmap; + // else find the closest match using pixmaps of other IDs (great optim!) + else if ( !d->m_pixmaps.isEmpty() ) + { + int minDistance = -1; + QMap< int, PagePrivate::PixmapObject >::const_iterator it = d->m_pixmaps.begin(), end = d->m_pixmaps.end(); + for ( ; it != end; ++it ) + { + int pixWidth = (*it).m_pixmap->width(), + distance = pixWidth > w ? pixWidth - w : w - pixWidth; + if ( minDistance == -1 || distance < minDistance ) + { + pixmap = (*it).m_pixmap; + minDistance = distance; + } + } + } + + return pixmap; +} diff --git a/core/page.h b/core/page.h index af1aa4067..22701c2b4 100644 --- a/core/page.h +++ b/core/page.h @@ -11,7 +11,6 @@ #define _OKULAR_PAGE_H_ #include -#include #include #include @@ -19,6 +18,7 @@ class QDomDocument; class QDomNode; +class QPixmap; class QRect; class PagePainter; @@ -354,13 +354,7 @@ class OKULAR_EXPORT Page */ friend class ::PagePainter; - class PixmapObject - { - public: - QPixmap *m_pixmap; - Rotation m_rotation; - }; - QMap< int, PixmapObject > m_pixmaps; + const QPixmap * _o_nearestPixmap( int, int, int ) const; QLinkedList< ObjectRect* > m_rects; QLinkedList< HighlightAreaRect* > m_highlights; diff --git a/core/page_p.h b/core/page_p.h index 8ffe4e0b0..0d43b3ec7 100644 --- a/core/page_p.h +++ b/core/page_p.h @@ -13,6 +13,7 @@ // qt/kde includes #include +#include #include #include @@ -37,6 +38,14 @@ class PagePrivate void imageRotationDone( RotationJob * job ); QMatrix rotationMatrix() const; + class PixmapObject + { + public: + QPixmap *m_pixmap; + Rotation m_rotation; + }; + QMap< int, PixmapObject > m_pixmaps; + Page *m_page; int m_number; Rotation m_orientation; diff --git a/ui/pagepainter.cpp b/ui/pagepainter.cpp index 84b49e38c..2cb222946 100644 --- a/ui/pagepainter.cpp +++ b/ui/pagepainter.cpp @@ -39,29 +39,7 @@ void PagePainter::paintPageOnPainter( QPainter * destPainter, const Okular::Page int pixID, int flags, int scaledWidth, int scaledHeight, const QRect &limits ) { /** 1 - RETRIEVE THE 'PAGE+ID' PIXMAP OR A SIMILAR 'PAGE' ONE **/ - const QPixmap * pixmap = 0; - - // if a pixmap is present for given id, use it - QMap< int, Okular::Page::PixmapObject >::const_iterator itPixmap = page->m_pixmaps.find( pixID ); - if ( itPixmap != page->m_pixmaps.end() ) - pixmap = itPixmap.value().m_pixmap; - - // else find the closest match using pixmaps of other IDs (great optim!) - else if ( !page->m_pixmaps.isEmpty() ) - { - int minDistance = -1; - QMap< int, Okular::Page::PixmapObject >::const_iterator it = page->m_pixmaps.begin(), end = page->m_pixmaps.end(); - for ( ; it != end; ++it ) - { - int pixWidth = (*it).m_pixmap->width(), - distance = pixWidth > scaledWidth ? pixWidth - scaledWidth : scaledWidth - pixWidth; - if ( minDistance == -1 || distance < minDistance ) - { - pixmap = (*it).m_pixmap; - minDistance = distance; - } - } - } + const QPixmap * pixmap = page->_o_nearestPixmap( pixID, scaledWidth, scaledHeight ); /** 1B - IF NO PIXMAP, DRAW EMPTY PAGE **/ double pixmapRescaleRatio = pixmap ? scaledWidth / (double)pixmap->width() : -1;