2013-04-14 03:44:02 +00:00
|
|
|
/*
|
|
|
|
* divelistview.cpp
|
|
|
|
*
|
|
|
|
* classes for the divelist of Subsurface
|
|
|
|
*
|
|
|
|
*/
|
2013-04-12 07:24:07 +00:00
|
|
|
#include "divelistview.h"
|
2013-04-27 15:27:27 +00:00
|
|
|
#include "models.h"
|
|
|
|
#include "modeldelegates.h"
|
2013-09-23 05:24:28 +00:00
|
|
|
#include "mainwindow.h"
|
2013-06-04 12:56:20 +00:00
|
|
|
#include "../display.h"
|
2013-05-02 22:27:36 +00:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QHeaderView>
|
2013-05-14 01:14:59 +00:00
|
|
|
#include <QDebug>
|
2013-05-21 19:51:49 +00:00
|
|
|
#include <QSettings>
|
2013-05-14 01:14:59 +00:00
|
|
|
#include <QKeyEvent>
|
2013-05-16 19:00:33 +00:00
|
|
|
#include <QSortFilterProxyModel>
|
2013-05-21 19:51:49 +00:00
|
|
|
#include <QAction>
|
2013-05-30 04:04:42 +00:00
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QKeyEvent>
|
2013-06-05 18:29:11 +00:00
|
|
|
#include <QMenu>
|
2013-04-12 07:24:07 +00:00
|
|
|
|
2013-05-30 04:04:42 +00:00
|
|
|
DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelection(false),
|
|
|
|
currentHeaderClicked(-1), searchBox(new QLineEdit(this))
|
2013-04-12 07:24:07 +00:00
|
|
|
{
|
2013-04-24 15:57:30 +00:00
|
|
|
setUniformRowHeights(true);
|
2013-06-17 21:59:50 +00:00
|
|
|
setItemDelegateForColumn(DiveTripModel::RATING, new StarWidgetsDelegate());
|
2013-05-16 19:00:33 +00:00
|
|
|
QSortFilterProxyModel *model = new QSortFilterProxyModel(this);
|
2013-06-17 21:59:50 +00:00
|
|
|
model->setSortRole(DiveTripModel::SORT_ROLE);
|
2013-05-30 04:04:42 +00:00
|
|
|
model->setFilterKeyColumn(-1); // filter all columns
|
2013-05-16 19:00:33 +00:00
|
|
|
setModel(model);
|
2013-06-12 17:14:25 +00:00
|
|
|
connect(model, SIGNAL(layoutChanged()), this, SLOT(fixMessyQtModelBehaviour()));
|
|
|
|
|
2013-05-28 19:56:58 +00:00
|
|
|
setSortingEnabled(false);
|
2013-06-06 00:06:42 +00:00
|
|
|
setContextMenuPolicy(Qt::DefaultContextMenu);
|
2013-05-21 19:51:49 +00:00
|
|
|
header()->setContextMenuPolicy(Qt::ActionsContextMenu);
|
2013-10-01 13:52:23 +00:00
|
|
|
const QFontMetrics metrics(defaultModelFont());
|
|
|
|
header()->setMinimumHeight(metrics.height() + 10);
|
2013-05-30 04:04:42 +00:00
|
|
|
QAction *showSearchBox = new QAction(tr("Show Search Box"), this);
|
|
|
|
showSearchBox->setShortcut( Qt::CTRL + Qt::Key_F);
|
|
|
|
showSearchBox->setShortcutContext(Qt::ApplicationShortcut);
|
|
|
|
addAction(showSearchBox);
|
|
|
|
|
|
|
|
searchBox->installEventFilter(this);
|
|
|
|
searchBox->hide();
|
|
|
|
connect(showSearchBox, SIGNAL(triggered(bool)), this, SLOT(showSearchEdit()));
|
|
|
|
connect(searchBox, SIGNAL(textChanged(QString)), model, SLOT(setFilterFixedString(QString)));
|
2013-06-14 16:17:46 +00:00
|
|
|
selectedTrips.clear();
|
2013-05-30 04:04:42 +00:00
|
|
|
}
|
|
|
|
|
2013-06-12 17:14:25 +00:00
|
|
|
void DiveListView::fixMessyQtModelBehaviour()
|
|
|
|
{
|
|
|
|
QAbstractItemModel *m = model();
|
|
|
|
for(int i = 0; i < model()->rowCount(); i++){
|
|
|
|
if (m->rowCount( m->index(i, 0) ) != 0){
|
|
|
|
setFirstColumnSpanned(i, QModelIndex(), true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-05 06:41:52 +00:00
|
|
|
void DiveListView::unselectDives()
|
|
|
|
{
|
|
|
|
selectionModel()->clearSelection();
|
|
|
|
}
|
|
|
|
|
2013-06-08 01:25:30 +00:00
|
|
|
void DiveListView::selectDive(struct dive *dive, bool scrollto, bool toggle)
|
2013-06-05 06:41:52 +00:00
|
|
|
{
|
|
|
|
QSortFilterProxyModel *m = qobject_cast<QSortFilterProxyModel*>(model());
|
2013-06-17 21:59:50 +00:00
|
|
|
QModelIndexList match = m->match(m->index(0,0), DiveTripModel::NR, dive->number, 1, Qt::MatchRecursive);
|
2013-06-08 05:57:45 +00:00
|
|
|
QItemSelectionModel::SelectionFlags flags;
|
2013-06-05 06:41:52 +00:00
|
|
|
QModelIndex idx = match.first();
|
|
|
|
|
|
|
|
QModelIndex parent = idx.parent();
|
|
|
|
if (parent.isValid())
|
|
|
|
expand(parent);
|
2013-06-08 01:25:30 +00:00
|
|
|
flags = toggle ? QItemSelectionModel::Toggle : QItemSelectionModel::Select;
|
|
|
|
flags |= QItemSelectionModel::Rows;
|
2013-09-23 05:24:28 +00:00
|
|
|
selectionModel()->select(idx, flags);
|
2013-06-05 06:41:52 +00:00
|
|
|
if (scrollto)
|
|
|
|
scrollTo(idx, PositionAtCenter);
|
|
|
|
}
|
|
|
|
|
2013-05-30 04:04:42 +00:00
|
|
|
void DiveListView::showSearchEdit()
|
|
|
|
{
|
|
|
|
searchBox->show();
|
|
|
|
searchBox->setFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DiveListView::eventFilter(QObject* , QEvent* event)
|
|
|
|
{
|
|
|
|
if(event->type() != QEvent::KeyPress){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QKeyEvent *keyEv = static_cast<QKeyEvent*>(event);
|
|
|
|
if (keyEv->key() != Qt::Key_Escape){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
searchBox->clear();
|
|
|
|
searchBox->hide();
|
|
|
|
QSortFilterProxyModel *m = qobject_cast<QSortFilterProxyModel*>(model());
|
|
|
|
m->setFilterFixedString(QString());
|
|
|
|
return true;
|
2013-05-16 19:00:33 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 04:53:04 +00:00
|
|
|
// NOTE! This loses trip selection, because while we remember the
|
|
|
|
// dives, we don't remember the trips (see the "currentSelectedDives"
|
|
|
|
// list). I haven't figured out how to look up the trip from the
|
|
|
|
// index. TRIP_ROLE vs DIVE_ROLE?
|
2013-05-29 20:43:14 +00:00
|
|
|
void DiveListView::headerClicked(int i)
|
2013-05-28 20:07:43 +00:00
|
|
|
{
|
2013-05-29 14:51:33 +00:00
|
|
|
QItemSelection oldSelection = selectionModel()->selection();
|
2013-05-28 20:46:40 +00:00
|
|
|
QList<struct dive*> currentSelectedDives;
|
2013-05-31 04:53:04 +00:00
|
|
|
DiveTripModel::Layout newLayout;
|
2013-06-05 08:28:22 +00:00
|
|
|
bool first = true;
|
2013-05-31 04:53:04 +00:00
|
|
|
|
2013-06-17 21:59:50 +00:00
|
|
|
newLayout = i == (int) DiveTripModel::NR ? DiveTripModel::TREE : DiveTripModel::LIST;
|
2013-05-31 04:53:04 +00:00
|
|
|
|
2013-05-29 20:43:14 +00:00
|
|
|
Q_FOREACH(const QModelIndex& index , oldSelection.indexes()) {
|
2013-05-29 17:03:36 +00:00
|
|
|
if (index.column() != 0) // We only care about the dives, so, let's stick to rows and discard columns.
|
|
|
|
continue;
|
|
|
|
|
2013-06-17 21:59:50 +00:00
|
|
|
struct dive *d = (struct dive *) index.data(DiveTripModel::DIVE_ROLE).value<void*>();
|
2013-05-29 20:43:14 +00:00
|
|
|
if (d)
|
2013-05-28 20:46:40 +00:00
|
|
|
currentSelectedDives.push_back(d);
|
|
|
|
}
|
|
|
|
|
2013-06-05 08:28:22 +00:00
|
|
|
unselectDives();
|
2013-05-31 04:53:04 +00:00
|
|
|
|
|
|
|
/* No layout change? Just re-sort, and scroll to first selection, making sure all selections are expanded */
|
|
|
|
if (currentLayout == newLayout) {
|
|
|
|
sortByColumn(i);
|
|
|
|
} else {
|
|
|
|
// clear the model, repopulate with new indexes.
|
|
|
|
reload(newLayout, false);
|
|
|
|
sortByColumn(i, Qt::DescendingOrder);
|
|
|
|
}
|
2013-05-29 19:46:27 +00:00
|
|
|
|
2013-06-06 05:04:50 +00:00
|
|
|
// repopulate the selections.
|
2013-05-29 20:43:14 +00:00
|
|
|
Q_FOREACH(struct dive *d, currentSelectedDives) {
|
2013-06-05 08:28:22 +00:00
|
|
|
selectDive(d, first);
|
|
|
|
first = false;
|
2013-05-28 20:46:40 +00:00
|
|
|
}
|
2013-05-28 20:07:43 +00:00
|
|
|
}
|
|
|
|
|
2013-05-29 17:03:36 +00:00
|
|
|
void DiveListView::reload(DiveTripModel::Layout layout, bool forceSort)
|
2013-05-16 19:00:33 +00:00
|
|
|
{
|
2013-06-06 02:22:08 +00:00
|
|
|
if (layout == DiveTripModel::CURRENT)
|
|
|
|
layout = currentLayout;
|
|
|
|
else
|
|
|
|
currentLayout = layout;
|
2013-05-28 20:46:40 +00:00
|
|
|
header()->setClickable(true);
|
|
|
|
connect(header(), SIGNAL(sectionPressed(int)), this, SLOT(headerClicked(int)), Qt::UniqueConnection);
|
|
|
|
|
2013-05-16 19:00:33 +00:00
|
|
|
QSortFilterProxyModel *m = qobject_cast<QSortFilterProxyModel*>(model());
|
|
|
|
QAbstractItemModel *oldModel = m->sourceModel();
|
2013-05-16 21:09:45 +00:00
|
|
|
if (oldModel)
|
|
|
|
oldModel->deleteLater();
|
2013-05-29 17:03:36 +00:00
|
|
|
|
2013-05-28 19:56:58 +00:00
|
|
|
DiveTripModel *tripModel = new DiveTripModel(this);
|
2013-05-28 20:07:43 +00:00
|
|
|
tripModel->setLayout(layout);
|
2013-05-28 19:56:58 +00:00
|
|
|
|
|
|
|
m->setSourceModel(tripModel);
|
2013-05-29 17:03:36 +00:00
|
|
|
|
|
|
|
if(!forceSort)
|
|
|
|
return;
|
|
|
|
|
2013-05-16 19:00:33 +00:00
|
|
|
sortByColumn(0, Qt::DescendingOrder);
|
2013-06-04 12:56:20 +00:00
|
|
|
if (amount_selected && selected_dive >= 0) {
|
2013-06-05 08:15:32 +00:00
|
|
|
selectDive(current_dive, true);
|
2013-06-04 12:56:20 +00:00
|
|
|
} else {
|
|
|
|
QModelIndex firstDiveOrTrip = m->index(0,0);
|
|
|
|
if (firstDiveOrTrip.isValid()) {
|
|
|
|
if (m->index(0,0, firstDiveOrTrip).isValid())
|
|
|
|
setCurrentIndex(m->index(0,0, firstDiveOrTrip));
|
|
|
|
else
|
|
|
|
setCurrentIndex(firstDiveOrTrip);
|
|
|
|
}
|
2013-05-16 19:00:33 +00:00
|
|
|
}
|
2013-05-26 17:12:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListView::reloadHeaderActions()
|
|
|
|
{
|
2013-05-21 19:51:49 +00:00
|
|
|
// Populate the context menu of the headers that will show
|
|
|
|
// the menu to show / hide columns.
|
2013-05-23 04:25:05 +00:00
|
|
|
if (!header()->actions().size()) {
|
2013-05-21 19:51:49 +00:00
|
|
|
QAction *visibleAction = new QAction("Visible:", header());
|
|
|
|
header()->addAction(visibleAction);
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup("DiveListColumnState");
|
2013-05-23 04:25:05 +00:00
|
|
|
for(int i = 0; i < model()->columnCount(); i++) {
|
2013-05-29 21:18:10 +00:00
|
|
|
QString title = QString("%1").arg(model()->headerData(i, Qt::Horizontal).toString());
|
2013-05-26 17:12:45 +00:00
|
|
|
QString settingName = QString("showColumn%1").arg(i);
|
2013-05-21 19:51:49 +00:00
|
|
|
QAction *a = new QAction(title, header());
|
2013-05-26 17:12:45 +00:00
|
|
|
bool shown = s.value(settingName, true).toBool();
|
2013-05-21 19:51:49 +00:00
|
|
|
a->setCheckable(true);
|
2013-05-26 17:12:45 +00:00
|
|
|
a->setChecked(shown);
|
2013-05-21 19:51:49 +00:00
|
|
|
a->setProperty("index", i);
|
2013-05-26 17:12:45 +00:00
|
|
|
a->setProperty("settingName", settingName);
|
|
|
|
connect(a, SIGNAL(triggered(bool)), this, SLOT(toggleColumnVisibilityByIndex()));
|
2013-05-21 19:51:49 +00:00
|
|
|
header()->addAction(a);
|
2013-05-26 17:12:45 +00:00
|
|
|
setColumnHidden(i, !shown);
|
2013-05-21 19:51:49 +00:00
|
|
|
}
|
|
|
|
s.endGroup();
|
2013-06-09 23:09:39 +00:00
|
|
|
} else {
|
|
|
|
// Skip first QAction item ( static text Visible )
|
|
|
|
for(int i = 0; i < model()->columnCount(); i++) {
|
|
|
|
QString title = QString("%1").arg(model()->headerData(i, Qt::Horizontal).toString());
|
|
|
|
header()->actions()[i+1]->setText( title );
|
|
|
|
}
|
2013-05-21 19:51:49 +00:00
|
|
|
}
|
2013-05-14 01:14:59 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 17:12:45 +00:00
|
|
|
void DiveListView::toggleColumnVisibilityByIndex()
|
2013-05-14 01:14:59 +00:00
|
|
|
{
|
2013-05-21 19:51:49 +00:00
|
|
|
QAction *action = qobject_cast<QAction*>(sender());
|
|
|
|
if (!action)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup("DiveListColumnState");
|
2013-05-26 17:12:45 +00:00
|
|
|
s.setValue(action->property("settingName").toString(), action->isChecked());
|
2013-05-21 19:51:49 +00:00
|
|
|
s.endGroup();
|
2013-05-22 02:07:19 +00:00
|
|
|
s.sync();
|
2013-05-26 17:12:45 +00:00
|
|
|
setColumnHidden(action->property("index").toInt(), !action->isChecked());
|
2013-05-14 01:14:59 +00:00
|
|
|
}
|
|
|
|
|
2013-05-14 11:18:26 +00:00
|
|
|
void DiveListView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
|
|
|
|
{
|
|
|
|
if (!current.isValid())
|
|
|
|
return;
|
2013-06-05 07:46:23 +00:00
|
|
|
scrollTo(current);
|
2013-05-14 11:18:26 +00:00
|
|
|
}
|
|
|
|
|
2013-05-14 01:14:59 +00:00
|
|
|
void DiveListView::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
|
|
|
|
{
|
2013-05-29 17:03:36 +00:00
|
|
|
QItemSelection newSelected = selected.size() ? selected : selectionModel()->selection();
|
|
|
|
QItemSelection newDeselected = deselected;
|
|
|
|
|
|
|
|
disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(selectionChanged(QItemSelection,QItemSelection)));
|
|
|
|
disconnect(selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(currentChanged(QModelIndex,QModelIndex)));
|
|
|
|
|
2013-06-14 13:17:38 +00:00
|
|
|
Q_FOREACH(const QModelIndex& index, newDeselected.indexes()) {
|
2013-05-31 12:05:33 +00:00
|
|
|
if (index.column() != 0)
|
2013-05-29 17:03:36 +00:00
|
|
|
continue;
|
2013-05-14 01:14:59 +00:00
|
|
|
const QAbstractItemModel *model = index.model();
|
2013-06-17 21:59:50 +00:00
|
|
|
struct dive *dive = (struct dive*) model->data(index, DiveTripModel::DIVE_ROLE).value<void*>();
|
2013-05-14 07:28:30 +00:00
|
|
|
if (!dive) { // it's a trip!
|
2013-05-14 01:14:59 +00:00
|
|
|
if (model->rowCount(index)) {
|
2013-06-17 21:59:50 +00:00
|
|
|
struct dive *child = (struct dive*) model->data(index.child(0,0), DiveTripModel::DIVE_ROLE).value<void*>();
|
2013-06-14 16:17:46 +00:00
|
|
|
if (child && child->divetrip)
|
|
|
|
selectedTrips.remove(child->divetrip);
|
2013-05-31 12:05:33 +00:00
|
|
|
while (child) {
|
2013-06-14 13:17:38 +00:00
|
|
|
deselect_dive(get_index_for_dive(child));
|
2013-05-31 12:05:33 +00:00
|
|
|
child = child->next;
|
|
|
|
}
|
2013-05-14 01:14:59 +00:00
|
|
|
}
|
2013-05-31 12:05:33 +00:00
|
|
|
} else {
|
2013-06-14 13:17:38 +00:00
|
|
|
deselect_dive(get_index_for_dive(dive));
|
2013-05-31 12:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-14 13:17:38 +00:00
|
|
|
Q_FOREACH(const QModelIndex& index, newSelected.indexes()) {
|
2013-05-31 12:05:33 +00:00
|
|
|
if (index.column() != 0)
|
|
|
|
continue;
|
2013-06-14 13:17:38 +00:00
|
|
|
|
2013-05-31 12:05:33 +00:00
|
|
|
const QAbstractItemModel *model = index.model();
|
2013-06-17 21:59:50 +00:00
|
|
|
struct dive *dive = (struct dive*) model->data(index, DiveTripModel::DIVE_ROLE).value<void*>();
|
2013-05-31 12:05:33 +00:00
|
|
|
if (!dive) { // it's a trip!
|
|
|
|
if (model->rowCount(index)) {
|
2013-06-14 13:17:38 +00:00
|
|
|
QItemSelection selection;
|
2013-06-17 21:59:50 +00:00
|
|
|
struct dive *child = (struct dive*) model->data(index.child(0,0), DiveTripModel::DIVE_ROLE).value<void*>();
|
2013-06-14 16:17:46 +00:00
|
|
|
if (child && child->divetrip)
|
|
|
|
selectedTrips.insert(child->divetrip);
|
2013-05-31 12:05:33 +00:00
|
|
|
while (child) {
|
2013-06-14 13:17:38 +00:00
|
|
|
select_dive(get_index_for_dive(child));
|
2013-05-31 12:05:33 +00:00
|
|
|
child = child->next;
|
|
|
|
}
|
2013-06-14 13:17:38 +00:00
|
|
|
selection.select(index.child(0,0), index.child(model->rowCount(index) -1 , 0));
|
|
|
|
selectionModel()->select(selection, QItemSelectionModel::Select | QItemSelectionModel::Rows);
|
|
|
|
selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select | QItemSelectionModel::NoUpdate);
|
|
|
|
if (!isExpanded(index))
|
|
|
|
expand(index);
|
2013-05-31 12:05:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-06-14 13:17:38 +00:00
|
|
|
select_dive(get_index_for_dive(dive));
|
2013-05-14 01:14:59 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-29 17:03:36 +00:00
|
|
|
QTreeView::selectionChanged(selectionModel()->selection(), newDeselected);
|
|
|
|
connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(selectionChanged(QItemSelection,QItemSelection)));
|
|
|
|
connect(selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(currentChanged(QModelIndex,QModelIndex)));
|
2013-05-31 12:05:33 +00:00
|
|
|
// now that everything is up to date, update the widgets
|
|
|
|
Q_EMIT currentDiveChanged(selected_dive);
|
2013-04-12 07:24:07 +00:00
|
|
|
}
|
2013-06-05 18:29:11 +00:00
|
|
|
|
2013-09-24 04:57:28 +00:00
|
|
|
void DiveListView::mergeDives()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct dive *dive, *maindive = NULL;
|
|
|
|
|
|
|
|
for_each_dive(i, dive) {
|
|
|
|
if (dive->selected) {
|
|
|
|
if (!maindive)
|
|
|
|
maindive = dive;
|
|
|
|
else
|
|
|
|
maindive = merge_two_dives(maindive, dive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mainWindow()->refreshDisplay();
|
|
|
|
}
|
|
|
|
|
2013-06-27 05:16:40 +00:00
|
|
|
void DiveListView::merge_trip(const QModelIndex &a, int offset)
|
|
|
|
{
|
|
|
|
int i = a.row() + offset;
|
|
|
|
QModelIndex b = a.sibling(i,0);
|
|
|
|
|
|
|
|
dive_trip_t *trip_a = (dive_trip_t *) a.data(DiveTripModel::TRIP_ROLE).value<void*>();
|
|
|
|
dive_trip_t *trip_b = (dive_trip_t *) b.data(DiveTripModel::TRIP_ROLE).value<void*>();
|
|
|
|
|
|
|
|
if (trip_a == trip_b || !trip_a || !trip_b)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!trip_a->location)
|
|
|
|
trip_a->location = strdup(trip_b->location);
|
|
|
|
if (!trip_a->notes)
|
|
|
|
trip_a->notes = strdup(trip_b->notes);
|
|
|
|
while (trip_b->dives)
|
|
|
|
add_dive_to_trip(trip_b->dives, trip_a);
|
|
|
|
reload(currentLayout, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListView::mergeTripAbove()
|
|
|
|
{
|
|
|
|
merge_trip(contextMenuIndex, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListView::mergeTripBelow()
|
|
|
|
{
|
|
|
|
merge_trip(contextMenuIndex, +1);
|
|
|
|
}
|
|
|
|
|
2013-06-06 00:06:42 +00:00
|
|
|
void DiveListView::removeFromTrip()
|
2013-06-05 18:29:11 +00:00
|
|
|
{
|
2013-06-17 21:59:50 +00:00
|
|
|
struct dive *d = (struct dive *) contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<void*>();
|
2013-06-06 00:06:42 +00:00
|
|
|
if (!d) // shouldn't happen as we only are setting up this action if this is a dive
|
2013-06-05 18:29:11 +00:00
|
|
|
return;
|
2013-06-06 00:06:42 +00:00
|
|
|
remove_dive_from_trip(d);
|
2013-06-06 01:37:18 +00:00
|
|
|
reload(currentLayout, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListView::deleteDive()
|
|
|
|
{
|
2013-09-23 05:24:28 +00:00
|
|
|
int nr;
|
2013-06-17 21:59:50 +00:00
|
|
|
struct dive *d = (struct dive *) contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<void*>();
|
2013-09-23 05:24:28 +00:00
|
|
|
if (d) {
|
|
|
|
nr = get_divenr(d);
|
2013-06-06 01:37:18 +00:00
|
|
|
delete_single_dive(get_index_for_dive(d));
|
2013-09-23 05:24:28 +00:00
|
|
|
if (amount_selected == 0) {
|
|
|
|
if (nr > 0)
|
|
|
|
select_dive(nr - 1);
|
|
|
|
else
|
|
|
|
mainWindow()->cleanUpEmpty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mainWindow()->refreshDisplay();
|
2013-06-06 01:37:18 +00:00
|
|
|
reload(currentLayout, false);
|
2013-06-06 00:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListView::testSlot()
|
|
|
|
{
|
2013-06-17 21:59:50 +00:00
|
|
|
struct dive *d = (struct dive *) contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<void*>();
|
2013-06-06 00:06:42 +00:00
|
|
|
if (d) {
|
|
|
|
qDebug("testSlot called on dive #%d", d->number);
|
|
|
|
} else {
|
|
|
|
QModelIndex child = contextMenuIndex.child(0, 0);
|
2013-06-17 21:59:50 +00:00
|
|
|
d = (struct dive *) child.data(DiveTripModel::DIVE_ROLE).value<void*>();
|
2013-06-06 00:06:42 +00:00
|
|
|
if (d)
|
|
|
|
qDebug("testSlot called on trip including dive #%d", d->number);
|
|
|
|
else
|
|
|
|
qDebug("testSlot called on trip with no dive");
|
2013-06-05 18:29:11 +00:00
|
|
|
}
|
2013-06-06 00:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListView::contextMenuEvent(QContextMenuEvent *event)
|
|
|
|
{
|
|
|
|
QAction *collapseAction = NULL;
|
|
|
|
// let's remember where we are
|
|
|
|
contextMenuIndex = indexAt(event->pos());
|
2013-06-17 21:59:50 +00:00
|
|
|
struct dive *d = (struct dive *) contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<void*>();
|
2013-06-27 05:16:40 +00:00
|
|
|
dive_trip_t *trip = (dive_trip_t *) contextMenuIndex.data(DiveTripModel::TRIP_ROLE).value<void*>();
|
2013-06-05 18:29:11 +00:00
|
|
|
QMenu popup(this);
|
2013-06-05 21:51:15 +00:00
|
|
|
if (currentLayout == DiveTripModel::TREE) {
|
|
|
|
popup.addAction(tr("expand all"), this, SLOT(expandAll()));
|
|
|
|
popup.addAction(tr("collapse all"), this, SLOT(collapseAll()));
|
|
|
|
collapseAction = popup.addAction(tr("collapse"), this, SLOT(collapseAll()));
|
2013-06-06 00:06:42 +00:00
|
|
|
if (d) {
|
|
|
|
popup.addAction(tr("remove dive from trip"), this, SLOT(removeFromTrip()));
|
|
|
|
}
|
2013-06-27 05:16:40 +00:00
|
|
|
if (trip) {
|
|
|
|
popup.addAction(tr("Merge trip with trip above"), this, SLOT(mergeTripAbove()));
|
|
|
|
popup.addAction(tr("Merge trip with trip below"), this, SLOT(mergeTripBelow()));
|
|
|
|
}
|
2013-06-05 21:51:15 +00:00
|
|
|
}
|
2013-06-14 14:13:06 +00:00
|
|
|
if (d)
|
|
|
|
popup.addAction(tr("delete dive"), this, SLOT(deleteDive()));
|
2013-09-24 10:42:40 +00:00
|
|
|
if (amount_selected > 1 && consecutive_selected())
|
2013-09-24 04:57:28 +00:00
|
|
|
popup.addAction(tr("merge selected dives"), this, SLOT(mergeDives()));
|
2013-06-05 21:39:54 +00:00
|
|
|
// "collapse all" really closes all trips,
|
2013-06-05 21:51:15 +00:00
|
|
|
// "collapse" keeps the trip with the selected dive open
|
2013-06-06 00:06:42 +00:00
|
|
|
QAction * actionTaken = popup.exec(event->globalPos());
|
|
|
|
if (actionTaken == collapseAction && collapseAction) {
|
|
|
|
this->setAnimated(false);
|
2013-06-05 18:29:11 +00:00
|
|
|
selectDive(current_dive, true);
|
2013-06-06 00:06:42 +00:00
|
|
|
this->setAnimated(true);
|
|
|
|
}
|
|
|
|
event->accept();
|
2013-06-05 18:29:11 +00:00
|
|
|
}
|