File indexing completed on 2024-04-28 05:49:11

0001 /*
0002     SPDX-FileCopyrightText: 2021 Markus Ebner <info@ebner-markus.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "MatchExportDialog.h"
0008 #include "SearchPlugin.h"
0009 
0010 #include <QAction>
0011 #include <QMenu>
0012 #include <QRegularExpression>
0013 
0014 MatchExportDialog::MatchExportDialog(QWidget *parent, QAbstractItemModel *matchModel, QRegularExpression *regExp)
0015     : QDialog(parent)
0016     , m_matchModel(matchModel)
0017     , m_regExp(regExp)
0018 {
0019     setupUi(this);
0020     setWindowTitle(i18n("Export Search Result Matches"));
0021 
0022     QAction *exportPatternTextActionForInsertRegexButton =
0023         exportPatternText->addAction(QIcon::fromTheme(QStringLiteral("code-context")), QLineEdit::TrailingPosition);
0024 
0025     connect(exportPatternTextActionForInsertRegexButton, &QAction::triggered, this, [this]() {
0026         QPoint pos = exportPatternText->pos();
0027         pos.rx() += exportPatternText->width() - ((16 + 4) * devicePixelRatioF());
0028         pos.ry() += exportPatternText->height();
0029         QMenu menu(this);
0030         QSet<QAction *> actionList;
0031         KatePluginSearchView::addRegexHelperActionsForReplace(&actionList, &menu);
0032         auto action = menu.exec(mapToGlobal(pos));
0033         KatePluginSearchView::regexHelperActOnAction(action, actionList, exportPatternText);
0034     });
0035 
0036     connect(pushButton, &QPushButton::clicked, this, &MatchExportDialog::generateMatchExport);
0037 }
0038 
0039 MatchExportDialog::~MatchExportDialog()
0040 {
0041 }
0042 
0043 void MatchExportDialog::generateMatchExport()
0044 {
0045     QString exportPattern = this->exportPatternText->text();
0046     QString exportResult;
0047     QModelIndex rootIndex = m_matchModel->index(0, 0);
0048 
0049     int fileCount = m_matchModel->rowCount(rootIndex);
0050     for (int i = 0; i < fileCount; ++i) {
0051         QModelIndex fileIndex = m_matchModel->index(i, 0, rootIndex);
0052         int matchCount = m_matchModel->rowCount(fileIndex);
0053         for (int j = 0; j < matchCount; ++j) {
0054             QModelIndex matchIndex = m_matchModel->index(j, 0, fileIndex);
0055             QRegularExpressionMatch match = m_regExp->match(matchIndex.data(MatchModel::MatchRole).toString());
0056             exportResult += MatchModel::generateReplaceString(match, exportPattern) + QLatin1String("\n");
0057         }
0058     }
0059     this->exportResultText->setPlainText(exportResult);
0060 }
0061 
0062 #include "moc_MatchExportDialog.cpp"