first primitive file watcher implemented

This commit is contained in:
nemu 2009-05-22 11:38:21 +00:00
parent c31c4c6c87
commit c89026c018
7 changed files with 252 additions and 3 deletions

View File

@ -0,0 +1,117 @@
/****************************************************************************
PFileWatcher.cpp
Author: Andreas Suter
e-mail: andreas.suter@psi.ch
$Id$
*****************************************************************************/
/***************************************************************************
* Copyright (C) 2009 by Andreas Suter *
* andreas.suter@psi.ch *
* *
* 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. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <qfileinfo.h>
#include <qstring.h>
#include <qdatetime.h>
#include <qtimer.h>
#include <qmessagebox.h>
#include "PFileWatcher.h"
//----------------------------------------------------------------------------------------------------
/**
* <p>
*/
PFileWatcher::PFileWatcher(const QString &fileName, const QDateTime &lastModified) : fFileName(fileName), fLastModified(lastModified)
{
fFileInfo = 0;
fFileInfo = new QFileInfo(fFileName);
if (!fFileInfo) {
fValid = false;
}
}
//----------------------------------------------------------------------------------------------------
/**
* <p>
*/
PFileWatcher::~PFileWatcher()
{
if (fFileInfo) {
delete fFileInfo;
fFileInfo = 0;
}
}
//----------------------------------------------------------------------------------------------------
/**
* <p>
*/
bool PFileWatcher::modified()
{
bool result = false;
fFileInfo->refresh();
if (fFileInfo->lastModified() > fLastModified)
result = true;
return result;
}
//----------------------------------------------------------------------------------------------------
/**
* <p>
*/
void PFileWatcher::modified(int timeout)
{
fTimerCheck = new QTimer(this);
connect( fTimerCheck, SIGNAL(timeout()), this, SLOT(checkIfModified()) );
QTimer::singleShot(timeout * 1000, this, SLOT(stopFileCheck()));
fTimerCheck->start(1000);
}
//----------------------------------------------------------------------------------------------------
/**
* <p>
*/
void PFileWatcher::checkIfModified()
{
fFileInfo->refresh();
if (fFileInfo->lastModified() > fLastModified) {
fTimerCheck->stop();
emit changed();
}
}
//----------------------------------------------------------------------------------------------------
/**
* <p>
*/
void PFileWatcher::stopFileCheck()
{
fTimerCheck->stop();
}

View File

