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"
18 #include "qt_helpers.h"
19 #include "FuncRequest.h"
20 #include "BufferView.h"
23 #include "FuncRequest.h"
25 #include "GuiKeySymbol.h"
28 #include "support/gettext.h"
29 #include "frontends/alert.h"
41 static void uniqueInsert(QComboBox * box, QString const & text)
43 for (int i = box->count(); --i >= 0; )
44 if (box->itemText(i) == text)
47 box->insertItem(0, text);
51 GuiSearch::GuiSearch(GuiView & lv)
52 : GuiDialog(lv, "findreplace", qt_("Find and Replace"))
56 // fix height to minimum
57 setFixedHeight(sizeHint().height());
59 // align items in grid on top
60 mainGridLayout->setAlignment(Qt::AlignTop);
62 connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
63 this, SLOT(slotButtonBox(QAbstractButton *)));
64 connect(findPB, SIGNAL(clicked()), this, SLOT(findClicked()));
65 connect(replacePB, SIGNAL(clicked()), this, SLOT(replaceClicked()));
66 connect(replaceallPB, SIGNAL(clicked()), this, SLOT(replaceallClicked()));
67 connect(findCO, SIGNAL(editTextChanged(QString)),
68 this, SLOT(findChanged()));
70 setFocusProxy(findCO);
72 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
73 bc().setCancel(buttonBox->button(QDialogButtonBox::Close));
75 findCO->setCompleter(0);
76 replaceCO->setCompleter(0);
78 replacePB->setEnabled(false);
79 replaceallPB->setEnabled(false);
83 void GuiSearch::keyPressEvent(QKeyEvent * ev)
86 setKeySymbol(&sym, ev);
88 // we catch the key sequences for forward and backwards search
90 KeyModifier mod = lyx::q_key_state(ev->modifiers());
91 KeySequence keyseq(&theTopLevelKeymap(), &theTopLevelKeymap());
92 FuncRequest fr = keyseq.addkey(sym, mod);
93 if (fr == FuncRequest(LFUN_WORD_FIND_FORWARD) || fr == FuncRequest(LFUN_WORD_FIND)) {
97 if (fr == FuncRequest(LFUN_WORD_FIND_BACKWARD)) {
102 QDialog::keyPressEvent(ev);
106 void GuiSearch::showEvent(QShowEvent * e)
110 findCO->lineEdit()->selectAll();
111 GuiDialog::showEvent(e);
115 void GuiSearch::findChanged()
117 findPB->setEnabled(!findCO->currentText().isEmpty());
118 bool const replace = !findCO->currentText().isEmpty() && !isBufferReadonly();
119 replacePB->setEnabled(replace);
120 replaceallPB->setEnabled(replace);
121 replaceLA->setEnabled(replace);
122 replaceCO->setEnabled(replace);
126 void GuiSearch::findClicked(bool const backwards)
128 docstring const needle = qstring_to_ucs4(findCO->currentText());
129 find(needle, caseCB->isChecked(), wordsCB->isChecked(),
130 (!backwards && !backwardsCB->isChecked()));
131 uniqueInsert(findCO, findCO->currentText());
132 findCO->lineEdit()->selectAll();
136 void GuiSearch::replaceClicked()
138 docstring const needle = qstring_to_ucs4(findCO->currentText());
139 docstring const repl = qstring_to_ucs4(replaceCO->currentText());
140 replace(needle, repl, caseCB->isChecked(), wordsCB->isChecked(),
141 !backwardsCB->isChecked(), false);
142 uniqueInsert(findCO, findCO->currentText());
143 uniqueInsert(replaceCO, replaceCO->currentText());
147 void GuiSearch::replaceallClicked()
149 replace(qstring_to_ucs4(findCO->currentText()),
150 qstring_to_ucs4(replaceCO->currentText()),
151 caseCB->isChecked(), wordsCB->isChecked(), true, true);
152 uniqueInsert(findCO, findCO->currentText());
153 uniqueInsert(replaceCO, replaceCO->currentText());
157 void GuiSearch::find(docstring const & search, bool casesensitive,
158 bool matchword, bool forward)
160 docstring const sdata =
161 find2string(search, casesensitive, matchword, forward);
162 dispatch(FuncRequest(LFUN_WORD_FIND, sdata));
166 void GuiSearch::replace(docstring const & search, docstring const & replace,
167 bool casesensitive, bool matchword,
168 bool forward, bool all)
170 docstring const sdata =
171 replace2string(replace, search, casesensitive,
172 matchword, all, forward);
173 dispatch(FuncRequest(LFUN_WORD_REPLACE, sdata));
177 } // namespace frontend
181 #include "moc_GuiSearch.cpp"