From e833b3ce6504903df966cd2f0662c8d180b650ff Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Wed, 28 Dec 2016 17:03:28 +0100 Subject: [PATCH] Fix menu titles not showing up QMenu title/section are optional to draw from the style side but we have crucial information in them so reuse old KMenu code to create a fake action that behaves as title I'll see if KF5 wants to accept this as contribution later BUGS: 374079 --- CMakeLists.txt | 1 + part.cpp | 5 ++-- ui/annotationpopup.cpp | 5 ++-- ui/okmenutitle.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++ ui/okmenutitle.h | 31 ++++++++++++++++++++ ui/pageview.cpp | 5 ++-- 6 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 ui/okmenutitle.cpp create mode 100644 ui/okmenutitle.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f3cbc1199..39d4b62ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -264,6 +264,7 @@ set(okularpart_SRCS ui/ktreeviewsearchline.cpp ui/latexrenderer.cpp ui/minibar.cpp + ui/okmenutitle.cpp ui/pageitemdelegate.cpp ui/pagepainter.cpp ui/pagesizelabel.cpp diff --git a/part.cpp b/part.cpp index f0482b86f..fff1ffcca 100644 --- a/part.cpp +++ b/part.cpp @@ -94,6 +94,7 @@ #include "ui/fileprinterpreview.h" #include "ui/guiutils.h" #include "ui/layers.h" +#include "ui/okmenutitle.h" #include "conf/preferencesdialog.h" #include "settings.h" #include "core/action.h" @@ -2518,7 +2519,7 @@ void Part::slotShowMenu(const Okular::Page *page, const QPoint &point) QAction *fitPageWidth = 0; if (page) { - popup->setTitle( i18n( "Page %1", page->number() + 1 ) ); + popup->addAction( new OKMenuTitle( popup, i18n( "Page %1", page->number() + 1 ) ) ); if ( ( !currentPage && m_document->bookmarkManager()->isBookmarked( page->number() ) ) || ( currentPage && m_document->bookmarkManager()->isBookmarked( m_document->viewport() ) ) ) removeBookmark = popup->addAction( QIcon::fromTheme(QStringLiteral("edit-delete-bookmark")), i18n("Remove Bookmark") ); @@ -2533,7 +2534,7 @@ void Part::slotShowMenu(const Okular::Page *page, const QPoint &point) if ((m_showMenuBarAction && !m_showMenuBarAction->isChecked()) || (m_showFullScreenAction && m_showFullScreenAction->isChecked())) { - popup->setTitle( i18n( "Tools" ) ); + popup->addAction( new OKMenuTitle( popup, i18n( "Tools" ) ) ); if (m_showMenuBarAction && !m_showMenuBarAction->isChecked()) popup->addAction(m_showMenuBarAction); if (m_showFullScreenAction && m_showFullScreenAction->isChecked()) popup->addAction(m_showFullScreenAction); reallyShow = true; diff --git a/ui/annotationpopup.cpp b/ui/annotationpopup.cpp index d073f2133..f6efc9fc7 100644 --- a/ui/annotationpopup.cpp +++ b/ui/annotationpopup.cpp @@ -18,6 +18,7 @@ #include "core/annotations.h" #include "core/document.h" #include "guiutils.h" +#include "okmenutitle.h" Q_DECLARE_METATYPE( AnnotationPopup::AnnotPagePair ) @@ -84,7 +85,7 @@ void AnnotationPopup::exec( const QPoint &point ) const AnnotPagePair &pair = mAnnotations.at(0); - menu.setTitle( i18np( "Annotation", "%1 Annotations", mAnnotations.count() ) ); + menu.addAction( new OKMenuTitle( &menu, i18np( "Annotation", "%1 Annotations", mAnnotations.count() ) ) ); action = menu.addAction( QIcon::fromTheme( QStringLiteral("comment") ), i18n( "&Open Pop-up Note" ) ); action->setData( QVariant::fromValue( pair ) ); @@ -124,7 +125,7 @@ void AnnotationPopup::exec( const QPoint &point ) { foreach ( const AnnotPagePair& pair, mAnnotations ) { - menu.setTitle( GuiUtils::captionForAnnotation( pair.annotation ) ); + menu.addAction( new OKMenuTitle( &menu, GuiUtils::captionForAnnotation( pair.annotation ) ) ); action = menu.addAction( QIcon::fromTheme( QStringLiteral("comment") ), i18n( "&Open Pop-up Note" ) ); action->setData( QVariant::fromValue( pair ) ); diff --git a/ui/okmenutitle.cpp b/ui/okmenutitle.cpp new file mode 100644 index 000000000..cc44a5480 --- /dev/null +++ b/ui/okmenutitle.cpp @@ -0,0 +1,65 @@ +/* This file was part of the KDE libraries (copied partially from kmenu.cpp) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "okmenutitle.h" + +#include +#include +#include +#include +#include + +OKMenuTitle::OKMenuTitle(QMenu *menu, const QString &text, const QIcon &icon) + : QWidgetAction(menu) +{ + QAction *buttonAction = new QAction(menu); + QFont font = buttonAction->font(); + font.setBold(true); + buttonAction->setFont(font); + buttonAction->setText(text); + buttonAction->setIcon(icon); + + QToolButton *titleButton = new QToolButton(menu); + titleButton->installEventFilter(this); // prevent clicks on the title of the menu + titleButton->setDefaultAction(buttonAction); + titleButton->setDown(true); // prevent hover style changes in some styles + titleButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); + setDefaultWidget(titleButton); +} + +bool OKMenuTitle::eventFilter(QObject *object, QEvent *event) +{ + Q_UNUSED(object); + + if (event->type() == QEvent::Paint) { + return false; + } else if (event->type() == QEvent::KeyRelease) { + // If we're receiving the key release event is because we just gained + // focus though a key event, use the same key to move it to the next action + if (static_cast(parentWidget())->activeAction() == this) { + QKeyEvent *ke = static_cast(event); + QKeyEvent newKe(QEvent::KeyPress, ke->key(), ke->modifiers(), ke->text(), ke->isAutoRepeat(), ke->count()); + QApplication::sendEvent(parentWidget(), &newKe); + } + + // TODO What happens when there's multiple OKMenuTitle or only OKMenuTitle in a menu + } + + event->accept(); + return true; +} + diff --git a/ui/okmenutitle.h b/ui/okmenutitle.h new file mode 100644 index 000000000..aa9fa3165 --- /dev/null +++ b/ui/okmenutitle.h @@ -0,0 +1,31 @@ +/* This file was part of the KDE libraries (copied partially from kmenu.cpp) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef OKMENUTITLE_H +#define OKMENUTITLE_H + +#include + +class OKMenuTitle : public QWidgetAction +{ +public: + OKMenuTitle(QMenu *menu, const QString &text, const QIcon &icon = QIcon()); + + bool eventFilter(QObject *object, QEvent *event) override; +}; + +#endif diff --git a/ui/pageview.cpp b/ui/pageview.cpp index b116b36a0..95f068c18 100644 --- a/ui/pageview.cpp +++ b/ui/pageview.cpp @@ -70,6 +70,7 @@ #include "pageviewannotator.h" #include "priorities.h" #include "toolaction.h" +#include "okmenutitle.h" #ifdef HAVE_SPEECH #include "tts.h" #endif @@ -2774,7 +2775,7 @@ void PageView::mouseReleaseEvent( QMouseEvent * e ) QAction *imageToFile = 0; if ( d->document->supportsSearching() && !selectedText.isEmpty() ) { - menu.setTitle( i18np( "Text (1 character)", "Text (%1 characters)", selectedText.length() ) ); + menu.addAction( new OKMenuTitle( &menu, i18np( "Text (1 character)", "Text (%1 characters)", selectedText.length() ) ) ); textToClipboard = menu.addAction( QIcon::fromTheme(QStringLiteral("edit-copy")), i18n( "Copy to Clipboard" ) ); bool copyAllowed = d->document->isAllowed( Okular::AllowCopy ); if ( !copyAllowed ) @@ -2791,7 +2792,7 @@ void PageView::mouseReleaseEvent( QMouseEvent * e ) addWebShortcutsMenu( &menu, selectedText ); } } - menu.setTitle( i18n( "Image (%1 by %2 pixels)", selectionRect.width(), selectionRect.height() ) ); + menu.addAction( new OKMenuTitle( &menu, i18n( "Image (%1 by %2 pixels)", selectionRect.width(), selectionRect.height() ) ) ); imageToClipboard = menu.addAction( QIcon::fromTheme(QStringLiteral("image-x-generic")), i18n( "Copy to Clipboard" ) ); imageToFile = menu.addAction( QIcon::fromTheme(QStringLiteral("document-save")), i18n( "Save to File..." ) ); QAction *choice = menu.exec( e->globalPos() );