This commit is contained in:
Dirk Hohndel 2013-05-29 19:55:24 +09:00
commit 8df20f4149
5 changed files with 74 additions and 7 deletions

View file

@ -22,16 +22,53 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec
setItemDelegateForColumn(TreeItemDT::RATING, new StarWidgetsDelegate()); setItemDelegateForColumn(TreeItemDT::RATING, new StarWidgetsDelegate());
QSortFilterProxyModel *model = new QSortFilterProxyModel(this); QSortFilterProxyModel *model = new QSortFilterProxyModel(this);
setModel(model); setModel(model);
setSortingEnabled(false);
header()->setContextMenuPolicy(Qt::ActionsContextMenu); 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()); QSortFilterProxyModel *m = qobject_cast<QSortFilterProxyModel*>(model());
QAbstractItemModel *oldModel = m->sourceModel(); QAbstractItemModel *oldModel = m->sourceModel();
if (oldModel) if (oldModel)
oldModel->deleteLater(); oldModel->deleteLater();
m->setSourceModel(new DiveTripModel(this)); DiveTripModel *tripModel = new DiveTripModel(this);
tripModel->setLayout(layout);
m->setSourceModel(tripModel);
sortByColumn(0, Qt::DescendingOrder); sortByColumn(0, Qt::DescendingOrder);
QModelIndex firstDiveOrTrip = m->index(0,0); QModelIndex firstDiveOrTrip = m->index(0,0);
if (firstDiveOrTrip.isValid()) { if (firstDiveOrTrip.isValid()) {

View file

@ -15,6 +15,7 @@
*/ */
#include <QTreeView> #include <QTreeView>
#include "models.h"
class DiveListView : public QTreeView class DiveListView : public QTreeView
{ {
@ -28,11 +29,12 @@ public:
void keyPressEvent(QKeyEvent* event); void keyPressEvent(QKeyEvent* event);
void keyReleaseEvent(QKeyEvent*); void keyReleaseEvent(QKeyEvent*);
void setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags command); void setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags command);
void reload(); void reload(DiveTripModel::Layout layout = DiveTripModel::TREE);
public slots: public slots:
void toggleColumnVisibilityByIndex(); void toggleColumnVisibilityByIndex();
void reloadHeaderActions(); void reloadHeaderActions();
void headerClicked(int);
Q_SIGNALS: Q_SIGNALS:
void currentDiveChanged(int divenr); void currentDiveChanged(int divenr);

View file

@ -73,7 +73,7 @@
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="sortingEnabled"> <property name="sortingEnabled">
<bool>true</bool> <bool>false</bool>
</property> </property>
<property name="animated"> <property name="animated">
<bool>true</bool> <bool>true</bool>
@ -103,7 +103,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>763</width> <width>763</width>
<height>25</height> <height>20</height>
</rect> </rect>
</property> </property>
<widget class="QMenu" name="menuFile"> <widget class="QMenu" name="menuFile">

View file

@ -1003,7 +1003,6 @@ DiveTripModel::DiveTripModel(QObject* parent) :
QAbstractItemModel(parent) QAbstractItemModel(parent)
{ {
rootItem = new TreeItemDT(); rootItem = new TreeItemDT();
setupModelData();
} }
DiveTripModel::~DiveTripModel() DiveTripModel::~DiveTripModel()
@ -1096,6 +1095,11 @@ void DiveTripModel::setupModelData()
{ {
int i = dive_table.nr; int i = dive_table.nr;
if (rowCount()){
beginRemoveRows(QModelIndex(), 0, rowCount()-1);
endRemoveRows();
}
while (--i >= 0) { while (--i >= 0) {
struct dive* dive = get_dive(i); struct dive* dive = get_dive(i);
update_cylinder_related_info(dive); update_cylinder_related_info(dive);
@ -1104,11 +1108,14 @@ void DiveTripModel::setupModelData()
DiveItem* diveItem = new DiveItem(); DiveItem* diveItem = new DiveItem();
diveItem->dive = dive; diveItem->dive = dive;
if (!trip) { if (!trip || currentLayout == LIST) {
diveItem->parent = rootItem; diveItem->parent = rootItem;
rootItem->children.push_back(diveItem); rootItem->children.push_back(diveItem);
continue; continue;
} }
if (currentLayout == LIST)
continue;
if (!trips.keys().contains(trip)) { if (!trips.keys().contains(trip)) {
TripItem* tripItem = new TripItem(); TripItem* tripItem = new TripItem();
tripItem->trip = trip; tripItem->trip = trip;
@ -1121,4 +1128,20 @@ void DiveTripModel::setupModelData()
TripItem* tripItem = trips[trip]; TripItem* tripItem = trips[trip];
tripItem->children.push_back(diveItem); 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();
} }

View file

@ -144,6 +144,8 @@ class DiveTripModel : public QAbstractItemModel
Q_OBJECT Q_OBJECT
public: public:
enum Layout{TREE, LIST};
DiveTripModel(QObject *parent = 0); DiveTripModel(QObject *parent = 0);
~DiveTripModel(); ~DiveTripModel();
@ -155,11 +157,14 @@ public:
/*reimp*/ QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; /*reimp*/ QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
/*reimp*/ QModelIndex parent(const QModelIndex &child) const; /*reimp*/ QModelIndex parent(const QModelIndex &child) const;
Layout layout() const;
void setLayout(Layout layout);
private: private:
void setupModelData(); void setupModelData();
TreeItemDT *rootItem; TreeItemDT *rootItem;
QMap<dive_trip_t*, TripItem*> trips; QMap<dive_trip_t*, TripItem*> trips;
Layout currentLayout;
}; };
#endif #endif