]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSearch.cpp
move Controller inheritance further up the tree
[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")
44 {
45         setupUi(this);
46         setViewTitle(_("Find and Replace"));
47
48         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
49         connect(findPB, SIGNAL(clicked()), this, SLOT(findClicked()));
50         connect(replacePB, SIGNAL(clicked()), this, SLOT(replaceClicked()));
51         connect(replaceallPB, SIGNAL(clicked()), this, SLOT(replaceallClicked()));
52         connect(findCO, SIGNAL(editTextChanged(QString)),
53                 this, SLOT(findChanged()));
54
55         setFocusProxy(findCO);
56
57         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
58         bc().setCancel(closePB);
59         bc().addReadOnly(replaceCO);
60         bc().addReadOnly(replacePB);
61         bc().addReadOnly(replaceallPB);
62
63         replacePB->setEnabled(false);
64         replaceallPB->setEnabled(false);
65 }
66
67
68 void GuiSearch::showView()
69 {
70         findCO->lineEdit()->setSelection(0, findCO->lineEdit()->text().length());
71         GuiDialog::showView();
72 }
73
74
75 void GuiSearch::closeEvent(QCloseEvent * e)
76 {
77         slotClose();
78         GuiDialog::closeEvent(e);
79 }
80
81
82 void GuiSearch::findChanged()
83 {
84         if (findCO->currentText().isEmpty()) {
85                 findPB->setEnabled(false);
86                 replacePB->setEnabled(false);
87                 replaceallPB->setEnabled(false);
88         } else {
89                 findPB->setEnabled(true);
90                 replacePB->setEnabled(!controller().isBufferReadonly());
91                 replaceallPB->setEnabled(!controller().isBufferReadonly());
92         }
93 }
94
95
96 void GuiSearch::findClicked()
97 {
98         docstring const needle = qstring_to_ucs4(findCO->currentText());
99         find(needle, caseCB->isChecked(), wordsCB->isChecked(),
100                 !backwardsCB->isChecked());
101         uniqueInsert(findCO, findCO->currentText());
102         findCO->lineEdit()->setSelection(0, findCO->lineEdit()->text().length());
103 }
104
105
106 void GuiSearch::replaceClicked()
107 {
108         docstring const needle = qstring_to_ucs4(findCO->currentText());
109         docstring const repl = qstring_to_ucs4(replaceCO->currentText());
110         replace(needle, repl, caseCB->isChecked(), wordsCB->isChecked(),
111                 !backwardsCB->isChecked(), false);
112         uniqueInsert(findCO, findCO->currentText());
113         uniqueInsert(replaceCO, replaceCO->currentText());
114 }
115
116
117 void GuiSearch::replaceallClicked()
118 {
119         replace(qstring_to_ucs4(findCO->currentText()),
120                 qstring_to_ucs4(replaceCO->currentText()),
121                 caseCB->isChecked(), wordsCB->isChecked(), true, true);
122         uniqueInsert(findCO, findCO->currentText());
123         uniqueInsert(replaceCO, replaceCO->currentText());
124 }
125
126
127 void GuiSearch::find(docstring const & search, bool casesensitive,
128                          bool matchword, bool forward)
129 {
130         docstring const data = find2string(search, casesensitive,
131                                               matchword, forward);
132         dispatch(FuncRequest(LFUN_WORD_FIND, data));
133 }
134
135
136 void GuiSearch::replace(docstring const & search, docstring const & replace,
137                             bool casesensitive, bool matchword,
138                             bool forward, bool all)
139 {
140         docstring const data =
141                 replace2string(search, replace, casesensitive,
142                                      matchword, all, forward);
143         dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
144 }
145
146 Dialog * createGuiSearch(LyXView & lv) { return new GuiSearch(lv); }
147
148
149 } // namespace frontend
150 } // namespace lyx
151
152
153 #include "GuiSearch_moc.cpp"