[go: up one dir, main page]

File: qaregexpwidget.cpp

package info (click to toggle)
regexplorer 0.1.3-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 332 kB
  • ctags: 222
  • sloc: cpp: 1,166; perl: 99; makefile: 45; sh: 32
file content (539 lines) | stat: -rw-r--r-- 15,408 bytes parent folder | download
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// 
// $Id: qaregexpwidget.cpp,v 1.3 1999/08/16 02:01:39 amos Exp $
//
// Implementation of QRegExpWidget class
//
// Jan Borsodi <amos@abn.hibu.no>
// Created on: <21-Jul-1999 21:22:46 amos>
//
// Copyright (C) 1999 Jan Borsodi.  All rights reserved.
//

#include "qaregexpwidget.hpp"
#include <qaregexp.hpp>
#include <qaregexperror.hpp>

#include <qframe.h>
#include <qlayout.h>
#include <qwidget.h>
#include <qstring.h>
#include <qlineedit.h>
#include <qmultilinedit.h>
#include <qtextview.h>
#include <qsplitter.h>
#include <qlabel.h>
#include <qspinbox.h>
#include <qcolor.h>
#include <qstylesheet.h>
#include <qmainwindow.h>
#include <qpopupmenu.h>
#include <qmenubar.h>
#include <qstatusbar.h>
#include <qvgroupbox.h>
#include <qlistbox.h>
#include <qpoint.h>
#include <qrect.h>

/*!
  \class QRegExpWidget qregexpwidget.hpp
  \brief A widget with the possibility to see regexps in action.

  Has a text field for regexp, a multiline edit for the text,
  and a output text view. When a regexp matches it will display
  the match in a different color, sub expressions can also be matched.
*/

/*!
  Default constructor
*/

