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