GNU Octave  3.8.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-2013 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 ("Find and Replace");
75  setWindowIcon (QIcon(":/actions/icons/search.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  _search_selection_check_box->setCheckable (false); // TODO: Not implemented.
111  _search_selection_check_box->setEnabled (false);
112 
113  _edit_area = edit_area;
114  connect (_find_next_button, SIGNAL (clicked ()),
115  this, SLOT (find_next ()));
116  connect (_find_prev_button, SIGNAL (clicked ()),
117  this, SLOT (find_prev ()));
118  connect (_more_button, SIGNAL (toggled (bool)),
119  _extension, SLOT (setVisible (bool)));
120  connect (_replace_button, SIGNAL (clicked ()),
121  this, SLOT (replace ()));
122  connect (_replace_all_button, SIGNAL (clicked ()),
123  this, SLOT (replace_all ()));
124  connect (_backward_check_box, SIGNAL (stateChanged (int)),
125  this, SLOT (handle_backward_search_changed (int)));
126  connect (_button_box, SIGNAL (rejected ()),
127  this, SLOT (close ()));
128 
129  QVBoxLayout *extension_layout = new QVBoxLayout ();
130  extension_layout->setMargin (0);
131  extension_layout->addWidget (_whole_words_check_box);
132  extension_layout->addWidget (_backward_check_box);
133  extension_layout->addWidget (_search_selection_check_box);
134  _extension->setLayout (extension_layout);
135 
136  QGridLayout *top_left_layout = new QGridLayout;
137  top_left_layout->addWidget (_search_label, 1, 1);
138  top_left_layout->addWidget (_search_line_edit, 1, 2);
139  top_left_layout->addWidget (_replace_label, 2, 1);
140  top_left_layout->addWidget (_replace_line_edit, 2, 2);
141 
142  QVBoxLayout *left_layout = new QVBoxLayout;
143  left_layout->addLayout (top_left_layout);
144  left_layout->insertStretch (1, 5);
145  left_layout->addWidget (_case_check_box);
146  left_layout->addWidget (_from_start_check_box);
147  left_layout->addWidget (_wrap_check_box);
148  left_layout->addWidget (_regex_check_box);
149 
150  QGridLayout *main_layout = new QGridLayout;
151  main_layout->setSizeConstraint (QLayout::SetFixedSize);
152  main_layout->addLayout (left_layout, 0, 0);
153  main_layout->addWidget (_button_box, 0, 1);
154  main_layout->addWidget (_extension, 1, 0);
155  setLayout (main_layout);
156 
157  _extension->hide ();
158  _find_next_button->setDefault (true);
159  _find_result_available = false;
160 
161  // move dialog to side of the parent if there is room on the desktop to do so.
162  QWidget * desktop = QApplication::desktop ();
163  int xp = p->x () + p->frameGeometry ().width ();
164  int yp= p->y ();
165  if (desktop != 0 && sizeHint ().isValid ())
166  {
167  if (xp + sizeHint ().width () > desktop->width ())
168  xp = desktop->width () - sizeHint ().width ();
169  }
170 
171  move (xp, yp);
172 
173 }
174 
175 // set text of "search from start" depending on backward search
176 void
178 {
179  if (backward)
180  _from_start_check_box->setText (tr ("Search from end"));
181  else
182  _from_start_check_box->setText (tr ("Search from start"));
183 }
184 
185 // initialize search text with selected text if this is in one single line
186 void
188 {
189  if (_edit_area->hasSelectedText ())
190  {
191  int lbeg, lend, cbeg, cend;
192  _edit_area->getSelection(&lbeg,&cbeg,&lend,&cend);
193  if (lbeg == lend)
194  _search_line_edit->setText (_edit_area->selectedText ());
195  }
196 }
197 
198 void
200 {
201  find (!_backward_check_box->isChecked ());
202 }
203 
204 void
206 {
207  find (_backward_check_box->isChecked ());
208 }
209 
210 void
211 find_dialog::find (bool forward)
212 {
213  int line = -1, col = -1;
214  bool do_wrap = _wrap_check_box->isChecked ();
215  bool do_forward = true;
216 
218  {
219  // we found a match last time, cursor is at the end of the match
220  if (!forward)
221  {
222  // backward: go back one position or we will find the same again
223  do_forward = false;
224  _edit_area->getCursorPosition (&line,&col);
225  if (col > 0)
226  _edit_area->setCursorPosition (line,--col);
227  }
228  }
229 
230  _find_result_available = false;
231 
232  if (_from_start_check_box->isChecked ())
233  {
234  line = 0;
235  col = 0;
236  if (_backward_check_box->isChecked ())
237  do_wrap = true;
238  }
239 
240  if (_edit_area)
241  {
243  = _edit_area->findFirst (_search_line_edit->text (),
244  _regex_check_box->isChecked (),
245  _case_check_box->isChecked (),
246  _whole_words_check_box->isChecked (),
247  do_wrap,
248  do_forward,
249  line,col,
250  true
251 #ifdef HAVE_QSCI_VERSION_2_6_0
252  , true
253 #endif
254  );
255  }
257  _from_start_check_box->setChecked (0);
258  else
260 }
261 
262 
263 void
265 {
266  if (_edit_area)
267  {
268  _edit_area->replace (_replace_line_edit->text ());
269  if (!_edit_area->findNext())
271  }
272 }
273 
274 void
276 {
277  int count = 0;
278 
279  // check whether find & replace srings are different (avoid endless loop!)
280  int strDiff;
281  Qt::CaseSensitivity cs;
282  if (_case_check_box->isChecked())
283  {
284  cs = Qt::CaseSensitive;
285  }
286  else
287  {
288  cs = Qt::CaseInsensitive;
289  }
290  strDiff = QString::compare (_search_line_edit->text(),
291  _replace_line_edit->text(), cs);
292 
293  // replace all if strings are different
294  if (_edit_area && strDiff )
295  {
296  find (!_backward_check_box->isChecked ()); // find first occurence
297  while (_find_result_available) // while search string is found
298  {
299  _edit_area->replace (_replace_line_edit->text ()); // replace
300  count++; // inc counter
301  _find_result_available = _edit_area->findNext(); // and find next
302  }
303  QMessageBox msg_box (QMessageBox::Information, tr ("Replace Result"),
304  tr ("%1 items replaced").arg(count),
305  QMessageBox::Ok, this);
306  msg_box.exec ();
307  }
308  // TODO: Show number of replaced strings
309 }
310 
311 void
313 {
314  QMessageBox msg_box (QMessageBox::Information, tr ("Find Result"),
315  tr ("No more matches found"), QMessageBox::Ok, this);
316  msg_box.exec ();
317 }
318 
319 
320 #endif