summaryrefslogtreecommitdiff
path: root/src/plugins/regexp/regexpwindow.cpp
blob: a00a9137a0cf23743ba5105d404cecd7dcdff8a5 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
** 
** Non-Open Source Usage  
** 
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.  
** 
** GNU General Public License Usage 
** 
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception version
** 1.2, included in the file GPL_EXCEPTION.txt in this package.  
** 
***************************************************************************/
#include "regexpwindow.h"
#include "settings.h"

#include <QtGui/QCheckBox>
#include <QtGui/QComboBox>
#include <QtGui/QLabel>
#include <QtGui/QLayout>
#include <QtGui/QLineEdit>
#include <QtGui/QContextMenuEvent>
#include <QtGui/QMenu>
#include <QtGui/QInputDialog>

using namespace RegExp::Internal;

RegExpWindow::RegExpWindow(QWidget *parent) :
   QWidget(parent),
   patternLabel(new QLabel(tr("&Pattern:"))),
   escapedPatternLabel(new QLabel(tr("&Escaped Pattern:"))),
   syntaxLabel(new QLabel(tr("&Pattern Syntax:"))),
   textLabel(new QLabel(tr("&Text:"))),
   patternComboBox (new QComboBox),
   escapedPatternLineEdit(new QLineEdit),
   textComboBox(new QComboBox),
   caseSensitiveCheckBox(new QCheckBox(tr("Case &Sensitive"))),
   minimalCheckBox(new QCheckBox(tr("&Minimal"))),
   syntaxComboBox(new QComboBox),
   indexLabel(new QLabel(tr("Index of Match:"))),
   matchedLengthLabel(new QLabel(tr("Matched Length:"))),
   indexEdit(new QLineEdit),
   matchedLengthEdit(new QLineEdit)
{
    QVBoxLayout *vboxLayout = new QVBoxLayout(this);
    QGridLayout *mainLayout = new QGridLayout;

    patternComboBox->setEditable(true);
    patternComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);

    patternLabel->setBuddy(patternComboBox);

    mainLayout->addWidget(patternLabel, 0, 0);
    mainLayout->addWidget(patternComboBox, 0, 1);

    escapedPatternLineEdit->setReadOnly(true);
    QPalette palette = escapedPatternLineEdit->palette();
    palette.setBrush(QPalette::Base, palette.brush(QPalette::Disabled, QPalette::Base));
    escapedPatternLineEdit->setPalette(palette);

    escapedPatternLabel->setBuddy(escapedPatternLineEdit);

    mainLayout->addWidget(escapedPatternLabel, 1, 0);
    mainLayout->addWidget(escapedPatternLineEdit, 1, 1);

    syntaxComboBox->addItem(tr("Regular expression v1"), QRegExp::RegExp);
    syntaxComboBox->addItem(tr("Regular expression v2"), QRegExp::RegExp2);
    syntaxComboBox->addItem(tr("Wildcard"), QRegExp::Wildcard);
    syntaxComboBox->addItem(tr("Fixed string"), QRegExp::FixedString);

    syntaxLabel->setBuddy(syntaxComboBox);

    mainLayout->addWidget(syntaxLabel, 2, 0);
    mainLayout->addWidget(syntaxComboBox, 2, 1);

    QHBoxLayout *checkBoxLayout = new QHBoxLayout;

    caseSensitiveCheckBox->setChecked(true);

    checkBoxLayout->addWidget(caseSensitiveCheckBox);
    checkBoxLayout->addWidget(minimalCheckBox);
    checkBoxLayout->addStretch(1);

    mainLayout->addLayout(checkBoxLayout, 3, 0, 1, 2);

    textComboBox->setEditable(true);
    textComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);

    textLabel->setBuddy(textComboBox);

    mainLayout->addWidget(textLabel, 4, 0);
    mainLayout->addWidget(textComboBox, 4, 1);

    indexEdit->setReadOnly(true);

    mainLayout->addWidget(indexLabel, 5, 0);
    mainLayout->addWidget(indexEdit, 5, 1);

    matchedLengthEdit->setReadOnly(true);

    mainLayout->addWidget(matchedLengthLabel, 6, 0);
    mainLayout->addWidget(matchedLengthEdit, 6, 1);

    vboxLayout->addLayout(mainLayout);
    vboxLayout->addItem(new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding));

    for (int i = 0; i < MaxCaptures; ++i) {
        captureLabels[i] = new QLabel(tr("Capture %1:").arg(i));
        captureEdits[i] = new QLineEdit;
        captureEdits[i]->setReadOnly(true);
    }
    captureLabels[0]->setText(tr("Match:"));

    for (int j = 0; j < MaxCaptures; ++j) {
        mainLayout->addWidget(captureLabels[j], 7 + j, 0);
        mainLayout->addWidget(captureEdits[j], 7 + j, 1);
    }

    connect(patternComboBox, SIGNAL(editTextChanged(const QString &)), this, SLOT(refresh()));
    connect(textComboBox, SIGNAL(editTextChanged(const QString &)), this, SLOT(refresh()));
    connect(caseSensitiveCheckBox, SIGNAL(toggled(bool)), this, SLOT(refresh()));
    connect(minimalCheckBox, SIGNAL(toggled(bool)), this, SLOT(refresh()));
    connect(syntaxComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(refresh()));

    setWindowTitle(tr("Regular Expression"));
    refresh();
}

static const char *escapedBackSlash = "\\\\";
static const char *escapedDoubleQuote = "\\\"";

