aboutsummaryrefslogtreecommitdiffstats
path: root/qt-ui/maintab.cpp
blob: bbd9ebad8b9dea53b646e4b6049da344480cf1d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
 * maintab.cpp
 *
 * classes for the "notebook" area of the main window of Subsurface
 *
 */
#include "maintab.h"
#include "ui_maintab.h"
#include "addcylinderdialog.h"
#include "addweightsystemdialog.h"
#include "mainwindow.h"
#include "../helpers.h"
#include "../statistics.h"
#include "divelistview.h"

#include <QLabel>
#include <QDebug>

MainTab::MainTab(QWidget *parent) : QTabWidget(parent),
				    ui(new Ui::MainTab()),
				    weightModel(new WeightModel()),
				    cylindersModel(new CylindersModel()),
				    currentDive(0)
{
	ui->setupUi(this);
	ui->cylinders->setModel(cylindersModel);
	ui->weights->setModel(weightModel);
	ui->diveNotesMessage->hide();
	ui->diveNotesMessage->setCloseButtonVisible(false);

	// we start out with the fields read-only; once things are
	// filled from a dive, they are made writeable
	ui->location->setReadOnly(true);
	ui->divemaster->setReadOnly(true);
	ui->buddy->setReadOnly(true);
	ui->suit->setReadOnly(true);
	ui->notes->setReadOnly(true);
	ui->rating->setReadOnly(true);
	ui->visibility->setReadOnly(true);
	ui->editAccept->hide();
	ui->editReset->hide();

	ui->location->installEventFilter(this);
	ui->divemaster->installEventFilter(this);
	ui->buddy->installEventFilter(this);
	ui->suit->installEventFilter(this);
	ui->notes->installEventFilter(this);
	ui->rating->installEventFilter(this);
	ui->visibility->installEventFilter(this);

	/* example of where code is more concise than Qt designer */
	QList<QObject *> infoTabWidgets = ui->infoTab->children();
	Q_FOREACH(QObject* obj, infoTabWidgets) {
		QLabel* label = qobject_cast<QLabel *>(obj);
		if (label)
			label->setAlignment(Qt::AlignHCenter);
	}
	QList<QObject *> statisticsTabWidgets = ui->statisticsTab->children();
	Q_FOREACH(QObject* obj, statisticsTabWidgets) {
		QLabel* label = qobject_cast<QLabel *>(obj);
		if (label)
			label->setAlignment(Qt::AlignHCenter);
	}

	/*Thid couldn't be done on the ui file because element
	is floating, instead of being fixed on the layout. */
	QIcon plusIcon(":plus");
	addCylinder = new QPushButton(plusIcon, QString(), ui->cylindersGroup);
	addCylinder->setFlat(true);
	addCylinder->setToolTip(tr("Add Cylinder"));
	addWeight = new QPushButton(plusIcon, QString(), ui->weightGroup);
	addWeight->setFlat(true);
	addWeight->setToolTip(tr("Add Weight System"));
}

// We need to manually position the 'plus' on cylinder and weight.
void MainTab::resizeEvent(QResizeEvent* event)
{
	if (ui->cylindersGroup->isVisible())
		addCylinder->setGeometry( ui->cylindersGroup->contentsRect().width() - 30, 2, 24,24);

	if (ui->weightGroup->isVisible())
		addWeight->setGeometry( ui->weightGroup->contentsRect().width() - 30, 2, 24,24);

    QTabWidget::resizeEvent(event);
}

void MainTab::showEvent(QShowEvent* event)
{
	QTabWidget::showEvent(event);
	addCylinder->setGeometry( ui->cylindersGroup->contentsRect().width() - 30, 2, 24,24);
	addWeight->setGeometry( ui->weightGroup->contentsRect().width() - 30, 2, 24,24);
}


bool MainTab::eventFilter(QObject* object, QEvent* event)
{
	if (event->type() == QEvent::FocusIn) {
		if (ui->editAccept->isVisible() || !currentDive)
			return false;

		ui->editAccept->setChecked(true);
		ui->editAccept->show();
		ui->editReset->show();
		on_editAccept_clicked(true);
	}
	return false;
}

void MainTab::clearEquipment()
{
}

