]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSearch.cpp
next one
[lyx.git] / src / frontends / qt4 / GuiSearch.cpp
1 /**
2  * \file GuiSearch.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Edwin Leuven
8  * \author Angus Leeming
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiSearch.h"
16
17 #include "FuncRequest.h"
18 #include "lyxfind.h"
19
20 #include "qt_helpers.h"
21
22 #include <QLineEdit>
23 #include <QCloseEvent>
24
25 using std::string;
26
27
28 namespace lyx {
29 namespace frontend {
30
31 static void uniqueInsert(QComboBox * box, QString const & text)
32 {
33         for (int i = 0; i < box->count(); ++i) {
34                 if (box->itemText(i) == text)
35                         return;
36         }
37
38         box->addItem(text);
39 }
40
41
42 GuiSearch::GuiSearch(LyXView & lv)
43         : GuiDialog(lv, "findreplace"), Controller(this)
44 {
45         setupUi(this);
46         setController(this, false);
47         setViewTitle(_("Find and Replace"));
48
49         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
50         connect(findPB, SIGNAL(clicked()), this, SLOT(findClicked()));
51         connect(replacePB, SIGNAL(clicked()), this, SLOT(replaceClicked()));
52         connect(replaceallPB, SIGNAL(clicked()), this, SLOT(replaceallClicked()));
53         connect(findCO, SIGNAL(editTextChanged(const QString &)),
54                 this, SLOT(findChanged()));
55
56         setFocusProxy(findCO);
57
58         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
59         bc().setCancel(closePB);
60         bc().addReadOnly(replaceCO);
61         bc().addReadOnly(replacePB);
62         bc().addReadOnly(replaceallPB);
63
64         replacePB->setEnabled(false);
65         replaceallPB->setEnabled(false);
66 }
67
68
69 void GuiSearch::showView()
70 {
71         findCO->lineEdit()->setSelection(0, findCO->lineEdit()->text().length());
72         GuiDialog::showView();
73 }
74
75
76 void GuiSearch::closeEvent(QCloseEvent * e)
77 {
78         slotClose();
79         GuiDialog::closeEvent(e);
80 }
81
82
83 void GuiSearch::findChanged()
84 {
85         if (findCO->currentText().isEmpty()) {
86                 findPB->setEnabled(false);
87                 replacePB->setEnabled(false);
88                 replaceallPB->setEnabled(false);
89         } else {
90                 findPB->setEnabled(true);
91                 replacePB->setEnabled(!controller().isBufferReadonly());
92                 replaceallPB->setEnabled(!controller().isBufferReadonly());
93         }
94 }
95
96
97 void GuiSearch::findClicked()
98 {
99         docstring const needle = qstring_to_ucs4(findCO->currentText());
100         find(needle, caseCB->isChecked(), wordsCB->isChecked(),
101                 !backwardsCB->isChecked());
102         uniqueInsert(findCO, findCO->currentText());
103         findCO->lineEdit()->setSelection(0, findCO->lineEdit()->text().length());
104 }
105
106
107 void GuiSearch::replaceClicked()
108 {
109         docstring const needle = qstring_to_ucs4(findCO->currentText());
110         docstring const repl = qstring_to_ucs4(replaceCO->currentText());
111         replace(needle, repl, caseCB->isChecked(), wordsCB->isChecked(),
112                 !backwardsCB->isChecked(), false);
113         uniqueInsert(findCO, findCO->currentText());
114         uniqueInsert(replaceCO, replaceCO->currentText());
115 }
116
117
118 void GuiSearch::replaceallClicked()
119 {
120         replace(qstring_to_ucs4(findCO->currentText()),
121                 qstring_to_ucs4(replaceCO->currentText()),
122                 caseCB->isChecked(), wordsCB->isChecked(), true, true);
123         uniqueInsert(findCO, findCO->currentText());
124         uniqueInsert(replaceCO, replaceCO->currentText());
125 }
126
127
128 void GuiSearch::find(docstring const & search, bool casesensitive,
129                          bool matchword, bool forward)
130 {
131         docstring const data = find2string(search, casesensitive,
132                                               matchword, forward);
133         dispatch(FuncRequest(LFUN_WORD_FIND, data));
134 }
135
136
137 void GuiSearch::replace(docstring const & search, docstring const & replace,
138                             bool casesensitive, bool matchword,
139                             bool forward, bool all)
140 {
141         docstring const data =
142                 replace2string(search, replace, casesensitive,
143                                      matchword, all, forward);
144         dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
145 }
146
147 Dialog * createGuiSearch(LyXView & lv) { return new GuiSearch(lv); }
148
149
150 } // namespace frontend
151 } // namespace lyx
152
153
154 #include "GuiSearch_moc.cpp"