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 "qt_helpers.h"
19 #include "FuncRequest.h"
22 #include <QCloseEvent>
31 static void uniqueInsert(QComboBox * box, QString const & text)
33 for (int i = box->count(); --i >= 0; )
34 if (box->itemText(i) == text)
41 GuiSearch::GuiSearch(GuiView & lv)
42 : GuiDialog(lv, "findreplace", qt_("Find and Replace"))
46 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
47 connect(findPB, SIGNAL(clicked()), this, SLOT(findClicked()));
48 connect(replacePB, SIGNAL(clicked()), this, SLOT(replaceClicked()));
49 connect(replaceallPB, SIGNAL(clicked()), this, SLOT(replaceallClicked()));
50 connect(findCO, SIGNAL(editTextChanged(QString)),
51 this, SLOT(findChanged()));
53 setFocusProxy(findCO);
55 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
56 bc().setCancel(closePB);
57 bc().addReadOnly(replaceCO);
58 bc().addReadOnly(replacePB);
59 bc().addReadOnly(replaceallPB);
61 replacePB->setEnabled(false);
62 replaceallPB->setEnabled(false);
66 void GuiSearch::showEvent(QShowEvent * e)
68 findCO->lineEdit()->selectAll();
69 GuiDialog::showEvent(e);
73 void GuiSearch::closeEvent(QCloseEvent * e)
76 GuiDialog::closeEvent(e);
80 void GuiSearch::findChanged()
82 if (findCO->currentText().isEmpty()) {
83 findPB->setEnabled(false);
84 replacePB->setEnabled(false);
85 replaceallPB->setEnabled(false);
87 findPB->setEnabled(true);
88 replacePB->setEnabled(!isBufferReadonly());
89 replaceallPB->setEnabled(!isBufferReadonly());
94 void GuiSearch::findClicked()
96 docstring const needle = qstring_to_ucs4(findCO->currentText());
97 find(needle, caseCB->isChecked(), wordsCB->isChecked(),
98 !backwardsCB->isChecked());
99 uniqueInsert(findCO, findCO->currentText());
100 findCO->lineEdit()->selectAll();
104 void GuiSearch::replaceClicked()
106 docstring const needle = qstring_to_ucs4(findCO->currentText());
107 docstring const repl = qstring_to_ucs4(replaceCO->currentText());
108 replace(needle, repl, caseCB->isChecked(), wordsCB->isChecked(),
109 !backwardsCB->isChecked(), false);
110 uniqueInsert(findCO, findCO->currentText());
111 uniqueInsert(replaceCO, replaceCO->currentText());
115 void GuiSearch::replaceallClicked()
117 replace(qstring_to_ucs4(findCO->currentText()),
118 qstring_to_ucs4(replaceCO->currentText()),
119 caseCB->isChecked(), wordsCB->isChecked(), true, true);
120 uniqueInsert(findCO, findCO->currentText());
121 uniqueInsert(replaceCO, replaceCO->currentText());
125 void GuiSearch::find(docstring const & search, bool casesensitive,
126 bool matchword, bool forward)
128 docstring const data =
129 find2string(search, casesensitive, matchword, forward);
130 dispatch(FuncRequest(LFUN_WORD_FIND, data));
134 void GuiSearch::replace(docstring const & search, docstring const & replace,
135 bool casesensitive, bool matchword,
136 bool forward, bool all)
138 docstring const data =
139 replace2string(search, replace, casesensitive,
140 matchword, all, forward);
141 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
144 Dialog * createGuiSearch(GuiView & lv) { return new GuiSearch(lv); }
147 } // namespace frontend
151 #include "GuiSearch_moc.cpp"