Commit 9392dd27 authored by Enrico Ros's avatar Enrico Ros

Break everything I can. (still compiles but I don't want it to do! :-).

Cleaned up a lot. Added 2 classes, reorganized the code. Also reindented
the part to please AACid's style. I'll resort some of the funcitonalities
soon. Plz, wait for the end of breakings before making changes.

svn path=/branches/kpdf_experiments/kdegraphics/kpdf/; revision=344958
parent 871422ce
......@@ -38,7 +38,7 @@ shellrc_DATA = kpdf_shell.rc
kde_module_LTLIBRARIES = libkpdfpart.la
# the Part's source, library search path, and link libraries
libkpdfpart_la_SOURCES = QOutputDev.cpp QOutputDevKPrinter.cpp kpdf_part.cpp kpdf_pagewidget.cc thumbnail.cpp thumbnaillist.cpp kpdf_error.cpp xpdf_errors.cpp thumbnailgenerator.cpp
libkpdfpart_la_SOURCES = QOutputDev.cpp QOutputDevKPrinter.cpp kpdf_part.cpp kpdf_pagewidget.cc thumbnail.cpp thumbnaillist.cpp kpdf_error.cpp xpdf_errors.cpp thumbnailgenerator.cpp document.cpp page.cpp
libkpdfpart_la_LDFLAGS = -module $(KDE_PLUGIN) $(all_libraries)
libkpdfpart_la_LIBADD = ../xpdf/libxpdf.la $(LIB_KPARTS) $(LIB_KFILE) $(LIB_KDEPRINT) $(LIB_KUTILS) -lm
......
/***************************************************************************
* 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. *
***************************************************************************/
// qt includes
#include <qfile.h>
#include <qmutex.h>
#include <qvaluevector.h>
// local includes
#include "PDFDoc.h"
//#include "TextOutputDev.h"
#include "kpdf_error.h"
#include "document.h"
#include "page.h"
// structure used internally by KPDFDocument for data storage
struct KPDFDocumentPrivate
{
// document related
QMutex docLock;
PDFDoc * pdfdoc;
int currentPage;
QValueVector< KPDFPage* > pages;
// observers related (note: won't delete oservers)
QValueList< KPDFDocumentObserver* > observers;
};
#define foreachObserver( cmd ) {\
QValueList<KPDFDocumentObserver*>::iterator it = d->observers.begin();\
QValueList<KPDFDocumentObserver*>::iterator end = d->observers.end();\
for ( ; it != end ; ++ it ) { (*it)-> cmd ; } }
/*
* KPDFDocument class
*/
KPDFDocument::KPDFDocument()
{
d = new KPDFDocumentPrivate;
d->pdfdoc = 0;
}
KPDFDocument::~KPDFDocument()
{
close();
delete d;
}
bool KPDFDocument::openFile( const QString & docFile )
{
// docFile is always local so we can use QFile on it
QFile fileReadTest( docFile );
if ( !fileReadTest.open( IO_ReadOnly ) )
return false;
fileReadTest.close();
GString *filename = new GString( QFile::encodeName( docFile ) );
delete d->pdfdoc;
d->pdfdoc = new PDFDoc( filename, 0, 0 );
deletePages();
if ( !d->pdfdoc->isOk() || d->pdfdoc->getNumPages() < 1 )
{
delete d->pdfdoc;
d->pdfdoc = 0;
return false;
}
// clear xpdf errors
errors::clear();
// build Pages
uint pageCount = d->pdfdoc->getNumPages();
d->pages.resize( pageCount );
for ( uint i = 0; i < pageCount ; i++ )
d->pages[i] = new KPDFPage( i, d->pdfdoc->getPageHeight(i + 1), d->pdfdoc->getPageWidth(i + 1) );
//filter = NONE; TODO
sendFilteredPageList();
setCurrentPage( 0 );
return true;
}
void KPDFDocument::close()
{
//stopRunningJobs()...
deletePages();
delete d->pdfdoc;
d->pdfdoc = 0;
}
const KPDFPage * KPDFDocument::page( uint n ) const
{
return ( n < d->pages.count() ) ? d->pages[n] : 0;
}
uint KPDFDocument::pages() const
{
return d->pdfdoc ? d->pdfdoc->getNumPages() : 0;
}
void KPDFDocument::setCurrentPage( uint page, float position )
{ //FIXME event queuing to avoid flow interruption (!!??)
if ( (int)page == d->currentPage ) //TODO check position
return;
d->currentPage = page;
foreachObserver( pageSetCurrent( page, position ) );
pageChanged();
}
void KPDFDocument::find( bool /*nextMatch*/, const QString & /*text*/ )
{
/*
TextOutputDev *textOut;
Unicode *u;
bool found;
double xMin1, yMin1, xMax1, yMax1;
int len, pg;
// This is more or less copied from what xpdf does, surely can be optimized
len = s.length();
u = (Unicode *)gmalloc(len * sizeof(Unicode));
for (int i = 0; i < len; ++i) u[i] = (Unicode)(s.latin1()[i] & 0xff);
// search current
found = m_outputDev->find(u, len, next);
if (!found)
{
// search following pages
textOut = new TextOutputDev(NULL, gTrue, gFalse, gFalse);
if (!textOut->isOk())
{
gfree(u);
delete textOut;
return;
}
pg = m_currentPage + 1;
while(!found && pg <= d->pdfdoc->getNumPages())
{
m_docMutex.lock();
d->pdfdoc->displayPage(textOut, pg, 72, 72, 0, gTrue, gFalse);
m_docMutex.unlock();
found = textOut->findText(u, len, gTrue, gTrue, gFalse, gFalse, &xMin1, &yMin1, &xMax1, &yMax1);
if (!found) pg++;
}
if (!found && m_currentPage != 1)
{
if (KMessageBox::questionYesNo(widget(), i18n("End of document reached.\nContinue from the beginning?")) == KMessageBox::Yes)
{
// search previous pages
pg = 1;
while(!found && pg < m_currentPage)
{
m_docMutex.lock();
d->pdfdoc->displayPage(textOut, pg, 72, 72, 0, gTrue, gFalse);
m_docMutex.unlock();
found = textOut->findText(u, len, gTrue, gTrue, gFalse, gFalse, &xMin1, &yMin1, &xMax1, &yMax1);
if (!found) pg++;
}
}
}
delete textOut;
if (found)
{
kdDebug() << "found at " << pg << endl;
goToPage(pg);
// xpdf says: can happen that we do not find the text if coalescing is bad OUCH
m_outputDev->find(u, len, false);
}
else
{
if (next) KMessageBox::information(widget(), i18n("No more matches found for '%1'.").arg(s));
else KMessageBox::information(widget(), i18n("No matches found for '%1'.").arg(s));
}
}
if (found) m_findText = s;
else m_findText = QString::null;
gfree(u);
*/
}
void KPDFDocument::goToLink( /* QString anchor */ )
{
}
void KPDFDocument::addObserver( KPDFDocumentObserver * pObserver )
{
d->observers.push_back( pObserver );
}
void KPDFDocument::sendFilteredPageList()
{ //TODO implement filtering
// make up a value list of the pages [1,2,3..]
uint pageCount = d->pages.count();
QValueList<int> pagesList;
for ( uint i = 0; i < pageCount ; i++ )
pagesList.push_back( i );
// send the list to observers
foreachObserver( pageSetup( pagesList ) );
}
void KPDFDocument::deletePages()
{
if ( d->pages.isEmpty() )
return;
// broadcast an empty page list to observers
QValueList<int> pagesList;
foreachObserver( pageSetup( pagesList ) );
// delete pages and clear container
for ( uint i = 0; i < d->pages.count() ; i++ )
delete d->pages[i];
d->pages.clear();
d->currentPage = -1;
}
#include "document.moc"
/***************************************************************************
* 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. *
***************************************************************************/
#ifndef _KPDF_DOCUMENT_H_
#define _KPDF_DOCUMENT_H_
#include <qobject.h>
#include <qvaluelist.h>
class KPDFPage;
/**
* @short Base class for objects being notified when something changes.
*
* Inherit this class and call KPDFDocument->addObserver( obsClass ) to get
* notified of asyncronous events (a new thumbnail has arrived, a pixmap has
* changed, and other events).
*/
class KPDFDocumentObserver
{
public:
// monitor changes in pixmaps (generation thread complete)
virtual void notifyThumbnailChanged( int /*pageNumber*/ ) {};
virtual void notifyPixmapChanged( int /*pageNumber*/ ) {};
// commands from the Document to observers
virtual void pageSetup( const QValueList<int> & /*pages*/ ) {};
virtual void pageSetCurrent( int /*pageNumber*/, float /*position*/ ) {};
virtual void pageSetHilight( int /*x*/, int /*y*/, int /*width*/, int /*height*/ ) {};
};
/**
* @short The information container. Actions (like find) take place here.
*
* xxxxxx
* yyy.
*/
class KPDFDocument : public QObject
{
Q_OBJECT
public:
KPDFDocument();
~KPDFDocument();
// observers related methods
void addObserver( KPDFDocumentObserver * pObserver );
// document handling
bool openFile( const QString & docFile );
void close();
// document control
const KPDFPage * page( uint page ) const;
uint pages() const;
uint currentPage() const { return 0; }
void setCurrentPage( uint page, float position = 0.0 );
void find( bool nextMatch, const QString & text = "" );
void goToLink( /* QString anchor */ );
bool atBegin() {return false;}
bool atEnd() {return false;}
void setZoom( float zoom ) {};
void zoom( float offset ) {};
signals:
void pageChanged();
private:
void sendFilteredPageList();
void deletePages();
class KPDFDocumentPrivate * d;
};
#endif
......@@ -43,11 +43,12 @@ namespace KPDF
paperColor.rgb8 = splashMakeRGB8(0xff, 0xff, 0xff);
m_outputdev = new QOutputDev(paperColor);
setFocusPolicy( QWidget::StrongFocus );
setAcceptDrops( true );
viewport()->setFocusPolicy( QWidget::WheelFocus );
QPushButton * w = new QPushButton( viewport() );
w->setPixmap( SmallIcon("up") );
setCornerWidget( w );
QPushButton * w = new QPushButton( viewport() );
w->setPixmap( SmallIcon("up") );
setCornerWidget( w );
}
PageWidget::~PageWidget()
{
......@@ -306,12 +307,10 @@ namespace KPDF
if ( m_doc )
{
// Pixels per point when the zoomFactor is 1.
const float basePpp = QPaintDevice::x11AppDpiX() / 72.0;
const float ppp = basePpp * m_zoomFactor; // pixels per point
const float ppp = (float)QPaintDevice::x11AppDpiX() * m_zoomFactor; // pixels per point
m_docMutex->lock();
m_doc->displayPage(m_outputdev, m_currentPage, ppp * 72.0, ppp * 72.0, 0, true, true);
m_doc->displayPage(m_outputdev, m_currentPage, ppp, ppp, 0, true, true);
m_docMutex->unlock();
resizeContents ( m_outputdev->getImage().width ( ), m_outputdev->getImage().height ( ));
......
......@@ -16,6 +16,7 @@
#define _KPDF_PAGEWIDGET_H_
#include <qscrollview.h>
#include "document.h"
#include "CharTypes.h"
......@@ -34,7 +35,7 @@ namespace KPDF
/**
* Widget displaying a pixmap containing a PDF page and Links.
*/
class PageWidget : public QScrollView
class PageWidget : public QScrollView, public KPDFDocumentObserver
{
Q_OBJECT
......@@ -47,6 +48,16 @@ namespace KPDF
void setPixelsPerPoint(float);
/* void setLinks(); */
// inherited from KPDFDocumentObserver
void pageSetup( const QValueList<int> & /*pages*/ )
{/*
m_outputDev->setPDFDocument(d->pdfdoc);
*/}
void pageSetCurrent( int /*pageNumber*/, float /*position*/ )
{
//m_pageWidget->setPage(m_currentPage);
}
void setPage(int pagenum);
void enableScrollBars( bool b );
/**
......@@ -108,6 +119,40 @@ namespace KPDF
};
}
/*
ZoomMode m_zoomMode;
float m_zoomFactor;
// Do with these first. We can always add the other zoommodes which can
// be specified in a Destination later.
enum ZoomMode { FitInWindow, FitWidth, FitVisible, FixedFactor };
*/
/*
connect( m_pageWidget, SIGNAL( ReadUp() ), SLOT( slotReadUp() ));
connect( m_pageWidget, SIGNAL( ReadDown() ), SLOT( slotReadDown() ));
connect( m_pageWidget, SIGNAL( spacePressed() ), this, SLOT( slotReadDown() ) );
void Part::slotReadUp()
{
if( !m_doc )
return;
if( !m_pageWidget->readUp() ) {
if ( previousPage() )
m_pageWidget->scrollBottom();
}
}
void Part::slotReadDown()
{
if( !m_doc )
return;
if( !m_pageWidget->readDown() ) {
if ( nextPage() )
m_pageWidget->scrollTop();
}
}
*/
#endif
// vim:ts=2:sw=2:tw=78:et
This diff is collapsed.
......@@ -32,12 +32,13 @@ class KSelectAction;
class LinkAction;
class LinkDest;
class PDFDoc;
class XOutputDev;
class ThumbnailList;
class PDFPartView;
class KPDFDocument;
namespace KPDF
{
class BrowserExtension;
......@@ -49,131 +50,105 @@ namespace KPDF
*
* @short Main Part
* @author Wilco Greven <greven@kde.org>
* @version 0.1
* @version 0.2
*/
class Part : public KParts::ReadOnlyPart
{
Q_OBJECT
Q_OBJECT
public:
// Do with these first. We can always add the other zoommodes which can
// be specified in a Destination later.
enum ZoomMode { FitInWindow, FitWidth, FitVisible, FixedFactor };
/**
* Default constructor
*/
Part(QWidget* parentWidget, const char* widgetName,
QObject* parent, const char* name, const QStringList& args);
/**
* Destructor
*/
virtual ~Part();
static KAboutData* createAboutData();
/**
* Default constructor
*/
Part(QWidget* parentWidget, const char* widgetName,
QObject* parent, const char* name, const QStringList& args);
bool closeURL();
/**
* Destructor
*/
virtual ~Part();
void displayPage(int pageNumber, float zoomFactor = 1.0);
/*void displayDestination(LinkDest*);*/
void updateActionPage();
PageWidget* pageWidget() const {return m_outputDev;}
public slots:
void print();
static KAboutData* createAboutData();
signals:
void rightClick();
// reimplemented from KParts::ReadOnlyPart
bool closeURL();
void displayPage(int pageNumber ); //TODO REMOVE ME!
protected:
/**
* This must be implemented by each part
*/
virtual bool openFile();
void update();
void readSettings();
void writeSettings();
bool nextPage();
bool previousPage();
void updateAction();
void goToPage( int page );
void doPrint( KPrinter& printer );
// reimplemented from KParts::ReadOnlyPart
virtual bool openFile();
void readSettings();
void writeSettings();
void updateAction();
void doPrint( KPrinter& printer );
protected slots:
void find();
void findNext();
void zoomIn() { m_zoomFactor += 0.1; update(); };
void zoomOut() { m_zoomFactor -= 0.1; update(); };
void back() { /* stub */ };
void forward() { /* stub */ };
void slotNextPage();
void slotPreviousPage();
void slotGotoEnd();
void slotGotoStart();
void slotGoToPage();
void printPreview();
/*void executeAction(LinkAction*);*/
void showScrollBars( bool );
void showMarkList( bool );
void slotReadUp();
void slotReadDown();
void slotOpenUrlDropped( const KURL & );
void slotZoom( const QString& );
// connected to actions
void slotGoToPage();
void slotPreviousPage();
void slotNextPage();
void slotGotoStart();
void slotGotoEnd();
void slotFind();
void slotFindNext();
void slotZoom( const QString& );
void slotZoomIn();
void slotZoomOut();
void slotFitToWidthToggled( bool );
void slotPrintPreview();
void slotToggleScrollBars( bool );
void slotToggleThumbnails( bool );
void slotSaveFileAs();
// can be connected do widget elements
void updateActions();
public slots:
// connected to Shell action (and browserExtension), not local one
void slotPrint();
private:
void doFind(QString s, bool next);
ThumbnailList *m_thumbnailList;
// TODO rename to something else
PageWidget* m_outputDev;
PDFDoc* m_doc;
KAction* m_firstPage;
KAction* m_lastPage;
KAction* m_prevPage;
KAction* m_nextPage;
KAction *m_gotoPage;
KToggleAction* m_showScrollBars;
KToggleAction* m_showPageList;
KSelectAction *m_zoomTo;
KToggleAction* m_fitToWidth;
KAction *m_find;
KAction *m_findNext;
QString m_findText;
// first page is page 1
int m_currentPage;
QMutex m_docMutex;
ZoomMode m_zoomMode;
float m_zoomFactor;
static unsigned int m_count;
private slots:
void slotFitToWidthToggled();
void redrawPage();
void pageClicked ( int );
void fileSaveAs();
// PORT!! ###
// PDFDoc* m_doc;
QMutex m_docMutex; // REMOVE MEEE
// the document
KPDFDocument * document;
// main widgets
ThumbnailList *m_thumbnailList;
PageWidget *m_pageWidget;
// static instances counter
static unsigned int m_count;
// actions
KAction *m_gotoPage;
KAction *m_prevPage;
KAction *m_nextPage;
KAction *m_firstPage;
KAction *m_lastPage;
KToggleAction *m_showScrollBars;
KToggleAction *m_showPageList;
KSelectAction *m_zoomTo;
KToggleAction *m_fitToWidth;
KAction *m_find;
KAction *m_findNext;
};
class BrowserExtension : public KParts::BrowserExtension
{
Q_OBJECT
Q_OBJECT
public:
BrowserExtension(Part*);
BrowserExtension(Part*);
public slots:
// Automatically detected by the host.
void print();
// Automatically detected by the host.
void print();
};
}
......
......@@ -115,7 +115,7 @@ Shell::setupActions()
KStdAction::open(this, SLOT(fileOpen()), actionCollection());
recent = KStdAction::openRecent( this, SLOT( openURL( const KURL& ) ),
actionCollection() );
KStdAction::print(m_part, SLOT(print()), actionCollection());
KStdAction::print(m_part, SLOT(slotPrint()), actionCollection());
KStdAction::quit(this, SLOT(slotQuit()), actionCollection());
......
/***************************************************************************
* 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. *
***************************************************************************/
// qt includes
#include <qpixmap.h>
#include <qstring.h>
#include <qpainter.h>
// local includes
#include "TextOutputDev.h"
#include "page.h"
KPDFPage::KPDFPage( uint i, float w, float h )
: m_number( i ), m_width( w ), m_height( h ), m_zoom( 1 ),
m_pixmap( 0 ), m_thumbnail( 0 ), m_text( 0 ), m_overlay( 0 )
{
printf( "hello %d\n", i );
}
KPDFPage::~KPDFPage()
{
printf( "bye[%d] ", m_number );
delete m_pixmap;
delete m_thumbnail;
delete m_text;
delete m_overlay;
}
/** DRAWING **/
void KPDFPage::drawPixmap( QPainter * p, const QRect & limits ) // MUTEXED