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