void MainTab::clearInfo()
{
	ui->sacText->clear();
	ui->otuText->clear();
	ui->oxygenHeliumText->clear();
	ui->gasUsedText->clear();
	ui->dateText->clear();
	ui->diveTimeText->clear();
	ui->surfaceIntervalText->clear();
	ui->maximumDepthText->clear();
	ui->averageDepthText->clear();
	ui->waterTemperatureText->clear();
	ui->airTemperatureText->clear();
	ui->airPressureText->clear();
}

void MainTab::clearStats()
{
	ui->maximumDepthAllText->clear();
	ui->minimumDepthAllText->clear();
	ui->averageDepthAllText->clear();
	ui->maximumSacAllText->clear();
	ui->minimumSacAllText->clear();
	ui->averageSacAllText->clear();
	ui->divesAllText->clear();
	ui->maximumTemperatureAllText->clear();
	ui->minimumTemperatureAllText->clear();
	ui->averageTemperatureAllText->clear();
	ui->totalTimeAllText->clear();
	ui->averageTimeAllText->clear();
	ui->longestAllText->clear();
	ui->shortestAllText->clear();
}

#define UPDATE_TEXT(d, field)				\
	if (!d || !d->field)				\
		ui->field->setText("");			\
	else						\
		ui->field->setText(d->field)


