-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhashwidget.cc
125 lines (106 loc) · 4.05 KB
/
hashwidget.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "hashwidget.hpp"
#include "hashthread.hpp"
#include <gui/messbox.h>
#include <QtWidgets>
namespace Plugin {
class HashWidget::HashWidgetPrivate
{
public:
explicit HashWidgetPrivate(HashWidget *q)
: q_ptr(q)
{
hashComboBox = new QComboBox(q_ptr);
auto metaEnums = QMetaEnum::fromType<QCryptographicHash::Algorithm>();
for (int i = 0; i < metaEnums.keyCount(); ++i) {
auto value = metaEnums.value(i);
if (value == QCryptographicHash::NumAlgorithms) {
continue;
}
hashComboBox->addItem(metaEnums.key(i), value);
if (value == QCryptographicHash::Md5) {
hashComboBox->setCurrentText(metaEnums.key(i));
}
}
selectFileButton = new QPushButton(HashWidget::tr("Select File"), q_ptr);
selectFileButton->setToolTip(HashWidget::tr("Select file to calculate hash."));
selectFileButton->setObjectName("BlueButton");
calculateButton = new QPushButton(HashWidget::tr("Calculate"), q_ptr);
calculateButton->setToolTip(HashWidget::tr("Calculate hash."));
calculateButton->setObjectName("BlueButton");
inputEdit = new QTextEdit(q_ptr);
outputEdit = new QTextEdit(q_ptr);
hashThread = new HashThread(q_ptr);
}
void setupUI() const
{
auto *buttonLayout = new QHBoxLayout;
buttonLayout->setSpacing(20);
buttonLayout->addWidget(selectFileButton);
buttonLayout->addWidget(hashComboBox);
buttonLayout->addWidget(calculateButton);
auto *descriptionLabel = new QLabel(
HashWidget::tr("If Input String is file path and file exists, calculate hash of file. "
"Otherwise calculate hash of Input String."),
q_ptr);
descriptionLabel->setWordWrap(true);
auto *layout = new QVBoxLayout(q_ptr);
layout->addWidget(descriptionLabel);
layout->addWidget(new QLabel(HashWidget::tr("Input:"), q_ptr));
layout->addWidget(inputEdit);
layout->addLayout(buttonLayout);
layout->addWidget(new QLabel(HashWidget::tr("Output:"), q_ptr));
layout->addWidget(outputEdit);
}
HashWidget *q_ptr;
QComboBox *hashComboBox;
QPushButton *selectFileButton;
QPushButton *calculateButton;
QTextEdit *inputEdit;
QTextEdit *outputEdit;
HashThread *hashThread;
};
HashWidget::HashWidget(QWidget *parent)
: QWidget{parent}
, d_ptr(new HashWidgetPrivate(this))
{
d_ptr->setupUI();
buildConnect();
}
HashWidget::~HashWidget() = default;
void HashWidget::onSelectFile()
{
auto path = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation)
.value(0, QDir::homePath());
auto filePath = QFileDialog::getOpenFileName(this, tr("Select File"), path);
d_ptr->inputEdit->setText(filePath);
}
void HashWidget::onCalculate()
{
auto input = d_ptr->inputEdit->toPlainText();
if (input.isEmpty()) {
GUI::MessBox::Warning(this, tr("Input is empty!"), GUI::MessBox::CloseButton);
return;
}
auto hash = d_ptr->hashComboBox->currentData().toInt();
auto started = d_ptr->hashThread->startHash(input,
static_cast<QCryptographicHash::Algorithm>(hash));
if (!started) {
GUI::MessBox::Warning(this, tr("Hash thread is running!"), GUI::MessBox::CloseButton);
return;
}
d_ptr->calculateButton->setEnabled(false);
d_ptr->calculateButton->setText(tr("Calculating..."));
}
void HashWidget::onHashFinished(const QString &result)
{
d_ptr->outputEdit->setText(result);
d_ptr->calculateButton->setEnabled(true);
d_ptr->calculateButton->setText(tr("Calculate"));
}
void HashWidget::buildConnect()
{
connect(d_ptr->selectFileButton, &QPushButton::clicked, this, &HashWidget::onSelectFile);
connect(d_ptr->calculateButton, &QPushButton::clicked, this, &HashWidget::onCalculate);
connect(d_ptr->hashThread, &HashThread::hashFinished, this, &HashWidget::onHashFinished);
}
} // namespace Plugin