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