]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FindAndReplace.cpp
2a0024a89ae1ca2788f957e805814fcf0596ee0b
[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 FindAndReplace::FindAndReplace(GuiView & parent)
47         : DockView(parent, "Find LyX", "Find LyX Dialog", Qt::RightDockWidgetArea),
48         parent_view_(parent)
49 {
50         setupUi(this);
51         find_work_area_->setGuiView(parent_view_);
52         find_work_area_->init();
53         setFocusProxy(find_work_area_);
54 }
55
56
57 bool FindAndReplace::eventFilter(QObject *obj, QEvent *event)
58 {
59         LYXERR(Debug::DEBUG, "FindAndReplace::eventFilter()" << std::endl);
60         if (obj == find_work_area_ && event->type() == QEvent::KeyPress) {
61                 QKeyEvent *e = static_cast<QKeyEvent *> (event);
62                 if (e->key() == Qt::Key_Escape && e->modifiers() == Qt::NoModifier) {
63                         on_closePB_clicked();
64                         return true;
65                 } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
66                         if (e->modifiers() == Qt::ShiftModifier) {
67                                 on_findPrevPB_clicked();
68                                 return true;
69                         } else if (e->modifiers() == Qt::NoModifier) {
70                                 on_findNextPB_clicked();
71                                 return true;
72                         }
73                 }
74         }
75         // standard event processing
76         return QObject::eventFilter(obj, event);
77 }
78
79
80 void FindAndReplace::selectAll()
81 {
82         dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
83         dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
84         find_work_area_->redraw();
85 }
86
87
88 void FindAndReplace::findAdv(bool casesensitive,
89                 bool matchword, bool backwards,
90                 bool expandmacros, bool ignoreformat)
91 {
92         Buffer & buffer = find_work_area_->bufferView().buffer();
93         docstring searchString;
94         if (!ignoreformat) {
95                 OutputParams runparams(&buffer.params().encoding());
96                 odocstringstream os;
97                 runparams.nice = true;
98                 runparams.flavor = OutputParams::LATEX;
99                 runparams.linelen = 80; //lyxrc.plaintext_linelen;
100                 // No side effect of file copying and image conversion
101                 runparams.dryrun = true;
102                 buffer.texrow().reset();
103 //              latexParagraphs(buffer, buffer.paragraphs(), os, buffer.texrow(), runparams);
104                 ParagraphList::const_iterator pit = buffer.paragraphs().begin();
105                 ParagraphList::const_iterator const end = buffer.paragraphs().end();
106                 for (; pit != end; ++pit) {
107                         TeXOnePar(buffer, buffer.text(), pit, os, buffer.texrow(), runparams);
108                         LYXERR0("searchString up to here: " << os.str());
109                 }
110                 searchString = os.str();
111         } else {
112                 ParIterator it = buffer.par_iterator_begin();
113                 ParIterator end = buffer.par_iterator_end();
114                 for (; it != end; ++it) {
115                         LYXERR0("Adding to search string: '" << it->asString(false) << "'");
116                         searchString += it->asString(AS_STR_INSETS);
117                 }
118         }
119 //      lyxerr << "Searching for '" << to_utf8(searchString) << "'" << std::endl;
120         if (to_utf8(searchString).empty()) {
121                 buffer.message(_("Nothing to search"));
122                 return;
123         }
124         bool const regexp = to_utf8(searchString).find("\\regexp") != std::string::npos;
125         FindAdvOptions opt(searchString, casesensitive, matchword, ! backwards,
126                 expandmacros, ignoreformat, regexp);
127         std::cerr << "Dispatching LFUN_WORD_FINDADV" << std::endl;
128         std::ostringstream oss;
129         oss << opt;
130         std::cerr << "Dispatching LFUN_WORD_FINDADV" << std::endl;
131         dispatch(FuncRequest(LFUN_WORD_FINDADV, from_utf8(oss.str())));
132
133         //      findAdv(&theApp()->currentView()->currentWorkArea()->bufferView(),
134         //                      searchString, len, casesensitive, matchword, ! backwards, expandmacros);
135 }
136
137 bool FindAndReplace::initialiseParams(std::string const &)
138 {
139         find_work_area_->setFocus();
140         selectAll();
141         return true;
142 }
143
144
145 void FindAndReplace::find(bool backwards)
146 {
147         parent_view_.setCurrentWorkArea(parent_view_.currentMainWorkArea());
148         findAdv(caseCB->isChecked(),
149                         wordsCB->isChecked(),
150                         backwards,
151                         expandMacrosCB->isChecked(),
152                         ignoreFormatCB->isChecked());
153         parent_view_.currentMainWorkArea()->redraw();
154         find_work_area_->setFocus();
155 }
156
157
158 void FindAndReplace::on_regexpInsertCombo_currentIndexChanged(int index)
159 {
160         static char const * regexps[] = {
161                 ".*", ".+", "[a-z]+", "[0-9]+"
162         };
163         //lyxerr << "Index: " << index << std::endl;
164         if (index >= 1 && index < 1 + int(sizeof(regexps)/sizeof(regexps[0]))) {
165                 find_work_area_->setFocus();
166                 Cursor & cur = find_work_area_->bufferView().cursor();
167                 if (! cur.inRegexped())
168                         dispatch(FuncRequest(LFUN_REGEXP_MODE));
169                 dispatch(FuncRequest(LFUN_SELF_INSERT, regexps[index - 1]));
170                 regexpInsertCombo->setCurrentIndex(0);
171         }
172 }
173
174
175 void FindAndReplace::on_closePB_clicked()
176 {
177         find_work_area_->disable();
178         LYXERR(Debug::DEBUG, "Dispatching dialog-hide findreplaceadv" << std::endl);
179         parent_view_.dispatch(FuncRequest(LFUN_DIALOG_TOGGLE, "findreplaceadv"));
180 }
181
182
183 void FindAndReplace::on_findNextPB_clicked() {
184         find(false);
185 }
186
187
188 void FindAndReplace::on_findPrevPB_clicked() {
189         find(true);
190 }
191
192
193 void FindAndReplace::on_replacePB_clicked()
194 {
195 }
196
197
198 void FindAndReplace::on_replaceallPB_clicked()
199 {
200 }
201
202 Dialog * createGuiSearchAdv(GuiView & lv)
203 {
204         return new FindAndReplace(lv);
205 }
206
207
208 } // namespace frontend
209 } // namespace lyx
210
211
212 #include "moc_FindAndReplace.cpp"