1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| #include "textfinder.h"
#include "ui_textfinder.h"
#include <QFile>
#include <QTextStream>
#include <QFileDialog>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QTextEdit>
#include <QTextCharFormat>
#include <QTextCursor>
#include <QMessageBox>
#include <QKeyEvent>
textfinder::textfinder(QWidget *parent) : QWidget(parent), ui(new Ui::textfinder)
{
ui->setupUi(this);
loadtextfile();
setAcceptDrops(true);
ui->textEdit->setAcceptDrops(false);
ui->textEdit->setReadOnly(true);
}
textfinder::~textfinder()
{
delete ui;
}
void textfinder::on_findButton_clicked()
{
//QLabel compteur = ui->compteur->int();
QString searchString = ui->lineEdit->text();
QTextDocument *document = ui->textEdit->document();
bool found = false;
if (isFirstTime == false)
document->undo();
if (searchString.isEmpty())
QMessageBox::information(this, tr("Recherche Vide"), "Veuillez taper un mot à rechercher.");
else
{
QTextCursor highlightCursor(document);
QTextCursor cursor(document);
cursor.beginEditBlock();
QTextCharFormat plainFormat(highlightCursor.charFormat());
QTextCharFormat underlineFormat = plainFormat;
underlineFormat.setBackground(Qt::yellow);
while (!highlightCursor.isNull() && !highlightCursor.atEnd())
{
highlightCursor = document->find(searchString, highlightCursor, QTextDocument::FindWholeWords);
if (!highlightCursor.isNull())
{
found = true;
highlightCursor.movePosition(QTextCursor::WordRight, QTextCursor::KeepAnchor);
highlightCursor.mergeCharFormat(underlineFormat);
}
}
cursor.endEditBlock();
isFirstTime = false;
if (found == false) {
QMessageBox::information(this, tr("Mot non trouve"), "Mot introuvable.");
}
}
ui->textEdit->find(searchString, QTextDocument::FindWholeWords);
}
void textfinder::loadtextfile()
{
QFile inputFile(":/input.txt");
//inputFile.open(QIODevice::ReadOnly);
QTextStream in(&inputFile);
in.setCodec("UTF-8");
QString line = in.readAll();
inputFile.close();
ui->textEdit->setPlainText(line);
QTextCursor cursor = ui->textEdit->textCursor();
cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
QString cheminImage = "C:/Users/etudiant/Documents/textfinder/img.png";
QString texteFinal = ui->textEdit->toHtml() + "<img src = \""+ cheminImage +"\" alt = \"\"/>";
ui->textEdit->setHtml(texteFinal);
}
void textfinder::loadtextfile(QString fileName)
{
QFile inputFile(fileName);
inputFile.open(QIODevice::ReadOnly);
QTextStream in(&inputFile);
in.setCodec("UTF-8");
QString line = in.readAll();
inputFile.close();
ui->textEdit->setPlainText(line);
QTextCursor cursor = ui->textEdit->textCursor();
cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
}
void textfinder::on_pushButton_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "/home", tr("Text files (*.txt)"));
loadtextfile(fileName);
}
void textfinder::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}
void textfinder::dropEvent(QDropEvent *event)
{
QList<QUrl> urls = event->mimeData()->urls();
QString fileName = urls.first().toLocalFile();
event->accept();
loadtextfile(fileName);
} |
Partager