\section{KFind Class Reference}
\label{classKFind}\index{KFind@{KFind}}
A generic implementation of the \char`\"{}find\char`\"{} function.  


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

Inheritance diagram for KFind:\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=47pt]{classKFind__inherit__graph}
\end{center}
\end{figure}
Collaboration diagram for KFind:\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=193pt]{classKFind__coll__graph}
\end{center}
\end{figure}
\subsection*{Public Types}
\begin{CompactItemize}
\item 
enum {\bf Result} \{ {\bf No\-Match}, 
{\bf Match}
 \}
\end{CompactItemize}
\subsection*{Signals}
\begin{CompactItemize}
\item 
void {\bf highlight} (const  {\bf QString} \&text, int matching\-Index, int matched\-Length)
\begin{CompactList}\small\item\em Connect to this signal to implement highlighting of found text during the find operation. \item\end{CompactList}\item 
void {\bf find\-Next} ()\label{classKFind_l1}

\item 
void {\bf options\-Changed} ()
\begin{CompactList}\small\item\em Emitted when the options have changed. \item\end{CompactList}\item 
void {\bf dialog\-Closed} ()
\begin{CompactList}\small\item\em Emitted when the 'find next' dialog is being closed. \item\end{CompactList}\end{CompactItemize}
\subsection*{Public Member Functions}
\begin{CompactItemize}
\item 
{\bf KFind} (const  {\bf QString} \&pattern, long options, {\bf QWidget} $\ast${\bf parent})\label{classKFind_a0}

