Skip to content

Commit

Permalink
Target saving and loading done. Simple exception throwing/catching also
Browse files Browse the repository at this point in the history
included!
  • Loading branch information
Shadowen committed Dec 29, 2014
1 parent 36c235b commit a94694b
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 11 deletions.
31 changes: 28 additions & 3 deletions groundstation/mainpic/mainpicwidget.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "mainpicwidget.h"
;

MainPicWidget::MainPicWidget(QWidget *parent) :
QWidget(parent)
{
Expand Down Expand Up @@ -65,8 +65,25 @@ void MainPicWidget::mainWindowResized(){
}

void MainPicWidget::setPicture(QString picturePath){
currentPicture = picturePath;

// Load the targets
try{
targets = targetFileHandler.readFile(currentPicture + TargetFileHandler::fileExtension);
}catch(TargetFileHandler::FileReadException e){
qDebug() << e.what();
// TODO actual error handling
}
// Change display pic
picDisplay->displayPicture(picturePath);
picDisplay->displayPicture(currentPicture);
// Add targets to table
for (int row = 0; row < targets.size(); row++){
targetTable->insertRow(row);
targetTable->setItem(row, 0, new QTableWidgetItem(targets[row].name));
targetTable->setItem(row, 1, new QTableWidgetItem(QString::number(targets[row].x)));
targetTable->setItem(row, 2, new QTableWidgetItem(QString::number(targets[row].y)));
targetTable->setItem(row, 3, new QTableWidgetItem("Placeholder"));
}
}


Expand All @@ -85,5 +102,13 @@ void MainPicWidget::addTarget(const QString& name, const int& x, const int& y){
targetTable->setItem(row, 0, new QTableWidgetItem(name));
targetTable->setItem(row, 1, new QTableWidgetItem(QString::number(x)));
targetTable->setItem(row, 2, new QTableWidgetItem(QString::number(y)));
targetTable->setItem(row, 3, new QTableWidgetItem("Blue"));
targetTable->setItem(row, 3, new QTableWidgetItem("Placeholder"));

// Save the new targets into the file
try{
targetFileHandler.saveFile(targets, currentPicture + TargetFileHandler::fileExtension);
}catch(TargetFileHandler::FileWriteException e){
qDebug() << e.what();
// TODO actual error handling
}
}
7 changes: 6 additions & 1 deletion groundstation/mainpic/mainpicwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
#include <QVector>
#include <QString>
#include <QHeaderView>
#include <QDebug>

#include "mainpicdisplay.h"
#include "target.h"
#include "targetfilehandler.h"

class MainPicWidget : public QWidget
{
Expand All @@ -27,9 +29,12 @@ class MainPicWidget : public QWidget
void setPicture(QString);
void addTarget(const QString& name, const int& x, const int& y);
private:
QString currentPicture;

QScrollArea* scrollArea;
MainPicDisplay* picDisplay;
QTableWidget *targetTable;
QTableWidget* targetTable;
TargetFileHandler targetFileHandler;
signals:

public slots:
Expand Down
22 changes: 20 additions & 2 deletions groundstation/mainpic/target.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
#include "target.h"

// Init the serialization seperator character(s)
const QString Target::SER_SEP = "\t";

Target::Target(){
}

Target::Target(const QString& iname, const int& ix, const int& iy)
{
Target::Target(const QString& iname, const int& ix, const int& iy){
name = iname;
x = ix;
y = iy;
}

Target::~Target(){
}

QTextStream& operator<<(QTextStream&strm, const Target &t) {
return strm << t.name << Target::SER_SEP << t.x << Target::SER_SEP << t.y;
}

QTextStream& operator>>(QTextStream& strm, Target &t){
QString s = strm.readLine();
QStringList sl = s.split(Target::SER_SEP);
// Decode the string
QString name = sl[0];
int x = sl[1].toInt();
int y = sl[2].toInt();
// Make the object
t = Target(name, x, y);
return strm;
}
13 changes: 12 additions & 1 deletion groundstation/mainpic/target.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
#ifndef TARGET_H
#define TARGET_H

#include <QTextStream>
#include <QString>
#include <QStringList>

using namespace std;

class Target
{
public:
static const QString SER_SEP;

public:
QString name;
int x;
Expand All @@ -13,7 +20,11 @@ class Target
public:
Target();
Target(const QString& iname, const int& ix, const int& iy);
~Target();
virtual ~Target();

private:
friend QTextStream& operator<<(QTextStream&, const Target &);
friend QTextStream& operator>>(QTextStream&, Target &);
};

#endif // TARGET_H
42 changes: 42 additions & 0 deletions groundstation/mainpic/targetfilehandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "targetfilehandler.h"

const QString TargetFileHandler::fileExtension = ".targets";

TargetFileHandler::TargetFileHandler()
{
}

void TargetFileHandler::saveFile(const QVector<Target>& targets, const QString& fileName){
QFile file(fileName);
// Open the file
if (!file.open(QIODevice::WriteOnly)) {
// TODO throw different errors depending on why
throw FileWriteException();
}
// Write targets out
QTextStream out(&file);
for (int i = 0; i < targets.size(); i++){
out << targets[i] << "\n";
}
file.close();
}

QVector<Target> TargetFileHandler::readFile(const QString& fileName){
QFile file(fileName);
// Open the file
if (!file.open(QIODevice::ReadOnly)) {
// TODO throw different errors depending on why
// ie. file doesn't exist
throw FileReadException();
}
// Read the file
QVector<Target> targets;
QTextStream in(&file);
while(!in.atEnd()) {
Target t;
in >> t;
targets.append(t);
}
file.close();
return targets;
}
43 changes: 43 additions & 0 deletions groundstation/mainpic/targetfilehandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef TARGETFILEHANDLER_H
#define TARGETFILEHANDLER_H

#include <QVector>
#include <QString>
#include <QFile>
#include <QIODevice>
#include <QMessageBox>
#include <QTextStream>

#include "target.h"

using namespace std;

class TargetFileHandler
{
public:
static const QString fileExtension;
class FileReadException : public exception
{
public:
virtual const char* what() const throw()
{
return "File Read Exception";
}
};
class FileWriteException : public exception
{
public:
virtual const char* what() const throw()
{
return "File Write Exception";
}
};

public:
TargetFileHandler();

void saveFile(const QVector<Target>&, const QString&);
QVector<Target> readFile(const QString&);
};

#endif // TARGETFILEHANDLER_H
9 changes: 5 additions & 4 deletions groundstation/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->setupUi(this);

mainPic = new MainPicWidget();
setCentralWidget(mainPic);
mainPic = new MainPicWidget();
setCentralWidget(mainPic);

mainPic->setPicture("C:/Users/wesley/git/groundstation/groundstation/580.jpg");
mainPic->setPicture("C:/Users/wesley/git/groundstation/groundstation/580.jpg");
}

MainWindow::~MainWindow()
Expand Down
1 change: 1 addition & 0 deletions groundstation/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QResizeEvent>

#include "mainpic/mainpicwidget.h"
#include "mainpic/targetfilehandler.h"

using namespace std;
namespace Ui {
Expand Down

0 comments on commit a94694b

Please sign in to comment.