From 4a4a2a1b524b2b62b3bc41b1619c049ab1f05437 Mon Sep 17 00:00:00 2001 From: Andy Sardina Date: Sun, 25 Aug 2019 13:19:43 +0300 Subject: [PATCH] Add "find in this document" to selection context menu BUG: 408355 FIXED-IN: 1.9.0 --- part.cpp | 6 ++++++ ui/pageview.cpp | 21 +++++++++++++++++++-- ui/pageview.h | 2 ++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/part.cpp b/part.cpp index 39d798c73..20cbc64f9 100644 --- a/part.cpp +++ b/part.cpp @@ -524,6 +524,12 @@ m_cliPresentation(false), m_cliPrint(false), m_cliPrintAndExit(false), m_embedMo QMetaObject::invokeMethod( m_pageView, "setFocus", Qt::QueuedConnection ); //usability setting // m_splitter->setFocusProxy(m_pageView); connect( m_pageView.data(), &PageView::rightClick, this, &Part::slotShowMenu ); + connect( m_pageView, &PageView::triggerSearch, this, + [this] (const QString& searchText){ + m_findBar->startSearch(searchText); + slotShowFindBar(); + } + ); connect( m_document, &Document::error, this, &Part::errorMessage ); connect( m_document, &Document::warning, this, &Part::warningMessage ); connect( m_document, &Document::notice, this, &Part::noticeMessage ); diff --git a/ui/pageview.cpp b/ui/pageview.cpp index 393c2fca3..3447cf0c5 100644 --- a/ui/pageview.cpp +++ b/ui/pageview.cpp @@ -103,6 +103,12 @@ static const int pageflags = PagePainter::Accessibility | PagePainter::EnhanceLi static const float kZoomValues[] = { 0.12, 0.25, 0.33, 0.50, 0.66, 0.75, 1.00, 1.25, 1.50, 2.00, 4.00, 8.00, 16.00 }; +// This is the length of the text that will be shown when the user is searching for a specific piece of text. +static const int searchTextPreviewLength = 21; + +// When following a link, only a preview of this length will be used to set the text of the action. +static const int linkTextPreviewLength = 30; + static inline double normClamp( double value, double def ) { return ( value < 0.0 || value > 1.0 ) ? def : value; @@ -2833,6 +2839,7 @@ void PageView::mouseReleaseEvent( QMouseEvent * e ) #endif if ( copyAllowed ) { + addSearchWithinDocumentAction( &menu, selectedText ); addWebShortcutsMenu( &menu, selectedText ); } } @@ -3116,6 +3123,7 @@ void PageView::mouseReleaseEvent( QMouseEvent * e ) } else { + addSearchWithinDocumentAction(menu, d->selectedText()); addWebShortcutsMenu( menu, d->selectedText() ); } @@ -3125,7 +3133,7 @@ void PageView::mouseReleaseEvent( QMouseEvent * e ) url = UrlUtils::getUrl( d->selectedText() ); if ( !url.isEmpty() ) { - const QString squeezedText = KStringHandler::rsqueeze( url, 30 ); + const QString squeezedText = KStringHandler::rsqueeze( url, linkTextPreviewLength ); httpLink = menu->addAction( i18n( "Go to '%1'", squeezedText ) ); httpLink->setObjectName(QStringLiteral("GoToAction")); } @@ -4431,7 +4439,7 @@ void PageView::addWebShortcutsMenu( QMenu * menu, const QString & text ) QMenu *webShortcutsMenu = new QMenu( menu ); webShortcutsMenu->setIcon( QIcon::fromTheme( QStringLiteral("preferences-web-browser-shortcuts") ) ); - const QString squeezedText = KStringHandler::rsqueeze( searchText, 21 ); + const QString squeezedText = KStringHandler::rsqueeze( searchText, searchTextPreviewLength ); webShortcutsMenu->setTitle( i18n( "Search for '%1' with", squeezedText ) ); QAction *action = nullptr; @@ -4507,6 +4515,15 @@ QMenu* PageView::createProcessLinkMenu(PageViewItem *item, const QPoint &eventPo return nullptr; } +void PageView::addSearchWithinDocumentAction(QMenu *menu, const QString &searchText) +{ + const QString squeezedText = KStringHandler::rsqueeze( searchText, searchTextPreviewLength ); + QAction *action = new QAction(i18n("Search for '%1' in open document", squeezedText), menu); + action->setIcon( QIcon::fromTheme( QStringLiteral("document-preview") ) ); + connect(action, &QAction::triggered, [this, searchText]{Q_EMIT triggerSearch(searchText);}); + menu->addAction( action ); +} + //BEGIN private SLOTS void PageView::slotRelayoutPages() // called by: notifySetup, viewportResizeEvent, slotViewMode, slotContinuousToggled, updateZoom diff --git a/ui/pageview.h b/ui/pageview.h index 1e06e1a08..f26800ee1 100644 --- a/ui/pageview.h +++ b/ui/pageview.h @@ -135,6 +135,7 @@ Q_OBJECT void mouseForwardButtonClick(); void escPressed(); void fitWindowToPage( const QSize& pageViewPortSize, const QSize& pageSize ); + void triggerSearch( const QString& text ); protected: bool event( QEvent * event ) override; @@ -196,6 +197,7 @@ Q_OBJECT void resizeContentArea( const QSize & newSize ); void updatePageStep(); + void addSearchWithinDocumentAction(QMenu * menu, const QString & searchText ); void addWebShortcutsMenu( QMenu * menu, const QString & text ); QMenu* createProcessLinkMenu( PageViewItem *item, const QPoint & eventPos ); // used when selecting stuff, makes the view scroll as necessary to keep the mouse inside the view