Bug 94016 - Watch file option in kpdf
Summary: Watch file option in kpdf
Status: RESOLVED FIXED
Alias: None
Product: kpdf
Classification: Applications
Component: general (show other bugs)
Version: unspecified
Platform: Gentoo Packages Linux
: NOR wishlist
Target Milestone: ---
Assignee: Albert Astals Cid
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-11-27 07:33 UTC by Dmitriy Morozov
Modified: 2006-10-04 16:01 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dmitriy Morozov 2004-11-27 07:33:27 UTC
Version:            (using KDE KDE 3.3.1)
Installed from:    Gentoo Packages

KPDF would become a tremendously useful application if it had an option to watch a file (like in kghostview settings->Watch file) that would tell the program to monitor the file for changes, and if it does change then reload it (while staying on the same page). 

This is particularly useful when making presentations in LaTeX especially since right now no application that can render pdf well has such an option.
Comment 1 Albert Astals Cid 2004-12-01 00:27:11 UTC
CVS commit by aacid: 

Watch files as kghostview does (use the very same code :-D)
Will be available in KDE 3.4
FEATURE: 94016


  M +51 -1     kpdf_part.cpp   1.93
  M +10 -4     kpdf_part.h   1.42
  M +1 -0      kpdf_part.rc   1.21


--- kdegraphics/kpdf/kpdf/kpdf_part.cpp  #1.92:1.93
@@ -33,4 +33,5 @@
 #include <kapplication.h>
 #include <kdebug.h>
+#include <kdirwatch.h>
 #include <kinstance.h>
 #include <kprinter.h>
@@ -100,4 +101,5 @@ Part::Part(QWidget *parentWidget, const 
 
   setWidget(pdfpartview);
+  m_watchFile = new KToggleAction( i18n( "&Watch File" ), 0, this, SLOT( slotWatchFile() ), actionCollection(), "watch_file" );
 
   m_showScrollBars = new KToggleAction( i18n( "Show &Scrollbars" ), 0,
@@ -186,4 +188,10 @@ Part::Part(QWidget *parentWidget, const 
   m_zoomTo->setCurrentItem( idx );
 
+  m_watcher = new KDirWatch( this );
+  connect( m_watcher, SIGNAL( dirty( const QString& ) ), this, SLOT( slotFileDirty( const QString& ) ) );
+  m_dirtyHandler = new QTimer( this );
+  connect( m_dirtyHandler, SIGNAL( timeout() ),this, SLOT( slotDoFileDirty() ) );
+
+
 
   // set our XML-UI resource file
@@ -304,4 +312,5 @@ void Part::writeSettings()
     general.writeEntry( "ShowTOC", m_showTOC->isChecked() );
     general.writeEntry( "ShowPageList", m_showPageList->isChecked() );
+    general.writeEntry( "WatchFile", m_watchFile->isChecked() );
     general.sync();
 }
@@ -316,4 +325,6 @@ void Part::readSettings()
     m_showPageList->setChecked( general.readBoolEntry( "ShowPageList", true ) );
     showMarkList( m_showPageList->isChecked() );
+    m_watchFile->setChecked( general.readBoolEntry( "WatchFile", true ) );
+    slotWatchFile();
 }
 
@@ -392,7 +403,46 @@ Part::createAboutData()
 }
 
+  void
+Part::slotWatchFile()
+{
+  if( m_watchFile->isChecked() )
+    m_watcher->startScan();
+  else
+  {
+    m_dirtyHandler->stop();
+    m_watcher->stopScan();
+  }
+}
+
+  void
+Part::slotFileDirty( const QString& fileName )
+{
+  // The beauty of this is that each start cancels the previous one.
+  // This means that timeout() is only fired when there have
+  // no changes to the file for the last 750 milisecs.
+  // This is supposed to ensure that we don't update on every other byte
+  // that gets written to the file.
+  if ( fileName == m_file )
+  {
+    m_dirtyHandler->start( 750, true );
+  }
+}
+
+  void
+Part::slotDoFileDirty()
+{
+  int p = m_currentPage;
+  if (openFile())
+  {
+    if (p > m_doc->getNumPages()) p = m_doc->getNumPages();
+    goToPage(p);
+  }
+}
+
+
   bool
 Part::closeURL()
 {
+  if (!m_file.isEmpty()) m_watcher->removeFile(m_file);
   pdfpartview->stopThumbnailGeneration();
   delete m_doc;
@@ -461,6 +511,6 @@ Part::openFile()
     m_outputDev->setPDFDocument(m_doc);
     goToPage(1);
-
   }
+  if (!m_watcher->contains(m_file)) m_watcher->addFile(m_file);
 
   return true;

--- kdegraphics/kpdf/kpdf/kpdf_part.h  #1.41:1.42
@@ -27,4 +27,5 @@ class KAboutData;
 class KAction;
 class KConfig;
+class KDirWatch;
 class KURL;
 class KToggleAction;
@@ -141,4 +142,5 @@ namespace KPDF
       KAction* m_nextPage;
       KAction *m_gotoPage;
+      KToggleAction* m_watchFile;
       KToggleAction* m_showScrollBars;
       KToggleAction* m_showPageList;
@@ -148,5 +150,6 @@ namespace KPDF
       KAction *m_find;
       KAction *m_findNext;
-
+      KDirWatch *m_watcher;
+      QTimer *m_dirtyHandler;
       QString m_findText;
 
@@ -165,4 +168,7 @@ namespace KPDF
                 void pageClicked ( int );
     void fileSaveAs();
+    void slotWatchFile();
+    void slotFileDirty( const QString& );
+    void slotDoFileDirty();
   };
 

--- kdegraphics/kpdf/kpdf/kpdf_part.rc  #1.20:1.21
@@ -28,4 +28,5 @@
   <Menu name="settings"><text>&amp;Settings</text>
     <Action name="show_scrollbars" group="show_merge"/>
+    <Action name="watch_file" group="show_merge"/>
     <Action name="show_toc" group="show_merge"/>
     <Action name="show_page_list" group="show_merge"/>


Comment 2 Paolo Peruzzi 2006-10-04 16:01:01 UTC
I remember that watching pdf with kpdf was working. But with my current version it doesn't work anymore. Nothing happens if pdflatex modifies the pdf file.

$ kpdf --version
Qt: 3.3.6
KDE: 3.5.2
KPDF: 0.5.2

It's gentoo, kde-base/kpdf-3.5.2.