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