Skip to content

Commit 5158e9b

Browse files
committed
Finally fixed ProgressBar #4
- If the ProgressBar range would overflow, scale down the values to [0, INT_MAX] - Now also properly changed order in which checkboxes are checked so that the hashes will be computed in the correct order
1 parent 6df68b3 commit 5158e9b

File tree

8 files changed

+130
-72
lines changed

8 files changed

+130
-72
lines changed

GUI/FileHasherUI/Controller.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include <QTime>
66
#include <QJsonDocument>
77

8+
constexpr int BAR_RANGE_MIN = 0;
9+
constexpr int BAR_RANGE_MAX = INT32_MAX;
10+
811
void AddToOutput(QString entry, QListWidget* output)
912
{
1013
// Get current time
@@ -82,18 +85,27 @@ void Controller::HandleResults(const QStringList& result)
8285
}
8386
}
8487

85-
void Controller::UpdateFileProgress(const size_t min, const size_t max, const size_t value)
88+
void Controller::UpdateFileProgress(const size_t fileSize, const size_t value)
8689
{
87-
ui->fileProgressBar->setRange((int)min, (int)max);
88-
ui->fileProgressBar->setValue((int)std::min(value, max));
90+
double percentage = static_cast<double>(value) / static_cast<double>(fileSize);
91+
if (fileSize > BAR_RANGE_MAX)
92+
{
93+
ui->fileProgressBar->setRange(BAR_RANGE_MIN, BAR_RANGE_MAX);
94+
int scaledValue = static_cast<int>(BAR_RANGE_MAX * percentage);
95+
ui->fileProgressBar->setValue((int)std::min(scaledValue, BAR_RANGE_MAX));
96+
}
97+
else
98+
{
99+
ui->fileProgressBar->setRange(BAR_RANGE_MIN, (int)fileSize);
100+
ui->fileProgressBar->setValue((int)std::min(value, fileSize));
101+
}
89102

90-
if (value >= max)
103+
if (value >= fileSize)
91104
{
92105
ui->fileProgressBar->setFormat("100%");
93106
}
94107
else
95108
{
96-
double percentage = static_cast<double>(value) / static_cast<double>(max);
97109
ui->fileProgressBar->setFormat(QString::number((percentage * 100), 'g', 3)+ "%");
98110
}
99111
}
@@ -147,7 +159,7 @@ void Controller::AddToCache(QStringList data)
147159
{
148160
entry = QJsonObject();
149161
entry["filePath"] = data[2];
150-
entry["fileSizeKB"] = (int)delegate->GetSizeFromString(data[4]);
162+
entry["fileSizeB"] = (int)delegate->GetFileSize(data[2]);
151163
entry["hashes"] = QJsonObject();
152164
}
153165

@@ -209,7 +221,7 @@ void Worker::DoWork(const std::vector<HashingAlgorithm*>& hashAlgorithms, const
209221
// No need to monitor empty files
210222
if (currentFileSize > 0)
211223
{
212-
emit UpdateFileStatus(0U, currentFileSize, 0U);
224+
emit UpdateFileStatus(currentFileSize, 0U);
213225
// Tell the display worker to start monitoring the progress of this hash
214226
emit controller->StartMonitoring(hashAlgorithm, currentFileSize);
215227
}
@@ -270,14 +282,14 @@ void Worker::MonitorProgress(const HashingAlgorithm* algorithm, const size_t fil
270282
size_t progress = algorithm->GetBytesProcessed();
271283
if (progress != oldValue)
272284
{
273-
emit UpdateFileStatus(ui->fileProgressBar->minimum(), fileSize, progress);
285+
emit UpdateFileStatus(fileSize, progress);
274286
oldValue = progress;
275287
}
276288

277289
QThread::msleep(10);
278290
}
279291

280-
emit UpdateFileStatus(ui->fileProgressBar->minimum(), fileSize, algorithm->GetBytesProcessed());
292+
emit UpdateFileStatus(fileSize, algorithm->GetBytesProcessed());
281293
}
282294

283295
void Worker::TerminateMonitoring()

GUI/FileHasherUI/Controller.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Worker : public QObject
3434

3535
signals:
3636
void resultReady(const QStringList &result);
37-
void UpdateFileStatus(const size_t min, const size_t max, const size_t value);
37+
void UpdateFileStatus(const size_t fileSize, const size_t value);
3838
void WaitForMonitor();
3939
void ReportError(const QStringList &data);
4040
};
@@ -65,7 +65,7 @@ class Controller : public QObject
6565
// This function will be called when the Worker thread wants to pass on his results,
6666
// most importantly this function will be executed in our main thread, meaning we can do send events
6767
void HandleResults(const QStringList &);
68-
void UpdateFileProgress(const size_t min, const size_t max, const size_t value);
68+
void UpdateFileProgress(const size_t fileSize, const size_t value);
6969
void HandleError(const QStringList &data);
7070
inline void SetHashingStatus(const bool status)
7171
{

GUI/FileHasherUI/filehasher.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,6 @@ void FileHasher::on_hashButton_clicked()
138138
return;
139139
}
140140

