File indexing completed on 2024-04-21 05:51:38

0001 /*
0002  *  SPDX-FileCopyrightText: 2002-2003 Jesper K. Pedersen <blackie@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-only
0005  **/
0006 
0007 #include "regexpbuttons.h"
0008 
0009 #include <QButtonGroup>
0010 
0011 #include <KIconLoader>
0012 #include <KLocalizedString>
0013 #include <QStandardPaths>
0014 
0015 #include "dcbutton.h"
0016 #include "regexpconverter.h"
0017 
0018 RegExpButtons::RegExpButtons(QWidget *parent, const QString &name)
0019     : QToolBar(name, parent)
0020     , _keepMode(false)
0021 {
0022     _grp = new QButtonGroup(this);
0023     _grp->setExclusive(true);
0024 
0025     // The "select" button.
0026     _selectBut = new QToolButton(this);
0027 
0028     QPixmap pix = KIconLoader::global()->loadIcon(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kregexpeditor/pics/select.png")),
0029                                                   KIconLoader::Toolbar);
0030 
0031     _selectBut->setIcon(static_cast<QIcon>(pix));
0032     addWidget(_selectBut);
0033     _grp->addButton(_selectBut);
0034     _selectBut->setCheckable(true);
0035     connect(_selectBut, &QAbstractButton::clicked, this, &RegExpButtons::doSelect);
0036     connect(_selectBut, &QAbstractButton::clicked, this, &RegExpButtons::slotSetNonKeepMode);
0037 
0038     _selectBut->setToolTip(i18n("Selection tool"));
0039     _selectBut->setWhatsThis(
0040         i18n("<p>This will change the state of the editor to <i>selection state</i>.</p>"
0041              "<p>In this state you will not be inserting <i>regexp items</i>, but instead select them. "
0042              "To select a number of items, press down the left mouse button and drag it over the items.</p>"
0043              "<p>When you have selected a number of items, you may use cut/copy/paste. These functions are "
0044              "found in the right mouse button menu.</p>"));
0045 
0046     // Insert buttons.
0047     DoubleClickButton *but;
0048 
0049     but = insert(TEXT,
0050                  QStringLiteral("text"),
0051                  i18n("Text"),
0052                  i18n("<qt>This will insert a text field, where you may write text. The text you write will "
0053                       "be matched literally. (i.e. you do not need to escape any characters)</qt>"));
0054     addWidget(but);
0055 
0056     but = insert(CHARSET,
0057                  QStringLiteral("characters"),
0058                  i18n("A single character specified in a range"),
0059                  i18n("<p>This will match a single character from a predefined range.</p>"
0060                       "<p>When you insert this widget a dialog box will appear, which lets you specify "
0061                       "which characters this <i>regexp item</i> will match.</p>"));
0062     addWidget(but);
0063 
0064     but = insert(DOT, QStringLiteral("anychar"), i18n("Any character"), i18n("<qt>This will match any single character</qt>"));
0065     addWidget(but);
0066 
0067     but = insert(REPEAT,
0068                  QStringLiteral("repeat"),
0069                  i18n("Repeated content"),
0070                  i18n("<qt>This <i>regexp item</i> will repeat the <i>regexp items</i> it surrounds "
0071                       "a specified number of times.<br />"
0072                       "The number of times to repeat may be specified using ranges; e.g. it could be specified "
0073                       "that it should match from 2 to 4 times, that it should match exactly 5 times, or "
0074                       "that it should match at least one time.<br />"
0075                       "Examples:<br />"
0076                       "If you specify that it should match <i>any</i> time, and the content it surrounds "
0077                       "is <tt>abc</tt>, then this <i>regexp item</i> will match the empty string, "
0078                       "the string <tt>abc</tt>, the string <tt>abcabc</tt>, the string <tt>abcabcabcabc</tt>, "
0079                       "etc.</qt>"));
0080     addWidget(but);
0081 
0082     but = insert(ALTN,
0083                  QStringLiteral("altn"),
0084                  i18n("Alternatives"),
0085                  i18n("<p>This <i>regexp item</i> will match any of its alternatives.</p>"
0086                       "<p>Alternatives are specified by placing <i>regexp items</i> on top of "
0087                       "each other inside this widget.</p>"));
0088     addWidget(but);
0089 
0090     but = insert(COMPOUND,
0091                  QStringLiteral("compound"),
0092                  i18n("Compound regexp"),
0093                  i18n("<qt>This <i>regexp item</i> serves two purposes:"
0094                       "<ul><li>It makes it possible for you to collapse a huge <i>regexp item</i> into "
0095                       "a small box. This makes it easier for you to get an overview of large "
0096                       "<i>regexp items</i>. This is especially useful if you load a predefined <i>regexp item</i> "
0097                       "that you perhaps do not care about the inner workings of.</qt>"));
0098     addWidget(but);
0099 
0100     but = insert(BEGLINE, QStringLiteral("begline"), i18n("Beginning of line"), i18n("<qt>This will match the beginning of a line.</qt>"));
0101     addWidget(but);
0102 
0103     but = insert(ENDLINE, QStringLiteral("endline"), i18n("End of line"), i18n("<qt>This will match the end of a line.</qt>"));
0104     addWidget(but);
0105 
0106     _wordBoundary = insert(WORDBOUNDARY,
0107                            QStringLiteral("wordboundary"),
0108                            i18n("Word boundary"),
0109                            i18n("<qt>This asserts a word boundary (This part does not actually match any characters)</qt>"));
0110     addWidget(_wordBoundary);
0111 
0112     _nonWordBoundary = insert(NONWORDBOUNDARY,
0113                               QStringLiteral("nonwordboundary"),
0114                               i18n("Non Word boundary"),
0115                               i18n("<qt>This asserts a non-word boundary "
0116                                    "(This part does not actually match any characters)</qt>"));
0117     addWidget(_nonWordBoundary);
0118 
0119     _posLookAhead = insert(POSLOOKAHEAD,
0120                            QStringLiteral("poslookahead"),
0121                            i18n("Positive Look Ahead"),
0122                            i18n("<qt>This asserts a regular expression (This part does not actually match any characters). "
0123                                 "You can only use this at the end of a regular expression.</qt>"));
0124     addWidget(_posLookAhead);
0125 
0126     _negLookAhead = insert(NEGLOOKAHEAD,
0127                            QStringLiteral("neglookahead"),
0128                            i18n("Negative Look Ahead"),
0129                            i18n("<qt>This asserts a regular expression that must not match "
0130                                 "(This part does not actually match any characters). "
0131                                 "You can only use this at the end of a regular expression.</qt>"));
0132     addWidget(_negLookAhead);
0133 }
0134 
0135 DoubleClickButton *RegExpButtons::insert(RegExpType tp, const QString &name, const QString &tooltip, const QString &whatsthis)
0136 {
0137     QPixmap pix = KIconLoader::global()->loadIcon(
0138         QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kregexpeditor/pics/") + name + QStringLiteral(".png")),
0139         KIconLoader::Toolbar);
0140 
0141     DoubleClickButton *but = new DoubleClickButton(pix, this, QStringLiteral("RegExpButtons::but"));
0142 
0143     connect(but, &DoubleClickButton::clicked, [this, tp]() {
0144         Q_EMIT clicked(tp);
0145     });
0146     connect(but, &QAbstractButton::clicked, this, &RegExpButtons::slotSetNonKeepMode);
0147     connect(but, &DoubleClickButton::doubleClicked, this, &RegExpButtons::slotSetKeepMode);
0148 
0149     _grp->addButton(but);
0150     but->setCheckable(true);
0151     but->setToolTip(tooltip);
0152     but->setWhatsThis(whatsthis);
0153 
0154     return but;
0155 }
0156 
0157 void RegExpButtons::slotUnSelect()
0158 {
0159     if (_grp->checkedId() != -1) {
0160         QToolButton *pb = static_cast<QToolButton *>(_grp->checkedButton());
0161         if (pb) {
0162             pb->setChecked(false);
0163         }
0164     }
0165 }
0166 
0167 void RegExpButtons::slotSetKeepMode()
0168 {
0169     _keepMode = true;
0170 }
0171 
0172 void RegExpButtons::slotSetNonKeepMode()
0173 {
0174     _keepMode = false;
0175 }
0176 
0177 void RegExpButtons::slotSelectNewAction()
0178 {
0179     if (!_keepMode) {
0180         Q_EMIT doSelect();
0181         _selectBut->click();
0182     }
0183 }
0184 
0185 void RegExpButtons::setFeatures(int features)
0186 {
0187     _wordBoundary->setVisible(features & RegExpConverter::WordBoundary);
0188     _nonWordBoundary->setVisible(features & RegExpConverter::NonWordBoundary);
0189     _posLookAhead->setVisible(features & RegExpConverter::PosLookAhead);
0190     _negLookAhead->setVisible(features & RegExpConverter::NegLookAhead);
0191 }
0192 
0193 #include "moc_regexpbuttons.cpp"