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