QaRegExpWidget::QaRegExpWidget( QWidget *parent, const char *name )
    : QMainWindow( parent, name ), Reg( "" )
{
    setCaption( "RegExplorer - 0.1.3" );
    SubValue = 0;
    MatchCount = 0;
    SubMatch = 0;
    Multiple = false;
    Central = new QWidget( this );
    QVBoxLayout *clay = new QVBoxLayout( Central, 1 );
    QSplitter *MainSplit = new QSplitter( Horizontal, Central );
    clay->addWidget( MainSplit, 1 );
    MainSplit->setOpaqueResize( true );
    QWidget *LeftSplit = new QWidget( MainSplit, "left split widget" );
    Lay = new QVBoxLayout( LeftSplit, 0, 3 );
    {
        QHBoxLayout *hlay = new QHBoxLayout();
        Lay->addLayout( hlay, 0 );
        {
            QLabel *patlab = new QLabel( tr( "Pattern:" ), LeftSplit );
            hlay->addWidget( patlab, 0 );
            QLineEdit *line = new QLineEdit( LeftSplit );
            hlay->addWidget( line, 0 );
            connect( line, SIGNAL( textChanged( const QString & ) ),
                     this, SLOT( textChange( const QString & ) ) );
        }

        QSplitter *split = new QSplitter( Horizontal, LeftSplit );
        split->setOpaqueResize( true );
        Lay->addWidget( split, 1 );
        {
            Edit = new QMultiLineEdit( split );
            Edit->setText( "void test::setText( const QString &s )\n"
                           "{\n"
                           "}\n" );
            connect( Edit, SIGNAL( textChanged() ),
                     this, SLOT( multiTextChanged() ) );

            TextVu = new QTextView( split );
            TextVu->setPaper( QColor( 150, 152, 255 ) );
            TextVu->setText( "void test::setText( const QString &s )<br>"
                             "{<br>"
                             "}<br>" );
            TextVu->setFocusPolicy( NoFocus );
        }
        QValueList<int> ilst;
        ilst.append( 1 );
        ilst.append( 1 );
        split->setSizes( ilst );

        QSplitter *SubSplit = new QSplitter( Vertical, MainSplit );
        {
            Sub = new QListBox( SubSplit );
            connect( Sub, SIGNAL( highlighted( int ) ),
                     this, SLOT( valueChange( int ) ) );

            Matches = new QListBox( SubSplit );
            connect( Matches, SIGNAL( highlighted( int ) ),
                     this, SLOT( valueChangeMultiple( int ) ) );
            Matches->hide();
        }
        SubSplit->setOpaqueResize( true );

        ErrLabel = new QLabel( " ", statusBar() );
        statusBar()->addWidget( ErrLabel, 1 );
    }
    QValueList<int> ilst;
    ilst.append( 4 );
    ilst.append( 1 );
    MainSplit->setSizes( ilst );
    setCentralWidget( Central );

    FileMenu = new QPopupMenu( 0, "file menu" );
    {
        menuBar()->insertItem( tr( "&File" ), FileMenu );

        FileMenu->setItemEnabled( FileMenu->insertItem( tr( "&Load" ), this, SLOT( doLoad() ), CTRL + Key_L ), false );
        FileMenu->setItemEnabled( FileMenu->insertItem( tr( "&Save" ), this, SLOT( doSave() ), CTRL + Key_S ), false );
        FileMenu->insertSeparator();
        FileMenu->insertItem( tr( "&Exit" ), this, SLOT( close() ), CTRL + Key_Q );
    }

    EditMenu = new QPopupMenu( 0, "modes menu" );
    {
        menuBar()->insertItem( tr( "&Edit" ), EditMenu );

        EditMenu->setItemEnabled( EditMenu->insertItem( tr( "&New Group" ) ), false );
        EditMenu->setItemEnabled( EditMenu->insertItem( tr( "&Delete Group" ) ), false );
        EditMenu->setItemEnabled( EditMenu->insertItem( tr( "&Add RegExp" ) ), false );
        EditMenu->setItemEnabled( EditMenu->insertItem( tr( "&Remove RegExp" ) ), false );
    }

    MatchMenu = new QPopupMenu( 0, "match menu" );
    {
        menuBar()->insertItem( tr( "&Match Type" ), MatchMenu );

        MatchNormalID = MatchMenu->insertItem( tr( "&Normal" ), this, SLOT( doNormal() ), CTRL + Key_N );
        MatchMultipleID = MatchMenu->insertItem( tr( "&Multiple" ), this, SLOT( doMultiple() ), CTRL + Key_M );
        MatchMenu->setItemEnabled( MatchMenu->insertItem( tr( "&Split" ) ), false );
        MatchMenu->insertSeparator();
        MatchCaseID = MatchMenu->insertItem( tr( "&Case Sensitive" ), this, SLOT( doCase() ), CTRL + Key_I );

        MatchMenu->setCheckable( true );
        MatchMenu->setItemChecked( MatchNormalID, true );
        MatchMenu->setItemChecked( MatchCaseID, true );
    }

    ModesMenu = new QPopupMenu( 0, "modes menu" );
    {
        menuBar()->insertItem( tr( "Em&ulation Mode" ), ModesMenu );

        ModesMenu->setItemEnabled( ModesMenu->insertItem( tr( "&None" ) ), false );
        ModesMenu->setItemEnabled( ModesMenu->insertItem( tr( "&Lisp" ) ), false );
        ModesMenu->setItemEnabled( ModesMenu->insertItem( tr( "&Perl" ) ), false );
        ModesMenu->setItemEnabled( ModesMenu->insertItem( tr( "&Python" ) ), false );

        ModesMenu->setCheckable( true );
        ModesMenu->setItemChecked( ModesMenu->idAt( 0 ), true );
    }

    QString s;
    for ( int i = 0; i < Edit->numLines(); i++ )
    {
        s += Edit->textLine( i );
        if ( i < Edit->numLines() )
            s+= "\n";
    }
    EditString = s;
    textChange( "" );
}

void QaRegExpWidget::doNormal()
{
    if ( MatchMenu->isItemChecked( MatchNormalID ) )
        return;
    MatchMenu->setItemChecked( MatchNormalID, true );
    MatchMenu->setItemChecked( MatchMultipleID, false );
    Multiple = false;

    Matches->setEnabled( false );
    updateMatch();

    // Fix for listbox repaint bug
    Matches->hide();
//    Matches->show();
}

void QaRegExpWidget::doMultiple()
{
    if ( !MatchMenu->isItemChecked( MatchNormalID ) )
        return;
    MatchMenu->setItemChecked( MatchNormalID, false );
    MatchMenu->setItemChecked( MatchMultipleID, true );
    Multiple = true;

    Matches->setEnabled( true );
    updateMatch();

    // Fix for listbox repaint bug
//    Matches->hide();
    Matches->show();
}

void QaRegExpWidget::doLoad()
{
}

