added file system watcher
This commit is contained in:
parent
f7caf65251
commit
cb2f7822b7
@ -1,115 +0,0 @@
|
||||
/****************************************************************************
|
||||
|
||||
PFileWatcher.cpp
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
*****************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2010 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 <QMessageBox>
|
||||
|
||||
#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) {
|
||||
fLastModified = fFileInfo->lastModified();
|
||||
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) {
|
||||
fLastModified = fFileInfo->lastModified();
|
||||
fTimerCheck->stop();
|
||||
emit changed();
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*/
|
||||
void PFileWatcher::stopFileCheck()
|
||||
{
|
||||
fTimerCheck->stop();
|
||||
}
|
@ -1,74 +0,0 @@
|
||||
/****************************************************************************
|
||||
|
||||
PFileWatcher.h
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
*****************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2010 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. *
|
||||
***************************************************************************/
|
||||
|
||||
/*
|
||||
CHECK IF THIS IS STILL NEEDED!! AS FAR AS I KNOW, STARTING FROM Qt4.x THERE IS A PROPER Qt-WAY TO DO THIS!!
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _PFILEWATCHER_H_
|
||||
#define _PFILEWATCHER_H_
|
||||
|
||||
#include <QObject>
|
||||
#include <QFileInfo>
|
||||
#include <QString>
|
||||
#include <QDateTime>
|
||||
#include <QTimer>
|
||||
|
||||
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_
|
@ -52,7 +52,7 @@ PGetTitleBlockDialog::PGetTitleBlockDialog(QWidget *parent, Qt::WindowFlags f) :
|
||||
void PGetTitleBlockDialog::helpContent()
|
||||
{
|
||||
PHelp *help = new PHelp("http://lmu.web.psi.ch/musrfit/title.html");
|
||||
help->show();
|
||||
//help->show();
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
@ -45,8 +45,8 @@ class PSubTextEdit : public QPlainTextEdit
|
||||
PSubTextEdit(PAdmin *admin = 0, QWidget *parent = 0);
|
||||
virtual ~PSubTextEdit() {}
|
||||
|
||||
void setLastModified(const QDateTime &lastModified) { fLastModified = lastModified; }
|
||||
QDateTime getLastModified() const { return fLastModified; }
|
||||
// void setLastModified(const QDateTime &lastModified) { fLastModified = lastModified; }
|
||||
// QDateTime getLastModified() const { return fLastModified; }
|
||||
|
||||
public slots:
|
||||
void insertTitle();
|
||||
@ -67,7 +67,7 @@ class PSubTextEdit : public QPlainTextEdit
|
||||
|
||||
private:
|
||||
PAdmin *fAdmin;
|
||||
QDateTime fLastModified;
|
||||
// QDateTime fLastModified;
|
||||
};
|
||||
|
||||
#endif // _PSUBTEXTEDIT_H_
|
||||
|
@ -57,7 +57,6 @@ using namespace std;
|
||||
#include <QtDebug>
|
||||
|
||||
#include "PTextEdit.h"
|
||||
//#include "PFileWatcher.h"
|
||||
#include "PSubTextEdit.h"
|
||||
#include "PAdmin.h"
|
||||
#include "PFindDialog.h"
|
||||
@ -78,6 +77,13 @@ PTextEdit::PTextEdit( QWidget *parent, Qt::WindowFlags f )
|
||||
{
|
||||
fAdmin = new PAdmin();
|
||||
|
||||
fFileSystemWatcher = new QFileSystemWatcher();
|
||||
if (fFileSystemWatcher == 0) {
|
||||
QMessageBox::information(this, "**ERROR**", "Couldn't invoke QFileSystemWatcher!");
|
||||
} else {
|
||||
connect( fFileSystemWatcher, SIGNAL(fileChanged(const QString&)), this, SLOT(fileChanged(const QString&)));
|
||||
}
|
||||
|
||||
fMusrT0Action = 0;
|
||||
|
||||
fMsr2DataParam = 0;
|
||||
@ -112,7 +118,6 @@ PTextEdit::PTextEdit( QWidget *parent, Qt::WindowFlags f )
|
||||
fileNew();
|
||||
}
|
||||
|
||||
connect( fTabWidget, SIGNAL( currentChanged(QWidget*) ), this, SLOT( checkIfModified(QWidget*) ));
|
||||
connect( fTabWidget, SIGNAL( currentChanged(QWidget*) ), this, SLOT( applyFontSettings(QWidget*) ));
|
||||
}
|
||||
|
||||
@ -408,7 +413,7 @@ void PTextEdit::setupTextActions()
|
||||
this, SLOT( textFamily( const QString & ) ) );
|
||||
QLineEdit *edit = fComboFont->lineEdit();
|
||||
if (edit == 0) {
|
||||
qDebug() << endl << "**ERROR** PTextEdit::setupTextActions(): cannot edit fComboFont" << endl;
|
||||
// qDebug() << endl << "**ERROR** PTextEdit::setupTextActions(): cannot edit fComboFont" << endl;
|
||||
return;
|
||||
}
|
||||
edit->setText( fAdmin->getFontName() );
|
||||
@ -424,7 +429,7 @@ void PTextEdit::setupTextActions()
|
||||
this, SLOT( textSize( const QString & ) ) );
|
||||
edit = fComboSize->lineEdit();
|
||||
if (edit == 0) {
|
||||
qDebug() << endl << "**ERROR** PTextEdit::setupTextActions(): cannot edit fComboSize" << endl;
|
||||
// qDebug() << endl << "**ERROR** PTextEdit::setupTextActions(): cannot edit fComboSize" << endl;
|
||||
return;
|
||||
}
|
||||
edit->setText( QString("%1").arg(fAdmin->getFontSize()) );
|
||||
@ -554,10 +559,6 @@ void PTextEdit::load( const QString &f, const int index )
|
||||
QFileInfo info(f);
|
||||
|
||||
PSubTextEdit *edit = new PSubTextEdit( fAdmin );
|
||||
edit->setLastModified(info.lastModified());
|
||||
// edit->setAcceptRichText( false );
|
||||
// edit->setFontFamily(fAdmin->getFontName());
|
||||
// edit->setFontPointSize(fAdmin->getFontSize());
|
||||
edit->setFont(QFont(fAdmin->getFontName(), fAdmin->getFontSize()));
|
||||
|
||||
if (index == -1)
|
||||
@ -568,6 +569,8 @@ void PTextEdit::load( const QString &f, const int index )
|
||||
if ( !file.open( QIODevice::ReadOnly ) )
|
||||
return;
|
||||
|
||||
fFileSystemWatcher->addPath(f);
|
||||
|
||||
QTextStream ts( &file );
|
||||
QString txt = ts.readAll();
|
||||
edit->setPlainText( txt );
|
||||
@ -713,8 +716,8 @@ void PTextEdit::insertCommandBlock()
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------------------
|
||||
* <p>
|
||||
/**
|
||||
* <p>
|
||||
*/
|
||||
void PTextEdit::insertFourierBlock()
|
||||
{
|
||||
@ -746,9 +749,6 @@ void PTextEdit::insertStatisticBlock()
|
||||
void PTextEdit::fileNew()
|
||||
{
|
||||
PSubTextEdit *edit = new PSubTextEdit( fAdmin );
|
||||
// edit->setAcceptRichText( false );
|
||||
// edit->setFontFamily(fAdmin->getFontName());
|
||||
// edit->setFontPointSize(fAdmin->getFontSize());
|
||||
edit->setFont(QFont(fAdmin->getFontName(), fAdmin->getFontSize()));
|
||||
doConnections( edit );
|
||||
fTabWidget->addTab( edit, tr( "noname" ) );
|
||||
@ -777,7 +777,7 @@ void PTextEdit::fileOpen()
|
||||
finfo1.setFile(*it);
|
||||
for (int i=0; i<fTabWidget->count(); i++) {
|
||||
tabFln = *fFilenames.find( dynamic_cast<PSubTextEdit*>(fTabWidget->widget(i)));
|
||||
qDebug() << endl << "tabFln=" << tabFln;
|
||||
// qDebug() << endl << "tabFln=" << tabFln;
|
||||
finfo2.setFile(tabFln);
|
||||
if (finfo1.absoluteFilePath() == finfo2.absoluteFilePath()) {
|
||||
alreadyOpen = true;
|
||||
@ -809,14 +809,6 @@ void PTextEdit::fileReload()
|
||||
fileClose(false);
|
||||
load(fln, index);
|
||||
}
|
||||
|
||||
/*
|
||||
// clean up file watcher object if present
|
||||
if (fFileWatcher) {
|
||||
delete fFileWatcher;
|
||||
fFileWatcher = 0;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------------------
|
||||
@ -887,7 +879,7 @@ void PTextEdit::filePrint()
|
||||
font.setPointSize( 10 ); // we define 10pt to be a nice base size for printing
|
||||
p.setFont( font );
|
||||
|
||||
int yPos = 0; // y-position for each line
|
||||
int yPos = 0; // y-position for each line
|
||||
QFontMetrics fm = p.fontMetrics();
|
||||
int dpiy = printer.logicalDpiY();
|
||||
int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins
|
||||
@ -898,7 +890,7 @@ void PTextEdit::filePrint()
|
||||
// new page needed?
|
||||
if ( margin + yPos > printer.height() - margin ) {
|
||||
printer.newPage(); // no more room on this page
|
||||
yPos = 0; // back to top of page
|
||||
yPos = 0; // back to top of page
|
||||
}
|
||||
|
||||
// print data
|
||||
@ -928,7 +920,11 @@ void PTextEdit::fileClose(const bool check)
|
||||
return;
|
||||
}
|
||||
|
||||
QString str = *fFilenames.find(currentEditor());
|
||||
fFileSystemWatcher->removePath(str);
|
||||
|
||||
delete currentEditor();
|
||||
|
||||
if ( currentEditor() )
|
||||
currentEditor()->viewport()->setFocus();
|
||||
}
|
||||
@ -956,8 +952,14 @@ void PTextEdit::fileCloseAll()
|
||||
}
|
||||
|
||||
// close all editor tabs
|
||||
QString str;
|
||||
do {
|
||||
// remove file from file system watcher
|
||||
str = *fFilenames.find(currentEditor());
|
||||
fFileSystemWatcher->removePath(str);
|
||||
|
||||
delete currentEditor();
|
||||
|
||||
if ( currentEditor() )
|
||||
currentEditor()->viewport()->setFocus();
|
||||
} while ( currentEditor() );
|
||||
@ -994,11 +996,17 @@ void PTextEdit::fileCloseAllOthers()
|
||||
|
||||
// close all editor tabs
|
||||
int i=0;
|
||||
QString str;
|
||||
do {
|
||||
if (fTabWidget->tabText(i) != label)
|
||||
if (fTabWidget->tabText(i) != label) {
|
||||
// remove file from file system watcher
|
||||
str = *fFilenames.find(dynamic_cast<PSubTextEdit*>(fTabWidget->widget(i)));
|
||||
fFileSystemWatcher->removePath(str);
|
||||
|
||||
delete fTabWidget->widget(i);
|
||||
else
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
} while ( fTabWidget->count() > 1 );
|
||||
|
||||
currentEditor()->viewport()->setFocus();
|
||||
@ -1963,11 +1971,6 @@ void PTextEdit::musrT0()
|
||||
system(cmd.toLatin1());
|
||||
|
||||
QString fln = *fFilenames.find( currentEditor() );
|
||||
/*
|
||||
fFileWatcher = new PFileWatcher(fln, currentEditor()->getLastModified());
|
||||
fFileWatcher->modified(3600);
|
||||
connect(fFileWatcher, SIGNAL( changed() ), this, SLOT( fileReload() ));
|
||||
*/
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------------------
|
||||
@ -2228,28 +2231,35 @@ void PTextEdit::applyFontSettings(QWidget*)
|
||||
|
||||
//----------------------------------------------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
* <p>Called when an open file is changing outside the editor (SLOT).
|
||||
*
|
||||
* \param fileName file name of the changed file.
|
||||
*/
|
||||
void PTextEdit::checkIfModified(QWidget*)
|
||||
void PTextEdit::fileChanged(const QString &fileName)
|
||||
{
|
||||
/*
|
||||
if ( fTabWidget->currentWidget() && fTabWidget->currentWidget()->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);
|
||||
}
|
||||
QString str = "File '" + fileName + "' changed on the system.\nDo you want to reload it?";
|
||||
int result = QMessageBox::question(this, "**INFO**", str, QMessageBox::Yes, QMessageBox::No);
|
||||
if (result == QMessageBox::Yes) {
|
||||
// find correct file
|
||||
int idx = -1;
|
||||
for (int i=0; i<fTabWidget->count(); i++) {
|
||||
if (fTabWidget->tabText(i) == QFileInfo(fileName).fileName()) {
|
||||
idx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
if (idx == -1)
|
||||
return;
|
||||
|
||||
// remove file from file system watcher
|
||||
fFileSystemWatcher->removePath(fileName);
|
||||
|
||||
// close tab
|
||||
fTabWidget->removeTab(idx);
|
||||
// load it
|
||||
load(fileName, idx);
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------------------
|
||||
|
@ -36,10 +36,10 @@
|
||||
#include <QMap>
|
||||
|
||||
#include "musredit.h"
|
||||
//#include "PFileWatcher.h"
|
||||
|
||||
class PSubTextEdit;
|
||||
class PAdmin;
|
||||
class QFileSystemWatcher;
|
||||
class QAction;
|
||||
class QComboBox;
|
||||
class QTabWidget;
|
||||
@ -130,10 +130,11 @@ private slots:
|
||||
void replaceAll();
|
||||
|
||||
void applyFontSettings(QWidget*);
|
||||
void checkIfModified(QWidget*);
|
||||
void fileChanged(const QString &fileName);
|
||||
|
||||
private:
|
||||
PAdmin *fAdmin;
|
||||
QFileSystemWatcher *fFileSystemWatcher;
|
||||
|
||||
QAction *fMusrT0Action;
|
||||
|
||||
@ -151,8 +152,6 @@ private:
|
||||
|
||||
QTabWidget *fTabWidget;
|
||||
QMap<PSubTextEdit*, QString> fFilenames;
|
||||
|
||||
// PFileWatcher *fFileWatcher;
|
||||
};
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user