From 515c6d3a88982740b366d89eb519265bd61655b1 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Mon, 3 Nov 2014 17:09:41 +0100 Subject: [PATCH] FindToolBar: Refactor opening the tool bar Introduce flags for what to do instead of doing much of the logic itself in that method. Change-Id: I3ee38e39549e1267c55c94c3a93211492eb2e2f3 Reviewed-by: Daniel Teske --- src/plugins/coreplugin/find/findtoolbar.cpp | 38 ++++++++++++++------- src/plugins/coreplugin/find/findtoolbar.h | 11 +++++- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/plugins/coreplugin/find/findtoolbar.cpp b/src/plugins/coreplugin/find/findtoolbar.cpp index 3f4989e4257..16f33e59b4d 100644 --- a/src/plugins/coreplugin/find/findtoolbar.cpp +++ b/src/plugins/coreplugin/find/findtoolbar.cpp @@ -161,6 +161,15 @@ FindToolBar::FindToolBar(FindPlugin *plugin, CurrentDocumentFind *currentDocumen mfind->addAction(cmd, Constants::G_FIND_CURRENTDOCUMENT); connect(m_findInDocumentAction, SIGNAL(triggered()), this, SLOT(openFind())); + // Pressing the find shortcut while focus is in the tool bar should not change the search text, + // so register a different find action for the tool bar + auto localFindAction = new QAction(this); + cmd = ActionManager::registerAction(localFindAction, Constants::FIND_IN_DOCUMENT, + Context(Constants::C_FINDTOOLBAR)); + connect(localFindAction, &QAction::triggered, this, [this]() { + openFindToolBar(OpenFlags(UpdateAll & ~UpdateFindText)); + }); + if (QApplication::clipboard()->supportsFindBuffer()) { m_enterFindStringAction = new QAction(tr("Enter Find String"), this); cmd = Core::ActionManager::registerAction(m_enterFindStringAction, "Find.EnterFindString", globalcontext); @@ -531,7 +540,6 @@ void FindToolBar::putSelectionToFindClipboard() openFind(false); const QString text = m_currentDocumentFind->currentFindString(); QApplication::clipboard()->setText(text, QClipboard::FindBuffer); - setFindText(text); } @@ -669,10 +677,13 @@ bool FindToolBar::canShowAllControls(bool replaceIsVisible) const void FindToolBar::openFind(bool focus) { setBackward(false); - openFindToolBar(focus); + OpenFlags flags = UpdateAll; + if (!focus) // remove focus flag + flags = flags & ~UpdateFocusAndSelect; + openFindToolBar(flags); } -void FindToolBar::openFindToolBar(bool focus) +void FindToolBar::openFindToolBar(OpenFlags flags) { installEventFilters(); Core::FindToolBarPlaceHolder *holder = findToolBarPlaceHolder(); @@ -688,30 +699,33 @@ void FindToolBar::openFindToolBar(bool focus) m_currentDocumentFind->acceptCandidate(); holder->setVisible(true); setVisible(true); - // We do not want to change the text when we currently have the focus and user presses the - // find shortcut - if (!focus || !toolBarHasFocus()) { +// We do not want to change the text when we currently have the focus and user presses the +// find shortcut +// if (!focus || !toolBarHasFocus()) { + if (flags & UpdateFindText) { QString text = m_currentDocumentFind->currentFindString(); if (!text.isEmpty()) setFindText(text); } - if (focus) + if (flags & UpdateFocusAndSelect) setFocus(); - m_currentDocumentFind->defineFindScope(); - m_currentDocumentFind->highlightAll(getFindText(), effectiveFindFlags()); - if (focus) + if (flags & UpdateFindScope) + m_currentDocumentFind->defineFindScope(); + if (flags & UpdateHighlight) + m_currentDocumentFind->highlightAll(getFindText(), effectiveFindFlags()); + if (flags & UpdateFocusAndSelect) selectFindText(); } void FindToolBar::findNextSelected() { - openFind(false); + openFindToolBar(OpenFlags(UpdateAll & ~UpdateFocusAndSelect)); invokeFindNext(); } void FindToolBar::findPreviousSelected() { - openFind(false); + openFindToolBar(OpenFlags(UpdateAll & ~UpdateFocusAndSelect)); invokeFindPrevious(); } diff --git a/src/plugins/coreplugin/find/findtoolbar.h b/src/plugins/coreplugin/find/findtoolbar.h index b0694e0ef14..d77a421b1bf 100644 --- a/src/plugins/coreplugin/find/findtoolbar.h +++ b/src/plugins/coreplugin/find/findtoolbar.h @@ -75,13 +75,22 @@ class FindToolBar : public Utils::StyledBar Q_OBJECT public: + enum OpenFlag { + UpdateFocusAndSelect = 0x01, + UpdateFindScope = 0x02, + UpdateFindText = 0x04, + UpdateHighlight = 0x08, + UpdateAll = 0x0F + }; + Q_DECLARE_FLAGS(OpenFlags, OpenFlag) + explicit FindToolBar(FindPlugin *plugin, CurrentDocumentFind *currentDocumentFind); ~FindToolBar(); void readSettings(); void writeSettings(); - void openFindToolBar(bool focus = true); + void openFindToolBar(OpenFlags flags = UpdateAll); void setUseFakeVim(bool on); void setLightColoredIcon(bool lightColored);