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-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-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-05-02 02:51:34 +00:00
|
|
|
setItemDelegateForColumn(TreeItemDT::RATING, new StarWidgetsDelegate());
|
2013-05-16 19:00:33 +00:00
|
|
|
QSortFilterProxyModel *model = new QSortFilterProxyModel(this);
|
2013-05-29 05:54:39 +00:00
|
|
|
model->setSortRole(TreeItemDT::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-05-28 19:56:58 +00:00
|
|
|
setSortingEnabled(false);
|
2013-05-21 19:51:49 +00:00
|
|
|
header()->setContextMenuPolicy(Qt::ActionsContextMenu);
|
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-05 06:41:52 +00:00
|
|
|
void DiveListView::unselectDives()
|
|
|
|
{
|
|
|
|
selectionModel()->clearSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListView::selectDive(struct dive *dive, bool scrollto)
|
|
|
|
{
|
|
|
|
QSortFilterProxyModel *m = qobject_cast<QSortFilterProxyModel*>(model());
|
|
|
|
QModelIndexList match = m->match(m->index(0,0), TreeItemDT::NR, dive->number, 1, Qt::MatchRecursive);
|
|
|
|
QModelIndex idx = match.first();
|
|
|
|
|
|
|
|
QModelIndex parent = idx.parent();
|
|
|
|
if (parent.isValid())
|
|
|
|
expand(parent);
|
|
|
|
selectionModel()->select( idx, QItemSelectionModel::Select | QItemSelectionModel::Rows);
|
|
|
|
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;
|
|
|
|
bool scrolled = false;
|
|
|
|
|
|
|
|
newLayout = i == (int) TreeItemDT::NR ? DiveTripModel::TREE : DiveTripModel::LIST;
|
|
|
|
|
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-05-28 20:46:40 +00:00
|
|
|
struct dive *d = (struct dive *) index.data(TreeItemDT::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-05-29 14:51:33 +00:00
|
|
|
selectionModel()->clearSelection();
|
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
|
|
|
|
|
|
|
QSortFilterProxyModel *m = qobject_cast<QSortFilterProxyModel*>(model());
|
2013-05-29 17:03:36 +00:00
|
|
|
|
|
|
|
// repopulat the selections.
|
2013-05-29 20:43:14 +00:00
|
|
|
Q_FOREACH(struct dive *d, currentSelectedDives) {
|
2013-05-29 19:46:27 +00:00
|
|
|
QModelIndexList match = m->match(m->index(0,0), TreeItemDT::NR, d->number, 1, Qt::MatchRecursive);
|
2013-05-29 17:03:36 +00:00
|
|
|
QModelIndex idx = match.first();
|
2013-05-31 04:53:04 +00:00
|
|
|
|
|
|
|
if (newLayout == DiveTripModel::TREE) {
|
2013-05-29 19:46:27 +00:00
|
|
|
QModelIndex parent = idx.parent();
|
2013-05-31 04:53:04 +00:00
|
|
|
if (parent.isValid())
|
|
|
|
expand(parent);
|
2013-05-29 19:46:27 +00:00
|
|
|
}
|
2013-05-29 17:03:36 +00:00
|
|
|
selectionModel()->select( idx, QItemSelectionModel::Select | QItemSelectionModel::Rows);
|
2013-05-31 04:53:04 +00:00
|
|
|
if (!scrolled) {
|
|
|
|
scrollTo(idx, PositionAtCenter);
|
|
|
|
scrolled = true;
|
|
|
|
}
|
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-05-29 19:46:27 +00:00
|
|
|
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) {
|
|
|
|
// magic happens and the selected dive is expanded and scrolled to
|
|
|
|
} 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-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;
|
|
|
|
const QAbstractItemModel *model = current.model();
|
|
|
|
int selectedDive = 0;
|
|
|
|
struct dive *dive = (struct dive*) model->data(current, TreeItemDT::DIVE_ROLE).value<void*>();
|
2013-05-29 19:50:25 +00:00
|
|
|
if (!dive) // it's a trip! select first child.
|
2013-05-14 11:18:26 +00:00
|
|
|
dive = (struct dive*) model->data(current.child(0,0), TreeItemDT::DIVE_ROLE).value<void*>();
|
2013-05-29 19:50:25 +00:00
|
|
|
selectedDive = get_divenr(dive);
|
2013-05-14 11:18:26 +00:00
|
|
|
if (selectedDive == selected_dive)
|
|
|
|
return;
|
2013-05-31 04:53:04 +00:00
|
|
|
scrollTo(current);
|
2013-05-14 11:18:26 +00:00
|
|
|
Q_EMIT currentDiveChanged(selectedDive);
|
|
|
|
}
|
|
|
|
|
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)));
|
|
|
|
|
|
|
|
Q_FOREACH(const QModelIndex& index, newSelected.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();
|
|
|
|
struct dive *dive = (struct dive*) model->data(index, TreeItemDT::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)) {
|
|
|
|
QItemSelection selection;
|
2013-05-31 12:05:33 +00:00
|
|
|
struct dive *child = (struct dive*) model->data(index.child(0,0), TreeItemDT::DIVE_ROLE).value<void*>();
|
|
|
|
while (child) {
|
|
|
|
select_dive(get_index_for_dive(child));
|
|
|
|
child = child->next;
|
|
|
|
}
|
2013-05-14 01:14:59 +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);
|
2013-05-29 20:43:14 +00:00
|
|
|
if (!isExpanded(index))
|
2013-05-14 02:39:49 +00:00
|
|
|
expand(index);
|
2013-05-14 01:14:59 +00:00
|
|
|
}
|
2013-05-31 12:05:33 +00:00
|
|
|
} else {
|
|
|
|
select_dive(get_index_for_dive(dive));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Q_FOREACH(const QModelIndex& index, newDeselected.indexes()) {
|
|
|
|
if (index.column() != 0)
|
|
|
|
continue;
|
|
|
|
const QAbstractItemModel *model = index.model();
|
|
|
|
struct dive *dive = (struct dive*) model->data(index, TreeItemDT::DIVE_ROLE).value<void*>();
|
|
|
|
if (!dive) { // it's a trip!
|
|
|
|
if (model->rowCount(index)) {
|
|
|
|
struct dive *child = (struct dive*) model->data(index.child(0,0), TreeItemDT::DIVE_ROLE).value<void*>();
|
|
|
|
while (child) {
|
|
|
|
deselect_dive(get_index_for_dive(child));
|
|
|
|
child = child->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
deselect_dive(get_index_for_dive(dive));
|
2013-05-14 01:14:59 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-14 02:39:49 +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
|
|
|
}
|