summaryrefslogtreecommitdiffstats
path: root/qt-ui/divelistview.cpp
blob: e5d4baddc2d704be7d447e450291feb9e152e1af (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
/*
 * divelistview.cpp
 *
 * classes for the divelist of Subsurface
 *
 */
#include "divelistview.h"
#include "models.h"
#include "modeldelegates.h"
#include <QApplication>
#include <QHeaderView>
#include <QDebug>
#include <QKeyEvent>


DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelection(false)
{
	setUniformRowHeights(true);
	setItemDelegateForColumn(TreeItemDT::RATING, new StarWidgetsDelegate());
}

void DiveListView::setModel(QAbstractItemModel* model)
{
	QTreeView::setModel(model);
}

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);
}

void DiveListView::keyPressEvent(QKeyEvent* event)
{
	if(event->modifiers())
		mouseClickSelection = true;
	QTreeView::keyPressEvent(event);
}

void DiveListView::keyReleaseEvent(QKeyEvent* event)
{
	mouseClickSelection = false;
	QWidget::keyReleaseEvent(event);
}

void DiveListView::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
{
	QList<QModelIndex> parents;
	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*>();
		if (!dive) { // is's a trip!
			if (model->rowCount(index)) {
				expand(index);	// leave this - even if it looks like it shouldn't be here. looks like I'v found a Qt bug.
						// the subselection is removed, but the painting is not. this cleans the area.
			}
		} else if (!parents.contains(index.parent())) {
			parents.push_back(index.parent());
		}
	}

	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*>();
		if (!dive) { // is's a trip!
			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);
				if (!isExpanded(index)) {
					expand(index);
				}
			}
		} else if (!parents.contains(index.parent())) {
			parents.push_back(index.parent());
		}
	}

	Q_FOREACH(const QModelIndex& index, parents) {
		qDebug() << "Expanding";
		expand(index);
	}
}