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 // align items in grid on top
54 mainGridLayout->setAlignment(Qt::AlignTop);
56 connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
57 this, SLOT(slotButtonBox(QAbstractButton *)));
58 connect(findPB, SIGNAL(clicked()), this, SLOT(findClicked()));
59 connect(replacePB, SIGNAL(clicked()), this, SLOT(replaceClicked()));
60 connect(replaceallPB, SIGNAL(clicked()), this, SLOT(replaceallClicked()));
61 connect(findCO, SIGNAL(editTextChanged(QString)),
62 this, SLOT(findChanged()));
64 setFocusProxy(findCO);
66 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
67 bc().setCancel(buttonBox->button(QDialogButtonBox::Close));
68 bc().addReadOnly(replaceCO);
69 bc().addReadOnly(replacePB);
70 bc().addReadOnly(replaceallPB);
72 replacePB->setEnabled(false);
73 replaceallPB->setEnabled(false);
77 void GuiSearch::showEvent(QShowEvent * e)
80 findCO->lineEdit()->selectAll();
81 GuiDialog::showEvent(e);
85 void GuiSearch::findChanged()
87 if (findCO->currentText().isEmpty()) {
88 findPB->setEnabled(false);
89 replacePB->setEnabled(false);
90 replaceallPB->setEnabled(false);
92 findPB->setEnabled(true);
93 replacePB->setEnabled(!isBufferReadonly());
94 replaceallPB->setEnabled(!isBufferReadonly());
99 void GuiSearch::findClicked()
101 docstring const needle = qstring_to_ucs4(findCO->currentText());
102 find(needle, caseCB->isChecked(), wordsCB->isChecked(),
103 !backwardsCB->isChecked());
104 uniqueInsert(findCO, findCO->currentText());
105 findCO->lineEdit()->selectAll();
109 void GuiSearch::replaceClicked()
111 docstring const needle = qstring_to_ucs4(findCO->currentText());
112 docstring const repl = qstring_to_ucs4(replaceCO->currentText());
113 replace(needle, repl, caseCB->isChecked(), wordsCB->isChecked(),
114 !backwardsCB->isChecked(), false);
115 uniqueInsert(findCO, findCO->currentText());
116 uniqueInsert(replaceCO, replaceCO->currentText());
120 void GuiSearch::replaceallClicked()
122 replace(qstring_to_ucs4(findCO->currentText()),
123 qstring_to_ucs4(replaceCO->currentText()),
124 caseCB->isChecked(), wordsCB->isChecked(), true, true);
125 uniqueInsert(findCO, findCO->currentText());
126 uniqueInsert(replaceCO, replaceCO->currentText());
130 void GuiSearch::find(docstring const & search, bool casesensitive,
131 bool matchword, bool forward)
133 docstring const sdata =
134 find2string(search, casesensitive, matchword, forward);
135 dispatch(FuncRequest(LFUN_WORD_FIND, sdata));
139 void GuiSearch::replace(docstring const & search, docstring const & replace,
140 bool casesensitive, bool matchword,
141 bool forward, bool all)
143 docstring const sdata =
144 replace2string(replace, search, casesensitive,
145 matchword, all, forward);
146 dispatch(FuncRequest(LFUN_WORD_REPLACE, sdata));
150 Dialog * createGuiSearch(GuiView & lv) { return new GuiSearch(lv); }
153 } // namespace frontend
157 #include "moc_GuiSearch.cpp"