]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt/GuiSearch.cpp
#12423 fix a focus change problem
[lyx.git] / src / frontends / qt / GuiSearch.cpp
index 519c1c2b48bf68a186efc8bf5c99b40c8f8ede24..f12d42c11d42f155ee08a3d55f9ee4b24b27893e 100644 (file)
 #include "GuiKeySymbol.h"
 #include "GuiView.h"
 
+#include "qt_helpers.h"
+#include "support/filetools.h"
 #include "support/debug.h"
 #include "support/gettext.h"
+#include "support/FileName.h"
 #include "frontends/alert.h"
 #include "frontends/Clipboard.h"
 
 #include <QClipboard>
+#include <QPainter>
 #include <QLineEdit>
 #include <QSettings>
+#include <QHideEvent>
 #include <QShowEvent>
 #include "QSizePolicy"
+#if QT_VERSION >= 0x050000
+#include <QSvgRenderer>
+#endif
 
 using namespace std;
+using namespace lyx::support;
 
 using lyx::KeySymbol;
 
@@ -55,8 +64,8 @@ static void uniqueInsert(QComboBox * box, QString const & text)
 }
 
 
-GuiSearchWidget::GuiSearchWidget(QWidget * parent)
-       :       QWidget(parent)
+GuiSearchWidget::GuiSearchWidget(QWidget * parent, GuiView & view)
+       :       QWidget(parent), view_(view)
 {
        setupUi(this);
 
@@ -82,6 +91,36 @@ GuiSearchWidget::GuiSearchWidget(QWidget * parent)
 
        setFocusProxy(findCO);
 
+       // Use a FancyLineEdit due to the indicator icons
+       findLE_ = new FancyLineEdit(this);
+       findCO->setLineEdit(findLE_);
+
+       // And a menu in minimal mode
+       menu_ = new QMenu();
+       act_casesense_ = new QAction(qt_("&Case sensitive[[search]]"), this);
+       act_casesense_->setCheckable(true);
+       act_wholewords_ = new QAction(qt_("Wh&ole words"), this);
+       act_wholewords_->setCheckable(true);
+       act_selection_ = new QAction(qt_("Selection onl&y"), this);
+       act_selection_->setCheckable(true);
+       act_immediate_ = new QAction(qt_("Search as yo&u type"), this);
+       act_immediate_->setCheckable(true);
+       act_wrap_ = new QAction(qt_("&Wrap"), this);
+       act_wrap_->setCheckable(true);
+
+       menu_->addAction(act_casesense_);
+       menu_->addAction(act_wholewords_);
+       menu_->addAction(act_selection_);
+       menu_->addAction(act_immediate_);
+       menu_->addAction(act_wrap_);
+       findLE_->setButtonMenu(FancyLineEdit::Right, menu_);
+
+       connect(act_casesense_, SIGNAL(triggered()), this, SLOT(caseSenseActTriggered()));
+       connect(act_wholewords_, SIGNAL(triggered()), this, SLOT(wholeWordsActTriggered()));
+       connect(act_selection_, SIGNAL(triggered()), this, SLOT(searchSelActTriggered()));
+       connect(act_immediate_, SIGNAL(triggered()), this, SLOT(immediateActTriggered()));
+       connect(act_wrap_, SIGNAL(triggered()), this, SLOT(wrapActTriggered()));
+
        findCO->setCompleter(nullptr);
        replaceCO->setCompleter(nullptr);
 
@@ -91,18 +130,28 @@ GuiSearchWidget::GuiSearchWidget(QWidget * parent)
 }
 
 