void QaRegExpWidget::doSave()
{
}

void QaRegExpWidget::doCase()
{
    MatchMenu->setItemChecked( MatchCaseID, !MatchMenu->isItemChecked( MatchCaseID ) );
    Reg.setCaseSensitive( MatchMenu->isItemChecked( MatchCaseID ) );
    updateMatch();
}

/*!
  Destroys the object
*/

QaRegExpWidget::~QaRegExpWidget()
{
    delete Lay;
}

/*!
  Text has changed
*/

void QaRegExpWidget::textChange( const QString &t )
{
    QString regstr = t;
    convertText( regstr );
    try
    {
        Reg.setExpression( regstr );
        Reg.setCaseSensitive( MatchMenu->isItemChecked( MatchCaseID ) );
        if ( Multiple )
        {
            if ( SubMatch >= MatchCount )
                SubMatch = MatchCount - 1;
        }
        if ( SubValue > Reg.subCount() )
            SubValue = Reg.subCount();
        if ( SubMatch < 0 )
            SubMatch = 0;
        updateMatch();
    }
    catch ( QaRegExpError &e )
    {
        switch ( e.errorCode() )
        {
            case QaRegExpError::UnmatchedBracket:
            case QaRegExpError::InvalidRange:
            case QaRegExpError::UnmatchedParenthesis:
                ErrLabel->setText( tr( "Incomplete input" ) );
                break;
            default:
                ErrLabel->setText( e.error() );
        }
    }
}

/*!
  Converts backslashified expressions to their correct ascii values
*/

void QaRegExpWidget::convertText( QString &s )
{
    int i = 0;
    while ( ( i = s.find( "\\", i ) ) != -1 )
    {
        QChar c = s.at( i+1 );
        if ( c == "t" )
            s.replace( i, 2, "\t" );
        else if ( c == "n" )
            s.replace( i, 2, "\n" );
        else if ( c == "r" )
            s.replace( i, 2, "\r" );
        else if ( c == "f" )
            s.replace( i, 2, "\f" );
        else if ( c == "d" )
        {
            s.replace( i, 2, "[0-9]" );
            i += 4;
        }
        else if ( c == "D" )
        {
            s.replace( i, 2, "[^0-9]" );
            i += 4;
        }
        else if ( c == "w" )
        {
            QString str( QString( "[" ) + wordCharacters() + QString( "]" ) );
            s.replace( i, 2, str );
            i += str.length() - 1;
        }
        else if ( c == "W" )
        {
            QString str( QString( "[^" ) + wordCharacters() + QString( "]" ) );
            s.replace( i, 2, str );
            i += str.length() - 1;
        }
        else if ( c == "s" )
        {
            QString str( "[ \t\n\r\f]" );
            s.replace( i, 2, str );
            i += str.length() - 1;
        }
        else if ( c == "S" )
        {
            QString str( "[^ \t\n\r\f]" );
            s.replace( i, 2, str );
            i += str.length() - 1;
        }
        else
            i++;
        i++;
    }
}

QString QaRegExpWidget::wordCharacters()
{
    return QString( tr( "a-zA-Z_0-9" ) );
}


/*!
  Adds <br> where \n is located.
*/

QString QaRegExpWidget::toHtml( const QString &s )
{
    return QStyleSheet::convertFromPlainText( s );
}

/*!
  Called whenever the sub expression value changes.
*/

void QaRegExpWidget::valueChange( int val )
{
    if ( SubValue == val )
        return;
    SubValue = val;
    if ( Multiple )
        updateMatch();
    else
        updateOutput();
}

/*!
  Called whenever the sub expression value changes.
*/

void QaRegExpWidget::valueChangeMultiple( int val )
{
    if ( SubMatch == val )
        return;
    SubMatch = val;
    updateMatch();
}

/*!
  Update the output
*/

