summaryrefslogtreecommitdiffstats
path: root/examples/regexptester/regexptester.cpp
blob: 326c020489efe47dbff1716a5d7d15b644f58b0f (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
#include <qapplication.h>
#include <qcheckbox.h>
#include <qclipboard.h>
#include <qcombobox.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qpushbutton.h>
#include <qregexp.h>
#include <qstatusbar.h>
#include <qtable.h>

#include "regexptester.h"


RegexpTester::RegexpTester(TQWidget* parent, const char* name, bool modal,
			   WFlags f)
    : TQDialog(parent, name, modal, f)
{
    regexLabel = new TQLabel(this);
    regexComboBox = new TQComboBox(this);
    regexComboBox->setEditable(true);
    regexComboBox->setSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Preferred);
    regexLabel->setBuddy(regexComboBox);
    textLabel = new TQLabel(this);
    textComboBox = new TQComboBox(this);
    textComboBox->setEditable(true);
    textComboBox->setSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Preferred);
    textLabel->setBuddy(textComboBox);
    caseSensitiveCheckBox = new TQCheckBox(this);
    caseSensitiveCheckBox->setChecked(true);
    minimalCheckBox = new TQCheckBox(this);
    wildcardCheckBox = new TQCheckBox(this);
    resultTable = new TQTable(3, 3, this);
    resultTable->verticalHeader()->hide();
    resultTable->setLeftMargin(0);
    resultTable->horizontalHeader()->hide();
    resultTable->setTopMargin(0);
    resultTable->setReadOnly(true);
    executePushButton = new TQPushButton(this);
    executePushButton->setDefault(true);
    copyPushButton = new TQPushButton(this);
    tquitPushButton = new TQPushButton(this);
    statusBar = new TQStatusBar(this);

    TQGridLayout *gridLayout = new TQGridLayout(2, 2, 6);
    gridLayout->addWidget(regexLabel, 0, 0);
    gridLayout->addWidget(regexComboBox, 0, 1);
    gridLayout->addWidget(textLabel, 1, 0);
    gridLayout->addWidget(textComboBox, 1, 1);
    TQHBoxLayout *checkboxLayout = new TQHBoxLayout(0, 6, 6);
    checkboxLayout->addWidget(caseSensitiveCheckBox);
    checkboxLayout->addWidget(minimalCheckBox);
    checkboxLayout->addWidget(wildcardCheckBox);
    checkboxLayout->addStretch(1);
    TQVBoxLayout *buttonLayout = new TQVBoxLayout(0, 6, 6);
    buttonLayout->addWidget(executePushButton);
    buttonLayout->addWidget(copyPushButton);
    buttonLayout->addWidget(tquitPushButton);
    buttonLayout->addStretch(1);
    TQHBoxLayout *middleLayout = new TQHBoxLayout(0, 6, 6);
    middleLayout->addWidget(resultTable);
    middleLayout->addLayout(buttonLayout);
    TQVBoxLayout *mainLayout = new TQVBoxLayout(this, 6, 6);
    mainLayout->addLayout(gridLayout);
    mainLayout->addLayout(checkboxLayout);
    mainLayout->addLayout(middleLayout);
    mainLayout->addWidget(statusBar);

    resize(TQSize(500, 350).expandedTo(minimumSizeHint()));

    languageChange();

    connect(copyPushButton, SIGNAL(clicked()), this, SLOT(copy()));
    connect(executePushButton, SIGNAL(clicked()), this, SLOT(execute()));
    connect(tquitPushButton, SIGNAL(clicked()), this, SLOT(accept()));

    execute();
}

void RegexpTester::execute()
{
    TQString regex = regexComboBox->currentText();
    TQString text = textComboBox->currentText();
    if (!regex.isEmpty() && !text.isEmpty()) {
	TQRegExp re(regex);
	re.setCaseSensitive(caseSensitiveCheckBox->isChecked());
	re.setMinimal(minimalCheckBox->isChecked());
	bool wildcard = wildcardCheckBox->isChecked();
	re.setWildcard(wildcard);
	if (!re.isValid()) {
	    statusBar->message(tr("Invalid regular expression: %1")
				.arg(re.errorString()));
	    return;
	}
	int offset = re.search(text);
	int captures = re.numCaptures();
	int row = 0;
	const int OFFSET = 5;
	resultTable->setNumRows(0);
	resultTable->setNumRows(captures + OFFSET);
	resultTable->setText(row, 0, tr("Regex"));
	TQString escaped = regex;
	escaped = escaped.replace("\\", "\\\\");
	resultTable->setText(row, 1, escaped);
	resultTable->item(row, 1)->setSpan(1, 2);
	if (offset != -1) {
	    ++row;
	    resultTable->setText(row, 0, tr("Offset"));
	    resultTable->setText(row, 1, TQString::number(offset));
	    resultTable->item(row, 1)->setSpan(1, 2);
	    if (!wildcard) {
		++row;
		resultTable->setText(row, 0, tr("Captures"));
		resultTable->setText(row, 1, TQString::number(captures));
		resultTable->item(row, 1)->setSpan(1, 2);
		++row;
		resultTable->setText(row, 1, tr("Text"));
		resultTable->setText(row, 2, tr("Characters"));
	    }
	    ++row;
	    resultTable->setText(row, 0, tr("Match"));
	    resultTable->setText(row, 1, re.cap(0));
	    resultTable->setText(row, 2, TQString::number(re.matchedLength()));
	    if (!wildcard) {
		for (int i = 1; i <= captures; ++i) {
		    resultTable->setText(row + i, 0, tr("Capture #%1").arg(i));
		    resultTable->setText(row + i, 1, re.cap(i));
		    resultTable->setText(row + i, 2,
					TQString::number(re.cap(i).length()));
		}
	    }
	    else
		resultTable->setNumRows(3);
	}
	else {
	    resultTable->setNumRows(2);
	    ++row;
	    resultTable->setText(row, 0, tr("No matches"));
	    resultTable->item(row, 0)->setSpan(1, 3);
	}
	resultTable->adjustColumn(0);
	resultTable->adjustColumn(1);
	resultTable->adjustColumn(2);
	statusBar->message(tr("Executed \"%1\" on \"%2\"")
				.arg(escaped).arg(text));
    }
    else
	statusBar->message(tr("A regular expression and a text must be given"));
}

void RegexpTester::copy()
{
    TQString escaped = regexComboBox->currentText();
    if (!escaped.isEmpty()) {
	escaped = escaped.replace("\\", "\\\\");
	TQClipboard *cb = TQApplication::clipboard();
	cb->setText(escaped, TQClipboard::Clipboard);
	if (cb->supportsSelection())
	    cb->setText(escaped, TQClipboard::Selection);
	statusBar->message(tr("Copied \"%1\" to the clipboard")
				.arg(escaped));
    }
}

void RegexpTester::languageChange()
{
    setCaption(tr("Regex Tester"));
    regexLabel->setText(tr("&Regex:"));
    regexComboBox->insertItem(tr("[A-Z]+=(\\d+):(\\d*)"));
    textLabel->setText(tr("&Text:"));
    textComboBox->insertItem(tr("ABC=12:3456"));
    caseSensitiveCheckBox->setText(tr("Case &Sensitive"));
    minimalCheckBox->setText(tr("&Minimal"));
    wildcardCheckBox->setText(tr("&Wildcard"));
    copyPushButton->setText(tr("&Copy"));
    executePushButton->setText(tr("&Execute"));
    tquitPushButton->setText(tr("&Quit"));
}