]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FindAndReplace.cpp
05600feb961e06065b489166b1a6403180882e3d
[lyx.git] / src / frontends / qt4 / FindAndReplace.cpp
1 /**
2  * \file FindAndReplace.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Tommaso Cucinotta
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "FindAndReplace.h"
14
15 #include "GuiApplication.h"
16 #include "qt_helpers.h"
17 #include "GuiView.h"
18 #include "GuiWorkArea.h"
19
20 #include "buffer_funcs.h"
21 #include "BufferParams.h"
22 #include "Cursor.h"
23 #include "FuncRequest.h"
24 #include "lyxfind.h"
25 #include "OutputParams.h"
26 #include "output_latex.h"
27 #include "TexRow.h"
28
29 #include "support/debug.h"
30 #include "support/FileName.h"
31 #include "support/gettext.h"
32 #include "support/lassert.h"
33
34 #include <QCloseEvent>
35 #include <QLineEdit>
36
37 #include <iostream>
38
39 using namespace std;
40 using namespace lyx::support;
41
42 namespace lyx {
43 namespace frontend {
44
45
46 FindAndReplaceWidget::FindAndReplaceWidget(GuiView & view)
47         :       view_(view)
48 {
49         setupUi(this);
50         find_work_area_->setGuiView(view_);
51         find_work_area_->init();
52         setFocusProxy(find_work_area_);
53         replace_work_area_->setGuiView(view_);
54         replace_work_area_->init();
55         // We don't want two cursors blinking.
56         replace_work_area_->stopBlinkingCursor();
57 }
58
59
60 bool FindAndReplaceWidget::eventFilter(QObject *obj, QEvent *event)
61 {
62         LYXERR(Debug::FIND, "FindAndReplace::eventFilter()");
63         if (obj == find_work_area_ && event->type() == QEvent::KeyPress) {
64                 QKeyEvent *e = static_cast<QKeyEvent *> (event);
65                 if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
66                         on_closePB_clicked();
67                         return true;
68                 } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
69                         if (e->modifiers() == Qt::ShiftModifier) {
70                                 on_findPrevPB_clicked();
71                                 return true;
72                         } else if (e->modifiers() == Qt::NoModifier) {
73                                 on_findNextPB_clicked();
74                                 return true;
75                         }
76                 }
77         }
78         // standard event processing
79         return QWidget::eventFilter(obj, event);
80 }
81
82 static docstring buffer_to_latex(Buffer & buffer) {
83         OutputParams runparams(&buffer.params().encoding());
84         odocstringstream os;
85         runparams.nice = true;
86         runparams.flavor = OutputParams::LATEX;
87         runparams.linelen = 80; //lyxrc.plaintext_linelen;
88         // No side effect of file copying and image conversion
89         runparams.dryrun = true;
90         buffer.texrow().reset();
91         ParagraphList::const_iterator pit = buffer.paragraphs().begin();
92         ParagraphList::const_iterator const end = buffer.paragraphs().end();
93         for (; pit != end; ++pit) {
94                 TeXOnePar(buffer, buffer.text(), pit, os, buffer.texrow(), runparams);
95                 LYXERR(Debug::FIND, "searchString up to here: " << os.str());
96         }
97         return os.str();
98 }
99
100 void FindAndReplaceWidget::findAndReplace(
101         bool casesensitive, bool matchword, bool backwards,
102         bool expandmacros, bool ignoreformat, bool replace,
103         bool keep_case)
104 {
105         Buffer & buffer = find_work_area_->bufferView().buffer();
106         docstring searchString;
107         if (!ignoreformat) {
108                 searchString = buffer_to_latex(buffer);
109         } else {
110                 ParIterator it = buffer.par_iterator_begin();
111                 ParIterator end = buffer.par_iterator_end();
112                 OutputParams runparams(&buffer.params().encoding());
113                 odocstringstream os;
114                 runparams.nice = true;
115                 runparams.flavor = OutputParams::LATEX;
116                 runparams.linelen = 100000; //lyxrc.plaintext_linelen;
117                 runparams.dryrun = true;
118                 for (; it != end; ++it) {
119                         LYXERR(Debug::FIND, "Adding to search string: '" << it->asString(false) << "'");
120                         searchString += it->stringify(pos_type(0), it->size(), AS_STR_INSETS, runparams);
121                 }
122         }
123         if (to_utf8(searchString).empty()) {
124                 buffer.message(_("Nothing to search"));
125                 return;
126         }
127         bool const regexp = to_utf8(searchString).find("\\regexp") != std::string::npos;
128         docstring replaceString;
129         if (replace) {
130                 Buffer & repl_buffer = replace_work_area_->bufferView().buffer();
131                 ostringstream oss;
132                 repl_buffer.write(oss);
133                 replaceString = from_utf8(oss.str()); //buffer_to_latex(replace_buffer);
134         } else {
135                 replaceString = from_utf8(LYX_FR_NULL_STRING);
136         }
137         LYXERR(Debug::FIND, "FindAndReplaceOptions: "
138                << "searchstring=" << searchString
139                << ", casesensitiv=" << casesensitive
140                << ", matchword=" << matchword
141                << ", backwards=" << backwards
142                << ", expandmacros=" << expandmacros
143                << ", ignoreformat=" << ignoreformat
144                << ", regexp=" << regexp
145                << ", replaceString" << replaceString
146                << ", keep_case=" << keep_case);
147         FindAndReplaceOptions opt(searchString, casesensitive, matchword, ! backwards,
148                   expandmacros, ignoreformat, regexp, replaceString, keep_case);
149         LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
150         std::ostringstream oss;
151         oss << opt;
152         LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
153         dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
154 }
155
156
157 void FindAndReplaceWidget::findAndReplace(bool backwards, bool replace)
158 {
159         // FIXME: create a Dialog::returnFocus() or something instead of this:
160         view_.setCurrentWorkArea(view_.currentMainWorkArea());
161         // FIXME: This should be an LFUN.
162         findAndReplace(caseCB->isChecked(),
163                 wordsCB->isChecked(),
164                 backwards,
165                 expandMacrosCB->isChecked(),
166                 ignoreFormatCB->isChecked(),
167                 replace,
168                 keepCaseCB->isChecked());
169         view_.currentMainWorkArea()->redraw();
170         find_work_area_->setFocus();
171 }
172
173
174 void FindAndReplaceWidget::on_regexpInsertCombo_currentIndexChanged(int index)
175 {
176         static char const * regexps[] = {
177                 ".*", ".+", "[a-z]+", "[0-9]+"
178         };
179         LYXERR(Debug::FIND, "Index: " << index);
180         if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
181                 find_work_area_->setFocus();
182                 Cursor & cur = find_work_area_->bufferView().cursor();
183                 if (! cur.inRegexped())
184                         dispatch(FuncRequest(LFUN_REGEXP_MODE));
185                 dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
186                 regexpInsertCombo->setCurrentIndex(0);
187         }
188 }
189
190
191 void FindAndReplaceWidget::on_closePB_clicked()
192 {
193         dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
194 }
195
196
197 void FindAndReplaceWidget::on_findNextPB_clicked() {
198         findAndReplace(false, false);
199 }
200
201
202 void FindAndReplaceWidget::on_findPrevPB_clicked() {
203         findAndReplace(true, false);
204 }
205
206
207 void FindAndReplaceWidget::on_replaceNextPB_clicked()
208 {
209         findAndReplace(false, true);
210 }
211
212
213 void FindAndReplaceWidget::on_replacePrevPB_clicked()
214 {
215         findAndReplace(true, true);
216 }
217
218
219 void FindAndReplaceWidget::on_replaceallPB_clicked()
220 {
221 }
222
223
224 void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
225 {
226         replace_work_area_->setEnabled(true);
227         replace_work_area_->redraw();
228         find_work_area_->setFocus();
229         dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
230         dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
231         find_work_area_->redraw();
232         find_work_area_->installEventFilter(this);
233 }
234
235
236 void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
237 {
238         find_work_area_->removeEventFilter(this);
239         this->QWidget::hideEvent(ev);
240 }
241
242
243 bool FindAndReplaceWidget::initialiseParams(std::string const & /* params */)
244 {
245         find_work_area_->redraw();
246         replace_work_area_->setEnabled(true);
247         replace_work_area_->redraw();
248         find_work_area_->setFocus();
249         dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
250         dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
251         return true;
252 }
253
254
255 FindAndReplace::FindAndReplace(GuiView & parent,
256                 Qt::DockWidgetArea area, Qt::WindowFlags flags)
257         : DockView(parent, "Find LyX", qt_("Find LyX Dialog"), area, flags)
258 {
259         widget_ = new FindAndReplaceWidget(parent);
260         setWidget(widget_);
261         setFocusProxy(widget_);
262 }
263
264
265 FindAndReplace::~FindAndReplace()
266 {
267         setFocusProxy(0);
268         delete widget_;
269 }
270
271
272 bool FindAndReplace::initialiseParams(std::string const & params)
273 {
274         return widget_->initialiseParams(params);
275 }
276
277
278 Dialog * createGuiSearchAdv(GuiView & lv)
279 {
280         return new FindAndReplace(lv, Qt::RightDockWidgetArea);
281 }
282
283
284 } // namespace frontend
285 } // namespace lyx
286
287
288 #include "moc_FindAndReplace.cpp"