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>
|
|
|
|
#include <QKeyEvent>
|
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-14 01:14:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-05-14 02:42:31 +00:00
|
|
|
void DiveListView::keyPressEvent(QKeyEvent* event)
|
|
|
|
{
|
|
|
|
if(event->modifiers())
|
|
|
|
mouseClickSelection = true;
|
|
|
|
QTreeView::keyPressEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListView::keyReleaseEvent(QKeyEvent* event)
|
|
|
|
{
|
|
|
|
mouseClickSelection = false;
|
|
|
|
QWidget::keyReleaseEvent(event);
|
|
|
|
}
|
|
|
|
|
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*>();
|
|
|
|
if (!dive) { // is's a trip!
|
|
|
|
if (model->rowCount(index)) {
|
2013-05-14 02:39:49 +00:00
|
|
|
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.
|
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*>();
|
|
|
|
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);
|
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
|
|
|
}
|