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 findCO->setCompleter(0);
73 replaceCO->setCompleter(0);
75 replacePB->setEnabled(false);
76 replaceallPB->setEnabled(false);
80 void GuiSearch::showEvent(QShowEvent * e)
83 findCO->lineEdit()->selectAll();
84 GuiDialog::showEvent(e);
88 void GuiSearch::findChanged()
90 if (findCO->currentText().isEmpty()) {
91 findPB->setEnabled(false);
92 replacePB->setEnabled(false);
93 replaceallPB->setEnabled(false);
95 findPB->setEnabled(true);
96 replacePB->setEnabled(!isBufferReadonly());
97 replaceallPB->setEnabled(!isBufferReadonly());
102 void GuiSearch::findClicked()
104 docstring const needle = qstring_to_ucs4(findCO->currentText());
105 find(needle, caseCB->isChecked(), wordsCB->isChecked(),
106 !backwardsCB->isChecked());
107 uniqueInsert(findCO, findCO->currentText());
108 findCO->lineEdit()->selectAll();
112 void GuiSearch::replaceClicked()
114 docstring const needle = qstring_to_ucs4(findCO->currentText());
115 docstring const repl = qstring_to_ucs4(replaceCO->currentText());
116 replace(needle, repl, caseCB->isChecked(), wordsCB->isChecked(),
117 !backwardsCB->isChecked(), false);
118 uniqueInsert(findCO, findCO->currentText());
119 uniqueInsert(replaceCO, replaceCO->currentText());
123 void GuiSearch::replaceallClicked()
125 replace(qstring_to_ucs4(findCO->currentText()),
126 qstring_to_ucs4(replaceCO->currentText()),
127 caseCB->isChecked(), wordsCB->isChecked(), true, true);
128 uniqueInsert(findCO, findCO->currentText());
129 uniqueInsert(replaceCO, replaceCO->currentText());
133 void GuiSearch::find(docstring const & search, bool casesensitive,
134 bool matchword, bool forward)
136 docstring const sdata =
137 find2string(search, casesensitive, matchword, forward);
138 dispatch(FuncRequest(LFUN_WORD_FIND, sdata));
142 void GuiSearch::replace(docstring const & search, docstring const & replace,
143 bool casesensitive, bool matchword,
144 bool forward, bool all)
146 docstring const sdata =
147 replace2string(replace, search, casesensitive,
148 matchword, all, forward);
149 dispatch(FuncRequest(LFUN_WORD_REPLACE, sdata));
153 Dialog * createGuiSearch(GuiView & lv) { return new GuiSearch(lv); }
156 } // namespace frontend
160 #include "moc_GuiSearch.cpp"