2000-11-29 12:17:44 +00:00
# include <config.h>
2000-07-16 00:20:08 +00:00
# include <kaction.h>
2000-07-05 10:44:59 +00:00
# include <kaboutdata.h>
# include <kaboutdialog.h>
# include <kapp.h>
# include <kbugreport.h>
# include <kconfig.h>
2000-05-15 04:53:53 +00:00
# include <klocale.h>
# include <kdebug.h>
2000-05-17 10:09:01 +00:00
# include <kglobal.h>
2000-07-05 10:44:59 +00:00
# include <kimageeffect.h>
# include <kinstance.h>
2000-11-29 12:17:44 +00:00
# include <qobject.h>
# include <qlabel.h>
# include <qstring.h>
# include <qscrollview.h>
# include <qimage.h>
# include <qpixmap.h>
2000-05-15 04:53:53 +00:00
2000-11-29 12:17:44 +00:00
# include "fontpool.h"
2000-05-15 04:53:53 +00:00
# include "kdvi_multipage.moc"
2000-11-03 14:38:50 +00:00
# include "kviewpart.h"
2000-11-29 12:17:44 +00:00
# include "optiondialog.h"
# include "print.h"
2000-05-15 04:53:53 +00:00
extern " C "
{
2000-06-04 15:55:22 +00:00
void * init_libkdvi ( )
2000-05-15 04:53:53 +00:00
{
return new KDVIMultiPageFactory ;
}
} ;
2000-06-27 09:14:42 +00:00
2000-05-15 04:53:53 +00:00
KInstance * KDVIMultiPageFactory : : s_instance = 0L ;
2000-06-27 09:14:42 +00:00
2000-05-15 04:53:53 +00:00
KDVIMultiPageFactory : : KDVIMultiPageFactory ( )
{
}
2000-06-27 09:14:42 +00:00
2000-05-15 04:53:53 +00:00
KDVIMultiPageFactory : : ~ KDVIMultiPageFactory ( )
{
if ( s_instance )
delete s_instance ;
2000-06-27 09:14:42 +00:00
2000-05-15 04:53:53 +00:00
s_instance = 0 ;
}
2000-06-27 09:14:42 +00:00
KParts : : Part * KDVIMultiPageFactory : : createPart ( QWidget * parentWidget , const char * widgetName , QObject * parent , const char * name , const char * , const QStringList & )
2000-05-15 04:53:53 +00:00
{
2000-06-27 09:14:42 +00:00
KParts : : Part * obj = new KDVIMultiPage ( parentWidget , widgetName , parent , name ) ;
2000-05-15 04:53:53 +00:00
emit objectCreated ( obj ) ;
return obj ;
}
2000-06-27 09:14:42 +00:00
2000-05-15 04:53:53 +00:00
KInstance * KDVIMultiPageFactory : : instance ( )
{
if ( ! s_instance )
s_instance = new KInstance ( " kdvi " ) ;
return s_instance ;
2000-06-27 09:14:42 +00:00
}
2000-05-15 04:53:53 +00:00
2000-06-27 09:14:42 +00:00
KDVIMultiPage : : KDVIMultiPage ( QWidget * parentWidget , const char * widgetName , QObject * parent , const char * name )
: KMultiPage ( parentWidget , widgetName , parent , name ) , window ( 0 ) , options ( 0 )
2000-05-15 04:53:53 +00:00
{
2000-06-30 06:41:47 +00:00
timer_id = - 1 ;
setInstance ( KDVIMultiPageFactory : : instance ( ) ) ;
2000-05-15 04:53:53 +00:00
2000-11-29 12:17:44 +00:00
window = new dviWindow ( 1.0 , true , scrollView ( ) ) ;
2000-05-17 10:09:01 +00:00
preferencesChanged ( ) ;
2000-11-03 14:38:50 +00:00
new KAction ( i18n ( " &DVI Options " ) , 0 , this ,
2000-06-11 07:04:57 +00:00
SLOT ( doSettings ( ) ) , actionCollection ( ) ,
" settings_dvi " ) ;
2000-06-27 09:14:42 +00:00
2000-07-05 10:44:59 +00:00
new KAction ( i18n ( " About the KDVI plugin... " ) , 0 , this ,
2000-06-30 06:41:47 +00:00
SLOT ( about ( ) ) , actionCollection ( ) ,
" about_kdvi " ) ;
2000-07-05 10:44:59 +00:00
new KAction ( i18n ( " Help on the KDVI plugin... " ) , 0 , this ,
2000-06-30 06:41:47 +00:00
SLOT ( helpme ( ) ) , actionCollection ( ) ,
" help_dvi " ) ;
2000-07-05 10:44:59 +00:00
new KAction ( i18n ( " Report Bug in the KDVI plugin... " ) , 0 , this ,
SLOT ( bugform ( ) ) , actionCollection ( ) ,
" bug_dvi " ) ;
2000-05-17 10:09:01 +00:00
setXMLFile ( " kdvi_part.rc " ) ;
2000-05-15 04:53:53 +00:00
scrollView ( ) - > addChild ( window ) ;
2000-06-11 07:04:57 +00:00
connect ( window , SIGNAL ( request_goto_page ( int , int ) ) , this , SLOT ( goto_page ( int , int ) ) ) ;
2000-05-22 11:58:18 +00:00
readSettings ( ) ;
2000-05-15 04:53:53 +00:00
}
KDVIMultiPage : : ~ KDVIMultiPage ( )
{
2000-06-30 06:41:47 +00:00
if ( timer_id ! = - 1 )
killTimer ( timer_id ) ;
timer_id = - 1 ;
2000-05-22 11:58:18 +00:00
writeSettings ( ) ;
2000-05-15 04:53:53 +00:00
}
bool KDVIMultiPage : : openFile ( )
{
window - > setFile ( m_file ) ;
2000-06-30 06:41:47 +00:00
window - > gotoPage ( 1 ) ;
window - > changePageSize ( ) ; // This also calles drawPage();
2000-05-15 04:53:53 +00:00
emit numberOfPages ( window - > totalPages ( ) ) ;
scrollView ( ) - > resizeContents ( window - > width ( ) , window - > height ( ) ) ;
2000-05-22 08:00:41 +00:00
emit previewChanged ( true ) ;
2000-08-11 15:31:46 +00:00
return true ; //@@@ THIS IS WRONG!! Really, we should return something meaningful here.
2000-05-15 04:53:53 +00:00
}
2000-05-17 08:50:57 +00:00
bool KDVIMultiPage : : closeURL ( )
{
2000-07-25 08:52:17 +00:00
window - > setFile ( " " ) ; // That means: close the file. Resize the widget to 0x0.
2000-05-22 08:00:41 +00:00
emit previewChanged ( false ) ;
2000-05-17 08:50:57 +00:00
return true ;
}
2000-05-15 04:53:53 +00:00
QStringList KDVIMultiPage : : fileFormats ( )
{
QStringList r ;
2000-05-19 08:41:17 +00:00
r < < i18n ( " *.dvi|DVI files (*.dvi) " ) ;
2000-05-15 04:53:53 +00:00
return r ;
}
bool KDVIMultiPage : : gotoPage ( int page )
{
2000-05-17 08:50:57 +00:00
window - > gotoPage ( page + 1 ) ;
2000-05-22 08:00:41 +00:00
emit previewChanged ( true ) ;
2000-05-15 04:53:53 +00:00
return true ;
}
2000-06-11 07:04:57 +00:00
void KDVIMultiPage : : goto_page ( int page , int y )
{
window - > gotoPage ( page + 1 ) ;
2000-07-18 11:38:27 +00:00
scrollView ( ) - > ensureVisible ( scrollView ( ) - > width ( ) / 2 , y ) ;
2000-06-11 07:04:57 +00:00
emit previewChanged ( true ) ;
2000-07-08 11:58:30 +00:00
emit pageInfo ( window - > totalPages ( ) , page ) ;
2000-06-11 07:04:57 +00:00
}
2000-05-15 04:53:53 +00:00
2000-05-22 08:21:38 +00:00
double KDVIMultiPage : : setZoom ( double zoom )
2000-05-15 04:53:53 +00:00
{
2000-11-03 14:38:50 +00:00
if ( zoom < KViewPart : : minZoom / 1000.0 )
zoom = KViewPart : : minZoom / 1000.0 ;
if ( zoom > KViewPart : : maxZoom / 1000.0 )
zoom = KViewPart : : maxZoom / 1000.0 ;
double z = window - > setZoom ( zoom ) ;
2000-05-15 04:53:53 +00:00
scrollView ( ) - > resizeContents ( window - > width ( ) , window - > height ( ) ) ;
2000-05-22 08:21:38 +00:00
2000-11-03 14:38:50 +00:00
return z ;
2000-05-15 04:53:53 +00:00
}
2000-06-27 09:14:42 +00:00
extern unsigned int page_w , page_h ;
2000-05-15 14:53:59 +00:00
2000-05-15 04:53:53 +00:00
double KDVIMultiPage : : zoomForHeight ( int height )
{
2000-05-15 14:53:59 +00:00
return ( window - > zoom ( ) * ( double ) height ) / ( double ) page_h ;
2000-05-15 04:53:53 +00:00
}
double KDVIMultiPage : : zoomForWidth ( int width )
{
2000-05-15 14:53:59 +00:00
return ( window - > zoom ( ) * ( double ) width ) / ( double ) page_w ;
2000-05-15 04:53:53 +00:00
}
2000-05-16 11:18:25 +00:00
void KDVIMultiPage : : setPaperSize ( double w , double h )
{
window - > setPaper ( w , h ) ;
}
2000-05-16 14:13:41 +00:00
bool KDVIMultiPage : : preview ( QPainter * p , int w , int h )
{
2000-05-17 08:50:57 +00:00
QPixmap * map = window - > pix ( ) ;
if ( ! map )
2000-05-16 14:13:41 +00:00
return false ;
2000-05-17 08:50:57 +00:00
// TODO: use higher quality preview if anti-aliasing?
//p->drawImage(0, 0, window->pix()->convertToImage().smoothScale(w,h));
p - > scale ( ( double ) w / ( double ) map - > width ( ) , ( double ) h / ( double ) map - > height ( ) ) ;
p - > drawPixmap ( 0 , 0 , * map ) ;
2000-05-16 14:13:41 +00:00
return true ;
}
2000-05-17 10:09:01 +00:00
void KDVIMultiPage : : doSettings ( )
{
2000-06-30 06:41:47 +00:00
if ( options ) {
options - > show ( ) ;
return ;
}
2000-05-17 10:09:01 +00:00
options = new OptionDialog ( window ) ;
connect ( options , SIGNAL ( preferencesChanged ( ) ) , this , SLOT ( preferencesChanged ( ) ) ) ;
options - > show ( ) ;
}
2000-06-30 06:41:47 +00:00
void KDVIMultiPage : : about ( )
{
2000-07-05 10:44:59 +00:00
KAboutDialog * ab = new KAboutDialog ( KAboutDialog : : AbtAppStandard ,
i18n ( " the KDVI plugin " ) ,
KAboutDialog : : Close , KAboutDialog : : Close ) ;
2000-07-18 11:38:27 +00:00
ab - > setProduct ( " kdvi " , " 0.9 " , QString : : null , QString : : null ) ;
2000-07-05 10:44:59 +00:00
ab - > addTextPage ( i18n ( " About " ) ,
i18n ( " A previewer for Device Independent files (DVI files) produced "
" by the TeX typesetting system.<br> "
" Based on kdvi 0.4.3 and on xdvik, version 18f.<br><hr> "
" For latest information, visit "
" <a href= \" http://devel-home.kde.org/~kdvi \" >KDVI's Homepage</a>. " ) ,
true ) ;
ab - > addTextPage ( i18n ( " Authors " ) ,
i18n ( " Markku Hinhala<br> "
" Author of kdvi 0.4.3 "
" <hr> "
" Stefan Kebekus<br> "
" <a href= \" http://btm8x5.mat.uni-bayreuth.de/~kebekus \" > "
" http://btm8x5.mat.uni-bayreuth.de/~kebekus</a><br> "
" <a href= \" mailto:kebekus@kde.org \" >kebekus@kde.org</a><br> "
" Current maintainer of kdvi. Major rewrite of version 0.4.3. "
" Implementation of hyperlinks.<br> "
" <hr> "
" Nicolai Langfeldt<br> "
" Maintainer of xdvik "
" <hr> "
" Paul Vojta<br> "
" Author of xdvi<br> "
" <hr> "
" Many others. Really, lots of people who were involved in kdvi, xdvik and "
" xdvi. I apologize to those who I did not mention here. Please send me an "
" email if you think your name belongs here. " ) ,
true ) ;
2000-06-30 06:41:47 +00:00
ab - > show ( ) ;
}
2000-07-05 10:44:59 +00:00
void KDVIMultiPage : : bugform ( )
{
2000-08-06 10:25:11 +00:00
KAboutData * kab = new KAboutData ( " kdvi " , I18N_NOOP ( " KDVI " ) , " 0.9 " , 0 , 0 , 0 , 0 , 0 ) ;
2000-07-05 10:44:59 +00:00
KBugReport * kbr = new KBugReport ( 0 , true , kab ) ;
kbr - > show ( ) ;
}
2000-06-30 06:41:47 +00:00
void KDVIMultiPage : : helpme ( )
{
kapp - > invokeHelp ( " " , " kdvi " ) ;
}
2000-05-17 10:09:01 +00:00
void KDVIMultiPage : : preferencesChanged ( )
{
2000-06-30 06:41:47 +00:00
# ifdef DEBUG
2000-07-05 10:44:59 +00:00
kdDebug ( 4300 ) < < " preferencesChanged " < < endl ;
2000-06-30 06:41:47 +00:00
# endif
2000-06-14 04:15:22 +00:00
2000-05-22 11:58:18 +00:00
KConfig * config = instance ( ) - > config ( ) ;
2000-05-17 10:09:01 +00:00
QString s ;
2000-06-27 09:14:42 +00:00
2000-06-14 04:15:22 +00:00
config - > reparseConfiguration ( ) ;
2000-05-17 10:09:01 +00:00
config - > setGroup ( " kdvi " ) ;
2000-06-27 09:14:42 +00:00
2000-11-29 12:17:44 +00:00
int mfmode = config - > readNumEntry ( " MetafontMode " , DefaultMFMode ) ;
if ( ( mfmode < 0 ) | | ( mfmode > = NumberOfMFModes ) )
config - > writeEntry ( " MetafontMode " , mfmode = DefaultMFMode ) ;
2000-11-25 15:31:29 +00:00
window - > setMetafontMode ( mfmode ) ;
2000-06-27 09:14:42 +00:00
2000-05-17 10:09:01 +00:00
int makepk = config - > readNumEntry ( " MakePK " ) ;
if ( makepk ! = window - > makePK ( ) )
2000-06-27 09:14:42 +00:00
window - > setMakePK ( makepk ) ;
2000-07-11 11:00:35 +00:00
int showPS = config - > readNumEntry ( " ShowPS " , 1 ) ;
2000-05-17 10:09:01 +00:00
if ( showPS ! = window - > showPS ( ) )
window - > setShowPS ( showPS ) ;
2000-06-14 04:15:22 +00:00
2000-07-11 11:00:35 +00:00
int showHyperLinks = config - > readNumEntry ( " ShowHyperLinks " , 1 ) ;
2000-06-14 04:15:22 +00:00
if ( showHyperLinks ! = window - > showHyperLinks ( ) )
window - > setShowHyperLinks ( showHyperLinks ) ;
2000-05-17 10:09:01 +00:00
}
2000-05-18 10:51:08 +00:00
2000-08-06 10:25:11 +00:00
bool KDVIMultiPage : : print ( const QStringList & pages , int current )
2000-05-18 10:51:08 +00:00
{
Print * printdlg = new Print ( window , " printdlg " ) ;
2000-06-27 09:14:42 +00:00
2000-05-18 10:51:08 +00:00
printdlg - > setFile ( m_file ) ;
2000-05-18 10:58:41 +00:00
printdlg - > setCurrentPage ( current + 1 , window - > totalPages ( ) ) ;
2000-08-06 10:25:11 +00:00
printdlg - > setMarkList ( pages ) ;
2000-05-18 10:51:08 +00:00
printdlg - > exec ( ) ;
2000-06-27 09:14:42 +00:00
delete printdlg ;
2000-05-18 10:58:41 +00:00
return true ;
2000-05-18 10:51:08 +00:00
}
2000-06-01 10:27:41 +00:00
2000-06-30 06:41:47 +00:00
// Explanation of the timerEvent.
//
// This is a dreadful hack. The problem we adress with this timer
// event is the following: the kviewshell has a KDirWatch object which
// looks at the DVI file and calls reload() when the object has
// changed. That works very nicely in principle, but in practise, when
// TeX runs for several seconds over a complicated file, this does not
// work at all. First, most of the time, while TeX is still writing,
// the file is invalid. Thus, reload() is very often called when the
// DVI file is bad. We solve this problem by checking the file
// first. If the file is bad, we do not reload. Second, when the file
// finally becomes good, it very often happens that KDirWatch does not
// notify us anymore. Whether this is a bug or a side effect of a
// feature of KDirWatch, I dare not say. We remedy that problem by
// using a timer: when reload() was called on a bad file, we
// automatically come back (via the timerEvent() function) every
// second and check if the file becaome good. If so, we stop the
// timer. It may well happen that KDirWatch calls us several times
// while we are waiting for the file to become good, but that does not
// do any harm.
//
// -- Stefan Kebekus.
void KDVIMultiPage : : timerEvent ( QTimerEvent * e )
{
2000-07-05 10:44:59 +00:00
# ifdef DEBUG
kdDebug ( 4300 ) < < " Timer Event " < < endl ;
# endif
2000-06-30 06:41:47 +00:00
reload ( ) ;
}
2000-06-01 10:27:41 +00:00
void KDVIMultiPage : : reload ( )
{
2000-07-05 10:44:59 +00:00
# ifdef DEBUG
kdDebug ( 4300 ) < < " Reload file " < < m_file < < endl ;
# endif
2000-06-23 00:22:44 +00:00
if ( window - > correctDVI ( m_file ) ) {
2000-06-30 06:41:47 +00:00
killTimer ( timer_id ) ;
timer_id = - 1 ;
int currsav = window - > curr_page ( ) ;
2000-07-07 02:07:26 +00:00
2000-07-08 11:58:30 +00:00
window - > setFile ( m_file ) ;
2000-06-30 06:41:47 +00:00
window - > gotoPage ( currsav ) ;
2000-07-10 12:44:26 +00:00
emit pageInfo ( window - > totalPages ( ) , window - > curr_page ( ) - 1 ) ; // We don't use "currsav" here, because that page may no longer exist. In that case, gotoPage already selected another page.
2000-06-30 06:41:47 +00:00
scrollView ( ) - > resizeContents ( window - > width ( ) , window - > height ( ) ) ;
2000-06-23 00:22:44 +00:00
emit previewChanged ( true ) ;
2000-06-30 06:41:47 +00:00
} else {
if ( timer_id = = - 1 )
timer_id = startTimer ( 1000 ) ;
2000-06-23 00:22:44 +00:00
}
2000-06-01 10:27:41 +00:00
}