Removed inconsistency when user tried to add dive while planning.

The user could add a dive, and in the middle click on the 'plan'
button. Since we didn't cared about that on the widget, a lot of
inconsistencies could occour. this fixes that by setting a flag
on the Planner, that has now three modes: nothing, plan and add.
(maybe in the future an edit mode will be valid too.)

If in 'NOTHING' mode, user can enter the addition, edition and
planning. If in any other mode, user can't do a thing. The mode
gets back to NOTHING when user accepts or cancels a plan / add
/ edition.

Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2013-11-08 22:09:46 -02:00 committed by Dirk Hohndel
parent f850a0817c
commit 8a970c64c2
4 changed files with 46 additions and 25 deletions

View file

@ -431,13 +431,6 @@ void DivePlannerPointsModel::loadFromDive(dive* d)
* as soon as the model is modified, it will * as soon as the model is modified, it will
* remove all samples from the current dive. * remove all samples from the current dive.
* */ * */
/* On the safe side, clear everything before
editing the new dive. */
beginRemoveRows(QModelIndex(), 0, rowCount()-1);
divepoints.clear();
endRemoveRows();
backupSamples.clear(); backupSamples.clear();
for(int i = 1; i < d->dc.samples-1; i++){ for(int i = 1; i < d->dc.samples-1; i++){
backupSamples.push_back( d->dc.sample[i]); backupSamples.push_back( d->dc.sample[i]);
@ -940,9 +933,9 @@ void DivePlannerWidget::lastStopChanged(bool checked)
plannerModel->setLastStop6m(checked); plannerModel->setLastStop6m(checked);
} }
void DivePlannerPointsModel::setPlanMode(bool isPlan) void DivePlannerPointsModel::setPlanMode(Mode m)
{ {
mode = isPlan ? PLAN : ADD; mode = m;
} }
bool DivePlannerPointsModel::isPlanner() bool DivePlannerPointsModel::isPlanner()
@ -1028,7 +1021,7 @@ int DivePlannerPointsModel::rowCount(const QModelIndex& parent) const
return divepoints.count(); return divepoints.count();
} }
DivePlannerPointsModel::DivePlannerPointsModel(QObject* parent): QAbstractTableModel(parent) DivePlannerPointsModel::DivePlannerPointsModel(QObject* parent): QAbstractTableModel(parent), mode(NOTHING)
{ {
} }
@ -1168,13 +1161,24 @@ void DivePlannerPointsModel::cancelPlan()
return; return;
} }
} }
clear();
emit planCanceled();
setPlanMode(NOTHING);
}
DivePlannerPointsModel::Mode DivePlannerPointsModel::currentMode() const
{
return mode;
}
void DivePlannerPointsModel::clear()
{
beginRemoveRows(QModelIndex(), 0, rowCount()-1); beginRemoveRows(QModelIndex(), 0, rowCount()-1);
divepoints.clear(); divepoints.clear();
endRemoveRows(); endRemoveRows();
emit planCanceled();
} }
void DivePlannerPointsModel::createTemporaryPlan() void DivePlannerPointsModel::createTemporaryPlan()
{ {
// This needs to be done in the following steps: // This needs to be done in the following steps:
@ -1209,9 +1213,7 @@ void DivePlannerPointsModel::createTemporaryPlan()
void DivePlannerPointsModel::undoEdition() void DivePlannerPointsModel::undoEdition()
{ {
beginRemoveRows(QModelIndex(), 0, rowCount()-1); clear();
divepoints.clear();
endRemoveRows();
Q_FOREACH(const sample &s, backupSamples){ Q_FOREACH(const sample &s, backupSamples){
plannerModel->addStop(s.depth.mm, s.time.seconds, tr("Air"), 0); plannerModel->addStop(s.depth.mm, s.time.seconds, tr("Air"), 0);
} }
@ -1249,8 +1251,7 @@ void DivePlannerPointsModel::createPlan()
// Remove and clean the diveplan, so we don't delete // Remove and clean the diveplan, so we don't delete
// the dive by mistake. // the dive by mistake.
diveplan.dp = NULL; diveplan.dp = NULL;
beginRemoveRows(QModelIndex(), 0, rowCount() -1 ); clear();
divepoints.clear();
endRemoveRows();
planCreated(); planCreated();
setPlanMode(NOTHING);
} }

View file

@ -21,6 +21,7 @@ class DivePlannerPointsModel : public QAbstractTableModel{
public: public:
static DivePlannerPointsModel* instance(); static DivePlannerPointsModel* instance();
enum Sections{REMOVE, DEPTH, DURATION, GAS, CCSETPOINT, COLUMNS}; enum Sections{REMOVE, DEPTH, DURATION, GAS, CCSETPOINT, COLUMNS};
enum Mode { NOTHING, PLAN, ADD };
virtual int columnCount(const QModelIndex& parent = QModelIndex()) const; virtual int columnCount(const QModelIndex& parent = QModelIndex()) const;
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;
@ -28,11 +29,11 @@ public:
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(Mode mode);
void setPlanMode(bool);
bool isPlanner(); bool isPlanner();
void createSimpleDive(); void createSimpleDive();
void clear();
Mode currentMode() const;
/** /**
* @return the row number. * @return the row number.
*/ */
@ -63,7 +64,7 @@ signals:
private: private:
explicit DivePlannerPointsModel(QObject* parent = 0); explicit DivePlannerPointsModel(QObject* parent = 0);
struct diveplan diveplan; struct diveplan diveplan;
Modes mode; Mode mode;
QVector<divedatapoint> divepoints; QVector<divedatapoint> divepoints;
struct dive *tempDive; struct dive *tempDive;
void deleteTemporaryPlan(struct divedatapoint *dp); void deleteTemporaryPlan(struct divedatapoint *dp);

View file

@ -497,6 +497,7 @@ void MainTab::acceptChanges()
mainWindow()->showProfile(); mainWindow()->showProfile();
mainWindow()->refreshDisplay(); mainWindow()->refreshDisplay();
mark_divelist_changed(TRUE); mark_divelist_changed(TRUE);
DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::NOTHING);
} }
editMode = NONE; editMode = NONE;
@ -610,6 +611,7 @@ void MainTab::rejectChanges()
updateDiveInfo(selected_dive); updateDiveInfo(selected_dive);
mainWindow()->showProfile(); mainWindow()->showProfile();
mainWindow()->refreshDisplay(); mainWindow()->refreshDisplay();
DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::NOTHING);
} }
editMode = NONE; editMode = NONE;
} }

