summaryrefslogtreecommitdiffstats
path: root/parts/replace/replacedlgimpl.cpp
blob: 59eee128d162eb092b12144e94f8a2421c1538ec (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <qcheckbox.h>
#include <qradiobutton.h>
#include <qstring.h>
#include <qregexp.h>
#include <qlabel.h>

#include <ktrader.h>
#include <kparts/componentfactory.h>
#include <kregexpeditorinterface.h>
#include <kurlrequester.h>
#include <kurlcompletion.h>
#include <klineedit.h>
#include <kcombobox.h>
#include <kdebug.h>

#include "replacedlgimpl.h"

namespace
{
/// @todo This is the same function as in ../grepview/grepviewwidget.cpp and
/// should probably be placed in a common place. For now it seemed like too
/// little code to bother with.
QString escape(const QString &str)
{
    QString escaped("[]{}()\\^$?.+-*");
    QString res;

    for (uint i=0; i < str.length(); ++i)
    {
        if (escaped.find(str[i]) != -1)
            res += "\\";
        res += str[i];
    }

    return res;
}
}


ReplaceDlgImpl::ReplaceDlgImpl(QWidget* parent, const char* name, bool modal, WFlags fl)
        : ReplaceDlg(parent,name, modal,fl), _regexp_dialog( 0 )

{
    connect( find_button, SIGNAL( clicked() ), SLOT( saveComboHistories() ) );
    connect( regexp_button, SIGNAL( clicked() ), SLOT( showRegExpEditor() ) );
    connect( find_combo, SIGNAL( textChanged( const QString & ) ),
        SLOT( validateFind( const QString & ) ) );
    connect( regexp_combo, SIGNAL( textChanged ( const QString & ) ),
             SLOT( validateExpression( const QString & ) ) );
    connect( strings_regexp_radio, SIGNAL( toggled( bool ) ), SLOT( toggleExpression( bool ) ) );

    // disable the editor button if the regexp editor isn't installed
    if ( KTrader::self()->query("KRegExpEditor/KRegExpEditor").isEmpty() )
    {
        strings_regexp_radio->disconnect( regexp_button );
    }

    path_urlreq->completionObject()->setMode(KURLCompletion::DirCompletion);
    path_urlreq->setMode( KFile::Directory | KFile::LocalOnly );

    expression_varning_label->hide();
}

ReplaceDlgImpl::~ReplaceDlgImpl()
{}

void ReplaceDlgImpl::show( QString const & path )
{
    path_urlreq->lineEdit()->setText( path );

    find_combo->setCurrentText( "" );
    replacement_combo->setCurrentText( "" );
    regexp_combo->setCurrentText( "" );

    strings_all_radio->setChecked( true );
    find_combo->setFocus();
    
    find_button->setEnabled( false );

    QDialog::show();
}


void ReplaceDlgImpl::showRegExpEditor()
{
    _regexp_dialog = KParts::ComponentFactory::createInstanceFromQuery<QDialog>( "KRegExpEditor/KRegExpEditor" );

    if ( _regexp_dialog )
    {
        KRegExpEditorInterface *editor =
            static_cast<KRegExpEditorInterface *>( _regexp_dialog->qt_cast( "KRegExpEditorInterface" ) );

        editor->setRegExp( regexp_combo->currentText() );

        if ( _regexp_dialog->exec() == QDialog::Accepted )
        {
            regexp_combo->setCurrentText( editor->regExp() );
        }
    }
}

void ReplaceDlgImpl::validateFind( const QString & )
{
    //kdDebug(0) << "ReplaceWidget::validateFind()" << endl;

    bool x = find_combo->currentText().isEmpty() && ! strings_regexp_radio->isOn();
    find_button->setEnabled( !x );
}

void ReplaceDlgImpl::validateExpression( const QString & )
{
    //kdDebug(0) << "ReplaceWidget::validateExpression()" << endl;

    QString pattern = regexp_combo->currentText();
    QRegExp re( pattern );

    if ( pattern.isEmpty() || !re.isValid() )
    {
        expression_varning_label->show();
        find_button->setEnabled( false );
    }
    else
    {
        expression_varning_label->hide();
        find_button->setEnabled( true );
    }
}

void ReplaceDlgImpl::toggleExpression( bool on )
{
    if ( on )
    {
        validateExpression( QString() );
    }
    else
    {
        expression_varning_label->hide();
        find_button->setEnabled( true );
    }
}

void ReplaceDlgImpl::saveComboHistories()
{
    if ( find_combo->isEnabled() && ! find_combo->currentText().isEmpty() )
    {
        find_combo->addToHistory( find_combo->currentText() );
    }

    if ( ! replacement_combo->currentText().isEmpty() )
    {
        replacement_combo->addToHistory( replacement_combo->currentText() );
    }

    if ( regexp_combo->isEnabled() && ! regexp_combo->currentText().isEmpty() )
    {
        regexp_combo->addToHistory( regexp_combo->currentText() );
    }
}

QRegExp ReplaceDlgImpl::expressionPattern()
{
    QString pattern = escape( find_combo->currentText() );

    QRegExp re;
    re.setCaseSensitive( case_box->isChecked() );
    re.setMinimal( true );

    if ( strings_wholewords_radio->isChecked() )
    {
        pattern = "\\b" + pattern + "\\b";
    }
    else if ( strings_regexp_radio->isChecked() )
    {
        pattern = regexp_combo->currentText();
    }

    re.setPattern( pattern );

    return re;
}

QString ReplaceDlgImpl::replacementString()
{
    return replacement_combo->currentText();
}

#include "replacedlgimpl.moc"