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"
25 #include "support/gettext.h"
26 #include "frontends/alert.h"
36 static void uniqueInsert(QComboBox * box, QString const & text)
38 for (int i = box->count(); --i >= 0; )
39 if (box->itemText(i) == text)
42 box->insertItem(0, text);
46 GuiSearch::GuiSearch(GuiView & lv)
47 : GuiDialog(lv, "findreplace", qt_("Find and Replace"))
51 // fix height to minimum
52 setFixedHeight(sizeHint().height());
54 // align items in grid on top
55 mainGridLayout->setAlignment(Qt::AlignTop);
57 connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
58 this, SLOT(slotButtonBox(QAbstractButton *)));
59 connect(findPB, SIGNAL(clicked()), this, SLOT(findClicked()));
60 connect(replacePB, SIGNAL(clicked()), this, SLOT(replaceClicked()));
61 connect(replaceallPB, SIGNAL(clicked()), this, SLOT(replaceallClicked()));
62 connect(findCO, SIGNAL(editTextChanged(QString)),
63 this, SLOT(findChanged()));
65 setFocusProxy(findCO);
67 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
68 bc().setCancel(buttonBox->button(QDialogButtonBox::Close));
69 bc().addReadOnly(replaceCO);
70 bc().addReadOnly(replacePB);
71 bc().addReadOnly(replaceallPB);
73 findCO->setCompleter(0);
74 replaceCO->setCompleter(0);
76 replacePB->setEnabled(false);
77 replaceallPB->setEnabled(false);
81 void GuiSearch::showEvent(QShowEvent * e)
84 findCO->lineEdit()->selectAll();
85 GuiDialog::showEvent(e);
89 void GuiSearch::findChanged()
91 if (findCO->currentText().isEmpty()) {
92 findPB->setEnabled(false);
93 replacePB->setEnabled(false);
94 replaceallPB->setEnabled(false);
96 findPB->setEnabled(true);
97 replacePB->setEnabled(!isBufferReadonly());
98 replaceallPB->setEnabled(!isBufferReadonly());
103 void GuiSearch::findClicked()
105 docstring const needle = qstring_to_ucs4(findCO->currentText());
106 find(needle, caseCB->isChecked(), wordsCB->isChecked(),
107 !backwardsCB->isChecked());
108 uniqueInsert(findCO, findCO->currentText());
109 findCO->lineEdit()->selectAll();
113 void GuiSearch::replaceClicked()
115 docstring const needle = qstring_to_ucs4(findCO->currentText());
116 docstring const repl = qstring_to_ucs4(replaceCO->currentText());
117 replace(needle, repl, caseCB->isChecked(), wordsCB->isChecked(),
118 !backwardsCB->isChecked(), false);
119 uniqueInsert(findCO, findCO->currentText());
120 uniqueInsert(replaceCO, replaceCO->currentText());
124 void GuiSearch::replaceallClicked()
126 replace(qstring_to_ucs4(findCO->currentText()),
127 qstring_to_ucs4(replaceCO->currentText()),
128 caseCB->isChecked(), wordsCB->isChecked(), true, true);
129 uniqueInsert(findCO, findCO->currentText());
130 uniqueInsert(replaceCO, replaceCO->currentText());
134 void GuiSearch::find(docstring const & search, bool casesensitive,
135 bool matchword, bool forward)
137 docstring const sdata =
138 find2string(search, casesensitive, matchword, forward);
139 dispatch(FuncRequest(LFUN_WORD_FIND, sdata));
143 void GuiSearch::replace(docstring const & search, docstring const & replace,
144 bool casesensitive, bool matchword,
145 bool forward, bool all)
147 docstring const sdata =
148 replace2string(replace, search, casesensitive,
149 matchword, all, forward);
150 dispatch(FuncRequest(LFUN_WORD_REPLACE, sdata));
154 Dialog * createGuiSearch(GuiView & lv) { return new GuiSearch(lv); }
157 } // namespace frontend
161 #include "moc_GuiSearch.cpp"