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