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