\item 
bool {\bf need\-Data} () const 
\item 
void {\bf set\-Data} (const  {\bf QString} \&data, int start\-Pos=-1)
\begin{CompactList}\small\item\em Call this when need\-Data returns true, before calling {\bf find}{\rm (p.\,\pageref{classKFind_a4})}(). \item\end{CompactList}\item 
Result {\bf find} ()
\begin{CompactList}\small\item\em Walk the text fragment (e.g. \item\end{CompactList}\item 
long {\bf options} () const 
\begin{CompactList}\small\item\em Return the current options. \item\end{CompactList}\item 
virtual void {\bf set\-Options} (long options)
\begin{CompactList}\small\item\em Set new options. \item\end{CompactList}\item 
int {\bf num\-Matches} () const 
\begin{CompactList}\small\item\em Return the number of matches found (i.e. \item\end{CompactList}\item 
virtual void {\bf reset\-Counts} ()
\begin{CompactList}\small\item\em Call this to reset the num\-Matches count (and the num\-Replacements count for a {\bf KReplace}{\rm (p.\,\pageref{classKReplace})}). \item\end{CompactList}\item 
virtual bool {\bf validate\-Match} (const  {\bf QString} \&, int, int)
\begin{CompactList}\small\item\em Virtual method, which allows applications to add extra checks for validating a candidate match. \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 saying \char`\"{}no match was found\char`\"{}, if that was the case. \item\end{CompactList}\item 
{\bf KDialog\-Base} $\ast$ {\bf find\-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\-Find\-Next\-Dialog} ()
\begin{CompactList}\small\item\em Close the \char`\"{}find next?\char`\"{} dialog. \item\end{CompactList}\end{CompactItemize}
\subsection*{Static Public Member Functions}
\begin{CompactItemize}
\item 
int {\bf find} (const  {\bf QString} \&text, const  {\bf QString} \&pattern, int index, long options, int $\ast$matchedlength)
\begin{CompactList}\small\item\em Search the given string, and returns whether a match was found. \item\end{CompactList}\item 
int {\bf find} (const  {\bf QString} \&text, const  {\bf QReg\-Exp} \&pattern, int index, long options, int $\ast$matchedlength)\label{classKFind_e1}

\end{CompactItemize}
\subsection*{Protected Slots}
\begin{CompactItemize}
\item 
void {\bf slot\-Find\-Next} ()\label{classKFind_j0}

\item 
void {\bf slot\-Dialog\-Closed} ()\label{classKFind_j1}

\end{CompactItemize}
\subsection*{Protected Member Functions}
\begin{CompactItemize}
\item 
{\bf KFind} (const  {\bf QString} \&pattern, const  {\bf QString} \&replacement, long options, {\bf QWidget} $\ast${\bf parent})\label{classKFind_b0}

\item 
{\bf QWidget} $\ast$ {\bf parent\-Widget} () const \label{classKFind_b1}

\end{CompactItemize}
\subsection*{Friends}
\begin{CompactItemize}
\item 
class {\bf KReplace}\label{classKFind_n0}

\end{CompactItemize}


\subsection{Detailed Description}
A generic implementation of the \char`\"{}find\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 find feature:}}

{\bf {\bf  In the slot connected to the find action, after using {\bf KFind\-Dialog}{\rm (p.\,\pageref{classKFindDialog})}: \small\begin{alltt}\end{alltt}\normalsize 
}}

{\bf {\bf \small\begin{alltt}  // This creates a find-next-prompt dialog if needed.
  m\_find = new KFind(pattern, options);\end{alltt}\normalsize 
}}

{\bf {\bf \small\begin{alltt}  // Connect highlight signal to code which handles highlighting
  // of found text.
  connect( m\_find, 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\_find, SIGNAL( findNext() ),
          this, SLOT( slotFindNext() ) );
 \end{alltt}\normalsize 
}}

{\bf {\bf  Then initialize the variables determining the \char`\"{}current position\char`\"{} (to the cursor, if the option From\-Cursor is set, to the beginning of the selection if the option Selected\-Text is set, and to the beginning of the document otherwise). Initialize the \char`\"{}end of search\char`\"{} variables as well (end of doc or end of selection). Swap begin and end if Find\-Backwards. Finally, call slot\-Find\-Next();}}

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

{\bf {\bf \small\begin{alltt}          // Let KFind inspect the text fragment, and display a dialog if a match is found
          res = m\_find-$>${\bf find()}{\rm (p.\,\pageref{classKFind_a4})};\end{alltt}\normalsize 
}}

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

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

{\bf {\bf  Don't forget delete m\_\-find in the destructor of your class, unless you gave it a parent widget on construction.}}

{\bf {\bf  This implementation allows to have a \char`\"{}Find Next\char`\"{} action, which resumes the search, even if the user closed the \char`\"{}Find Next\char`\"{} dialog.}}

{\bf {\bf  A \char`\"{}Find Previous\char`\"{} action can simply switch temporarily the value of Find\-Backwards and call slot\-Find\-Next() - and reset the value afterwards. }}



Definition at line 97 of file kfind.h.

\subsection{Member Function Documentation}
\index{KFind@{KFind}!needData@{needData}}
\index{needData@{needData}!KFind@{KFind}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}bool KFind::need\-Data () const}\label{classKFind_a2}


\begin{Desc}
\item[Returns:]true if the application must supply a new text fragment It also means the last call returned \char`\"{}No\-Match\char`\"{}. But by storing this here the application doesn't have to store it in a member variable (between calls to slot\-Find\-Next()). \end{Desc}


Definition at line 81 of file kfind.cpp.

References QString::length().\index{KFind@{KFind}!setData@{setData}}
\index{setData@{setData}!KFind@{KFind}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void KFind::set\-Data (const {\bf QString} \& {\em data}, int {\em start\-Pos} = -1)}\label{classKFind_a3}


Call this when need\-Data returns true, before calling {\bf find}{\rm (p.\,\pageref{classKFind_a4})}(). 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em data}]the text fragment (line) \item[{\em start\-Pos}]if set, the index at which the search should start. This is only necessary for the very first call to set\-Data usually, for the 'find in selection' feature. A value of -1 (the default value) means \char`\"{}process all the data\char`\"{}, i.e. either 0 or data.length()-1 depending on Find\-Backwards. \end{description}
\end{Desc}


Definition at line 90 of file kfind.cpp.

References QString::length().\index{KFind@{KFind}!find@{find}}
\index{find@{find}!KFind@{KFind}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}KFind::Result KFind::find ()}\label{classKFind_a4}


Walk the text fragment (e.g. 

text-processor line, kspread cell) looking for matches. For each match, emits the {\bf highlight()}{\rm (p.\,\pageref{classKFind_l0})} signal and displays the find-again dialog proceeding. 

Definition at line 117 of file kfind.cpp.

References find\-Next\-Dialog(), highlight(), QDialog::show(), and validate\-Match().

Referenced by find(), and KReplace::replace().\index{KFind@{KFind}!options@{options}}
\index{options@{options}!KFind@{KFind}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}long KFind::options () const\hspace{0.3cm}{\tt  [inline]}}\label{classKFind_a5}


Return the current options. 

Warning: this is usually the same value as the one passed to the constructor, but options might change \_\-during\_\- the replace operation: e.g. the \char`\"{}All\char`\"{} button resets the Prompt\-On\-Replace flag. 

Definition at line 142 of file kfind.h.\index{KFind@{KFind}!setOptions@{setOptions}}
\index{setOptions@{setOptions}!KFind@{KFind}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void KFind::set\-Options (long {\em options})\hspace{0.3cm}{\tt  [virtual]}}\label{classKFind_a6}


Set new options. 

Usually this is used for setting or clearing the Find\-Backwards options. 

Definition at line 383 of file kfind.cpp.\index{KFind@{KFind}!numMatches@{numMatches}}
\index{numMatches@{numMatches}!KFind@{KFind}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}int KFind::num\-Matches () const\hspace{0.3cm}{\tt  [inline]}}\label{classKFind_a7}


Return the number of matches found (i.e. 

the number of times the {\bf highlight}{\rm (p.\,\pageref{classKFind_l0})} signal was emitted). If 0, can be used in a dialog box to tell the user \char`\"{}no match was found\char`\"{}. The final dialog does so already, unless you used set\-Display\-Final\-Dialog(false). 

Definition at line 156 of file kfind.h.

Referenced by display\-Final\-Dialog().\index{KFind@{KFind}!resetCounts@{resetCounts}}
\index{resetCounts@{resetCounts}!KFind@{KFind}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}virtual void KFind::reset\-Counts ()\hspace{0.3cm}{\tt  [inline, virtual]}}\label{classKFind_a8}


Call this to reset the num\-Matches count (and the num\-Replacements count for a {\bf KReplace}{\rm (p.\,\pageref{classKReplace})}). 

Can be useful if reusing the same {\bf KReplace}{\rm (p.\,\pageref{classKReplace})} for different operations, or when restarting from the beginning of the document. 

Reimplemented in {\bf KReplace} {\rm (p.\,\pageref{classKReplace_a3})}.

Definition at line 164 of file kfind.h.

Referenced by KReplace::reset\-Counts().\index{KFind@{KFind}!validateMatch@{validateMatch}}
\index{validateMatch@{validateMatch}!KFind@{KFind}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}virtual bool KFind::validate\-Match (const {\bf QString} \&, int, int)\hspace{0.3cm}{\tt  [inline, virtual]}}\label{classKFind_a9}


