• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • kdeui
  • findreplace
kreplacedialog.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2001, S.R.Haque <srhaque@iee.org>.
3  Copyright (C) 2002, David Faure <david@mandrakesoft.com>
4  This file is part of the KDE project
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2, as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "kreplacedialog.h"
22 #include "kfinddialog_p.h"
23 
24 #include <QtGui/QCheckBox>
25 #include <QtGui/QGroupBox>
26 #include <QtGui/QLabel>
27 #include <QtGui/QLayout>
28 #include <QtGui/QLineEdit>
29 #include <QtCore/QRegExp>
30 #include <khistorycombobox.h>
31 #include <klocale.h>
32 #include <kmessagebox.h>
33 #include <kdebug.h>
34 
40 class KReplaceDialogPrivate
41 {
42  public:
43  KReplaceDialogPrivate(KReplaceDialog *q)
44  : q(q)
45  , initialShowDone(false)
46  , replaceExtension (0)
47  {
48  }
49 
50  void _k_slotOk();
51 
52  KReplaceDialog *q;
53  QStringList replaceStrings;
54  bool initialShowDone;
55  QWidget *replaceExtension;
56 };
57 
58 KReplaceDialog::KReplaceDialog(QWidget *parent, long options, const QStringList &findStrings,
59  const QStringList &replaceStrings, bool hasSelection)
60  : KFindDialog(parent, options, findStrings, hasSelection, true /*create replace dialog*/),
61  d(new KReplaceDialogPrivate(this))
62 {
63  d->replaceStrings = replaceStrings;
64 }
65 
66 KReplaceDialog::~KReplaceDialog()
67 {
68  delete d;
69 }
70 
71 void KReplaceDialog::showEvent( QShowEvent *e )
72 {
73  if ( !d->initialShowDone )
74  {
75  d->initialShowDone = true; // only once
76 
77  if (!d->replaceStrings.isEmpty())
78  {
79  setReplacementHistory(d->replaceStrings);
80  KFindDialog::d->replace->lineEdit()->setText( d->replaceStrings[0] );
81  }
82  }
83 
84  KFindDialog::showEvent(e);
85 }
86 
87 long KReplaceDialog::options() const
88 {
89  long options = 0;
90 
91  options = KFindDialog::options();
92  if (KFindDialog::d->promptOnReplace->isChecked())
93  options |= PromptOnReplace;
94  if (KFindDialog::d->backRef->isChecked())
95  options |= BackReference;
96  return options;
97 }
98 
99 QWidget *KReplaceDialog::replaceExtension() const
100 {
101  if (!d->replaceExtension)
102  {
103  d->replaceExtension = new QWidget(KFindDialog::d->replaceGrp);
104  KFindDialog::d->replaceLayout->addWidget(d->replaceExtension, 3, 0, 1, 2);
105  }
106 
107  return d->replaceExtension;
108 }
109 
110 QString KReplaceDialog::replacement() const
111 {
112  return KFindDialog::d->replace->currentText();
113 }
114 
115 QStringList KReplaceDialog::replacementHistory() const
116 {
117  QStringList lst = KFindDialog::d->replace->historyItems();
118  // historyItems() doesn't tell us about the case of replacing with an empty string
119  if ( KFindDialog::d->replace->lineEdit()->text().isEmpty() )
120  lst.prepend( QString() );
121  return lst;
122 }
123 
124 void KReplaceDialog::setOptions(long options)
125 {
126  KFindDialog::setOptions(options);
127  KFindDialog::d->promptOnReplace->setChecked(options & PromptOnReplace);
128  KFindDialog::d->backRef->setChecked(options & BackReference);
129 }
130 
131 void KReplaceDialog::setReplacementHistory(const QStringList &strings)
132 {
133  if (strings.count() > 0)
134  KFindDialog::d->replace->setHistoryItems(strings, true);
135  else
136  KFindDialog::d->replace->clearHistory();
137 }
138 
139 void KReplaceDialogPrivate::_k_slotOk()
140 {
141  // If regex and backrefs are enabled, do a sanity check.
142  if ( q->KFindDialog::d->regExp->isChecked() && q->KFindDialog::d->backRef->isChecked() )
143  {
144  QRegExp r ( q->pattern() );
145  int caps = r.numCaptures();
146  QRegExp check(QString("((?:\\\\)+)(\\d+)"));
147  int p = 0;
148  QString rep = q->replacement();
149  while ( (p = check.indexIn( rep, p ) ) > -1 )
150  {
151  if ( check.cap(1).length()%2 && check.cap(2).toInt() > caps )
152  {
153  KMessageBox::information( q, i18n(
154  "Your replacement string is referencing a capture greater than '\\%1', ", caps ) +
155  ( caps ?
156  i18np("but your pattern only defines 1 capture.",
157  "but your pattern only defines %1 captures.", caps ) :
158  i18n("but your pattern defines no captures.") ) +
159  i18n("\nPlease correct.") );
160  return; // abort OKing
161  }
162  p += check.matchedLength();
163  }
164 
165  }
166 
167  q->KFindDialog::d->_k_slotOk();
168  q->KFindDialog::d->replace->addToHistory(q->replacement());
169 }
170 
171 // kate: space-indent on; indent-width 4; replace-tabs on;
172 #include "kreplacedialog.moc"
QAction::text
text
i18n
QString i18n(const char *text)
KFindDialog
A generic "find" dialog.
Definition: kfinddialog.h:78
QWidget
kdebug.h
KReplaceDialog::replacementHistory
QStringList replacementHistory() const
Returns the list of history items.
Definition: kreplacedialog.cpp:115
i18np
QString i18np(const char *sing, const char *plur, const A1 &a1)
KMessageBox::information
static void information(QWidget *parent, const QString &text, const QString &caption=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
Display an "Information" dialog.
Definition: kmessagebox.cpp:960
KReplaceDialog::options
long options() const
Returns the state of the options.
Definition: kreplacedialog.cpp:87
QRegExp::numCaptures
int numCaptures() const
klocale.h
kreplacedialog.h
KFindDialog::options
long options() const
Returns the state of the options.
Definition: kfinddialog.cpp:281
KReplaceDialog
A generic "replace" dialog.
Definition: kreplacedialog.h:54
KReplaceDialog::BackReference
Definition: kreplacedialog.h:67
QRegExp
QList::count
int count(const T &value) const
QShowEvent
KReplaceDialog::replacement
QString replacement() const
Returns the replacement string.
Definition: kreplacedialog.cpp:110
KReplaceDialog::KReplaceDialog
KReplaceDialog(QWidget *parent=0, long options=0, const QStringList &findStrings=QStringList(), const QStringList &replaceStrings=QStringList(), bool hasSelection=true)
Construct a replace dialog.read-only or rather select-only combo box with a parent object and a name...
Definition: kreplacedialog.cpp:58
KReplaceDialog::showEvent
virtual void showEvent(QShowEvent *)
Definition: kreplacedialog.cpp:71
KReplaceDialog::PromptOnReplace
Definition: kreplacedialog.h:66
KReplaceDialog::~KReplaceDialog
virtual ~KReplaceDialog()
Destructor.
Definition: kreplacedialog.cpp:66
khistorycombobox.h
QString
KFindDialog::showEvent
virtual void showEvent(QShowEvent *)
Definition: kfinddialog.cpp:254
QStringList
KReplaceDialog::setReplacementHistory
void setReplacementHistory(const QStringList &history)
Provide the list of strings to be displayed as the history of replacement strings.
Definition: kreplacedialog.cpp:131
KStandardAction::replace
KAction * replace(const QObject *recvr, const char *slot, QObject *parent)
Find and replace matches.
Definition: kstandardaction.cpp:344
KFindDialog::setOptions
void setOptions(long options)
Set the options which are checked.
Definition: kfinddialog.cpp:398
QWidget::QWidget
QWidget(QWidget *parent, QFlags< Qt::WindowType > f)
KReplaceDialog::setOptions
void setOptions(long options)
Set the options which are enabled.
Definition: kreplacedialog.cpp:124
QList::prepend
void prepend(const T &value)
kmessagebox.h
KReplaceDialog::replaceExtension
QWidget * replaceExtension() const
Returns an empty widget which the user may fill with additional UI elements as required.
Definition: kreplacedialog.cpp:99
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:00 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal