22 #include <tdeapplication.h>
26 #include <tdemessagebox.h>
28 #include "kreplacedialog.h"
32 #define INDEX_NOMATCH -1
34 class KReplaceNextDialog :
public KDialogBase
37 KReplaceNextDialog( TQWidget *parent );
38 void setLabel(
const TQString& pattern,
const TQString& replacement );
43 KReplaceNextDialog::KReplaceNextDialog(TQWidget *parent) :
44 KDialogBase(parent, 0, false,
46 User3 | User2 | User1 | Close,
49 i18n(
"&All"), i18n(
"&Skip"), i18n(
"Replace"))
51 m_mainLabel =
new TQLabel(
this );
52 setMainWidget( m_mainLabel );
53 resize(minimumSize());
56 void KReplaceNextDialog::setLabel(
const TQString& pattern,
const TQString& replacement )
58 m_mainLabel->setText( i18n(
"Replace '%1' with '%2'?").arg(pattern).arg(replacement) );
63 KReplace::KReplace(
const TQString &pattern,
const TQString &replacement,
long options, TQWidget *parent) :
64 KFind( pattern, options, parent )
67 m_replacement = replacement;
70 KReplace::KReplace(
const TQString &pattern,
const TQString &replacement,
long options, TQWidget *parent, TQWidget *dlg) :
71 KFind( pattern, options, parent, dlg )
74 m_replacement = replacement;
84 if ( m_dialog || create )
89 KReplaceNextDialog* KReplace::dialog()
93 m_dialog =
new KReplaceNextDialog( parentWidget() );
94 connect( m_dialog, TQT_SIGNAL( user1Clicked() ),
this, TQT_SLOT( slotReplaceAll() ) );
95 connect( m_dialog, TQT_SIGNAL( user2Clicked() ),
this, TQT_SLOT( slotSkip() ) );
96 connect( m_dialog, TQT_SIGNAL( user3Clicked() ),
this, TQT_SLOT( slotReplace() ) );
97 connect( m_dialog, TQT_SIGNAL( finished() ),
this, TQT_SLOT( slotDialogClosed() ) );
99 return static_cast<KReplaceNextDialog *
>(m_dialog);
104 if ( !m_replacements )
105 KMessageBox::information(parentWidget(), i18n(
"No text was replaced."));
107 KMessageBox::information(parentWidget(), i18n(
"1 replacement done.",
"%n replacements done.", m_replacements ) );
113 kdDebug() << k_funcinfo <<
"m_index=" << m_index <<
endl;
115 if ( m_index == INDEX_NOMATCH && m_lastResult ==
Match )
124 kdDebug() << k_funcinfo <<
"beginning of loop: m_index=" << m_index <<
endl;
128 m_index =
KFind::find(m_text, *m_regExp, m_index, m_options, &m_matchedLength);
130 m_index =
KFind::find(m_text, m_pattern, m_index, m_options, &m_matchedLength);
132 kdDebug() << k_funcinfo <<
"KFind::find returned m_index=" << m_index <<
endl;
142 kdDebug() << k_funcinfo <<
"PromptOnReplace" <<
endl;
145 TQString matchedText = m_text.mid( m_index, m_matchedLength );
146 TQString rep = matchedText;
148 dialog()->setLabel( matchedText, rep );
153 emit
highlight(m_text, m_index, m_matchedLength);
155 m_lastResult =
Match;
172 m_index = INDEX_NOMATCH;
174 while (m_index != INDEX_NOMATCH);
180 int KReplace::replace(TQString &text,
const TQString &pattern,
const TQString &replacement,
int index,
long options,
int *replacedLength)
184 index =
KFind::find(text, pattern, index, options, &matchedLength);
187 *replacedLength =
replace(text, replacement, index, options, matchedLength);
191 index += *replacedLength;
196 int KReplace::replace(TQString &text,
const TQRegExp &pattern,
const TQString &replacement,
int index,
long options,
int *replacedLength)
200 index =
KFind::find(text, pattern, index, options, &matchedLength);
203 *replacedLength =
replace(text, replacement, index, options, matchedLength);
207 index += *replacedLength;
212 int KReplace::replace(TQString &text,
const TQString &replacement,
int index,
long options,
int length)
214 TQString rep = replacement;
216 if ( options & KReplaceDialog::BackReference )
217 rep.replace(
"\\0", text.mid( index, length ) );
219 text.replace(index, length, rep);
223 void KReplace::slotReplaceAll()
231 void KReplace::slotSkip()
237 if ( m_dialogClosed ) {
244 void KReplace::slotReplace()
247 if ( m_dialogClosed ) {
254 void KReplace::doReplace()
256 int replacedLength =
KReplace::replace(m_text, m_replacement, m_index, m_options, m_matchedLength);
260 emit
replace(m_text, m_index, replacedLength, m_matchedLength);
262 kdDebug() << k_funcinfo <<
"after replace() signal: m_index=" << m_index <<
" replacedLength=" << replacedLength <<
endl;
265 if (m_options & KReplaceDialog::FindBackwards)
268 m_index += replacedLength;
270 if ( m_pattern.isEmpty() )
274 kdDebug() << k_funcinfo <<
"after adjustement: m_index=" << m_index <<
endl;
297 if ( showNumMatches )
299 if ( !m_replacements )
300 message = i18n(
"No text was replaced.");
302 message = i18n(
"1 replacement done.",
"%n replacements done.", m_replacements );
307 message = i18n(
"Beginning of document reached." );
309 message = i18n(
"End of document reached." );
316 i18n(
"Do you want to restart search from the end?")
317 : i18n(
"Do you want to restart search at the beginning?");
319 int ret = KMessageBox::questionYesNo( parentWidget(), message, TQString::null, i18n(
"Restart"), i18n(
"Stop") );
320 return( ret == KMessageBox::Yes );
328 #include "kreplace.moc"
KDialogBase * replaceNextDialog(bool create=false)
Return (or create) the dialog that shows the "find next?" prompt.
Result find()
Walk the text fragment (e.g.
void closeReplaceNextDialog()
Close the "replace next?" dialog.
kdbgstream kdDebug(int area=0)
virtual void resetCounts()
Call this to reset the numMatches count (and the numReplacements count for a KReplace).
virtual bool validateMatch(const TQString &text, int index, int matchedlength)
Virtual method, which allows applications to add extra checks for validating a candidate match...
Interpret the pattern as a regular expression.
void highlight(const TQString &text, int matchingIndex, int matchedLength)
Connect to this signal to implement highlighting of found text during the find operation.
A generic "replace" dialog.
virtual bool shouldRestart(bool forceAsking=false, bool showNumMatches=true) const
Returns true if we should restart the search from scratch.
Result replace()
Walk the text fragment (e.g.
virtual ~KReplace()
Destructor.
A generic implementation of the "find" function.
virtual void displayFinalDialog() const
Displays the final dialog telling the user how many replacements were made.
Should the user be prompted before the replace operation?
void closeFindNextDialog()
Close the "find next?" dialog.
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...
Start from current cursor position.
kndbgstream & endl(kndbgstream &s)
virtual void resetCounts()
Call this to reset the numMatches & numReplacements counts.
void optionsChanged()
Emitted when the options have changed.