\section{KReplace Class Reference}
\label{classKReplace}\index{KReplace@{KReplace}}
A generic implementation of the \char`\"{}replace\char`\"{} function.  


{\tt \#include $<$kreplace.h$>$}

Inheritance diagram for KReplace:\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=47pt]{classKReplace__inherit__graph}
\end{center}
\end{figure}
Collaboration diagram for KReplace:\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=236pt]{classKReplace__coll__graph}
\end{center}
\end{figure}
\subsection*{Signals}
\begin{CompactItemize}
\item 
void {\bf replace} (const  {\bf QString} \&text, int replacement\-Index, int replaced\-Length, int matched\-Length)
\begin{CompactList}\small\item\em Connect to this slot to implement updating of replaced text during the replace operation. \item\end{CompactList}\end{CompactItemize}
\subsection*{Public Member Functions}
\begin{CompactItemize}
\item 
{\bf KReplace} (const  {\bf QString} \&pattern, const  {\bf QString} \&replacement, long options, {\bf QWidget} $\ast${\bf parent}=0)
\begin{CompactList}\small\item\em Will create a prompt dialog and use it as needed. \item\end{CompactList}\item 
int {\bf num\-Replacements} () const 
\begin{CompactList}\small\item\em Return the number of replacements made (i.e. \item\end{CompactList}\item 
virtual void {\bf reset\-Counts} ()
\begin{CompactList}\small\item\em Call this to reset the num\-Matches \& num\-Replacements counts. \item\end{CompactList}\item 
Result {\bf replace} ()
\begin{CompactList}\small\item\em Walk the text fragment (e.g. \item\end{CompactList}\item 
{\bf KDialog\-Base} $\ast$ {\bf replace\-Next\-Dialog} (bool create=false)
\begin{CompactList}\small\item\em Return (or create) the dialog that shows the \char`\"{}find next?\char`\"{} prompt. \item\end{CompactList}\item 
void {\bf close\-Replace\-Next\-Dialog} ()
\begin{CompactList}\small\item\em Close the \char`\"{}replace next?\char`\"{} dialog. \item\end{CompactList}\item 
virtual bool {\bf should\-Restart} (bool force\-Asking=false, bool show\-Num\-Matches=true) const 
\begin{CompactList}\small\item\em Returns true if we should restart the search from scratch. \item\end{CompactList}\item 
virtual void {\bf display\-Final\-Dialog} () const 
\begin{CompactList}\small\item\em Displays the final dialog telling the user how many replacements were made. \item\end{CompactList}\end{CompactItemize}
\subsection*{Static Public Member Functions}
\begin{CompactItemize}
\item 
int {\bf replace} ({\bf QString} \&text, const  {\bf QString} \&pattern, const  {\bf QString} \&replacement, int index, long options, int $\ast$replaced\-Length)
\begin{CompactList}\small\item\em Search the given string, replaces with the given replacement string, and returns whether a match was found. \item\end{CompactList}\item 
int {\bf replace} ({\bf QString} \&text, const  {\bf QReg\-Exp} \&pattern, const  {\bf QString} \&replacement, int index, long options, int $\ast$replaced\-Length)\label{classKReplace_e1}

\end{CompactItemize}
\subsection*{Protected Slots}
\begin{CompactItemize}
\item 
void {\bf slot\-Skip} ()\label{classKReplace_j0}

\item 
void {\bf slot\-Replace} ()\label{classKReplace_j1}

\item 
void {\bf slot\-Replace\-All} ()\label{classKReplace_j2}

\end{CompactItemize}


\subsection{Detailed Description}
A generic implementation of the \char`\"{}replace\char`\"{} function. 

\begin{Desc}
\item[Author:]S.R.Haque $<${\tt srhaque@iee.org}$>$, David Faure $<${\tt faure@kde.org}$>$\end{Desc}


{\bf  Detail}

{\bf  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.}

{\bf  }

{\bf {\bf  Example}}

{\bf {\bf  To use the class to implement a complete replace feature:}}

{\bf {\bf  In the slot connect to the replace action, after using {\bf KReplace\-Dialog}{\rm (p.\,\pageref{classKReplaceDialog})}: \small\begin{alltt}\end{alltt}\normalsize 
}}

{\bf {\bf \small\begin{alltt}  // This creates a replace-on-prompt dialog if needed.
  m\_replace = new KReplace(pattern, replacement, options);\end{alltt}\normalsize 
}}

{\bf {\bf \small\begin{alltt}  // Connect signals to code which handles highlighting
  // of found text, and on-the-fly replacement.
  connect( m\_replace, SIGNAL( {\bf highlight( const QString &, int, int )}{\rm (p.\,\pageref{classKFind_l0})} ),
          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( {\bf replace(const QString &, int, int, int)}{\rm (p.\,\pageref{classKReplace_l0})} ),
          this, SLOT( slotReplace(const QString &, int, int, int) ) );\end{alltt}\normalsize 
}}

{\bf {\bf \small\begin{alltt}  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();\end{alltt}\normalsize 
}}

{\bf {\bf \small\begin{alltt} \small\begin{alltt}
  void slotReplaceNext()
  \{
      KFind::Result res = KFind::NoMatch;
      while ( res == KFind::NoMatch \&\&  ) \{
          if ( m\_replace-$>${\bf needData()}{\rm (p.\,\pageref{classKFind_a2})} )
              m\_replace-$>$setData( <current text fragment> );\end{alltt}\normalsize 
\end{alltt}\normalsize 
}}

{\bf {\bf \small\begin{alltt}\small\begin{alltt}          // Let KReplace inspect the text fragment, and display a dialog if a match is found
          res = m\_replace-$>$res();\end{alltt}\normalsize 
\end{alltt}\normalsize 
}}

{\bf {\bf \small\begin{alltt}\small\begin{alltt}          if ( res == KFind::NoMatch ) \{
              $<$Move to the next non-empty text fragment, honouring the FindBackwards setting for the direction$>$
          \}
      \}\end{alltt}\normalsize 
\end{alltt}\normalsize 
}}

{\bf {\bf \small\begin{alltt}\small\begin{alltt}      if ( res == KFind::NoMatch ) // i.e. at end
          $<$Call either  m\_replace-$>${\bf displayFinalDialog()}{\rm (p.\,\pageref{classKReplace_a8})}; delete m\_replace; m\_replace = 0L;
           or           if ( m\_replace-$>${\bf shouldRestart()}{\rm (p.\,\pageref{classKReplace_a7})} ) \{ reinit and call slotReplaceNext(); \}
                        else \{ delete m\_replace; m\_replace = 0L; \}$>$
  \}
 \end{alltt}\normalsize 
\end{alltt}\normalsize 
}}

{\bf {\bf \small\begin{alltt} Don't forget delete m\_find in the destructor of your class,
 unless you gave it a parent widget on construction.\end{alltt}\normalsize 
}}

{\bf {\bf \small\begin{alltt} \end{alltt}\normalsize 
 }}



Definition at line 96 of file kreplace.h.

\subsection{Constructor \& Destructor Documentation}
\index{KReplace@{KReplace}!KReplace@{KReplace}}
\index{KReplace@{KReplace}!KReplace@{KReplace}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}KReplace::KReplace (const {\bf QString} \& {\em pattern}, const {\bf QString} \& {\em replacement}, long {\em options}, {\bf QWidget} $\ast$ {\em parent} = 0)}\label{classKReplace_a0}


Will create a prompt dialog and use it as needed. 



Definition at line 63 of file kreplace.cpp.

\subsection{Member Function Documentation}
\index{KReplace@{KReplace}!numReplacements@{numReplacements}}
\index{numReplacements@{numReplacements}!KReplace@{KReplace}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}int KReplace::num\-Replacements () const\hspace{0.3cm}{\tt  [inline]}}\label{classKReplace_a2}