void MainTab::updateDiveInfo(int dive)
{
	// So, this is what happens now:
	// Every tab should be populated from this method,
	// it will be called whenever a new dive is selected
	// I'm already populating the 'notes' box
	// to show how it can be done.
	// If you are unsure about the name of something,
	// open the file maintab.ui on the designer
	// click on the item and check its objectName,
	// the access is ui->objectName from here on.
	volume_t sacVal;
	temperature_t temp;
	struct dive *prevd;
	struct dive *d = get_dive(dive);

	process_selected_dives();
	process_all_dives(d, &prevd);
	currentDive = d;
	UPDATE_TEXT(d, notes);
	UPDATE_TEXT(d, location);
	UPDATE_TEXT(d, suit);
	UPDATE_TEXT(d, divemaster);
	UPDATE_TEXT(d, buddy);
	/* infoTab */
	if (d) {
		/* make the fields writeable */
		ui->location->setReadOnly(false);
		ui->divemaster->setReadOnly(false);
		ui->buddy->setReadOnly(false);
		ui->suit->setReadOnly(false);
		ui->notes->setReadOnly(false);
		ui->rating->setReadOnly(false);
		ui->visibility->setReadOnly(false);
		/* and fill them from the dive */
		ui->rating->setCurrentStars(d->rating);
		ui->maximumDepthText->setText(get_depth_string(d->maxdepth, TRUE));
		ui->averageDepthText->setText(get_depth_string(d->meandepth, TRUE));
		ui->otuText->setText(QString("%1").arg(d->otu));
		ui->waterTemperatureText->setText(get_temperature_string(d->watertemp, TRUE));
		ui->airTemperatureText->setText(get_temperature_string(d->airtemp, TRUE));
		ui->gasUsedText->setText(get_volume_string(get_gas_used(d), TRUE));
		ui->oxygenHeliumText->setText(get_gaslist(d));
		ui->dateText->setText(get_dive_date_string(d->when));
		ui->diveTimeText->setText(QString::number((int)((d->duration.seconds + 30) / 60)));
		if (prevd)
			ui->surfaceIntervalText->setText(get_time_string(d->when - (prevd->when + prevd->duration.seconds), 4));
		if ((sacVal.mliter = d->sac) > 0)
			ui->sacText->setText(get_volume_string(sacVal, TRUE).append(tr("/min")));
		else
			ui->sacText->clear();
		if (d->surface_pressure.mbar)
			/* this is ALWAYS displayed in mbar */
			ui->airPressureText->setText(QString("%1mbar").arg(d->surface_pressure.mbar));
		else
			ui->airPressureText->clear();
		ui->visibility->setCurrentStars(d->visibility);
		ui->maximumDepthAllText->setText(get_depth_string(stats_selection.max_depth, TRUE));
		ui->minimumDepthAllText->setText(get_depth_string(stats_selection.min_depth, TRUE));
		ui->averageDepthAllText->setText(get_depth_string(stats_selection.avg_depth, TRUE));
		ui->maximumSacAllText->setText(get_volume_string(stats_selection.max_sac, TRUE).append(tr("/min")));
		ui->minimumSacAllText->setText(get_volume_string(stats_selection.min_sac, TRUE).append(tr("/min")));
		ui->averageSacAllText->setText(get_volume_string(stats_selection.avg_sac, TRUE).append(tr("/min")));
		ui->divesAllText->setText(QString::number(stats_selection.selection_size));
		temp.mkelvin = stats_selection.max_temp;
		ui->maximumTemperatureAllText->setText(get_temperature_string(temp, TRUE));
		temp.mkelvin = stats_selection.min_temp;
		ui->minimumTemperatureAllText->setText(get_temperature_string(temp, TRUE));
		if (stats_selection.combined_temp && stats_selection.combined_count) {
			const char *unit;
			get_temp_units(0, &unit);
			ui->averageTemperatureAllText->setText(QString("%1%2").arg(stats_selection.combined_temp / stats_selection.combined_count, 0, 'f', 1).arg(unit));
		}
		ui->totalTimeAllText->setText(get_time_string(stats_selection.total_time.seconds, 0));
		int seconds = stats_selection.total_time.seconds;
		if (stats_selection.selection_size)
			seconds /= stats_selection.selection_size;
		ui->averageTimeAllText->setText(get_time_string(seconds, 0));
		ui->longestAllText->setText(get_time_string(stats_selection.longest_time.seconds, 0));
		ui->shortestAllText->setText(get_time_string(stats_selection.shortest_time.seconds, 0));
		cylindersModel->setDive(d);
		weightModel->setDive(d);
	} else {
		/* make the fields read-only */
		ui->location->setReadOnly(true);
		ui->divemaster->setReadOnly(true);
		ui->buddy->setReadOnly(true);
		ui->suit->setReadOnly(true);
		ui->notes->setReadOnly(true);
		ui->rating->setReadOnly(true);
		ui->visibility->setReadOnly(true);
		/* clear the fields */
		ui->rating->setCurrentStars(0);
		ui->sacText->clear();
		ui->otuText->clear();
		ui->oxygenHeliumText->clear();
		ui->dateText->clear();
		ui->diveTimeText->clear();
		ui->surfaceIntervalText->clear();
		ui->maximumDepthText->clear();
		ui->averageDepthText->clear();
		ui->visibility->setCurrentStars(0);
		ui->waterTemperatureText->clear();
		ui->airTemperatureText->clear();
		ui->gasUsedText->clear();
		ui->airPressureText->clear();
		cylindersModel->clear();
		weightModel->clear();
	}
	/* statisticsTab*/
	/* we can access the stats_selection struct, but how do we ensure the relevant dives are selected
	 * if we don't use the gtk widget to drive this?
	 * Maybe call process_selected_dives? Or re-write to query our Qt list view.
	 */
// 	qDebug("max temp %u",stats_selection.max_temp);
// 	qDebug("min temp %u",stats_selection.min_temp);
}

void MainTab::on_addCylinder_clicked()
{
	if (cylindersModel->rowCount() >= MAX_CYLINDERS)
		return;

	AddCylinderDialog dialog(this);
	cylinder_t newCylinder;
	newCylinder.type.description = "";

	dialog.setCylinder(&newCylinder);
	int result = dialog.exec();
	if (result == QDialog::Rejected) {
		return;
	}

	dialog.updateCylinder();
	cylindersModel->add(&newCylinder);
}

void MainTab::on_editCylinder_clicked()
{
}

void MainTab::on_delCylinder_clicked()
{
}

void MainTab::on_addWeight_clicked()
{
	if (weightModel->rowCount() >= MAX_WEIGHTSYSTEMS)
		return;

	AddWeightsystemDialog dialog(this);
	weightsystem_t newWeightsystem;
	newWeightsystem.description = "";
	newWeightsystem.weight.grams = 0;

	dialog.setWeightsystem(&newWeightsystem);
	int result = dialog.exec();
	if (result == QDialog::Rejected)
		return;

	dialog.updateWeightsystem();
	weightModel->add(&newWeightsystem);
}

