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