@ -0,0 +1,66 @@
/****************************************************************************
PFileWatcher.h
Author: Andreas Suter
e-mail: andreas.suter@psi.ch
$Id$
*****************************************************************************/
/***************************************************************************
* Copyright (C) 2009 by Andreas Suter *
* andreas.suter@psi.ch *
* *
* 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. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef _PFILEWATCHER_H_
#define _PFILEWATCHER_H_
#include <qobject.h>
#include <qfileinfo.h>
class PFileWatcher : public QObject
{
Q_OBJECT
public:
PFileWatcher(const QString &fileName, const QDateTime &lastModified);
virtual ~PFileWatcher();
virtual bool isValid() { return fValid; }
virtual bool modified();
virtual void modified(int timeout);
signals:
void changed();
private slots:
void checkIfModified();
void stopFileCheck();
private:
bool fValid;
QString fFileName;
QFileInfo *fFileInfo;
QDateTime fLastModified;
QTimer *fTimerCheck;
};
#endif // _PFILEWATCHER_H_

View File

@ -60,6 +60,7 @@ PSubTextEdit::PSubTextEdit(PAdmin *admin,
QTextEdit(parent, name), QTextEdit(parent, name),
fAdmin(admin) fAdmin(admin)
{ {
// fLastModified = QDateTime::fromString("1900-01-01 00:00:00");
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------

View File

@ -33,6 +33,7 @@
#define _PSUBTEXTEDIT_H_ #define _PSUBTEXTEDIT_H_
#include <qtextedit.h> #include <qtextedit.h>
#include <qdatetime.h>
#include "PAdmin.h" #include "PAdmin.h"
@ -43,6 +44,9 @@ class PSubTextEdit : public QTextEdit
public: public:
PSubTextEdit(PAdmin *admin = 0, QWidget *parent = 0, const char *name = 0); PSubTextEdit(PAdmin *admin = 0, QWidget *parent = 0, const char *name = 0);
void setLastModified(const QDateTime &lastModified) { fLastModified = lastModified; }
QDateTime getLastModified() const { return fLastModified; }
protected: protected:
virtual QPopupMenu *createPopupMenu( const QPoint &pos); virtual QPopupMenu *createPopupMenu( const QPoint &pos);
@ -62,6 +66,7 @@ class PSubTextEdit : public QTextEdit
private: private:
PAdmin *fAdmin; PAdmin *fAdmin;
QDateTime fLastModified;
}; };
#endif // _PSUBTEXTEDIT_H_ #endif // _PSUBTEXTEDIT_H_

View File

@ -29,8 +29,10 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/ ***************************************************************************/
/*
#include <iostream> #include <iostream>
using namespace std; using namespace std;
*/
#include <qtextedit.h> #include <qtextedit.h>
#include <qaction.h> #include <qaction.h>
@ -57,6 +59,7 @@ using namespace std;
#include <qpixmap.h> #include <qpixmap.h>
#include "PTextEdit.h" #include "PTextEdit.h"
#include "PFileWatcher.h"
#include "PSubTextEdit.h" #include "PSubTextEdit.h"
#include "PAdmin.h" #include "PAdmin.h"
#include "PFindDialog.h" #include "PFindDialog.h"
@ -109,6 +112,8 @@ PTextEdit::PTextEdit( QWidget *parent, const char *name )
fMsr2DataParam = 0; fMsr2DataParam = 0;
fFindReplaceData = 0, fFindReplaceData = 0,
fFileWatcher = 0;
fKeepMinuit2Output = false; fKeepMinuit2Output = false;
fDump = 0; // 0 = no dump, 1 = ascii dump, 2 = root dump fDump = 0; // 0 = no dump, 1 = ascii dump, 2 = root dump
@ -134,6 +139,8 @@ PTextEdit::PTextEdit( QWidget *parent, const char *name )
} else { } else {
fileNew(); fileNew();
} }
connect( fTabWidget, SIGNAL( currentChanged(QWidget*) ), this, SLOT( checkIfModified(QWidget*) ));
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
@ -150,6 +157,10 @@ PTextEdit::~PTextEdit()
delete fFindReplaceData; delete fFindReplaceData;
fFindReplaceData = 0; fFindReplaceData = 0;
} }
if (fFileWatcher) {
delete fFileWatcher;
fFileWatcher = 0;
}
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
@ -394,7 +405,10 @@ void PTextEdit::load( const QString &f, const int index )
if ( !QFile::exists( f ) ) if ( !QFile::exists( f ) )
return; return;
QFileInfo info(f);
PSubTextEdit *edit = new PSubTextEdit( fAdmin ); PSubTextEdit *edit = new PSubTextEdit( fAdmin );
edit->setLastModified(info.lastModified());
edit->setTextFormat( PlainText ); edit->setTextFormat( PlainText );
edit->setFamily("Courier"); edit->setFamily("Courier");
edit->setPointSize(11); // 11pt edit->setPointSize(11); // 11pt
@ -524,6 +538,12 @@ void PTextEdit::fileReload()
fileClose(false); fileClose(false);
load(fln, index); load(fln, index);
} }
// clean up file watcher object if present
if (fFileWatcher) {
delete fFileWatcher;
fFileWatcher = 0;
}
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
@ -1534,6 +1554,11 @@ void PTextEdit::musrT0()
cmd += str + " &"; cmd += str + " &";
system(cmd.latin1()); system(cmd.latin1());
QString fln = *fFilenames.find( currentEditor() );
fFileWatcher = new PFileWatcher(fln, currentEditor()->getLastModified());
fFileWatcher->modified(3600);
connect(fFileWatcher, SIGNAL( changed() ), this, SLOT( fileReload() ));
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
@ -1675,7 +1700,7 @@ void PTextEdit::fontChanged( const QFont &f )
/** /**
* <p> * <p>
*/ */
void PTextEdit::textChanged() void PTextEdit::textChanged(const bool forced)
{ {
if (!currentEditor()) if (!currentEditor())
return; return;
@ -1691,6 +1716,10 @@ void PTextEdit::textChanged()
if ((fTabWidget->label(fTabWidget->currentPageIndex()).find("*") < 0) && if ((fTabWidget->label(fTabWidget->currentPageIndex()).find("*") < 0) &&
currentEditor()->isModified()) currentEditor()->isModified())
fTabWidget->setTabLabel(fTabWidget->currentPage(), tabLabel+"*"); fTabWidget->setTabLabel(fTabWidget->currentPage(), tabLabel+"*");
if ((fTabWidget->label(fTabWidget->currentPageIndex()).find("*") < 0) &&
forced)
fTabWidget->setTabLabel(fTabWidget->currentPage(), tabLabel+"*");
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
@ -1744,6 +1773,30 @@ void PTextEdit::replaceAll()
currentEditor()->setCursorPosition(currentPara, currentIndex); currentEditor()->setCursorPosition(currentPara, currentIndex);
} }
//----------------------------------------------------------------------------------------------------
/**
* <p>
*/
void PTextEdit::checkIfModified(QWidget*)
{
if ( fTabWidget->currentPage() && fTabWidget->currentPage()->inherits( "PSubTextEdit" ) ) {
QString fln = *fFilenames.find( currentEditor() );
PFileWatcher fw(fln, currentEditor()->getLastModified());
if (fw.isValid()) {
if (fw.modified()) {
int result = QMessageBox::information( this, "**INFO**",
"File modified on disk. Do you want to reload it?",
QMessageBox::Yes, QMessageBox::No);
if (result == QMessageBox::Yes) {
fileReload();
} else {
textChanged(true);
}
}
}
}
}
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
// END // END
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------