static QString escapePattern(const QString &pattern)
{
    QString escaped = pattern;
    escaped.replace(QString(QLatin1Char('\\')) , QLatin1String(escapedBackSlash));
    const QChar doubleQuote(QLatin1Char('"'));
    escaped.replace(doubleQuote, QString(QLatin1String(escapedDoubleQuote)));
    escaped.prepend(doubleQuote);
    escaped.append(doubleQuote);
    return escaped;
}

static QString unescapePattern(QString escaped)
{
    // remove quotes
    const QChar doubleQuote(QLatin1Char('"'));
    if (escaped.endsWith(doubleQuote))
        escaped.truncate(escaped.size() - 1);
    if (escaped.startsWith(doubleQuote))
        escaped.remove(0, 1);

    const int size = escaped.size();
    if (!size)
        return QString();

    // parse out escapes. Do not just replace.
    QString pattern;
    const QChar backSlash = QLatin1Char('\\');
    bool escapeSeen = false;
    for (int  i = 0; i < size; i++) {
        const QChar c = escaped.at(i);
        if (c == backSlash && !escapeSeen)
            escapeSeen = true;
        else {
            pattern.push_back(c);
            escapeSeen = false;
        }
    }
    return pattern;
}

void RegExpWindow::refresh()
{
    setUpdatesEnabled(false);

    const QString pattern = patternComboBox->currentText();
    const QString text = textComboBox->currentText();

    escapedPatternLineEdit->setText(escapePattern(pattern));

    QRegExp rx(pattern);
    const Qt::CaseSensitivity cs = caseSensitiveCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;
    rx.setCaseSensitivity(cs);
    rx.setMinimal(minimalCheckBox->isChecked());
    const QRegExp::PatternSyntax syntax = QRegExp::PatternSyntax(
            syntaxComboBox->itemData(syntaxComboBox->currentIndex()).toInt());
    rx.setPatternSyntax(syntax);

    QPalette palette = patternComboBox->palette();
    if (rx.isValid()) {
        palette.setColor(QPalette::Text,
                         textComboBox->palette().color(QPalette::Text));
    } else {
        palette.setColor(QPalette::Text, Qt::red);
    }
    patternComboBox->setPalette(palette);

    indexEdit->setText(QString::number(rx.indexIn(text)));
    matchedLengthEdit->setText(QString::number(rx.matchedLength()));
    for (int i = 0; i < MaxCaptures; ++i) {
        const bool enabled = i <= rx.numCaptures();
        captureLabels[i]->setEnabled(enabled);
        captureEdits[i]->setEnabled(enabled);
        captureEdits[i]->setText(rx.cap(i));
    }

    setUpdatesEnabled(true);
}

static void saveTextCombo(const QComboBox *cb, QString &current, QStringList &items)
{
    current =  cb->currentText();
    items.clear();
    if (const int count = cb->count())
        for (int i = 0;i <  count; i++) {
            const QString text = cb->itemText(i);
            if (items.indexOf(text) == -1)
                items += text;
        }
}

Settings RegExpWindow::settings() const
{
    Settings rc;
    rc.m_patternSyntax = static_cast<QRegExp::PatternSyntax>(syntaxComboBox->itemData(syntaxComboBox->currentIndex()).toInt());
    rc.m_minimal = minimalCheckBox->checkState() == Qt::Checked;
    rc.m_caseSensitive = caseSensitiveCheckBox->checkState() == Qt::Checked;
    saveTextCombo(patternComboBox, rc.m_currentPattern, rc.m_patterns);
    saveTextCombo(textComboBox,  rc.m_currentMatch, rc.m_matches);
    return rc;
}

static void restoreTextCombo(const QString &current, const QStringList &items, QComboBox *cb)
{
    cb->clear();
    cb->addItems(items);
    cb->lineEdit()->setText(current);
}

void RegExpWindow::setSettings(const Settings &s)
{
    const int patternIndex = syntaxComboBox->findData(QVariant(s.m_patternSyntax));
    syntaxComboBox->setCurrentIndex(patternIndex);
    minimalCheckBox->setCheckState(s.m_minimal ? Qt::Checked : Qt::Unchecked);
    caseSensitiveCheckBox->setCheckState(s.m_caseSensitive ? Qt::Checked : Qt::Unchecked);
    restoreTextCombo(s.m_currentPattern, s.m_patterns, patternComboBox);
    restoreTextCombo(s.m_currentMatch, s.m_matches, textComboBox);
}

void RegExpWindow::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu menu(this);

    QAction *enterQuotedAction = menu.addAction(tr("Enter pattern from code..."));
    connect(enterQuotedAction, SIGNAL(triggered()), this, SLOT(enterEscaped()));
    menu.addSeparator();

    QAction *clearPatternsAction = menu.addAction(tr("Clear patterns"));
    connect(clearPatternsAction, SIGNAL(triggered()), patternComboBox, SLOT(clear()));

    QAction *clearTextsAction = menu.addAction(tr("Clear texts"));
    connect(clearTextsAction, SIGNAL(triggered()), textComboBox, SLOT(clear()));

    event->accept();
    menu.exec(event->globalPos());
}

void  RegExpWindow::enterEscaped()
{
    const QString escapedPattern = QInputDialog::getText (this, tr("Enter pattern from code"), tr("Pattern"));
    if ( escapedPattern.isEmpty())
        return;
    patternComboBox->lineEdit()->setText(unescapePattern(escapedPattern));

}