.TH "KReplace" 3 "28 Jan 2004" "kutils Library" \" -*- nroff -*-
.ad l
.nh
.SH NAME
KReplace \- A generic implementation of the 'replace' function.  

.PP
.SH SYNOPSIS
.br
.PP
\fC#include <kreplace.h>\fP
.PP
Inherits \fBKFind\fP.
.PP
.SS "Signals"

.in +1c
.ti -1c
.RI "void \fBreplace\fP (const  \fBQString\fP &text, int replacementIndex, int replacedLength, int matchedLength)"
.br
.RI "\fIConnect to this slot to implement updating of replaced text during the replace operation. \fP"
.in -1c
.SS "Public Member Functions"

.in +1c
.ti -1c
.RI "\fBKReplace\fP (const  \fBQString\fP &pattern, const  \fBQString\fP &replacement, long options, \fBQWidget\fP *\fBparent\fP=0)"
.br
.RI "\fIWill create a prompt dialog and use it as needed. \fP"
.ti -1c
.RI "int \fBnumReplacements\fP () const "
.br
.RI "\fIReturn the number of replacements made (i.e. \fP"
.ti -1c
.RI "virtual void \fBresetCounts\fP ()"
.br
.RI "\fICall this to reset the numMatches & numReplacements counts. \fP"
.ti -1c
.RI "Result \fBreplace\fP ()"
.br
.RI "\fIWalk the text fragment (e.g. \fP"
.ti -1c
.RI "\fBKDialogBase\fP * \fBreplaceNextDialog\fP (bool create=false)"
.br
.RI "\fIReturn (or create) the dialog that shows the 'find next?' prompt. \fP"
.ti -1c
.RI "void \fBcloseReplaceNextDialog\fP ()"
.br
.RI "\fIClose the 'replace next?' dialog. \fP"
.ti -1c
.RI "virtual bool \fBshouldRestart\fP (bool forceAsking=false, bool showNumMatches=true) const "
.br
.RI "\fIReturns true if we should restart the search from scratch. \fP"
.ti -1c
.RI "virtual void \fBdisplayFinalDialog\fP () const "
.br
.RI "\fIDisplays the final dialog telling the user how many replacements were made. \fP"
.in -1c
.SS "Static Public Member Functions"

.in +1c
.ti -1c
.RI "int \fBreplace\fP (\fBQString\fP &text, const  \fBQString\fP &pattern, const  \fBQString\fP &replacement, int index, long options, int *replacedLength)"
.br
.RI "\fISearch the given string, replaces with the given replacement string, and returns whether a match was found. \fP"
.ti -1c
.RI "int \fBreplace\fP (\fBQString\fP &text, const  \fBQRegExp\fP &pattern, const  \fBQString\fP &replacement, int index, long options, int *replacedLength)"
.br
.in -1c
.SS "Protected Slots"

.in +1c
.ti -1c
.RI "void \fBslotSkip\fP ()"
.br
.ti -1c
.RI "void \fBslotReplace\fP ()"
.br
.ti -1c
.RI "void \fBslotReplaceAll\fP ()"
.br
.in -1c
.SH "Detailed Description"
.PP 
A generic implementation of the 'replace' function. 

\fBAuthor:\fP
.RS 4
S.R.Haque <srhaque@iee.org>, David Faure <faure@kde.org>
.RE
.PP
.PP
\fB Detail\fP
.PP
\fB This class includes prompt handling etc. Also provides some static functions which can be used to create custom behaviour instead of using the class directly.\fP
.PP
\fB \fP
.PP
\fB\fB Example\fP\fP
.PP
\fB\fB To use the class to implement a complete replace feature:\fP\fP
.PP
\fB\fB In the slot connect to the replace action, after using \fBKReplaceDialog\fP: 
.PP
.nf

.PP
\fP\fP
.PP
\fB\fB
.PP
.nf
  // This creates a replace-on-prompt dialog if needed.
  m_replace = new KReplace(pattern, replacement, options);
