From 864492d09ec50435ef930e303db55d9255405c04 Mon Sep 17 00:00:00 2001 From: wesleyOnLucy Date: Wed, 31 Dec 2014 22:16:39 -0500 Subject: [PATCH] Editing the TargetTable is now bound to the underlying targets data array. --- groundstation/mainpic/mainpicwidget.cpp | 44 ++++++++++++++++++++----- groundstation/mainpic/mainpicwidget.h | 1 + groundstation/mainpic/target.cpp | 3 ++ groundstation/mainpic/target.h | 11 +++++-- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/groundstation/mainpic/mainpicwidget.cpp b/groundstation/mainpic/mainpicwidget.cpp index 6fe88dc..817623b 100644 --- a/groundstation/mainpic/mainpicwidget.cpp +++ b/groundstation/mainpic/mainpicwidget.cpp @@ -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(); @@ -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; @@ -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){ @@ -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 - } } diff --git a/groundstation/mainpic/mainpicwidget.h b/groundstation/mainpic/mainpicwidget.h index d2b8b32..84f5897 100644 --- a/groundstation/mainpic/mainpicwidget.h +++ b/groundstation/mainpic/mainpicwidget.h @@ -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 diff --git a/groundstation/mainpic/target.cpp b/groundstation/mainpic/target.cpp index 46df762..e2bea6c 100644 --- a/groundstation/mainpic/target.cpp +++ b/groundstation/mainpic/target.cpp @@ -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 Target::FIELD_NAMES = QVector() << "Name" << "x" << "y"; Target::Target(){ } diff --git a/groundstation/mainpic/target.h b/groundstation/mainpic/target.h index 042ee28..66f3367 100644 --- a/groundstation/mainpic/target.h +++ b/groundstation/mainpic/target.h @@ -2,8 +2,11 @@ #define TARGET_H #include +#include #include #include +#include +#include using namespace std; @@ -11,6 +14,7 @@ class Target { public: static const QString SER_SEP; + static const QVector FIELD_NAMES; public: QString name; @@ -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