Commit a54fc1f6 authored by con's avatar con
Browse files

Work around stupid bug in QtConcurrent.

You have to call setProgressValue(AndText) at least once
unconditionally, before resultReporting and isProgressUpdateNeeded
actually do the right thing.

Also use qreal progress values internally while iterating directories.
parent 9c47c422
......@@ -74,9 +74,10 @@ void runFileSearch(QFutureInterface<FileSearchResultList> &future,
QTextDocument::FindFlags flags,
QMap<QString, QString> fileToContentsMap)
{
future.setProgressRange(0, files->maxProgress());
int numFilesSearched = 0;
int numMatches = 0;
future.setProgressRange(0, files->maxProgress());
future.setProgressValueAndText(files->currentProgress(), msgFound(searchTerm, numMatches, numFilesSearched));
bool caseInsensitive = !(flags & QTextDocument::FindCaseSensitively);
bool wholeWord = (flags & QTextDocument::FindWholeWords);
......@@ -95,10 +96,9 @@ void runFileSearch(QFutureInterface<FileSearchResultList> &future,
QFile file;
QBuffer buffer;
FileSearchResultList results;
while (files->hasNext()) {
FileSearchResultList results;
const QString &s = files->next();
future.setProgressRange(0, files->maxProgress());
if (future.isPaused())
future.waitForResume();
if (future.isCanceled()) {
......@@ -182,13 +182,21 @@ void runFileSearch(QFutureInterface<FileSearchResultList> &future,
}
firstChunk = false;
}
if (!results.isEmpty())
future.reportResult(results);
++numFilesSearched;
if (future.isProgressUpdateNeeded())
if (future.isProgressUpdateNeeded()) {
if (!results.isEmpty()) {
future.reportResult(results);
results.clear();
}
future.setProgressRange(0, files->maxProgress());
future.setProgressValueAndText(files->currentProgress(), msgFound(searchTerm, numMatches, numFilesSearched));
}
device->close();
}
if (!results.isEmpty()) {
future.reportResult(results);
results.clear();
}
if (!future.isCanceled())
future.setProgressValueAndText(files->currentProgress(), msgFound(searchTerm, numMatches, numFilesSearched));
delete files;
......@@ -200,9 +208,10 @@ void runFileSearchRegExp(QFutureInterface<FileSearchResultList> &future,
QTextDocument::FindFlags flags,
QMap<QString, QString> fileToContentsMap)
{
future.setProgressRange(0, files->maxProgress());
int numFilesSearched = 0;
int numMatches = 0;
future.setProgressRange(0, files->maxProgress());
future.setProgressValueAndText(files->currentProgress(), msgFound(searchTerm, numMatches, numFilesSearched));
if (flags & QTextDocument::FindWholeWords)
searchTerm = QString::fromLatin1("\\b%1\\b").arg(searchTerm);
const Qt::CaseSensitivity caseSensitivity = (flags & QTextDocument::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive;
......@@ -211,10 +220,9 @@ void runFileSearchRegExp(QFutureInterface<FileSearchResultList> &future,
QFile file;
QString str;
QTextStream stream;
FileSearchResultList results;
while (files->hasNext()) {
FileSearchResultList results;
const QString &s = files->next();
future.setProgressRange(0, files->maxProgress());
if (future.isPaused())
future.waitForResume();
if (future.isCanceled()) {
......@@ -247,13 +255,22 @@ void runFileSearchRegExp(QFutureInterface<FileSearchResultList> &future,
}
++lineNr;
}
future.reportResult(results);
++numFilesSearched;
if (future.isProgressUpdateNeeded())
if (future.isProgressUpdateNeeded()) {
if (!results.isEmpty()) {
future.reportResult(results);
results.clear();
}
future.setProgressRange(0, files->maxProgress());
future.setProgressValueAndText(files->currentProgress(), msgFound(searchTerm, numMatches, numFilesSearched));
}
if (needsToCloseFile)
file.close();
}
if (!results.isEmpty()) {
future.reportResult(results);
results.clear();
}
if (!future.isCanceled())
future.setProgressValueAndText(files->currentProgress(), msgFound(searchTerm, numMatches, numFilesSearched));
delete files;
......@@ -357,13 +374,13 @@ int FileIterator::currentProgress() const
// #pragma mark -- SubDirFileIterator
namespace {
const int MAX_PROGRESS = 360;
const int MAX_PROGRESS = 1000;
}
SubDirFileIterator::SubDirFileIterator(const QStringList &directories, const QStringList &filters)
: m_filters(filters), m_progress(0)
{
int maxPer = MAX_PROGRESS/directories.count();
qreal maxPer = MAX_PROGRESS/directories.count();
foreach (const QString &directoryEntry, directories) {
if (!directoryEntry.isEmpty()) {
m_dirs.push(QDir(directoryEntry));
......@@ -379,7 +396,7 @@ bool SubDirFileIterator::hasNext() const
return true;
while(!m_dirs.isEmpty() && m_currentFiles.isEmpty()) {
QDir dir = m_dirs.pop();
int dirProgressMax = m_progressValues.pop();
qreal dirProgressMax = m_progressValues.pop();
bool processed = m_processedValues.pop();
if (dir.exists()) {
QStringList subDirs;
......@@ -397,10 +414,9 @@ bool SubDirFileIterator::hasNext() const
}
m_progress += dirProgressMax;
} else {
int subProgress = dirProgressMax/(subDirs.size()+1);
int selfProgress = subProgress + dirProgressMax%(subDirs.size()+1);
qreal subProgress = dirProgressMax/(subDirs.size()+1);
m_dirs.push(dir);
m_progressValues.push(selfProgress);
m_progressValues.push(subProgress);
m_processedValues.push(true);
QStringListIterator it(subDirs);
it.toBack();
......@@ -436,5 +452,5 @@ int SubDirFileIterator::maxProgress() const
int SubDirFileIterator::currentProgress() const
{
return m_progress;
return qMin(qRound(m_progress), MAX_PROGRESS);
}
......@@ -72,9 +72,9 @@ public:
private:
QStringList m_filters;
mutable QStack<QDir> m_dirs;
mutable QStack<int> m_progressValues;
mutable QStack<qreal> m_progressValues;
mutable QStack<bool> m_processedValues;
mutable int m_progress;
mutable qreal m_progress;
mutable QStringList m_currentFiles;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment