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-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-04-12 07:24:07 +00:00
|
|
|
|
2013-05-14 01:14:59 +00:00
|
|
|
|
|
|
|
DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelection(false)
|
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-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-16 19:00:33 +00:00
|
|
|
}
|
|
|
|
|
2013-05-28 20:07:43 +00:00
|
|
|
void DiveListView::headerClicked(int i )
|
|
|
|
{
|
2013-05-28 20:46:40 +00:00
|
|
|
QModelIndexList oldSelection = selectionModel()->selectedRows();
|
|
|
|
QList<struct dive*> currentSelectedDives;
|
|
|
|
Q_FOREACH(const QModelIndex& index , oldSelection){
|
|
|
|
struct dive *d = (struct dive *) index.data(TreeItemDT::DIVE_ROLE).value<void*>();
|
|
|
|
if (d){
|
|
|
|
currentSelectedDives.push_back(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 20:07:43 +00:00
|
|
|
if (i == (int) TreeItemDT::NR){
|
|
|
|
reload(DiveTripModel::TREE);
|
|
|
|
}else{
|
|
|
|
reload(DiveTripModel::LIST);
|
|
|
|
}
|
2013-05-28 20:46:40 +00:00
|
|
|
|
|
|
|
QModelIndexList newSelection;
|
|
|
|
QItemSelection newSelection2;
|
|
|
|
|
|
|
|
Q_FOREACH(struct dive *d, currentSelectedDives){
|
|
|
|
QModelIndexList match = model()->match(model()->index(0,0), TreeItemDT::DIVE_ROLE, QVariant::fromValue<void*>(d), 1, Qt::MatchRecursive);
|
|
|
|
if (match.count() == 0){
|
|
|
|
qDebug() << "Well, this shouldn't happen.";
|
|
|
|
}else{
|
|
|
|
newSelection << match.first();
|
|
|
|
}
|
|
|
|
}
|
2013-05-28 20:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListView::reload(DiveTripModel::Layout layout)
|
2013-05-16 19:00:33 +00:00
|
|
|
{
|
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-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-16 19:00:33 +00:00
|
|
|
sortByColumn(0, Qt::DescendingOrder);
|
|
|
|
QModelIndex firstDiveOrTrip = m->index(0,0);
|
2013-05-23 04:25:05 +00:00
|
|
|
if (firstDiveOrTrip.isValid()) {
|
2013-05-16 19:00:33 +00:00
|
|
|
if (m->index(0,0, firstDiveOrTrip).isValid())
|
|
|
|
setCurrentIndex(m->index(0,0, firstDiveOrTrip));
|
|
|
|
else
|
|
|
|
setCurrentIndex(firstDiveOrTrip);
|
|
|
|
}
|
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-26 17:12:45 +00:00
|
|
|
QString title = QString("show %1").arg(model()->headerData(i, Qt::Horizontal).toString());
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListView::setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags command)
|
|
|
|
{
|
|
|
|
if (mouseClickSelection)
|
|
|
|
QTreeView::setSelection(rect, command);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListView::mousePressEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
mouseClickSelection = true;
|
|
|
|
QTreeView::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListView::mouseReleaseEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
mouseClickSelection = false;
|
|
|
|
QTreeView::mouseReleaseEvent(event);
|
|
|
|
}
|
|
|
|
|
2013-05-14 02:42:31 +00:00
|
|
|
void DiveListView::keyPressEvent(QKeyEvent* event)
|
|
|
|
{
|
2013-05-23 04:25:05 +00:00
|
|
|
if (event->modifiers())
|
2013-05-14 02:42:31 +00:00
|
|
|
mouseClickSelection = true;
|
|
|
|
QTreeView::keyPressEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListView::keyReleaseEvent(QKeyEvent* event)
|
|
|
|
{
|
|
|
|
mouseClickSelection = false;
|
|
|
|
QWidget::keyReleaseEvent(event);
|
|
|
|
}
|
|
|
|
|
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*>();
|
|
|
|
if (!dive) { // it's a trip! select first child.
|
|
|
|
dive = (struct dive*) model->data(current.child(0,0), TreeItemDT::DIVE_ROLE).value<void*>();
|
|
|
|
selectedDive = get_divenr(dive);
|
2013-05-23 04:25:05 +00:00
|
|
|
} else {
|
2013-05-14 11:18:26 +00:00
|
|
|
selectedDive = get_divenr(dive);
|
|
|
|
}
|
|
|
|
if (selectedDive == selected_dive)
|
|
|
|
return;
|
|
|
|
Q_EMIT currentDiveChanged(selectedDive);
|
|
|
|
}
|
|
|
|
|
2013-05-14 01:14:59 +00:00
|
|
|
void DiveListView::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
|
|
|
|
{
|
2013-05-14 02:42:31 +00:00
|
|
|
QList<QModelIndex> parents;
|
2013-05-14 01:14:59 +00:00
|
|
|
Q_FOREACH(const QModelIndex& index, deselected.indexes()) {
|
|
|
|
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)) {
|
2013-05-14 07:28:30 +00:00
|
|
|
expand(index); // leave this - even if it looks like it shouldn't be here. looks like I've found a Qt bug.
|
2013-05-14 02:39:49 +00:00
|
|
|
// the subselection is removed, but the painting is not. this cleans the area.
|
2013-05-14 01:14:59 +00:00
|
|
|
}
|
2013-05-14 02:42:31 +00:00
|
|
|
} else if (!parents.contains(index.parent())) {
|
|
|
|
parents.push_back(index.parent());
|
2013-05-14 01:14:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_FOREACH(const QModelIndex& index, selected.indexes()) {
|
|
|
|
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;
|
|
|
|
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-14 02:42:31 +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-14 02:42:31 +00:00
|
|
|
} else if (!parents.contains(index.parent())) {
|
2013-05-14 02:39:49 +00:00
|
|
|
parents.push_back(index.parent());
|
2013-05-14 01:14:59 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-14 02:39:49 +00:00
|
|
|
|
2013-05-14 02:44:13 +00:00
|
|
|
Q_FOREACH(const QModelIndex& index, parents)
|
2013-05-14 02:39:49 +00:00
|
|
|
expand(index);
|
2013-04-12 07:24:07 +00:00
|
|
|
}
|