diff options
-rw-r--r-- | qt-ui/divelistview.cpp | 44 | ||||
-rw-r--r-- | qt-ui/divelistview.h | 6 |
2 files changed, 42 insertions, 8 deletions
diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index 4aeb3b646..eb718fc83 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -29,6 +29,7 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec model->setFilterKeyColumn(-1); // filter all columns setModel(model); setSortingEnabled(false); + setContextMenuPolicy(Qt::DefaultContextMenu); header()->setContextMenuPolicy(Qt::ActionsContextMenu); QAction *showSearchBox = new QAction(tr("Show Search Box"), this); showSearchBox->setShortcut( Qt::CTRL + Qt::Key_F); @@ -272,23 +273,52 @@ void DiveListView::selectionChanged(const QItemSelection& selected, const QItemS Q_EMIT currentDiveChanged(selected_dive); } -void DiveListView::mousePressEvent(QMouseEvent *event) +void DiveListView::removeFromTrip() { - QAction *collapseAction = NULL; - // all we care about is the unmodified right click - if ( ! (event->modifiers() == Qt::NoModifier && event->buttons() & Qt::RightButton)) { - event->ignore(); - QTreeView::mousePressEvent(event); + struct dive *d = (struct dive *) contextMenuIndex.data(TreeItemDT::DIVE_ROLE).value<void*>(); + if (!d) // shouldn't happen as we only are setting up this action if this is a dive return; + remove_dive_from_trip(d); + reload(); +} + +void DiveListView::testSlot() +{ + struct dive *d = (struct dive *) contextMenuIndex.data(TreeItemDT::DIVE_ROLE).value<void*>(); + if (d) { + qDebug("testSlot called on dive #%d", d->number); + } else { + QModelIndex child = contextMenuIndex.child(0, 0); + d = (struct dive *) child.data(TreeItemDT::DIVE_ROLE).value<void*>(); + if (d) + qDebug("testSlot called on trip including dive #%d", d->number); + else + qDebug("testSlot called on trip with no dive"); } +} + +void DiveListView::contextMenuEvent(QContextMenuEvent *event) +{ + QAction *collapseAction = NULL; + // let's remember where we are + contextMenuIndex = indexAt(event->pos()); + struct dive *d = (struct dive *) contextMenuIndex.data(TreeItemDT::DIVE_ROLE).value<void*>(); QMenu popup(this); if (currentLayout == DiveTripModel::TREE) { popup.addAction(tr("expand all"), this, SLOT(expandAll())); popup.addAction(tr("collapse all"), this, SLOT(collapseAll())); collapseAction = popup.addAction(tr("collapse"), this, SLOT(collapseAll())); + if (d) { + popup.addAction(tr("remove dive from trip"), this, SLOT(removeFromTrip())); + } } // "collapse all" really closes all trips, // "collapse" keeps the trip with the selected dive open - if (popup.exec(event->globalPos()) == collapseAction && collapseAction) + QAction * actionTaken = popup.exec(event->globalPos()); + if (actionTaken == collapseAction && collapseAction) { + this->setAnimated(false); selectDive(current_dive, true); + this->setAnimated(true); + } + event->accept(); } diff --git a/qt-ui/divelistview.h b/qt-ui/divelistview.h index 2bce35612..40e2d5892 100644 --- a/qt-ui/divelistview.h +++ b/qt-ui/divelistview.h @@ -28,21 +28,25 @@ public: bool eventFilter(QObject* , QEvent* ); void unselectDives(); void selectDive(struct dive *, bool scrollto = false); - void mousePressEvent(QMouseEvent *event); + void contextMenuEvent(QContextMenuEvent *event); public slots: void toggleColumnVisibilityByIndex(); void reloadHeaderActions(); void headerClicked(int); void showSearchEdit(); + void removeFromTrip(); + void testSlot(); Q_SIGNALS: void currentDiveChanged(int divenr); + private: bool mouseClickSelection; int currentHeaderClicked; DiveTripModel::Layout currentLayout; QLineEdit *searchBox; + QModelIndex contextMenuIndex; }; #endif // DIVELISTVIEW_H |