]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FindAndReplace.cpp
9f32ad9bf8b316f7a8082a7f06e8dee8832065e8
[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/convert.h"
30 #include "support/debug.h"
31 #include "support/FileName.h"
32 #include "support/gettext.h"
33 #include "support/lassert.h"
34
35 #include <QCloseEvent>
36 #include <QLineEdit>
37
38 #include <iostream>
39
40 using namespace std;
41 using namespace lyx::support;
42
43 namespace lyx {
44 namespace frontend {
45
46
47 FindAndReplaceWidget::FindAndReplaceWidget(GuiView & view)
48         :       view_(view)
49 {
50         setupUi(this);
51         find_work_area_->setGuiView(view_);
52         find_work_area_->init();
53         setFocusProxy(find_work_area_);
54         replace_work_area_->setGuiView(view_);
55         replace_work_area_->init();
56         // We don't want two cursors blinking.
57         replace_work_area_->stopBlinkingCursor();
58 }
59
60
61 bool FindAndReplaceWidget::eventFilter(QObject *obj, QEvent *event)
62 {
63         LYXERR(Debug::DEBUG, "FindAndReplace::eventFilter()" << std::endl);
64         if (obj == find_work_area_ && event->type() == QEvent::KeyPress) {
65                 QKeyEvent *e = static_cast<QKeyEvent *> (event);
66                 if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
67                         on_closePB_clicked();
68                         return true;
69                 } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
70                         if (e->modifiers() == Qt::ShiftModifier) {
71                                 on_findPrevPB_clicked();
72                                 return true;
73                         } else if (e->modifiers() == Qt::NoModifier) {
74                                 on_findNextPB_clicked();
75                                 return true;
76                         }
77                 }
78         }
79         // standard event processing
80         return QWidget::eventFilter(obj, event);
81 }
82
83
84 void FindAndReplaceWidget::findAdv(bool casesensitive,
85                 bool matchword, bool backwards,
86                 bool expandmacros, bool ignoreformat)
87 {
88         Buffer & buffer = find_work_area_->bufferView().buffer();
89         docstring searchString;
90         if (!ignoreformat) {
91                 OutputParams runparams(&buffer.params().encoding());
92                 odocstringstream os;
93                 runparams.nice = true;
94                 runparams.flavor = OutputParams::LATEX;
95                 runparams.linelen = 80; //lyxrc.plaintext_linelen;
96                 // No side effect of file copying and image conversion
97                 runparams.dryrun = true;
98                 buffer.texrow().reset();
99 //              latexParagraphs(buffer, buffer.paragraphs(), os, buffer.texrow(), runparams);
100                 ParagraphList::const_iterator pit = buffer.paragraphs().begin();
101                 ParagraphList::const_iterator const end = buffer.paragraphs().end();
102                 for (; pit != end; ++pit) {
103                         TeXOnePar(buffer, buffer.text(), pit, os, buffer.texrow(), runparams);
104                         LYXERR0("searchString up to here: " << os.str());
105                 }
106                 searchString = os.str();
107         } else {
108                 ParIterator it = buffer.par_iterator_begin();
109                 ParIterator end = buffer.par_iterator_end();
110                 OutputParams runparams(&buffer.params().encoding());
111                 odocstringstream os;
112                 runparams.nice = true;
113                 runparams.flavor = OutputParams::LATEX;
114                 runparams.linelen = 100000; //lyxrc.plaintext_linelen;
115                 runparams.dryrun = true;
116                 for (; it != end; ++it) {
117                         LYXERR(Debug::DEBUG, "Adding to search string: '" << it->asString(false) << "'");
118                         searchString += it->stringify(pos_type(0), it->size(), AS_STR_INSETS, runparams);
119                 }
120         }
121 //      lyxerr << "Searching for '" << to_utf8(searchString) << "'" << std::endl;
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         FindAdvOptions opt(searchString, casesensitive, matchword, ! backwards,
128                 expandmacros, ignoreformat, regexp);
129         LYXERR(Debug::DEBUG, "Dispatching LFUN_WORD_FINDADV" << std::endl);
130         std::ostringstream oss;
131         oss << opt;
132         LYXERR(Debug::DEBUG, "Dispatching LFUN_WORD_FINDADV" << std::endl);
133         dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
134
135         //      findAdv(&theApp()->currentView()->currentWorkArea()->bufferView(),
136         //                      searchString, len, casesensitive, matchword, ! backwards, expandmacros);
137 }
138
139
140 void FindAndReplaceWidget::find(bool backwards)
141 {
142         // FIXME: create a Dialog::returnFocus() or something instead of this:
143         view_.setCurrentWorkArea(view_.currentMainWorkArea());
144         // FIXME: This should be an LFUN.
145         findAdv(caseCB->isChecked(),
146                         wordsCB->isChecked(),
147                         backwards,
148                         expandMacrosCB->isChecked(),
149                         ignoreFormatCB->isChecked());
150         view_.currentMainWorkArea()->redraw();
151         find_work_area_->setFocus();
152 }
153
154
155 void FindAndReplaceWidget::on_regexpInsertCombo_currentIndexChanged(int index)
156 {
157         static char const * regexps[] = {
158                 ".*", ".+", "[a-z]+", "[0-9]+"
159         };
160         //lyxerr << "Index: " << index << std::endl;
161         if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
162                 find_work_area_->setFocus();
163                 Cursor & cur = find_work_area_->bufferView().cursor();
164                 if (! cur.inRegexped())
165                         dispatch(FuncRequest(LFUN_REGEXP_MODE));
166                 dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
167                 regexpInsertCombo->setCurrentIndex(0);
168         }
169 }
170
171
172 void FindAndReplaceWidget::on_closePB_clicked()
173 {
174         dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
175 }
176
177
178 void FindAndReplaceWidget::on_findNextPB_clicked() {
179         find(false);
180 }
181
182
183 void FindAndReplaceWidget::on_findPrevPB_clicked() {
184         find(true);
185 }
186
187
188 void FindAndReplaceWidget::on_replacePB_clicked()
189 {
190 }
191
192
193 void FindAndReplaceWidget::on_replaceallPB_clicked()
194 {
195 }
196
197
198 void FindAndReplaceWidget::showEvent(QShowEvent *ev)
199 {
200         replace_work_area_->setEnabled(true);
201         replace_work_area_->redraw();
202         find_work_area_->setFocus();
203         dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
204         dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
205         find_work_area_->redraw();
206         find_work_area_->installEventFilter(this);
207 }
208
209
210 void FindAndReplaceWidget::hideEvent(QHideEvent *ev)
211 {
212         find_work_area_->removeEventFilter(this);
213         this->QWidget::hideEvent(ev);
214 }
215
216
217 bool FindAndReplaceWidget::initialiseParams(std::string const & params)
218 {
219         find_work_area_->redraw();
220         replace_work_area_->setEnabled(true);
221         replace_work_area_->redraw();
222         find_work_area_->setFocus();
223         dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
224         dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
225         return true;
226 }
227
228
229 FindAndReplace::FindAndReplace(GuiView & parent,
230                 Qt::DockWidgetArea area, Qt::WindowFlags flags)
231         : DockView(parent, "Find LyX", qt_("Find LyX Dialog"), area, flags)
232 {
233         widget_ = new FindAndReplaceWidget(parent);
234         setWidget(widget_);
235         setFocusProxy(widget_);
236 }
237
238
239 FindAndReplace::~FindAndReplace()
240 {
241         setFocusProxy(0);
242         delete widget_;
243 }
244
245
246 bool FindAndReplace::initialiseParams(std::string const & params)
247 {
248         return widget_->initialiseParams(params);
249 }
250
251
252 Dialog * createGuiSearchAdv(GuiView & lv)
253 {
254         return new FindAndReplace(lv, Qt::RightDockWidgetArea);
255 }
256
257
258 } // namespace frontend
259 } // namespace lyx
260
261
262 #include "moc_FindAndReplace.cpp"