From fe02d0962810b1ebca33c219274a5e3390de5286 Mon Sep 17 00:00:00 2001 From: Benjamin Poulain Date: Sun, 22 Aug 2010 01:17:43 +0200 Subject: Make the FindBar a bit more robust The FindBar was slots show() and hide() instead of the virtual method QWidget::setVisible(). This can lead to subtle bugs, because the behavior is different depending on the type of call (signal-slot connection VS direct call on a pointer to QWidget) and the function called (hide() is != than setVisible(false)). --- src/findbar.cpp | 38 +++++++++++++++----------------------- src/findbar.h | 4 ++-- src/mainwindow.cpp | 1 + 3 files changed, 18 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/findbar.cpp b/src/findbar.cpp index b3b80fac..18a4f168 100644 --- a/src/findbar.cpp +++ b/src/findbar.cpp @@ -138,25 +138,27 @@ bool FindBar::highlightAllState() const return m_highlightAll->isChecked(); } - -void FindBar::show() +void FindBar::setVisible(bool visible) { - // show findbar if not visible - if (isHidden()) - { - emit visibilityChanged(true); - QWidget::show(); + QWidget::setVisible(visible); + + if (visible != isVisible()) + emit visibilityChanged(visible); + + if (visible) { + // show findbar if not visible emit searchString(m_lineEdit->text()); - } - m_hideTimer->start(60000); + m_hideTimer->start(60000); - // set focus to findbar if user select showFindBar shortcut - m_lineEdit->setFocus(); - m_lineEdit->selectAll(); + // set focus to findbar if user select showFindBar shortcut + m_lineEdit->setFocus(); + m_lineEdit->selectAll(); + } else { + m_hideTimer->stop(); + } } - void FindBar::notifyMatch(bool match) { QPalette p = m_lineEdit->palette(); @@ -180,16 +182,6 @@ void FindBar::notifyMatch(bool match) m_hideTimer->start(60000); } - -void FindBar::hide() -{ - m_hideTimer->stop(); - emit visibilityChanged(false); - QWidget::hide(); - emit(searchString(m_lineEdit->text())); -} - - void FindBar::toggleVisibility() { isVisible() ? hide() : show(); diff --git a/src/findbar.h b/src/findbar.h index 38bbcbc5..974d6137 100644 --- a/src/findbar.h +++ b/src/findbar.h @@ -55,9 +55,9 @@ public: void notifyMatch(bool match); bool highlightAllState() const; + void setVisible(bool visible); + public slots: - void show(); - void hide(); void toggleVisibility(); signals: diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 4f9d6dab..28a3bebf 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -370,6 +370,7 @@ void MainWindow::setupActions() findShortcut.setAlternate(Qt::Key_Slash); a->setShortcut(findShortcut); a->setCheckable(true); + a->setChecked(m_findBar->isVisible()); connect(m_findBar, SIGNAL(visibilityChanged(bool)), a, SLOT(setChecked(bool))); KStandardAction::findNext(this, SLOT(findNext()) , actionCollection()); -- cgit v1.2.1 From 8e19bc87f8a83fde67e8492c0fbe7dd48752ca94 Mon Sep 17 00:00:00 2001 From: Benjamin Poulain Date: Sun, 22 Aug 2010 01:57:59 +0200 Subject: Change the Find action to show() instead of toggle() The find actions generally show a find bar, and is not a toggle action. A general pattern of use is to press "/" or Ctrl+F to show the find bar with the currently selected text, and press escape to hide the bar. --- src/findbar.cpp | 5 ----- src/findbar.h | 3 --- src/mainwindow.cpp | 3 +-- 3 files changed, 1 insertion(+), 10 deletions(-) (limited to 'src') diff --git a/src/findbar.cpp b/src/findbar.cpp index 18a4f168..bad3bf50 100644 --- a/src/findbar.cpp +++ b/src/findbar.cpp @@ -181,8 +181,3 @@ void FindBar::notifyMatch(bool match) m_lineEdit->setPalette(p); m_hideTimer->start(60000); } - -void FindBar::toggleVisibility() -{ - isVisible() ? hide() : show(); -} diff --git a/src/findbar.h b/src/findbar.h index 974d6137..d82e9722 100644 --- a/src/findbar.h +++ b/src/findbar.h @@ -57,9 +57,6 @@ public: void setVisible(bool visible); -public slots: - void toggleVisibility(); - signals: void searchString(const QString &); void visibilityChanged(bool); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 28a3bebf..5eb43aa0 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -365,11 +365,10 @@ void MainWindow::setupActions() KStandardAction::print(this, SLOT(printRequested()), actionCollection()); KStandardAction::quit(this , SLOT(close()), actionCollection()); - a = KStandardAction::find(m_findBar, SLOT(toggleVisibility()), actionCollection()); + a = KStandardAction::find(m_findBar, SLOT(show()), actionCollection()); KShortcut findShortcut = KStandardShortcut::find(); findShortcut.setAlternate(Qt::Key_Slash); a->setShortcut(findShortcut); - a->setCheckable(true); a->setChecked(m_findBar->isVisible()); connect(m_findBar, SIGNAL(visibilityChanged(bool)), a, SLOT(setChecked(bool))); -- cgit v1.2.1 From 67138dcf8e3a1788928fd31256b29a257dae5eee Mon Sep 17 00:00:00 2001 From: Benjamin Poulain Date: Sun, 22 Aug 2010 04:06:54 +0200 Subject: Use the selected text as the default search string of the FindBar The common behavior of the Find action is to start a search with the current selected text if any. This patch implement this behavior for Rekonq. --- src/findbar.cpp | 14 +++++++++----- src/findbar.h | 5 +++-- src/mainwindow.cpp | 9 +++++++++ src/mainwindow.h | 2 ++ 4 files changed, 23 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/findbar.cpp b/src/findbar.cpp index bad3bf50..0e3f7385 100644 --- a/src/findbar.cpp +++ b/src/findbar.cpp @@ -49,16 +49,14 @@ #include -FindBar::FindBar(QWidget *parent) - : QWidget(parent) +FindBar::FindBar(MainWindow *window) + : QWidget(window) + , m_mainWindow(window) , m_lineEdit(new KLineEdit(this)) , m_hideTimer(new QTimer(this)) , m_matchCase(new QCheckBox(i18n("&Match case"), this)) , m_highlightAll(new QCheckBox(i18n("&Highlight all"), this)) { - // mainwindow pointer - MainWindow *window = qobject_cast(parent); - QHBoxLayout *layout = new QHBoxLayout; // cosmetic @@ -146,6 +144,12 @@ void FindBar::setVisible(bool visible) emit visibilityChanged(visible); if (visible) { + if (!hasFocus()) { + const QString selectedText = m_mainWindow->selectedText(); + if (!selectedText.isEmpty()) + m_lineEdit->setText(selectedText); + } + // show findbar if not visible emit searchString(m_lineEdit->text()); diff --git a/src/findbar.h b/src/findbar.h index d82e9722..6451aac8 100644 --- a/src/findbar.h +++ b/src/findbar.h @@ -41,14 +41,14 @@ // Forward Declarations class QString; - +class MainWindow; class REKONQ_TESTS_EXPORT FindBar : public QWidget { Q_OBJECT public: - FindBar(QWidget *parent); + FindBar(MainWindow *parent); ~FindBar(); bool matchCase() const; @@ -62,6 +62,7 @@ signals: void visibilityChanged(bool); private: + MainWindow *m_mainWindow; KLineEdit *m_lineEdit; QTimer *m_hideTimer; QCheckBox *m_matchCase; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 5eb43aa0..44cc6d59 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -907,6 +907,15 @@ void MainWindow::setWidgetsVisible(bool makeVisible) } +QString MainWindow::selectedText() const +{ + if (!currentTab()) + return QString(); + + return currentTab()->view()->selectedText(); +} + + void MainWindow::viewPageSource() { if (!currentTab()) diff --git a/src/mainwindow.h b/src/mainwindow.h index 3aa57f5c..cd55faa5 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -80,6 +80,8 @@ public: virtual QSize sizeHint() const; void setWidgetsVisible(bool makeFullScreen); + QString selectedText() const; + private: void setupBookmarksAndToolsShortcuts(); void setupActions(); -- cgit v1.2.1