org.kde.koala

Class KFind

public class KFind extends QObject

Author: S.R.Haque , David Faure , Arend van Beelen jr.

UNKNOWN: A generic implementation of the "find" function. etail: This class includes prompt handling etc. Also provides some static functions which can be used to create custom behavior instead of using the class directly. xample: To use the class to implement a complete find feature: In the slot connected to the find action, after using KFindDialog:

  // This creates a find-next-prompt dialog if needed.
  m_find = new KFind(pattern, options, this);
  // Connect highlight signal to code which handles highlighting
  // of found text.
  connect( m_find, SIGNAL("highlight( String, int, int )"),
          this, SLOT("slotHighlight( String, int, int )") );
  // Connect findNext signal - called when pressing the button in the dialog
  connect( m_find, SIGNAL("findNext()"),
          this, SLOT("slotFindNext()") );
 
If you are using a non-modal find dialog (the recommended new way in KDE-3.2), you should call right away m_find.closeFindNextDialog(). 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 slotFindNext();
  void slotFindNext()
  {
      KFind.Result res = KFind.NoMatch;
      while ( res == KFind.NoMatch &&  ) {
          if ( m_find.needData() )
              m_find.setData(  );
          // Let KFind inspect the text fragment, and display a dialog if a match is found
          res = m_find.find();
          if ( res == KFind.NoMatch ) {
              
          }
      }
      if ( res == KFind.NoMatch ) // i.e. at end
          
  }
 
Don't forget to delete m_find in the destructor of your class, unless you gave it a parent widget on construction. This implementation allows to have a "Find Next" action, which resumes the search, even if the user closed the "Find Next" dialog. A "Find Previous" action can simply switch temporarily the value of FindBackwards and call slotFindNext() - and reset the value afterwards. See KFindSignals for signals emitted by KFind @brief A generic implementation of the "find" function.

Field Summary
static intMatch
static intNoMatch
Constructor Summary
protected KFind(Class dummy)
KFind(String pattern, long options, QWidget parent)
Only use this constructor if you don't use KFindDialog, or if you use it as a modal dialog.
Method Summary
StringclassName()
voidcloseFindNextDialog()
Close the "find next?
protected QWidgetdialogsParent()
voiddisplayFinalDialog()
Displays the final dialog saying "no match was found", if that was the case.
voiddispose()
Delete the wrapped C++ instance ahead of finalize()
protected voidfinalize()
Deletes the wrapped C++ instance
intfind()
Walk the text fragment (e.g. text-processor line, kspread cell) looking for matches.
static intfind(String text, String pattern, int index, long options, int[] matchedlength)
Search the given string, and returns whether a match was found.
static intfind(String text, QRegExp pattern, int index, long options, int[] matchedlength)
KDialogBasefindNextDialog(boolean create)
Return (or create) the dialog that shows the "find next?
KDialogBasefindNextDialog()
intindex()
booleanisDisposed()
Has the wrapped C++ instance been deleted?
QMetaObjectmetaObject()
booleanneedData()
intnumMatches()
Return the number of matches found (i.e. the number of times the highlight signal was emitted).
longoptions()
Return the current options.
protected QWidgetparentWidget()
Stringpattern()
voidresetCounts()
Call this to reset the numMatches count (and the numReplacements count for a KReplace).
voidsetData(String data, int startPos)
Call this when needData returns true, before calling find().
voidsetData(String data)
voidsetData(int id, String data, int startPos)
Call this when needData returns true, before calling find().
voidsetData(int id, String data)
voidsetOptions(long options)
Set new options.
voidsetPattern(String pattern)
Change the pattern we're looking for
booleanshouldRestart(boolean forceAsking, boolean showNumMatches)
Returns true if we should restart the search from scratch.
booleanshouldRestart(boolean forceAsking)
booleanshouldRestart()
protected voidslotDialogClosed()
protected voidslotFindNext()
booleanvalidateMatch(String text, int index, int matchedlength)
Virtual method, which allows applications to add extra checks for validating a candidate match.

Field Detail

Match

public static final int Match

NoMatch

public static final int NoMatch

Constructor Detail

KFind

protected KFind(Class dummy)

KFind

public KFind(String pattern, long options, QWidget parent)
Only use this constructor if you don't use KFindDialog, or if you use it as a modal dialog.

UNKNOWN: Only use this constructor if you don't use KFindDialog, or if you use it as a modal dialog.

Method Detail

className

public String className()

closeFindNextDialog

public void closeFindNextDialog()
Close the "find next?" dialog. The application should do this when the last match was hit. If the application deletes the KFind, then "find previous" won't be possible anymore. IMPORTANT: you should also call this if you are using a non-modal find dialog, to tell KFind not to pop up its own dialog.