+bool GuiSearchWidget::initialiseParams(std::string const & str)
+{
+       if (!str.empty())
+               findCO->lineEdit()->setText(toqstr(str));
+       return true;
+}
+
+
 void GuiSearchWidget::keyPressEvent(QKeyEvent * ev)
 {
        KeySymbol sym;
        setKeySymbol(&sym, ev);
 
        // catch Return and Shift-Return
-       if (ev->key() == Qt::Key_Return) {
-               findClicked(ev->modifiers() == Qt::ShiftModifier);
+       if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Enter) {
+               doFind(ev->modifiers() == Qt::ShiftModifier);
                return;
        }
        if (ev->key() == Qt::Key_Escape) {
-               dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplace"));
+               dispatch(FuncRequest(LFUN_DIALOG_HIDE, "findreplace"));
+               view_.setFocus();
+               bv_->buffer().updateBuffer();
                return;
        }
 
@@ -111,12 +160,13 @@ void GuiSearchWidget::keyPressEvent(QKeyEvent * ev)
                KeyModifier mod = lyx::q_key_state(ev->modifiers());
                KeySequence keyseq(&theTopLevelKeymap(), &theTopLevelKeymap());
                FuncRequest fr = keyseq.addkey(sym, mod);
-               if (fr == FuncRequest(LFUN_WORD_FIND_FORWARD) || fr == FuncRequest(LFUN_WORD_FIND)) {
-                       findClicked();
+               if (fr == FuncRequest(LFUN_WORD_FIND_FORWARD)
+                   || fr == FuncRequest(LFUN_WORD_FIND)) {
+                       doFind();
                        return;
                }
                if (fr == FuncRequest(LFUN_WORD_FIND_BACKWARD)) {
-                       findClicked(true);
+                       doFind(true);
                        return;
                }
                if (fr == FuncRequest(LFUN_DIALOG_TOGGLE, "findreplace")) {
@@ -139,9 +189,18 @@ void GuiSearchWidget::minimizeClicked(bool const toggle)
        replacePrevPB->setHidden(minimized_);
        replaceallPB->setHidden(minimized_);
        CBFrame->setHidden(minimized_);
+
        if (minimized_) {
                minimizePB->setText(qt_("Ex&pand"));
                minimizePB->setToolTip(qt_("Show replace and option widgets"));
+               // update menu items
+               blockSignals(true);
+               act_casesense_->setChecked(caseCB->isChecked());
+               act_immediate_->setChecked(instantSearchCB->isChecked());
+               act_selection_->setChecked(selectionCB->isChecked());
+               act_wholewords_->setChecked(wordsCB->isChecked());
+               act_wrap_->setChecked(wrapCB->isChecked());
+               blockSignals(false);
        } else {
                minimizePB->setText(qt_("&Minimize"));
                minimizePB->setToolTip(qt_("Hide replace and option widgets"));
@@ -149,6 +208,203 @@ void GuiSearchWidget::minimizeClicked(bool const toggle)
 
        Q_EMIT needSizeUpdate();
        Q_EMIT needTitleBarUpdate();
+       handleIndicators();
+}
+
+
+void GuiSearchWidget::handleIndicators()
+{
+       findLE_->setButtonVisible(FancyLineEdit::Right, minimized_);
+
+       QString tip;
+
+       if (minimized_) {
+               int pms = 0;
+               if (caseCB->isChecked())
+                       ++pms;
+               if (wordsCB->isChecked())
+                       ++pms;
+               if (selectionCB->isChecked())
+                       ++pms;
+               if (instantSearchCB->isChecked())
+                       ++pms;
+               if (wrapCB->isChecked())
+                       ++pms;
+
+               bool const dark_mode = guiApp && guiApp->isInDarkMode();
+               qreal dpr = 1.0;
+#if QT_VERSION >= 0x050000
+               // Consider device/pixel ratio (HiDPI)
+               if (guiApp && guiApp->currentView())
+                       dpr = guiApp->currentView()->devicePixelRatio();
+#endif
+               QString imagedir = "images/";
+               QPixmap bpixmap = getPixmap("images/", "search-options", "svgz,png");
+               QPixmap pm = bpixmap;
+
+               if (pms > 0) {
+                       int const gap = 3;
+                       QPixmap scaled_pm = QPixmap(bpixmap.size() * dpr);
+                       pm = QPixmap(pms * scaled_pm.width() + ((pms - 1) * gap),
+                                    scaled_pm.height());
+                       pm.fill(Qt::transparent);
+                       QPainter painter(&pm);
+                       int x = 0;
+                       
+                       tip = qt_("Active options:");
+                       tip += "<ul>";
+                       if (caseCB->isChecked()) {
+                               tip += "<li>" + qt_("Case sensitive search");
+                               QPixmap spixmap = getPixmap("images/", "search-case-sensitive", "svgz,png");
+#if QT_VERSION < 0x050000
+                               painter.drawPixmap(x, 0, spixmap);
+#else
+                               // With Qt5, we render SVG directly for HiDPI scalability
+                               FileName fname = imageLibFileSearch(imagedir, "search-case-sensitive", "svgz,png");
+                               QString fpath = toqstr(fname.absFileName());
+                               if (!fpath.isEmpty()) {
+                                       QSvgRenderer svgRenderer(fpath);
+                                       if (svgRenderer.isValid())
+                                               svgRenderer.render(&painter, QRectF(0, 0, spixmap.width() * dpr,
+                                                                                   spixmap.height() * dpr));
+                               }
+#endif
+                               x += (spixmap.width() * dpr) + gap;
+                       }
+                       if (wordsCB->isChecked()) {
+                               tip += "<li>" + qt_("Whole words only");
+                               QPixmap spixmap = getPixmap("images/", "search-whole-words", "svgz,png");
+#if QT_VERSION < 0x050000
+                               painter.drawPixmap(x, 0, spixmap);
+#else
+                               FileName fname = imageLibFileSearch(imagedir, "search-whole-words", "svgz,png");
+                               QString fpath = toqstr(fname.absFileName());
+                               if (!fpath.isEmpty()) {
+                                       QSvgRenderer svgRenderer(fpath);
+                                       if (svgRenderer.isValid())
+                                               svgRenderer.render(&painter, QRectF(x, 0, spixmap.width() * dpr,
+                                                                                   spixmap.height() * dpr));
+                               }
+#endif
+                               x += (spixmap.width() * dpr) + gap;
+                       }
+                       if (selectionCB->isChecked()) {
+                               tip += "<li>" + qt_("Search only in selection");
+                               QPixmap spixmap = getPixmap("images/", "search-selection", "svgz,png");
+#if QT_VERSION < 0x050000
+                               painter.drawPixmap(x, 0, spixmap);
+#else
+                               FileName fname = imageLibFileSearch(imagedir, "search-selection", "svgz,png");
+                               QString fpath = toqstr(fname.absFileName());
+                               if (!fpath.isEmpty()) {
+                                       QSvgRenderer svgRenderer(fpath);
+                                       if (svgRenderer.isValid())
+                                               svgRenderer.render(&painter, QRectF(x, 0, spixmap.width() * dpr,
+                                                                                   spixmap.height() * dpr));
+                               }
+#endif
+                               x += (spixmap.width() * dpr) + gap;
+                       }
+                       if (instantSearchCB->isChecked()) {
+                               tip += "<li>" + qt_("Search as you type");
+                               QPixmap spixmap = getPixmap("images/", "search-instant", "svgz,png");
+#if QT_VERSION < 0x050000
+                               painter.drawPixmap(x, 0, spixmap);
+#else
+                               FileName fname = imageLibFileSearch(imagedir, "search-instant", "svgz,png");
+                               QString fpath = toqstr(fname.absFileName());
+                               if (!fpath.isEmpty()) {
+                                       QSvgRenderer svgRenderer(fpath);
+                                       if (svgRenderer.isValid())
+                                               svgRenderer.render(&painter, QRectF(x, 0, spixmap.width() * dpr,
+                                                                                   spixmap.height() * dpr));
+                               }
+#endif
+                               x += (spixmap.width() * dpr) + gap;
+                       }
+                       if (wrapCB->isChecked()) {
+                               tip += "<li>" + qt_("Wrap search");
+                               QPixmap spixmap = getPixmap("images/", "search-wrap", "svgz,png");
+#if QT_VERSION < 0x050000
+                               painter.drawPixmap(x, 0, spixmap);
+#else
+                               FileName fname = imageLibFileSearch(imagedir, "search-wrap", "svgz,png");
+                               QString fpath = toqstr(fname.absFileName());
+                               if (!fpath.isEmpty()) {
+                                       QSvgRenderer svgRenderer(fpath);
+                                       if (svgRenderer.isValid())
+                                               svgRenderer.render(&painter, QRectF(x, 0, spixmap.width() * dpr,
+                                                                                   spixmap.height() * dpr));
+                               }
+#endif
+                               x += (spixmap.width() * dpr) + gap;
+                       }
+                       tip += "</ul>";
+#if QT_VERSION >= 0x050000
+                       pm.setDevicePixelRatio(dpr);
+#endif
+                       painter.end();
+               } else {
+                       tip = qt_("Click here to change search options");
+#if QT_VERSION >= 0x050000
+                       // With Qt5, we render SVG directly for HiDPI scalability
+                       FileName fname = imageLibFileSearch(imagedir, "search-options", "svgz,png");
+                       QString fpath = toqstr(fname.absFileName());
+                       if (!fpath.isEmpty()) {
+                               QSvgRenderer svgRenderer(fpath);
+                               if (svgRenderer.isValid()) {
+                                       pm = QPixmap(bpixmap.size() * dpr);
+                                       pm.fill(Qt::transparent);
+                                       QPainter painter(&pm);
+                                       svgRenderer.render(&painter);
+                                       pm.setDevicePixelRatio(dpr);
+                               }
+                       }
+#endif
+               }
+               if (dark_mode) {
+                       QImage img = pm.toImage();
+                       img.invertPixels();
+                       pm.convertFromImage(img);
+               }
+               findLE_->setButtonPixmap(FancyLineEdit::Right, pm);
+       }
+       findLE_->setButtonToolTip(FancyLineEdit::Right, tip);
+}
+
+
+void GuiSearchWidget::caseSenseActTriggered()
+{
+       caseCB->setChecked(act_casesense_->isChecked());
+       handleIndicators();
+}
+
+
+void GuiSearchWidget::wholeWordsActTriggered()
+{
+       wordsCB->setChecked(act_wholewords_->isChecked());
+       handleIndicators();
+}
+
+
+void GuiSearchWidget::searchSelActTriggered()
+{
+       selectionCB->setChecked(act_selection_->isChecked());
+       handleIndicators();
+}
+
+
+void GuiSearchWidget::immediateActTriggered()
+{
+       instantSearchCB->setChecked(act_immediate_->isChecked());
+       handleIndicators();
+}
+
+
+void GuiSearchWidget::wrapActTriggered()
+{
+       wrapCB->setChecked(act_wrap_->isChecked());
+       handleIndicators();
 }
 
 
@@ -161,16 +417,26 @@ void GuiSearchWidget::showEvent(QShowEvent * e)
 }
 
 
+void GuiSearchWidget::hideEvent(QHideEvent *)
+{
+       dispatch(FuncRequest(LFUN_DIALOG_HIDE, "findreplace"));
+       view_.setFocus();
+       // update toolbar status
+       if (bv_)
+               bv_->buffer().updateBuffer();
+}
+
+
 void GuiSearchWidget::findBufferChanged()
 {
        docstring search = theClipboard().getFindBuffer();
-       if (!search.empty()) {
+       // update from find buffer, but only if the strings differ (else we
+       // might end up in loops with search as you type)
+       if (!search.empty() && toqstr(search) != findCO->lineEdit()->text()) {
                LYXERR(Debug::CLIPBOARD, "from findbuffer: " << search);
-#if QT_VERSION > 0x050000
-               findCO->setCurrentText(toqstr(search));
-#else
-               findCO->setEditText(toqstr(search));
-#endif
+               findCO->lineEdit()->selectAll();
+               findCO->lineEdit()->insert(toqstr(search));
+               findCO->lineEdit()->selectAll();
        }
 }
 
@@ -184,42 +450,32 @@ void GuiSearchWidget::findChanged()
        replacePB->setEnabled(replace);
        replacePrevPB->setEnabled(replace);
        replaceallPB->setEnabled(replace);
-       if (instantSearchCB->isChecked() && !emptytext)
-               findClicked();
+       if (instantSearchCB->isChecked())
+               doFind(false, true);
 }
 
 
-void GuiSearchWidget::findClicked(bool const backwards)
+void GuiSearchWidget::findClicked()
 {
-       docstring const needle = qstring_to_ucs4(findCO->currentText());
-       find(needle, caseCB->isChecked(), wordsCB->isChecked(), !backwards,
-            instantSearchCB->isChecked(), wrapCB->isChecked());
-       uniqueInsert(findCO, findCO->currentText());
-       if (!instantSearchCB->isChecked())
-               findCO->lineEdit()->selectAll();
+       doFind();
 }
 
 
 void GuiSearchWidget::findPrevClicked()
 {
-       findClicked(true);
+       doFind(true);
 }
 
 
-void GuiSearchWidget::replaceClicked(bool const backwards)
+void GuiSearchWidget::replaceClicked()
 {
-       docstring const needle = qstring_to_ucs4(findCO->currentText());
-       docstring const repl = qstring_to_ucs4(replaceCO->currentText());
-       replace(needle, repl, caseCB->isChecked(), wordsCB->isChecked(),
-               !backwards, false, wrapCB->isChecked());
-       uniqueInsert(findCO, findCO->currentText());
-       uniqueInsert(replaceCO, replaceCO->currentText());
+       doReplace();
 }
 
 
 void GuiSearchWidget::replacePrevClicked()
 {
-       replaceClicked(true);
+       doReplace(true);
 }
 
 
@@ -227,31 +483,55 @@ void GuiSearchWidget::replaceallClicked()
 {
        replace(qstring_to_ucs4(findCO->currentText()),
                qstring_to_ucs4(replaceCO->currentText()),
-               caseCB->isChecked(), wordsCB->isChecked(), true, true, true);
+               caseCB->isChecked(), wordsCB->isChecked(),
+               true, true, true, selectionCB->isChecked());
        uniqueInsert(findCO, findCO->currentText());
        uniqueInsert(replaceCO, replaceCO->currentText());
 }
 
 
+void GuiSearchWidget::doFind(bool const backwards, bool const instant)
+{
+       docstring const needle = qstring_to_ucs4(findCO->currentText());
+       find(needle, caseCB->isChecked(), wordsCB->isChecked(), !backwards,
+            instant, wrapCB->isChecked(), selectionCB->isChecked());
+       uniqueInsert(findCO, findCO->currentText());
+       if (!instant)
+               findCO->lineEdit()->selectAll();
+}
+
+
 void GuiSearchWidget::find(docstring const & search, bool casesensitive,
-                        bool matchword, bool forward, bool instant,
-                        bool wrap)
+                          bool matchword, bool forward, bool instant,
+                          bool wrap, bool onlysel)
 {
        docstring const sdata =
                find2string(search, casesensitive, matchword,
-                           forward, wrap, instant);
+                           forward, wrap, instant, onlysel);
 
        dispatch(FuncRequest(LFUN_WORD_FIND, sdata));
 }
 
 
