summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleix Pol <aleixpol@kde.org>2016-10-29 10:23:14 (GMT)
committerAleix Pol <aleixpol@kde.org>2016-10-29 10:23:25 (GMT)
commitc75a2e52d96b559d7ab5f3a9ed4654887212aebb (patch)
treeb80729c172e4ba7f7dac6b606584f431d7ebb57f
parent7304d320e3d5c196f159a67cddb805947e5a2ec1 (diff)
Make context menu on a bookmark menu action work again
Solves a todo from porting to kf5 Reviewed by Albert Astals
-rw-r--r--part.cpp50
-rw-r--r--part.h3
2 files changed, 40 insertions, 13 deletions
diff --git a/part.cpp b/part.cpp
index 2db9387..324ad4c 100644
--- a/part.cpp
+++ b/part.cpp
@@ -44,6 +44,7 @@
#include <QSpinBox>
#include <QStandardPaths>
#include <QWidgetAction>
+#include <QContextMenuEvent>
#include <KAboutApplicationDialog>
#include <KActionCollection>
@@ -2156,15 +2157,8 @@ void Part::slotRenameCurrentViewportBookmark()
slotRenameBookmark( m_document->viewport() );
}
-void Part::slotAboutToShowContextMenu(QMenu * /*menu*/, QAction *action, QMenu *contextMenu)
+bool Part::aboutToShowContextMenu(QMenu * /*menu*/, QAction *action, QMenu *contextMenu)
{
- const QList<QAction*> actions = contextMenu->findChildren<QAction*>(QStringLiteral("OkularPrivateRenameBookmarkActions"));
- foreach(QAction *a, actions)
- {
- contextMenu->removeAction(a);
- delete a;
- }
-
KBookmarkAction *ba = dynamic_cast<KBookmarkAction*>(action);
if (ba != NULL)
{
@@ -2174,6 +2168,7 @@ void Part::slotAboutToShowContextMenu(QMenu * /*menu*/, QAction *action, QMenu *
renameAction->setData(ba->property("htmlRef").toString());
renameAction->setObjectName(QStringLiteral("OkularPrivateRenameBookmarkActions"));
}
+ return ba;
}
void Part::slotPreviousBookmark()
@@ -3009,12 +3004,10 @@ void Part::rebuildBookmarkMenu( bool unplugActions )
bool containerFound = false;
for (int i = 0; !containerFound && i < clients.size(); ++i)
{
- QWidget *container = factory()->container(QStringLiteral("bookmarks"), clients[i]);
+ QMenu *container = dynamic_cast<QMenu*>(factory()->container(QStringLiteral("bookmarks"), clients[i]));
if (container && container->actions().contains(m_bookmarkActions.first()))
{
- Q_ASSERT(dynamic_cast<QMenu*>(container));
- disconnect(container, 0, this, 0);
- connect(container, SIGNAL(aboutToShowContextMenu(QMenu*,QAction*,QMenu*)), this, SLOT(slotAboutToShowContextMenu(QMenu*,QAction*,QMenu*))); // kf5 FIXME
+ container->installEventFilter(this);
containerFound = true;
}
}
@@ -3024,6 +3017,39 @@ void Part::rebuildBookmarkMenu( bool unplugActions )
m_nextBookmark->setEnabled( havebookmarks );
}
+bool Part::eventFilter(QObject * watched, QEvent * event)
+{
+ switch (event->type()) {
+ case QEvent::ContextMenu: {
+ QContextMenuEvent *e = static_cast<QContextMenuEvent *>(event);
+ QMenu *menu = static_cast<QMenu *>(watched);
+
+ QScopedPointer<QMenu> ctxMenu(new QMenu);
+
+ QPoint pos;
+ bool ret = false;
+ if (e->reason() == QContextMenuEvent::Mouse) {
+ pos = e->pos();
+ ret = aboutToShowContextMenu(menu, menu->actionAt(e->pos()), ctxMenu.data());
+ } else if (menu->activeAction()) {
+ pos = menu->actionGeometry(menu->activeAction()).center();
+ ret = aboutToShowContextMenu(menu, menu->activeAction(), ctxMenu.data());
+ }
+ ctxMenu->exec(menu->mapToGlobal(pos));
+
+ if (ret) {
+ event->accept();
+ }
+ return ret;
+ }
+
+ default:
+ break;
+ }
+
+ return false;
+}
+
void Part::updateAboutBackendAction()
{
const KPluginMetaData data = m_document->generatorInfo();
diff --git a/part.h b/part.h
index a9b0b73..4e8bef1 100644
--- a/part.h
+++ b/part.h
@@ -190,7 +190,6 @@ class OKULARPART_EXPORT Part : public KParts::ReadWritePart, public Okular::Docu
void slotAddBookmark();
void slotRenameBookmarkFromMenu();
void slotRenameCurrentViewportBookmark();
- void slotAboutToShowContextMenu(QMenu *menu, QAction *action, QMenu *contextMenu);
void slotPreviousBookmark();
void slotNextBookmark();
void slotFindNext();
@@ -240,6 +239,8 @@ class OKULARPART_EXPORT Part : public KParts::ReadWritePart, public Okular::Docu
void moveSplitter( const int sideWidgetSize );
private:
+ bool aboutToShowContextMenu(QMenu *menu, QAction *action, QMenu *contextMenu);
+ bool eventFilter(QObject * watched, QEvent * event) override;
Document::OpenResult doOpenFile(const QMimeType &mime, const QString &fileNameToOpen, bool *isCompressedFile);
void setupViewerActions();