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));
70 findCO->setCompleter(0);
71 replaceCO->setCompleter(0);
73 replacePB->setEnabled(false);
74 replaceallPB->setEnabled(false);
78 void GuiSearch::showEvent(QShowEvent * e)
82 findCO->lineEdit()->selectAll();
83 GuiDialog::showEvent(e);
87 void GuiSearch::findChanged()
89 bool const replace = !findCO->currentText().isEmpty() && !isBufferReadonly();
90 findPB->setEnabled(replace);
91 replacePB->setEnabled(replace);
92 replaceallPB->setEnabled(replace);
93 replaceLA->setEnabled(replace);
94 replaceCO->setEnabled(replace);
98 void GuiSearch::findClicked()
100 docstring const needle = qstring_to_ucs4(findCO->currentText());
101 find(needle, caseCB->isChecked(), wordsCB->isChecked(),
102 !backwardsCB->isChecked());
103 uniqueInsert(findCO, findCO->currentText());
104 findCO->lineEdit()->selectAll();
108 void GuiSearch::replaceClicked()
110 docstring const needle = qstring_to_ucs4(findCO->currentText());
111 docstring const repl = qstring_to_ucs4(replaceCO->currentText());
112 replace(needle, repl, caseCB->isChecked(), wordsCB->isChecked(),
113 !backwardsCB->isChecked(), false);
114 uniqueInsert(findCO, findCO->currentText());
115 uniqueInsert(replaceCO, replaceCO->currentText());
119 void GuiSearch::replaceallClicked()
121 replace(qstring_to_ucs4(findCO->currentText()),
122 qstring_to_ucs4(replaceCO->currentText()),
123 caseCB->isChecked(), wordsCB->isChecked(), true, true);
124 uniqueInsert(findCO, findCO->currentText());
125 uniqueInsert(replaceCO, replaceCO->currentText());
129 void GuiSearch::find(docstring const & search, bool casesensitive,
130 bool matchword, bool forward)
132 docstring const sdata =
133 find2string(search, casesensitive, matchword, forward);
134 dispatch(FuncRequest(LFUN_WORD_FIND, sdata));
138 void GuiSearch::replace(docstring const & search, docstring const & replace,
139 bool casesensitive, bool matchword,
140 bool forward, bool all)
142 docstring const sdata =
143 replace2string(replace, search, casesensitive,
144 matchword, all, forward);
145 dispatch(FuncRequest(LFUN_WORD_REPLACE, sdata));
149 Dialog * createGuiSearch(GuiView & lv) { return new GuiSearch(lv); }
152 } // namespace frontend
156 #include "moc_GuiSearch.cpp"