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>
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")
45 setViewTitle(_("Find and Replace"));
47 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
48 connect(findPB, SIGNAL(clicked()), this, SLOT(findClicked()));
49 connect(replacePB, SIGNAL(clicked()), this, SLOT(replaceClicked()));
50 connect(replaceallPB, SIGNAL(clicked()), this, SLOT(replaceallClicked()));
51 connect(findCO, SIGNAL(editTextChanged(QString)),
52 this, SLOT(findChanged()));
54 setFocusProxy(findCO);
56 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
57 bc().setCancel(closePB);
58 bc().addReadOnly(replaceCO);
59 bc().addReadOnly(replacePB);
60 bc().addReadOnly(replaceallPB);
62 replacePB->setEnabled(false);
63 replaceallPB->setEnabled(false);
67 void GuiSearch::showEvent(QShowEvent * e)
69 findCO->lineEdit()->selectAll();
70 GuiDialog::showEvent(e);
74 void GuiSearch::closeEvent(QCloseEvent * e)
77 GuiDialog::closeEvent(e);
81 void GuiSearch::findChanged()
83 if (findCO->currentText().isEmpty()) {
84 findPB->setEnabled(false);
85 replacePB->setEnabled(false);
86 replaceallPB->setEnabled(false);
88 findPB->setEnabled(true);
89 replacePB->setEnabled(!isBufferReadonly());
90 replaceallPB->setEnabled(!isBufferReadonly());
95 void GuiSearch::findClicked()
97 docstring const needle = qstring_to_ucs4(findCO->currentText());
98 find(needle, caseCB->isChecked(), wordsCB->isChecked(),
99 !backwardsCB->isChecked());
100 uniqueInsert(findCO, findCO->currentText());
101 findCO->lineEdit()->selectAll();
105 void GuiSearch::replaceClicked()
107 docstring const needle = qstring_to_ucs4(findCO->currentText());
108 docstring const repl = qstring_to_ucs4(replaceCO->currentText());
109 replace(needle, repl, caseCB->isChecked(), wordsCB->isChecked(),
110 !backwardsCB->isChecked(), false);
111 uniqueInsert(findCO, findCO->currentText());
112 uniqueInsert(replaceCO, replaceCO->currentText());
116 void GuiSearch::replaceallClicked()
118 replace(qstring_to_ucs4(findCO->currentText()),
119 qstring_to_ucs4(replaceCO->currentText()),
120 caseCB->isChecked(), wordsCB->isChecked(), true, true);
121 uniqueInsert(findCO, findCO->currentText());
122 uniqueInsert(replaceCO, replaceCO->currentText());
126 void GuiSearch::find(docstring const & search, bool casesensitive,
127 bool matchword, bool forward)
129 docstring const data =
130 find2string(search, casesensitive, matchword, forward);
131 dispatch(FuncRequest(LFUN_WORD_FIND, data));
135 void GuiSearch::replace(docstring const & search, docstring const & replace,
136 bool casesensitive, bool matchword,
137 bool forward, bool all)
139 docstring const data =
140 replace2string(search, replace, casesensitive,
141 matchword, all, forward);
142 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
145 Dialog * createGuiSearch(GuiView & lv) { return new GuiSearch(lv); }
148 } // namespace frontend
152 #include "GuiSearch_moc.cpp"