File indexing completed on 2024-05-05 05:54:19
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 "kregexpeditorwindow.h" 0008 0009 #include <QActionGroup> 0010 #include <QApplication> 0011 #include <QDebug> 0012 #include <QDialogButtonBox> 0013 #include <QFileDialog> 0014 #include <QFrame> 0015 #include <QMenu> 0016 #include <QMenuBar> 0017 #include <QPushButton> 0018 #include <QVBoxLayout> 0019 0020 #include <KActionCollection> 0021 #include <KHelpClient> 0022 #include <KLocalizedString> 0023 #include <KSelectAction> 0024 0025 #include "emacsregexpconverter.h" 0026 #include "kregexpeditorprivate.h" 0027 #include "qtregexpconverter.h" 0028 0029 using namespace Qt::Literals::StringLiterals; 0030 0031 const QString KRegExpEditorWindow::version = u"1.0"_s; 0032 0033 KRegExpEditorWindow::KRegExpEditorWindow(QWidget *parent) 0034 : KXmlGuiWindow(parent) 0035 { 0036 setWindowTitle(i18nc("@title:window", "Regular Expression Editor")); 0037 0038 _editor = new KRegExpEditorPrivate(this); 0039 setCentralWidget(_editor); 0040 0041 setupActions(); 0042 0043 // connect(_editor, &KRegExpEditorGUI::canUndo, this, &KRegExpEditorWindow::canUndo); 0044 // connect(_editor, &KRegExpEditorGUI::canRedo, this, &KRegExpEditorWindow::canRedo); 0045 // connect(_editor, &KRegExpEditorGUI::changes, this, &KRegExpEditorWindow::changes); 0046 0047 // QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help); 0048 // QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok); 0049 // okButton->setDefault(true); 0050 // okButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0051 // layout->addWidget(buttonBox); 0052 // connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); 0053 // connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0054 // connect(buttonBox, &QDialogButtonBox::helpRequested, this, &KRegExpEditorWindow::showHelp); 0055 resize(640, 400); 0056 } 0057 0058 KRegExpEditorWindow::~KRegExpEditorWindow() 0059 { 0060 } 0061 0062 void KRegExpEditorWindow::setupActions() 0063 { 0064 // File 0065 KStandardAction::save(_editor, &KRegExpEditorPrivate::slotUndo, actionCollection()); 0066 0067 // Edit 0068 auto action = KStandardAction::undo(_editor, &KRegExpEditorPrivate::slotUndo, actionCollection()); 0069 action->setEnabled(false); 0070 connect(_editor, &KRegExpEditorPrivate::canUndo, this, [action](bool canUndo) { 0071 action->setEnabled(canUndo); 0072 }); 0073 action = KStandardAction::redo(_editor, &KRegExpEditorPrivate::slotRedo, actionCollection()); 0074 action->setEnabled(false); 0075 connect(_editor, &KRegExpEditorPrivate::canRedo, this, [action](bool canRedo) { 0076 action->setEnabled(canRedo); 0077 }); 0078 action = KStandardAction::save(_editor, &KRegExpEditorPrivate::slotSave, actionCollection()); 0079 action->setEnabled(false); 0080 connect(_editor, &KRegExpEditorPrivate::canSave, this, [action](bool canSave) { 0081 action->setEnabled(canSave); 0082 }); 0083 action = KStandardAction::copy(_editor, &KRegExpEditorPrivate::slotCopy, actionCollection()); 0084 action->setEnabled(false); 0085 connect(_editor, &KRegExpEditorPrivate::anythingSelected, this, [action](bool anythingSelected) { 0086 action->setEnabled(anythingSelected); 0087 }); 0088 action = KStandardAction::cut(_editor, &KRegExpEditorPrivate::slotCut, actionCollection()); 0089 action->setEnabled(false); 0090 connect(_editor, &KRegExpEditorPrivate::anythingSelected, this, [action](bool anythingSelected) { 0091 action->setEnabled(anythingSelected); 0092 }); 0093 action = KStandardAction::paste(_editor, &KRegExpEditorPrivate::slotPaste, actionCollection()); 0094 action->setEnabled(false); 0095 0096 // Verify 0097 auto verifyAction = new QAction(QIcon::fromTheme(QStringLiteral("tools-check-spelling")), i18n("Verify regular expression"), this); 0098 actionCollection()->addAction(u"kregexpeditor_verify"_s, verifyAction); 0099 connect(verifyAction, &QAction::triggered, this, [this](bool) { 0100 _editor->doVerify(); 0101 }); 0102 0103 auto loadAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open")), i18nc("@action:inmenu", "Load text"), this); 0104 loadAction->setToolTip(i18n("Load text in the verifier window")); 0105 actionCollection()->addAction(u"kregexpeditor_load"_s, loadAction); 0106 connect(loadAction, &QAction::triggered, this, [this](bool) { 0107 const QString fileName = QFileDialog::getOpenFileName(this, QString(), QString(), QString()); 0108 if (!fileName.isNull()) { 0109 Q_EMIT _editor->setVerifyText(fileName); 0110 } 0111 }); 0112 0113 // Auto Verify 0114 QAction *autoVerify = new QAction(i18n("Verify on the Fly"), this); 0115 autoVerify->setCheckable(true); 0116 autoVerify->setChecked(true); 0117 // connect(autoVerify, &QAction::toggled, this, &VerifyButtons::updateVerifyButton); 0118 connect(autoVerify, &QAction::toggled, this, [this](bool checked) { 0119 _editor->setAutoVerify(checked); 0120 }); 0121 actionCollection()->addAction(u"kregexpeditor_config_autoverify"_s, autoVerify); 0122 autoVerify->setToolTip(i18n("Toggle on-the-fly verification of regular expression")); 0123 autoVerify->setWhatsThis( 0124 i18n("Enabling this option will make the verifier update for each edit. " 0125 "If the verify window contains much text, or if the regular expression is either " 0126 "complex or matches a lot of time, this may be very slow.")); 0127 0128 // Match greedy 0129 QAction *matchGreedy = new QAction(i18n("Match Greedy"), this); 0130 matchGreedy->setCheckable(true); 0131 matchGreedy->setChecked(false); 0132 connect(matchGreedy, &QAction::toggled, this, [this](bool checked) { 0133 _editor->setMatchGreedy(checked); 0134 }); 0135 actionCollection()->addAction(u"kregexpeditor_config_matchgreedy"_s, matchGreedy); 0136 matchGreedy->setToolTip(i18n("Toggle greedy matching when verifying the regular expression.")); 0137 matchGreedy->setWhatsThis(i18n("When this option is enabled, the regular expression will be evaluated on a so-called greedy way.")); 0138 0139 // Converters 0140 auto languageMenu = new KSelectAction(i18nc("@title:menu", "RegExp Languages"), this); 0141 0142 // Qt 0143 RegExpConverter *qtConverter = new QtRegExpConverter(); 0144 m_converters.append(std::pair(qtConverter, static_cast<QAction *>(nullptr))); 0145 QString qtConverterName = qtConverter->name(); 0146 _editor->setSyntax(qtConverter); 0147 0148 // Emacs 0149 auto converter = new EmacsRegExpConverter(); 0150 m_converters.append(std::pair(converter, static_cast<QAction *>(nullptr))); 0151 0152 auto converterGroup = new QActionGroup(this); 0153 for (auto it = m_converters.begin(); it != m_converters.end(); ++it) { 0154 QString name = it->first->name(); 0155 auto action = new QAction(name, this); 0156 action->setCheckable(true); 0157 converterGroup->addAction(action); 0158 languageMenu->addAction(action); 0159 it->second = action; 0160 0161 if (it->first == qtConverter) { 0162 action->setEnabled(true); 0163 } 0164 } 0165 0166 connect(converterGroup, &QActionGroup::triggered, this, [this](QAction *action) { 0167 for (auto it = m_converters.begin(); it != m_converters.end(); ++it) { 0168 if (it->second == action) { 0169 _editor->setSyntax(it->first); 0170 } 0171 } 0172 }); 0173 0174 actionCollection()->addAction(u"kregexpeditor_config_languages"_s, languageMenu); 0175 0176 setupGUI(ToolBar | Keys | Save | Create, u"kregexpeditorui.rc"_s); 0177 } 0178 0179 void KRegExpEditorWindow::setMatchText(const QString &txt) 0180 { 0181 _editor->setMatchText(txt); 0182 } 0183 0184 void KRegExpEditorWindow::showHelp() 0185 { 0186 KHelpClient::invokeHelp(QString(), QStringLiteral("kregexpeditor")); 0187 }