summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArno <arno@disconnect.de>2018-12-09 05:09:09 +0100
committerArno <arno@disconnect.de>2018-12-09 05:09:09 +0100
commitde795b8ab7d061b6ef25233ef5faf699643bb81b (patch)
treeaa59cf49e1aa4c3a1fe54caf512df22127d25c36
parent18f15fc3be0c7c2847153672fbe86a53a8247b0c (diff)
downloadShemovCleaner-de795b8ab7d061b6ef25233ef5faf699643bb81b.tar.gz
ShemovCleaner-de795b8ab7d061b6ef25233ef5faf699643bb81b.tar.bz2
ShemovCleaner-de795b8ab7d061b6ef25233ef5faf699643bb81b.zip
Improve subject guessing
Use QRegularExpression instead of QRegExp, since the former is recommended. Escape the filename, so we don't have to worry about special chars and then match line by line. If there is a number at the end of the found subject, remove it. The former logic with QRegExp, indexOf and no escape wouldn't match files with whitespaces and special chars...
-rw-r--r--filewidget.cpp31
1 files changed, 18 insertions, 13 deletions
diff --git a/filewidget.cpp b/filewidget.cpp
index 31dfa92..6294487 100644
--- a/filewidget.cpp
+++ b/filewidget.cpp
@@ -395,22 +395,27 @@ void FileWidget::guessSubject(){
QModelIndexList sel = mFileView->selectionModel()->selectedRows(NameColumn);
if(!sel.isEmpty()){
QFileInfo fi(sel.first().data().toString());
- QString fRe = QString(".*%1.*").arg(fi.completeBaseName());
- const QRegExp fnRe(fRe);
- int index = mDescript.indexOf(fnRe);
- if(index > -1){
- QString subject(mDescript.at(index));
- if(subject.startsWith('"')){
- int nextQuot = subject.indexOf('"', 1);
- if(nextQuot > -1){
- subject = subject.remove(0, nextQuot + 1).trimmed();
+ QString fRe = QRegularExpression::escape(fi.completeBaseName());
+ const QRegularExpression fnRe(fRe);
+ QRegularExpressionMatch m;
+ for(QString l : mDescript){
+ m = fnRe.match(l);
+ if(m.hasMatch()){
+ QString subject;
+ if(l.startsWith('"')){
+ int nextQuot = l.indexOf('"', 1);
+ if(nextQuot > -1){
+ subject = l.remove(0, nextQuot + 1).trimmed();
+ }
}
+ subject.replace(QRegularExpression("\\d+$"), "");
+ subject = subject.trimmed();
+ qApp->clipboard()->setText(subject);
+ emit statusMessage(subject);
+ return;
}
- qApp->clipboard()->setText(subject);
- emit statusMessage(subject);
- }else{
- emit statusMessage(tr("No subject found!"));
}
+ emit statusMessage(tr("No subject found!"));
}
}