+ port RegExp utility to Qt4

This commit is contained in:
wmayer 2014-08-21 17:32:30 +02:00
parent 1b4fb56cdc
commit 811fb3be24
7 changed files with 352 additions and 243 deletions

View File

@ -0,0 +1,60 @@
project(RegExp)
set(APP_VERSION "1.0")
cmake_minimum_required(VERSION 2.6.0 FATAL_ERROR)
find_package(Qt4)
include_directories(
${QT_INCLUDE_DIR}
${QT_QTCORE_INCLUDE_DIR}
${QT_QTGUI_INCLUDE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
)
link_directories(${QT_LIBRARY_DIR})
add_definitions(-D_UNICODE)
QT4_WRAP_CPP(RegExp_MOC_SRCS
regexpdialog.h
)
QT4_WRAP_UI(RegExp_UIC_HDRS
regexpdialog.ui
)
SET(RegExp_SRCS
${RegExp_UIC_HDRS}
${RegExp_MOC_SRCS}
main.cpp
regexpdialog.cpp
regexpdialog.h
)
if (WIN32)
SET(RegExp_LIBS
debug qtmaind.lib
optimized qtmain.lib
)
endif(WIN32)
SET(RegExp_LIBS
${RegExp_LIBS}
${QT_QTCORE_LIBRARY_DEBUG}
${QT_QTCORE_LIBRARY}
${QT_QTGUI_LIBRARY_DEBUG}
${QT_QTGUI_LIBRARY}
${QT_QTXML_LIBRARY_DEBUG}
${QT_QTXML_LIBRARY}
)
add_executable(RegExp WIN32 ${RegExp_SRCS})
target_link_libraries(RegExp ${RegExp_LIBS})
set_target_properties(RegExp PROPERTIES OUTPUT_NAME "RegExp")
set_target_properties(RegExp PROPERTIES DEBUG_OUTPUT_NAME "RegExpD")
# dirty hack to avoid Debug/Release subdirectory
set_target_properties(RegExp PROPERTIES PREFIX "../")
set_target_properties(RegExp PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

View File

@ -1,11 +1,13 @@
######################################################################
# Automatically generated by qmake (1.06c) Sat Apr 9 10:49:46 2005
# Automatically generated by qmake (2.01a) Di 3. Jul 11:56:12 2012
######################################################################
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
# Input
HEADERS += regexpdialog.h
INTERFACES += regexpdialogbase.ui
FORMS += regexpdialog.ui
SOURCES += main.cpp regexpdialog.cpp

View File

@ -27,13 +27,12 @@
int main( int argc, char** argv )
{
QApplication app( argc, argv );
QApplication app(argc, argv);
RegExpDialog dialog( 0, 0, TRUE );
app.setMainWidget(&dialog);
RegExpDialog dialog(0);
app.setActiveWindow(&dialog);
dialog.exec();
dialog.exec();
return 0;
return 0;
}

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();
}

View File

@ -24,24 +24,27 @@
#ifndef REG_EXP_DIALOG_H
#define REG_EXP_DIALOG_H
#include "regexpdialogbase.h"
#include <qdialog.h>
#include <qregexp.h>
#include <qsyntaxhighlighter.h>
class RegExpSyntaxHighlighter;
class RegExpDialog : public RegExpDialogBase
class Ui_RegExpDialog;
class RegExpDialog : public QDialog
{
Q_OBJECT
Q_OBJECT
public:
RegExpDialog( QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags f = 0 );
~RegExpDialog();
RegExpDialog(QWidget* parent = 0);
~RegExpDialog();
void about();
void about();
protected:
void performRegExp();
RegExpSyntaxHighlighter* rxhilighter;
protected Q_SLOTS:
void performRegExp();
private:
RegExpSyntaxHighlighter* rxhilighter;
Ui_RegExpDialog* ui;
};
// -------------------------------------------------------------
@ -49,15 +52,16 @@ protected:
class RegExpSyntaxHighlighter : public QSyntaxHighlighter
{
public:
RegExpSyntaxHighlighter ( QTextEdit * textEdit );
~RegExpSyntaxHighlighter();
RegExpSyntaxHighlighter (QTextEdit * textEdit);
~RegExpSyntaxHighlighter();
int highlightParagraph ( const QString & text, int endStateOfLastPara );
void highlightMatchedText( const QRegExp& );
void resethighlight();
void highlightBlock (const QString & text);
//int highlightParagraph ( const QString & text, int endStateOfLastPara );
void highlightMatchedText( const QRegExp& );
void resethighlight();
private:
QRegExp regexp;
QRegExp regexp;
};
#endif // REG_EXP_DIALOG_H

View File

@ -0,0 +1,180 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>RegExpDialog</class>
<widget class="QDialog" name="RegExpDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>519</width>
<height>285</height>
</rect>
</property>
<property name="windowTitle">
<string>RegExp Explorer</string>
</property>
<property name="sizeGripEnabled">
<bool>true</bool>
</property>
<layout class="QGridLayout">
<property name="margin">
<number>10</number>
</property>
<property name="spacing">
<number>6</number>
</property>
<item row="4" column="0">
<widget class="QLineEdit" name="lineEdit1"/>
</item>
<item row="2" column="0">
<layout class="QHBoxLayout">
<property name="spacing">
<number>6</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QCheckBox" name="checkBox1">
<property name="text">
<string>Case sensitive</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox2">
<property name="text">
<string>Wildcard</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox3">
<property name="text">
<string>Minimal</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item row="5" column="0">
<layout class="QHBoxLayout">
<property name="spacing">
<number>6</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="textLabel3">
<property name="text">
<string>Status:</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="textLabel4">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Expanding</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>80</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="buttonClose">
<property name="windowTitle">
<string/>
</property>
<property name="text">
<string>&amp;Close</string>
</property>
<property name="autoDefault">
<bool>true</bool>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonHelp">
<property name="text">
<string>&amp;Help</string>
</property>
<property name="autoDefault">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item row="3" column="0">
<widget class="QLabel" name="textLabel2">
<property name="text">
<string>Regular Expression:</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="textLabel1">
<property name="text">
<string>Text:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QTextEdit" name="textEdit1"/>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<tabstops>
<tabstop>textEdit1</tabstop>
<tabstop>checkBox1</tabstop>
<tabstop>checkBox2</tabstop>
<tabstop>checkBox3</tabstop>
<tabstop>lineEdit1</tabstop>
<tabstop>buttonClose</tabstop>
<tabstop>buttonHelp</tabstop>
</tabstops>
<resources/>
<connections>
<connection>
<sender>buttonClose</sender>
<signal>clicked()</signal>
<receiver>RegExpDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>400</x>
<y>263</y>
</hint>
<hint type="destinationlabel">
<x>312</x>
<y>283</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -1,166 +0,0 @@
<ui version="4.0" >
<author></author>
<comment></comment>
<exportmacro></exportmacro>
<class>RegExpDialogBase</class>
<widget class="QDialog" name="RegExpDialogBase" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>519</width>
<height>285</height>
</rect>
</property>
<property name="windowTitle" >
<string>RegExp Explorer</string>
</property>
<property name="sizeGripEnabled" >
<bool>true</bool>
</property>
<layout class="QGridLayout" >
<property name="margin" >
<number>10</number>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item row="4" column="0" >
<widget class="QLineEdit" name="lineEdit1" />
</item>
<item row="2" column="0" >
<layout class="QHBoxLayout" >
<property name="margin" >
<number>0</number>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item>
<widget class="QCheckBox" name="checkBox1" >
<property name="text" >
<string>Case sensitive</string>
</property>
<property name="checked" >
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox2" >
<property name="text" >
<string>Wildcard</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox3" >
<property name="text" >
<string>Minimal</string>
</property>
<property name="checked" >
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item row="5" column="0" >
<layout class="QHBoxLayout" >
<property name="margin" >
<number>0</number>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item>
<widget class="QLabel" name="textLabel3" >
<property name="text" >
<string>Status:</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="textLabel4" >
<property name="text" >
<string/>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType" >
<enum>QSizePolicy::Expanding</enum>
</property>
<property name="sizeHint" >
<size>
<width>80</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="buttonClose" >
<property name="windowTitle" >
<string/>
</property>
<property name="text" >
<string>&amp;Close</string>
</property>
<property name="autoDefault" >
<bool>true</bool>
</property>
<property name="default" >
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonHelp" >
<property name="text" >
<string>&amp;Help</string>
</property>
<property name="autoDefault" >
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item row="3" column="0" >
<widget class="QLabel" name="textLabel2" >
<property name="text" >
<string>Regular Expression:</string>
</property>
</widget>
</item>
<item row="0" column="0" >
<widget class="QLabel" name="textLabel1" >
<property name="text" >
<string>Text:</string>
</property>
</widget>
</item>
<item row="1" column="0" >
<widget class="QTextEdit" name="textEdit1" />
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11" />
<pixmapfunction>qPixmapFromMimeSource</pixmapfunction>
<tabstops>
<tabstop>textEdit1</tabstop>
<tabstop>checkBox1</tabstop>
<tabstop>checkBox2</tabstop>
<tabstop>checkBox3</tabstop>
<tabstop>lineEdit1</tabstop>
<tabstop>buttonClose</tabstop>
<tabstop>buttonHelp</tabstop>
</tabstops>
<resources/>
<connections/>
</ui>