void MainTab::on_editWeight_clicked()
{
}

void MainTab::on_delWeight_clicked()
{
}

void MainTab::reload()
{
}

void MainTab::on_editAccept_clicked(bool edit)
{
	ui->location->setReadOnly(!edit);
	ui->divemaster->setReadOnly(!edit);
	ui->buddy->setReadOnly(!edit);
	ui->suit->setReadOnly(!edit);
	ui->notes->setReadOnly(!edit);
	ui->rating->setReadOnly(!edit);
	ui->visibility->setReadOnly(!edit);

	mainWindow()->dive_list()->setEnabled(!edit);

	if (edit) {
		ui->diveNotesMessage->setText(tr("This dive is being edited. click on finish / reset when ready."));
		ui->diveNotesMessage->animatedShow();
		notesBackup.buddy = ui->buddy->text();
		notesBackup.suit = ui->suit->text();
		notesBackup.notes = ui->notes->toPlainText();
		notesBackup.divemaster = ui->divemaster->text();
		notesBackup.location = ui->location->text();
		notesBackup.rating = ui->rating->currentStars();
		notesBackup.visibility = ui->visibility->currentStars();
	} else {
		ui->diveNotesMessage->animatedHide();
		ui->editAccept->hide();
		ui->editReset->hide();
		/* now figure out if things have changed */
		if (notesBackup.buddy != ui->buddy->text() ||
		    notesBackup.suit != ui->suit->text() ||
		    notesBackup.notes != ui->notes->toPlainText() ||
		    notesBackup.divemaster != ui->divemaster->text() ||
		    notesBackup.location != ui->location->text() ||
		    notesBackup.visibility != ui->visibility->currentStars() ||
		    notesBackup.rating != ui->rating->currentStars())
			mark_divelist_changed(TRUE);
	}
}

void MainTab::on_editReset_clicked()
{
	if (!ui->editAccept->isChecked())
		return;

	ui->buddy->setText(notesBackup.buddy);
	ui->suit->setText(notesBackup.suit);
	ui->notes->setText(notesBackup.notes);
	ui->divemaster->setText(notesBackup.divemaster);
	ui->location->setText(notesBackup.location);
	ui->rating->setCurrentStars(notesBackup.rating);
	ui->visibility->setCurrentStars(notesBackup.visibility);
	ui->editAccept->setChecked(false);
	ui->diveNotesMessage->animatedHide();

	ui->location->setReadOnly(true);
	ui->divemaster->setReadOnly(true);
	ui->buddy->setReadOnly(true);
	ui->suit->setReadOnly(true);
	ui->notes->setReadOnly(true);
	ui->rating->setReadOnly(true);
	ui->visibility->setReadOnly(true);
	mainWindow()->dive_list()->setEnabled(true);

	ui->editAccept->hide();
	ui->editReset->hide();
}

#define EDIT_NOTES(what, text) \
	QByteArray textByteArray = text.toLocal8Bit(); \
	free(currentDive->what);\
	currentDive->what = strdup(textByteArray.data());

void MainTab::on_buddy_textChanged(const QString& text)
{
	if (!currentDive)
		return;
	EDIT_NOTES(buddy, text);
}

void MainTab::on_divemaster_textChanged(const QString& text)
{
	if (!currentDive)
		return;
	EDIT_NOTES(divemaster, text);
}

void MainTab::on_location_textChanged(const QString& text)
{
	if (!currentDive)
		return;
	EDIT_NOTES(location, text);
}

void MainTab::on_suit_textChanged(const QString& text)
{
	if (!currentDive)
		return;
	EDIT_NOTES(suit, text);
}

void MainTab::on_notes_textChanged()
{
	if (!currentDive)
		return;
	EDIT_NOTES(notes, ui->notes->toPlainText());
}

#undef EDIT_NOTES

void MainTab::on_rating_valueChanged(int value)
{
	if (!currentDive)
		return;
	currentDive->rating = value;
}

void MainTab::on_visibility_valueChanged(int value)
{
	if (!currentDive)
		return;
	currentDive->visibility = value;
}