thumbnail.cpp 2.99 KB
Newer Older
Albert Astals Cid's avatar
Albert Astals Cid committed
1
/***************************************************************************
2
 *   Copyright (C) 2004 by Albert Astals Cid <tsdgeos@terra.es>            *
Albert Astals Cid's avatar
Albert Astals Cid committed
3
4
5
6
7
8
9
10
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include <qlabel.h>
11
#include <qpainter.h>
Albert Astals Cid's avatar
Albert Astals Cid committed
12
13

#include "thumbnail.h"
14
#include "document.h"
15
#include "page.h"
Albert Astals Cid's avatar
Albert Astals Cid committed
16

17
Thumbnail::Thumbnail( QWidget *parent, const KPDFPage *page )
18
    : QWidget( parent ), m_page( page ), m_previewWidth( 0 ), m_previewHeight( 0 ), m_selected( false )
Albert Astals Cid's avatar
Albert Astals Cid committed
19
{
20
    m_labelNumber = page->number() + 1;
21
    setPaletteBackgroundColor( palette().active().base() );
Albert Astals Cid's avatar
Albert Astals Cid committed
22
23
}

24
25
//BEGIN commands 
int Thumbnail::setThumbnailWidth( int width )
26
{
27
28
29
30
31
32
33
    // compute and update drawable area dimensions
    // note: 3 pixels are left (in each dimension) for page decorations 
    m_previewWidth = width - 3;
    //TODO albert: check page rotation for aspect ratio
    m_previewHeight = (int)(m_page->ratio() * width);

    // reposition label at bottom
34
35
    m_labelHeight = QFontMetrics( font() ).height();
    m_labelWidth = width;
36
37

    // resize the widget
38
    int totalHeight = m_previewHeight + 3 + m_labelHeight;
39
40
41
42
43
44
45
46
    resize( width, totalHeight );

    // return this->height plus a little (4px) margin to the next page
    return totalHeight + 4;
}

void Thumbnail::setSelected( bool selected )
{
47
48
    m_selected = selected;
    update( 0, m_previewHeight + 3, m_labelWidth, m_labelHeight );
49
}
50
//END commands 
51

52
53
//BEGIN query methods 
int Thumbnail::pageNumber() const
Albert Astals Cid's avatar
Albert Astals Cid committed
54
{
55
    return m_page->number();
Albert Astals Cid's avatar
Albert Astals Cid committed
56
57
}

58
int Thumbnail::previewWidth() const
Albert Astals Cid's avatar
Albert Astals Cid committed
59
{
60
    return m_previewWidth;
Albert Astals Cid's avatar
Albert Astals Cid committed
61
62
}

63
int Thumbnail::previewHeight() const
Albert Astals Cid's avatar
Albert Astals Cid committed
64
{
65
    return m_previewHeight;
Albert Astals Cid's avatar
Albert Astals Cid committed
66
}
67
68
69
70
71
72
73
//END query methods 

void Thumbnail::paintEvent( QPaintEvent * e )
{
    QRect clipRect = e->rect();
    QPainter p( this );

74
75
76
77
78
    // draw the bottom label
    QColor fillColor = m_selected ? palette().active().highlight() : palette().active().base();
    p.fillRect( 0, m_previewHeight + 3, m_labelWidth, m_labelHeight, fillColor );
    p.drawText( 0, m_previewHeight + 3, m_labelWidth, m_labelHeight, Qt::AlignCenter, QString::number( m_labelNumber ) );

79
80
81
82
83
    // draw page outline
    p.drawRect( 0, 0, m_previewWidth + 2, m_previewHeight + 2 );
    p.setPen( Qt::gray );
    p.drawLine( 4, m_previewHeight + 2, m_previewWidth + 2, m_previewHeight + 2 );
    p.drawLine( m_previewWidth + 2, 4, m_previewWidth + 2, m_previewHeight + 2 );
Albert Astals Cid's avatar
Albert Astals Cid committed
84

85
86
87
88
    // draw the pixmap
    p.translate( 1, 1 );
    clipRect.moveBy( -1, -1 );
    clipRect = clipRect.intersect( QRect( 0, 0, m_previewWidth, m_previewHeight ) );
89
    m_page->drawPixmap( THUMBNAILS_ID, &p, clipRect, m_previewWidth, m_previewHeight );
Albert Astals Cid's avatar
Albert Astals Cid committed
90

91
92
    p.end();
}