Merge branch 'addDive'

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2013-09-19 23:02:50 -05:00
commit 46c23c53a1
9 changed files with 116 additions and 35 deletions

13
dive.c
View file

@ -161,6 +161,19 @@ struct dive *alloc_dive(void)
return dive; return dive;
} }
void copy_samples(struct dive* s, struct dive* d)
{
/* instead of carefully copying them one by one and calling add_sample
* over and over again, let's just copy the whole blob */
if (!s || !d)
return;
int nr = s->dc.samples;
d->dc.samples = nr;
d->dc.sample = malloc(nr * sizeof(struct sample));
if (d->dc.sample)
memcpy(d->dc.sample, s->dc.sample, nr * sizeof(struct sample));
}
struct sample *prepare_sample(struct divecomputer *dc) struct sample *prepare_sample(struct divecomputer *dc)
{ {
if (dc) { if (dc) {

3
dive.h
View file

@ -622,6 +622,7 @@ extern unsigned int dc_airtemp(struct divecomputer *dc);
extern struct dive *merge_dives(struct dive *a, struct dive *b, int offset, gboolean prefer_downloaded); extern struct dive *merge_dives(struct dive *a, struct dive *b, int offset, gboolean prefer_downloaded);
extern struct dive *try_to_merge(struct dive *a, struct dive *b, gboolean prefer_downloaded); extern struct dive *try_to_merge(struct dive *a, struct dive *b, gboolean prefer_downloaded);
extern void renumber_dives(int nr); extern void renumber_dives(int nr);
extern void copy_samples(struct dive *s, struct dive *d);
extern void add_gas_switch_event(struct dive *dive, struct divecomputer *dc, int time, int idx); extern void add_gas_switch_event(struct dive *dive, struct divecomputer *dc, int time, int idx);
extern void add_event(struct divecomputer *dc, int time, int type, int flags, int value, const char *name); extern void add_event(struct divecomputer *dc, int time, int type, int flags, int value, const char *name);
@ -719,7 +720,7 @@ void free_dps(struct divedatapoint *dp);
void get_gas_string(int o2, int he, char *buf, int len); void get_gas_string(int o2, int he, char *buf, int len);
struct divedatapoint *create_dp(int time_incr, int depth, int o2, int he, int po2); struct divedatapoint *create_dp(int time_incr, int depth, int o2, int he, int po2);
void dump_plan(struct diveplan *diveplan); void dump_plan(struct diveplan *diveplan);
void plan(struct diveplan *diveplan, char **cached_datap, struct dive **divep, char **error_string_p); void plan(struct diveplan *diveplan, char **cached_datap, struct dive **divep, bool add_deco, char **error_string_p);
void delete_single_dive(int idx); void delete_single_dive(int idx);
struct event *get_next_event(struct event *event, char *name); struct event *get_next_event(struct event *event, char *name);

View file

@ -594,7 +594,7 @@ static void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive)
} }
#endif #endif
void plan(struct diveplan *diveplan, char **cached_datap, struct dive **divep, char **error_string_p) void plan(struct diveplan *diveplan, char **cached_datap, struct dive **divep, bool add_deco, char **error_string_p)
{ {
struct dive *dive; struct dive *dive;
struct sample *sample; struct sample *sample;
@ -623,6 +623,19 @@ void plan(struct diveplan *diveplan, char **cached_datap, struct dive **divep, c
get_gas_from_events(&dive->dc, sample->time.seconds, &o2, &he); get_gas_from_events(&dive->dc, sample->time.seconds, &o2, &he);
po2 = dive->dc.sample[dive->dc.samples - 1].po2; po2 = dive->dc.sample[dive->dc.samples - 1].po2;
depth = dive->dc.sample[dive->dc.samples - 1].depth.mm; depth = dive->dc.sample[dive->dc.samples - 1].depth.mm;
/* if all we wanted was the dive just get us back to the surface */
if (!add_deco) {
transitiontime = depth / 150; /* this still needs to be made configurable */
plan_add_segment(diveplan, transitiontime, 0, o2, he, po2);
/* re-create the dive */
delete_single_dive(dive_table.nr - 1);
*divep = dive = create_dive_from_plan(diveplan, error_string_p);
if (dive)
record_dive(dive);
return;
}
tissue_tolerance = tissue_at_end(dive, cached_datap, error_string_p); tissue_tolerance = tissue_at_end(dive, cached_datap, error_string_p);
ceiling = deco_allowed_depth(tissue_tolerance, diveplan->surface_pressure / 1000.0, dive, 1); ceiling = deco_allowed_depth(tissue_tolerance, diveplan->surface_pressure / 1000.0, dive, 1);
#if DEBUG_PLAN & 4 #if DEBUG_PLAN & 4

View file

@ -6,7 +6,7 @@
extern "C" { extern "C" {
#endif #endif
extern void plan(struct diveplan *diveplan, char **cache_datap, struct dive **divep, char **error_string_p); extern void plan(struct diveplan *diveplan, char **cache_datap, struct dive **divep, bool add_plan, char **error_string_p);
extern int validate_gas(const char *text, int *o2_p, int *he_p); extern int validate_gas(const char *text, int *o2_p, int *he_p);
extern int validate_time(const char *text, int *sec_p, int *rel_p); extern int validate_time(const char *text, int *sec_p, int *rel_p);
extern int validate_depth(const char *text, int *mm_p); extern int validate_depth(const char *text, int *mm_p);

View file

@ -4,6 +4,7 @@
#include "modeldelegates.h" #include "modeldelegates.h"
#include "ui_diveplanner.h" #include "ui_diveplanner.h"
#include "mainwindow.h" #include "mainwindow.h"
#include "maintab.h"
#include "tableview.h" #include "tableview.h"
#include "graphicsview-common.h" #include "graphicsview-common.h"
@ -164,7 +165,7 @@ DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent)
gasListView->hide(); gasListView->hide();
connect(gasListView, SIGNAL(activated(QModelIndex)), this, SLOT(selectGas(QModelIndex))); connect(gasListView, SIGNAL(activated(QModelIndex)), this, SLOT(selectGas(QModelIndex)));
connect(plannerModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(createDecoStops())); connect(plannerModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(drawProfile()));
connect(plannerModel, SIGNAL(rowsInserted(const QModelIndex&,int,int)), connect(plannerModel, SIGNAL(rowsInserted(const QModelIndex&,int,int)),
this, SLOT(pointInserted(const QModelIndex&, int, int))); this, SLOT(pointInserted(const QModelIndex&, int, int)));
@ -185,7 +186,7 @@ void DivePlannerGraphics::pointInserted(const QModelIndex& parent, int start , i
connect(gasChooseBtn, SIGNAL(clicked()), this, SLOT(prepareSelectGas())); connect(gasChooseBtn, SIGNAL(clicked()), this, SLOT(prepareSelectGas()));
gases << gasChooseBtn; gases << gasChooseBtn;
createDecoStops(); drawProfile();
} }
void DivePlannerGraphics::keyDownAction() void DivePlannerGraphics::keyDownAction()
@ -219,7 +220,7 @@ void DivePlannerGraphics::keyUpAction()
plannerModel->editStop(row, dp); plannerModel->editStop(row, dp);
} }
} }
createDecoStops(); drawProfile();
} }
void DivePlannerGraphics::keyLeftAction() void DivePlannerGraphics::keyLeftAction()
@ -303,7 +304,7 @@ void DivePlannerGraphics::pointsRemoved(const QModelIndex& , int start, int end)
gases.pop_back(); gases.pop_back();
} }
scene()->clearSelection(); scene()->clearSelection();
createDecoStops(); drawProfile();
} }
bool intLessThan(int a, int b){ bool intLessThan(int a, int b){
@ -343,7 +344,7 @@ void DivePlannerGraphics::increaseDepth()
return; return;
depthLine->setMaximum( depthLine->maximum() + 10); depthLine->setMaximum( depthLine->maximum() + 10);
depthLine->updateTicks(); depthLine->updateTicks();
createDecoStops(); drawProfile();
} }
void DivePlannerGraphics::increaseTime() void DivePlannerGraphics::increaseTime()
@ -351,7 +352,7 @@ void DivePlannerGraphics::increaseTime()
minMinutes += 10; minMinutes += 10;
timeLine->setMaximum( minMinutes ); timeLine->setMaximum( minMinutes );
timeLine->updateTicks(); timeLine->updateTicks();
createDecoStops(); drawProfile();
} }
void DivePlannerGraphics::decreaseDepth() void DivePlannerGraphics::decreaseDepth()
@ -370,7 +371,7 @@ void DivePlannerGraphics::decreaseDepth()
} }
depthLine->setMaximum(depthLine->maximum() - 10); depthLine->setMaximum(depthLine->maximum() - 10);
depthLine->updateTicks(); depthLine->updateTicks();
createDecoStops(); drawProfile();
} }
void DivePlannerGraphics::decreaseTime() void DivePlannerGraphics::decreaseTime()
@ -385,7 +386,7 @@ void DivePlannerGraphics::decreaseTime()
minMinutes -= 10; minMinutes -= 10;
timeLine->setMaximum(timeLine->maximum() -10); timeLine->setMaximum(timeLine->maximum() -10);
timeLine->updateTicks(); timeLine->updateTicks();
createDecoStops(); drawProfile();
} }
void DivePlannerGraphics::mouseDoubleClickEvent(QMouseEvent* event) void DivePlannerGraphics::mouseDoubleClickEvent(QMouseEvent* event)
@ -415,7 +416,7 @@ void DivePlannerGraphics::selectGas(const QModelIndex& index)
gasListView->hide(); gasListView->hide();
} }
void DivePlannerGraphics::createDecoStops() void DivePlannerGraphics::drawProfile()
{ {
qDeleteAll(lines); qDeleteAll(lines);
lines.clear(); lines.clear();
@ -552,7 +553,7 @@ void DivePlannerGraphics::moveActiveHandler(const QPointF& mappedPos, const int
qDeleteAll(lines); qDeleteAll(lines);
lines.clear(); lines.clear();
createDecoStops(); drawProfile();
} }
@ -620,7 +621,7 @@ void DivePlannerGraphics::mouseReleaseEvent(QMouseEvent* event)
activeDraggedHandler->setPos(QPointF(xpos, ypos)); activeDraggedHandler->setPos(QPointF(xpos, ypos));
activeDraggedHandler = 0; activeDraggedHandler = 0;
createDecoStops(); drawProfile();
} }
} }
@ -908,6 +909,16 @@ void DivePlannerWidget::lastStopChanged(bool checked)
plannerModel->setLastStop6m(checked); plannerModel->setLastStop6m(checked);
} }
void DivePlannerPointsModel::setPlanMode(bool isPlan)
{
mode = isPlan ? PLAN : ADD;
}
bool DivePlannerPointsModel::isPlanner()
{
return mode == PLAN;
}
int DivePlannerPointsModel::columnCount(const QModelIndex& parent) const int DivePlannerPointsModel::columnCount(const QModelIndex& parent) const
{ {
return COLUMNS; return COLUMNS;
@ -1116,7 +1127,7 @@ struct diveplan DivePlannerPointsModel::getDiveplan()
void DivePlannerPointsModel::cancelPlan() void DivePlannerPointsModel::cancelPlan()
{ {
if(rowCount()){ if(mode == PLAN && rowCount()){
if (QMessageBox::warning(mainWindow(), tr("Save the Plan?"), if (QMessageBox::warning(mainWindow(), tr("Save the Plan?"),
tr("You have a working plan, \n are you sure that you wanna cancel it?"), tr("You have a working plan, \n are you sure that you wanna cancel it?"),
QMessageBox::Ok | QMessageBox::Cancel) != QMessageBox::Ok){ QMessageBox::Ok | QMessageBox::Cancel) != QMessageBox::Ok){
@ -1152,7 +1163,9 @@ void DivePlannerPointsModel::createTemporaryPlan()
char *cache = NULL; char *cache = NULL;
tempDive = NULL; tempDive = NULL;
char *errorString = NULL; char *errorString = NULL;
plan(&diveplan, &cache, &tempDive, &errorString); plan(&diveplan, &cache, &tempDive, isPlanner(), &errorString);
if (mode == ADD)
copy_samples(tempDive, current_dive);
#if DEBUG_PLAN #if DEBUG_PLAN
dump_plan(&diveplan); dump_plan(&diveplan);
#endif #endif
@ -1185,7 +1198,7 @@ void DivePlannerPointsModel::createPlan()
char *errorString = NULL; char *errorString = NULL;
createTemporaryPlan(); createTemporaryPlan();
plan(&diveplan, &cache, &tempDive, &errorString); plan(&diveplan, &cache, &tempDive, isPlanner(), &errorString);
mark_divelist_changed(TRUE); mark_divelist_changed(TRUE);
// Remove and clean the diveplan, so we don't delete // Remove and clean the diveplan, so we don't delete

View file

@ -29,9 +29,12 @@ public:
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const; virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole); virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
virtual Qt::ItemFlags flags(const QModelIndex& index) const; virtual Qt::ItemFlags flags(const QModelIndex& index) const;
void removeSelectedPoints(const QVector<int>& rows); void removeSelectedPoints(const QVector<int>& rows);
enum Modes { PLAN, ADD };
void setPlanMode(bool);
bool isPlanner();
/** /**
* @return the row number. * @return the row number.
@ -50,9 +53,9 @@ public slots:
void setLastStop6m(bool value); void setLastStop6m(bool value);
void createPlan(); void createPlan();
void remove(const QModelIndex& index); void remove(const QModelIndex& index);
void cancelPlan(); void cancelPlan();
void createTemporaryPlan(); void createTemporaryPlan();
void deleteTemporaryPlan(); void deleteTemporaryPlan();
signals: signals:
void planCreated(); void planCreated();
@ -61,6 +64,7 @@ signals:
private: private:
explicit DivePlannerPointsModel(QObject* parent = 0); explicit DivePlannerPointsModel(QObject* parent = 0);
struct diveplan diveplan; struct diveplan diveplan;
Modes mode;
QVector<divedatapoint> divepoints; QVector<divedatapoint> divepoints;
struct dive *tempDive; struct dive *tempDive;
void deleteTemporaryPlan(struct divedatapoint *dp); void deleteTemporaryPlan(struct divedatapoint *dp);
@ -143,7 +147,7 @@ private slots:
void increaseDepth(); void increaseDepth();
void decreaseTime(); void decreaseTime();
void decreaseDepth();; void decreaseDepth();;
void createDecoStops(); void drawProfile();
void prepareSelectGas(); void prepareSelectGas();
void selectGas(const QModelIndex& index); void selectGas(const QModelIndex& index);
void pointInserted(const QModelIndex&, int start, int end); void pointInserted(const QModelIndex&, int start, int end);

View file

@ -14,6 +14,7 @@
#include "modeldelegates.h" #include "modeldelegates.h"
#include "globe.h" #include "globe.h"
#include "completionmodels.h" #include "completionmodels.h"
#include "diveplanner.h"
#include <QLabel> #include <QLabel>
#include <QCompleter> #include <QCompleter>
@ -90,9 +91,15 @@ MainTab::MainTab(QWidget *parent) : QTabWidget(parent),
ui->suit->setCompleter(completers.suit); ui->suit->setCompleter(completers.suit);
} }
void MainTab::addDiveStarted()
{
enableEdition();
editMode = ADD;
}
void MainTab::enableEdition() void MainTab::enableEdition()
{ {
if (!selected_dive) if (selected_dive < 0 || editMode != NONE)
return; return;
mainWindow()->dive_list()->setEnabled(false); mainWindow()->dive_list()->setEnabled(false);
@ -306,6 +313,7 @@ void MainTab::updateDiveInfo(int dive)
} else { } else {
/* clear the fields */ /* clear the fields */
ui->rating->setCurrentStars(0); ui->rating->setCurrentStars(0);
ui->coordinates->clear();
ui->sacText->clear(); ui->sacText->clear();
ui->otuText->clear(); ui->otuText->clear();
ui->oxygenHeliumText->clear(); ui->oxygenHeliumText->clear();
@ -387,6 +395,13 @@ void MainTab::acceptChanges()
mainWindow()->globe()->centerOn(current_dive); mainWindow()->globe()->centerOn(current_dive);
} }
} }
if (editMode == ADD) {
// clean up the dive data (get duration, depth information from samples)
fixup_dive(current_dive);
DivePlannerPointsModel::instance()->cancelPlan();
mainWindow()->showProfile();
mainWindow()->refreshDisplay();
}
editMode = NONE; editMode = NONE;
QPalette p; QPalette p;
@ -462,6 +477,14 @@ void MainTab::rejectChanges()
ui->coordinates->setPalette(p); ui->coordinates->setPalette(p);
ui->divemaster->setPalette(p); ui->divemaster->setPalette(p);
ui->suit->setPalette(p); ui->suit->setPalette(p);
if (editMode == ADD) {
// clean up
delete_single_dive(selected_dive);
selected_dive = -1;
DivePlannerPointsModel::instance()->cancelPlan();
mainWindow()->showProfile();
mainWindow()->refreshDisplay();
}
editMode = NONE; editMode = NONE;
} }
#undef EDIT_TEXT2 #undef EDIT_TEXT2
@ -507,7 +530,7 @@ void MainTab::on_location_textChanged(const QString& text)
// we are editing a trip // we are editing a trip
dive_trip_t *currentTrip = *mainWindow()->dive_list()->selectedTrips.begin(); dive_trip_t *currentTrip = *mainWindow()->dive_list()->selectedTrips.begin();
EDIT_TEXT(currentTrip->location, text); EDIT_TEXT(currentTrip->location, text);
} else if (editMode == DIVE){ } else if (editMode == DIVE || editMode == ADD){
if (!ui->coordinates->isModified() || if (!ui->coordinates->isModified() ||
ui->coordinates->text().trimmed().isEmpty()) { ui->coordinates->text().trimmed().isEmpty()) {
struct dive* dive; struct dive* dive;
@ -547,7 +570,7 @@ void MainTab::on_notes_textChanged()
// we are editing a trip // we are editing a trip
dive_trip_t *currentTrip = *mainWindow()->dive_list()->selectedTrips.begin(); dive_trip_t *currentTrip = *mainWindow()->dive_list()->selectedTrips.begin();
EDIT_TEXT(currentTrip->notes, ui->notes->toPlainText()); EDIT_TEXT(currentTrip->notes, ui->notes->toPlainText());
} else if (editMode == DIVE) { } else if (editMode == DIVE || editMode == ADD) {
EDIT_SELECTED_DIVES( EDIT_TEXT(mydive->notes, ui->notes->toPlainText()) ); EDIT_SELECTED_DIVES( EDIT_TEXT(mydive->notes, ui->notes->toPlainText()) );
} }
markChangedWidget(ui->notes); markChangedWidget(ui->notes);

View file

@ -68,13 +68,14 @@ public slots:
void on_visibility_valueChanged(int value); void on_visibility_valueChanged(int value);
void editCylinderWidget(const QModelIndex& index); void editCylinderWidget(const QModelIndex& index);
void editWeigthWidget(const QModelIndex& index); void editWeigthWidget(const QModelIndex& index);
void addDiveStarted();
private: private:
Ui::MainTab *ui; Ui::MainTab *ui;
WeightModel *weightModel; WeightModel *weightModel;
CylindersModel *cylindersModel; CylindersModel *cylindersModel;
QMap<dive*, NotesBackup> notesBackup; QMap<dive*, NotesBackup> notesBackup;
enum { NONE, DIVE, TRIP } editMode; enum { NONE, DIVE, TRIP, ADD } editMode;
Completers completers; Completers completers;
void enableEdition(); void enableEdition();
}; };

