Skip to content

Commit

Permalink
Editing the TargetTable is now bound to the underlying targets data
Browse files Browse the repository at this point in the history
array.
  • Loading branch information
Shadowen committed Jan 1, 2015
1 parent a94694b commit 864492d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
44 changes: 35 additions & 9 deletions groundstation/mainpic/mainpicwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ MainPicWidget::MainPicWidget(QWidget *parent) :
// The pic display area
picDisplay = new MainPicDisplay(&targets, this);
scrollArea->setWidget(picDisplay);
QObject::connect(picDisplay, SIGNAL(clicked(QMouseEvent*)), this, SLOT(onPictureClicked(QMouseEvent*)));
connect(picDisplay, SIGNAL(clicked(QMouseEvent*)), this, SLOT(onPictureClicked(QMouseEvent*)));

QWidget *sideBar = new QWidget();
QVBoxLayout *sideBarLayout = new QVBoxLayout();
Expand Down Expand Up @@ -44,6 +44,7 @@ MainPicWidget::MainPicWidget(QWidget *parent) :
// Target Table
targetTable = new QTableWidget();
cpLayout->addWidget(targetTable, 3, 1);
connect(targetTable, SIGNAL(cellChanged(int,int)), this, SLOT(onTargetTableChanged(int,int)));
targetTable->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
targetTable->setColumnCount(4);
QStringList tableHeader;
Expand Down Expand Up @@ -89,6 +90,39 @@ void MainPicWidget::setPicture(QString picturePath){

void MainPicWidget::onPictureClicked(QMouseEvent* event){
addTarget("Unnamed", event->x(), event->y());
// Try to 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
}
}

void MainPicWidget::onTargetTableChanged(int row, int column){
// Update the table
switch(column){
case 0:
targets[row].name = targetTable->item(row, column)->text();
break;
case 1:
targets[row].x = targetTable->item(row, column)->text().toInt();
break;
case 2:
targets[row].y = targetTable->item(row,column)->text().toInt();
break;
default:
qDebug() << QString("Exception in onTargetTableChanged(") + QString::number(row) + ", " + QString::number(column) + ").";
return;
}
// Save
// Try to 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
}
}

void MainPicWidget::addTarget(const QString& name, const int& x, const int& y){
Expand All @@ -103,12 +137,4 @@ void MainPicWidget::addTarget(const QString& name, const int& x, const int& y){
targetTable->setItem(row, 1, new QTableWidgetItem(QString::number(x)));
targetTable->setItem(row, 2, new QTableWidgetItem(QString::number(y)));
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
}
}
1 change: 1 addition & 0 deletions groundstation/mainpic/mainpicwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class MainPicWidget : public QWidget
public slots:
void toggleMode();
void onPictureClicked(QMouseEvent* event);
void onTargetTableChanged(int row, int column);
};

#endif // MAINPICWIDGET_H
3 changes: 3 additions & 0 deletions groundstation/mainpic/target.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include "target.h"

// Init some static variables
// Init the serialization seperator character(s)
const QString Target::SER_SEP = "\t";
// Init the field names
const QVector<QString> Target::FIELD_NAMES = QVector<QString>() << "Name" << "x" << "y";

Target::Target(){
}
Expand Down
11 changes: 9 additions & 2 deletions groundstation/mainpic/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
#define TARGET_H

#include <QTextStream>
#include <QVector>
#include <QString>
#include <QStringList>
#include <QMap>
#include <QList>

using namespace std;

class Target
{
public:
static const QString SER_SEP;
static const QVector<QString> FIELD_NAMES;

public:
QString name;
Expand All @@ -22,9 +26,12 @@ class Target
Target(const QString& iname, const int& ix, const int& iy);
virtual ~Target();

QString getFieldByNumber(const int& fieldID);
void setFieldByNumber(const int& fieldID, const QString& value);

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

#endif // TARGET_H

0 comments on commit 864492d

Please sign in to comment.