Skip to content

Commit

Permalink
More fine-grained progress report both in the general progress bar an…
Browse files Browse the repository at this point in the history
…d the individual hashes.

- Progress report on each hash computation in the assigned table cell.
- Global progress report using the windows task bar button.
- More fine-grained progress report in both hash checking and computing.
  • Loading branch information
FelixdelasPozas committed Apr 15, 2019
1 parent 98042fa commit ffecc7f
Show file tree
Hide file tree
Showing 23 changed files with 671 additions and 292 deletions.
7 changes: 6 additions & 1 deletion AboutDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
// Project
#include <AboutDialog.h>

const QString VERSION = QString("version 1.0.1");
// Qt
#include <QtGlobal>

const QString VERSION = QString("version 1.1.0");

//-----------------------------------------------------------------
AboutDialog::AboutDialog(QWidget *parent, Qt::WindowFlags flags)
Expand All @@ -35,4 +38,6 @@ AboutDialog::AboutDialog(QWidget *parent, Qt::WindowFlags flags)

m_compilationDate->setText(tr("Compiled on ") + compilation_date + compilation_time);
m_version->setText(VERSION);

m_qtVersion->setText(tr("version %1.%2.%3").arg(QT_VERSION_MAJOR).arg(QT_VERSION_MINOR).arg(QT_VERSION_PATCH));
}
2 changes: 1 addition & 1 deletion AboutDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ Qt Framework</string>
</widget>
</item>
<item>
<widget class="QLabel" name="label_27">
<widget class="QLabel" name="m_qtVersion">
<property name="text">
<string>version 5.9</string>
</property>
Expand Down
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ cmake_minimum_required (VERSION 2.8.6)

# Version Number
set (SIMPLE_HASHER_VERSION_MAJOR 1)
set (SIMPLE_HASHER_VERSION_MINOR 0)
set (SIMPLE_HASHER_VERSION_PATCH 1)
set (SIMPLE_HASHER_VERSION_MINOR 1)
set (SIMPLE_HASHER_VERSION_PATCH 0)

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
Expand All @@ -14,7 +14,7 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)

# Find the QtWidgets library
find_package(Qt5 COMPONENTS Widgets Multimedia)
find_package(Qt5 COMPONENTS Widgets Multimedia WinExtras)

# We need add -DQT_WIDGETS_LIB when using QtWidgets in Qt 5.
#add_definitions(${Qt5Widgets_DEFINITIONS})
Expand Down Expand Up @@ -79,6 +79,7 @@ set (CORE_SOURCES

set(CORE_EXTERNAL_LIBS
Qt5::Widgets
Qt5::WinExtras
)

add_executable(SimpleHasher ${CORE_SOURCES})
Expand Down
100 changes: 76 additions & 24 deletions ComputerThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//----------------------------------------------------------------
ComputerThread::ComputerThread(QMap<QString, HashList> computations, const int threadsNum, QObject *parent)
: QThread {parent}
, m_computations{computations}
, m_computations(computations)
, m_abort {false}
, m_hashNumber {0}
, m_progress {0}
Expand Down Expand Up @@ -59,19 +59,14 @@ QMap<QString, HashList> ComputerThread::getResults() const
void ComputerThread::abort()
{
m_abort = true;
m_condition.wakeAll();
}

//----------------------------------------------------------------
void ComputerThread::onHashComputed(const QString &filename, const Hash *hash)
{
QMutexLocker lock(&m_progressMutex);

emit hashComputed(filename, hash);

++m_progress;

emit progress((100*m_progress)/m_hashNumber);

for(auto hashSPtr: m_computations[filename])
{
if(hashSPtr.get() == hash)
Expand All @@ -81,22 +76,45 @@ void ComputerThread::onHashComputed(const QString &filename, const Hash *hash)
}
}

--m_threadsNum;
m_condition.wakeAll();
emit hashComputed(filename, hash);
}

