page.cpp 7.54 KB
Newer Older
1 2 3 4 5 6 7 8 9
/***************************************************************************
 *   Copyright (C) 2004 by Enrico Ros <eros.kde@email.it>                  *
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/

10 11
// qt includes
#include <qapplication.h>
12
#include <qimage.h>
13 14 15
#include <qpixmap.h>
#include <qstring.h>
#include <qpainter.h>
16
#include <qmap.h>
17

18 19 20
// system includes
#include <string.h>

21
// local includes
22
#include "Link.h"
23 24 25
#include "TextOutputDev.h"
#include "page.h"

26 27
// TODO add painting effects (plus selection rectangle)
// TODO think about moving rendering ...
28

29
KPDFPage::KPDFPage( int page, float w, float h, int r )
30
    : m_number( page ), m_rotation( r ), m_width( w ), m_height( h ),
31 32
    m_hilighting( false ), m_bookmarking( false ), m_sLeft( 0 ),
    m_sTop( 0 ), m_sRight( 0 ), m_sBottom( 0 ), m_text( 0 )
33 34 35 36 37
{
}

KPDFPage::~KPDFPage()
{
38 39 40
    QMap<int,QPixmap *>::iterator it = m_pixmaps.begin(), end = m_pixmaps.end();
    for ( ; it != end; ++it )
        delete *it;
41 42 43
    QValueList< KPDFLink * >::iterator lIt = m_links.begin(), lEnd = m_links.end();
    for ( ; lIt != lEnd; ++lIt )
        delete *lIt;
44 45 46
    delete m_text;
}

47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
bool KPDFPage::hasPixmap( int id, int width, int height ) const
{
    if ( !m_pixmaps.contains( id ) )
        return false;
    QPixmap * p = m_pixmaps[ id ];
    return p ? ( p->width() == width && p->height() == height ) : false;
}

bool KPDFPage::hasSearchPage() const
{
    return (m_text != 0);
}

bool KPDFPage::hasLink( int mouseX, int mouseY ) const
{
63 64 65 66 67 68 69
    if ( m_links.count() < 0 )
        return false;
    QValueList< KPDFLink * >::const_iterator it = m_links.begin(), end = m_links.end();
    for ( ; it != end; ++it )
        if ( (*it)->contains( mouseX, mouseY ) )
            return true;
    return false;
70 71 72
}

// BEGIN commands (paint / search)
73
void KPDFPage::drawPixmap( int id, QPainter * p, const QRect & limits, int width, int height ) const
Enrico Ros's avatar
Enrico Ros committed
74
{
75 76 77 78 79 80 81 82
    QPixmap * pixmap = 0;

    // if a pixmap is present for given id, use it
    if ( m_pixmaps.contains( id ) )
        pixmap = m_pixmaps[ id ];

    // else find the closest match using pixmaps of other IDs (great optim!)
    else if ( !m_pixmaps.isEmpty() )
Enrico Ros's avatar
Enrico Ros committed
83
    {
84 85 86
        int minDistance = -1;
        QMap<int,QPixmap *>::const_iterator it = m_pixmaps.begin(), end = m_pixmaps.end();
        for ( ; it != end; ++it )
Enrico Ros's avatar
Enrico Ros committed
87
        {
88 89 90 91 92 93 94
            int pixWidth = (*it)->width(),
                distance = pixWidth > width ? pixWidth - width : width - pixWidth;
            if ( minDistance == -1 || distance < minDistance )
            {
                pixmap = *it;
                minDistance = distance;
            }
Enrico Ros's avatar
Enrico Ros committed
95
        }
Enrico Ros's avatar
Enrico Ros committed
96
    }
97

98 99
    // if we found a pixmap draw it
    if ( pixmap )
100
    {
101 102 103 104
        // fast blit the pixmap if it has the right size..
        if ( pixmap->width() == width && pixmap->height() == height )
            p->drawPixmap( limits.topLeft(), *pixmap, limits );
        // ..else set a scale matrix to the painter and paint a quick 'zoomed' pixmap
105 106
        else
        {
107 108 109 110 111 112
            p->save();
            p->scale( width / (double)pixmap->width(), height / (double)pixmap->height() );
            p->drawPixmap( 0,0, *pixmap, 0,0, pixmap->width(), pixmap->height() );
            p->restore();
            // draw a red cross (to hilight that the pixmap has not the right size)
            p->setPen( Qt::red );
113 114
            p->drawLine( 0, 0, width-1, height-1 );
            p->drawLine( 0, height-1, width-1, 0 );
115
        }
116
        // draw selection
117
        if ( m_hilighting )
118 119 120 121 122 123 124 125 126 127 128 129 130
        {
            int x = (int)( m_sLeft * width / m_width ),
                y = (int)( m_sTop * height / m_height ),
                w = (int)( m_sRight * width / m_width ) - x,
                h = (int)( m_sBottom * height / m_height ) - y;
            if ( w > 0 && h > 0 )
            {
                p->setBrush( Qt::SolidPattern );
                p->setPen( QPen( Qt::black, 1 ) ); // should not be necessary bug a Qt bug makes it necessary
                p->setRasterOp( Qt::NotROP );
                p->drawRect( x, y, w, h );
            }
        }
131
    }
132
    // else draw a blank area
133
    else
134
        p->fillRect( limits, Qt::white /*FIXME change to the page bg color*/ );
