3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS.
15 #include "GuiApplication.h"
16 #include "GuiSearch.h"
19 #include "qt_helpers.h"
20 #include "FuncRequest.h"
22 #include "BufferView.h"
25 #include "FuncRequest.h"
27 #include "GuiKeySymbol.h"
30 #include "qt_helpers.h"
31 #include "support/filetools.h"
32 #include "support/debug.h"
33 #include "support/gettext.h"
34 #include "support/FileName.h"
35 #include "frontends/alert.h"
36 #include "frontends/Clipboard.h"
43 #include "QSizePolicy"
44 #if QT_VERSION >= 0x050000
45 #include <QSvgRenderer>
49 using namespace lyx::support;
56 static void uniqueInsert(QComboBox * box, QString const & text)
58 for (int i = box->count(); --i >= 0; )
59 if (box->itemText(i) == text)
62 box->insertItem(0, text);
66 GuiSearchWidget::GuiSearchWidget(QWidget * parent)
71 // fix height to minimum
72 setFixedHeight(sizeHint().height());
74 // align items in grid on top
75 gridLayout->setAlignment(Qt::AlignTop);
77 connect(findPB, SIGNAL(clicked()), this, SLOT(findClicked()));
78 connect(findPrevPB, SIGNAL(clicked()), this, SLOT(findPrevClicked()));
79 connect(minimizePB, SIGNAL(clicked()), this, SLOT(minimizeClicked()));
80 connect(replacePB, SIGNAL(clicked()), this, SLOT(replaceClicked()));
81 connect(replacePrevPB, SIGNAL(clicked()), this, SLOT(replacePrevClicked()));
82 connect(replaceallPB, SIGNAL(clicked()), this, SLOT(replaceallClicked()));
83 connect(findCO, SIGNAL(editTextChanged(QString)),
84 this, SLOT(findChanged()));
85 if(qApp->clipboard()->supportsFindBuffer()) {
86 connect(qApp->clipboard(), SIGNAL(findBufferChanged()),
87 this, SLOT(findBufferChanged()));
91 setFocusProxy(findCO);
93 // Use a FancyLineEdit due to the indicator icons
94 findLE_ = new FancyLineEdit(this);
95 findCO->setLineEdit(findLE_);
97 // And a menu in minimal mode
99 act_casesense_ = new QAction(qt_("&Case sensitive[[search]]"), this);
100 act_casesense_->setCheckable(true);
101 act_wholewords_ = new QAction(qt_("Wh&ole words"), this);
102 act_wholewords_->setCheckable(true);
103 act_selection_ = new QAction(qt_("Selection onl&y"), this);
104 act_selection_->setCheckable(true);
105 act_immediate_ = new QAction(qt_("Search as yo&u type"), this);
106 act_immediate_->setCheckable(true);
107 act_wrap_ = new QAction(qt_("&Wrap"), this);
108 act_wrap_->setCheckable(true);
110 menu_->addAction(act_casesense_);
111 menu_->addAction(act_wholewords_);
112 menu_->addAction(act_selection_);
113 menu_->addAction(act_immediate_);
114 menu_->addAction(act_wrap_);
115 findLE_->setButtonMenu(FancyLineEdit::Right, menu_);
117 connect(act_casesense_, SIGNAL(triggered()), this, SLOT(caseSenseActTriggered()));
118 connect(act_wholewords_, SIGNAL(triggered()), this, SLOT(wholeWordsActTriggered()));
119 connect(act_selection_, SIGNAL(triggered()), this, SLOT(searchSelActTriggered()));
120 connect(act_immediate_, SIGNAL(triggered()), this, SLOT(immediateActTriggered()));
121 connect(act_wrap_, SIGNAL(triggered()), this, SLOT(wrapActTriggered()));
123 findCO->setCompleter(nullptr);
124 replaceCO->setCompleter(nullptr);
126 replacePB->setEnabled(false);
127 replacePrevPB->setEnabled(false);
128 replaceallPB->setEnabled(false);
132 bool GuiSearchWidget::initialiseParams(std::string const & str)
135 findCO->lineEdit()->setText(toqstr(str));
140 void GuiSearchWidget::keyPressEvent(QKeyEvent * ev)
143 setKeySymbol(&sym, ev);
145 // catch Return and Shift-Return
146 if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Enter) {
147 doFind(ev->modifiers() == Qt::ShiftModifier);
150 if (ev->key() == Qt::Key_Escape) {
151 dispatch(FuncRequest(LFUN_DIALOG_HIDE, "findreplace"));
155 // we catch the key sequences for forward and backwards search
157 KeyModifier mod = lyx::q_key_state(ev->modifiers());
158 KeySequence keyseq(&theTopLevelKeymap(), &theTopLevelKeymap());
159 FuncRequest fr = keyseq.addkey(sym, mod);
160 if (fr == FuncRequest(LFUN_WORD_FIND_FORWARD)
161 || fr == FuncRequest(LFUN_WORD_FIND)) {
165 if (fr == FuncRequest(LFUN_WORD_FIND_BACKWARD)) {
169 if (fr == FuncRequest(LFUN_DIALOG_TOGGLE, "findreplace")) {
174 QWidget::keyPressEvent(ev);
178 void GuiSearchWidget::minimizeClicked(bool const toggle)
181 minimized_ = !minimized_;
183 replaceLA->setHidden(minimized_);
184 replaceCO->setHidden(minimized_);
185 replacePB->setHidden(minimized_);
186 replacePrevPB->setHidden(minimized_);
187 replaceallPB->setHidden(minimized_);
188 CBFrame->setHidden(minimized_);
191 minimizePB->setText(qt_("Ex&pand"));
192 minimizePB->setToolTip(qt_("Show replace and option widgets"));
195 act_casesense_->setChecked(caseCB->isChecked());
196 act_immediate_->setChecked(instantSearchCB->isChecked());
197 act_selection_->setChecked(selectionCB->isChecked());
198 act_wholewords_->setChecked(wordsCB->isChecked());
199 act_wrap_->setChecked(wrapCB->isChecked());
202 minimizePB->setText(qt_("&Minimize"));
203 minimizePB->setToolTip(qt_("Hide replace and option widgets"));
206 Q_EMIT needSizeUpdate();
207 Q_EMIT needTitleBarUpdate();
212 void GuiSearchWidget::handleIndicators()
214 findLE_->setButtonVisible(FancyLineEdit::Right, minimized_);
220 if (caseCB->isChecked())
222 if (wordsCB->isChecked())
224 if (selectionCB->isChecked())
226 if (instantSearchCB->isChecked())
228 if (wrapCB->isChecked())
231 bool const dark_mode = guiApp && guiApp->isInDarkMode();
233 #if QT_VERSION >= 0x050000
234 // Consider device/pixel ratio (HiDPI)
235 if (guiApp && guiApp->currentView())
236 dpr = guiApp->currentView()->devicePixelRatio();
238 QString imagedir = "images/";
239 QPixmap bpixmap = getPixmap("images/", "search-options", "svgz,png");
240 QPixmap pm = bpixmap;
244 QPixmap scaled_pm = QPixmap(bpixmap.size() * dpr);
245 pm = QPixmap(pms * scaled_pm.width() + ((pms - 1) * gap),
247 pm.fill(Qt::transparent);
248 QPainter painter(&pm);
251 tip = qt_("Active options:");
253 if (caseCB->isChecked()) {
254 tip += "<li>" + qt_("Case sensitive search");
255 QPixmap spixmap = getPixmap("images/", "search-case-sensitive", "svgz,png");
256 #if QT_VERSION < 0x050000
257 painter.drawPixmap(x, 0, spixmap);
259 // With Qt5, we render SVG directly for HiDPI scalability
260 FileName fname = imageLibFileSearch(imagedir, "search-case-sensitive", "svgz,png");
261 QString fpath = toqstr(fname.absFileName());
262 if (!fpath.isEmpty()) {
263 QSvgRenderer svgRenderer(fpath);
264 if (svgRenderer.isValid())
265 svgRenderer.render(&painter, QRectF(0, 0, spixmap.width() * dpr,
266 spixmap.height() * dpr));
269 x += (spixmap.width() * dpr) + gap;
271 if (wordsCB->isChecked()) {
272 tip += "<li>" + qt_("Whole words only");
273 QPixmap spixmap = getPixmap("images/", "search-whole-words", "svgz,png");
274 #if QT_VERSION < 0x050000
275 painter.drawPixmap(x, 0, spixmap);
277 FileName fname = imageLibFileSearch(imagedir, "search-whole-words", "svgz,png");
278 QString fpath = toqstr(fname.absFileName());
279 if (!fpath.isEmpty()) {
280 QSvgRenderer svgRenderer(fpath);
281 if (svgRenderer.isValid())
282 svgRenderer.render(&painter, QRectF(x, 0, spixmap.width() * dpr,
283 spixmap.height() * dpr));
286 x += (spixmap.width() * dpr) + gap;
288 if (selectionCB->isChecked()) {
289 tip += "<li>" + qt_("Search only in selection");
290 QPixmap spixmap = getPixmap("images/", "search-selection", "svgz,png");
291 #if QT_VERSION < 0x050000
292 painter.drawPixmap(x, 0, spixmap);
294 FileName fname = imageLibFileSearch(imagedir, "search-selection", "svgz,png");
295 QString fpath = toqstr(fname.absFileName());
296 if (!fpath.isEmpty()) {
297 QSvgRenderer svgRenderer(fpath);
298 if (svgRenderer.isValid())
299 svgRenderer.render(&painter, QRectF(x, 0, spixmap.width() * dpr,
300 spixmap.height() * dpr));
303 x += (spixmap.width() * dpr) + gap;
305 if (instantSearchCB->isChecked()) {
306 tip += "<li>" + qt_("Search as you type");
307 QPixmap spixmap = getPixmap("images/", "search-instant", "svgz,png");
308 #if QT_VERSION < 0x050000
309 painter.drawPixmap(x, 0, spixmap);
311 FileName fname = imageLibFileSearch(imagedir, "search-instant", "svgz,png");
312 QString fpath = toqstr(fname.absFileName());
313 if (!fpath.isEmpty()) {
314 QSvgRenderer svgRenderer(fpath);
315 if (svgRenderer.isValid())
316 svgRenderer.render(&painter, QRectF(x, 0, spixmap.width() * dpr,
317 spixmap.height() * dpr));
320 x += (spixmap.width() * dpr) + gap;
322 if (wrapCB->isChecked()) {
323 tip += "<li>" + qt_("Wrap search");
324 QPixmap spixmap = getPixmap("images/", "search-wrap", "svgz,png");
325 #if QT_VERSION < 0x050000
326 painter.drawPixmap(x, 0, spixmap);
328 FileName fname = imageLibFileSearch(imagedir, "search-wrap", "svgz,png");
329 QString fpath = toqstr(fname.absFileName());
330 if (!fpath.isEmpty()) {
331 QSvgRenderer svgRenderer(fpath);
332 if (svgRenderer.isValid())
333 svgRenderer.render(&painter, QRectF(x, 0, spixmap.width() * dpr,
334 spixmap.height() * dpr));
337 x += (spixmap.width() * dpr) + gap;
340 #if QT_VERSION >= 0x050000
341 pm.setDevicePixelRatio(dpr);
345 tip = qt_("Click here to change search options");
346 #if QT_VERSION >= 0x050000
347 // With Qt5, we render SVG directly for HiDPI scalability
348 FileName fname = imageLibFileSearch(imagedir, "search-options", "svgz,png");
349 QString fpath = toqstr(fname.absFileName());
350 if (!fpath.isEmpty()) {
351 QSvgRenderer svgRenderer(fpath);
352 if (svgRenderer.isValid()) {
353 pm = QPixmap(bpixmap.size() * dpr);
354 pm.fill(Qt::transparent);
355 QPainter painter(&pm);
356 svgRenderer.render(&painter);
357 pm.setDevicePixelRatio(dpr);
363 QImage img = pm.toImage();
365 pm.convertFromImage(img);
367 findLE_->setButtonPixmap(FancyLineEdit::Right, pm);
369 findLE_->setButtonToolTip(FancyLineEdit::Right, tip);
373 void GuiSearchWidget::caseSenseActTriggered()
375 caseCB->setChecked(act_casesense_->isChecked());
380 void GuiSearchWidget::wholeWordsActTriggered()
382 wordsCB->setChecked(act_wholewords_->isChecked());
387 void GuiSearchWidget::searchSelActTriggered()
389 selectionCB->setChecked(act_selection_->isChecked());
394 void GuiSearchWidget::immediateActTriggered()
396 instantSearchCB->setChecked(act_immediate_->isChecked());
401 void GuiSearchWidget::wrapActTriggered()
403 wrapCB->setChecked(act_wrap_->isChecked());
408 void GuiSearchWidget::showEvent(QShowEvent * e)
412 findCO->lineEdit()->selectAll();
413 QWidget::showEvent(e);
417 void GuiSearchWidget::findBufferChanged()
419 docstring search = theClipboard().getFindBuffer();
420 // update from find buffer, but only if the strings differ (else we
421 // might end up in loops with search as you type)
422 if (!search.empty() && toqstr(search) != findCO->lineEdit()->text()) {
423 LYXERR(Debug::CLIPBOARD, "from findbuffer: " << search);
424 findCO->lineEdit()->selectAll();
425 findCO->lineEdit()->insert(toqstr(search));
426 findCO->lineEdit()->selectAll();
431 void GuiSearchWidget::findChanged()
433 bool const emptytext = findCO->currentText().isEmpty();
434 findPB->setEnabled(!emptytext);
435 findPrevPB->setEnabled(!emptytext);
436 bool const replace = !emptytext && bv_ && !bv_->buffer().isReadonly();
437 replacePB->setEnabled(replace);
438 replacePrevPB->setEnabled(replace);
439 replaceallPB->setEnabled(replace);
440 if (instantSearchCB->isChecked())
445 void GuiSearchWidget::findClicked()
451 void GuiSearchWidget::findPrevClicked()
457 void GuiSearchWidget::replaceClicked()
463 void GuiSearchWidget::replacePrevClicked()
469 void GuiSearchWidget::replaceallClicked()
471 replace(qstring_to_ucs4(findCO->currentText()),
472 qstring_to_ucs4(replaceCO->currentText()),
473 caseCB->isChecked(), wordsCB->isChecked(),
474 true, true, true, selectionCB->isChecked());
475 uniqueInsert(findCO, findCO->currentText());
476 uniqueInsert(replaceCO, replaceCO->currentText());
480 void GuiSearchWidget::doFind(bool const backwards, bool const instant)
482 docstring const needle = qstring_to_ucs4(findCO->currentText());
483 find(needle, caseCB->isChecked(), wordsCB->isChecked(), !backwards,
484 instant, wrapCB->isChecked(), selectionCB->isChecked());
485 uniqueInsert(findCO, findCO->currentText());
487 findCO->lineEdit()->selectAll();
491 void GuiSearchWidget::find(docstring const & search, bool casesensitive,
492 bool matchword, bool forward, bool instant,
493 bool wrap, bool onlysel)
495 docstring const sdata =
496 find2string(search, casesensitive, matchword,
497 forward, wrap, instant, onlysel);
499 dispatch(FuncRequest(LFUN_WORD_FIND, sdata));
503 void GuiSearchWidget::doReplace(bool const backwards)
505 docstring const needle = qstring_to_ucs4(findCO->currentText());
506 docstring const repl = qstring_to_ucs4(replaceCO->currentText());
507 replace(needle, repl, caseCB->isChecked(), wordsCB->isChecked(),
508 !backwards, false, wrapCB->isChecked(), selectionCB->isChecked());
509 uniqueInsert(findCO, findCO->currentText());
510 uniqueInsert(replaceCO, replaceCO->currentText());
514 void GuiSearchWidget::replace(docstring const & search, docstring const & replace,
515 bool casesensitive, bool matchword,
516 bool forward, bool all, bool wrap, bool onlysel)
518 docstring const sdata =
519 replace2string(replace, search, casesensitive,
520 matchword, all, forward, true, wrap, onlysel);
522 dispatch(FuncRequest(LFUN_WORD_REPLACE, sdata));
525 void GuiSearchWidget::saveSession(QSettings & settings, QString const & session_key) const
527 settings.setValue(session_key + "/casesensitive", caseCB->isChecked());
528 settings.setValue(session_key + "/words", wordsCB->isChecked());
529 settings.setValue(session_key + "/instant", instantSearchCB->isChecked());
530 settings.setValue(session_key + "/wrap", wrapCB->isChecked());
531 settings.setValue(session_key + "/selection", selectionCB->isChecked());
532 settings.setValue(session_key + "/minimized", minimized_);
536 void GuiSearchWidget::restoreSession(QString const & session_key)
539 caseCB->setChecked(settings.value(session_key + "/casesensitive", false).toBool());
540 act_casesense_->setChecked(settings.value(session_key + "/casesensitive", false).toBool());
541 wordsCB->setChecked(settings.value(session_key + "/words", false).toBool());
542 act_wholewords_->setChecked(settings.value(session_key + "/words", false).toBool());
543 instantSearchCB->setChecked(settings.value(session_key + "/instant", false).toBool());
544 act_immediate_->setChecked(settings.value(session_key + "/instant", false).toBool());
545 wrapCB->setChecked(settings.value(session_key + "/wrap", false).toBool());
546 act_wrap_->setChecked(settings.value(session_key + "/wrap", false).toBool());
547 selectionCB->setChecked(settings.value(session_key + "/selection", false).toBool());
548 act_selection_->setChecked(settings.value(session_key + "/selection", false).toBool());
549 minimized_ = settings.value(session_key + "/minimized", false).toBool();
550 // initialize hidings
551 minimizeClicked(false);
555 GuiSearch::GuiSearch(GuiView & parent, Qt::DockWidgetArea area, Qt::WindowFlags flags)
556 : DockView(parent, "findreplace", qt_("Search and Replace"), area, flags),
557 widget_(new GuiSearchWidget(this))
560 widget_->setBufferView(bufferview());
561 setFocusProxy(widget_);
563 connect(widget_, SIGNAL(needTitleBarUpdate()), this, SLOT(updateTitle()));
564 connect(widget_, SIGNAL(needSizeUpdate()), this, SLOT(updateSize()));
567 void GuiSearch::onBufferViewChanged()
569 widget_->setEnabled(static_cast<bool>(bufferview()));
570 widget_->setBufferView(bufferview());
574 void GuiSearch::updateView()
581 void GuiSearch::saveSession(QSettings & settings) const
583 Dialog::saveSession(settings);
584 widget_->saveSession(settings, sessionKey());
588 void GuiSearch::restoreSession()
590 DockView::restoreSession();
591 widget_->restoreSession(sessionKey());
595 void GuiSearch::updateTitle()
597 if (widget_->isMinimized()) {
599 setTitleBarWidget(new QWidget());
600 titleBarWidget()->hide();
603 setTitleBarWidget(nullptr);
607 void GuiSearch::updateSize()
609 widget_->setFixedHeight(widget_->sizeHint().height());
610 if (widget_->isMinimized())
611 setFixedHeight(widget_->sizeHint().height());
613 // undo setFixedHeight
614 setMaximumHeight(QWIDGETSIZE_MAX);
621 } // namespace frontend
625 #include "moc_GuiSearch.cpp"