+void GuiSearchWidget::doReplace(bool const backwards)
+{
+       docstring const needle = qstring_to_ucs4(findCO->currentText());
+       docstring const repl = qstring_to_ucs4(replaceCO->currentText());
+       replace(needle, repl, caseCB->isChecked(), wordsCB->isChecked(),
+               !backwards, false, wrapCB->isChecked(), selectionCB->isChecked());
+       uniqueInsert(findCO, findCO->currentText());
+       uniqueInsert(replaceCO, replaceCO->currentText());
+}
+
+
 void GuiSearchWidget::replace(docstring const & search, docstring const & replace,
                            bool casesensitive, bool matchword,
-                           bool forward, bool all, bool wrap)
+                           bool forward, bool all, bool wrap, bool onlysel)
 {
        docstring const sdata =
                replace2string(replace, search, casesensitive,
-                                    matchword, all, forward, wrap);
+                              matchword, all, forward, true, wrap, onlysel);
+
        dispatch(FuncRequest(LFUN_WORD_REPLACE, sdata));
 }
 
@@ -261,6 +541,7 @@ void GuiSearchWidget::saveSession(QSettings & settings, QString const & session_
        settings.setValue(session_key + "/words", wordsCB->isChecked());
        settings.setValue(session_key + "/instant", instantSearchCB->isChecked());
        settings.setValue(session_key + "/wrap", wrapCB->isChecked());
+       settings.setValue(session_key + "/selection", selectionCB->isChecked());
        settings.setValue(session_key + "/minimized", minimized_);
 }
 
