Skip to content

Commit

Permalink
Added user defined group 5A. Data for group is read from files.
Browse files Browse the repository at this point in the history
  • Loading branch information
jontio committed Nov 6, 2016
1 parent da4a3a6 commit 585e27c
Show file tree
Hide file tree
Showing 13 changed files with 716 additions and 38 deletions.
6 changes: 4 additions & 2 deletions JMPX/JMPX.pro
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ HEADERS += \
mainwindow.h \
vol/volumemeter.h \
../libJMPX/JMPXInterface.h \
options.h
options.h \
fileloader.h

SOURCES += \
main.cpp \
mainwindow.cpp \
vol/volumemeter.cpp \
options.cpp
options.cpp \
fileloader.cpp

unix {
HEADERS += nowplaying_linux.h
Expand Down
99 changes: 99 additions & 0 deletions JMPX/fileloader.cpp
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;
}

48 changes: 48 additions & 0 deletions JMPX/fileloader.h
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
30 changes: 20 additions & 10 deletions JMPX/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,16 @@ MainWindow::MainWindow(QWidget *parent) :
}
}

//song title checker (has no settings)
nowplaying=new NowPlaying(this);
connect(nowplaying,SIGNAL(songtitlechanged(QString)),this,SLOT(songtitlecheck(QString)));

//Fileloader for group 5A (has settings)
fileloader=new FileLoader(this);
connect(fileloader,SIGNAL(dataLoadSignal(QByteArray)),this,SLOT(pushdatato5a(QByteArray)));

//load settings
if(pJMPX)options->loadsettings(pJMPX);
if(pJMPX)options->loadsettings(pJMPX,fileloader);

//update low rate info
updatelowrateinfo();
Expand All @@ -61,10 +69,6 @@ MainWindow::MainWindow(QWidget *parent) :
ptimer->setInterval(20);
connect(ptimer,SIGNAL(timeout()),this,SLOT(updatedisplay()));

//song title checker
nowplaying=new NowPlaying(this);
connect(nowplaying,SIGNAL(songtitlechanged(QString)),this,SLOT(songtitlecheck(QString)));

//restore modulate enable
QSettings settings("JontiSoft", "JMPX");
ui->checkBox_modulate->setChecked(settings.value("checkBox_modulate",false).toBool());
Expand All @@ -74,7 +78,7 @@ MainWindow::MainWindow(QWidget *parent) :
MainWindow::~MainWindow()
{
//save settings
if(pJMPX)options->savesettings(pJMPX);
if(pJMPX)options->savesettings(pJMPX,fileloader);
QSettings settings("JontiSoft", "JMPX");
settings.setValue("checkBox_modulate",ui->checkBox_modulate->isChecked());
delete ui;
Expand Down Expand Up @@ -104,10 +108,10 @@ void MainWindow::updatedisplay()
void MainWindow::on_action_Options_triggered()
{
if(!pJMPX)return;
options->populatesettings(pJMPX);
options->populatesettings(pJMPX,fileloader);
if(options->exec()==QDialog::Accepted)
{
options->pushsetting(pJMPX);
options->pushsetting(pJMPX,fileloader);

ui->checkBox_modulate->setChecked(pJMPX->IsActive());
songtitlecheck(nowplaying->rt_title);
Expand Down Expand Up @@ -166,10 +170,10 @@ void MainWindow::on_action_About_triggered()
{
QMessageBox::about(this,"JMPX",""
"<H1>Stereo and RDS encoder for FM transmitters</H1>"
"<H3>v2.0.0</H3>"
"<H3>v2.0.1</H3>"
"<p>When connected to an FM transmitter via a soundcard this program allows you to transmit in stereo along with the ability to send information using RDS (Radio Data System) to the listeners. With RDS the listerners can see what your station is called and other usefull information.</p>"
"<p>For more information about this application see <a href=\"http://jontio.zapto.org/hda1/paradise/jmpxencoder/jmpx.html\">http://jontio.zapto.org/hda1/paradise/jmpxencoder/jmpx.html</a>.</p>"
"<p>Jonti 2015</p>" );
"<p>Jonti 2016</p>" );
}

void MainWindow::on_actionAbout_Qt_triggered()
Expand All @@ -193,3 +197,9 @@ void MainWindow::songtitlecheck(const QString &title)
//set RT with title
pJMPX->RDS_SetRT(title);
}

void MainWindow::pushdatato5a(const QByteArray &data)
{
if((!pJMPX)||!pJMPX.data()->IsActive())return;
pJMPX->RDS_Set_5A_data(data);
}
3 changes: 3 additions & 0 deletions JMPX/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "nowplaying_mac.h"
#endif

#include "fileloader.h"

namespace Ui {
class MainWindow;
Expand All @@ -40,6 +41,7 @@ class MainWindow : public QMainWindow {
Options *options;
void updatelowrateinfo();
NowPlaying *nowplaying;
FileLoader *fileloader;
private slots:
void volbarssetfixedequalwidth();
void updatedisplay();
Expand All @@ -48,6 +50,7 @@ private slots:
void on_action_About_triggered();
void on_actionAbout_Qt_triggered();
void songtitlecheck(const QString &title);
void pushdatato5a(const QByteArray &data);
};

#endif // MAINWINDOW_H
Loading

0 comments on commit 585e27c

Please sign in to comment.