GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
find-dialog.cc
Go to the documentation of this file.
1 /****************************************************************************
2 
3 Find dialog derived from an example from Qt Toolkit (license below (**))
4 
5 Copyright (C) 2012-2015 Torsten <[email protected]>
6 Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
7  All rights reserved.
8  Contact: Nokia Corporation ([email protected])
9 
10 This file is part of Octave.
11 
12 Octave is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by the
14 Free Software Foundation; either version 3 of the License, or (at your
15 option) any later version.
16 
17 Octave is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with Octave; see the file COPYING. If not, see
24 <http://www.gnu.org/licenses/>.
25 
26 ** This file is part of the examples of the Qt Toolkit.
27 **
28 ** $QT_BEGIN_LICENSE:LGPL$
29 ** Commercial Usage
30 ** Licensees holding valid Qt Commercial licenses may use this file in
31 ** accordance with the Qt Commercial License Agreement provided with the
32 ** Software or, alternatively, in accordance with the terms contained in
33 ** a written agreement between you and Nokia.
34 **
35 ** GNU Lesser General Public License Usage
36 ** Alternatively, this file may be used under the terms of the GNU Lesser
37 ** General Public License version 2.1 as published by the Free Software
38 ** Foundation and appearing in the file LICENSE.LGPL included in the
39 ** packaging of this file. Please review the following information to
40 ** ensure the GNU Lesser General Public License version 2.1 requirements
41 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
42 **
43 ** In addition, as a special exception, Nokia gives you certain additional
44 ** rights. These rights are described in the Nokia Qt LGPL Exception
45 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
46 **
47 ** GNU General Public License Usage
48 ** Alternatively, this file may be used under the terms of the GNU
49 ** General Public License version 3.0 as published by the Free Software
50 ** Foundation and appearing in the file LICENSE.GPL included in the
51 ** packaging of this file. Please review the following information to
52 ** ensure the GNU General Public License version 3.0 requirements will be
53 ** met: http://www.gnu.org/copyleft/gpl.html.
54 **
55 ** If you have questions regarding the use of this file, please contact
56 ** Nokia at [email protected].
57 ** $QT_END_LICENSE$
58 **
59 ****************************************************************************/
60 
61 #ifdef HAVE_CONFIG_H
62 #include <config.h>
63 #endif
64 
65 #ifdef HAVE_QSCINTILLA
66 
67 #include <QtGui>
68 #include <QIcon>
69 #include "find-dialog.h"
70 
72  : QDialog (p)
73 {
74  setWindowTitle (tr ("Find and Replace"));
75  setWindowIcon (QIcon(":/actions/icons/find.png"));
76 
77  _search_label = new QLabel (tr ("Find &what:"));
78  _search_line_edit = new QLineEdit;
79  _search_label->setBuddy (_search_line_edit);
80  _replace_label = new QLabel (tr ("Re&place with:"));
81  _replace_line_edit = new QLineEdit;
82  _replace_label->setBuddy (_replace_line_edit);
83 
84  _case_check_box = new QCheckBox (tr ("Match &case"));
85  _from_start_check_box = new QCheckBox (tr ("Search from &start"));
86  _wrap_check_box = new QCheckBox (tr ("&Wrap while searching"));
87  _wrap_check_box->setChecked(true);
88  _find_next_button = new QPushButton (tr ("&Find Next"));
89  _find_prev_button = new QPushButton (tr ("Find &Previous"));
90  _replace_button = new QPushButton (tr ("&Replace"));
91  _replace_all_button = new QPushButton (tr ("Replace &All"));
92 
93  _more_button = new QPushButton (tr ("&More..."));
94  _more_button->setCheckable (true);
95  _more_button->setAutoDefault (false);
96 
97  _button_box = new QDialogButtonBox (Qt::Vertical);
98  _button_box->addButton (_find_next_button, QDialogButtonBox::ActionRole);
99  _button_box->addButton (_find_prev_button, QDialogButtonBox::ActionRole);
100  _button_box->addButton (_replace_button, QDialogButtonBox::ActionRole);
101  _button_box->addButton (_replace_all_button, QDialogButtonBox::ActionRole);
102  _button_box->addButton (_more_button, QDialogButtonBox::ActionRole);
103  _button_box->addButton (QDialogButtonBox::Close);
104 
105  _extension = new QWidget (this);
106  _whole_words_check_box = new QCheckBox (tr ("&Whole words"));
107  _regex_check_box = new QCheckBox (tr ("Regular E&xpressions"));
108  _backward_check_box = new QCheckBox (tr ("Search &backward"));
109  _search_selection_check_box = new QCheckBox (tr ("Search se&lection"));
110 #ifdef HAVE_QSCI_FINDSELECTION
111  _search_selection_check_box->setCheckable (true);
112  _search_selection_check_box->setEnabled (edit_area->hasSelectedText ());
113 #else
114  _search_selection_check_box->setCheckable (false);
115  _search_selection_check_box->setEnabled (false);
116 #endif
117 
118  _edit_area = edit_area;
119  connect (_find_next_button, SIGNAL (clicked ()),
120  this, SLOT (find_next ()));
121  connect (_find_prev_button, SIGNAL (clicked ()),
122  this, SLOT (find_prev ()));
123  connect (_more_button, SIGNAL (toggled (bool)),
124  _extension, SLOT (setVisible (bool)));
125  connect (_replace_button, SIGNAL (clicked ()),
126  this, SLOT (replace ()));
127  connect (_replace_all_button, SIGNAL (clicked ()),
128  this, SLOT (replace_all ()));
129  connect (_backward_check_box, SIGNAL (stateChanged (int)),
130  this, SLOT (handle_backward_search_changed (int)));
131  connect (_button_box, SIGNAL (rejected ()),
132  this, SLOT (close ()));
133  connect (_search_line_edit, SIGNAL (textChanged (QString)),
134  this, SLOT (handle_search_text_changed (QString)));
135 
136 #ifdef HAVE_QSCI_FINDSELECTION
137  connect (_edit_area, SIGNAL (copyAvailable (bool)),
138  this, SLOT (handle_selection_changed (bool)));
139  connect (_search_selection_check_box, SIGNAL (stateChanged (int)),
140  this, SLOT (handle_sel_search_changed (int)));
141 #endif
142 
143  QVBoxLayout *extension_layout = new QVBoxLayout ();
144  extension_layout->setMargin (0);
145  extension_layout->addWidget (_whole_words_check_box);
146  extension_layout->addWidget (_backward_check_box);
147  extension_layout->addWidget (_search_selection_check_box);
148  _extension->setLayout (extension_layout);
149 
150  QGridLayout *top_left_layout = new QGridLayout;
151  top_left_layout->addWidget (_search_label, 1, 1);
152  top_left_layout->addWidget (_search_line_edit, 1, 2);
153  top_left_layout->addWidget (_replace_label, 2, 1);
154  top_left_layout->addWidget (_replace_line_edit, 2, 2);
155 
156  QVBoxLayout *left_layout = new QVBoxLayout;
157  left_layout->addLayout (top_left_layout);
158  left_layout->insertStretch (1, 5);
159  left_layout->addWidget (_case_check_box);
160  left_layout->addWidget (_from_start_check_box);
161  left_layout->addWidget (_wrap_check_box);
162  left_layout->addWidget (_regex_check_box);
163 
164  QGridLayout *main_layout = new QGridLayout;
165  main_layout->setSizeConstraint (QLayout::SetFixedSize);
166  main_layout->addLayout (left_layout, 0, 0);
167  main_layout->addWidget (_button_box, 0, 1);
168  main_layout->addWidget (_extension, 1, 0);
169  setLayout (main_layout);
170 
171  _extension->hide ();
172  _find_next_button->setDefault (true);
173  _find_result_available = false;
174  _rep_all = 0;
175  _rep_active = false;
176 
177  // move dialog to side of the parent if there is room on the desktop to do so.
178  int xp = p->x () +20;
179  int yp = p->y () + p->frameGeometry ().height () - sizeHint ().height () -20;
180 
181  if (yp < 0)
182  yp = 0;
183 
184  move (xp, yp);
185 
186 }
187 
188 // set text of "search from start" depending on backward search
189 void
191 {
192  if (backward)
193  _from_start_check_box->setText (tr ("Search from end"));
194  else
195  _from_start_check_box->setText (tr ("Search from start"));
196 }
197 
198 // search text has changed: reset the search
199 void
201 {
202  if (_search_selection_check_box->isChecked ())
203  _find_result_available = false;
204 }
205 
206 #ifdef HAVE_QSCI_FINDSELECTION
207 void
208 find_dialog::handle_sel_search_changed (int selected)
209 {
210  _from_start_check_box->setEnabled (! selected);
211  _find_result_available = false;
212 }
213 
214 void
215 find_dialog::handle_selection_changed (bool has_selected)
216 {
217  if (_rep_active)
218  return;
219 
220  _search_selection_check_box->setEnabled (has_selected);
221  _find_result_available = false;
222  if (! has_selected)
223  _search_selection_check_box->setChecked (false);
224 }
225 #endif
226 
227 // initialize search text with selected text if this is in one single line
228 void
230 {
231  if (_edit_area->hasSelectedText ())
232  {
233  int lbeg, lend, cbeg, cend;
234  _edit_area->getSelection(&lbeg,&cbeg,&lend,&cend);
235  if (lbeg == lend)
236  _search_line_edit->setText (_edit_area->selectedText ());
237  }
238 }
239 
240 void
242 {
243  find (!_backward_check_box->isChecked ());
244 }
245 
246 void
248 {
249  find (_backward_check_box->isChecked ());
250 }
251 
252 void
253 find_dialog::find (bool forward)
254 {
255  int line, col;
256  line = col = -1;
257  bool do_wrap = _wrap_check_box->isChecked ();
258  bool do_forward = forward;
259 
260  if (_rep_all)
261  {
262  if (_rep_all == 1)
263  {
264  line = 0;
265  col = 0;
266  }
267  do_wrap = false;
268  // The following line is a workaround for the issue that when replacing
269  // a text with a new one with different size within the selection,
270  // the selection is not updated leading to missing or extra replacements.
271  // This does not happen, when the selection is search backwards
272  do_forward = ! _search_selection_check_box->isChecked ();
273  }
274  else
275  {
276  if (_from_start_check_box->isChecked ())
277  {
278  if (do_forward)
279  {
280  line = 0;
281  col = 0;
282  }
283  else
284  {
285  line = _edit_area->lines () - 1;
286  col = _edit_area->text (line).length () - 1;
287  if (col == -1)
288  col = 0;
289  }
290  }
291  else if (! do_forward)
292  {
293  // search from position before search characters text length
294  // if search backward on existing results,
295  _edit_area->getCursorPosition (&line,&col);
296  if (_find_result_available && _edit_area->hasSelectedText ())
297  {
298  int currpos = _edit_area->positionFromLineIndex(line,col);
299  currpos -= (_search_line_edit->text ().length ());
300  if (currpos < 0)
301  currpos = 0;
302  _edit_area->lineIndexFromPosition(currpos, &line,&col);
303  }
304  }
305  }
306 
307  if (_edit_area)
308  {
309  if (_edit_area->hasSelectedText ()
310  && _search_selection_check_box->isChecked ())
311  {
312 #ifdef HAVE_QSCI_FINDSELECTION
314  _find_result_available = _edit_area->findNext ();
315  else
317  = _edit_area->findFirstInSelection (
318  _search_line_edit->text (),
319  _regex_check_box->isChecked (),
320  _case_check_box->isChecked (),
321  _whole_words_check_box->isChecked (),
322  do_forward,
323  true
324 #ifdef HAVE_QSCI_VERSION_2_6_0
325  , true
326 #endif
327  );
328 #endif
329  }
330  else
331  {
333  = _edit_area->findFirst (_search_line_edit->text (),
334  _regex_check_box->isChecked (),
335  _case_check_box->isChecked (),
336  _whole_words_check_box->isChecked (),
337  do_wrap,
338  do_forward,
339  line,col,
340  true
341 #ifdef HAVE_QSCI_VERSION_2_6_0
342  , true
343 #endif
344  );
345  }
346  }
347 
349  _from_start_check_box->setChecked (0);
350  else if (! _rep_all)
352 }
353 
354 void
356 {
357  _rep_active = true; // changes in selection not made by the user
358  _edit_area->replace (_replace_line_edit->text ());
359  _rep_active = false;
360 }
361 
362 void
364 {
365  if (_edit_area)
366  {
367  // The following line is a workaround for the issue that when replacing
368  // a text with a new one with different size within the selection,
369  // the selection is not updated leading to missing or extra replacements.
370  // This does not happen, when the selection is search backwards
371  if (_search_selection_check_box->isChecked ())
372  _backward_check_box->setChecked (true);
373 
374  // do the replace if we have selected text
375  if (_find_result_available && _edit_area->hasSelectedText ())
376  do_replace ();
377 
378  find_next ();
379  }
380 }
381 
382 void
384 {
385  int line, col;
386 
387  if (_edit_area)
388  {
389  _edit_area->getCursorPosition (&line,&col);
390 
391  _rep_all = 1;
392  find_next (); // find first occurence (forward)
393  while (_find_result_available) // while search string is found
394  {
395  do_replace ();
396  _rep_all++; // inc counter
397  find_next (); // find next
398  }
399 
400  QMessageBox msg_box (QMessageBox::Information, tr ("Replace Result"),
401  tr ("%1 items replaced").arg(_rep_all-1),
402  QMessageBox::Ok, this);
403  msg_box.exec ();
404 
405  _rep_all = 0;
406  _find_result_available = false;
407 
408  if (! _search_selection_check_box->isChecked ())
409  _edit_area->setCursorPosition (line,col);
410  }
411 }
412 
413 void
415 {
416  QMessageBox msg_box (QMessageBox::Information, tr ("Find Result"),
417  tr ("No more matches found"), QMessageBox::Ok, this);
418  msg_box.exec ();
419 }
420 
421 
422 #endif
void replace()
void find(bool forward=true)
QCheckBox * _search_selection_check_box
Definition: find-dialog.h:109
void handle_backward_search_changed(int)
void init_search_text()
void no_matches_message()
void find_prev()
bool _find_result_available
Definition: find-dialog.h:119
find_dialog(QsciScintilla *edit_area, QWidget *parent=0)
static void replace(QString &text, const QRegExp &re, const QString &after)
Definition: parser.cc:566
void find_next()
bool _rep_active
Definition: find-dialog.h:121
QCheckBox * _whole_words_check_box
Definition: find-dialog.h:107
QLineEdit * _replace_line_edit
Definition: find-dialog.h:103
void replace_all()
QCheckBox * _backward_check_box
Definition: find-dialog.h:110
QLineEdit * _search_line_edit
Definition: find-dialog.h:101
double arg(double x)
Definition: lo-mappers.h:37
QCheckBox * _regex_check_box
Definition: find-dialog.h:108
void handle_search_text_changed(QString new_search_text)
QCheckBox * _case_check_box
Definition: find-dialog.h:104
void do_replace()
QCheckBox * _wrap_check_box
Definition: find-dialog.h:106
QsciScintilla * _edit_area
Definition: find-dialog.h:118
QCheckBox * _from_start_check_box
Definition: find-dialog.h:105