//----------------------------------------------------------------
void ComputerThread::onProgressSignaled()
{
QMutexLocker lock(&m_progressMutex);

if(m_hashNumber > 0)
{
auto progressValue = m_progress * 100;

std::for_each(m_threads.constBegin(), m_threads.constEnd(), [&progressValue](const std::shared_ptr<HashChecker> task) { if(task) progressValue += task->progress();});

emit progress(progressValue/m_hashNumber);
}
}

//----------------------------------------------------------------
void ComputerThread::run()
{
QString fileErrors;
QList<std::shared_ptr<HashChecker>> threads;

for(int i = 0; i < m_computations.keys().size() && !m_abort; ++i)
{
auto filename = m_computations.keys().at(i);

for(auto hash: m_computations[filename])
{
// wait if we have reached the maximum number of threads.
if(m_threadsNum == m_maxThreads)
{
m_mutex.lock();
m_condition.wait(&m_mutex);
m_mutex.unlock();
}

if(m_abort) break;

auto file = new QFile{filename};

if(!file->open(QIODevice::ReadOnly) || !file->seek(0))
Expand All @@ -108,30 +126,35 @@ void ComputerThread::run()

auto runnable = std::make_shared<HashChecker>(hash, file);
connect(runnable.get(), SIGNAL(hashComputed(const QString &, const Hash *)), this, SLOT(onHashComputed(const QString &, const Hash *)));
connect(runnable.get(), SIGNAL(hashUpdated(const QString &, const Hash *, const int)), this, SIGNAL(hashUpdated(const QString &, const Hash *, const int)));
connect(runnable.get(), SIGNAL(progressed()), this, SLOT(onProgressSignaled()));
connect(runnable.get(), SIGNAL(finished()), this, SLOT(onThreadFinished()));

threads << runnable;

// wait if we have reached the maximum number of threads.
if(m_threadsNum == m_maxThreads)
{
m_mutex.lock();
m_condition.wait(&m_mutex);
m_mutex.unlock();
}
QMutexLocker lock(&m_progressMutex);

++m_threadsNum;
++m_threadsNum;
m_threads << runnable;
}
runnable->start();
}
}

QApplication::processEvents();
while(!m_threads.isEmpty() && !m_abort)
{
m_mutex.lock();
m_condition.wait(&m_mutex);
m_mutex.unlock();
}

for(auto thread: threads)
if(m_abort)
{
// waits for all threads to finish before ending.
thread->wait();
QMutexLocker lock(&m_progressMutex);

std::for_each(m_threads.begin(), m_threads.end(), [](std::shared_ptr<HashChecker> thread) { thread->abort(); });
}
threads.clear();

QApplication::processEvents();

if(!fileErrors.isEmpty())
{
Expand All @@ -146,3 +169,32 @@ void ComputerThread::run()
dialog.exec();
}
}

//----------------------------------------------------------------
void ComputerThread::onThreadFinished()
{
QMutexLocker lock(&m_progressMutex);

--m_threadsNum;
++m_progress;

auto senderThread = qobject_cast<HashChecker *>(sender());
if(senderThread)
{
disconnect(senderThread, SIGNAL(hashComputed(const QString &, const Hash *)), this, SLOT(onHashComputed(const QString &, const Hash *)));
disconnect(senderThread, SIGNAL(hashUpdated(const QString &, const Hash *, const int)), this, SIGNAL(hashUpdated(const QString &, const Hash *, const int)));
disconnect(senderThread, SIGNAL(progressed()), this, SLOT(onProgressSignaled()));
disconnect(senderThread, SIGNAL(finished()), this, SLOT(onThreadFinished()));

for(auto thread: m_threads)
{
if(thread.get() == senderThread)
{
m_threads.removeOne(thread);
break;
}
}
}

m_condition.wakeAll();
}
Loading

0 comments on commit ffecc7f

Please sign in to comment.