Refactoring in trkConvert (#649)

* refactoring: DocWindow constructor to take datalog rather than filename.

* Neatening DocWindow.cpp
This commit is contained in:
jmpenn 2018-07-19 17:45:47 -05:00 committed by GitHub
parent 951aae4be4
commit 6f82d11122
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 54 deletions

View File

@ -14,72 +14,68 @@
#include "CSV_Formatter.hh"
#include "Varlist_Formatter.hh"
// Notes:
// Need to be able to search for a variable by pattern.
// Use a QLineEdit widget for the text box.
DocWindow::DocWindow(const QString &name)
DocWindow::DocWindow(TRK_DataLog* data_log )
: QMainWindow( 0, 0) {
QTextStream out(stdout);
foundItemIndex = 0;
datalog = data_log;
trkFileName = name;
// Build the Menus
QAction * fileLoadAction = new QAction( "&Open File...", this );
fileLoadAction->setShortcut(tr("CTRL+O"));
connect( fileLoadAction, &QAction::triggered , this, &DocWindow::load );
QAction * csvSaveAction = new QAction( "&Export as CSV...", this );
connect( csvSaveAction, &QAction::triggered, this, &DocWindow::saveAsCSV );
QAction * varListSaveAction = new QAction( "&Export as Variable List...", this );
connect( varListSaveAction, &QAction::triggered, this, &DocWindow::saveAsVarList );
// Build the FILE menu
QMenu *fileMenu = menuBar()->addMenu("&File");
QAction * fileLoadAction = new QAction( "&Open File...", this );
fileLoadAction->setShortcut(tr("CTRL+O"));
fileMenu->addAction(fileLoadAction);
connect( fileLoadAction, &QAction::triggered , this, &DocWindow::load );
fileMenu->addSeparator();
QAction * csvSaveAction = new QAction( "&Export as CSV...", this );
fileMenu->addAction(csvSaveAction);
connect( csvSaveAction, &QAction::triggered, this, &DocWindow::saveAsCSV );
QAction * varListSaveAction = new QAction( "&Export as Variable List...", this );
fileMenu->addAction(varListSaveAction);
connect( varListSaveAction, &QAction::triggered, this, &DocWindow::saveAsVarList );
QAction * editSelectAction = new QAction( "&Select All", this );
editSelectAction->setShortcut(tr("CTRL+A"));
connect( editSelectAction, &QAction::triggered, this, &DocWindow::checkAll );
QAction * editClearAction = new QAction( "&Clear All", this );
connect( editClearAction, &QAction::triggered, this, &DocWindow::unCheckAll );
// Build the EDIT menu
QMenu *editMenu = menuBar()->addMenu("&Edit");
QAction * editSelectAction = new QAction( "&Select All", this );
editSelectAction->setShortcut(tr("CTRL+A"));
editMenu->addAction(editSelectAction);
connect( editSelectAction, &QAction::triggered, this, &DocWindow::checkAll );
QAction * editClearAction = new QAction( "&Clear All", this );
editMenu->addAction(editClearAction);
connect( editClearAction, &QAction::triggered, this, &DocWindow::unCheckAll );
// Build the search interface.
QHBoxLayout *hbox = new QHBoxLayout();
QPushButton *backward = new QPushButton(QChar(0x25C0), this);
hbox->addWidget(backward);
connect( backward, &QPushButton::released, this, &DocWindow::findAgainBackward);
QPushButton *forward = new QPushButton(QChar(0x25B6), this);
hbox->addWidget(forward);
connect( forward, &QPushButton::released, this, &DocWindow::findAgainForward);
searchLineEdit = new QLineEdit;
searchLineEdit->setPlaceholderText("Search Pattern");
connect(searchLineEdit, SIGNAL(returnPressed()), this, SLOT(find()));
hbox->addWidget(backward);
hbox->addWidget(forward);
hbox->addWidget(searchLineEdit);
connect(searchLineEdit, SIGNAL(returnPressed()), this, SLOT(find()));
QVBoxLayout *vbox = new QVBoxLayout();
// Build the Table Widget that displays the variable names, types, and units.
varTable = new VarTableWidget(this);
datalog = new TRK_DataLog( trkFileName.toStdString().c_str() );
int recordCount = datalog->parameterCount();
for (int ii = 0; ii < recordCount; ii++) {
varTable->addRecord( Qt::Checked,
@ -104,8 +100,11 @@ void DocWindow::load() {
newFileName = QFileDialog::getOpenFileName(this,
tr("Open Data File"), ".", tr("Data Files (*.trk)"));
QFileInfo trkFileInfo( newFileName);
TRK_DataLog* newdatalog = new TRK_DataLog( trkFileInfo.absoluteFilePath().toStdString());
if (!newFileName.isEmpty()) {
DocWindow* w = new DocWindow(newFileName);
DocWindow* w = new DocWindow(newdatalog);
w->setWindowTitle(newFileName);
w->resize(800, 500);
w->show();
@ -114,7 +113,7 @@ void DocWindow::load() {
void DocWindow::formattedSave(LogFormatter &formatter) {
QFileInfo trkFileInfo( trkFileName);
QFileInfo trkFileInfo( datalog->getFileName().c_str());
QString outFileName = trkFileInfo.canonicalPath();
outFileName += "/";

View File

@ -10,7 +10,7 @@ class DocWindow : public QMainWindow {
Q_OBJECT
public:
DocWindow(const QString &name);
DocWindow(TRK_DataLog* data_log );
~DocWindow(){};
void formattedSave(LogFormatter &formatter);
@ -30,9 +30,8 @@ class DocWindow : public QMainWindow {
void findAgainBackward();
private:
QString searchPattern;
int foundItemIndex;
QString trkFileName;
QString searchPattern;
QLineEdit* searchLineEdit;
VarTableWidget* varTable;
TRK_DataLog* datalog;

View File

@ -1,5 +1,6 @@
#include "TRK_DataLog.hh"
#include <iostream>
#include <string.h>
#include <cstring>
#include <cstdlib>
@ -63,10 +64,10 @@ const char* TypeName[] = {
const int TRK_DataLog::LittleEndian = 1;
const int TRK_DataLog::BigEndian = 2;
TRK_DataLog::TRK_DataLog(const char* file_name) {
TRK_DataLog::TRK_DataLog(std::string file_name) {
fileName = file_name;
in_fp = fopen(fileName, "rb");
in_fp = fopen(fileName.c_str(), "rb");
if (in_fp != NULL) {
@ -122,25 +123,29 @@ TRK_DataLog::TRK_DataLog(const char* file_name) {
}
}
std::string TRK_DataLog::getFileName() const {
return fileName;
}
int TRK_DataLog::parameterCount() const {
return (int)N_params;
}
const char* TRK_DataLog::parameterName(unsigned int n) {
const char* TRK_DataLog::parameterName(unsigned int n) const {
if (n < N_params)
return paramDescriptions[n]->parameterName;
else
return "BadIndex";
}
const char* TRK_DataLog::parameterUnits(unsigned int n) {
const char* TRK_DataLog::parameterUnits(unsigned int n) const {
if (n < N_params)
return paramDescriptions[n]->unitsName;
else
return "BadIndex";
}
const char* TRK_DataLog::parameterType(unsigned int n) {
const char* TRK_DataLog::parameterType(unsigned int n) const {
if (n < N_params)
return TypeName[ paramDescriptions[n]->dataType ];
else

View File

@ -12,16 +12,7 @@
class TRK_DataLog {
public:
const char* fileName;
FILE* in_fp;
int version;
int endianness;
uint32_t N_params;
fpos_t dataPosition;
int dataRecordSize;
char* dataRecord;
public:
static const int LittleEndian;
static const int BigEndian;
@ -31,17 +22,28 @@ class TRK_DataLog {
// Constructors
TRK_DataLog(){}
TRK_DataLog(const char* fileName);
TRK_DataLog(std::string fileName);
std::string getFileName() const;
int parameterCount() const;
const char* parameterName(unsigned int n);
const char* parameterUnits(unsigned int n);
const char* parameterType(unsigned int n);
const char* parameterName(unsigned int n) const;
const char* parameterUnits(unsigned int n) const;
const char* parameterType(unsigned int n) const;
void selectAllParameters();
void selectParameter(unsigned int index);
void selectParameter(const char * paramName);
void deselectParameter(unsigned int index);
void formattedWrite(FILE* out_fp, LogFormatter* formatter);
private:
std::string fileName;
FILE* in_fp;
int version;
int endianness;
uint32_t N_params;
fpos_t dataPosition;
int dataRecordSize;
char* dataRecord;
};
#endif

View File

@ -68,7 +68,10 @@ int main(int argc, char *argv[]) {
if (!trkFilePath.isEmpty()) {
QFileInfo trkFileInfo( trkFilePath);
DocWindow* w1 = new DocWindow( trkFileInfo.absoluteFilePath());
TRK_DataLog* datalog =
new TRK_DataLog( trkFileInfo.absoluteFilePath().toStdString().c_str() );
DocWindow* w1 = new DocWindow(datalog);
w1->setWindowTitle( trkFileInfo.fileName());
w1->resize(800, 500);
w1->show();