View File

@ -36,6 +36,7 @@
#include <qmap.h> #include <qmap.h>
#include "musrgui.h" #include "musrgui.h"
#include "PFileWatcher.h"
class PSubTextEdit; class PSubTextEdit;
class PAdmin; class PAdmin;
@ -109,12 +110,14 @@ private slots:
void helpAbout(); void helpAbout();
void fontChanged( const QFont &f ); void fontChanged( const QFont &f );
void textChanged(); void textChanged(const bool forced = false);
void replace(); void replace();
void replaceAndClose(); void replaceAndClose();
void replaceAll(); void replaceAll();
void checkIfModified(QWidget*);
private: private:
PAdmin *fAdmin; PAdmin *fAdmin;
@ -129,6 +132,8 @@ private:
QTabWidget *fTabWidget; QTabWidget *fTabWidget;
QMap<PSubTextEdit*, QString> fFilenames; QMap<PSubTextEdit*, QString> fFilenames;
PFileWatcher *fFileWatcher;
}; };

View File

@ -14,6 +14,7 @@ CONFIG += qt \
debug debug
HEADERS = musrgui.h \ HEADERS = musrgui.h \
PFileWatcher.h \
PTextEdit.h \ PTextEdit.h \
PSubTextEdit.h \ PSubTextEdit.h \
PAdmin.h \ PAdmin.h \
@ -32,7 +33,8 @@ HEADERS = musrgui.h \
PMsr2DataDialog.h \ PMsr2DataDialog.h \
PGetPlotDialog.h PGetPlotDialog.h
SOURCES = PTextEdit.cpp \ SOURCES = PFileWatcher.cpp \
PTextEdit.cpp \
PSubTextEdit.cpp \ PSubTextEdit.cpp \
PAdmin.cpp \ PAdmin.cpp \
PFindDialog.cpp \ PFindDialog.cpp \