@@ -269,9 +550,15 @@ void GuiSearchWidget::restoreSession(QString const & session_key)
 {
        QSettings settings;
        caseCB->setChecked(settings.value(session_key + "/casesensitive", false).toBool());
+       act_casesense_->setChecked(settings.value(session_key + "/casesensitive", false).toBool());
        wordsCB->setChecked(settings.value(session_key + "/words", false).toBool());
+       act_wholewords_->setChecked(settings.value(session_key + "/words", false).toBool());
        instantSearchCB->setChecked(settings.value(session_key + "/instant", false).toBool());
-       wrapCB->setChecked(settings.value(session_key + "/wrap", false).toBool());
+       act_immediate_->setChecked(settings.value(session_key + "/instant", false).toBool());
+       wrapCB->setChecked(settings.value(session_key + "/wrap", true).toBool());
+       act_wrap_->setChecked(settings.value(session_key + "/wrap", true).toBool());
+       selectionCB->setChecked(settings.value(session_key + "/selection", false).toBool());
+       act_selection_->setChecked(settings.value(session_key + "/selection", false).toBool());
        minimized_ = settings.value(session_key + "/minimized", false).toBool();
        // initialize hidings
        minimizeClicked(false);
@@ -280,16 +567,54 @@ void GuiSearchWidget::restoreSession(QString const & session_key)
 
 GuiSearch::GuiSearch(GuiView & parent, Qt::DockWidgetArea area, Qt::WindowFlags flags)
        : DockView(parent, "findreplace", qt_("Search and Replace"), area, flags),
-         widget_(new GuiSearchWidget(this))
+         widget_(new GuiSearchWidget(this, parent))
 {
        setWidget(widget_);
        widget_->setBufferView(bufferview());
-       setFocusProxy(widget_->findCO);
+       setFocusProxy(widget_);
 
        connect(widget_, SIGNAL(needTitleBarUpdate()), this, SLOT(updateTitle()));
        connect(widget_, SIGNAL(needSizeUpdate()), this, SLOT(updateSize()));
 }
 
+
+void GuiSearch::mousePressEvent(QMouseEvent * event)
+{
+       if (isFloating() && event->button() == Qt::LeftButton) {
+#if QT_VERSION >= 0x060000
+               dragPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
+#else
+               dragPosition = event->globalPos() - frameGeometry().topLeft();
+#endif
+               event->accept();
+       } else
+               DockView::mousePressEvent(event);
+}
+
+
+void GuiSearch::mouseMoveEvent(QMouseEvent * event)
+{
+       if (isFloating() && event->buttons() & Qt::LeftButton) {
+#if QT_VERSION >= 0x060000
+               move(event->globalPosition().toPoint() - dragPosition);
+#else
+               move(event->globalPos() - dragPosition);
+#endif
+               event->accept();
+       } else
+               DockView::mouseMoveEvent(event);
+}
+
+
+void GuiSearch::mouseDoubleClickEvent(QMouseEvent * event)
+{
+       if (event->button() == Qt::LeftButton)
+               setFloating(!isFloating());
+       else
+               DockView::mouseDoubleClickEvent(event);
+}
+
+
 void GuiSearch::onBufferViewChanged()
 {
        widget_->setEnabled(static_cast<bool>(bufferview()));
@@ -324,9 +649,10 @@ void GuiSearch::updateTitle()
                // remove title bar
                setTitleBarWidget(new QWidget());
                titleBarWidget()->hide();
-       } else
+       } else if (titleBarWidget()) {
                // restore title bar
                setTitleBarWidget(nullptr);
+       }
 }