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"
30 static void uniqueInsert(QComboBox * box, QString const & text)
32 for (int i = box->count(); --i >= 0; )
33 if (box->itemText(i) == text)
36 box->insertItem(0, text);
40 GuiSearch::GuiSearch(GuiView & lv)
41 : GuiDialog(lv, "findreplace", qt_("Find and Replace"))
45 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
46 connect(findPB, SIGNAL(clicked()), this, SLOT(findClicked()));
47 connect(replacePB, SIGNAL(clicked()), this, SLOT(replaceClicked()));
48 connect(replaceallPB, SIGNAL(clicked()), this, SLOT(replaceallClicked()));
49 connect(findCO, SIGNAL(editTextChanged(QString)),
50 this, SLOT(findChanged()));
52 setFocusProxy(findCO);
54 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
55 bc().setCancel(closePB);
56 bc().addReadOnly(replaceCO);
57 bc().addReadOnly(replacePB);
58 bc().addReadOnly(replaceallPB);
60 replacePB->setEnabled(false);
61 replaceallPB->setEnabled(false);
65 void GuiSearch::showEvent(QShowEvent * e)
68 findCO->lineEdit()->selectAll();
69 GuiDialog::showEvent(e);
73 void GuiSearch::findChanged()
75 if (findCO->currentText().isEmpty()) {
76 findPB->setEnabled(false);
77 replacePB->setEnabled(false);
78 replaceallPB->setEnabled(false);
80 findPB->setEnabled(true);
81 replacePB->setEnabled(!isBufferReadonly());
82 replaceallPB->setEnabled(!isBufferReadonly());
87 void GuiSearch::findClicked()
89 docstring const needle = qstring_to_ucs4(findCO->currentText());
90 find(needle, caseCB->isChecked(), wordsCB->isChecked(),
91 !backwardsCB->isChecked());
92 uniqueInsert(findCO, findCO->currentText());
93 findCO->lineEdit()->selectAll();
97 void GuiSearch::replaceClicked()
99 docstring const needle = qstring_to_ucs4(findCO->currentText());
100 docstring const repl = qstring_to_ucs4(replaceCO->currentText());
101 replace(needle, repl, caseCB->isChecked(), wordsCB->isChecked(),
102 !backwardsCB->isChecked(), false);
103 uniqueInsert(findCO, findCO->currentText());
104 uniqueInsert(replaceCO, replaceCO->currentText());
108 void GuiSearch::replaceallClicked()
110 replace(qstring_to_ucs4(findCO->currentText()),
111 qstring_to_ucs4(replaceCO->currentText()),
112 caseCB->isChecked(), wordsCB->isChecked(), true, true);
113 uniqueInsert(findCO, findCO->currentText());
114 uniqueInsert(replaceCO, replaceCO->currentText());
118 void GuiSearch::find(docstring const & search, bool casesensitive,
119 bool matchword, bool forward)
121 docstring const data =
122 find2string(search, casesensitive, matchword, forward);
123 dispatch(FuncRequest(LFUN_WORD_FIND, data));
127 void GuiSearch::replace(docstring const & search, docstring const & replace,
128 bool casesensitive, bool matchword,
129 bool forward, bool all)
131 docstring const data =
132 replace2string(replace, search, casesensitive,
133 matchword, all, forward);
134 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
137 Dialog * createGuiSearch(GuiView & lv) { return new GuiSearch(lv); }
140 } // namespace frontend
144 #include "moc_GuiSearch.cpp"