Commit b1ad685b authored by Enrico Ros's avatar Enrico Ros
Browse files

Small API fixes. Preparing for mem-manager / async-gen. Removed KPDFDocument

dependancy from QObject. Kpdf_Part is now a Document Observer (only monitors
page changes to update actions).
Modified Accessibility(Recolor) settings and config dialog, fixed a bug,
page color change on-the-fly (pixmap refresh mechanism to add..).

svn path=/branches/kpdf_experiments/kdegraphics/kpdf/; revision=361759
parent 49438a20
This diff is collapsed.
......@@ -11,17 +11,19 @@
<entry key="HighlightLinks" type="Bool" >
<default>false</default>
</entry>
<entry key="ChangeColors" type="Bool" >
<default>false</default>
</entry>
<entry key="RenderMode" type="Enum" >
<default>Normal</default>
<default>Inverted</default>
<choices>
<choice name="Normal" />
<choice name="pad_paper_color" />
<choice name="Inverted" />
<choice name="Paper" />
<choice name="pad_paper_color" />
<choice name="Recolor" />
<choice name="pad_fg_color" />
<choice name="pad_bg_color" />
<choice name="BlackWhite" />
<choice name="pad_contrast" />
</choices>
</entry>
<entry key="RecolorForeground" type="Color" >
......
......@@ -57,6 +57,7 @@ public:
int currentPage;
QRect currentViewport;
QValueVector< KPDFPage* > pages;
QColor paperColor;
// find related
QString searchText;
......@@ -84,12 +85,9 @@ KPDFDocument::KPDFDocument()
d->pdfdoc = 0;
d->currentPage = -1;
d->searchPage = -1;
// load paper color from Settings
QColor col = Settings::paperColor();
SplashColor paperColor;
paperColor.rgb8 = splashMakeRGB8( col.red(), col.green(), col.blue() );
// create the output device
d->kpdfOutputDev = new KPDFOutputDev( paperColor );
d->kpdfOutputDev = 0;
// create the outputdev
reparseConfig();
}
KPDFDocument::~KPDFDocument()
......@@ -179,7 +177,7 @@ bool KPDFDocument::openDocument( const QString & docFile )
d->pages[i] = new KPDFPage( i, d->pdfdoc->getPageWidth(i+1), d->pdfdoc->getPageHeight(i+1), d->pdfdoc->getPageRotate(i+1) );
// filter pages, setup observers and set the first page as current
processPageList( true );
slotSetCurrentPage( 0 );
setCurrentPage( 0 );
}
// check local directory for an overlay xml
......@@ -212,29 +210,47 @@ void KPDFDocument::closeDocument()
}
uint KPDFDocument::currentPage() const
void KPDFDocument::addObserver( KPDFDocumentObserver * pObserver )
{
return d->currentPage;
d->observers[ pObserver->observerId() ] = pObserver;
}
uint KPDFDocument::pages() const
void KPDFDocument::reparseConfig()
{
return d->pdfdoc ? d->pdfdoc->getNumPages() : 0;
// load paper color from Settings or use the white default color
QColor color = ( (Settings::renderMode() == Settings::EnumRenderMode::Paper ) &&
Settings::changeColors() ) ? Settings::paperColor() : Qt::white;
if ( color != d->paperColor || !d->kpdfOutputDev )
{
d->paperColor = color;
SplashColor paperColor;
paperColor.rgb8 = splashMakeRGB8( d->paperColor.red(), d->paperColor.green(), d->paperColor.blue() );
// rebuild the output device using the new paper color
d->docLock.lock();
delete d->kpdfOutputDev;
d->kpdfOutputDev = new KPDFOutputDev( paperColor );
if ( d->pdfdoc )
d->kpdfOutputDev->startDoc( d->pdfdoc->getXRef() );
d->docLock.unlock();
// invalidate pixmaps
//FIXME missing
}
}
bool KPDFDocument::atBegin() const
uint KPDFDocument::currentPage() const
{
return d->currentPage < 1;
return d->currentPage;
}
bool KPDFDocument::atEnd() const
uint KPDFDocument::pages() const
{
return d->currentPage >= ((int)d->pages.count() - 1);
return d->pdfdoc ? d->pdfdoc->getNumPages() : 0;
}
const KPDFPage * KPDFDocument::page( uint n ) const
bool KPDFDocument::okToPrint() const
{
return ( n < d->pages.count() ) ? d->pages[n] : 0;
return d->pdfdoc->okToPrint();
}
Outline * KPDFDocument::outline() const
......@@ -242,12 +258,12 @@ Outline * KPDFDocument::outline() const
return d->pdfdoc ? d->pdfdoc->getOutline() : 0;
}
void KPDFDocument::addObserver( KPDFDocumentObserver * pObserver )
const KPDFPage * KPDFDocument::page( uint n ) const
{
d->observers[ pObserver->observerId() ] = pObserver;
return ( n < d->pages.count() ) ? d->pages[n] : 0;
}
void KPDFDocument::requestPixmap( int id, uint page, int width, int height, bool syn )
{
KPDFPage * kp = d->pages[page];
......@@ -305,51 +321,7 @@ void KPDFDocument::requestTextPage( uint n )
page->setSearchPage( td.takeTextPage() );
}
bool KPDFDocument::okToPrint() const
{
return d->pdfdoc->okToPrint();
}
bool KPDFDocument::print(KPrinter &printer)
{
KTempFile tf( QString::null, ".ps" );
PSOutputDev *psOut = new PSOutputDev(tf.name().latin1(), d->pdfdoc->getXRef(), d->pdfdoc->getCatalog(), 1, d->pdfdoc->getNumPages(), psModePS);
if (psOut->isOk())
{
std::list<int> pages;
if (!printer.previewOnly())
{
QValueList<int> pageList = printer.pageList();
QValueList<int>::const_iterator it;
for(it = pageList.begin(); it != pageList.end(); ++it) pages.push_back(*it);
}
else
{
for(int i = 1; i <= d->pdfdoc->getNumPages(); i++) pages.push_back(i);
}
d->docLock.lock();
d->pdfdoc->displayPages(psOut, pages, 72, 72, 0, globalParams->getPSCrop(), gFalse);
d->docLock.unlock();
// needs to be here so that the file is flushed, do not merge with the one
// in the else
delete psOut;
printer.printFiles(tf.name(), true);
return true;
}
else
{
delete psOut;
return false;
}
}
// BEGIN slots
void KPDFDocument::slotSetCurrentPage( int page, const QRect & viewport )
void KPDFDocument::setCurrentPage( int page, const QRect & viewport )
{
if ( page < 0 )
page = 0;
......@@ -360,27 +332,9 @@ void KPDFDocument::slotSetCurrentPage( int page, const QRect & viewport )
d->currentPage = page;
d->currentViewport = viewport;
foreachObserver( pageSetCurrent( page, viewport ) );
pageChanged();
}
void KPDFDocument::slotSetFilter( const QString & pattern, bool keepCase )
{
d->filterText = pattern;
d->filterCase = keepCase;
processPageList( false );
}
void KPDFDocument::slotToggleBookmark( int n )
{
KPDFPage * page = ( n < (int)d->pages.count() ) ? d->pages[ n ] : 0;
if ( page )
{
page->toggleAttribute( KPDFPage::Bookmark );
foreachObserver( notifyPixmapChanged( n ) );
}
}
void KPDFDocument::slotFind( const QString & string, bool keepCase )
void KPDFDocument::findText( const QString & string, bool keepCase )
{
// turn selection drawing off on filtered pages
if ( !d->filterText.isEmpty() )
......@@ -435,14 +389,31 @@ void KPDFDocument::slotFind( const QString & string, bool keepCase )
int pageNumber = foundPage->number();
d->searchPage = pageNumber;
foundPage->setAttribute( KPDFPage::Highlight );
slotSetCurrentPage( pageNumber );
setCurrentPage( pageNumber );
foreachObserver( notifyPixmapChanged( pageNumber ) );
}
else
KMessageBox::information( 0, i18n("No matches found for '%1'.").arg(d->searchText) );
}
void KPDFDocument::slotProcessLink( const KPDFLink * link )
void KPDFDocument::findTextAll( const QString & pattern, bool keepCase )
{
d->filterText = pattern;
d->filterCase = keepCase;
processPageList( false );
}
void KPDFDocument::toggleBookmark( int n )
{
KPDFPage * page = ( n < (int)d->pages.count() ) ? d->pages[ n ] : 0;
if ( page )
{
page->toggleAttribute( KPDFPage::Bookmark );
foreachObserver( notifyPixmapChanged( n ) );
}
}
void KPDFDocument::processLink( const KPDFLink * link )
{
if ( !link )
return;
......@@ -512,7 +483,7 @@ void KPDFDocument::slotProcessLink( const KPDFLink * link )
destFitR
read and fit left,bottom,right,top
}*/
slotSetCurrentPage( pageNum, r );
setCurrentPage( pageNum, r );
}
delete namedDest;
delete dest;
......@@ -571,13 +542,13 @@ void KPDFDocument::slotProcessLink( const KPDFLink * link )
case KPDFLink::Named: {
const char * name = link->getName();
if ( !strcmp( name, "NextPage" ) && (d->currentPage < (int)d->pages.count() - 1) )
slotSetCurrentPage( d->currentPage + 1 );
setCurrentPage( d->currentPage + 1 );
else if ( !strcmp( name, "PrevPage" ) && d->currentPage > 0 )
slotSetCurrentPage( d->currentPage - 1 );
setCurrentPage( d->currentPage - 1 );
else if ( !strcmp( name, "FirstPage" ) )
slotSetCurrentPage( 0 );
setCurrentPage( 0 );
else if ( !strcmp( name, "LastPage" ) )
slotSetCurrentPage( d->pages.count() - 1 );
setCurrentPage( d->pages.count() - 1 );
else if ( !strcmp( name, "GoBack" ) )
{} //TODO
else if ( !strcmp( name, "GoForward" ) )
......@@ -601,6 +572,44 @@ void KPDFDocument::slotProcessLink( const KPDFLink * link )
break;
}
}
bool KPDFDocument::print(KPrinter &printer)
{
KTempFile tf( QString::null, ".ps" );
PSOutputDev *psOut = new PSOutputDev(tf.name().latin1(), d->pdfdoc->getXRef(), d->pdfdoc->getCatalog(), 1, d->pdfdoc->getNumPages(), psModePS);
if (psOut->isOk())
{
std::list<int> pages;
if (!printer.previewOnly())
{
QValueList<int> pageList = printer.pageList();
QValueList<int>::const_iterator it;
for(it = pageList.begin(); it != pageList.end(); ++it) pages.push_back(*it);
}
else
{
for(int i = 1; i <= d->pdfdoc->getNumPages(); i++) pages.push_back(i);
}
d->docLock.lock();
d->pdfdoc->displayPages(psOut, pages, 72, 72, 0, globalParams->getPSCrop(), gFalse);
d->docLock.unlock();
// needs to be here so that the file is flushed, do not merge with the one
// in the else
delete psOut;
printer.printFiles(tf.name(), true);
return true;
}
else
{
delete psOut;
return false;
}
}
//END slots
QString KPDFDocument::giveAbsolutePath( const QString & fileName )
......
......@@ -15,9 +15,7 @@
#include <qvaluevector.h>
class KPrinter;
class Outline;
class KPDFPage;
class KPDFLink;
......@@ -29,21 +27,22 @@ class KPDFLink;
*/
class KPDFDocumentObserver
{
public:
// you must give each observer a unique ID (used for notifications)
virtual uint observerId() const = 0;
public:
// you must give each observer a unique ID (used for notifications)
virtual uint observerId() const = 0;
// monitor changes in pixmaps (generation thread complete)
virtual void notifyPixmapChanged( int /*pageNumber*/ ) {};
// monitor changes in pixmaps (generation thread complete)
virtual void notifyPixmapChanged( int /*pageNumber*/ ) {};
// commands from the Document to all observers
virtual void pageSetup( const QValueVector<KPDFPage*> & /*pages*/, bool /*documentChanged*/ ) {};
virtual void pageSetCurrent( int /*pageNumber*/, const QRect & /*viewport*/ = QRect() ) {};
// commands from the Document to all observers
virtual void pageSetup( const QValueVector<KPDFPage*> & /*pages*/, bool /*documentChanged*/ ) {};
virtual void pageSetCurrent( int /*pageNumber*/, const QRect & /*viewport*/ = QRect() ) {};
};
#define PAGEVIEW_ID 1
#define THUMBNAILS_ID 2
#define TOC_ID 3
#define PART_ID 1
#define PAGEVIEW_ID 2
#define THUMBNAILS_ID 3
#define TOC_ID 4
/**
* @short The information container. Actions (like open,find) take place here.
......@@ -51,54 +50,44 @@ public:
* xxxxxx
* yyy.
*/
class KPDFDocument : public QObject
class KPDFDocument
{
Q_OBJECT
public:
KPDFDocument();
~KPDFDocument();
// document handling
bool openDocument( const QString & docFile );
void closeDocument();
// query methods
uint currentPage() const;
uint pages() const;
bool atBegin() const;
bool atEnd() const;
const KPDFPage * page( uint page ) const;
Outline * outline() const;
// observers related methods
void addObserver( KPDFDocumentObserver * pObserver );
void requestPixmap( int id, uint page, int width, int height, bool syncronous = false );
void requestTextPage( uint page );
bool okToPrint() const;
bool print(KPrinter &printer);
//public slots: TODO remove me
// document commands via slots
void slotSetCurrentPage( int page, const QRect & viewport = QRect() );
void slotSetFilter( const QString & pattern, bool caseSensitive );
void slotToggleBookmark( int page );
void slotFind( const QString & text = "", bool caseSensitive = false );
void slotProcessLink( const KPDFLink * link );
signals:
// notify changes via signals
void pageChanged();
private:
QString giveAbsolutePath( const QString & fileName );
bool openRelativeFile( const QString & fileName );
void processPageList( bool documentChanged );
void unHilightPages();
class KPDFDocumentPrivate * d;
public:
KPDFDocument();
~KPDFDocument();
// document handling
bool openDocument( const QString & docFile );
void closeDocument();
// misc methods
void addObserver( KPDFDocumentObserver * pObserver );
void reparseConfig();
// query methods (const ones)
uint currentPage() const;
uint pages() const;
bool okToPrint() const;
Outline * outline() const;
const KPDFPage * page( uint page ) const;
// perform actions on document / pages
void requestPixmap( int id, uint page, int width, int height, bool syncronous = false );
void requestTextPage( uint page );
void setCurrentPage( int page, const QRect & viewport = QRect() );
void findText( const QString & text = "", bool caseSensitive = false );
void findTextAll( const QString & pattern, bool caseSensitive );
void toggleBookmark( int page );
void processLink( const KPDFLink * link );
bool print( KPrinter &printer );
private:
QString giveAbsolutePath( const QString & fileName );
bool openRelativeFile( const QString & fileName );
void processPageList( bool documentChanged );
void unHilightPages();
class KPDFDocumentPrivate * d;
};
#endif
......@@ -84,14 +84,13 @@ Part::Part(QWidget *parentWidget, const char *widgetName,
setInstance(KPDFPartFactory::instance());
// build the document
document = new KPDFDocument();
connect( document, SIGNAL( pageChanged() ), this, SLOT( updateActions() ) );
m_document = new KPDFDocument();
// widgets: ^searchbar (toolbar containing label and SearchWidget)
// m_searchToolBar = new KToolBar( parentWidget, "searchBar" );
// m_searchToolBar->boxLayout()->setSpacing( KDialog::spacingHint() );
// QLabel * sLabel = new QLabel( i18n( "&Search:" ), m_searchToolBar, "kde toolbar widget" );
// m_searchWidget = new SearchWidget( m_searchToolBar, document );
// m_searchWidget = new SearchWidget( m_searchToolBar, m_document );
// sLabel->setBuddy( m_searchWidget );
// m_searchToolBar->setStretchableWidget( m_searchWidget );
......@@ -105,14 +104,14 @@ Part::Part(QWidget *parentWidget, const char *widgetName,
m_toolBox->setMinimumWidth( 60 );
m_toolBox->setMaximumWidth( 200 );
TOC * tocFrame = new TOC( m_toolBox, document );
TOC * tocFrame = new TOC( m_toolBox, m_document );
m_toolBox->addItem( tocFrame, QIconSet(SmallIcon("text_left")), i18n("Contents") );
connect(tocFrame, SIGNAL(hasTOC(bool)), this, SLOT(enableTOC(bool)));
enableTOC( false );
QVBox * thumbsBox = new ThumbnailsBox( m_toolBox );
m_searchWidget = new SearchWidget( thumbsBox, document );
m_thumbnailList = new ThumbnailList( thumbsBox, document );
m_searchWidget = new SearchWidget( thumbsBox, m_document );
m_thumbnailList = new ThumbnailList( thumbsBox, m_document );
connect( m_thumbnailList, SIGNAL( urlDropped( const KURL& ) ), SLOT( openURL( const KURL & )));
m_toolBox->addItem( thumbsBox, QIconSet(SmallIcon("thumbnail")), i18n("Thumbnails") );
m_toolBox->setCurrentItem( thumbsBox );
......@@ -126,14 +125,15 @@ Part::Part(QWidget *parentWidget, const char *widgetName,
m_toolBox->setItemEnabled( iIdx, false );
// widgets: [] | [right 'pageView']
m_pageView = new PageView( m_splitter, document );
m_pageView = new PageView( m_splitter, m_document );
connect( m_pageView, SIGNAL( urlDropped( const KURL& ) ), SLOT( openURL( const KURL & )));
//connect(m_pageView, SIGNAL( rightClick() ), this, SIGNAL( rightClick() ));
// add document observers
document->addObserver( m_thumbnailList );
document->addObserver( m_pageView );
document->addObserver( tocFrame );
m_document->addObserver( this );
m_document->addObserver( m_thumbnailList );
m_document->addObserver( m_pageView );
m_document->addObserver( tocFrame );
// ACTIONS
KActionCollection * ac = actionCollection();
......@@ -186,15 +186,21 @@ Part::~Part()
// write to disk config file
Settings::writeConfig();
delete document;
delete m_document;
if ( --m_count == 0 )
delete globalParams;
}
void Part::pageSetCurrent( int, const QRect & )
{
// document tells that page has changed, so update actions
updateActions();
}
void Part::goToPage(uint i)
{
if (i <= document->pages())
document->slotSetCurrentPage( i - 1 );
if ( i <= m_document->pages() )
m_document->setCurrentPage( i - 1 );
}
void Part::openDocument(KURL doc)
......@@ -204,7 +210,7 @@ void Part::openDocument(KURL doc)
uint Part::pages()
{
return document->pages();
return m_document->pages();
}
//this don't go anywhere but is required by genericfactory.h
......@@ -220,7 +226,7 @@ KAboutData* Part::createAboutData()
bool Part::openFile()
{
bool ok = document->openDocument( m_file );
bool ok = m_document->openDocument( m_file );
m_find->setEnabled( ok );
return ok;
}
......@@ -234,28 +240,30 @@ bool Part::openURL(const KURL &url)
bool Part::closeURL()
{
document->closeDocument();
m_document->closeDocument();
return KParts::ReadOnlyPart::closeURL();
}
void Part::updateActions()
{
if ( document->pages() > 0 )
{
m_gotoPage->setEnabled(document->pages()>1);
m_firstPage->setEnabled(!document->atBegin());
m_prevPage->setEnabled(!document->atBegin());
m_lastPage->setEnabled(!document->atEnd());
m_nextPage->setEnabled(!document->atEnd());
}
else
{
m_gotoPage->setEnabled(false);
m_firstPage->setEnabled(false);
m_lastPage->setEnabled(false);
m_prevPage->setEnabled(false);
m_nextPage->setEnabled(false);
}
if ( m_document->pages() > 0 )
{
bool atBegin = m_document->currentPage() < 1;
bool atEnd = m_document->currentPage() >= (m_document->pages() - 1);
m_gotoPage->setEnabled( m_document->pages() > 1 );
m_firstPage->setEnabled( !atBegin );
m_prevPage->setEnabled( !atBegin );
m_lastPage->setEnabled( !atEnd );
m_nextPage->setEnabled( !atEnd );
}
else
{
m_gotoPage->setEnabled( false );
m_firstPage->setEnabled( false );
m_lastPage->setEnabled( false );
m_prevPage->setEnabled( false );
m_nextPage->setEnabled( false );
}
}
void Part::enableTOC(bool enable)
......@@ -295,52 +303,52 @@ public:
void Part::slotGoToPage()
{
KPDFGotoPageDialog pageDialog( m_pageView, document->currentPage() + 1, document->pages() );
if ( pageDialog.exec() == QDialog::Accepted )
document->slotSetCurrentPage( pageDialog.getPage() - 1 );
KPDFGotoPageDialog pageDialog( m_pageView, m_document->currentPage() + 1, m_document->pages() );
if ( pageDialog.exec() == QDialog::Accepted )
m_document->setCurrentPage( pageDialog.getPage() - 1 );
}
void Part::slotPreviousPage()
{
if ( !document->atBegin() )
document->slotSetCurrentPage( document->currentPage() - 1 );
if ( !m_document->currentPage() < 1 )
m_document->setCurrentPage( m_document->currentPage() - 1 );
}
void Part::slotNextPage()
{
if ( !document->