diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2013-06-06 09:06:42 +0900 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2013-06-06 09:06:42 +0900 |
commit | cd7488491ff2a3be57ba018c4fb8f1a30d749679 (patch) | |
tree | 5cfdf90e761c6fd0f01ebddc0d70657c0fed75bf /qt-ui/divelistview.cpp | |
parent | 235833b93e3c065ab0759617a5a331bcdb568ddf (diff) | |
download | subsurface-cd7488491ff2a3be57ba018c4fb8f1a30d749679.tar.gz |
Implement the context menu a different way
It seems that this is the way this is supposed to be done - instead of
manually looking at what kind of click we get, Qt decides when to create
a context menu for us - this way things like the Windows Menu button
will work automagically.
As an example I also implemented the "remove dive from trip"
functionality, which exposes some other bugs (like the fact that the
dive that isn't part of a trip ends up being sorted at the very end of
the dive list).
This commit contains a "testSlot" implementation to remind me how to
figure out which dive / trip we are on.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-ui/divelistview.cpp')
-rw-r--r-- | qt-ui/divelistview.cpp | 44 |
1 files changed, 37 insertions, 7 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(); } |