|
6 | 6 | #include <QApplication>
|
7 | 7 | #include <QToolTip>
|
8 | 8 | #include <QPainter>
|
| 9 | +#include <QtConcurrent/QtConcurrent> |
| 10 | +#include <QFuture> |
| 11 | +#include <QFutureWatcher> |
9 | 12 |
|
10 | 13 | void FileInfoWidget::addCopyableField(const QString& name, const QVariant& value)
|
11 | 14 | {
|
@@ -35,12 +38,31 @@ void FileInfoWidget::addHashField(
|
35 | 38 | auto& [row, column] = this->m_fieldPosition;
|
36 | 39 |
|
37 | 40 | const auto hashFieldColor = getThemeColor(AlphanumericHighlightColor);
|
38 |
| - const auto crypto = QCryptographicHash::hash(data, algorithm); |
39 |
| - const auto hashLabel = new CopyableLabel(crypto.toHex(), hashFieldColor); |
| 41 | + auto hashLabel = new CopyableLabel("Calculating...", hashFieldColor); |
40 | 42 | hashLabel->setFont(getMonospaceFont(this));
|
41 | 43 |
|
42 | 44 | this->m_layout->addWidget(new QLabel(hashName), row, column);
|
43 | 45 | this->m_layout->addWidget(hashLabel, row++, column + 1);
|
| 46 | + |
| 47 | + // Process the hash calculations in a separate thread and update the label when done |
| 48 | + QPointer<QFutureWatcher<QByteArray>> watcher = new QFutureWatcher<QByteArray>(this); |
| 49 | + connect(watcher, &QFutureWatcher<QByteArray>::finished, this, [watcher, hashLabel]() { |
| 50 | + if (watcher) |
| 51 | + { |
| 52 | + hashLabel->setText(watcher->result().toHex()); |
| 53 | + watcher->deleteLater(); |
| 54 | + } |
| 55 | + }); |
| 56 | + QFuture<QByteArray> future = QtConcurrent::run([data, algorithm]() { |
| 57 | + return QCryptographicHash::hash(data, algorithm); |
| 58 | + }); |
| 59 | + watcher->setFuture(future); |
| 60 | + connect(this, &QObject::destroyed, this, [watcher]() { |
| 61 | + if (watcher && watcher->isRunning()) { |
| 62 | + watcher->cancel(); |
| 63 | + watcher->waitForFinished(); |
| 64 | + } |
| 65 | + }); |
44 | 66 | }
|
45 | 67 |
|
46 | 68 | FileInfoWidget::FileInfoWidget(QWidget* parent, BinaryViewRef bv)
|
|
0 commit comments