kdeui: remove redundant RegExpAction and PlaceHolderAction members

QAction has a text() method which will return the same string (the
string passed to the QAction constructor)

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2023-06-29 05:37:22 +03:00
parent 3ff69a4d6b
commit 6a60f0d43a
3 changed files with 267 additions and 212 deletions

View file

@ -45,8 +45,8 @@ KFindDialog::KFindDialog(QWidget *parent, long options, const QStringList &findS
d(new KFindDialogPrivate(this))
{
setCaption(i18n("Find Text"));
setButtons( Ok | Cancel );
setDefaultButton( Ok );
setButtons(KDialog::Ok | KDialog::Cancel);
setDefaultButton(KDialog::Ok);
d->init(replaceDialog, findStrings, hasSelection);
setOptions(options);
@ -60,12 +60,10 @@ KFindDialog::~KFindDialog()
QWidget *KFindDialog::findExtension() const
{
if (!d->findExtension)
{
if (!d->findExtension) {
d->findExtension = new QWidget(d->findGrp);
d->findLayout->addWidget(d->findExtension, 3, 0, 1, 2);
}
return d->findExtension;
}
@ -184,8 +182,7 @@ void KFindDialog::KFindDialogPrivate::init(bool forReplace, const QStringList &_
findLabel->setBuddy(find);
replaceLabel->setBuddy(replace);
if (!forReplace)
{
if (!forReplace) {
promptOnReplace->hide();
replaceGrp->hide();
}
@ -193,54 +190,101 @@ void KFindDialog::KFindDialogPrivate::init(bool forReplace, const QStringList &_
findStrings = _findStrings;
find->setFocus();
q->enableButtonOk(!q->pattern().isEmpty());
if (forReplace)
{
q->setButtonGuiItem( KDialog::Ok, KGuiItem( i18n("&Replace"), QString(),
if (forReplace) {
q->setButtonGuiItem(
KDialog::Ok,
KGuiItem(
i18n("&Replace"), QString(),
i18n("Start replace"),
i18n("<qt>If you press the <b>Replace</b> button, the text you entered "
i18n(
"<qt>If you press the <b>Replace</b> button, the text you entered "
"above is searched for within the document and any occurrence is "
"replaced with the replacement text.</qt>")));
}
else
{
q->setButtonGuiItem( KDialog::Ok, KGuiItem( i18n("&Find"), "edit-find",
"replaced with the replacement text.</qt>"
)
)
);
} else {
q->setButtonGuiItem(
KDialog::Ok,
KGuiItem(
i18n("&Find"), "edit-find",
i18n("Start searching"),
i18n("<qt>If you press the <b>Find</b> button, the text you entered "
"above is searched for within the document.</qt>")));
i18n(
"<qt>If you press the <b>Find</b> button, the text you entered "
"above is searched for within the document.</qt>"
)
)
);
}
// QWhatsthis texts
find->setWhatsThis(i18n(
find->setWhatsThis(
i18n(
"Enter a pattern to search for, or select a previous pattern from "
"the list.") );
regExp->setWhatsThis(i18n(
"If enabled, search for a regular expression.") );
regExpItem->setWhatsThis(i18n(
"Click here to edit your regular expression using a graphical editor.") );
replace->setWhatsThis(i18n(
"Enter a replacement string, or select a previous one from the list.") );
backRef->setWhatsThis(i18n(
"the list."
)
);
regExp->setWhatsThis(
i18n(
"If enabled, search for a regular expression."
)
);
regExpItem->setWhatsThis(
i18n(
"Click here to edit your regular expression using a graphical editor."
)
);
replace->setWhatsThis(
i18n(
"Enter a replacement string, or select a previous one from the list."
)
);
backRef->setWhatsThis(
i18n(
"<qt>If enabled, any occurrence of <code><b>\\N</b></code>, where "
"<code><b>N</b></code> is an integer number, will be replaced with "
"the corresponding capture (\"parenthesized substring\") from the "
"pattern.<p>To include (a literal <code><b>\\N</b></code> in your "
"replacement, put an extra backslash in front of it, like "
"<code><b>\\\\N</b></code>.</p></qt>") );
backRefItem->setWhatsThis(i18n(
"Click for a menu of available captures.") );
wholeWordsOnly->setWhatsThis(i18n(
"Require word boundaries in both ends of a match to succeed.") );
fromCursor->setWhatsThis(i18n(
"Start searching at the current cursor location rather than at the top.") );
selectedText->setWhatsThis(i18n(
"Only search within the current selection.") );
caseSensitive->setWhatsThis(i18n(
"<code><b>\\\\N</b></code>.</p></qt>"
)
);
backRefItem->setWhatsThis(
i18n(
"Click for a menu of available captures."
)
);
wholeWordsOnly->setWhatsThis(
i18n(
"Require word boundaries in both ends of a match to succeed."
)
);
fromCursor->setWhatsThis(
i18n(
"Start searching at the current cursor location rather than at the top."
)
);
selectedText->setWhatsThis(
i18n(
"Only search within the current selection."
)
);
caseSensitive->setWhatsThis(
i18n(
"Perform a case sensitive search: entering the pattern "
"'Joe' will not match 'joe' or 'JOE', only 'Joe'.") );
findBackwards->setWhatsThis(i18n(
"Search backwards.") );
promptOnReplace->setWhatsThis(i18n(
"Ask before replacing each match found.") );
"'Joe' will not match 'joe' or 'JOE', only 'Joe'."
)
);
findBackwards->setWhatsThis(
i18n(
"Search backwards."
)
);
promptOnReplace->setWhatsThis(
i18n(
"Ask before replacing each match found."
)
);
q->connect(q, SIGNAL(okClicked()), q, SLOT(_k_slotOk()));
_k_textSearchChanged(find->lineEdit()->text());
@ -253,12 +297,12 @@ void KFindDialog::KFindDialogPrivate::_k_textSearchChanged( const QString & text
void KFindDialog::showEvent(QShowEvent *e)
{
if ( !d->initialShowDone )
{
if (!d->initialShowDone) {
d->initialShowDone = true; // only once
kDebug() << "showEvent\n";
if (!d->findStrings.isEmpty())
if (!d->findStrings.isEmpty()) {
setFindHistory(d->findStrings);
}
d->findStrings = QStringList();
if (!d->pattern.isEmpty()) {
d->find->lineEdit()->setText(d->pattern);
@ -281,19 +325,24 @@ void KFindDialog::showEvent( QShowEvent *e )
long KFindDialog::options() const
{
long options = 0;
if (d->caseSensitive->isChecked())
if (d->caseSensitive->isChecked()) {
options |= KFind::CaseSensitive;
if (d->wholeWordsOnly->isChecked())
}
if (d->wholeWordsOnly->isChecked()) {
options |= KFind::WholeWordsOnly;
if (d->fromCursor->isChecked())
}
if (d->fromCursor->isChecked()) {
options |= KFind::FromCursor;
if (d->findBackwards->isChecked())
}
if (d->findBackwards->isChecked()) {
options |= KFind::FindBackwards;
if (d->selectedText->isChecked())
}
if (d->selectedText->isChecked()) {
options |= KFind::SelectedText;
if (d->regExp->isChecked())
}
if (d->regExp->isChecked()) {
options |= KFind::RegularExpression;
}
return options;
}
@ -312,23 +361,24 @@ void KFindDialog::setPattern (const QString &pattern)
void KFindDialog::setFindHistory(const QStringList &strings)
{
if (strings.count() > 0)
{
if (strings.count() > 0) {
d->find->setHistoryItems(strings, true);
d->find->lineEdit()->setText(strings.first());
d->find->lineEdit()->selectAll();
}
else
} else {
d->find->clearHistory();
}
}
void KFindDialog::setHasSelection(bool hasSelection)
{
if (hasSelection) d->enabled |= KFind::SelectedText;
else d->enabled &= ~KFind::SelectedText;
if (hasSelection) {
d->enabled |= KFind::SelectedText;
} else {
d->enabled &= ~KFind::SelectedText;
}
d->selectedText->setEnabled(hasSelection);
if ( !hasSelection )
{
if (!hasSelection) {
d->selectedText->setChecked(false);
d->_k_slotSelectedTextToggled(hasSelection);
}
@ -338,14 +388,19 @@ void KFindDialog::KFindDialogPrivate::_k_slotSelectedTextToggled(bool selec)
{
// From cursor doesn't make sense if we have a selection
fromCursor->setEnabled(!selec && (enabled & KFind::FromCursor));
if ( selec ) // uncheck if disabled
if (selec) {
// uncheck if disabled
fromCursor->setChecked(false);
}
}
void KFindDialog::setHasCursor(bool hasCursor)
{
if (hasCursor) d->enabled |= KFind::FromCursor;
else d->enabled &= ~KFind::FromCursor;
if (hasCursor) {
d->enabled |= KFind::FromCursor;
} else {
d->enabled &= ~KFind::FromCursor;
}
d->fromCursor->setEnabled(hasCursor);
d->fromCursor->setChecked(hasCursor && (options() & KFind::FromCursor));
}
@ -353,8 +408,11 @@ void KFindDialog::setHasCursor(bool hasCursor)
void KFindDialog::setSupportsBackwardsFind(bool supports)
{
// ########## Shouldn't this hide the checkbox instead?
if (supports) d->enabled |= KFind::FindBackwards;
else d->enabled &= ~KFind::FindBackwards;
if (supports) {
d->enabled |= KFind::FindBackwards;
} else {
d->enabled &= ~KFind::FindBackwards;
}
d->findBackwards->setEnabled(supports);
d->findBackwards->setChecked(supports && (options() & KFind::FindBackwards));
}
@ -362,8 +420,11 @@ void KFindDialog::setSupportsBackwardsFind( bool supports )
void KFindDialog::setSupportsCaseSensitiveFind(bool supports)
{
// ########## This should hide the checkbox instead
if (supports) d->enabled |= KFind::CaseSensitive;
else d->enabled &= ~KFind::CaseSensitive;
if (supports) {
d->enabled |= KFind::CaseSensitive;
} else {
d->enabled &= ~KFind::CaseSensitive;
}
d->caseSensitive->setEnabled(supports);
d->caseSensitive->setChecked(supports && (options() & KFind::CaseSensitive));
}
@ -371,25 +432,28 @@ void KFindDialog::setSupportsCaseSensitiveFind( bool supports )
void KFindDialog::setSupportsWholeWordsFind(bool supports)
{
// ########## This should hide the checkbox instead
if (supports) d->enabled |= KFind::WholeWordsOnly;
else d->enabled &= ~KFind::WholeWordsOnly;
if (supports) {
d->enabled |= KFind::WholeWordsOnly;
} else {
d->enabled &= ~KFind::WholeWordsOnly;
}
d->wholeWordsOnly->setEnabled(supports);
d->wholeWordsOnly->setChecked(supports && (options() & KFind::WholeWordsOnly));
}
void KFindDialog::setSupportsRegularExpressionFind(bool supports)
{
if (supports) d->enabled |= KFind::RegularExpression;
else d->enabled &= ~KFind::RegularExpression;
if (supports) {
d->enabled |= KFind::RegularExpression;
} else {
d->enabled &= ~KFind::RegularExpression;
}
d->regExp->setEnabled(supports);
d->regExp->setChecked(supports && (options() & KFind::RegularExpression));
if( !supports)
{
if(!supports) {
d->regExpItem->hide();
d->regExp->hide();
}
else
{
} else {
d->regExpItem->show();
d->regExp->show();
}
@ -431,43 +495,39 @@ void KFindDialog::KFindDialogPrivate::_k_showPatterns()
{ I18N_NOOP("Digit"), "\\d", 0 },
};
class RegExpAction : public QAction
{
public:
RegExpAction(QObject *parent, const QString &text, const QString &regExp, int cursor)
: QAction( text, parent ), mText( text ), mRegExp( regExp ), mCursor( cursor )
: QAction(text, parent), mRegExp(regExp), mCursor(cursor)
{
}
QString text() const { return mText; }
QString regExp() const { return mRegExp; }
int cursor() const { return mCursor; }
private:
QString mText;
QString mRegExp;
int mCursor;
};
int i;
// Populate the popup menu.
if (!patterns)
{
if (!patterns) {
patterns = new QMenu(q);
for (i = 0; (unsigned)i < sizeof(items) / sizeof(items[0]); i++)
{
patterns->addAction(new RegExpAction(patterns, i18n(items[i].description),
for (int i = 0; i < sizeof(items) / sizeof(items[0]); i++) {
patterns->addAction(
new RegExpAction(
patterns, i18n(items[i].description),
items[i].regExp,
items[i].cursorAdjustment));
items[i].cursorAdjustment
)
);
}
}
// Insert the selection into the edit control.
QAction *action = patterns->exec(regExpItem->mapToGlobal(regExpItem->rect().bottomLeft()));
if (action)
{
if (action) {
RegExpAction *regExpAction = static_cast<RegExpAction*>(action);
if (regExpAction) {
QLineEdit *editor = find->lineEdit();
@ -482,15 +542,13 @@ class PlaceHolderAction : public QAction
{
public:
PlaceHolderAction(QObject *parent, const QString &text, int id)
: QAction( text, parent ), mText( text ), mId( id )
: QAction(text, parent), mId(id)
{
}
QString text() const { return mText; }
int id() const { return mId; }
private:
QString mText;
int mId;
};
@ -499,16 +557,14 @@ class PlaceHolderAction : public QAction
void KFindDialog::KFindDialogPrivate::_k_showPlaceholders()
{
// Populate the popup menu.
if (!placeholders)
{
if (!placeholders) {
placeholders = new QMenu(q);
q->connect(placeholders, SIGNAL(aboutToShow()), q, SLOT(_k_slotPlaceholdersAboutToShow()));
}
// Insert the selection into the edit control.
QAction *action = placeholders->exec(backRefItem->mapToGlobal(backRefItem->rect().bottomLeft()));
if (action)
{
if (action) {
PlaceHolderAction *placeHolderAction = static_cast<PlaceHolderAction*>(action);
if (placeHolderAction) {
QLineEdit *editor = replace->lineEdit();
@ -524,33 +580,36 @@ void KFindDialog::KFindDialogPrivate::_k_slotPlaceholdersAboutToShow()
QRegExp r( q->pattern() );
uint n = r.captureCount();
for ( uint i=0; i < n; i++ )
placeholders->addAction( new PlaceHolderAction(placeholders, i18n("Captured Text (%1)", i+1 ), i+1 ) );
for (uint i = 0; i < n; i++) {
placeholders->addAction(
new PlaceHolderAction(
placeholders, i18n("Captured Text (%1)", i + 1), i + 1
)
);
}
}
void KFindDialog::KFindDialogPrivate::_k_slotOk()
{
// Nothing to find?
if (q->pattern().isEmpty())
{
if (q->pattern().isEmpty()) {
KMessageBox::error(q, i18n("You must enter some text to search for."));
return;
}
if (regExp->isChecked())
{
if (regExp->isChecked()) {
// Check for a valid regular expression.
QRegExp _regExp(q->pattern());
if (!_regExp.isValid())
{
if (!_regExp.isValid()) {
KMessageBox::error(q, i18n("Invalid regular expression."));
return;
}
}
find->addToHistory(q->pattern());
if ( q->windowModality() != Qt::NonModal )
if (q->windowModality() != Qt::NonModal) {
q->accept();
}
// kate: space-indent on; indent-width 4; replace-tabs on;
}
#include "moc_kfinddialog.cpp"

View file

@ -24,16 +24,15 @@
#include "kfinddialog.h"
#include "kfind.h"
#include <QtCore/QStringList>
class KHistoryComboBox;
#include <QStringList>
#include <QCheckBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QMenu>
#include <QPushButton>
class KHistoryComboBox;
class KFindDialog::KFindDialogPrivate
{
public:

View file

@ -41,9 +41,9 @@ class KReplaceDialogPrivate
{
public:
KReplaceDialogPrivate(KReplaceDialog *q)
: q(q)
, initialShowDone(false)
, replaceExtension (0)
: q(q),
initialShowDone(false),
replaceExtension(nullptr)
{
}
@ -70,12 +70,10 @@ KReplaceDialog::~KReplaceDialog()
void KReplaceDialog::showEvent(QShowEvent *e )
{
if ( !d->initialShowDone )
{
if (!d->initialShowDone) {
d->initialShowDone = true; // only once
if (!d->replaceStrings.isEmpty())
{
if (!d->replaceStrings.isEmpty()) {
setReplacementHistory(d->replaceStrings);
KFindDialog::d->replace->lineEdit()->setText(d->replaceStrings[0]);
}
@ -87,23 +85,22 @@ void KReplaceDialog::showEvent( QShowEvent *e )
long KReplaceDialog::options() const
{
long options = 0;
options = KFindDialog::options();
if (KFindDialog::d->promptOnReplace->isChecked())
if (KFindDialog::d->promptOnReplace->isChecked()) {
options |= PromptOnReplace;
if (KFindDialog::d->backRef->isChecked())
}
if (KFindDialog::d->backRef->isChecked()) {
options |= BackReference;
}
return options;
}
QWidget *KReplaceDialog::replaceExtension() const
{
if (!d->replaceExtension)
{
if (!d->replaceExtension) {
d->replaceExtension = new QWidget(KFindDialog::d->replaceGrp);
KFindDialog::d->replaceLayout->addWidget(d->replaceExtension, 3, 0, 1, 2);
}
return d->replaceExtension;
}
@ -116,8 +113,9 @@ QStringList KReplaceDialog::replacementHistory() const
{
QStringList lst = KFindDialog::d->replace->historyItems();
// historyItems() doesn't tell us about the case of replacing with an empty string
if ( KFindDialog::d->replace->lineEdit()->text().isEmpty() )
if (KFindDialog::d->replace->lineEdit()->text().isEmpty()) {
lst.prepend(QString());
}
return lst;
}
@ -130,34 +128,34 @@ void KReplaceDialog::setOptions(long options)
void KReplaceDialog::setReplacementHistory(const QStringList &strings)
{
if (strings.count() > 0)
if (strings.count() > 0) {
KFindDialog::d->replace->setHistoryItems(strings, true);
else
} else {
KFindDialog::d->replace->clearHistory();
}
}
void KReplaceDialogPrivate::_k_slotOk()
{
// If regex and backrefs are enabled, do a sanity check.
if ( q->KFindDialog::d->regExp->isChecked() && q->KFindDialog::d->backRef->isChecked() )
{
if (q->KFindDialog::d->regExp->isChecked() && q->KFindDialog::d->backRef->isChecked()) {
QRegExp r(q->pattern());
int caps = r.captureCount();
QRegExp check(QString("((?:\\\\)+)(\\d+)"));
int p = 0;
QString rep = q->replacement();
while ( (p = check.indexIn( rep, p ) ) > -1 )
{
if ( check.cap(1).length()%2 && check.cap(2).toInt() > caps )
{
KMessageBox::information( q, i18n(
while ((p = check.indexIn(rep, p)) > -1) {
if (check.cap(1).length() % 2 && check.cap(2).toInt() > caps) {
KMessageBox::information(
q, i18n(
"Your replacement string is referencing a capture greater than '\\%1', ", caps) +
( caps ?
i18np("but your pattern only defines 1 capture.",
"but your pattern only defines %1 captures.", caps ) :
i18n("but your pattern defines no captures.") ) +
i18n("\nPlease correct.") );
return; // abort OKing
(
caps ?
i18np("but your pattern only defines 1 capture.", "but your pattern only defines %1 captures.", caps) :
i18n("but your pattern defines no captures.")
) + i18n("\nPlease correct.")
);
return; // abort
}
p += check.matchedLength();
}
@ -168,5 +166,4 @@ void KReplaceDialogPrivate::_k_slotOk()
q->KFindDialog::d->replace->addToHistory(q->replacement());
}
// kate: space-indent on; indent-width 4; replace-tabs on;
#include "moc_kreplacedialog.cpp"