void QaRegExpWidget::updateOutput()
{
    if ( Multiple )
    {
        if ( Reg.matchBeginning( SubMatch ) != -1 )
        {
            QString st = Reg.matchString( SubMatch );
            QString str = EditString;
            QString font( "<font color=#ffff00><b>" );
            QString fontback( "</b></font>" );
            QaRegExpRange range = Reg.matches()[SubMatch];
            int index1 = range.start();
            int index2 = range.start() + range.length();

            QString front,back,mid;
            front = str.mid( 0, index1 );
            mid = str.mid( index1, range.length() );
            back = str.mid( index2 );

            front = toHtml( front );
            front = front.mid( 3, front.length() - 7 );
            mid = toHtml( mid );
            mid = mid.mid( 3, mid.length() - 7 );
            back = toHtml( back );
            back = back.mid( 3, back.length() - 7 );
            str = front + font + mid + fontback + back;
            TextVu->setText( str );
            ErrLabel->setText( tr( "Match" ) );
        }
        else
        {
            QString str = QStyleSheet::convertFromPlainText( EditString );
            str = str.mid( 3, str.length() - 7 );
            TextVu->setText( str );
            ErrLabel->setText( tr( "No Match" ) );
        }
    }
    else
    {
        if ( Reg.matchBeginning( SubValue ) != -1 )
        {
            QString st = Reg.matchString( SubValue );
            QString str = EditString;
            QString font( "<font color=#ffff00><b>" );
            QString fontback( "</b></font>" );
            QaRegExpRange range = Reg.matches()[SubValue];
            int index1 = range.start();
            int index2 = range.start() + range.length();

            QString front,back,mid;
            front = str.mid( 0, index1 );
            mid = str.mid( index1, range.length() );
            back = str.mid( index2 );

            front = toHtml( front );
            front = front.mid( 3, front.length() - 7 );
            mid = toHtml( mid );
            mid = mid.mid( 3, mid.length() - 7 );
            back = toHtml( back );
            back = back.mid( 3, back.length() - 7 );
            str = front + font + mid + fontback + back;
            TextVu->setText( str );
            ErrLabel->setText( tr( "Match" ) );
        }
        else
        {
            QString str = QStyleSheet::convertFromPlainText( EditString );
            str = str.mid( 3, str.length() - 7 );
            TextVu->setText( str );
            ErrLabel->setText( tr( "No Match" ) );
        }
    }
}

/*!
  Text in the multilinedit widget has changed.
*/

void QaRegExpWidget::multiTextChanged()
{
    QString s;
    for ( int i = 0; i < Edit->numLines(); i++ )
    {
        s += Edit->textLine( i );
        if ( i < Edit->numLines() - 1 )
            s+= "\n";
    }
    EditString = s;
    updateMatch();
}

/*!
  Do a rematch of the regexp.
*/

void QaRegExpWidget::updateMatch()
{
    if ( Multiple )
    {
        if ( ( MatchCount = Reg.matchMultiple( EditString, SubValue ) ) > 0 )
        {
            if ( SubMatch >= MatchCount )
                SubMatch = MatchCount - 1;
            updateOutput();
        }
        else
        {
            QString str = QStyleSheet::convertFromPlainText( EditString );
            str = str.mid( 3, str.length() - 7 );
            TextVu->setText( str );
            ErrLabel->setText( tr( "NoMatch" ) );
        }
    }
    else
    {
        if ( Reg.match( EditString, SubValue ) )
        {
            updateOutput();
        }
        else
        {
            QString str = QStyleSheet::convertFromPlainText( EditString );
            str = str.mid( 3, str.length() - 7 );
            TextVu->setText( str );
            ErrLabel->setText( tr( "NoMatch" ) );
        }
    }
    Sub->clear();
    Matches->clear();
    if ( Multiple )
    {
        for ( int i = 0; i < MatchCount; i++ )
        {
            QString str;
            str = QString( "%1 - [%2,%3]" ).arg( i ).arg( Reg.matchBeginning(i) ).arg( Reg.matchEnd(i) );
            Matches->insertItem( str, i );
        }
        for ( int i = 0; i < Reg.subCount() + 1; i++ )
        {
            QString str;
            str = QString( "%1" ).arg( i );
            Sub->insertItem( str, i );
        }
        Matches->setSelected( SubMatch, true );
        Matches->setCurrentItem( SubMatch );
    }
    else
    {
        for ( int i = 0; i < Reg.subCount() + 1; i++ )
        {
            QString str;
            if ( Reg.matches()[i].start() != -1 )
                str = QString( "%1 - [%2,%3]" ).arg( i ).arg( Reg.matchBeginning(i) ).arg( Reg.matchEnd(i) );
            else
                str = QString( tr( "(%1) - No match" ) ).arg( i );
            Sub->insertItem( str, i );
        }
    }
    Sub->setSelected( SubValue, true );
    Sub->setCurrentItem( SubValue );
}