+ port RegExp utility to Qt4

This commit is contained in:
wmayer
2014-08-21 17:32:30 +02:00
parent 2732a324e8
commit 7815fed8df
7 changed files with 352 additions and 243 deletions

View File

@@ -22,6 +22,7 @@
#include "regexpdialog.h"
#include "ui_regexpdialog.h"
#include <qcheckbox.h>
#include <qlabel.h>
@@ -29,51 +30,54 @@
#include <qmessagebox.h>
#include <qtextedit.h>
RegExpDialog::RegExpDialog( QWidget* parent, const char* name, bool modal, WFlags f )
: RegExpDialogBase( parent, name, modal, f )
RegExpDialog::RegExpDialog(QWidget* parent)
: QDialog(parent), ui(new Ui_RegExpDialog())
{
rxhilighter = new RegExpSyntaxHighlighter( textEdit1 );
ui->setupUi(this);
rxhilighter = new RegExpSyntaxHighlighter(ui->textEdit1);
connect(ui->lineEdit1, SIGNAL(textChanged(const QString &)),
this, SLOT(performRegExp()));
}
RegExpDialog::~RegExpDialog()
{
delete ui;
}
void RegExpDialog::performRegExp()
{
QString txt = lineEdit1->text();
if ( txt.isEmpty() )
{
rxhilighter->resethighlight();
return;
}
QString txt = ui->lineEdit1->text();
if (txt.isEmpty()) {
rxhilighter->resethighlight();
return;
}
QRegExp rx(txt);
rx.setCaseSensitive( checkBox1->isChecked() );
rx.setWildcard( checkBox2->isChecked() );
rx.setMinimal( checkBox3->isChecked() );
QRegExp rx(txt);
rx.setCaseSensitivity(ui->checkBox1->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive);
rx.setPatternSyntax(ui->checkBox2->isChecked() ? QRegExp::Wildcard : QRegExp::RegExp);
rx.setMinimal(ui->checkBox3->isChecked());
// evaluate regular expression
textLabel4->setText( rx.errorString() );
if ( !rx.isValid() )
{
rxhilighter->resethighlight();
return; // invalid expression
}
// evaluate regular expression
ui->textLabel4->setText(rx.errorString());
if (!rx.isValid()) {
rxhilighter->resethighlight();
return; // invalid expression
}
rxhilighter->highlightMatchedText( rx );
rxhilighter->highlightMatchedText(rx);
}
void RegExpDialog::about()
{
QString msg = "This is a tool for playing around with regular expressions.";
QMessageBox::information(this, "RegExp Explorer", msg);
QString msg = "This is a tool for playing around with regular expressions.";
QMessageBox::information(this, "RegExp Explorer", msg);
}
// -------------------------------------------------------------
RegExpSyntaxHighlighter::RegExpSyntaxHighlighter ( QTextEdit * textEdit )
: QSyntaxHighlighter( textEdit )
RegExpSyntaxHighlighter::RegExpSyntaxHighlighter (QTextEdit * textEdit)
: QSyntaxHighlighter(textEdit)
{
}
@@ -81,45 +85,71 @@ RegExpSyntaxHighlighter::~RegExpSyntaxHighlighter()
{
}
void RegExpSyntaxHighlighter::highlightBlock (const QString & text)
{
QTextCharFormat regFormat;
regFormat.setForeground(Qt::black);
regFormat.setFontWeight(QFont::Normal);
setFormat(0, text.length(), regFormat);
if (regexp.isEmpty())
return; // empty regular expression
int pos = 0;
int last = -1;
regFormat.setFontWeight(QFont::Bold);
regFormat.setForeground(Qt::blue);
while ((pos = regexp.indexIn(text, pos)) != -1) {
if (last == pos)
break;
QString sub = text.mid(pos, regexp.matchedLength());
if (!sub.isEmpty()) {
setFormat(pos, sub.length(), regFormat);
}
pos += regexp.matchedLength();
last=pos;
}
}
#if 0
int RegExpSyntaxHighlighter::highlightParagraph ( const QString & text, int /*endStateOfLastPara*/ )
{
// reset format
QFont font = textEdit()->currentFont();
font.setBold( false );
setFormat(0, text.length(), font, Qt::black );
// reset format
QFont font = textEdit()->font();
font.setBold( false );
setFormat(0, text.length(), font, Qt::black );
if ( regexp.isEmpty() )
return 0; // empty regular expression
if (regexp.isEmpty())
return 0; // empty regular expression
int pos = 0;
int last = -1;
font.setBold( true );
int pos = 0;
int last = -1;
font.setBold(true);
while ( (pos = regexp.search(text, pos)) != -1 )
{
if ( last == pos )
break;
QString sub = text.mid( pos, regexp.matchedLength());
if ( !sub.isEmpty() )
{
setFormat(pos, sub.length(), font, Qt::blue );
while ( (pos = regexp.indexIn(text, pos)) != -1 ) {
if (last == pos)
break;
QString sub = text.mid( pos, regexp.matchedLength());
if (!sub.isEmpty()) {
setFormat(pos, sub.length(), font, Qt::blue );
}
pos += regexp.matchedLength();
last=pos;
}
pos += regexp.matchedLength();
last=pos;
}
return 0;
return 0;
}
void RegExpSyntaxHighlighter::highlightMatchedText( const QRegExp& rx )
#endif
void RegExpSyntaxHighlighter::highlightMatchedText(const QRegExp& rx)
{
regexp = rx;
rehighlight();
regexp = rx;
rehighlight();
}
void RegExpSyntaxHighlighter::resethighlight()
{
regexp.setPattern("");
rehighlight();
regexp.setPattern("");
rehighlight();
}