.PP
\fP\fP
.PP
\fB\fB
.PP
.nf
  // Connect signals to code which handles highlighting
  // of found text, and on-the-fly replacement.
  connect( m_replace, SIGNAL( \fBhighlight( const QString &, int, int )\fP ),
          this, SLOT( slotHighlight( const QString &, int, int ) ) );
  // Connect findNext signal - called when pressing the button in the dialog
  connect( m_replace, SIGNAL( findNext() ),
          this, SLOT( slotReplaceNext() ) );
  // Connect replace signal - called when doing a replacement
  connect( m_replace, SIGNAL( \fBreplace(const QString &, int, int, int)\fP ),
          this, SLOT( slotReplace(const QString &, int, int, int) ) );
.PP
\fP\fP
.PP
\fB\fB
.PP
.nf
  Then initialize the variables determining the 'current position'
  (to the cursor, if the option FromCursor is set,
   to the beginning of the selection if the option SelectedText is set,
   and to the beginning of the document otherwise).
  Initialize the 'end of search' variables as well (end of doc or end of selection).
  Swap begin and end if FindBackwards.
  Finally, call slotReplaceNext();
.PP
\fP\fP
.PP
\fB\fB
.PP
.nf
 
.PP
.nf

  void slotReplaceNext()
  {
      KFind::Result res = KFind::NoMatch;
      while ( res == KFind::NoMatch &&  ) {
          if ( m_replace->\fBneedData()\fP )
              m_replace->setData( <current text fragment> );
.PP
.PP
\fP\fP
.PP
\fB\fB
.PP
.nf

.PP
.nf
          // Let KReplace inspect the text fragment, and display a dialog if a match is found
          res = m_replace->res();
.PP
.PP
\fP\fP
.PP
\fB\fB
.PP
.nf

.PP
.nf
          if ( res == KFind::NoMatch ) {
              <Move to the next non-empty text fragment, honouring the FindBackwards setting for the direction>
          }
      }
.PP
.PP
\fP\fP
.PP
\fB\fB
.PP
.nf

.PP
.nf
      if ( res == KFind::NoMatch ) // i.e. at end
          <Call either  m_replace->\fBdisplayFinalDialog()\fP; delete m_replace; m_replace = 0L;
           or           if ( m_replace->\fBshouldRestart()\fP ) { reinit and call slotReplaceNext(); }
                        else { delete m_replace; m_replace = 0L; }>
  }
 
.PP
.PP
\fP\fP
.PP
\fB\fB
.PP
.nf
 Don't forget delete m_find in the destructor of your class,
 unless you gave it a parent widget on construction.
.PP
\fP\fP
.PP
\fB\fB
.PP
.nf
 
.PP
 \fP\fP
