]> git.lyx.org Git - features.git/blob - src/frontends/qt/GuiSearch.cpp
#10571 improved handling of WM's signal when switching from or to full-screen window
[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 "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         findCO->setCompleter(0);
73         replaceCO->setCompleter(0);
74
75         replacePB->setEnabled(false);
76         replaceallPB->setEnabled(false);
77 }
78
79
80 void GuiSearch::showEvent(QShowEvent * e)
81 {
82         findPB->setFocus();
83         findCO->lineEdit()->selectAll();
84         GuiDialog::showEvent(e);
85 }
86
87
88 void GuiSearch::findChanged()
89 {
90         if (findCO->currentText().isEmpty()) {
91                 findPB->setEnabled(false);
92                 replacePB->setEnabled(false);
93                 replaceallPB->setEnabled(false);
94         } else {
95                 findPB->setEnabled(true);
96                 replacePB->setEnabled(!isBufferReadonly());
97                 replaceallPB->setEnabled(!isBufferReadonly());
98         }
99 }
100
101
102 void GuiSearch::findClicked()
103 {
104         docstring const needle = qstring_to_ucs4(findCO->currentText());
105         find(needle, caseCB->isChecked(), wordsCB->isChecked(),
106                 !backwardsCB->isChecked());
107         uniqueInsert(findCO, findCO->currentText());
108         findCO->lineEdit()->selectAll();
109 }
110
111
112 void GuiSearch::replaceClicked()
113 {
114         docstring const needle = qstring_to_ucs4(findCO->currentText());
115         docstring const repl = qstring_to_ucs4(replaceCO->currentText());
116         replace(needle, repl, caseCB->isChecked(), wordsCB->isChecked(),
117                 !backwardsCB->isChecked(), false);
118         uniqueInsert(findCO, findCO->currentText());
119         uniqueInsert(replaceCO, replaceCO->currentText());
120 }
121
122
123 void GuiSearch::replaceallClicked()
124 {
125         replace(qstring_to_ucs4(findCO->currentText()),
126                 qstring_to_ucs4(replaceCO->currentText()),
127                 caseCB->isChecked(), wordsCB->isChecked(), true, true);
128         uniqueInsert(findCO, findCO->currentText());
129         uniqueInsert(replaceCO, replaceCO->currentText());
130 }
131
132
133 void GuiSearch::find(docstring const & search, bool casesensitive,
134                          bool matchword, bool forward)
135 {
136         docstring const sdata =
137                 find2string(search, casesensitive, matchword, forward);
138         dispatch(FuncRequest(LFUN_WORD_FIND, sdata));
139 }
140
141
142 void GuiSearch::replace(docstring const & search, docstring const & replace,
143                             bool casesensitive, bool matchword,
144                             bool forward, bool all)
145 {
146         docstring const sdata =
147                 replace2string(replace, search, casesensitive,
148                                      matchword, all, forward);
149         dispatch(FuncRequest(LFUN_WORD_REPLACE, sdata));
150 }
151
152
153 Dialog * createGuiSearch(GuiView & lv) { return new GuiSearch(lv); }
154
155
156 } // namespace frontend
157 } // namespace lyx
158
159
160 #include "moc_GuiSearch.cpp"