-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added user defined group 5A. Data for group is read from files.
- Loading branch information
Showing
13 changed files
with
716 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#include "fileloader.h" | ||
#include <QDebug> | ||
#include <QTimer> | ||
|
||
FileLoader::FileLoader(QObject *parent) : QObject(parent) | ||
{ | ||
filewatcher = new QFileSystemWatcher(this); | ||
connect(filewatcher, SIGNAL(fileChanged(const QString &)), this, SLOT(fileChanged(const QString &))); | ||
ashextext=false; | ||
loaderror=true; | ||
} | ||
|
||
void FileLoader::fileLoadErrorTimeout() | ||
{ | ||
if(!hasloaderror())return; | ||
qDebug()<<"fileLoadErrorTimeout"; | ||
if(!reloadfile())QTimer::singleShot(5000, this, SLOT(fileLoadErrorTimeout())); | ||
else if((filewatcher->files().isEmpty())&&(!filename.isEmpty())) | ||
{ | ||
qDebug()<<"fileCreated:"<<filename; | ||
filewatcher->addPath(filename); | ||
} | ||
} | ||
|
||
void FileLoader::set_filename(QString filename, bool ashextext) | ||
{ | ||
//if we are already watching the file then just leave the watcher alone | ||
if(filewatcher->files().contains(filename)) | ||
{ | ||
//if we failed to load the file then try again later | ||
if(!load_file(filename,ashextext))QTimer::singleShot(5000, this, SLOT(fileLoadErrorTimeout())); | ||
return; | ||
} | ||
//remove the all files from the watcher, set the filename, load the file, add the file to the watcher | ||
if(!filewatcher->files().isEmpty())filewatcher->removePaths(filewatcher->files()); | ||
//if we failed to load the file then try again later | ||
if(!load_file(filename,ashextext))QTimer::singleShot(5000, this, SLOT(fileLoadErrorTimeout())); | ||
if(!filename.isEmpty())filewatcher->addPath(get_filename()); | ||
} | ||
|
||
void FileLoader::fileChanged(const QString &path) | ||
{ | ||
if(!QFile(filename).exists())//if removed | ||
{ | ||
qDebug()<<"fileRemoved:"<<path; | ||
} else qDebug()<<"fileChanged:"<<path; | ||
if(!reloadfile())QTimer::singleShot(5000, this, SLOT(fileLoadErrorTimeout())); | ||
} | ||
|
||
bool FileLoader::load_file(QString _filename,bool _ashextext,bool invalidate) | ||
{ | ||
loaderror=true; | ||
if(_filename.isEmpty())invalidate=true;//invalidate if empty | ||
if(invalidate) | ||
{ | ||
filename.clear(); | ||
ashextext=!ashextext; | ||
} | ||
if((filename==_filename)&&(ashextext==_ashextext)) | ||
{ | ||
loaderror=false; | ||
emit dataLoadSignal(ba); | ||
return true; | ||
} | ||
filename=_filename; | ||
ashextext=_ashextext; | ||
ba.clear(); | ||
if(filename.isEmpty())//lets say nothing is ok | ||
{ | ||
loaderror=false; | ||
emit dataLoadSignal(ba); | ||
return true; | ||
} | ||
QFile file(filename); | ||
if(ashextext) | ||
{ | ||
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) | ||
{ | ||
qDebug()<<"failed opening file for reading"; | ||
emit dataLoadSignal(ba); | ||
return false; | ||
} | ||
ba=QByteArray::fromHex(file.readAll()); | ||
} | ||
else | ||
{ | ||
if(!file.open(QIODevice::ReadOnly)) | ||
{ | ||
qDebug()<<"failed opening file for reading"; | ||
emit dataLoadSignal(ba); | ||
return false; | ||
} | ||
ba=file.readAll(); | ||
} | ||
loaderror=false; | ||
emit dataLoadSignal(ba); | ||
return true; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#ifndef FILELOADER_H | ||
#define FILELOADER_H | ||
#include <QObject> | ||
#include <QFileSystemWatcher> | ||
#include <QFile> | ||
|
||
class FileLoader : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit FileLoader(QObject *parent = 0); | ||
|
||
//just set the file name and this class will take care of the rest | ||
void set_filename(QString filename, bool ashextext); | ||
|
||
//if you want to force a reload but i cant see why you would want to | ||
bool reloadfile(){return load_file(filename,ashextext,true);} | ||
|
||
//was there an error the last time the file was loaded? | ||
bool hasloaderror(){return loaderror;} | ||
|
||
//what is the file that is being monitored? | ||
QString get_filename(){return filename;} | ||
|
||
//is it in hex text? | ||
bool get_ashextext(){return ashextext;} | ||
|
||
signals: | ||
|
||
//emits when there is new data or the file has been reloaded | ||
void dataLoadSignal(const QByteArray &data); | ||
|
||
private: | ||
bool ashextext;// | ||
bool loaderror;// | ||
QString filename;// | ||
QByteArray ba;// | ||
QFileSystemWatcher *filewatcher; | ||
|
||
bool load_file(QString _filename,bool _ashextext){return load_file(_filename,_ashextext,false);} | ||
bool load_file(QString _filename,bool _ashextext,bool invalidate); | ||
|
||
private slots: | ||
void fileChanged(const QString &path); | ||
void fileLoadErrorTimeout(); | ||
}; | ||
|
||
#endif // FILELOADER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.