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 "GuiSearch.h"
17 #include "FuncRequest.h"
20 #include "qt_helpers.h"
23 #include <QCloseEvent>
32 static void uniqueInsert(QComboBox * box, QString const & text)
34 for (int i = box->count(); --i >= 0; )
35 if (box->itemText(i) == text)
42 GuiSearch::GuiSearch(GuiView & lv)
43 : GuiDialog(lv, "findreplace")
46 setViewTitle(_("Find and Replace"));
48 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
49 connect(findPB, SIGNAL(clicked()), this, SLOT(findClicked()));
50 connect(replacePB, SIGNAL(clicked()), this, SLOT(replaceClicked()));
51 connect(replaceallPB, SIGNAL(clicked()), this, SLOT(replaceallClicked()));
52 connect(findCO, SIGNAL(editTextChanged(QString)),
53 this, SLOT(findChanged()));
55 setFocusProxy(findCO);
57 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
58 bc().setCancel(closePB);
59 bc().addReadOnly(replaceCO);
60 bc().addReadOnly(replacePB);
61 bc().addReadOnly(replaceallPB);
63 replacePB->setEnabled(false);
64 replaceallPB->setEnabled(false);
68 void GuiSearch::showEvent(QShowEvent * e)
70 findCO->lineEdit()->selectAll();
71 GuiDialog::showEvent(e);
75 void GuiSearch::closeEvent(QCloseEvent * e)
78 GuiDialog::closeEvent(e);
82 void GuiSearch::findChanged()
84 if (findCO->currentText().isEmpty()) {
85 findPB->setEnabled(false);
86 replacePB->setEnabled(false);
87 replaceallPB->setEnabled(false);
89 findPB->setEnabled(true);
90 replacePB->setEnabled(!isBufferReadonly());
91 replaceallPB->setEnabled(!isBufferReadonly());
96 void GuiSearch::findClicked()
98 docstring const needle = qstring_to_ucs4(findCO->currentText());
99 find(needle, caseCB->isChecked(), wordsCB->isChecked(),
100 !backwardsCB->isChecked());
101 uniqueInsert(findCO, findCO->currentText());
102 findCO->lineEdit()->selectAll();
106 void GuiSearch::replaceClicked()
108 docstring const needle = qstring_to_ucs4(findCO->currentText());
109 docstring const repl = qstring_to_ucs4(replaceCO->currentText());
110 replace(needle, repl, caseCB->isChecked(), wordsCB->isChecked(),
111 !backwardsCB->isChecked(), false);
112 uniqueInsert(findCO, findCO->currentText());
113 uniqueInsert(replaceCO, replaceCO->currentText());
117 void GuiSearch::replaceallClicked()
119 replace(qstring_to_ucs4(findCO->currentText()),
120 qstring_to_ucs4(replaceCO->currentText()),
121 caseCB->isChecked(), wordsCB->isChecked(), true, true);
122 uniqueInsert(findCO, findCO->currentText());
123 uniqueInsert(replaceCO, replaceCO->currentText());
127 void GuiSearch::find(docstring const & search, bool casesensitive,
128 bool matchword, bool forward)
130 docstring const data =
131 find2string(search, casesensitive, matchword, forward);
132 dispatch(FuncRequest(LFUN_WORD_FIND, data));
136 void GuiSearch::replace(docstring const & search, docstring const & replace,
137 bool casesensitive, bool matchword,
138 bool forward, bool all)
140 docstring const data =
141 replace2string(search, replace, casesensitive,
142 matchword, all, forward);
143 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
146 Dialog * createGuiSearch(GuiView & lv) { return new GuiSearch(lv); }
149 } // namespace frontend
153 #include "GuiSearch_moc.cpp"