View file

@ -36,6 +36,8 @@
#include "about.h" #include "about.h"
#include "printdialog.h" #include "printdialog.h"
#include "glib/gi18n.h"
static MainWindow* instance = 0; static MainWindow* instance = 0;
MainWindow* mainWindow() MainWindow* mainWindow()
@ -209,6 +211,7 @@ void MainWindow::enableDcShortcuts()
void MainWindow::on_actionDivePlanner_triggered() void MainWindow::on_actionDivePlanner_triggered()
{ {
disableDcShortcuts(); disableDcShortcuts();
DivePlannerPointsModel::instance()->setPlanMode(true);
ui->stackedWidget->setCurrentIndex(1); ui->stackedWidget->setCurrentIndex(1);
ui->infoPane->setCurrentIndex(1); ui->infoPane->setCurrentIndex(1);
} }
@ -254,15 +257,25 @@ void MainWindow::on_actionEditDeviceNames_triggered()
void MainWindow::on_actionAddDive_triggered() void MainWindow::on_actionAddDive_triggered()
{ {
struct dive *dive; // clear the selection
dive = alloc_dive(); for (int i = 0; i < dive_table.nr; i++) {
struct dive *d = get_dive(i);
if (d && d->selected)
deselect_dive(i);
}
disableDcShortcuts();
DivePlannerPointsModel::instance()->setPlanMode(false);
// now cheat - create one dive that we use to store the info tab data in
struct dive *dive = alloc_dive();
dive->when = QDateTime::currentMSecsSinceEpoch() / 1000L;
dive->dc.model = _("manually added dive");
record_dive(dive); record_dive(dive);
process_dives(FALSE, FALSE); select_dive(get_divenr(dive));
ui->InfoWidget->updateDiveInfo(selected_dive);
ui->InfoWidget->reload(); ui->stackedWidget->setCurrentIndex(1);
ui->globe->reload(); ui->infoPane->setCurrentIndex(0);
ui->ListWidget->reload(DiveTripModel::TREE); refreshDisplay();
ui->ListWidget->setFocus(); ui->InfoWidget->addDiveStarted();
} }
void MainWindow::on_actionRenumber_triggered() void MainWindow::on_actionRenumber_triggered()