diff --git a/kpdf/pageview.cpp b/kpdf/pageview.cpp index ff8f7f30d..fccfb095c 100644 --- a/kpdf/pageview.cpp +++ b/kpdf/pageview.cpp @@ -34,7 +34,6 @@ #include "pageview.h" #include "pageviewutils.h" -#include "pixmapwidget.h" #include "page.h" #include "settings.h" diff --git a/kpdf/pixmapwidget.cpp b/kpdf/pixmapwidget.cpp index d67d22934..126934f45 100644 --- a/kpdf/pixmapwidget.cpp +++ b/kpdf/pixmapwidget.cpp @@ -8,166 +8,5 @@ * (at your option) any later version. * ***************************************************************************/ -#include -#include -#include -#include -#include "pixmapwidget.h" -#include "document.h" -#include "page.h" -#include "settings.h" -// TODO : REMOVE "#ifndef NDEBUG" stuff before merging to HEAD! (added 20041024) - -PixmapWidget::PixmapWidget( QWidget * parent, const KPDFPage * kp, const char * name ) -/* Note on PixmapWidget Wflags (Enrico): - It's SO IMPORTANT to set WNoAutoErase and WStaticContents. - If not, already exposed widgets will receive a paint event when resized - (for example when zooming/relayouting in PageView) even if not shown in - current PageView's viewport. -*/ - : QWidget( parent, name, WNoAutoErase | WStaticContents ), m_page( kp ), - m_marginLeft( 0 ), m_marginTop( 0 ), m_marginRight( 0 ), m_marginBottom( 0 ), - m_pixmapWidth( -1 ), m_pixmapHeight( -1 ), - m_zoomFactor( 1.0 ) -{ -} - -void PixmapWidget::setZoomFixed( double magFactor ) -{ - m_pixmapWidth = (int)( magFactor * m_page->width() ); - m_pixmapHeight = (int)( magFactor * m_page->height() ); - m_zoomFactor = magFactor; -} - -void PixmapWidget::setZoomFitWidth( int width ) -{ - m_pixmapWidth = width - m_marginLeft - m_marginRight; - m_pixmapHeight = (int)(m_page->ratio() * m_pixmapWidth); - // compute equivalent zoom factor - m_zoomFactor = (double)m_pixmapWidth / (double)m_page->width(); -} - -void PixmapWidget::setZoomFitRect( int rectWidth, int rectHeight ) -{ - rectWidth -= (m_marginLeft + m_marginRight); - rectHeight -= (m_marginTop + m_marginBottom); - double scaleW = (double)rectWidth / (double)m_page->width(); - double scaleH = (double)rectHeight / (double)m_page->height(); - setZoomFixed( scaleW < scaleH ? scaleW : scaleH ); -} - -int PixmapWidget::widthHint() const -{ -#ifndef NDEBUG - return m_marginLeft + pixmapWidth() + m_marginRight; -#else - return m_marginLeft + m_pixmapWidth + m_marginRight; -#endif -} - -int PixmapWidget::heightHint() const -{ -#ifndef NDEBUG - return m_marginTop + pixmapHeight() + m_marginBottom; -#else - return m_marginTop + m_pixmapHeight + m_marginBottom; -#endif -} - -int PixmapWidget::pixmapWidth() const -{ -#ifndef NDEBUG - // code enabled for dev/testers only. please make sure that setZoom* - // gets called - if ( m_pixmapWidth < 0 ) - { - kdDebug() << "No pixmapWidth set for page " << m_page->number() << " !" << endl; - return 1; - } -#endif - return m_pixmapWidth; -} - -int PixmapWidget::pixmapHeight() const -{ -#ifndef NDEBUG - // code enabled for dev/testers only. please make sure that setZoom* - // gets called - if ( m_pixmapHeight < 0 ) - { - kdDebug() << "No pixmapHeight set for page " << m_page->number() << " !" << endl; - return 1; - } -#endif - return m_pixmapHeight; -} - -int PixmapWidget::pageNumber() const -{ - return m_page->number(); -} - -const KPDFPage * PixmapWidget::page() const -{ - return m_page; -} - -void PixmapWidget::setPixmapMargins( int left, int top, int right, int bottom ) -{ - m_marginLeft = left; - m_marginTop = top; - m_marginRight = right; - m_marginBottom = bottom; -} - - -/** ThumbnailWidget **/ - -ThumbnailWidget::ThumbnailWidget( QWidget *parent, const KPDFPage *page ) - : PixmapWidget( parent, page ), m_selected( false ) -{ - m_labelNumber = page->number() + 1; - m_labelHeight = QFontMetrics( font() ).height(); - setPixmapMargins( 2, 1, 2, m_labelHeight + 2 ); -} - -void ThumbnailWidget::setSelected( bool selected ) -{ - m_selected = selected; - update( 0, m_pixmapHeight + 3, width(), m_labelHeight ); -} - -void ThumbnailWidget::paintEvent( QPaintEvent * e ) -{ - int width = m_marginLeft + m_pixmapWidth + m_marginRight; - QRect clipRect = e->rect(); - QPainter p( this ); - - // draw the bottom label - if ( clipRect.bottom() > m_pixmapHeight + 3 ) - { - QColor fillColor = m_selected ? palette().active().highlight() : palette().active().base(); - p.fillRect( 0, m_pixmapHeight + 4, width, m_labelHeight, fillColor ); - p.drawText( 0, m_pixmapHeight + 4, width, m_labelHeight, Qt::AlignCenter, QString::number( m_labelNumber ) ); - } - - // draw page outline and pixmap - if ( clipRect.top() < m_pixmapHeight + 4 ) - { - p.drawRect( 1, 1, m_pixmapWidth + 2, m_pixmapHeight + 2 ); - p.setPen( palette().active().base() ); - p.drawRect( 0, 0, m_pixmapWidth + 4, m_pixmapHeight + 4 ); - p.setPen( Qt::gray ); - p.drawLine( 5, m_pixmapHeight + 3, m_pixmapWidth + 3, m_pixmapHeight + 3 ); - p.drawLine( m_pixmapWidth + 3, 5, m_pixmapWidth + 3, m_pixmapHeight + 3 ); - - p.translate( 2, 2 ); - clipRect.moveBy( -2, -2 ); - clipRect = clipRect.intersect( QRect( 0, 0, m_pixmapWidth, m_pixmapHeight ) ); - m_page->drawPixmap( THUMBNAILS_ID, &p, clipRect, m_pixmapWidth, m_pixmapHeight ); - } - - p.end(); -} diff --git a/kpdf/pixmapwidget.h b/kpdf/pixmapwidget.h index 2a476990b..0cfe25189 100644 --- a/kpdf/pixmapwidget.h +++ b/kpdf/pixmapwidget.h @@ -11,65 +11,4 @@ #ifndef _PIXMAPWIDGET_H_ #define _PIXMAPWIDGET_H_ -#include -#include - -class KPDFPage; - -/** - * @short Graphical representation of a page. - * ... - */ -class PixmapWidget : public QWidget -{ -public: - PixmapWidget( QWidget * parent, const KPDFPage * page, const char * name = 0 ); - - // internal size/placements evaluators - void setZoomFixed( double magFactor = 1.0 ); - void setZoomFitWidth( int width ); - void setZoomFitRect( int rectWidth, int rectHeight ); - float zoomFactor() const { return m_zoomFactor; } - - // full size (for resizing) and inner pixmap size - int widthHint() const; - int heightHint() const; - int pixmapWidth() const; - int pixmapHeight() const; - - // other queries - int pageNumber() const; - const KPDFPage * page() const; - -protected: - void setPixmapMargins( int left, int top, int right, int bottom ); - - const KPDFPage * m_page; - int m_marginLeft, m_marginTop, m_marginRight, m_marginBottom; - int m_pixmapWidth, m_pixmapHeight; - float m_zoomFactor; -}; - - -/** - * @short ThumbnailList's selectable page. - * ... - */ -class ThumbnailWidget : public PixmapWidget -{ -public: - ThumbnailWidget( QWidget *parent, const KPDFPage *page ); - - // set the thumbnail selected state - void setSelected( bool selected ); - -protected: - void paintEvent(QPaintEvent *); - -private: - bool m_selected; - int m_labelNumber; - int m_labelHeight; -}; - #endif diff --git a/kpdf/thumbnaillist.cpp b/kpdf/thumbnaillist.cpp index a52590a13..330b9e475 100644 --- a/kpdf/thumbnaillist.cpp +++ b/kpdf/thumbnaillist.cpp @@ -8,6 +8,7 @@ ***************************************************************************/ #include +#include #include #include #include @@ -15,9 +16,38 @@ #include #include "thumbnaillist.h" -#include "pixmapwidget.h" #include "page.h" +// ThumbnailWidget represents a single thumbnail in the ThumbnailList +class ThumbnailWidget : public QWidget +{ + public: + ThumbnailWidget( QWidget * parent, const KPDFPage * page ); + + // set internal parameters to fit the page in the given width + void resizeFitWidth( int width ); + // set thumbnail's selected state + void setSelected( bool selected ); + + // query methods + int heightHint() const { return m_pixmapHeight + m_labelHeight + 4; } + int pixmapWidth() const { return m_pixmapWidth; } + int pixmapHeight() const { return m_pixmapHeight; } + int pageNumber() const { return m_page->number(); } + + protected: + void paintEvent(QPaintEvent *); + + private: + const KPDFPage * m_page; + bool m_selected; + int m_pixmapWidth, m_pixmapHeight; + int m_labelHeight, m_labelNumber; +}; + + +/** ThumbnailList implementation **/ + ThumbnailList::ThumbnailList( QWidget *parent, KPDFDocument *document ) : QScrollView( parent, "KPDF::Thumbnails", WNoAutoErase | WStaticContents ), m_document( document ), m_selected( 0 ), m_delayTimer( 0 ) @@ -83,8 +113,7 @@ void ThumbnailList::pageSetup( const QValueVector & pages, bool /*doc // add to the internal queue m_thumbnails.push_back( t ); // update total height (asking widget its own height) - t->setZoomFitWidth( width ); - t->resize( t->widthHint(), t->heightHint() ); + t->resizeFitWidth( width ); totalHeight += t->heightHint() + 4; t->show(); } @@ -220,8 +249,7 @@ void ThumbnailList::viewportResizeEvent( QResizeEvent * e ) { ThumbnailWidget *t = *thumbIt; moveChild( t, 0, totalHeight ); - t->setZoomFitWidth( newWidth ); - t->resize( t->widthHint(), t->heightHint() ); + t->resizeFitWidth( newWidth ); totalHeight += t->heightHint() + 4; } @@ -274,4 +302,63 @@ void ThumbnailList::requestPixmaps( int delayMs ) m_delayTimer->start( delayMs, true ); } + +/** ThumbnailWidget implementation **/ + +ThumbnailWidget::ThumbnailWidget( QWidget * parent, const KPDFPage * kp ) + : QWidget( parent, 0, WNoAutoErase ), m_page( kp ), + m_selected( false ), m_pixmapWidth( 10 ), m_pixmapHeight( 10 ) +{ + m_labelNumber = m_page->number() + 1; + m_labelHeight = QFontMetrics( font() ).height(); +} + +void ThumbnailWidget::resizeFitWidth( int width ) +{ + m_pixmapWidth = width - 4; + m_pixmapHeight = (int)(m_page->ratio() * m_pixmapWidth); + resize( width, heightHint() ); +} + +void ThumbnailWidget::setSelected( bool selected ) +{ + // update selected state + if ( m_selected != selected ) + { + m_selected = selected; + update( 0, m_pixmapHeight + 4, width(), m_labelHeight ); + } +} + +void ThumbnailWidget::paintEvent( QPaintEvent * e ) +{ + int width = m_pixmapWidth + 4; + QRect clipRect = e->rect(); + QPainter p( this ); + + // draw the bottom label + if ( clipRect.bottom() > m_pixmapHeight + 3 ) + { + QColor fillColor = m_selected ? palette().active().highlight() : palette().active().base(); + p.fillRect( 0, m_pixmapHeight + 4, width, m_labelHeight, fillColor ); + p.drawText( 0, m_pixmapHeight + 4, width, m_labelHeight, Qt::AlignCenter, QString::number( m_labelNumber ) ); + } + + // draw page outline and pixmap + if ( clipRect.top() < m_pixmapHeight + 4 ) + { + p.drawRect( 1, 1, m_pixmapWidth + 2, m_pixmapHeight + 2 ); + p.setPen( palette().active().base() ); + p.drawRect( 0, 0, m_pixmapWidth + 4, m_pixmapHeight + 4 ); + p.setPen( Qt::gray ); + p.drawLine( 5, m_pixmapHeight + 3, m_pixmapWidth + 3, m_pixmapHeight + 3 ); + p.drawLine( m_pixmapWidth + 3, 5, m_pixmapWidth + 3, m_pixmapHeight + 3 ); + + p.translate( 2, 2 ); + clipRect.moveBy( -2, -2 ); + clipRect = clipRect.intersect( QRect( 0, 0, m_pixmapWidth, m_pixmapHeight ) ); + m_page->drawPixmap( THUMBNAILS_ID, &p, clipRect, m_pixmapWidth, m_pixmapHeight ); + } +} + #include "thumbnaillist.moc"