• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeutils
 

tdeutils

  • tdeutils
kreplace.cpp
1 /*
2  Copyright (C) 2001, S.R.Haque <srhaque@iee.org>.
3  Copyright (C) 2002, David Faure <david@mandrakesoft.com>
4  This file is part of the KDE project
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2, as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include <tqlabel.h>
22 #include <tdeapplication.h>
23 #include <kdebug.h>
24 
25 #include <tdelocale.h>
26 #include <tdemessagebox.h>
27 #include "kreplace.h"
28 #include "kreplacedialog.h"
29 #include <tqregexp.h>
30 
31 //#define DEBUG_REPLACE
32 #define INDEX_NOMATCH -1
33 
34 class KReplaceNextDialog : public KDialogBase
35 {
36 public:
37  KReplaceNextDialog( TQWidget *parent );
38  void setLabel( const TQString& pattern, const TQString& replacement );
39 private:
40  TQLabel* m_mainLabel;
41 };
42 
43 KReplaceNextDialog::KReplaceNextDialog(TQWidget *parent) :
44  KDialogBase(parent, 0, false, // non-modal!
45  i18n("Replace"),
46  User3 | User2 | User1 | Close,
47  User3,
48  false,
49  i18n("&All"), i18n("&Skip"), i18n("Replace"))
50 {
51  m_mainLabel = new TQLabel( this );
52  setMainWidget( m_mainLabel );
53  resize(minimumSize());
54 }
55 
56 void KReplaceNextDialog::setLabel( const TQString& pattern, const TQString& replacement )
57 {
58  m_mainLabel->setText( i18n("Replace '%1' with '%2'?").arg(pattern).arg(replacement) );
59 }
60 
62 
63 KReplace::KReplace(const TQString &pattern, const TQString &replacement, long options, TQWidget *parent) :
64  KFind( pattern, options, parent )
65 {
66  m_replacements = 0;
67  m_replacement = replacement;
68 }
69 
70 KReplace::KReplace(const TQString &pattern, const TQString &replacement, long options, TQWidget *parent, TQWidget *dlg) :
71  KFind( pattern, options, parent, dlg )
72 {
73  m_replacements = 0;
74  m_replacement = replacement;
75 }
76 
77 KReplace::~KReplace()
78 {
79  // KFind::~KFind will delete m_dialog
80 }
81 
82 KDialogBase* KReplace::replaceNextDialog( bool create )
83 {
84  if ( m_dialog || create )
85  return dialog();
86  return 0L;
87 }
88 
89 KReplaceNextDialog* KReplace::dialog()
90 {
91  if ( !m_dialog )
92  {
93  m_dialog = new KReplaceNextDialog( parentWidget() );
94  connect( m_dialog, TQ_SIGNAL( user1Clicked() ), this, TQ_SLOT( slotReplaceAll() ) );
95  connect( m_dialog, TQ_SIGNAL( user2Clicked() ), this, TQ_SLOT( slotSkip() ) );
96  connect( m_dialog, TQ_SIGNAL( user3Clicked() ), this, TQ_SLOT( slotReplace() ) );
97  connect( m_dialog, TQ_SIGNAL( finished() ), this, TQ_SLOT( slotDialogClosed() ) );
98  }
99  return static_cast<KReplaceNextDialog *>(m_dialog);
100 }
101 
102 void KReplace::displayFinalDialog() const
103 {
104  if ( !m_replacements )
105  KMessageBox::information(parentWidget(), i18n("No text was replaced."));
106  else
107  KMessageBox::information(parentWidget(), i18n("1 replacement done.", "%n replacements done.", m_replacements ) );
108 }
109 
110 KFind::Result KReplace::replace()
111 {
112 #ifdef DEBUG_REPLACE
113  kdDebug() << k_funcinfo << "m_index=" << m_index << endl;
114 #endif
115  if ( m_index == INDEX_NOMATCH && m_lastResult == Match )
116  {
117  m_lastResult = NoMatch;
118  return NoMatch;
119  }
120 
121  do // this loop is only because validateMatch can fail
122  {
123 #ifdef DEBUG_REPLACE
124  kdDebug() << k_funcinfo << "beginning of loop: m_index=" << m_index << endl;
125 #endif
126  // Find the next match.
127  if ( m_options & KReplaceDialog::RegularExpression )
128  m_index = KFind::find(m_text, *m_regExp, m_index, m_options, &m_matchedLength);
129  else
130  m_index = KFind::find(m_text, m_pattern, m_index, m_options, &m_matchedLength);
131 #ifdef DEBUG_REPLACE
132  kdDebug() << k_funcinfo << "KFind::find returned m_index=" << m_index << endl;
133 #endif
134  if ( m_index != -1 )
135  {
136  // Flexibility: the app can add more rules to validate a possible match
137  if ( validateMatch( m_text, m_index, m_matchedLength ) )
138  {
139  if ( m_options & KReplaceDialog::PromptOnReplace )
140  {
141 #ifdef DEBUG_REPLACE
142  kdDebug() << k_funcinfo << "PromptOnReplace" << endl;
143 #endif
144  // Display accurate initial string and replacement string, they can vary
145  TQString matchedText = m_text.mid( m_index, m_matchedLength );
146  TQString rep = matchedText;
147  KReplace::replace(rep, m_replacement, 0, m_options, m_matchedLength);
148  dialog()->setLabel( matchedText, rep );
149  dialog()->show();
150 
151  // Tell the world about the match we found, in case someone wants to
152  // highlight it.
153  emit highlight(m_text, m_index, m_matchedLength);
154 
155  m_lastResult = Match;
156  return Match;
157  }
158  else
159  {
160  doReplace(); // this moves on too
161  }
162  }
163  else
164  {
165  // not validated -> move on
166  if (m_options & KFindDialog::FindBackwards)
167  m_index--;
168  else
169  m_index++;
170  }
171  } else
172  m_index = INDEX_NOMATCH; // will exit the loop
173  }
174  while (m_index != INDEX_NOMATCH);
175 
176  m_lastResult = NoMatch;
177  return NoMatch;
178 }
179 
180 int KReplace::replace(TQString &text, const TQString &pattern, const TQString &replacement, int index, long options, int *replacedLength)
181 {
182  int matchedLength;
183 
184  index = KFind::find(text, pattern, index, options, &matchedLength);
185  if (index != -1)
186  {
187  *replacedLength = replace(text, replacement, index, options, matchedLength);
188  if (options & KReplaceDialog::FindBackwards)
189  index--;
190  else
191  index += *replacedLength;
192  }
193  return index;
194 }
195 
196 int KReplace::replace(TQString &text, const TQRegExp &pattern, const TQString &replacement, int index, long options, int *replacedLength)
197 {
198  int matchedLength;
199 
200  index = KFind::find(text, pattern, index, options, &matchedLength);
201  if (index != -1)
202  {
203  *replacedLength = replace(text, replacement, index, options, matchedLength);
204  if (options & KReplaceDialog::FindBackwards)
205  index--;
206  else
207  index += *replacedLength;
208  }
209  return index;
210 }
211 
212 int KReplace::replace(TQString &text, const TQString &replacement, int index, long options, int length)
213 {
214  TQString rep = replacement;
215  // Backreferences: replace \0 with the right portion of 'text'
216  if ( options & KReplaceDialog::BackReference )
217  rep.replace( "\\0", text.mid( index, length ) );
218  // Then replace rep into the text
219  text.replace(index, length, rep);
220  return rep.length();
221 }
222 
223 void KReplace::slotReplaceAll()
224 {
225  doReplace();
226  m_options &= ~KReplaceDialog::PromptOnReplace;
227  emit optionsChanged();
228  emit findNext();
229 }
230 
231 void KReplace::slotSkip()
232 {
233  if (m_options & KReplaceDialog::FindBackwards)
234  m_index--;
235  else
236  m_index++;
237  if ( m_dialogClosed ) {
238  delete m_dialog; // hide it again
239  m_dialog = 0L;
240  } else
241  emit findNext();
242 }
243 
244 void KReplace::slotReplace()
245 {
246  doReplace();
247  if ( m_dialogClosed ) {
248  delete m_dialog; // hide it again
249  m_dialog = 0L;
250  } else
251  emit findNext();
252 }
253 
254 void KReplace::doReplace()
255 {
256  int replacedLength = KReplace::replace(m_text, m_replacement, m_index, m_options, m_matchedLength);
257 
258  // Tell the world about the replacement we made, in case someone wants to
259  // highlight it.
260  emit replace(m_text, m_index, replacedLength, m_matchedLength);
261 #ifdef DEBUG_REPLACE
262  kdDebug() << k_funcinfo << "after replace() signal: m_index=" << m_index << " replacedLength=" << replacedLength << endl;
263 #endif
264  m_replacements++;
265  if (m_options & KReplaceDialog::FindBackwards)
266  m_index--;
267  else {
268  m_index += replacedLength;
269  // when replacing the empty pattern, move on. See also kjs/regexp.cpp for how this should be done for regexps.
270  if ( m_pattern.isEmpty() )
271  ++m_index;
272  }
273 #ifdef DEBUG_REPLACE
274  kdDebug() << k_funcinfo << "after adjustement: m_index=" << m_index << endl;
275 #endif
276 }
277 
278 void KReplace::resetCounts()
279 {
280  KFind::resetCounts();
281  m_replacements = 0;
282 }
283 
284 bool KReplace::shouldRestart( bool forceAsking, bool showNumMatches ) const
285 {
286  // Only ask if we did a "find from cursor", otherwise it's pointless.
287  // ... Or if the prompt-on-replace option was set.
288  // Well, unless the user can modify the document during a search operation,
289  // hence the force boolean.
290  if ( !forceAsking && (m_options & KFindDialog::FromCursor) == 0
291  && (m_options & KReplaceDialog::PromptOnReplace) == 0 )
292  {
293  displayFinalDialog();
294  return false;
295  }
296  TQString message;
297  if ( showNumMatches )
298  {
299  if ( !m_replacements )
300  message = i18n("No text was replaced.");
301  else
302  message = i18n("1 replacement done.", "%n replacements done.", m_replacements );
303  }
304  else
305  {
306  if ( m_options & KFindDialog::FindBackwards )
307  message = i18n( "Beginning of document reached." );
308  else
309  message = i18n( "End of document reached." );
310  }
311 
312  message += "\n";
313  // Hope this word puzzle is ok, it's a different sentence
314  message +=
315  ( m_options & KFindDialog::FindBackwards ) ?
316  i18n("Do you want to restart search from the end?")
317  : i18n("Do you want to restart search at the beginning?");
318 
319  int ret = KMessageBox::questionYesNo( parentWidget(), message, TQString::null, i18n("Restart"), i18n("Stop") );
320  return( ret == KMessageBox::Yes );
321 }
322 
323 void KReplace::closeReplaceNextDialog()
324 {
325  closeFindNextDialog();
326 }
327 
328 #include "kreplace.moc"
KDialogBase
KFindDialog::FromCursor
@ FromCursor
Start from current cursor position.
Definition: kfinddialog.h:90
KFindDialog::RegularExpression
@ RegularExpression
Interpret the pattern as a regular expression.
Definition: kfinddialog.h:94
KFindDialog::FindBackwards
@ FindBackwards
Go backwards.
Definition: kfinddialog.h:93
KFind
A generic implementation of the "find" function.
Definition: kfind.h:105
KFind::validateMatch
virtual bool validateMatch(const TQString &text, int index, int matchedlength)
Virtual method, which allows applications to add extra checks for validating a candidate match.
Definition: kfind.h:244
KFind::Result
Result
Result enum.
Definition: kfind.h:140
KFind::NoMatch
@ NoMatch
No match was found.
Definition: kfind.h:141
KFind::Match
@ Match
A match was found.
Definition: kfind.h:142
KFind::pattern
TQString pattern() const
Definition: kfind.h:209
KFind::closeFindNextDialog
void closeFindNextDialog()
Close the "find next?" dialog.
Definition: kfind.cpp:681
KFind::index
int index() const
Definition: kfind.cpp:688
KFind::optionsChanged
void optionsChanged()
Emitted when the options have changed.
KFind::highlight
void highlight(const TQString &text, int matchingIndex, int matchedLength)
Connect to this signal to implement highlighting of found text during the find operation.
KFind::find
Result find()
Walk the text fragment (e.g.
Definition: kfind.cpp:221
KFind::options
long options() const
Return the current options.
Definition: kfind.h:196
KFind::resetCounts
virtual void resetCounts()
Call this to reset the numMatches count (and the numReplacements count for a KReplace).
Definition: kfind.h:232
KMessageBox::information
static void information(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, const TQString &dontShowAgainName=TQString::null, int options=Notify)
KMessageBox::questionYesNo
static int questionYesNo(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, const KGuiItem &buttonYes=KStdGuiItem::yes(), const KGuiItem &buttonNo=KStdGuiItem::no(), const TQString &dontAskAgainName=TQString::null, int options=Notify)
KReplaceDialog
A generic "replace" dialog.
Definition: kreplacedialog.h:61
KReplaceDialog::PromptOnReplace
@ PromptOnReplace
Should the user be prompted before the replace operation?
Definition: kreplacedialog.h:71
KReplace::displayFinalDialog
virtual void displayFinalDialog() const
Displays the final dialog telling the user how many replacements were made.
Definition: kreplace.cpp:102
KReplace::replaceNextDialog
KDialogBase * replaceNextDialog(bool create=false)
Return (or create) the dialog that shows the "find next?" prompt.
Definition: kreplace.cpp:82
KReplace::resetCounts
virtual void resetCounts()
Call this to reset the numMatches & numReplacements counts.
Definition: kreplace.cpp:278
KReplace::shouldRestart
virtual bool shouldRestart(bool forceAsking=false, bool showNumMatches=true) const
Returns true if we should restart the search from scratch.
Definition: kreplace.cpp:284
KReplace::replace
Result replace()
Walk the text fragment (e.g.
Definition: kreplace.cpp:110
KReplace::closeReplaceNextDialog
void closeReplaceNextDialog()
Close the "replace next?" dialog.
Definition: kreplace.cpp:323
KReplace::KReplace
KReplace(const TQString &pattern, const TQString &replacement, long options, TQWidget *parent=0)
Only use this constructor if you don't use KFindDialog, or if you use it as a modal dialog.
Definition: kreplace.cpp:63
KReplace::~KReplace
virtual ~KReplace()
Destructor.
Definition: kreplace.cpp:77
endl
kndbgstream & endl(kndbgstream &s)
kdDebug
kdbgstream kdDebug(int area=0)
tdelocale.h

tdeutils

Skip menu "tdeutils"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

tdeutils

Skip menu "tdeutils"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeutils by doxygen 1.9.1
This website is maintained by Timothy Pearson.