File indexing completed on 2024-03-24 05:54:40
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 "verifybuttons.h" 0008 0009 #include <KLocalizedString> 0010 #include <QIcon> 0011 0012 #include <QActionGroup> 0013 #include <QFileDialog> 0014 #include <QMenu> 0015 #include <QToolButton> 0016 0017 #include "emacsregexpconverter.h" 0018 #include "qtregexpconverter.h" 0019 0020 VerifyButtons::VerifyButtons(QWidget *parent, const QString &name) 0021 : QToolBar(name, parent) 0022 , _configMenu(nullptr) 0023 { 0024 _verify = new QToolButton(this); 0025 QIcon icon = QIcon::fromTheme(QStringLiteral("tools-check-spelling")); 0026 _verify->setIcon(icon); 0027 _verify->setToolTip(i18n("Verify regular expression")); 0028 _verify->setWhatsThis( 0029 i18n("Shows what part of the regular expression is being matched in the <i>verifier window</i>." 0030 "(The window below the graphical editor window).")); 0031 addWidget(_verify); 0032 connect(_verify, &QAbstractButton::clicked, this, &VerifyButtons::verify); 0033 0034 QToolButton *button = new QToolButton(this); 0035 button->setIcon(QIcon::fromTheme(QStringLiteral("document-open"))); 0036 addWidget(button); 0037 connect(button, &QAbstractButton::clicked, this, &VerifyButtons::loadText); 0038 button->setToolTip(i18n("Load text in the verifier window")); 0039 0040 button = new QToolButton(this); 0041 button->setIcon(QIcon::fromTheme(QStringLiteral("configure"))); 0042 addWidget(button); 0043 button->setToolTip(i18n("Verification Settings")); 0044 button->setPopupMode(QToolButton::InstantPopup); 0045 0046 // It is currently not possible to ask for the paragraph being highlighted, thefore scrolling to next/prev match 0047 // do not work. Enable this when they work. 0048 // _first = new QToolButton( QString::fromLatin1("<<"), this); 0049 // layout->addWidget( _first ); 0050 // connect(_first, SIGNAL(clicked()), this, SIGNAL(gotoFirst())); 0051 // _first->setFixedWidth( 25 ); 0052 // 0053 // _prev = new QToolButton(QString::fromLatin1("<"), this); 0054 // layout->addWidget( _prev ); 0055 // connect(_prev, SIGNAL(clicked()), this, SIGNAL(gotoPrev())); 0056 // _prev->setFixedWidth( 20 ); 0057 // 0058 // _next = new QToolButton(QString::fromLatin1(">"), this); 0059 // layout->addWidget( _next ); 0060 // connect(_next, SIGNAL(clicked()), this, SIGNAL(gotoNext())); 0061 // _next->setFixedWidth( 20 ); 0062 // 0063 // _last = new QToolButton(QString::fromLatin1(">>"), this); 0064 // layout->addWidget( _last ); 0065 // connect(_last, SIGNAL(clicked()), this, SIGNAL(gotoLast())); 0066 // _last->setFixedWidth( 25 ); 0067 0068 // Same as above 0069 // QLabel* label = new QLabel( i18n("Matches: "), this ); 0070 // layout->addWidget( label ); 0071 // _matches = new QLabel(i18n("-"), this ); 0072 // layout->addWidget( _matches ); 0073 // QString txt = i18n( "Shows number of times regular expression matches the text in the verifier window"); 0074 // label->setToolTip( txt ); 0075 // _matches->setToolTip( txt ); 0076 0077 _verify->setEnabled(false); 0078 0079 // -------------------------------------------------- RegExp Converters 0080 0081 // Qt 0082 RegExpConverter *converter = new QtRegExpConverter(); 0083 _converters.append(qMakePair(converter, static_cast<QAction *>(nullptr))); 0084 QString qtConverterName = converter->name(); 0085 0086 // Emacs 0087 converter = new EmacsRegExpConverter(); 0088 _converters.append(qMakePair(converter, static_cast<QAction *>(nullptr))); 0089 0090 // -------------------------------------------------- Initialize the config menu 0091 _configMenu = new QMenu(i18n("config menu"), this); 0092 0093 // Auto Verify 0094 QAction *autoVerify = new QAction(i18n("Verify on the Fly"), this); 0095 autoVerify->setCheckable(true); 0096 autoVerify->setChecked(true); 0097 connect(autoVerify, &QAction::toggled, this, &VerifyButtons::updateVerifyButton); 0098 connect(autoVerify, &QAction::toggled, this, &VerifyButtons::autoVerify); 0099 _configMenu->addAction(autoVerify); 0100 autoVerify->setToolTip(i18n("Toggle on-the-fly verification of regular expression")); 0101 autoVerify->setWhatsThis( 0102 i18n("Enabling this option will make the verifier update for each edit. " 0103 "If the verify window contains much text, or if the regular expression is either " 0104 "complex or matches a lot of time, this may be very slow.")); 0105 0106 QAction *matchGreedy = new QAction(i18n("Match Greedy"), this); 0107 matchGreedy->setCheckable(true); 0108 matchGreedy->setChecked(false); 0109 connect(matchGreedy, &QAction::toggled, this, &VerifyButtons::matchGreedy); 0110 _configMenu->addAction(matchGreedy); 0111 matchGreedy->setToolTip(i18n("Toggle greedy matching when verifying the regular expression.")); 0112 matchGreedy->setWhatsThis(i18n("When this option is enabled, the regular expression will be evaluated on a so-called greedy way.")); 0113 0114 // RegExp Languages 0115 _languages = new QMenu(i18n("RegExp Language"), _configMenu); 0116 _configMenu->addMenu(_languages); 0117 0118 QActionGroup *grp = new QActionGroup(this); 0119 for (QList<QPair<RegExpConverter *, QAction *>>::Iterator it = _converters.begin(); it != _converters.end(); ++it) { 0120 QString name = (*it).first->name(); 0121 QAction *action = new QAction(name, this); 0122 action->setCheckable(true); 0123 grp->addAction(action); 0124 (*it).second = action; 0125 } 0126 0127 _languages->addActions(grp->actions()); 0128 connect(grp, &QActionGroup::triggered, this, &VerifyButtons::slotChangeSyntax); 0129 _languages->setEnabled(false); 0130 0131 button->setMenu(_configMenu); 0132 0133 // Select the Qt converter by default 0134 setSyntax(qtConverterName); 0135 } 0136 0137 void VerifyButtons::updateVerifyButton(bool b) 0138 { 0139 _verify->setEnabled(!b); 0140 } 0141 0142 void VerifyButtons::loadText() 0143 { 0144 const QString fileName = QFileDialog::getOpenFileName(this, QString(), QString(), QString()); 0145 if (!fileName.isNull()) { 0146 Q_EMIT loadVerifyText(fileName); 0147 } 0148 } 0149 0150 // Qt anchors do not work for <pre>...</pre>, thefore scrolling to next/prev match 0151 // do not work. Enable this when they work. 0152 // void VerifyButtons::enableBackwardButtons( bool b ) 0153 // { 0154 // _first->setEnabled( b ); 0155 // _prev->setEnabled( b ); 0156 // } 0157 // 0158 // void VerifyButtons::enableForwardButtons( bool b ) 0159 // { 0160 // _next->setEnabled( b ); 0161 // _last->setEnabled( b ); 0162 // } 0163 0164 void VerifyButtons::setMatchCount(int /*count*/) 0165 { 0166 // currently this is not possible due to limitation in QSyntaxHighlighter 0167 /* 0168 if ( count == -1 ) { 0169 _matches->setText( QString::fromLatin1("-") ); 0170 } else { 0171 _matches->setText( QString::number( count ) ); 0172 } 0173 */ 0174 } 0175 0176 void VerifyButtons::slotChangeSyntax(QAction *action) 0177 { 0178 Q_EMIT changeSyntax(action->text()); 0179 } 0180 0181 RegExpConverter *VerifyButtons::setSyntax(const QString &which) 0182 { 0183 QString noAmpersand = which; 0184 noAmpersand.remove(QLatin1Char('&')); // HACK, can probably be done more cleanly 0185 for (auto it = _converters.begin(); it != _converters.end(); ++it) { 0186 QString name = (*it).first->name(); 0187 if (name == noAmpersand) { 0188 it->second->setChecked(true); 0189 return (*it).first; 0190 } 0191 } 0192 qWarning("No such converter: '%s'", qPrintable(noAmpersand)); 0193 return nullptr; 0194 } 0195 0196 void VerifyButtons::setAllowNonQtSyntax(bool b) 0197 { 0198 _languages->setEnabled(b); 0199 } 0200 0201 #include "moc_verifybuttons.cpp"