GNU Octave  4.2.1
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-2017 Torsten <ttl@justmail.de>
6 Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
7  All rights reserved.
8  Contact: Nokia Corporation (qt-info@nokia.com)
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 qt-info@nokia.com.
57 ** $QT_END_LICENSE$
58 **
59 
60 */
61 
62 #if defined (HAVE_CONFIG_H)
63 # include "config.h"
64 #endif
65 
66 #if defined (HAVE_QSCINTILLA)
67 
68 #include <QCheckBox>
69 #include <QCheckBox>
70 #include <QDialogButtonBox>
71 #include <QGridLayout>
72 #include <QIcon>
73 #include <QLabel>
74 #include <QLineEdit>
75 #include <QMessageBox>
76 #include <QPushButton>
77 #include <QVBoxLayout>
78 
79 #include "find-dialog.h"
80 
82  QList<QAction *> find_actions, QWidget *p)
83  : QDialog (p)
84 {
85  setWindowTitle (tr ("Find and Replace"));
86  setWindowIcon (QIcon(":/actions/icons/find.png"));
87 
88  _search_label = new QLabel (tr ("Find &what:"));
90  _search_label->setBuddy (_search_line_edit);
91  _replace_label = new QLabel (tr ("Re&place with:"));
94 
95  _case_check_box = new QCheckBox (tr ("Match &case"));
96  _from_start_check_box = new QCheckBox (tr ("Search from &start"));
97  _wrap_check_box = new QCheckBox (tr ("&Wrap while searching"));
98  _wrap_check_box->setChecked(true);
99  _find_next_button = new QPushButton (tr ("&Find Next"));
100  _find_prev_button = new QPushButton (tr ("Find &Previous"));
101  _replace_button = new QPushButton (tr ("&Replace"));
102  _replace_all_button = new QPushButton (tr ("Replace &All"));
103 
104  _more_button = new QPushButton (tr ("&More..."));
105  _more_button->setCheckable (true);
106  _more_button->setAutoDefault (false);
107 
108  _button_box = new QDialogButtonBox (Qt::Vertical);
109  _button_box->addButton (_find_next_button, QDialogButtonBox::ActionRole);
110  _button_box->addButton (_find_prev_button, QDialogButtonBox::ActionRole);
111  _button_box->addButton (_replace_button, QDialogButtonBox::ActionRole);
112  _button_box->addButton (_replace_all_button, QDialogButtonBox::ActionRole);
113  _button_box->addButton (_more_button, QDialogButtonBox::ActionRole);
114  _button_box->addButton (QDialogButtonBox::Close);
115 
116  _extension = new QWidget (this);
117  _whole_words_check_box = new QCheckBox (tr ("&Whole words"));
118  _regex_check_box = new QCheckBox (tr ("Regular E&xpressions"));
119  _backward_check_box = new QCheckBox (tr ("Search &backward"));
120  _search_selection_check_box = new QCheckBox (tr ("Search se&lection"));
121 #if defined (HAVE_QSCI_FINDSELECTION)
122  _search_selection_check_box->setCheckable (true);
123  _search_selection_check_box->setEnabled (edit_area->hasSelectedText ());
124 #else
125  _search_selection_check_box->setCheckable (false);
126  _search_selection_check_box->setEnabled (false);
127 #endif
128 
129  _edit_area = edit_area;
130  connect (_find_next_button, SIGNAL (clicked ()),
131  this, SLOT (find_next ()));
132  connect (_find_prev_button, SIGNAL (clicked ()),
133  this, SLOT (find_prev ()));
134  connect (_more_button, SIGNAL (toggled (bool)),
135  _extension, SLOT (setVisible (bool)));
136  connect (_replace_button, SIGNAL (clicked ()),
137  this, SLOT (replace ()));
138  connect (_replace_all_button, SIGNAL (clicked ()),
139  this, SLOT (replace_all ()));
140  connect (_backward_check_box, SIGNAL (stateChanged (int)),
141  this, SLOT (handle_backward_search_changed (int)));
142  connect (_button_box, SIGNAL (rejected ()),
143  this, SLOT (close ()));
144  connect (_search_line_edit, SIGNAL (textChanged (QString)),
145  this, SLOT (handle_search_text_changed (QString)));
146 
147 #if defined (HAVE_QSCI_FINDSELECTION)
148  connect (_edit_area, SIGNAL (copyAvailable (bool)),
149  this, SLOT (handle_selection_changed (bool)));
150  connect (_search_selection_check_box, SIGNAL (stateChanged (int)),
151  this, SLOT (handle_sel_search_changed (int)));
152 #endif
153 
154  QVBoxLayout *extension_layout = new QVBoxLayout ();
155  extension_layout->setMargin (0);
156  extension_layout->addWidget (_whole_words_check_box);
157  extension_layout->addWidget (_backward_check_box);
158  extension_layout->addWidget (_search_selection_check_box);
159  _extension->setLayout (extension_layout);
160 
161  QGridLayout *top_left_layout = new QGridLayout;
162  top_left_layout->addWidget (_search_label, 1, 1);
163  top_left_layout->addWidget (_search_line_edit, 1, 2);
164  top_left_layout->addWidget (_replace_label, 2, 1);
165  top_left_layout->addWidget (_replace_line_edit, 2, 2);
166 
167  QVBoxLayout *left_layout = new QVBoxLayout;
168  left_layout->addLayout (top_left_layout);
169  left_layout->insertStretch (1, 5);
170  left_layout->addWidget (_case_check_box);
171  left_layout->addWidget (_from_start_check_box);
172  left_layout->addWidget (_wrap_check_box);
173  left_layout->addWidget (_regex_check_box);
174 
175  QGridLayout *main_layout = new QGridLayout;
176  main_layout->setSizeConstraint (QLayout::SetFixedSize);
177  main_layout->addLayout (left_layout, 0, 0);
178  main_layout->addWidget (_button_box, 0, 1);
179  main_layout->addWidget (_extension, 1, 0);
180  setLayout (main_layout);
181 
182  _extension->hide ();
183  _find_next_button->setDefault (true);
184  _find_result_available = false;
185  _rep_all = 0;
186  _rep_active = false;
187 
188  // set the actions
189  addActions (find_actions);
190 
191  // move dialog to side of the parent if there is room on the desktop to do so.
192  int xp = p->x () +20;
193  int yp = p->y () + p->frameGeometry ().height () - sizeHint ().height () -20;
194 
195  if (yp < 0)
196  yp = 0;
197 
198  move (xp, yp);
199 
200 }
201 
202 // set text of "search from start" depending on backward search
203 void
205 {
206  if (backward)
207  _from_start_check_box->setText (tr ("Search from end"));
208  else
209  _from_start_check_box->setText (tr ("Search from start"));
210 }
211 
212 // search text has changed: reset the search
213 void
215 {
216  if (_search_selection_check_box->isChecked ())
217  _find_result_available = false;
218 }
219 
220 #if defined (HAVE_QSCI_FINDSELECTION)
221 void
223 {
224  _from_start_check_box->setEnabled (! selected);
225  _find_result_available = false;
226 }
227 #else
228 void
230 #endif
231 
232 #if defined (HAVE_QSCI_FINDSELECTION)
233 void
234 find_dialog::handle_selection_changed (bool has_selected)
235 {
236  if (_rep_active)
237  return;
238 
239  _search_selection_check_box->setEnabled (has_selected);
240  _find_result_available = false;
241  if (! has_selected)
242  _search_selection_check_box->setChecked (false);
243 }
244 #else
245 void
246 find_dialog::handle_selection_changed (bool /* has_selected */) { }
247 #endif
248 
249 // initialize search text with selected text if this is in one single line
250 void
252 {
253  if (_edit_area->hasSelectedText ())
254  {
255  int lbeg, lend, cbeg, cend;
256  _edit_area->getSelection(&lbeg,&cbeg,&lend,&cend);
257  if (lbeg == lend)
258  _search_line_edit->setText (_edit_area->selectedText ());
259  }
260 
261  // set focus to "Find what" and select all text
262  _search_line_edit->setFocus();
263  _search_line_edit->selectAll();
264 
265  // Default to "find" next time.
266  // Otherwise, it defaults to the last action, which may be "replace all".
267  _find_next_button->setDefault (true);
268 }
269 
270 void
272 {
273  find (! _backward_check_box->isChecked ());
274 }
275 
276 void
278 {
279  find (_backward_check_box->isChecked ());
280 }
281 
282 void
283 find_dialog::find (bool forward)
284 {
285  int line, col;
286  line = col = -1;
287  bool do_wrap = _wrap_check_box->isChecked ();
288  bool do_forward = forward;
289 
290  if (_rep_all)
291  {
292  if (_rep_all == 1)
293  {
294  line = 0;
295  col = 0;
296  }
297  do_wrap = false;
298  // The following line is a workaround for the issue that when replacing
299  // a text with a new one with different size within the selection,
300  // the selection is not updated leading to missing or extra replacements.
301  // This does not happen, when the selection is search backwards
302  do_forward = ! _search_selection_check_box->isChecked ();
303  }
304  else
305  {
306  if (_from_start_check_box->isChecked ())
307  {
308  if (do_forward)
309  {
310  line = 0;
311  col = 0;
312  }
313  else
314  {
315  line = _edit_area->lines () - 1;
316  col = _edit_area->text (line).length () - 1;
317  if (col == -1)
318  col = 0;
319  }
320  }
321  else if (! do_forward)
322  {
323  // search from position before search characters text length
324  // if search backward on existing results,
325  _edit_area->getCursorPosition (&line,&col);
326  if (_find_result_available && _edit_area->hasSelectedText ())
327  {
328  int currpos = _edit_area->positionFromLineIndex(line,col);
329  currpos -= (_search_line_edit->text ().length ());
330  if (currpos < 0)
331  currpos = 0;
332  _edit_area->lineIndexFromPosition(currpos, &line,&col);
333  }
334  }
335  }
336 
337  if (_edit_area)
338  {
339  if (_edit_area->hasSelectedText ()
340  && _search_selection_check_box->isChecked ())
341  {
342 #if defined (HAVE_QSCI_FINDSELECTION)
344  _find_result_available = _edit_area->findNext ();
345  else
347  = _edit_area->findFirstInSelection (
348  _search_line_edit->text (),
349  _regex_check_box->isChecked (),
350  _case_check_box->isChecked (),
351  _whole_words_check_box->isChecked (),
352  do_forward,
353  true
354 #if defined (HAVE_QSCI_VERSION_2_6_0)
355  , true
356 #endif
357  );
358 #endif
359  }
360  else
361  {
363  = _edit_area->findFirst (_search_line_edit->text (),
364  _regex_check_box->isChecked (),
365  _case_check_box->isChecked (),
366  _whole_words_check_box->isChecked (),
367  do_wrap,
368  do_forward,
369  line,col,
370  true
371 #if defined (HAVE_QSCI_VERSION_2_6_0)
372  , true
373 #endif
374  );
375  }
376  }
377 
379  _from_start_check_box->setChecked (0);
380  else if (! _rep_all)
382 }
383 
384 void
386 {
387  _rep_active = true; // changes in selection not made by the user
388  _edit_area->replace (_replace_line_edit->text ());
389  _rep_active = false;
390 }
391 
392 void
394 {
395  if (_edit_area)
396  {
397  // The following line is a workaround for the issue that when replacing
398  // a text with a new one with different size within the selection,
399  // the selection is not updated leading to missing or extra replacements.
400  // This does not happen, when the selection is search backwards
401  if (_search_selection_check_box->isChecked ())
402  _backward_check_box->setChecked (true);
403 
404  // do the replace if we have selected text
405  if (_find_result_available && _edit_area->hasSelectedText ())
406  do_replace ();
407 
408  find_next ();
409  }
410 }
411 
412 void
414 {
415  int line, col;
416 
417  if (_edit_area)
418  {
419  _edit_area->getCursorPosition (&line,&col);
420 
421  _rep_all = 1;
422  find_next (); // find first occurence (forward)
423  while (_find_result_available) // while search string is found
424  {
425  do_replace ();
426  _rep_all++; // inc counter
427  find_next (); // find next
428  }
429 
430  QMessageBox msg_box (QMessageBox::Information, tr ("Replace Result"),
431  tr ("%1 items replaced").arg(_rep_all-1),
432  QMessageBox::Ok, this);
433  msg_box.exec ();
434 
435  _rep_all = 0;
436  _find_result_available = false;
437 
438  if (! _search_selection_check_box->isChecked ())
439  _edit_area->setCursorPosition (line,col);
440  }
441 }
442 
443 void
445 {
446  QMessageBox msg_box (QMessageBox::Information, tr ("Find Result"),
447  tr ("No more matches found"), QMessageBox::Ok, this);
448  msg_box.exec ();
449 }
450 
451 #endif
void replace()
Definition: find-dialog.cc:393
void find(bool forward=true)
Definition: find-dialog.cc:283
QCheckBox * _search_selection_check_box
Definition: find-dialog.h:110
QPushButton * _find_prev_button
Definition: find-dialog.h:114
QWidget * _extension
Definition: find-dialog.h:118
QLabel * _search_label
Definition: find-dialog.h:101
void handle_backward_search_changed(int)
Definition: find-dialog.cc:204
void init_search_text()
Definition: find-dialog.cc:251
void no_matches_message()
Definition: find-dialog.cc:444
void find_prev()
Definition: find-dialog.cc:277
bool _find_result_available
Definition: find-dialog.h:120
octave_value arg
Definition: pr-output.cc:3440
QPushButton * _replace_all_button
Definition: find-dialog.h:116
void handle_sel_search_changed(int)
Definition: find-dialog.cc:229
QPushButton * _more_button
Definition: find-dialog.h:117
void find_next()
Definition: find-dialog.cc:271
bool _rep_active
Definition: find-dialog.h:122
QCheckBox * _whole_words_check_box
Definition: find-dialog.h:108
QLineEdit * _replace_line_edit
Definition: find-dialog.h:104
void replace_all()
Definition: find-dialog.cc:413
QCheckBox * _backward_check_box
Definition: find-dialog.h:111
QLineEdit * _search_line_edit
Definition: find-dialog.h:102
QCheckBox * _regex_check_box
Definition: find-dialog.h:109
void handle_search_text_changed(QString new_search_text)
Definition: find-dialog.cc:214
QCheckBox * _case_check_box
Definition: find-dialog.h:105
p
Definition: lu.cc:138
QDialogButtonBox * _button_box
Definition: find-dialog.h:112
void do_replace()
Definition: find-dialog.cc:385
QCheckBox * _wrap_check_box
Definition: find-dialog.h:107
QLabel * _replace_label
Definition: find-dialog.h:103
QPushButton * _find_next_button
Definition: find-dialog.h:113
void handle_selection_changed(bool has_selected)
Definition: find-dialog.cc:246
find_dialog(QsciScintilla *edit_area, QList< QAction * > find_actions, QWidget *parent=0)
Definition: find-dialog.cc:81
QsciScintilla * _edit_area
Definition: find-dialog.h:119
QPushButton * _replace_button
Definition: find-dialog.h:115
QCheckBox * _from_start_check_box
Definition: find-dialog.h:106