diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2013-05-29 19:55:24 +0900 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2013-05-29 19:55:24 +0900 |
commit | 8df20f414966db685e4709eb101e071b4783c09f (patch) | |
tree | e92d0772b2a393ae3ca73ebb47ac74e37adeb9d1 | |
parent | 4371a4a29859c9419a646cb7ed1182c19f54d7ab (diff) | |
parent | 5a994b08f6323680fe8362f129f097c9c2aaef2e (diff) | |
download | subsurface-8df20f414966db685e4709eb101e071b4783c09f.tar.gz |
Merge branch 'bug111' of https://github.com/tcanabrava/subsurface
-rw-r--r-- | qt-ui/divelistview.cpp | 41 | ||||
-rw-r--r-- | qt-ui/divelistview.h | 4 | ||||
-rw-r--r-- | qt-ui/mainwindow.ui | 4 | ||||
-rw-r--r-- | qt-ui/models.cpp | 27 | ||||
-rw-r--r-- | qt-ui/models.h | 5 |
5 files changed, 74 insertions, 7 deletions
diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index 4957e3374..948bfa57a 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -22,16 +22,53 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec setItemDelegateForColumn(TreeItemDT::RATING, new StarWidgetsDelegate()); QSortFilterProxyModel *model = new QSortFilterProxyModel(this); setModel(model); + setSortingEnabled(false); header()->setContextMenuPolicy(Qt::ActionsContextMenu); } -void DiveListView::reload() +void DiveListView::headerClicked(int i ) { + 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); + } + } + + if (i == (int) TreeItemDT::NR){ + reload(DiveTripModel::TREE); + }else{ + reload(DiveTripModel::LIST); + } + + 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(); + } + } +} + +void DiveListView::reload(DiveTripModel::Layout layout) +{ + header()->setClickable(true); + connect(header(), SIGNAL(sectionPressed(int)), this, SLOT(headerClicked(int)), Qt::UniqueConnection); + QSortFilterProxyModel *m = qobject_cast<QSortFilterProxyModel*>(model()); QAbstractItemModel *oldModel = m->sourceModel(); if (oldModel) oldModel->deleteLater(); - m->setSourceModel(new DiveTripModel(this)); + DiveTripModel *tripModel = new DiveTripModel(this); + tripModel->setLayout(layout); + + m->setSourceModel(tripModel); sortByColumn(0, Qt::DescendingOrder); QModelIndex firstDiveOrTrip = m->index(0,0); if (firstDiveOrTrip.isValid()) { diff --git a/qt-ui/divelistview.h b/qt-ui/divelistview.h index c1a33db51..99154d4ac 100644 --- a/qt-ui/divelistview.h +++ b/qt-ui/divelistview.h @@ -15,6 +15,7 @@ */ #include <QTreeView> +#include "models.h" class DiveListView : public QTreeView { @@ -28,11 +29,12 @@ public: void keyPressEvent(QKeyEvent* event); void keyReleaseEvent(QKeyEvent*); void setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags command); - void reload(); + void reload(DiveTripModel::Layout layout = DiveTripModel::TREE); public slots: void toggleColumnVisibilityByIndex(); void reloadHeaderActions(); + void headerClicked(int); Q_SIGNALS: void currentDiveChanged(int divenr); diff --git a/qt-ui/mainwindow.ui b/qt-ui/mainwindow.ui index 3f9726785..c72e36eb4 100644 --- a/qt-ui/mainwindow.ui +++ b/qt-ui/mainwindow.ui @@ -73,7 +73,7 @@ <bool>true</bool> </property> <property name="sortingEnabled"> - <bool>true</bool> + <bool>false</bool> </property> <property name="animated"> <bool>true</bool> @@ -103,7 +103,7 @@ <x>0</x> <y>0</y> <width>763</width> - <height>25</height> + <height>20</height> </rect> </property> <widget class="QMenu" name="menuFile"> diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 38ef8938c..98a84d7f9 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -1003,7 +1003,6 @@ DiveTripModel::DiveTripModel(QObject* parent) : QAbstractItemModel(parent) { rootItem = new TreeItemDT(); - setupModelData(); } DiveTripModel::~DiveTripModel() @@ -1096,6 +1095,11 @@ void DiveTripModel::setupModelData() { int i = dive_table.nr; + if (rowCount()){ + beginRemoveRows(QModelIndex(), 0, rowCount()-1); + endRemoveRows(); + } + while (--i >= 0) { struct dive* dive = get_dive(i); update_cylinder_related_info(dive); @@ -1104,11 +1108,14 @@ void DiveTripModel::setupModelData() DiveItem* diveItem = new DiveItem(); diveItem->dive = dive; - if (!trip) { + if (!trip || currentLayout == LIST) { diveItem->parent = rootItem; rootItem->children.push_back(diveItem); continue; } + if (currentLayout == LIST) + continue; + if (!trips.keys().contains(trip)) { TripItem* tripItem = new TripItem(); tripItem->trip = trip; @@ -1121,4 +1128,20 @@ void DiveTripModel::setupModelData() TripItem* tripItem = trips[trip]; tripItem->children.push_back(diveItem); } + + if (rowCount()){ + beginInsertRows(QModelIndex(), 0, rowCount()-1); + endInsertRows(); + } +} + +DiveTripModel::Layout DiveTripModel::layout() const +{ + return currentLayout; +} + +void DiveTripModel::setLayout(DiveTripModel::Layout layout) +{ + currentLayout = layout; + setupModelData(); } diff --git a/qt-ui/models.h b/qt-ui/models.h index f51566c1b..b4eb8d953 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -144,6 +144,8 @@ class DiveTripModel : public QAbstractItemModel Q_OBJECT public: + enum Layout{TREE, LIST}; + DiveTripModel(QObject *parent = 0); ~DiveTripModel(); @@ -155,11 +157,14 @@ public: /*reimp*/ QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; /*reimp*/ QModelIndex parent(const QModelIndex &child) const; + Layout layout() const; + void setLayout(Layout layout); private: void setupModelData(); TreeItemDT *rootItem; QMap<dive_trip_t*, TripItem*> trips; + Layout currentLayout; }; #endif |