00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "regexcheckdialog.h"
00020
00021 RegexCheckDialog::RegexCheckDialog( QWidget* parent ): KDialog( parent ) {
00022
00023
00024 setButtons( KDialog::Ok | KDialog::Cancel | KDialog::Try );
00025
00026
00027 QWidget* mainWidget = new QWidget();
00028
00029
00030 QGridLayout* layMain = new QGridLayout( mainWidget );
00031
00032
00033 QLabel* lblRegEx = new QLabel( i18nc( "@label:textbox the edit line to enter a regular expression", "Regular Expression:" ), mainWidget );
00034 layMain->addWidget( lblRegEx, 0, 0 );
00035 txtRegEx = new KLineEdit( mainWidget );
00036 layMain->addWidget( txtRegEx, 0, 1 );
00037
00038
00039 QLabel* lblTestString = new QLabel( i18nc( "@label:textbox the edit line to enter a phrase to test the regular expression", "Test Phrase:" ), mainWidget );
00040 layMain->addWidget( lblTestString, 1, 0 );
00041 txtTestString = new KLineEdit( mainWidget );
00042 layMain->addWidget( txtTestString, 1, 1 );
00043
00044
00045 QLabel* lblResult = new QLabel( i18nc( "@info:status a line which shows the result of the regex test", "Result:" ), mainWidget );
00046 layMain->addWidget( lblResult, 2, 0 );
00047 txtResult = new QLabel( mainWidget );
00048 txtResult->setAlignment( Qt::AlignCenter );
00049 QFont resultFont = txtResult->font();
00050 resultFont.setBold( true );
00051 txtResult->setFont( resultFont );
00052 layMain->addWidget( txtResult, 2, 1 );
00053
00054
00055 setMainWidget( mainWidget );
00056
00057 }
00058
00059 void RegexCheckDialog::slotButtonClicked(int button)
00060 {
00061
00062
00063 if( button == KDialog::Try ) {
00064
00065
00066 checkRegex();
00067
00068 } else {
00069 KDialog::slotButtonClicked(button);
00070 }
00071 }
00072
00073 void RegexCheckDialog::checkRegex()
00074 {
00075
00076 QString strRegex = txtRegEx->text();
00077 if( strRegex.isEmpty() ) {
00078 writeRed( i18nc( "@info:status no regex entered", "No Regex" ) );
00079 return;
00080 }
00081 QRegExp regex( strRegex );
00082
00083
00084 if( !regex.isValid() ) {
00085 KMessageBox::detailedError( this, i18nc( "@info Error message about an invalid regular expression", "The regular expression is invalid."), regex.errorString(), i18nc( "@title:window Title from error message box about a invalid regular expression", "Invalid regular expression") );
00086 writeRed( i18nc( "@info:status Short error message about an invalid regular expression", "invalid regex") );
00087 return;
00088 }
00089
00090
00091 QString testPhrase = txtTestString->text();
00092 if( testPhrase.isEmpty() ) {
00093 writeRed( i18nc( "@info:status no test phrase entered", "No Test Phrase" ) );
00094 return;
00095 }
00096
00097
00098 if( regex.exactMatch( testPhrase ) ) {
00099 writeGreen( i18nc( "@info:status The test phrase is matched exactly by the regular expression", "Matched" ) );
00100 } else {
00101 writeRed( i18nc( "@info:status The text phrase is not matched by the regular expression", "Failed" ) );
00102 }
00103
00104
00105 }
00106
00107 void RegexCheckDialog::writeBlack( QString text )
00108 {
00109
00110 QPalette pal = txtResult->palette();
00111 pal.setColor( QPalette::Foreground, Qt::black );
00112 txtResult->setPalette( pal );
00113
00114
00115 txtResult->setText( text );
00116
00117
00118 }
00119
00120 void RegexCheckDialog::writeGreen( QString text )
00121 {
00122
00123 QPalette pal = txtResult->palette();
00124 pal.setColor( QPalette::Foreground, Qt::green );
00125 txtResult->setPalette( pal );
00126
00127
00128 txtResult->setText( text );
00129
00130 }
00131
00132 void RegexCheckDialog::writeRed( QString text )
00133 {
00134
00135 QPalette pal = txtResult->palette();
00136 pal.setColor( QPalette::Foreground, Qt::red );
00137 txtResult->setPalette( pal );
00138
00139
00140 txtResult->setText( text );
00141
00142 }
00143
00144 QString RegexCheckDialog::getRegex()
00145 {
00146 return txtRegEx->text();
00147 }
00148
00149 void RegexCheckDialog::setRegex(QString regex)
00150 {
00151 txtRegEx->setText( regex );
00152 }
00153
00154