Return the number of replacements made (i.e. 

the number of times the {\bf replace}{\rm (p.\,\pageref{classKReplace_a4})} 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 set\-Display\-Final\-Dialog(false). 

Definition at line 113 of file kreplace.h.\index{KReplace@{KReplace}!resetCounts@{resetCounts}}
\index{resetCounts@{resetCounts}!KReplace@{KReplace}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void KReplace::reset\-Counts ()\hspace{0.3cm}{\tt  [virtual]}}\label{classKReplace_a3}


Call this to reset the num\-Matches \& num\-Replacements counts. 

Can be useful if reusing the same KReplace for different operations, or when restarting from the beginning of the document. 

Reimplemented from {\bf KFind} {\rm (p.\,\pageref{classKFind_a8})}.

Definition at line 273 of file kreplace.cpp.

References KFind::reset\-Counts().\index{KReplace@{KReplace}!replace@{replace}}
\index{replace@{replace}!KReplace@{KReplace}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}KFind::Result KReplace::replace ()}\label{classKReplace_a4}


Walk the text fragment (e.g. 

kwrite line, kspread cell) looking for matches. For each match, if prompt-on-replace is specified, emits the {\bf highlight()}{\rm (p.\,\pageref{classKFind_l0})} signal and displays the prompt-for-replace dialog before doing the replace. 

Definition at line 103 of file kreplace.cpp.

References KFind::find(), KFind::highlight(), and KFind::validate\-Match().

Referenced by replace().\index{KReplace@{KReplace}!replaceNextDialog@{replaceNextDialog}}
\index{replaceNextDialog@{replaceNextDialog}!KReplace@{KReplace}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}{\bf KDialog\-Base} $\ast$ KReplace::replace\-Next\-Dialog (bool {\em create} = false)}\label{classKReplace_a5}