141-
if (ui->sha256CB->isChecked())
142-
{
143-
hashAlgoVec.push_back(new SHA256Hasher());
144-
}
145-
if (ui->sha512CB->isChecked())
146-
{
147-
hashAlgoVec.push_back(new SHA512Hasher());
148-
}
149141
if (ui->md5CB->isChecked())
150142
{
151143
hashAlgoVec.push_back(new MD5Hasher());
@@ -154,6 +146,14 @@ void FileHasher::on_hashButton_clicked()
154146
{
155147
hashAlgoVec.push_back(new SHA1Hasher());
156148
}
149+
if (ui->sha256CB->isChecked())
150+
{
151+
hashAlgoVec.push_back(new SHA256Hasher());
152+
}
153+
if (ui->sha512CB->isChecked())
154+
{
155+
hashAlgoVec.push_back(new SHA512Hasher());
156+
}
157157

158158
if (hashAlgoVec.empty())
159159
{

GUI/FileHasherUI/filehasher.ui

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@
230230
<rect>
231231
<x>10</x>
232232
<y>10</y>
233-
<width>65</width>
233+
<width>91</width>
234234
<height>96</height>
235235
</rect>
236236
</property>

GUI/build-FileHasherUI-Desktop_Qt_5_15_0_MSVC2019_64bit-Debug/Makefile.Debug

Lines changed: 16 additions & 4 deletions
Large diffs are not rendered by default.

GUI/build-FileHasherUI-Desktop_Qt_5_15_0_MSVC2019_64bit-Debug/Makefile.Release

Lines changed: 16 additions & 4 deletions
Large diffs are not rendered by default.

GUI/build-FileHasherUI-Desktop_Qt_5_15_0_MSVC2019_64bit-Debug/ui_filehasher.h

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <QtWidgets/QTabWidget>
2323
#include <QtWidgets/QTableWidget>
2424
#include <QtWidgets/QToolButton>
25+
#include <QtWidgets/QVBoxLayout>
2526
#include <QtWidgets/QWidget>
2627

2728
QT_BEGIN_NAMESPACE
@@ -39,11 +40,14 @@ class Ui_FileHasher
3940
QPushButton *clearListButton;
4041
QTabWidget *hashAlgosWidget;
4142
QWidget *hashAlgoTab;
42-
QCheckBox *sha256CB;
43-
QCheckBox *md5CB;
4443
QPushButton *hashButton;
4544
QProgressBar *totalProgressBar;
4645
QProgressBar *fileProgressBar;
46+
QWidget *layoutWidget1;
47+
QVBoxLayout *hashButtons;
48+
QCheckBox *md5CB;
49+
QCheckBox *sha1CB;
50+
QCheckBox *sha256CB;
4751
QCheckBox *sha512CB;
4852
QGroupBox *groupBox;
4953
QToolButton *actionsButton;
@@ -118,16 +122,6 @@ class Ui_FileHasher
118122
hashAlgosWidget->setTabShape(QTabWidget::Rounded);
119123
hashAlgoTab = new QWidget();
120124
hashAlgoTab->setObjectName(QString::fromUtf8("hashAlgoTab"));
121-
sha256CB = new QCheckBox(hashAlgoTab);
122-
sha256CB->setObjectName(QString::fromUtf8("sha256CB"));
123-
sha256CB->setGeometry(QRect(10, 10, 131, 19));
124-
sha256CB->setCursor(QCursor(Qt::PointingHandCursor));
125-
md5CB = new QCheckBox(hashAlgoTab);
126-
md5CB->setObjectName(QString::fromUtf8("md5CB"));
127-
md5CB->setEnabled(true);
128-
md5CB->setGeometry(QRect(10, 50, 101, 19));
129-
md5CB->setCursor(QCursor(Qt::PointingHandCursor));
130-
md5CB->setCheckable(true);
131125
hashButton = new QPushButton(hashAlgoTab);
132126
hashButton->setObjectName(QString::fromUtf8("hashButton"));
133127
hashButton->setGeometry(QRect(10, 250, 201, 21));
@@ -144,10 +138,37 @@ class Ui_FileHasher
144138
fileProgressBar->setObjectName(QString::fromUtf8("fileProgressBar"));
145139
fileProgressBar->setGeometry(QRect(10, 190, 201, 23));
146140
fileProgressBar->setValue(0);
147-
sha512CB = new QCheckBox(hashAlgoTab);
141+
layoutWidget1 = new QWidget(hashAlgoTab);
142+
layoutWidget1->setObjectName(QString::fromUtf8("layoutWidget1"));
143+
layoutWidget1->setGeometry(QRect(10, 10, 91, 96));
144+
hashButtons = new QVBoxLayout(layoutWidget1);
145+
hashButtons->setObjectName(QString::fromUtf8("hashButtons"));
146+
hashButtons->setContentsMargins(0, 0, 0, 0);
147+
md5CB = new QCheckBox(layoutWidget1);
148+
md5CB->setObjectName(QString::fromUtf8("md5CB"));
149+
md5CB->setEnabled(true);
150+
md5CB->setCursor(QCursor(Qt::PointingHandCursor));
151+
md5CB->setCheckable(true);
152+
153+
hashButtons->addWidget(md5CB);
154+
155+
sha1CB = new QCheckBox(layoutWidget1);
156+
sha1CB->setObjectName(QString::fromUtf8("sha1CB"));
157+
158+
hashButtons->addWidget(sha1CB);
159+
160+
sha256CB = new QCheckBox(layoutWidget1);
161+
sha256CB->setObjectName(QString::fromUtf8("sha256CB"));
162+
sha256CB->setCursor(QCursor(Qt::PointingHandCursor));
163+
164+
hashButtons->addWidget(sha256CB);
165+
166+
sha512CB = new QCheckBox(layoutWidget1);
148167
sha512CB->setObjectName(QString::fromUtf8("sha512CB"));
149-
sha512CB->setGeometry(QRect(10, 30, 72, 19));
150168
sha512CB->setCursor(QCursor(Qt::PointingHandCursor));
169+
170+
hashButtons->addWidget(sha512CB);
171+
151172
hashAlgosWidget->addTab(hashAlgoTab, QString());
152173
groupBox = new QGroupBox(centralwidget);
153174
groupBox->setObjectName(QString::fromUtf8("groupBox"));
@@ -185,11 +206,12 @@ class Ui_FileHasher
185206
addFileButton->setText(QCoreApplication::translate("FileHasher", "Add File", nullptr));
186207
clearListButton->setText(QCoreApplication::translate("FileHasher", "Clear List", nullptr));
187208
hashTargetsWidget->setTabText(hashTargetsWidget->indexOf(filesToHashTab), QCoreApplication::translate("FileHasher", "Files to Hash", nullptr));
188-
sha256CB->setText(QCoreApplication::translate("FileHasher", "SHA256", nullptr));
189-
md5CB->setText(QCoreApplication::translate("FileHasher", "MD5", nullptr));
190209
hashButton->setText(QCoreApplication::translate("FileHasher", "Generate Hashes", nullptr));
191210
totalProgressBar->setFormat(QCoreApplication::translate("FileHasher", "Idle", nullptr));
192211
fileProgressBar->setFormat(QCoreApplication::translate("FileHasher", "Idle", nullptr));
212+
md5CB->setText(QCoreApplication::translate("FileHasher", "MD5", nullptr));
213+
sha1CB->setText(QCoreApplication::translate("FileHasher", "SHA1", nullptr));
214+
sha256CB->setText(QCoreApplication::translate("FileHasher", "SHA256", nullptr));
193215
sha512CB->setText(QCoreApplication::translate("FileHasher", "SHA512", nullptr));
194216
hashAlgosWidget->setTabText(hashAlgosWidget->indexOf(hashAlgoTab), QCoreApplication::translate("FileHasher", "Hash Algorithms", nullptr));
195217
groupBox->setTitle(QCoreApplication::translate("FileHasher", "Output", nullptr));

GUI/build-FileHasherUI-Desktop_Qt_5_15_0_MSVC2019_64bit-Release/ui_filehasher.h

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ class Ui_FileHasher
4343
QPushButton *hashButton;
4444
QProgressBar *totalProgressBar;
4545
QProgressBar *fileProgressBar;
46-
QWidget *widget;
47-
QVBoxLayout *verticalLayout;
48-
QCheckBox *sha256CB;
49-
QCheckBox *sha512CB;
46+
QWidget *layoutWidget1;
47+
QVBoxLayout *hashButtons;
5048
QCheckBox *md5CB;
5149
QCheckBox *sha1CB;
50+
QCheckBox *sha256CB;
51+
QCheckBox *sha512CB;
5252
QGroupBox *groupBox;
5353
QToolButton *actionsButton;
5454
QListWidget *outputList;
@@ -138,36 +138,36 @@ class Ui_FileHasher
138138
fileProgressBar->setObjectName(QString::fromUtf8("fileProgressBar"));
139139
fileProgressBar->setGeometry(QRect(10, 190, 201, 23));
140140
fileProgressBar->setValue(0);
141-
widget = new QWidget(hashAlgoTab);
142-
widget->setObjectName(QString::fromUtf8("widget"));
143-
widget->setGeometry(QRect(11, 12, 74, 96));
144-
verticalLayout = new QVBoxLayout(widget);
145-
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
146-
verticalLayout->setContentsMargins(0, 0, 0, 0);
147-
sha256CB = new QCheckBox(widget);
148-
sha256CB->setObjectName(QString::fromUtf8("sha256CB"));
149-
sha256CB->setCursor(QCursor(Qt::PointingHandCursor));
150-
151-
verticalLayout->addWidget(sha256CB);
152-
153-
sha512CB = new QCheckBox(widget);
154-
sha512CB->setObjectName(QString::fromUtf8("sha512CB"));
155-
sha512CB->setCursor(QCursor(Qt::PointingHandCursor));
156-
157-
verticalLayout->addWidget(sha512CB);
158-
159-
md5CB = new QCheckBox(widget);
141+
layoutWidget1 = new QWidget(hashAlgoTab);
142+
layoutWidget1->setObjectName(QString::fromUtf8("layoutWidget1"));
143+
layoutWidget1->setGeometry(QRect(10, 10, 91, 96));
144+
hashButtons = new QVBoxLayout(layoutWidget1);
145+
hashButtons->setObjectName(QString::fromUtf8("hashButtons"));
146+
hashButtons->setContentsMargins(0, 0, 0, 0);
147+
md5CB = new QCheckBox(layoutWidget1);
160148
md5CB->setObjectName(QString::fromUtf8("md5CB"));
161149
md5CB->setEnabled(true);
162150
md5CB->setCursor(QCursor(Qt::PointingHandCursor));
163151
md5CB->setCheckable(true);
164152

165-
verticalLayout->addWidget(md5CB);
153+
hashButtons->addWidget(md5CB);
166154

167-
sha1CB = new QCheckBox(widget);
155+
sha1CB = new QCheckBox(layoutWidget1);
168156
sha1CB->setObjectName(QString::fromUtf8("sha1CB"));
169157

170-
verticalLayout->addWidget(sha1CB);
158+
hashButtons->addWidget(sha1CB);
159+
160+
sha256CB = new QCheckBox(layoutWidget1);
161+
sha256CB->setObjectName(QString::fromUtf8("sha256CB"));
162+
sha256CB->setCursor(QCursor(Qt::PointingHandCursor));
163+
164+
hashButtons->addWidget(sha256CB);
165+
166+
sha512CB = new QCheckBox(layoutWidget1);
167+
sha512CB->setObjectName(QString::fromUtf8("sha512CB"));
168+
sha512CB->setCursor(QCursor(Qt::PointingHandCursor));
169+
170+
hashButtons->addWidget(sha512CB);
171171

172172
hashAlgosWidget->addTab(hashAlgoTab, QString());
173173
groupBox = new QGroupBox(centralwidget);
@@ -209,10 +209,10 @@ class Ui_FileHasher
209209
hashButton->setText(QCoreApplication::translate("FileHasher", "Generate Hashes", nullptr));
210210
totalProgressBar->setFormat(QCoreApplication::translate("FileHasher", "Idle", nullptr));
211211
fileProgressBar->setFormat(QCoreApplication::translate("FileHasher", "Idle", nullptr));
212-
sha256CB->setText(QCoreApplication::translate("FileHasher", "SHA256", nullptr));
213-
sha512CB->setText(QCoreApplication::translate("FileHasher", "SHA512", nullptr));
214212
md5CB->setText(QCoreApplication::translate("FileHasher", "MD5", nullptr));
215213
sha1CB->setText(QCoreApplication::translate("FileHasher", "SHA1", nullptr));
214+
sha256CB->setText(QCoreApplication::translate("FileHasher", "SHA256", nullptr));
215+
sha512CB->setText(QCoreApplication::translate("FileHasher", "SHA512", nullptr));
216216
hashAlgosWidget->setTabText(hashAlgosWidget->indexOf(hashAlgoTab), QCoreApplication::translate("FileHasher", "Hash Algorithms", nullptr));
217217
groupBox->setTitle(QCoreApplication::translate("FileHasher", "Output", nullptr));
218218
actionsButton->setText(QCoreApplication::translate("FileHasher", "Actions", nullptr));

0 commit comments

Comments
 (0)