summaryrefslogtreecommitdiffstats
path: root/tdeutils/kreplacedialog.cpp
blob: 59c408b8b0535853b0b663dc4ffced07c9c00146 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
    Copyright (C) 2001, S.R.Haque <[email protected]>.
    Copyright (C) 2002, David Faure <[email protected]>
    This file is part of the KDE project

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2, as published by the Free Software Foundation.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kreplacedialog.h"

#include <tqcheckbox.h>
#include <tqgroupbox.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqregexp.h>
#include <kcombobox.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kdebug.h>

/**
 * we need to insert the strings after the dialog is set
 * up, otherwise TQComboBox will deliver an aweful big sizeHint
 * for long replacement texts.
 */
class KReplaceDialog::KReplaceDialogPrivate {
  public:
    KReplaceDialogPrivate() : m_initialShowDone(false) {}
    TQStringList replaceStrings;
    bool m_initialShowDone;
};

KReplaceDialog::KReplaceDialog(TQWidget *parent, const char *name, long options, const TQStringList &findStrings, const TQStringList &replaceStrings, bool hasSelection) :
    KFindDialog(parent, name, true)
{
    d = new KReplaceDialogPrivate;
    d->replaceStrings = replaceStrings;
    init(true, findStrings, hasSelection);
    setOptions(options);
}

KReplaceDialog::~KReplaceDialog()
{
    delete d;
}

void KReplaceDialog::showEvent( TQShowEvent *e )
{
    if ( !d->m_initialShowDone )
    {
        d->m_initialShowDone = true; // only once

        if (!d->replaceStrings.isEmpty())
        {
          setReplacementHistory(d->replaceStrings);
          m_replace->lineEdit()->setText( d->replaceStrings[0] );
        }
    }

    KFindDialog::showEvent(e);
}

long KReplaceDialog::options() const
{
    long options = 0;

    options = KFindDialog::options();
    if (m_promptOnReplace->isChecked())
        options |= PromptOnReplace;
    if (m_backRef->isChecked())
        options |= BackReference;
    return options;
}

TQWidget *KReplaceDialog::replaceExtension()
{
    if (!m_replaceExtension)
    {
      m_replaceExtension = new TQWidget(m_replaceGrp);
      m_replaceLayout->addMultiCellWidget(m_replaceExtension, 3, 3, 0, 1);
    }

    return m_replaceExtension;
}

TQString KReplaceDialog::replacement() const
{
    return m_replace->currentText();
}

TQStringList KReplaceDialog::replacementHistory() const
{
    TQStringList lst = m_replace->historyItems();
    // historyItems() doesn't tell us about the case of replacing with an empty string
    if ( m_replace->lineEdit()->text().isEmpty() )
        lst.prepend( TQString::null );
    return lst;
}

void KReplaceDialog::setOptions(long options)
{
    KFindDialog::setOptions(options);
    m_promptOnReplace->setChecked(options & PromptOnReplace);
    m_backRef->setChecked(options & BackReference);
}

void KReplaceDialog::setReplacementHistory(const TQStringList &strings)
{
    if (strings.count() > 0)
        m_replace->setHistoryItems(strings, true);
    else
        m_replace->clearHistory();
}

void KReplaceDialog::slotOk()
{
    // If regex and backrefs are enabled, do a sanity check.
    if ( m_regExp->isChecked() && m_backRef->isChecked() )
    {
        TQRegExp r ( pattern() );
        int caps = r.numCaptures();
        TQRegExp check(TQString("((?:\\\\)+)(\\d+)"));
        int p = 0;
        TQString rep = replacement();
        while ( (p = check.search( rep, p ) ) > -1 )
        {
            if ( check.cap(1).length()%2 && check.cap(2).toInt() > caps )
            {
                KMessageBox::information( this, i18n(
                        "Your replacement string is referencing a capture greater than '\\%1', ").arg( caps ) +
                    ( caps ?
                        i18n("but your pattern only defines 1 capture.",
                             "but your pattern only defines %n captures.", caps ) :
                        i18n("but your pattern defines no captures.") ) +
                    i18n("\nPlease correct.") );
                return; // abort OKing
            }
            p += check.matchedLength();
        }

    }

    KFindDialog::slotOk();
    m_replace->addToHistory(replacement());
}
#include "kreplacedialog.moc"