Return (or create) the dialog that shows the \char`\"{}find next?\char`\"{} prompt. 

Usually you don't need to call this. One case where it can be useful, is when the user selects the \char`\"{}Find\char`\"{} menu item while a find operation is under way. In that case, the program may want to call set\-Active\-Window() on that dialog. 

Definition at line 75 of file kreplace.cpp.

References KStd\-Action::create().\index{KReplace@{KReplace}!closeReplaceNextDialog@{closeReplaceNextDialog}}
\index{closeReplaceNextDialog@{closeReplaceNextDialog}!KReplace@{KReplace}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void KReplace::close\-Replace\-Next\-Dialog ()}\label{classKReplace_a6}


Close the \char`\"{}replace next?\char`\"{} dialog. 

The application should do this when the last match was hit. If the application deletes the KReplace, then \char`\"{}find previous\char`\"{} won't be possible anymore. 

Definition at line 303 of file kreplace.cpp.

References KFind::close\-Find\-Next\-Dialog().\index{KReplace@{KReplace}!replace@{replace}}
\index{replace@{replace}!KReplace@{KReplace}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}int KReplace::replace ({\bf QString} \& {\em text}, const {\bf QString} \& {\em pattern}, const {\bf QString} \& {\em replacement}, int {\em index}, long {\em options}, int $\ast$ {\em replaced\-Length})\hspace{0.3cm}{\tt  [static]}}\label{classKReplace_e0}


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.

A performance optimised version of the function is provided for use with regular expressions.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em text}]The string to search. \item[{\em pattern}]The pattern to look for. \item[{\em replacement}]The replacement string to insert into the text. \item[{\em index}]The starting index into the string. \item[{\em options}]The options to use. \item[{\em replaced\-Length}]Output parameter, contains the length of the replaced string. Not always the same as replacement.length(), when backreferences are used. \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]The index at which a match was found, or -1 if no match was found. \end{Desc}


Definition at line 180 of file kreplace.cpp.

References KFind::find(), and replace().\index{KReplace@{KReplace}!shouldRestart@{shouldRestart}}
\index{shouldRestart@{shouldRestart}!KReplace@{KReplace}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}bool KReplace::should\-Restart (bool {\em force\-Asking} = false, bool {\em show\-Num\-Matches} = true) const\hspace{0.3cm}{\tt  [virtual]}}\label{classKReplace_a7}


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 Prompt\-On\-Replace option).

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em force\-Asking}]set to true if the user modified the document during the search. In that case it makes sense to restart the search again.\item[{\em show\-Num\-Matches}]set to true if the dialog should show the number of matches. Set to false if the application provides a \char`\"{}find previous\char`\"{} 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). \end{description}
\end{Desc}


Reimplemented from {\bf KFind} {\rm (p.\,\pageref{classKFind_a10})}.

Definition at line 279 of file kreplace.cpp.

References display\-Final\-Dialog(), and KMessage\-Box::question\-Yes\-No().\index{KReplace@{KReplace}!displayFinalDialog@{displayFinalDialog}}
\index{displayFinalDialog@{displayFinalDialog}!KReplace@{KReplace}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void KReplace::display\-Final\-Dialog () const\hspace{0.3cm}{\tt  [virtual]}}\label{classKReplace_a8}


Displays the final dialog telling the user how many replacements were made. 

Call either this or {\bf should\-Restart()}{\rm (p.\,\pageref{classKReplace_a7})}. 

Reimplemented from {\bf KFind} {\rm (p.\,\pageref{classKFind_a11})}.

Definition at line 95 of file kreplace.cpp.

References KMessage\-Box::information().

Referenced by should\-Restart().\index{KReplace@{KReplace}!replace@{replace}}
\index{replace@{replace}!KReplace@{KReplace}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void KReplace::replace (const {\bf QString} \& {\em text}, int {\em replacement\-Index}, int {\em replaced\-Length}, int {\em matched\-Length})\hspace{0.3cm}{\tt  [signal]}}\label{classKReplace_l0}


Connect to this slot to implement updating of replaced text during the replace operation. 

Extra care must be taken to properly implement the \char`\"{}no prompt-on-replace\char`\"{} case. For instance {\bf highlight}{\rm (p.\,\pageref{classKFind_l0})} isn't emitted in that case (some code might rely on it), and for performance reasons one should repaint after {\bf replace()}{\rm (p.\,\pageref{classKReplace_a4})} ONLY if prompt-on-replace was selected.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em text}]The text, in which the replacement has already been done. \item[{\em replacement\-Index}]Starting index of the matched substring \item[{\em replaced\-Length}]Length of the replacement string \item[{\em matched\-Length}]Length of the matched string \end{description}
\end{Desc}


The documentation for this class was generated from the following files:\begin{CompactItemize}
\item 
kreplace.h\item 
kreplace.cpp\end{CompactItemize}
