Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.40 KB | None | 0 0
  1. #include "textfinder.h"
  2. #include "ui_textfinder.h"
  3. #include <QFile>
  4. #include <QTextStream>
  5. #include <string>
  6. #include <vector>
  7. #include <iterator>
  8. #include <iostream>
  9. #include <unistd.h>
  10. #include <QTimer>
  11. #include <QFile>
  12. #include <QDebug>
  13. #include <regex>
  14. using namespace std;
  15.  
  16. TextFinder::TextFinder(QWidget *parent) :
  17.     QWidget(parent),
  18.     ui(new Ui::TextFinder)
  19. {
  20.     ui->setupUi(this);
  21.     loadTextFile();
  22.  
  23.     ui->tabMatches->setFocus();
  24.     ui->lineEdit->setFocus();
  25.     ui->textEdit->viewport()->installEventFilter(this);
  26. }
  27.  
  28. TextFinder::~TextFinder()
  29. {
  30.     delete ui;
  31. }
  32.  
  33. void TextFinder::mouseDoubleClickEvent(QMouseEvent * e) {
  34.     qDebug() << e;
  35. }
  36.  
  37. void TextFinder::mousePressEvent(QMouseEvent * e) {
  38.     qDebug() << e;
  39. }
  40.  
  41. void TextFinder::loadTextFile()
  42. {
  43.     QFile inputFile(":/input.txt");
  44.     inputFile.open(QIODevice::ReadOnly);
  45.  
  46.     QTextStream in(&inputFile);
  47.     QString line = in.readAll();
  48.     inputFile.close();
  49.  
  50.     ui->textEdit->setPlainText(line);
  51.     QTextCursor cursor = ui->textEdit->textCursor();
  52.     cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
  53. }
  54.  
  55. void TextFinder::appendMatchList(QString captures)
  56. {
  57.     const char *qp = qPrintable(captures);
  58.     ui->matchBox->appendPlainText(qp);
  59. }
  60.  
  61.  
  62. void TextFinder::highlightMatches()
  63. {
  64.     QRegularExpression rx(ui->lineEdit->text(), QRegularExpression::CaseInsensitiveOption);
  65.     QString searchString = ui->lineEdit->text();
  66.  
  67.     QTextCursor tc(ui->textEdit->textCursor());
  68.     tc.select(QTextCursor::Document);
  69.     QTextCursor hc(ui->textEdit->textCursor());
  70.  
  71.     QList<QTextEdit::ExtraSelection> extraSelections;
  72.  
  73.     QRegularExpressionMatch match;
  74.     QRegularExpressionMatchIterator i = rx.globalMatch(ui->textEdit->toPlainText());
  75.  
  76.     const char* txt = qPrintable(ui->textEdit->toPlainText());
  77.     string inText(txt);
  78.     const char* ss = qPrintable(searchString);
  79.     string s(ss);
  80.  
  81.     try
  82.     {
  83.         regex e (s);
  84.         regex_iterator<string::iterator> rit (inText.begin(), inText.end(), e);
  85.         regex_iterator<string::iterator> rend;
  86.  
  87.         while (rit != rend) {
  88.             cout << rit->str() << endl;
  89.             ++rit;
  90.         }
  91.     }
  92.     catch(...) {
  93.     }
  94.  
  95.     if (!ui->textEdit->isReadOnly()) {
  96.         ui->matchBox->clear();
  97.         ui->textEdit->moveCursor(QTextCursor::Start);
  98.         QColor color_green = QColor(Qt::green).lighter(130);
  99.         QColor color_blue = QColor(Qt::blue).lighter(170);
  100.  
  101.         while (i.hasNext()) {
  102.             QRegularExpressionMatch match = i.next();
  103.  
  104.             // Begin highlighting text
  105.             tc.beginEditBlock();
  106.             if (rx.captureCount() == 0) {
  107.                 hc.setPosition(match.capturedStart(), QTextCursor::MoveAnchor);
  108.                 hc.setPosition(match.capturedEnd(), QTextCursor::KeepAnchor);
  109.  
  110.                 QTextEdit::ExtraSelection extra;
  111.                 extra.format.setBackground(color_green);
  112.                 extra.cursor = hc;
  113.                 extraSelections.append(extra);
  114.             } else if (rx.captureCount() == 1) {
  115.                 hc.setPosition(match.capturedStart(), QTextCursor::MoveAnchor);
  116.                 hc.setPosition(match.capturedEnd(), QTextCursor::KeepAnchor);
  117.  
  118.                 QTextEdit::ExtraSelection extra;
  119.                 extra.format.setBackground(color_blue);
  120.                 extra.cursor = hc;
  121.                 extraSelections.append(extra);
  122.             }
  123.             // End highlight
  124.             tc.endEditBlock();
  125.         }
  126.         appendMatchList(match.captured());
  127.         ui->textEdit->setExtraSelections(extraSelections);
  128.     }
  129. }
  130.  
  131. void TextFinder::on_findButton_clicked()
  132. {
  133.     //highlightMatches();
  134.  
  135.     //QString searchString = ui->lineEdit->text();
  136.     //ui->textEdit->find(searchString, QTextDocument::FindWholeWords);
  137. }
  138.  
  139. void TextFinder::on_lineEdit_returnPressed()
  140. {
  141.     on_findButton_clicked();
  142. }
  143.  
  144. void TextFinder::on_lineEdit_textChanged(const QString &arg1)
  145. {
  146.     if (ui->lineEdit->text().size()<2) {
  147.         QTimer::singleShot(750, this, SLOT(highlightMatches()));
  148.     } else {
  149.         QTimer::singleShot(300, this, SLOT(highlightMatches()));
  150.     }
  151. }
  152.  
  153. /*
  154. void TextFinder::mouseDoubleClickEvent(QMouseEvent * e) {
  155.     bool m_leftButton = false;
  156.     if (e->button() == Qt::LeftButton) {
  157.         m_leftButton = true;
  158.         qDebug() << "true";
  159.     } else {
  160.         qDebug() << "false";
  161.     }
  162. }
  163. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement