From 6c5cf45534f8e10cc96134c3ae716953a131c8fe Mon Sep 17 00:00:00 2001
From: "Robert C. Helling" <helling@atdotde.de>
Date: Thu, 1 Jan 2015 17:00:46 +0100
Subject: [PATCH 3/3] Maintab combobox to set dive type

still needs some work

Signed-off-by: Robert C. Helling <helling@atdotde.de>
---
 dive.c                |  22 ++++-
 dive.h                |   3 +-
 pref.h                |   1 +
 qt-ui/maintab.cpp     |  23 +++++
 qt-ui/maintab.h       |   1 +
 qt-ui/maintab.ui      | 235 ++++++++++++++++++++++++++++----------------------
 qt-ui/preferences.cpp |   1 +
 subsurfacestartup.c   |   3 +-
 8 files changed, 182 insertions(+), 107 deletions(-)

diff --git a/dive.c b/dive.c
index cc86fdd..a36fe71 100644
--- a/dive.c
+++ b/dive.c
@@ -29,7 +29,7 @@ static const char *default_tags[] = {
 const char *cylinderuse_text[] = {
 	QT_TRANSLATE_NOOP("gettextFromC", "OC-gas"), QT_TRANSLATE_NOOP("gettextFromC", "diluent"), QT_TRANSLATE_NOOP("gettextFromC", "oxygen")
 };
-const char *dctype_text[] = { "OC", "CCR", "PSCR" };
+const char *dctype_text[] = { "OC", "CCR", "PSCR", "Freedive" };
 
 int event_is_gaschange(struct event *ev)
 {
@@ -864,6 +864,26 @@ int explicit_first_cylinder(struct dive *dive, struct divecomputer *dc)
 		return 0;
 }
 
+void update_setpoint_events(struct divecomputer *dc)
+{
+	struct event *ev = get_next_event(dc->events, "SP change");
+	bool changed = false;
+	int new_setpoint = 0;
+
+	if (dc->dctype == CCR)
+	    new_setpoint = prefs.defaultsetpoint;
+
+	while (ev) {
+		if (ev->time.seconds == 0) {
+			ev->value = new_setpoint;
+			changed = true;
+		}
+		ev = get_next_event(ev->next, "SP change");
+	}
+	if (!changed)
+		add_event(dc, 0, SAMPLE_EVENT_PO2, 0, new_setpoint, "SP change");
+}
+
 void sanitize_gasmix(struct gasmix *mix)
 {
 	unsigned int o2, he;
diff --git a/dive.h b/dive.h
index f67c736..8779d5f 100644
--- a/dive.h
+++ b/dive.h
@@ -47,7 +47,7 @@ extern "C" {
 #include <stdbool.h>
 #endif
 
-enum dive_comp_type {OC, CCR, PSCR, NUM_DC_TYPE};	// Flags (Open-circuit and Closed-circuit-rebreather) for setting dive computer type
+enum dive_comp_type {OC, CCR, PSCR, FREEDIVE, NUM_DC_TYPE};	// Flags (Open-circuit and Closed-circuit-rebreather) for setting dive computer type
 enum cylinderuse {OC_GAS, DILUENT, OXYGEN, NUM_GAS_USE}; // The different uses for cylinders
 
 extern const char *cylinderuse_text[];
@@ -838,6 +838,7 @@ extern void set_save_userid_local(short value);
 extern void set_userid(char *user_id);
 
 extern const char *get_dive_date_c_string(timestamp_t when);
+extern void update_setpoint_events(struct divecomputer *dc);
 #ifdef __cplusplus
 }
 #endif
diff --git a/pref.h b/pref.h
index 733a194..d2abb3f 100644
--- a/pref.h
+++ b/pref.h
@@ -69,6 +69,7 @@ struct preferences {
 	int decosac;
 	int o2consumption; // ml per min
 	int pscr_ratio; // dump ratio times 1000
+	int defaultsetpoint; // default setpoint in mbar
 	bool show_pictures_in_profile;
 	bool use_default_file;
 };
diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp
index 8981663..009ca64 100644
--- a/qt-ui/maintab.cpp
+++ b/qt-ui/maintab.cpp
@@ -85,6 +85,7 @@ MainTab::MainTab(QWidget *parent) : QTabWidget(parent),
 	ui.dateEdit->installEventFilter(this);
 	ui.timeEdit->installEventFilter(this);
 	ui.tagWidget->installEventFilter(this);
+	ui.DiveType->installEventFilter(this);
 
 	Q_FOREACH (QObject *obj, ui.statisticsTab->children()) {
 		QLabel *label = qobject_cast<QLabel *>(obj);
@@ -99,6 +100,10 @@ MainTab::MainTab(QWidget *parent) : QTabWidget(parent),
 	ui.weights->setBtnToolTip(tr("Add weight system"));
 	connect(ui.weights, SIGNAL(addButtonClicked()), this, SLOT(addWeight_clicked()));
 
+	// This needs to be the same order as enum dive_comp_type in dive.h!
+	ui.DiveType->insertItems(0, QStringList() << "OC" << "CCR" << "pSCR" << "Freedive");
+	connect(ui.DiveType, SIGNAL(currentIndexChanged(int)), this, SLOT(on_divetype_Changed(int)));
+
 	connect(ui.cylinders->view(), SIGNAL(clicked(QModelIndex)), this, SLOT(editCylinderWidget(QModelIndex)));
 	connect(ui.weights->view(), SIGNAL(clicked(QModelIndex)), this, SLOT(editWeightWidget(QModelIndex)));
 
@@ -410,6 +415,7 @@ void MainTab::updateDiveInfo(bool clear)
 	UPDATE_TEXT(displayed_dive, buddy);
 	UPDATE_TEMP(displayed_dive, airtemp);
 	UPDATE_TEMP(displayed_dive, watertemp);
+	ui.DiveType->setCurrentIndex(displayed_dive.dc.dctype);
 
 	if (!clear) {
 		updateGpsCoordinates(&displayed_dive);
@@ -439,6 +445,8 @@ void MainTab::updateDiveInfo(bool clear)
 			ui.TagLabel->setVisible(false);
 			ui.airTempLabel->setVisible(false);
 			ui.airtemp->setVisible(false);
+			ui.DiveType->setVisible(false);
+			ui.TypeLabel->setVisible(false);
 			ui.waterTempLabel->setVisible(false);
 			ui.watertemp->setVisible(false);
 			// rename the remaining fields and fill data from selected trip
@@ -468,6 +476,8 @@ void MainTab::updateDiveInfo(bool clear)
 			ui.tagWidget->setVisible(true);
 			ui.airTempLabel->setVisible(true);
 			ui.airtemp->setVisible(true);
+			ui.TypeLabel->setVisible(true);
+			ui.DiveType->setVisible(true);
 			ui.waterTempLabel->setVisible(true);
 			ui.watertemp->setVisible(true);
 			/* and fill them from the dive */
@@ -489,6 +499,7 @@ void MainTab::updateDiveInfo(bool clear)
 		ui.otuText->setText(QString("%1").arg(displayed_dive.otu));
 		ui.waterTemperatureText->setText(get_temperature_string(displayed_dive.watertemp, true));
 		ui.airTemperatureText->setText(get_temperature_string(displayed_dive.airtemp, true));
+		ui.DiveType->setCurrentIndex(current_dc->dctype);
 		volume_t gases[MAX_CYLINDERS] = {};
 		get_gas_used(&displayed_dive, gases);
 		QString volumes;
@@ -744,6 +755,8 @@ void MainTab::acceptChanges()
 			MODIFY_SELECTED_DIVES(EDIT_VALUE(visibility));
 		if (displayed_dive.airtemp.mkelvin != cd->airtemp.mkelvin)
 			MODIFY_SELECTED_DIVES(EDIT_VALUE(airtemp.mkelvin));
+		if (displayed_dive.dc.dctype != cd->dc.dctype)
+			MODIFY_SELECTED_DIVES(EDIT_VALUE(dc.dctype));
 		if (displayed_dive.watertemp.mkelvin != cd->watertemp.mkelvin)
 			MODIFY_SELECTED_DIVES(EDIT_VALUE(watertemp.mkelvin));
 		if (displayed_dive.when != cd->when) {
@@ -869,6 +882,7 @@ void MainTab::resetPallete()
 	ui.divemaster->setPalette(p);
 	ui.suit->setPalette(p);
 	ui.airtemp->setPalette(p);
+	ui.DiveType->setPalette(p);
 	ui.watertemp->setPalette(p);
 	ui.dateEdit->setPalette(p);
 	ui.timeEdit->setPalette(p);
@@ -980,6 +994,15 @@ void MainTab::on_airtemp_textChanged(const QString &text)
 	validate_temp_field(ui.airtemp, text);
 }
 
+void MainTab::on_divetype_Changed(const int &index)
+{
+	if (editMode == IGNORE)
+		return;
+	displayed_dive.dc.dctype = (enum dive_comp_type) index;
+	update_setpoint_events(&displayed_dive.dc);
+	markChangedWidget(ui.DiveType);
+}
+
 void MainTab::on_watertemp_textChanged(const QString &text)
 {
 	if (editMode == IGNORE)
diff --git a/qt-ui/maintab.h b/qt-ui/maintab.h
index 369dd17..6884f0a 100644
--- a/qt-ui/maintab.h
+++ b/qt-ui/maintab.h
@@ -71,6 +71,7 @@ slots:
 	void on_suit_textChanged(const QString &text);
 	void on_notes_textChanged();
 	void on_airtemp_textChanged(const QString &text);
+	void on_divetype_Changed(const int &index);
 	void on_watertemp_textChanged(const QString &text);
 	void validate_temp_field(QLineEdit *tempField, const QString &text);
 	void on_dateEdit_dateChanged(const QDate &date);
diff --git a/qt-ui/maintab.ui b/qt-ui/maintab.ui
index 47adbf7..96c7a8d 100644
--- a/qt-ui/maintab.ui
+++ b/qt-ui/maintab.ui
@@ -37,8 +37,8 @@
         <rect>
          <x>0</x>
          <y>0</y>
-         <width>670</width>
-         <height>649</height>
+         <width>644</width>
+         <height>639</height>
         </rect>
        </property>
        <layout class="QGridLayout" name="gridLayout_2">
@@ -57,13 +57,6 @@
         <property name="spacing">
          <number>2</number>
         </property>
-        <item row="5" column="0">
-         <widget class="QLabel" name="CoordinatedLabel">
-          <property name="text">
-           <string>Coordinates</string>
-          </property>
-         </widget>
-        </item>
         <item row="1" column="2">
          <layout class="QHBoxLayout" name="temperatureLabels">
           <item>
@@ -82,13 +75,6 @@
           </item>
          </layout>
         </item>
-        <item row="13" column="0">
-         <widget class="QLabel" name="NotesLabel">
-          <property name="text">
-           <string>Notes</string>
-          </property>
-         </widget>
-        </item>
         <item row="2" column="2">
          <layout class="QHBoxLayout" name="airWaterTempLayout">
           <item>
@@ -107,27 +93,41 @@
           </item>
          </layout>
         </item>
-        <item row="4" column="0" colspan="3">
-         <widget class="QLineEdit" name="location">
+        <item row="18" column="0">
+         <widget class="QLabel" name="NotesLabel">
+          <property name="text">
+           <string>Notes</string>
+          </property>
+         </widget>
+        </item>
+        <item row="13" column="2">
+         <widget class="TagWidget" name="buddy">
           <property name="readOnly">
            <bool>false</bool>
           </property>
          </widget>
         </item>
-        <item row="14" column="0" colspan="3">
+        <item row="19" column="0" colspan="3">
          <widget class="QTextEdit" name="notes">
           <property name="readOnly">
            <bool>false</bool>
           </property>
          </widget>
         </item>
-        <item row="9" column="2">
+        <item row="14" column="2">
          <widget class="QLabel" name="SuitLabel">
           <property name="text">
            <string>Suit</string>
           </property>
          </widget>
         </item>
+        <item row="11" column="2">
+         <widget class="QLabel" name="BuddyLabel">
+          <property name="text">
+           <string>Buddy</string>
+          </property>
+         </widget>
+        </item>
         <item row="3" column="0">
          <widget class="QLabel" name="LocationLabel">
           <property name="text">
@@ -135,49 +135,35 @@
           </property>
          </widget>
         </item>
-        <item row="8" column="2">
-         <widget class="TagWidget" name="buddy">
+        <item row="4" column="0" colspan="3">
+         <widget class="QLineEdit" name="location">
           <property name="readOnly">
            <bool>false</bool>
           </property>
          </widget>
         </item>
-        <item row="7" column="2">
-         <widget class="QLabel" name="BuddyLabel">
-          <property name="text">
-           <string>Buddy</string>
-          </property>
-         </widget>
-        </item>
-        <item row="6" column="0" colspan="3">
-         <widget class="QLineEdit" name="coordinates">
+        <item row="15" column="2">
+         <widget class="QLineEdit" name="suit">
           <property name="readOnly">
            <bool>false</bool>
           </property>
          </widget>
         </item>
-        <item row="7" column="0">
-         <widget class="QLabel" name="DivemasterLabel">
-          <property name="text">
-           <string>Divemaster</string>
-          </property>
-         </widget>
-        </item>
-        <item row="10" column="2">
-         <widget class="QLineEdit" name="suit">
+        <item row="13" column="0" colspan="2">
+         <widget class="TagWidget" name="divemaster">
           <property name="readOnly">
            <bool>false</bool>
           </property>
          </widget>
         </item>
-        <item row="11" column="0">
+        <item row="16" column="0">
          <widget class="QLabel" name="TagLabel">
           <property name="text">
            <string>Tags</string>
           </property>
          </widget>
         </item>
-        <item row="12" column="0" colspan="3">
+        <item row="17" column="0" colspan="3">
          <widget class="TagWidget" name="tagWidget">
           <property name="sizePolicy">
            <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
@@ -202,109 +188,151 @@
           </property>
          </widget>
         </item>
-        <item row="8" column="0" colspan="2">
-         <widget class="TagWidget" name="divemaster">
-          <property name="readOnly">
-           <bool>false</bool>
+        <item row="11" column="0">
+         <widget class="QLabel" name="DivemasterLabel">
+          <property name="text">
+           <string>Divemaster</string>
           </property>
          </widget>
         </item>
-        <item row="9" column="0" colspan="2">
-         <layout class="QHBoxLayout" name="ratingVisibilityLabels">
+        <item row="1" column="0">
+         <widget class="QLabel" name="label_2">
+          <property name="text">
+           <string>Date</string>
+          </property>
+         </widget>
+        </item>
+        <item row="2" column="0">
+         <widget class="QDateEdit" name="dateEdit">
+          <property name="calendarPopup">
+           <bool>true</bool>
+          </property>
+          <property name="timeSpec">
+           <enum>Qt::UTC</enum>
+          </property>
+         </widget>
+        </item>
+        <item row="1" column="1">
+         <widget class="QLabel" name="label">
+          <property name="text">
+           <string>Time</string>
+          </property>
+         </widget>
+        </item>
+        <item row="15" column="0" colspan="2">
+         <layout class="QHBoxLayout" name="ratingVisibilityWidgets">
           <item>
-           <widget class="QLabel" name="RatingLabel">
+           <widget class="StarWidget" name="rating" native="true">
             <property name="sizePolicy">
              <sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
               <horstretch>0</horstretch>
               <verstretch>0</verstretch>
              </sizepolicy>
             </property>
-            <property name="text">
-             <string>Rating</string>
+            <property name="focusPolicy">
+             <enum>Qt::StrongFocus</enum>
             </property>
            </widget>
           </item>
           <item>
-           <widget class="QLabel" name="visibilityLabel">
+           <widget class="StarWidget" name="visibility" native="true">
             <property name="sizePolicy">
              <sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
               <horstretch>0</horstretch>
               <verstretch>0</verstretch>
              </sizepolicy>
             </property>
-            <property name="text">
-             <string>Visibility</string>
+            <property name="focusPolicy">
+             <enum>Qt::StrongFocus</enum>
             </property>
            </widget>
           </item>
          </layout>
         </item>
-        <item row="10" column="0" colspan="2">
-         <layout class="QHBoxLayout" name="ratingVisibilityWidgets">
+        <item row="2" column="1">
+         <widget class="QTimeEdit" name="timeEdit">
+          <property name="sizePolicy">
+           <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
+            <horstretch>0</horstretch>
+            <verstretch>0</verstretch>
+           </sizepolicy>
+          </property>
+          <property name="timeSpec">
+           <enum>Qt::UTC</enum>
+          </property>
+         </widget>
+        </item>
+        <item row="14" column="0" colspan="2">
+         <layout class="QHBoxLayout" name="ratingVisibilityLabels">
           <item>
-           <widget class="StarWidget" name="rating" native="true">
+           <widget class="QLabel" name="RatingLabel">
             <property name="sizePolicy">
              <sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
               <horstretch>0</horstretch>
               <verstretch>0</verstretch>
              </sizepolicy>
             </property>
-            <property name="focusPolicy">
-             <enum>Qt::StrongFocus</enum>
+            <property name="text">
+             <string>Rating</string>
             </property>
            </widget>
           </item>
           <item>
-           <widget class="StarWidget" name="visibility" native="true">
+           <widget class="QLabel" name="visibilityLabel">
             <property name="sizePolicy">
              <sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
               <horstretch>0</horstretch>
               <verstretch>0</verstretch>
              </sizepolicy>
             </property>
-            <property name="focusPolicy">
-             <enum>Qt::StrongFocus</enum>
+            <property name="text">
+             <string>Visibility</string>
             </property>
            </widget>
           </item>
          </layout>
         </item>
-        <item row="1" column="1">
-         <widget class="QLabel" name="label">
-          <property name="text">
-           <string>Time</string>
-          </property>
-         </widget>
-        </item>
-        <item row="2" column="1">
-         <widget class="QTimeEdit" name="timeEdit">
-          <property name="sizePolicy">
-           <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
-            <horstretch>0</horstretch>
-            <verstretch>0</verstretch>
-           </sizepolicy>
-          </property>
-          <property name="timeSpec">
-           <enum>Qt::UTC</enum>
-          </property>
-         </widget>
-        </item>
-        <item row="2" column="0">
-         <widget class="QDateEdit" name="dateEdit">
-          <property name="calendarPopup">
-           <bool>true</bool>
-          </property>
-          <property name="timeSpec">
-           <enum>Qt::UTC</enum>
-          </property>
-         </widget>
+        <item row="6" column="0" colspan="3">
+         <layout class="QHBoxLayout" name="CoordinatesLabelLayout">
+          <item>
+           <widget class="QLabel" name="CoordinatedLabel">
+            <property name="text">
+             <string>Coordinates</string>
+            </property>
+           </widget>
+          </item>
+          <item>
+           <widget class="QLabel" name="TypeLabel">
+            <property name="text">
+             <string>Dive type</string>
+            </property>
+            <property name="alignment">
+             <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+            </property>
+           </widget>
+          </item>
+         </layout>
         </item>
-        <item row="1" column="0">
-         <widget class="QLabel" name="label_2">
-          <property name="text">
-           <string>Date</string>
-          </property>
-         </widget>
+        <item row="9" column="0" colspan="3">
+         <layout class="QHBoxLayout" name="CoordinatesTypeLayout">
+          <item>
+           <widget class="QLineEdit" name="coordinates">
+            <property name="readOnly">
+             <bool>false</bool>
+            </property>
+           </widget>
+          </item>
+          <item>
+           <widget class="QComboBox" name="DiveType">
+            <property name="sizePolicy">
+             <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
+              <horstretch>0</horstretch>
+              <verstretch>0</verstretch>
+             </sizepolicy>
+            </property>
+           </widget>
+          </item>
+         </layout>
         </item>
        </layout>
       </widget>
@@ -336,8 +364,8 @@
         <rect>
          <x>0</x>
          <y>0</y>
-         <width>100</width>
-         <height>30</height>
+         <width>644</width>
+         <height>639</height>
         </rect>
        </property>
        <layout class="QGridLayout" name="gridLayout_5">
@@ -416,8 +444,8 @@
         <rect>
          <x>0</x>
          <y>0</y>
-         <width>316</width>
-         <height>330</height>
+         <width>644</width>
+         <height>639</height>
         </rect>
        </property>
        <layout class="QGridLayout" name="gridLayout_6">
@@ -745,8 +773,8 @@
         <rect>
          <x>0</x>
          <y>0</y>
-         <width>319</width>
-         <height>188</height>
+         <width>644</width>
+         <height>639</height>
         </rect>
        </property>
        <layout class="QGridLayout" name="gridLayout_7">
@@ -1015,7 +1043,6 @@
   <tabstop>airtemp</tabstop>
   <tabstop>watertemp</tabstop>
   <tabstop>location</tabstop>
-  <tabstop>coordinates</tabstop>
   <tabstop>divemaster</tabstop>
   <tabstop>buddy</tabstop>
   <tabstop>rating</tabstop>
diff --git a/qt-ui/preferences.cpp b/qt-ui/preferences.cpp
index 5f7825c..b48a4ab 100644
--- a/qt-ui/preferences.cpp
+++ b/qt-ui/preferences.cpp
@@ -338,6 +338,7 @@ void PreferencesDialog::loadSettings()
 	GET_BOOL("show_sac", show_sac);
 	GET_BOOL("display_unused_tanks", display_unused_tanks);
 	GET_BOOL("show_average_depth", show_average_depth);
+	GET_INT("default_setpoint", defaultsetpoint);
 	s.endGroup();
 
 	s.beginGroup("GeneralSettings");
diff --git a/subsurfacestartup.c b/subsurfacestartup.c
index 8847ab3..348cbc2 100644
--- a/subsurfacestartup.c
+++ b/subsurfacestartup.c
@@ -48,7 +48,8 @@ struct preferences default_prefs = {
 	.o2consumption = 1000,
 	.pscr_ratio = 100,
 	.show_pictures_in_profile = true,
-	.tankbar = false
+	.tankbar = false,
+	.defaultsetpoint = 1100
 };
 
 int run_survey;
-- 
1.9.3 (Apple Git-50)

