]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FindAndReplace.cpp
If we are in a closeEvent, we don't want to close all buffers, because these may...
[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 {
104         Buffer & buffer = find_work_area_->bufferView().buffer();
105         docstring searchString;
106         if (!ignoreformat) {
107                 searchString = buffer_to_latex(buffer);
108         } else {
109                 ParIterator it = buffer.par_iterator_begin();
110                 ParIterator end = buffer.par_iterator_end();
111                 OutputParams runparams(&buffer.params().encoding());
112                 odocstringstream os;
113                 runparams.nice = true;
114                 runparams.flavor = OutputParams::LATEX;
115                 runparams.linelen = 100000; //lyxrc.plaintext_linelen;
116                 runparams.dryrun = true;
117                 for (; it != end; ++it) {
118                         LYXERR(Debug::FIND, "Adding to search string: '" << it->asString(false) << "'");
119                         searchString += it->stringify(pos_type(0), it->size(), AS_STR_INSETS, runparams);
120                 }
121         }
122         if (to_utf8(searchString).empty()) {
123                 buffer.message(_("Nothing to search"));
124                 return;
125         }
126         bool const regexp = to_utf8(searchString).find("\\regexp") != std::string::npos;
127         docstring replaceString;
128         if (replace) {
129                 Buffer & replace_buffer = replace_work_area_->bufferView().buffer();
130                 replaceString = buffer_to_latex(replace_buffer);
131         } else {
132                 replaceString = from_utf8(LYX_FR_NULL_STRING);
133         }
134         LYXERR(Debug::FIND, "FindAndReplaceOptions: "
135                << "searchstring=" << searchString
136                << ", casesensitiv=" << casesensitive
137                << ", matchword=" << matchword
138                << ", backwards=" << backwards
139                << ", expandmacros=" << expandmacros
140                << ", ignoreformat=" << ignoreformat
141                << ", regexp=" << regexp
142                << ", replaceString" << replaceString);
143         FindAndReplaceOptions opt(searchString, casesensitive, matchword, ! backwards,
144                 expandmacros, ignoreformat, regexp, replaceString);
145         LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
146         std::ostringstream oss;
147         oss << opt;
148         LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
149         dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
150
151         //      findAdv(&theApp()->currentView()->currentWorkArea()->bufferView(),
152         //                      searchString, len, casesensitive, matchword, ! backwards, expandmacros);
153 }
154
155
156 void FindAndReplaceWidget::findAndReplace(bool backwards, bool replace)
157 {
158         // FIXME: create a Dialog::returnFocus() or something instead of this:
159         view_.setCurrentWorkArea(view_.currentMainWorkArea());
160         // FIXME: This should be an LFUN.
161         findAndReplace(caseCB->isChecked(),
162                 wordsCB->isChecked(),
163                 backwards,
164                 expandMacrosCB->isChecked(),
165                 ignoreFormatCB->isChecked(),
166                 replace);
167         view_.currentMainWorkArea()->redraw();
168         find_work_area_->setFocus();
169 }
170
171
172 void FindAndReplaceWidget::on_regexpInsertCombo_currentIndexChanged(int index)
173 {
174         static char const * regexps[] = {
175                 ".*", ".+", "[a-z]+", "[0-9]+"
176         };
177         LYXERR(Debug::FIND, "Index: " << index);
178         if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
179                 find_work_area_->setFocus();
180                 Cursor & cur = find_work_area_->bufferView().cursor();
181                 if (! cur.inRegexped())
182                         dispatch(FuncRequest(LFUN_REGEXP_MODE));
183                 dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
184                 regexpInsertCombo->setCurrentIndex(0);
185         }
186 }
187
188
189 void FindAndReplaceWidget::on_closePB_clicked()
190 {
191         dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
192 }
193
194
195 void FindAndReplaceWidget::on_findNextPB_clicked() {
196         findAndReplace(false, false);
197 }
198
199
200 void FindAndReplaceWidget::on_findPrevPB_clicked() {
201         findAndReplace(true, false);
202 }
203
204
205 void FindAndReplaceWidget::on_replaceNextPB_clicked()
206 {
207         findAndReplace(false, true);
208 }
209
210
211 void FindAndReplaceWidget::on_replacePrevPB_clicked()
212 {
213         findAndReplace(true, true);
214 }
215
216
217 void FindAndReplaceWidget::on_replaceallPB_clicked()
218 {
219 }
220
221
222 void FindAndReplaceWidget::showEvent(QShowEvent * /* ev */)
223 {
224         replace_work_area_->setEnabled(true);
225         replace_work_area_->redraw();
226         find_work_area_->setFocus();
227         dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
228         dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
229         find_work_area_->redraw();
230         find_work_area_->installEventFilter(this);
231 }
232
233
234 void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
235 {
236         find_work_area_->removeEventFilter(this);
237         this->QWidget::hideEvent(ev);
238 }
239
240
241 bool FindAndReplaceWidget::initialiseParams(std::string const & /* params */)
242 {
243         find_work_area_->redraw();
244         replace_work_area_->setEnabled(true);
245         replace_work_area_->redraw();
246         find_work_area_->setFocus();
247         dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
248         dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
249         return true;
250 }
251
252
253 FindAndReplace::FindAndReplace(GuiView & parent,
254                 Qt::DockWidgetArea area, Qt::WindowFlags flags)
255         : DockView(parent, "Find LyX", qt_("Find LyX Dialog"), area, flags)
256 {
257         widget_ = new FindAndReplaceWidget(parent);
258         setWidget(widget_);
259         setFocusProxy(widget_);
260 }
261
262
263 FindAndReplace::~FindAndReplace()
264 {
265         setFocusProxy(0);
266         delete widget_;
267 }
268
269
270 bool FindAndReplace::initialiseParams(std::string const & params)
271 {
272         return widget_->initialiseParams(params);
273 }
274
275
276 Dialog * createGuiSearchAdv(GuiView & lv)
277 {
278         return new FindAndReplace(lv, Qt::RightDockWidgetArea);
279 }
280
281
282 } // namespace frontend
283 } // namespace lyx
284
285
286 #include "moc_FindAndReplace.cpp"