Virtual method, which allows applications to add extra checks for validating a candidate match. 

It's only necessary to reimplement this if the find dialog extension has been used to provide additional criterias.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em text}]The current text fragment \item[{\em index}]The starting index where the candidate match was found \item[{\em matchedlength}]The length of the candidate match \end{description}
\end{Desc}


Definition at line 176 of file kfind.h.

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


Returns true if we should restart the search from scratch. 

Can ask the user, or return false (if we already searched the whole document).

\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 in {\bf KReplace} {\rm (p.\,\pageref{classKReplace_a7})}.

Definition at line 356 of file kfind.cpp.

References display\-Final\-Dialog(), and KMessage\-Box::question\-Yes\-No().\index{KFind@{KFind}!find@{find}}
\index{find@{find}!KFind@{KFind}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}int KFind::find (const {\bf QString} \& {\em text}, const {\bf QString} \& {\em pattern}, int {\em index}, long {\em options}, int $\ast$ {\em matchedlength})\hspace{0.3cm}{\tt  [static]}}\label{classKFind_e0}


Search the given string, and returns whether a match was found. 

If one is, the length of the string matched 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 index}]The starting index into the string. \item[{\em options.}]The options to use. \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 181 of file kfind.cpp.

References QString::find(), find(), QString::find\-Rev(), and QString::length().\index{KFind@{KFind}!displayFinalDialog@{displayFinalDialog}}
\index{displayFinalDialog@{displayFinalDialog}!KFind@{KFind}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void KFind::display\-Final\-Dialog () const\hspace{0.3cm}{\tt  [virtual]}}\label{classKFind_a11}


Displays the final dialog saying \char`\"{}no match was found\char`\"{}, if that was the case. 

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

Reimplemented in {\bf KReplace} {\rm (p.\,\pageref{classKReplace_a8})}.

Definition at line 346 of file kfind.cpp.

References QString::arg(), KMessage\-Box::information(), and num\-Matches().

Referenced by should\-Restart().\index{KFind@{KFind}!findNextDialog@{findNextDialog}}
\index{findNextDialog@{findNextDialog}!KFind@{KFind}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}{\bf KDialog\-Base} $\ast$ KFind::find\-Next\-Dialog (bool {\em create} = false)}\label{classKFind_a12}


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 106 of file kfind.cpp.

References QObject::connect(), and KStd\-Action::create().

Referenced by find().\index{KFind@{KFind}!closeFindNextDialog@{closeFindNextDialog}}
\index{closeFindNextDialog@{closeFindNextDialog}!KFind@{KFind}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void KFind::close\-Find\-Next\-Dialog ()}\label{classKFind_a13}


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

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

Definition at line 388 of file kfind.cpp.

Referenced by KReplace::close\-Replace\-Next\-Dialog().\index{KFind@{KFind}!highlight@{highlight}}
\index{highlight@{highlight}!KFind@{KFind}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void KFind::highlight (const {\bf QString} \& {\em text}, int {\em matching\-Index}, int {\em matched\-Length})\hspace{0.3cm}{\tt  [signal]}}\label{classKFind_l0}


Connect to this signal to implement highlighting of found text during the find operation. 



Referenced by find(), and KReplace::replace().\index{KFind@{KFind}!optionsChanged@{optionsChanged}}
\index{optionsChanged@{optionsChanged}!KFind@{KFind}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void KFind::options\-Changed ()\hspace{0.3cm}{\tt  [signal]}}\label{classKFind_l2}


Emitted when the options have changed. 

This can happen e.g. with \char`\"{}Replace All\char`\"{}, or if our 'find next' dialog gets a \char`\"{}find previous\char`\"{} one day. \index{KFind@{KFind}!dialogClosed@{dialogClosed}}
\index{dialogClosed@{dialogClosed}!KFind@{KFind}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void KFind::dialog\-Closed ()\hspace{0.3cm}{\tt  [signal]}}\label{classKFind_l3}


Emitted when the 'find next' dialog is being closed. 

Some apps might want to remove the highlighted text when this happens. Apps without support for \char`\"{}Find Next\char`\"{} can also do m\_\-find-$>${\bf delete\-Later()} to terminate the find operation. 

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