]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiSearch.cpp
Keep dialog connected to cross-ref inset after Apply.
[lyx.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
70         findCO->setCompleter(0);
71         replaceCO->setCompleter(0);
72
73         replacePB->setEnabled(false);
74         replaceallPB->setEnabled(false);
75 }
76
77
78 void GuiSearch::showEvent(QShowEvent * e)
79 {
80         findChanged();
81         findPB->setFocus();
82         findCO->lineEdit()->selectAll();
83         GuiDialog::showEvent(e);
84 }
85
86
87 void GuiSearch::findChanged()
88 {
89         findPB->setEnabled(!findCO->currentText().isEmpty());
90         bool const replace = !findCO->currentText().isEmpty() && !isBufferReadonly();
91         replacePB->setEnabled(replace);
92         replaceallPB->setEnabled(replace);
93         replaceLA->setEnabled(replace);
94         replaceCO->setEnabled(replace);
95 }
96
97
98 void GuiSearch::findClicked()
99 {
100         docstring const needle = qstring_to_ucs4(findCO->currentText());
101         find(needle, caseCB->isChecked(), wordsCB->isChecked(),
102                 !backwardsCB->isChecked());
103         uniqueInsert(findCO, findCO->currentText());
104         findCO->lineEdit()->selectAll();
105 }
106
107
108 void GuiSearch::replaceClicked()
109 {
110         docstring const needle = qstring_to_ucs4(findCO->currentText());
111         docstring const repl = qstring_to_ucs4(replaceCO->currentText());
112         replace(needle, repl, caseCB->isChecked(), wordsCB->isChecked(),
113                 !backwardsCB->isChecked(), false);
114         uniqueInsert(findCO, findCO->currentText());
115         uniqueInsert(replaceCO, replaceCO->currentText());
116 }
117
118
119 void GuiSearch::replaceallClicked()
120 {
121         replace(qstring_to_ucs4(findCO->currentText()),
122                 qstring_to_ucs4(replaceCO->currentText()),
123                 caseCB->isChecked(), wordsCB->isChecked(), true, true);
124         uniqueInsert(findCO, findCO->currentText());
125         uniqueInsert(replaceCO, replaceCO->currentText());
126 }
127
128
129 void GuiSearch::find(docstring const & search, bool casesensitive,
130                          bool matchword, bool forward)
131 {
132         docstring const sdata =
133                 find2string(search, casesensitive, matchword, forward);
134         dispatch(FuncRequest(LFUN_WORD_FIND, sdata));
135 }
136
137
138 void GuiSearch::replace(docstring const & search, docstring const & replace,
139                             bool casesensitive, bool matchword,
140                             bool forward, bool all)
141 {
142         docstring const sdata =
143                 replace2string(replace, search, casesensitive,
144                                      matchword, all, forward);
145         dispatch(FuncRequest(LFUN_WORD_REPLACE, sdata));
146 }
147
148
149 } // namespace frontend
150 } // namespace lyx
151
152
153 #include "moc_GuiSearch.cpp"