Skip to content

Commit

Permalink
Rename class name
Browse files Browse the repository at this point in the history
  • Loading branch information
Yong++ committed Feb 10, 2022
1 parent 1853ec9 commit 7582dfe
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 59 deletions.
24 changes: 12 additions & 12 deletions tinypng/CompressThread.cpp → tinypng/Compress.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "CompressThread.h"
#include "Compress.h"
#include <QFile>
#include <QObject>
#include <QEventLoop>
Expand All @@ -11,7 +11,7 @@
#include <QNetworkProxy>
#include <QRandomGenerator>

CompressThread::CompressThread(QThread* td, const configItem& cfg, QString ort)
Compress::Compress(QThread* td, const configItem& cfg, QString ort)
: QObject(NULL),
row(0),
root(""),
Expand All @@ -38,21 +38,21 @@ CompressThread::CompressThread(QThread* td, const configItem& cfg, QString ort)

}

void CompressThread::addTask(const int r, const QString& rt, QString& ph) {
void Compress::addTask(const int r, const QString& rt, QString& ph) {
row = r;
root = rt;
path = ph;
state = 1;
this->eventloop->quit();
}

void CompressThread::quit() {
void Compress::quit() {
state = -1;
this->eventloop->quit();
this->thread->quit();
}

void CompressThread::run() {
void Compress::run() {


while (true) {
Expand Down Expand Up @@ -150,7 +150,7 @@ void CompressThread::run() {

}

void CompressThread::download(const QByteArray& bytes, const QString& ip) {
void Compress::download(const QByteArray& bytes, const QString& ip) {

QJsonDocument doc = QJsonDocument::fromJson(bytes);
QJsonObject jsonobj = doc.object();
Expand Down Expand Up @@ -210,7 +210,7 @@ void CompressThread::download(const QByteArray& bytes, const QString& ip) {
qDebug() << "wrote:" << file.write(binary);
file.close();

CompressThreadResult result;
CompressResult result;
result.errcode = 0;
result.size = binary.length();
result.path = outputPath;
Expand Down Expand Up @@ -239,12 +239,12 @@ void CompressThread::download(const QByteArray& bytes, const QString& ip) {
delete reply;
}

void CompressThread::setAuthentication(QNetworkReply* reply, QAuthenticator* authenticator) {
void Compress::setAuthentication(QNetworkReply* reply, QAuthenticator* authenticator) {
qDebug() << "to set auth info";
}

void CompressThread::emitError(const QString& msg, const int& errcode) {
CompressThreadResult result;
void Compress::emitError(const QString& msg, const int& errcode) {
CompressResult result;
result.errcode = errcode;
result.errmsg = msg;
result.row = this->row;
Expand All @@ -255,7 +255,7 @@ void CompressThread::emitError(const QString& msg, const int& errcode) {
}


QString CompressThread::generateIp() {
QString Compress::generateIp() {
qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
QStringList seg = QStringList();
for (int i = 0; i < 4; i++)
Expand All @@ -267,7 +267,7 @@ QString CompressThread::generateIp() {
return seg.join(".");
}

CompressThread::~CompressThread()
Compress::~Compress()
{
delete eventloop;
delete mgr;
Expand Down
10 changes: 5 additions & 5 deletions tinypng/CompressThread.h → tinypng/Compress.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QEventLoop>
#include "Config.h"

struct CompressThreadResult {
struct CompressResult {
int errcode;
QString errmsg;
int size;
Expand All @@ -18,9 +18,9 @@ struct CompressThreadResult {
};


Q_DECLARE_METATYPE(CompressThreadResult);
Q_DECLARE_METATYPE(CompressResult);

class CompressThread : public QObject
class Compress : public QObject
{
Q_OBJECT

Expand All @@ -34,8 +34,8 @@ class CompressThread : public QObject
int state; // 0 新任务待处理 -1 退出
QEventLoop* eventloop;
QNetworkAccessManager* mgr;
CompressThread(QThread* thread, const configItem& config, QString);
~CompressThread();
Compress(QThread* thread, const configItem& config, QString);
~Compress();

void addTask(int row, const QString& root, QString& path);
void quit();
Expand Down
30 changes: 15 additions & 15 deletions tinypng/CompressThreadDispatcher.cpp → tinypng/Dispatcher.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "CompressThreadDispatcher.h"
#include "Dispatcher.h"
#include <QListIterator>
#include <QTimer>

CompressThreadDispatcher::CompressThreadDispatcher(QThread* td, Console* c, MyTableView* t)
Dispatcher::Dispatcher(QThread* td, Console* c, MyTableView* t)
{
console = c;
tableview = t;
Expand All @@ -12,13 +12,13 @@ CompressThreadDispatcher::CompressThreadDispatcher(QThread* td, Console* c, MyTa
offsetRow = 0;
runThreadNum = 0;
this->mutex = new QMutex();
workers = QList<CompressThread*>();
workers = QList<Compress*>();
thread = td;
elapsedTimer = new QElapsedTimer();

}

void CompressThreadDispatcher::run() {
void Dispatcher::run() {
config = Config(this).get();
offsetRow = 0;
finishNum = 0;
Expand Down Expand Up @@ -49,7 +49,7 @@ void CompressThreadDispatcher::run() {

QThread* wokrer_thread = new QThread(this);

CompressThread* worker = new CompressThread(wokrer_thread, config, outputRoot);
Compress* worker = new Compress(wokrer_thread, config, outputRoot);
connect(worker, SIGNAL(finished(QVariant)), this, SLOT(doneTask(QVariant)));
connect(wokrer_thread, SIGNAL(started()), worker, SLOT(run()));
connect(wokrer_thread, SIGNAL(finished()), worker, SLOT(deleteLater()));
Expand All @@ -68,7 +68,7 @@ void CompressThreadDispatcher::run() {

}

void CompressThreadDispatcher::nextTask(CompressThread* worker) {
void Dispatcher::nextTask(Compress* worker) {

if (this->loop != nullptr && !this->loop->isRunning()) {
qDebug() << "-------------------------->the progress was paused~!";
Expand Down Expand Up @@ -101,11 +101,11 @@ void CompressThreadDispatcher::nextTask(CompressThread* worker) {



void CompressThreadDispatcher::doneTask(QVariant variant) {
void Dispatcher::doneTask(QVariant variant) {
this->mutex->lock();

finishNum++;
CompressThreadResult result = variant.value<CompressThreadResult>();
CompressResult result = variant.value<CompressResult>();
qDebug() << "收到结果:" << result.errcode << result.errmsg << result.size << result.path << result.row;


Expand Down Expand Up @@ -140,21 +140,21 @@ void CompressThreadDispatcher::doneTask(QVariant variant) {
this->tableview->update(this->model->index(result.row, 4));

//this->tableview->dataChanged(rowLeftIndex, rowRightIndex);
CompressThread* worker = (CompressThread*)sender();
Compress* worker = (Compress*)sender();
this->nextTask(worker);
this->mutex->unlock();


}

void CompressThreadDispatcher::quit() {
void Dispatcher::quit() {
qDebug() << "退出";


loop->quit();
QListIterator<CompressThread*> it(workers);
QListIterator<Compress*> it(workers);
while (it.hasNext()) {
CompressThread* worker = it.next();
Compress* worker = it.next();
if (worker->thread->isRunning()) {
disconnect(worker, SIGNAL(finished(QVariant)), this, SLOT(doneTask(QVariant)));
worker->quit();
Expand All @@ -177,7 +177,7 @@ void CompressThreadDispatcher::quit() {
workers.clear();
}

void CompressThreadDispatcher::finished() {
void Dispatcher::finished() {

if (finishNum > 0) {
QString spentime = QString().sprintf("%.3f", float(this->elapsedTimer->elapsed()) / 1000);
Expand All @@ -190,7 +190,7 @@ void CompressThreadDispatcher::finished() {

}

int CompressThreadDispatcher::getUnhandleNum() {
int Dispatcher::getUnhandleNum() {
int total = this->model->rowCount();
int num = 0;
for (int i = 0; i < total; i++) {
Expand All @@ -202,7 +202,7 @@ int CompressThreadDispatcher::getUnhandleNum() {
return num;
}

CompressThreadDispatcher::~CompressThreadDispatcher()
Dispatcher::~Dispatcher()
{
delete elapsedTimer;

Expand Down
12 changes: 6 additions & 6 deletions tinypng/CompressThreadDispatcher.h → tinypng/Dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#include "TableModel.h"
#include "Console.h"
#include "Config.h"
#include "CompressThread.h"
#include "Compress.h"
#include <QElapsedTimer>


class CompressThreadDispatcher : public QObject
class Dispatcher : public QObject
{
Q_OBJECT

Expand All @@ -31,14 +31,14 @@ class CompressThreadDispatcher : public QObject
QElapsedTimer* elapsedTimer;
QEventLoop* loop;
QMutex* mutex;
QList<CompressThread*> workers;
QList<Compress*> workers;
QThread* thread;


CompressThreadDispatcher(QThread* thread, Console* console, MyTableView* tableview);
Dispatcher(QThread* thread, Console* console, MyTableView* tableview);

void nextTask(CompressThread* thread);
~CompressThreadDispatcher();
void nextTask(Compress* thread);
~Dispatcher();
void quit();
private:
int getUnhandleNum();
Expand Down
2 changes: 1 addition & 1 deletion tinypng/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ MainWindow::MainWindow(QWidget* parent) :

QThread* dispatcherThread = new QThread();

dispatcher = new CompressThreadDispatcher(dispatcherThread, this->console, this->table);
dispatcher = new Dispatcher(dispatcherThread, this->console, this->table);
connect(dispatcher, SIGNAL(started()), this, SLOT(showPauseBtn()));
connect(dispatcherThread, SIGNAL(started()), dispatcher, SLOT(run()));
connect(dispatcherThread, SIGNAL(finished()), dispatcher, SLOT(finished()));
Expand Down
4 changes: 2 additions & 2 deletions tinypng/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "MyTableView.h"
#include "MyOverlay.h"
#include "Console.h"
#include "CompressThreadDispatcher.h"
#include "Dispatcher.h"
#include "Config.h"
#include "SettingWindow.h"
#include "Scanner.h"
Expand Down Expand Up @@ -37,7 +37,7 @@ class MainWindow :
void show();

private:
CompressThreadDispatcher* dispatcher;
Dispatcher* dispatcher;
Ui::MainWindow* ui;

void _buildTopBtns();
Expand Down
8 changes: 4 additions & 4 deletions tinypng/VsTinypng.pro
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ QT = core gui widgets network
CONFIG += c++11

HEADERS = \
$$PWD/CompressThread.h \
$$PWD/CompressThreadDispatcher.h \
$$PWD/Compress.h \
$$PWD/Dispatcher.h \
$$PWD/Config.h \
$$PWD/Console.h \
$$PWD/GridTableHeaderModel.h \
Expand All @@ -26,8 +26,8 @@ HEADERS = \
$$PWD/Utility.h \

SOURCES = \
$$PWD/CompressThread.cpp \
$$PWD/CompressThreadDispatcher.cpp \
$$PWD/Compress.cpp \
$$PWD/Dispatcher.cpp \
$$PWD/Config.cpp \
$$PWD/Console.cpp \
$$PWD/GridTableHeaderModel.cpp \
Expand Down
8 changes: 4 additions & 4 deletions tinypng/tinypng.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<QtMoc Include="Compress.h" />
<ClInclude Include="TableHeaderItem.h" />
<ClInclude Include="Utility.h" />
<QtMoc Include="SingleApplication.h" />
Expand All @@ -199,18 +200,17 @@
<QtMoc Include="Scanner.h" />
<QtMoc Include="MyOverlay.h" />
<QtMoc Include="Console.h" />
<QtMoc Include="CompressThread.h" />
<QtMoc Include="Config.h" />
<QtMoc Include="CompressThreadDispatcher.h" />
<QtMoc Include="Dispatcher.h" />
<QtMoc Include="GridTableHeaderView.h" />
<QtMoc Include="GridTableHeaderModel.h" />
<ClInclude Include="resource.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="CompressThread.cpp" />
<ClCompile Include="CompressThreadDispatcher.cpp" />
<ClCompile Include="Compress.cpp" />
<ClCompile Include="Config.cpp" />
<ClCompile Include="Console.cpp" />
<ClCompile Include="Dispatcher.cpp" />
<ClCompile Include="GridTableHeaderModel.cpp" />
<ClCompile Include="GridTableHeaderView.cpp" />
<ClCompile Include="Scanner.cpp" />
Expand Down
Loading

0 comments on commit 7582dfe

Please sign in to comment.