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