| 1 | #include <qapplication.h>
|
|---|
| 2 | #include <qcheckbox.h>
|
|---|
| 3 | #include <qclipboard.h>
|
|---|
| 4 | #include <qcombobox.h>
|
|---|
| 5 | #include <qlabel.h>
|
|---|
| 6 | #include <qlayout.h>
|
|---|
| 7 | #include <qpushbutton.h>
|
|---|
| 8 | #include <qregexp.h>
|
|---|
| 9 | #include <qstatusbar.h>
|
|---|
| 10 | #include <qtable.h>
|
|---|
| 11 |
|
|---|
| 12 | #include "regexptester.h"
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 | RegexpTester::RegexpTester(QWidget* parent, const char* name, bool modal,
|
|---|
| 16 | WFlags f)
|
|---|
| 17 | : QDialog(parent, name, modal, f)
|
|---|
| 18 | {
|
|---|
| 19 | regexLabel = new QLabel(this);
|
|---|
| 20 | regexComboBox = new QComboBox(this);
|
|---|
| 21 | regexComboBox->setEditable(true);
|
|---|
| 22 | regexComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
|---|
| 23 | regexLabel->setBuddy(regexComboBox);
|
|---|
| 24 | textLabel = new QLabel(this);
|
|---|
| 25 | textComboBox = new QComboBox(this);
|
|---|
| 26 | textComboBox->setEditable(true);
|
|---|
| 27 | textComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
|---|
| 28 | textLabel->setBuddy(textComboBox);
|
|---|
| 29 | caseSensitiveCheckBox = new QCheckBox(this);
|
|---|
| 30 | caseSensitiveCheckBox->setChecked(true);
|
|---|
| 31 | minimalCheckBox = new QCheckBox(this);
|
|---|
| 32 | wildcardCheckBox = new QCheckBox(this);
|
|---|
| 33 | resultTable = new QTable(3, 3, this);
|
|---|
| 34 | resultTable->verticalHeader()->hide();
|
|---|
| 35 | resultTable->setLeftMargin(0);
|
|---|
| 36 | resultTable->horizontalHeader()->hide();
|
|---|
| 37 | resultTable->setTopMargin(0);
|
|---|
| 38 | resultTable->setReadOnly(true);
|
|---|
| 39 | executePushButton = new QPushButton(this);
|
|---|
| 40 | executePushButton->setDefault(true);
|
|---|
| 41 | copyPushButton = new QPushButton(this);
|
|---|
| 42 | quitPushButton = new QPushButton(this);
|
|---|
| 43 | statusBar = new QStatusBar(this);
|
|---|
| 44 |
|
|---|
| 45 | QGridLayout *gridLayout = new QGridLayout(2, 2, 6);
|
|---|
| 46 | gridLayout->addWidget(regexLabel, 0, 0);
|
|---|
| 47 | gridLayout->addWidget(regexComboBox, 0, 1);
|
|---|
| 48 | gridLayout->addWidget(textLabel, 1, 0);
|
|---|
| 49 | gridLayout->addWidget(textComboBox, 1, 1);
|
|---|
| 50 | QHBoxLayout *checkboxLayout = new QHBoxLayout(0, 6, 6);
|
|---|
| 51 | checkboxLayout->addWidget(caseSensitiveCheckBox);
|
|---|
| 52 | checkboxLayout->addWidget(minimalCheckBox);
|
|---|
| 53 | checkboxLayout->addWidget(wildcardCheckBox);
|
|---|
| 54 | checkboxLayout->addStretch(1);
|
|---|
| 55 | QVBoxLayout *buttonLayout = new QVBoxLayout(0, 6, 6);
|
|---|
| 56 | buttonLayout->addWidget(executePushButton);
|
|---|
| 57 | buttonLayout->addWidget(copyPushButton);
|
|---|
| 58 | buttonLayout->addWidget(quitPushButton);
|
|---|
| 59 | buttonLayout->addStretch(1);
|
|---|
| 60 | QHBoxLayout *middleLayout = new QHBoxLayout(0, 6, 6);
|
|---|
| 61 | middleLayout->addWidget(resultTable);
|
|---|
| 62 | middleLayout->addLayout(buttonLayout);
|
|---|
| 63 | QVBoxLayout *mainLayout = new QVBoxLayout(this, 6, 6);
|
|---|
| 64 | mainLayout->addLayout(gridLayout);
|
|---|
| 65 | mainLayout->addLayout(checkboxLayout);
|
|---|
| 66 | mainLayout->addLayout(middleLayout);
|
|---|
| 67 | mainLayout->addWidget(statusBar);
|
|---|
| 68 |
|
|---|
| 69 | resize(QSize(500, 350).expandedTo(minimumSizeHint()));
|
|---|
| 70 |
|
|---|
| 71 | languageChange();
|
|---|
| 72 |
|
|---|
| 73 | connect(copyPushButton, SIGNAL(clicked()), this, SLOT(copy()));
|
|---|
| 74 | connect(executePushButton, SIGNAL(clicked()), this, SLOT(execute()));
|
|---|
| 75 | connect(quitPushButton, SIGNAL(clicked()), this, SLOT(accept()));
|
|---|
| 76 |
|
|---|
| 77 | execute();
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | void RegexpTester::execute()
|
|---|
| 81 | {
|
|---|
| 82 | QString regex = regexComboBox->currentText();
|
|---|
| 83 | QString text = textComboBox->currentText();
|
|---|
| 84 | if (!regex.isEmpty() && !text.isEmpty()) {
|
|---|
| 85 | QRegExp re(regex);
|
|---|
| 86 | re.setCaseSensitive(caseSensitiveCheckBox->isChecked());
|
|---|
| 87 | re.setMinimal(minimalCheckBox->isChecked());
|
|---|
| 88 | bool wildcard = wildcardCheckBox->isChecked();
|
|---|
| 89 | re.setWildcard(wildcard);
|
|---|
| 90 | if (!re.isValid()) {
|
|---|
| 91 | statusBar->message(tr("Invalid regular expression: %1")
|
|---|
| 92 | .arg(re.errorString()));
|
|---|
| 93 | return;
|
|---|
| 94 | }
|
|---|
| 95 | int offset = re.search(text);
|
|---|
| 96 | int captures = re.numCaptures();
|
|---|
| 97 | int row = 0;
|
|---|
| 98 | const int OFFSET = 5;
|
|---|
| 99 | resultTable->setNumRows(0);
|
|---|
| 100 | resultTable->setNumRows(captures + OFFSET);
|
|---|
| 101 | resultTable->setText(row, 0, tr("Regex"));
|
|---|
| 102 | QString escaped = regex;
|
|---|
| 103 | escaped = escaped.replace("\\", "\\\\");
|
|---|
| 104 | resultTable->setText(row, 1, escaped);
|
|---|
| 105 | resultTable->item(row, 1)->setSpan(1, 2);
|
|---|
| 106 | if (offset != -1) {
|
|---|
| 107 | ++row;
|
|---|
| 108 | resultTable->setText(row, 0, tr("Offset"));
|
|---|
| 109 | resultTable->setText(row, 1, QString::number(offset));
|
|---|
| 110 | resultTable->item(row, 1)->setSpan(1, 2);
|
|---|
| 111 | if (!wildcard) {
|
|---|
| 112 | ++row;
|
|---|
| 113 | resultTable->setText(row, 0, tr("Captures"));
|
|---|
| 114 | resultTable->setText(row, 1, QString::number(captures));
|
|---|
| 115 | resultTable->item(row, 1)->setSpan(1, 2);
|
|---|
| 116 | ++row;
|
|---|
| 117 | resultTable->setText(row, 1, tr("Text"));
|
|---|
| 118 | resultTable->setText(row, 2, tr("Characters"));
|
|---|
| 119 | }
|
|---|
| 120 | ++row;
|
|---|
| 121 | resultTable->setText(row, 0, tr("Match"));
|
|---|
| 122 | resultTable->setText(row, 1, re.cap(0));
|
|---|
| 123 | resultTable->setText(row, 2, QString::number(re.matchedLength()));
|
|---|
| 124 | if (!wildcard) {
|
|---|
| 125 | for (int i = 1; i <= captures; ++i) {
|
|---|
| 126 | resultTable->setText(row + i, 0, tr("Capture #%1").arg(i));
|
|---|
| 127 | resultTable->setText(row + i, 1, re.cap(i));
|
|---|
| 128 | resultTable->setText(row + i, 2,
|
|---|
| 129 | QString::number(re.cap(i).length()));
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 | else
|
|---|
| 133 | resultTable->setNumRows(3);
|
|---|
| 134 | }
|
|---|
| 135 | else {
|
|---|
| 136 | resultTable->setNumRows(2);
|
|---|
| 137 | ++row;
|
|---|
| 138 | resultTable->setText(row, 0, tr("No matches"));
|
|---|
| 139 | resultTable->item(row, 0)->setSpan(1, 3);
|
|---|
| 140 | }
|
|---|
| 141 | resultTable->adjustColumn(0);
|
|---|
| 142 | resultTable->adjustColumn(1);
|
|---|
| 143 | resultTable->adjustColumn(2);
|
|---|
| 144 | statusBar->message(tr("Executed \"%1\" on \"%2\"")
|
|---|
| 145 | .arg(escaped).arg(text));
|
|---|
| 146 | }
|
|---|
| 147 | else
|
|---|
| 148 | statusBar->message(tr("A regular expression and a text must be given"));
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | void RegexpTester::copy()
|
|---|
| 152 | {
|
|---|
| 153 | QString escaped = regexComboBox->currentText();
|
|---|
| 154 | if (!escaped.isEmpty()) {
|
|---|
| 155 | escaped = escaped.replace("\\", "\\\\");
|
|---|
| 156 | QClipboard *cb = QApplication::clipboard();
|
|---|
| 157 | cb->setText(escaped, QClipboard::Clipboard);
|
|---|
| 158 | if (cb->supportsSelection())
|
|---|
| 159 | cb->setText(escaped, QClipboard::Selection);
|
|---|
| 160 | statusBar->message(tr("Copied \"%1\" to the clipboard")
|
|---|
| 161 | .arg(escaped));
|
|---|
| 162 | }
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | void RegexpTester::languageChange()
|
|---|
| 166 | {
|
|---|
| 167 | setCaption(tr("Regex Tester"));
|
|---|
| 168 | regexLabel->setText(tr("&Regex:"));
|
|---|
| 169 | regexComboBox->insertItem(tr("[A-Z]+=(\\d+):(\\d*)"));
|
|---|
| 170 | textLabel->setText(tr("&Text:"));
|
|---|
| 171 | textComboBox->insertItem(tr("ABC=12:3456"));
|
|---|
| 172 | caseSensitiveCheckBox->setText(tr("Case &Sensitive"));
|
|---|
| 173 | minimalCheckBox->setText(tr("&Minimal"));
|
|---|
| 174 | wildcardCheckBox->setText(tr("&Wildcard"));
|
|---|
| 175 | copyPushButton->setText(tr("&Copy"));
|
|---|
| 176 | executePushButton->setText(tr("&Execute"));
|
|---|
| 177 | quitPushButton->setText(tr("&Quit"));
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|