• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Keine Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

ファイルからMD5計算


Commit MetaInfo

Revision2b6d750c1137e78974bb18ef8b8ca1cbf0744add (tree)
Zeit2013-12-15 09:50:45
Autoralucky4416 <alucky4416@user...>
Commiteralucky4416

Log Message

CHG: CalcMD5 Change to QThread.

Ändern Zusammenfassung

Diff

--- a/QtCalcMD5.pro
+++ b/QtCalcMD5.pro
@@ -11,8 +11,10 @@ TEMPLATE = app
1111
1212
1313 SOURCES += main.cpp\
14- mainwindow.cpp
14+ mainwindow.cpp \
15+ thcalcmd5.cpp
1516
16-HEADERS += mainwindow.h
17+HEADERS += mainwindow.h \
18+ thcalcmd5.h
1719
1820 FORMS += mainwindow.ui
--- a/mainwindow.cpp
+++ b/mainwindow.cpp
@@ -12,6 +12,7 @@
1212 #include "mainwindow.h"
1313 #include "ui_mainwindow.h"
1414
15+#if 0
1516 QString CalcMD5(QString filepath)
1617 {
1718 QString ret = "";
@@ -34,6 +35,7 @@ QString CalcMD5(QString filepath)
3435 }
3536 return ret;
3637 }
38+#endif
3739
3840 MainWindow::MainWindow(QWidget *parent) :
3941 QMainWindow(parent),
@@ -46,10 +48,22 @@ MainWindow::MainWindow(QWidget *parent) :
4648 firstShow = true;
4749
4850 setAcceptDrops(true); // available Drop file
51+
52+ thcalcmd5 = new ThCalcMD5(this);
53+ connect(thcalcmd5, SIGNAL(notify_calc_end(QString)),
54+ this, SLOT(slot_calc_end(QString)));
55+ connect(thcalcmd5, SIGNAL(notify_calc_progress(int)),
56+ this, SLOT(slot_calc_progress(int)));
57+ thcalcmd5->start();
58+
4959 }
5060
5161 MainWindow::~MainWindow()
5262 {
63+ thcalcmd5->stop();
64+ thcalcmd5->wait();
65+ delete thcalcmd5;
66+
5367 delete ui;
5468 }
5569
@@ -125,7 +139,6 @@ void MainWindow::on_toolButton_Select_clicked()
125139 } else {
126140 ;
127141 }
128- ui->progressBar->setValue(100);
129142 }
130143 }
131144
@@ -134,6 +147,13 @@ void MainWindow::on_pushButton_Start_clicked()
134147 // QMessageBox::information(this, tr("information"), tr("StartButton clicked."));
135148 if (ui->lineEdit_FilePath->text().isEmpty()) return;
136149
150+ ui->lineEdit_ResultMD5->clear();
151+ ui->progressBar->setValue(0);
152+ ui->pushButton_Abort->setEnabled(true);
153+
154+ thcalcmd5->sendEvent_Start(ui->lineEdit_FilePath->text());
155+
156+#if 0
137157 QString result;
138158 result = CalcMD5(ui->lineEdit_FilePath->text());
139159 if (result.isEmpty()) {
@@ -143,11 +163,13 @@ void MainWindow::on_pushButton_Start_clicked()
143163 ui->lineEdit_ResultMD5->setText(result);
144164 on_lineEdit_VerifyMD5_textChanged();
145165 }
166+#endif
146167 }
147168
148169 void MainWindow::on_pushButton_Abort_clicked()
149170 {
150- QMessageBox::information(this, tr("information"), tr("AbortButton clicked."));
171+// QMessageBox::information(this, tr("information"), tr("AbortButton clicked."));
172+ thcalcmd5->sendEvent_Stop();
151173 }
152174
153175 void MainWindow::on_lineEdit_VerifyMD5_textChanged()
@@ -164,3 +186,21 @@ void MainWindow::on_lineEdit_VerifyMD5_textChanged()
164186 }
165187
166188 }
189+
190+void MainWindow::slot_calc_end(QString result)
191+{
192+ if (result.isEmpty()) {
193+ ;
194+ } else {
195+ ui->lineEdit_ResultMD5->clear();
196+ ui->lineEdit_ResultMD5->setText(result);
197+
198+ on_lineEdit_VerifyMD5_textChanged();
199+ }
200+ ui->pushButton_Abort->setEnabled(false);
201+}
202+
203+void MainWindow::slot_calc_progress(int progress)
204+{
205+ ui->progressBar->setValue(progress);
206+}
--- a/mainwindow.h
+++ b/mainwindow.h
@@ -1,6 +1,8 @@
11 #ifndef MAINWINDOW_H
22 #define MAINWINDOW_H
33
4+#include "thcalcmd5.h"
5+
46 #include <QMainWindow>
57
68 namespace Ui {
@@ -22,6 +24,9 @@ private slots:
2224 void dragEnterEvent(QDragEnterEvent *event);
2325 void dropEvent(QDropEvent *event);
2426
27+ void slot_calc_end(QString);
28+ void slot_calc_progress(int);
29+
2530 void on_actionExit_triggered();
2631
2732 void on_actionAbout_triggered();
@@ -40,6 +45,8 @@ private:
4045 Ui::MainWindow *ui;
4146
4247 bool firstShow;
48+
49+ ThCalcMD5 *thcalcmd5;
4350 };
4451
4552 #endif // MAINWINDOW_H
--- a/mainwindow.ui
+++ b/mainwindow.ui
@@ -10,6 +10,18 @@
1010 <height>331</height>
1111 </rect>
1212 </property>
13+ <property name="minimumSize">
14+ <size>
15+ <width>403</width>
16+ <height>331</height>
17+ </size>
18+ </property>
19+ <property name="maximumSize">
20+ <size>
21+ <width>403</width>
22+ <height>331</height>
23+ </size>
24+ </property>
1325 <property name="windowTitle">
1426 <string>QtCalcMD5</string>
1527 </property>
--- /dev/null
+++ b/thcalcmd5.cpp
@@ -0,0 +1,106 @@
1+#include "thcalcmd5.h"
2+
3+#include <QDebug>
4+
5+#include <QFile>
6+#include <QCryptographicHash>
7+
8+ThCalcMD5::ThCalcMD5(QObject *parent) :
9+ QThread(parent)
10+{
11+ stopped = false;
12+
13+ EvtQue = new QQueue<EventData>();
14+}
15+
16+ThCalcMD5::~ThCalcMD5()
17+{
18+ delete EvtQue;
19+}
20+
21+void ThCalcMD5::stop()
22+{
23+ stopped = true;
24+}
25+
26+void ThCalcMD5::run()
27+{
28+ EventData event;
29+ int event_id;
30+ int status = 0; // 0 = idle, 1 = exec
31+
32+ QString result = "";
33+ QCryptographicHash hash(QCryptographicHash::Md5);
34+
35+ QFile in;
36+ char buf[8192];
37+ int bytesRead;
38+ quint64 filesize = 0;
39+ int totalread = 0;
40+
41+ while (!stopped) {
42+ if (EvtQue->isEmpty()) {
43+ if (status == 1) {
44+ if ((bytesRead = in.read(buf, sizeof(buf))) > 0) {
45+ hash.addData(buf, bytesRead);
46+ totalread += bytesRead;
47+// qDebug() << "Progress = " << (((double)totalread / (double)filesize) * 100.0);
48+ emit notify_calc_progress(((double)totalread / (double)filesize) * 100.0);
49+ } else {
50+ result = hash.result().toHex();
51+ emit notify_calc_progress(100);
52+ emit notify_calc_end(result);
53+ in.close();
54+ status = 0;
55+ }
56+ } else {
57+ msleep(100);
58+ }
59+ } else {
60+ event = EvtQue->dequeue();
61+ event_id = event.id;
62+ switch(event_id) {
63+ case Ev_Start:
64+ if (status == 0) {
65+ in.setFileName(event.param);
66+ if (!in.open(QIODevice::ReadOnly)) {
67+ emit notify_calc_end("");
68+ return;
69+ }
70+ filesize = in.size();
71+ totalread = 0;
72+ status = 1;
73+ }
74+ break;
75+ case Ev_Stop:
76+ if (status == 1) {
77+ in.close();
78+ emit notify_calc_end("");
79+ status = 0;
80+ }
81+ break;
82+ default:
83+ break;
84+ }
85+ }
86+ }
87+
88+}
89+
90+void ThCalcMD5::sendEvent_Start(QString filepath)
91+{
92+ EventData evt;
93+
94+ evt.id = Ev_Start;
95+ evt.param = filepath;
96+ EvtQue->enqueue(evt);
97+}
98+
99+void ThCalcMD5::sendEvent_Stop()
100+{
101+ EventData evt;
102+
103+ evt.id = Ev_Stop;
104+ EvtQue->enqueue(evt);
105+}
106+
--- /dev/null
+++ b/thcalcmd5.h
@@ -0,0 +1,46 @@
1+#ifndef THCALCMD5_H
2+#define THCALCMD5_H
3+
4+#include <QThread>
5+#include <QQueue>
6+
7+typedef struct StEventData {
8+ int id; // Event ID
9+ QString param; // param data, LCD output string
10+} EventData;
11+
12+class ThCalcMD5 : public QThread
13+{
14+ Q_OBJECT
15+public:
16+ explicit ThCalcMD5(QObject *parent = 0);
17+ ~ThCalcMD5();
18+ void stop();
19+
20+ void sendEvent_Start(QString filepath);
21+ void sendEvent_Stop();
22+
23+signals:
24+ void notify_calc_progress(int progress);
25+ void notify_calc_end(QString md5);
26+
27+public slots:
28+
29+protected:
30+ void run();
31+
32+private:
33+ volatile bool stopped;
34+
35+ // Event ID
36+ enum {
37+ Ev_Nop = 0,
38+ Ev_Start,
39+ Ev_Stop
40+ };
41+ QQueue<EventData> *EvtQue; // EventQueue
42+
43+
44+};
45+
46+#endif // THCALCMD5_H