View file

@ -219,8 +219,14 @@ void MainWindow::enableDcShortcuts()
void MainWindow::on_actionDivePlanner_triggered() void MainWindow::on_actionDivePlanner_triggered()
{ {
if(DivePlannerPointsModel::instance()->currentMode() != DivePlannerPointsModel::NOTHING){
qDebug() << DivePlannerPointsModel::instance()->currentMode();
QMessageBox::warning(this, tr("Warning"), "First finish the current edition before trying to do another." );
return;
}
disableDcShortcuts(); disableDcShortcuts();
DivePlannerPointsModel::instance()->setPlanMode(true); DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::PLAN);
DivePlannerPointsModel::instance()->clear();
ui.stackedWidget->setCurrentIndex(PLANNERPROFILE); ui.stackedWidget->setCurrentIndex(PLANNERPROFILE);
ui.infoPane->setCurrentIndex(PLANNERWIDGET); ui.infoPane->setCurrentIndex(PLANNERWIDGET);
} }
@ -271,6 +277,11 @@ void MainWindow::on_actionEditDeviceNames_triggered()
void MainWindow::on_actionAddDive_triggered() void MainWindow::on_actionAddDive_triggered()
{ {
if(DivePlannerPointsModel::instance()->currentMode() != DivePlannerPointsModel::NOTHING){
QMessageBox::warning(this, tr("Warning"), "First finish the current edition before trying to do another." );
return;
}
// clear the selection // clear the selection
for (int i = 0; i < dive_table.nr; i++) { for (int i = 0; i < dive_table.nr; i++) {
struct dive *d = get_dive(i); struct dive *d = get_dive(i);
@ -278,7 +289,8 @@ void MainWindow::on_actionAddDive_triggered()
deselect_dive(i); deselect_dive(i);
} }
disableDcShortcuts(); disableDcShortcuts();
DivePlannerPointsModel::instance()->setPlanMode(false); DivePlannerPointsModel::instance()->clear();
DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::ADD);
// now cheat - create one dive that we use to store the info tab data in // now cheat - create one dive that we use to store the info tab data in
struct dive *dive = alloc_dive(); struct dive *dive = alloc_dive();
dive->when = QDateTime::currentMSecsSinceEpoch() / 1000L + gettimezoneoffset(); dive->when = QDateTime::currentMSecsSinceEpoch() / 1000L + gettimezoneoffset();
@ -825,12 +837,17 @@ void MainWindow::on_actionImportCSV_triggered()
void MainWindow::editCurrentDive() void MainWindow::editCurrentDive()
{ {
if(DivePlannerPointsModel::instance()->currentMode() != DivePlannerPointsModel::NOTHING){
QMessageBox::warning(this, tr("Warning"), "First finish the current edition before trying to do another." );
return;
}
struct dive *d = current_dive; struct dive *d = current_dive;
QString defaultDC(d->dc.model); QString defaultDC(d->dc.model);
DivePlannerPointsModel::instance()->clear();
if (defaultDC == "manually added dive"){ if (defaultDC == "manually added dive"){
disableDcShortcuts(); disableDcShortcuts();
DivePlannerPointsModel::instance()->setPlanMode(false); DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::ADD);
ui.stackedWidget->setCurrentIndex(PLANNERPROFILE); // Planner. ui.stackedWidget->setCurrentIndex(PLANNERPROFILE); // Planner.
ui.infoPane->setCurrentIndex(MAINTAB); ui.infoPane->setCurrentIndex(MAINTAB);
DivePlannerPointsModel::instance()->loadFromDive(d); DivePlannerPointsModel::instance()->loadFromDive(d);