examples/tools/regexp/regexpdialog.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 
       
    44 #include "regexpdialog.h"
       
    45 
       
    46 RegExpDialog::RegExpDialog(QWidget *parent)
       
    47     : QDialog(parent)
       
    48 {
       
    49     patternComboBox = new QComboBox;
       
    50     patternComboBox->setEditable(true);
       
    51     patternComboBox->setSizePolicy(QSizePolicy::Expanding,
       
    52                                    QSizePolicy::Preferred);
       
    53 
       
    54     patternLabel = new QLabel(tr("&Pattern:"));
       
    55     patternLabel->setBuddy(patternComboBox);
       
    56 
       
    57     escapedPatternLineEdit = new QLineEdit;
       
    58     escapedPatternLineEdit->setReadOnly(true);
       
    59     QPalette palette = escapedPatternLineEdit->palette();
       
    60     palette.setBrush(QPalette::Base,
       
    61                      palette.brush(QPalette::Disabled, QPalette::Base));
       
    62     escapedPatternLineEdit->setPalette(palette);
       
    63 
       
    64     escapedPatternLabel = new QLabel(tr("&Escaped Pattern:"));
       
    65     escapedPatternLabel->setBuddy(escapedPatternLineEdit);
       
    66 
       
    67     syntaxComboBox = new QComboBox;
       
    68     syntaxComboBox->addItem(tr("Regular expression v1"), QRegExp::RegExp);
       
    69     syntaxComboBox->addItem(tr("Regular expression v2"), QRegExp::RegExp2);
       
    70     syntaxComboBox->addItem(tr("Wildcard"), QRegExp::Wildcard);
       
    71     syntaxComboBox->addItem(tr("Fixed string"), QRegExp::FixedString);
       
    72     syntaxComboBox->addItem(tr("W3C Xml Schema 1.1"), QRegExp::W3CXmlSchema11);
       
    73 
       
    74     syntaxLabel = new QLabel(tr("&Pattern Syntax:"));
       
    75     syntaxLabel->setBuddy(syntaxComboBox);
       
    76 
       
    77     textComboBox = new QComboBox;
       
    78     textComboBox->setEditable(true);
       
    79     textComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
       
    80 
       
    81     textLabel = new QLabel(tr("&Text:"));
       
    82     textLabel->setBuddy(textComboBox);
       
    83 
       
    84     caseSensitiveCheckBox = new QCheckBox(tr("Case &Sensitive"));
       
    85     caseSensitiveCheckBox->setChecked(true);
       
    86     minimalCheckBox = new QCheckBox(tr("&Minimal"));
       
    87 
       
    88     indexLabel = new QLabel(tr("Index of Match:"));
       
    89     indexEdit = new QLineEdit;
       
    90     indexEdit->setReadOnly(true);
       
    91 
       
    92     matchedLengthLabel = new QLabel(tr("Matched Length:"));
       
    93     matchedLengthEdit = new QLineEdit;
       
    94     matchedLengthEdit->setReadOnly(true);
       
    95 
       
    96     for (int i = 0; i < MaxCaptures; ++i) {
       
    97         captureLabels[i] = new QLabel(tr("Capture %1:").arg(i));
       
    98         captureEdits[i] = new QLineEdit;
       
    99         captureEdits[i]->setReadOnly(true);
       
   100     }
       
   101     captureLabels[0]->setText(tr("Match:"));
       
   102 
       
   103     QHBoxLayout *checkBoxLayout = new QHBoxLayout;
       
   104     checkBoxLayout->addWidget(caseSensitiveCheckBox);
       
   105     checkBoxLayout->addWidget(minimalCheckBox);
       
   106     checkBoxLayout->addStretch(1);
       
   107 
       
   108     QGridLayout *mainLayout = new QGridLayout;
       
   109     mainLayout->addWidget(patternLabel, 0, 0);
       
   110     mainLayout->addWidget(patternComboBox, 0, 1);
       
   111     mainLayout->addWidget(escapedPatternLabel, 1, 0);
       
   112     mainLayout->addWidget(escapedPatternLineEdit, 1, 1);
       
   113     mainLayout->addWidget(syntaxLabel, 2, 0);
       
   114     mainLayout->addWidget(syntaxComboBox, 2, 1);
       
   115     mainLayout->addLayout(checkBoxLayout, 3, 0, 1, 2);
       
   116     mainLayout->addWidget(textLabel, 4, 0);
       
   117     mainLayout->addWidget(textComboBox, 4, 1);
       
   118     mainLayout->addWidget(indexLabel, 5, 0);
       
   119     mainLayout->addWidget(indexEdit, 5, 1);
       
   120     mainLayout->addWidget(matchedLengthLabel, 6, 0);
       
   121     mainLayout->addWidget(matchedLengthEdit, 6, 1);
       
   122 
       
   123     for (int j = 0; j < MaxCaptures; ++j) {
       
   124         mainLayout->addWidget(captureLabels[j], 7 + j, 0);
       
   125         mainLayout->addWidget(captureEdits[j], 7 + j, 1);
       
   126     }
       
   127     setLayout(mainLayout);
       
   128 
       
   129     connect(patternComboBox, SIGNAL(editTextChanged(const QString &)),
       
   130             this, SLOT(refresh()));
       
   131     connect(textComboBox, SIGNAL(editTextChanged(const QString &)),
       
   132             this, SLOT(refresh()));
       
   133     connect(caseSensitiveCheckBox, SIGNAL(toggled(bool)),
       
   134             this, SLOT(refresh()));
       
   135     connect(minimalCheckBox, SIGNAL(toggled(bool)), this, SLOT(refresh()));
       
   136     connect(syntaxComboBox, SIGNAL(currentIndexChanged(int)),
       
   137             this, SLOT(refresh()));
       
   138 
       
   139     patternComboBox->addItem(tr("[A-Za-z_]+([A-Za-z_0-9]*)"));
       
   140     textComboBox->addItem(tr("(10 + delta4) * 32"));
       
   141 
       
   142     setWindowTitle(tr("RegExp"));
       
   143     setFixedHeight(sizeHint().height());
       
   144     refresh();
       
   145 }
       
   146 
       
   147 void RegExpDialog::refresh()
       
   148 {
       
   149     setUpdatesEnabled(false);
       
   150 
       
   151     QString pattern = patternComboBox->currentText();
       
   152     QString text = textComboBox->currentText();
       
   153 
       
   154     QString escaped = pattern;
       
   155     escaped.replace("\\", "\\\\");
       
   156     escaped.replace("\"", "\\\"");
       
   157     escaped.prepend("\"");
       
   158     escaped.append("\"");
       
   159     escapedPatternLineEdit->setText(escaped);
       
   160 
       
   161     QRegExp rx(pattern);
       
   162     Qt::CaseSensitivity cs = Qt::CaseInsensitive;
       
   163     if (caseSensitiveCheckBox->isChecked())
       
   164         cs = Qt::CaseSensitive;
       
   165     rx.setCaseSensitivity(cs);
       
   166     rx.setMinimal(minimalCheckBox->isChecked());
       
   167     QRegExp::PatternSyntax syntax = QRegExp::PatternSyntax(
       
   168             syntaxComboBox->itemData(syntaxComboBox->currentIndex()).toInt());
       
   169     rx.setPatternSyntax(syntax);
       
   170 
       
   171     QPalette palette = patternComboBox->palette();
       
   172     if (rx.isValid()) {
       
   173         palette.setColor(QPalette::Text,
       
   174                          textComboBox->palette().color(QPalette::Text));
       
   175     } else {
       
   176         palette.setColor(QPalette::Text, Qt::red);
       
   177     }
       
   178     patternComboBox->setPalette(palette);
       
   179 
       
   180     indexEdit->setText(QString::number(rx.indexIn(text)));
       
   181     matchedLengthEdit->setText(QString::number(rx.matchedLength()));
       
   182     for (int i = 0; i < MaxCaptures; ++i) {
       
   183         captureLabels[i]->setEnabled(i <= rx.numCaptures());
       
   184         captureEdits[i]->setEnabled(i <= rx.numCaptures());
       
   185         captureEdits[i]->setText(rx.cap(i));
       
   186     }
       
   187 
       
   188     setUpdatesEnabled(true);
       
   189 }