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.
16 #include "qt_helpers.h"
17 #include "FuncRequest.h"
18 #include "BufferView.h"
21 #include "GuiSearch.h"
24 #include "support/gettext.h"
25 #include "frontends/alert.h"
35 static void uniqueInsert(QComboBox * box, QString const & text)
37 for (int i = box->count(); --i >= 0; )
38 if (box->itemText(i) == text)
41 box->insertItem(0, text);
45 GuiSearch::GuiSearch(GuiView & lv)
46 : GuiDialog(lv, "findreplace", qt_("Find and Replace"))
50 // fix height to minimum
51 setFixedHeight(sizeHint().height());
53 connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
54 this, SLOT(slotButtonBox(QAbstractButton *)));
55 connect(findPB, SIGNAL(clicked()), this, SLOT(findClicked()));
56 connect(replacePB, SIGNAL(clicked()), this, SLOT(replaceClicked()));
57 connect(replaceallPB, SIGNAL(clicked()), this, SLOT(replaceallClicked()));
58 connect(findCO, SIGNAL(editTextChanged(QString)),
59 this, SLOT(findChanged()));
61 setFocusProxy(findCO);
63 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
64 bc().setCancel(buttonBox->button(QDialogButtonBox::Close));
65 bc().addReadOnly(replaceCO);
66 bc().addReadOnly(replacePB);
67 bc().addReadOnly(replaceallPB);
69 replacePB->setEnabled(false);
70 replaceallPB->setEnabled(false);
74 void GuiSearch::showEvent(QShowEvent * e)
77 findCO->lineEdit()->selectAll();
78 GuiDialog::showEvent(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 sdata =
131 find2string(search, casesensitive, matchword, forward);
132 dispatch(FuncRequest(LFUN_WORD_FIND, sdata));
136 void GuiSearch::replace(docstring const & search, docstring const & replace,
137 bool casesensitive, bool matchword,
138 bool forward, bool all)
140 docstring const sdata =
141 replace2string(replace, search, casesensitive,
142 matchword, all, forward);
143 dispatch(FuncRequest(LFUN_WORD_REPLACE, sdata));
147 Dialog * createGuiSearch(GuiView & lv) { return new GuiSearch(lv); }
150 } // namespace frontend
154 #include "moc_GuiSearch.cpp"