Reimplement the Sort method to change from Tree / List, and remember selection.

Things are working as they should, but I hit on -probably- a Qt bug
that makes painting on the table view a bit weird ( it only updates
the painting by moving the mouse around ). I'll try to fake the
mouse movements in a couple of commits after this one.
There's also a few columns that are not being correctly sorted,
probably something to do with the SortRole.

Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
This commit is contained in:
Tomaz Canabrava 2013-05-29 11:51:33 -03:00
parent bb77f5a44e
commit f46a2d56bc

View file

@ -29,32 +29,24 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec
void DiveListView::headerClicked(int i ) void DiveListView::headerClicked(int i )
{ {
QModelIndexList oldSelection = selectionModel()->selectedRows(); QItemSelection oldSelection = selectionModel()->selection();
QList<struct dive*> currentSelectedDives; QList<struct dive*> currentSelectedDives;
Q_FOREACH(const QModelIndex& index , oldSelection){ Q_FOREACH(const QModelIndex& index , oldSelection.indexes()){
struct dive *d = (struct dive *) index.data(TreeItemDT::DIVE_ROLE).value<void*>(); struct dive *d = (struct dive *) index.data(TreeItemDT::DIVE_ROLE).value<void*>();
if (d){ if (d){ // can also be a trip, so test.
currentSelectedDives.push_back(d); currentSelectedDives.push_back(d);
} }
} }
if (i == (int) TreeItemDT::NR){ reload( i == (int) TreeItemDT::NR ? DiveTripModel::TREE : DiveTripModel::LIST);
reload(DiveTripModel::TREE);
}else{
reload(DiveTripModel::LIST);
}
QModelIndexList newSelection;
QItemSelection newSelection2;
selectionModel()->clearSelection();
Q_FOREACH(struct dive *d, currentSelectedDives){ Q_FOREACH(struct dive *d, currentSelectedDives){
QModelIndexList match = model()->match(model()->index(0,0), TreeItemDT::DIVE_ROLE, QVariant::fromValue<void*>(d), 1, Qt::MatchRecursive); QModelIndexList match = model()->match(model()->index(0,0), TreeItemDT::DIVE_ROLE, QVariant::fromValue<void*>(d), 1, Qt::MatchRecursive);
if (match.count() == 0){ selectionModel()->select(match.first(), QItemSelectionModel::Select | QItemSelectionModel::Rows);
qDebug() << "Well, this shouldn't happen.";
}else{
newSelection << match.first();
}
} }
sortByColumn(i);
} }
void DiveListView::reload(DiveTripModel::Layout layout) void DiveListView::reload(DiveTripModel::Layout layout)
@ -122,8 +114,8 @@ void DiveListView::toggleColumnVisibilityByIndex()
void DiveListView::setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags command) void DiveListView::setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags command)
{ {
if (mouseClickSelection) //if (mouseClickSelection)
QTreeView::setSelection(rect, command); QTreeView::setSelection(rect, command);
} }
void DiveListView::mousePressEvent(QMouseEvent* event) void DiveListView::mousePressEvent(QMouseEvent* event)