CustusX  18.04
An IGT application
cxFileWatcherWidget.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxFileWatcherWidget.h"
13 #include "cxLogger.h"
14 #include <QFileSystemWatcher>
15 #include <QFileInfo>
16 
17 
18 namespace cx
19 {
20 FileWatcherWidget::FileWatcherWidget(QWidget* parent, QString objectName, QString windowTitle) :
21  BaseWidget(parent, objectName, windowTitle),
22  mFileSystemWatcher(new QFileSystemWatcher())
23 {
24  connect(mFileSystemWatcher, SIGNAL(fileChanged(const QString&)), this, SLOT(previewFileSlot(const QString&)));
25 }
26 
28 {}
29 
30 bool FileWatcherWidget::internalOpenNewFile(const QString absoluteFilePath)
31 {
32  if(mCurrentFile)
33  {
34  if(mCurrentFile->isOpen())
35  {
36  this->watchFile(false);
37  mCurrentFile->close();
38  }
39  }
40 
41  mCurrentFile.reset(new QFile(absoluteFilePath));
42  this->watchFile(true);
43 
44  if(!mCurrentFile->exists())
45  {
46  return false;
47  }
48 
49  if(!mCurrentFile->open(QIODevice::ReadOnly))
50  {
51  reportWarning("Could not open file "+absoluteFilePath);
52  //return false; Is this missing???
53  }
54  return true;
55 }
56 
58 {
59  if (!mCurrentFile)
60  return;
61 
62  QFileInfo info(*mCurrentFile);
63 
64  if (on)
65  {
66  if (!mFileSystemWatcher->files().contains(info.absoluteFilePath()))
67  mFileSystemWatcher->addPath(info.absoluteFilePath());
68  }
69  else
70  {
71  mFileSystemWatcher->removePath(info.absoluteFilePath());
72  }
73 }
74 } /* namespace cx */
bool internalOpenNewFile(const QString absoluteFilePath)
QFileSystemWatcher * mFileSystemWatcher
virtual void previewFileSlot(const QString &absoluteFilePath)=0
FileWatcherWidget(QWidget *parent, QString objectName, QString windowTitle)
boost::shared_ptr< QFile > mCurrentFile
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:88
Namespace for all CustusX production code.