135 136
}

137
bool KPDFPage::hasText( const QString & text, bool strictCase, bool fromTop )
138
{
139
    if ( !m_text )
140
        return false;
141 142 143 144 145 146 147 148 149 150 151 152 153 154

    const char * str = text.latin1();
    int len = text.length();
    Unicode *u = (Unicode *)gmalloc(len * sizeof(Unicode));
    for (int i = 0; i < len; ++i)
        u[i] = (Unicode) str[i];

    bool found = m_text->findText( u, len, fromTop ? gTrue : gFalse, gTrue, fromTop ? gFalse : gTrue, gFalse, &m_sLeft, &m_sTop, &m_sRight, &m_sBottom );
    if( found && strictCase )
    {
        GString * orig = m_text->getText( m_sLeft, m_sTop, m_sRight, m_sBottom );
        found = !strcmp( text.latin1(), orig->getCString() );
    }
    return found;
155 156
}

157
void KPDFPage::hilightLastSearch( bool on )
158
{
159 160
    m_hilighting = on;
    //if ( !on ) -> invalidate search rect?
161
}
162 163 164 165 166

void KPDFPage::bookmark( bool on )
{
    m_bookmarking = on;
}
167 168
// END commands (paint / search)

169

170
void KPDFPage::setPixmap( int id, QPixmap * pixmap )
171
{
172
    if ( m_pixmaps.contains( id ) )
173 174
        delete m_pixmaps[id];
    m_pixmaps[id] = pixmap;
175 176
}

177
void KPDFPage::setSearchPage( TextPage * tp )
178
{
179
    delete m_text;
180
    m_text = tp;
181
}
182

183 184 185 186 187 188
void KPDFPage::setLinks( const QValueList<KPDFLink *> links )
{
    QValueList< KPDFLink * >::iterator it = m_links.begin(), end = m_links.end();
    for ( ; it != end; ++it )
        delete *it;
    m_links = links;
189 190
}

191
/*
192
void KPDFPage::setPixmapOverlayNotations( ..DOMdescription.. )
193
{   //TODO this
194 195
}
*/
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275



KPDFLink::KPDFLink( int l, int t, int r, int b )
    : m_type( Unknown ), m_dest( 0 ), m_destNamed( 0 ),
    m_fileName( 0 ), m_parameters( 0 ), m_uri( 0 )
{
    // assign coordinates swapping them if negative width or height
    x_min = r > l ? l : r;
    x_max = r > l ? r : l;
    y_min = b > t ? t : b;
    y_max = b > t ? b : t;
}

KPDFLink::~KPDFLink()
{
    delete m_dest;
    delete [] m_destNamed;
    delete [] m_fileName;
    delete [] m_parameters;
    delete [] m_uri;
}

void KPDFLink::setLinkGoto( LinkDest * d, const char * n, const char * file )
{
    m_type = Goto;
    delete m_dest;
    m_dest = d;
    copyString( m_destNamed, n );
    copyString( m_fileName, file );
}

void KPDFLink::setLinkExecute( const char * file, const char * par )
{
    m_type = Execute;
    copyString( m_fileName, file );
    copyString( m_parameters, par );
}

void KPDFLink::setLinkNamed( const char * name )
{
    m_type = Action;
    copyString( m_uri, name );
}

void KPDFLink::setLinkURI( const char * uri )
{
    m_type = URI;
    copyString( m_uri, uri );
}

void KPDFLink::setLinkMovie( int ref_num, int ref_gen, const char * title )
{
    m_type = Movie;
    m_refNum = ref_num;
    m_refGen = ref_gen;
    copyString( m_uri, title );
}

KPDFLink::LinkType KPDFLink::type() const
{
    return m_type;
}

bool KPDFLink::contains( int x, int  y ) const
{
    return (x > x_min) && (x < x_max) && (y > y_min) && (y < y_max);
}

void KPDFLink::copyString( char * dest, const char * src )
{
    if ( dest )
        delete [] dest;
    dest = 0;
    if ( src )
    {
        dest = new char[ strlen(src) + 1 ];
        strcpy( dest, src );
    }
}