aboutsummaryrefslogtreecommitdiffstats
path: root/desktop-widgets/tripselectiondialog.cpp
blob: 2623fdc1016335d5117d9b1f284c410169af596a (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
// SPDX-License-Identifier: GPL-2.0
#include "tripselectiondialog.h"
#include "core/trip.h"
#include "core/qthelper.h"
#include <QShortcut>
#include <QPushButton>

TripSelectionDialog::TripSelectionDialog(QWidget *parent) : QDialog(parent)
{
	ui.setupUi(this);
	connect(ui.trips, &QListWidget::itemSelectionChanged, this, &TripSelectionDialog::selectionChanged);

	QShortcut *close = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this);
	connect(close, &QShortcut::activated, this, &QDialog::close);
	QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this);
	connect(quit, &QShortcut::activated, parent, &QWidget::close);

	// We could use a model, but it seems barely worth the hassle.
	QStringList list;
	list.reserve(trip_table.nr);
	for (int i = 0; i < trip_table.nr; ++i)
		list.push_back(get_trip_string(trip_table.trips[i]));
	ui.trips->addItems(list);
}

void TripSelectionDialog::selectionChanged()
{
	ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(selectedTrip() != nullptr);
}

dive_trip *TripSelectionDialog::selectedTrip() const
{
	// Accessing the selected index of a QListWidget is ridiculously cumbersome.
	// Note that "currentItem" is a different beast.
	QModelIndexList rows = ui.trips->selectionModel()->selectedRows();
	if (rows.size() != 1)
		return nullptr;
	int idx = rows[0].row();
	if (idx < 0 || idx >= trip_table.nr)
		return nullptr;
	return trip_table.trips[idx];
}

dive_trip *TripSelectionDialog::getTrip()
{
	return exec() ? selectedTrip() : nullptr;
}