UNKNOWN: Close the "find next?" dialog.

dialogsParent

protected QWidget dialogsParent()

displayFinalDialog

public void displayFinalDialog()
Displays the final dialog saying "no match was found", if that was the case. Call either this or shouldRestart().

UNKNOWN: Displays the final dialog saying "no match was found", if that was the case.

dispose

public void dispose()
Delete the wrapped C++ instance ahead of finalize()

finalize

protected void finalize()
Deletes the wrapped C++ instance

find

public int find()
Walk the text fragment (e.g. text-processor line, kspread cell) looking for matches. For each match, emits the highlight() signal and displays the find-again dialog proceeding.

UNKNOWN: Walk the text fragment (e.

find

public static int find(String text, String pattern, int index, long options, int[] matchedlength)
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.

Parameters: text The string to search. pattern The pattern to look for. index The starting index into the string. options The options to use. matchedlength The length of the string that was matched

Returns: The index at which a match was found, or -1 if no match was found.

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

find

public static int find(String text, QRegExp pattern, int index, long options, int[] matchedlength)

findNextDialog

public KDialogBase findNextDialog(boolean create)
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.

UNKNOWN: Return (or create) the dialog that shows the "find next?" prompt.

findNextDialog

public KDialogBase findNextDialog()

index

public int index()

Returns: the current matching index ( or -1 ). Same as the matchingIndex parameter passed to highlight. You usually don't need to use this, except maybe when updating the current data, so you need to call setData( newData, index() ).

UNKNOWN:

isDisposed

public boolean isDisposed()
Has the wrapped C++ instance been deleted?

metaObject

public QMetaObject metaObject()

needData

public boolean needData()

Returns: true if the application must supply a new text fragment It also means the last call returned "NoMatch". But by storing this here the application doesn't have to store it in a member variable (between calls to slotFindNext()).

UNKNOWN:

numMatches

public int numMatches()
Return the number of matches found (i.e. the number of times the highlight signal was emitted). If 0, can be used in a dialog box to tell the user "no match was found". The final dialog does so already, unless you used setDisplayFinalDialog(false).

UNKNOWN: Return the number of matches found (i.

options

public long options()
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 "All" button resets the PromptOnReplace flag.

UNKNOWN: Return the current options.

parentWidget

protected QWidget parentWidget()

pattern

public String pattern()

Returns: the pattern we're currently looking for

UNKNOWN:

resetCounts

public void resetCounts()
Call this to reset the numMatches count (and the numReplacements count for a KReplace). Can be useful if reusing the same KReplace for different operations, or when restarting from the beginning of the document.

UNKNOWN: Call this to reset the numMatches count (and the numReplacements count for a KReplace).

setData

public void setData(String data, int startPos)
Call this when needData returns true, before calling find().

Parameters: data the text fragment (line) startPos if set, the index at which the search should start. This is only necessary for the very first call to setData usually, for the 'find in selection' feature. A value of -1 (the default value) means "process all the data", i.e. either 0 or data.length()-1 depending on FindBackwards.

UNKNOWN: Call this when needData returns true, before calling find().

setData

public void setData(String data)

setData

public void setData(int id, String data, int startPos)
Call this when needData returns true, before calling find(). The use of ID's is especially useful if you're using the FindIncremental option.

Parameters: id the id of the text fragment data the text fragment (line) startPos if set, the index at which the search should start. This is only necessary for the very first call to setData usually, for the 'find in selection' feature. A value of -1 (the default value) means "process all the data", i.e. either 0 or data.length()-1 depending on FindBackwards.

UNKNOWN: Call this when needData returns true, before calling find().

setData

public void setData(int id, String data)

setOptions

public void setOptions(long options)
Set new options. Usually this is used for setting or clearing the FindBackwards options.

UNKNOWN: Set new options.

setPattern

public void setPattern(String pattern)
Change the pattern we're looking for

UNKNOWN: Change the pattern we're looking for

shouldRestart

public boolean shouldRestart(boolean forceAsking, boolean showNumMatches)
Returns true if we should restart the search from scratch. Can ask the user, or return false (if we already searched the whole document).

Parameters: forceAsking set to true if the user modified the document during the search. In that case it makes sense to restart the search again. showNumMatches 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).

UNKNOWN: Returns true if we should restart the search from scratch.

shouldRestart

public boolean shouldRestart(boolean forceAsking)

shouldRestart

public boolean shouldRestart()

slotDialogClosed

protected void slotDialogClosed()

slotFindNext

protected void slotFindNext()

validateMatch

public boolean validateMatch(String text, int index, int matchedlength)
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.

Parameters: text The current text fragment index The starting index where the candidate match was found matchedlength The length of the candidate match

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