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

KDEUI

  • 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
40class 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
58KReplaceDialog::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
66KReplaceDialog::~KReplaceDialog()
67{
68 delete d;
69}
70
71void 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
87long 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
99QWidget *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
110QString KReplaceDialog::replacement() const
111{
112 return KFindDialog::d->replace->currentText();
113}
114
115QStringList 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
124void 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
131void 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
139void 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"
KFindDialog
A generic "find" dialog.
Definition: kfinddialog.h:80
KFindDialog::showEvent
virtual void showEvent(QShowEvent *)
Definition: kfinddialog.cpp:254
KFindDialog::KReplaceDialog
friend class KReplaceDialog
Definition: kfinddialog.h:220
KFindDialog::options
long options() const
Returns the state of the options.
Definition: kfinddialog.cpp:281
KFindDialog::setOptions
void setOptions(long options)
Set the options which are checked.
Definition: kfinddialog.cpp:398
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
A generic "replace" dialog.
Definition: kreplacedialog.h:56
KReplaceDialog::replaceExtension
QWidget * replaceExtension() const
Returns an empty widget which the user may fill with additional UI elements as required.
Definition: kreplacedialog.cpp:99
KReplaceDialog::replacementHistory
QStringList replacementHistory() const
Returns the list of history items.
Definition: kreplacedialog.cpp:115
KReplaceDialog::options
long options() const
Returns the state of the options.
Definition: kreplacedialog.cpp:87
KReplaceDialog::showEvent
virtual void showEvent(QShowEvent *)
Definition: kreplacedialog.cpp:71
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
KReplaceDialog::setOptions
void setOptions(long options)
Set the options which are enabled.
Definition: kreplacedialog.cpp:124
KReplaceDialog::~KReplaceDialog
virtual ~KReplaceDialog()
Destructor.
Definition: kreplacedialog.cpp:66
KReplaceDialog::replacement
QString replacement() const
Returns the replacement string.
Definition: kreplacedialog.cpp:110
KReplaceDialog::BackReference
@ BackReference
Definition: kreplacedialog.h:67
KReplaceDialog::PromptOnReplace
@ PromptOnReplace
Definition: kreplacedialog.h:66
QWidget
kdebug.h
khistorycombobox.h
klocale.h
i18n
QString i18n(const char *text)
i18np
QString i18np(const char *sing, const char *plur, const A1 &a1)
kmessagebox.h
kreplacedialog.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
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