.PP
Definition at line 96 of file kreplace.h.
.SH "Constructor & Destructor Documentation"
.PP 
.SS "KReplace::KReplace (const \fBQString\fP & pattern, const \fBQString\fP & replacement, long options, \fBQWidget\fP * parent = 0)"
.PP
Will create a prompt dialog and use it as needed. Definition at line 63 of file kreplace.cpp.
.SH "Member Function Documentation"
.PP 
.SS "int KReplace::numReplacements () const\fC [inline]\fP"
.PP
Return the number of replacements made (i.e. the number of times the \fBreplace\fP signal was emitted). Can be used in a dialog box to tell the user how many replacements were made. The final dialog does so already, unless you used setDisplayFinalDialog(false). Definition at line 113 of file kreplace.h.
.SS "void KReplace::resetCounts ()\fC [virtual]\fP"
.PP
Call this to reset the numMatches & numReplacements counts. Can be useful if reusing the same KReplace for different operations, or when restarting from the beginning of the document. Reimplemented from \fBKFind\fP.
.PP
Definition at line 273 of file kreplace.cpp.
.PP
References KFind::resetCounts().
.SS "KFind::Result KReplace::replace ()"
.PP
Walk the text fragment (e.g. kwrite line, kspread cell) looking for matches. For each match, if prompt-on-replace is specified, emits the \fBhighlight()\fP signal and displays the prompt-for-replace dialog before doing the replace. Definition at line 103 of file kreplace.cpp.
.PP
References KFind::find(), KFind::highlight(), and KFind::validateMatch().
.PP
Referenced by replace().
.SS "\fBKDialogBase\fP * KReplace::replaceNextDialog (bool create = false)"
.PP
Return (or create) the dialog that shows the 'find next?' prompt. Usually you don't need to call this. One case where it can be useful, is when the user selects the 'Find' menu item while a find operation is under way. In that case, the program may want to call setActiveWindow() on that dialog. Definition at line 75 of file kreplace.cpp.
.PP
References KStdAction::create().
.SS "void KReplace::closeReplaceNextDialog ()"
.PP
Close the 'replace next?' dialog. The application should do this when the last match was hit. If the application deletes the KReplace, then 'find previous' won't be possible anymore. Definition at line 303 of file kreplace.cpp.
.PP
References KFind::closeFindNextDialog().
.SS "int KReplace::replace (\fBQString\fP & text, const \fBQString\fP & pattern, const \fBQString\fP & replacement, int index, long options, int * replacedLength)\fC [static]\fP"
.PP
Search the given string, replaces with the given replacement string, and returns whether a match was found. If one is, the replacement string length is also returned.
.PP
A performance optimised version of the function is provided for use with regular expressions.
.PP
\fBParameters:\fP
.RS 4
\fItext\fP The string to search. 
.br
\fIpattern\fP The pattern to look for. 
.br
\fIreplacement\fP The replacement string to insert into the text. 
.br
\fIindex\fP The starting index into the string. 
.br
\fIoptions\fP The options to use. 
.br
\fIreplacedLength\fP Output parameter, contains the length of the replaced string. Not always the same as replacement.length(), when backreferences are used. 
.RE
.PP
\fBReturns:\fP
.RS 4
The index at which a match was found, or -1 if no match was found. 
.RE
.PP
Definition at line 180 of file kreplace.cpp.
.PP
References KFind::find(), and replace().
.SS "bool KReplace::shouldRestart (bool forceAsking = false, bool showNumMatches = true) const\fC [virtual]\fP"
.PP
Returns true if we should restart the search from scratch. Can ask the user, or return false (if we already searched/replaced the whole document without the PromptOnReplace option).
.PP
\fBParameters:\fP
.RS 4
\fIforceAsking\fP set to true if the user modified the document during the search. In that case it makes sense to restart the search again.
.br
\fIshowNumMatches\fP set to true if the dialog should show the number of matches. Set to false if the application provides a 'find previous' action, in which case the match count will be erroneous when hitting the end, and we could even be hitting the beginning of the document (so not all matches have even been seen). 
.RE
.PP
Reimplemented from \fBKFind\fP.
.PP
Definition at line 279 of file kreplace.cpp.
.PP
References displayFinalDialog(), and KMessageBox::questionYesNo().
.SS "void KReplace::displayFinalDialog () const\fC [virtual]\fP"
.PP
Displays the final dialog telling the user how many replacements were made. Call either this or \fBshouldRestart()\fP. Reimplemented from \fBKFind\fP.
.PP
Definition at line 95 of file kreplace.cpp.
.PP
References KMessageBox::information().
.PP
Referenced by shouldRestart().
.SS "void KReplace::replace (const \fBQString\fP & text, int replacementIndex, int replacedLength, int matchedLength)\fC [signal]\fP"
.PP
Connect to this slot to implement updating of replaced text during the replace operation. Extra care must be taken to properly implement the 'no prompt-on-replace' case. For instance \fBhighlight\fP isn't emitted in that case (some code might rely on it), and for performance reasons one should repaint after \fBreplace()\fP ONLY if prompt-on-replace was selected.
.PP
\fBParameters:\fP
.RS 4
\fItext\fP The text, in which the replacement has already been done. 
.br
\fIreplacementIndex\fP Starting index of the matched substring 
.br
\fIreplacedLength\fP Length of the replacement string 
.br
\fImatchedLength\fP Length of the matched string 
.RE
.PP


.SH "Author"
.PP 
Generated automatically by Doxygen for kutils Library from the source code.
