Implement the format conversion worker
Ignore the format file edit. Reformat after merge
This commit is contained in:
parent
24e186e73b
commit
dcd8e5e34c
@ -1,4 +1,4 @@
|
|||||||
AccessModifierOffset: 0
|
AccessModifierOffset: -4
|
||||||
AlignEscapedNewlinesLeft: false
|
AlignEscapedNewlinesLeft: false
|
||||||
AlignTrailingComments: true
|
AlignTrailingComments: true
|
||||||
AllowAllParametersOfDeclarationOnNextLine: false
|
AllowAllParametersOfDeclarationOnNextLine: false
|
||||||
@ -12,7 +12,7 @@ BreakBeforeBinaryOperators: true
|
|||||||
BreakBeforeBraces: Attach
|
BreakBeforeBraces: Attach
|
||||||
BreakBeforeTernaryOperators: false
|
BreakBeforeTernaryOperators: false
|
||||||
BreakConstructorInitializersBeforeComma: false
|
BreakConstructorInitializersBeforeComma: false
|
||||||
ColumnLimit: 140
|
ColumnLimit: 120
|
||||||
CommentPragmas: ''
|
CommentPragmas: ''
|
||||||
ConstructorInitializerAllOnOneLineOrOnePerLine: false
|
ConstructorInitializerAllOnOneLineOrOnePerLine: false
|
||||||
ConstructorInitializerIndentWidth: 0
|
ConstructorInitializerIndentWidth: 0
|
||||||
|
@ -46,7 +46,8 @@ SOURCES += main.cpp\
|
|||||||
cropeditor/drawing/pathitem.cpp \
|
cropeditor/drawing/pathitem.cpp \
|
||||||
cropeditor/drawing/lineitem.cpp \
|
cropeditor/drawing/lineitem.cpp \
|
||||||
cropeditor/drawing/textitem.cpp \
|
cropeditor/drawing/textitem.cpp \
|
||||||
colorpicker/colorpickerscene.cpp
|
colorpicker/colorpickerscene.cpp \
|
||||||
|
worker/worker.cpp
|
||||||
|
|
||||||
HEADERS += mainwindow.hpp \
|
HEADERS += mainwindow.hpp \
|
||||||
cropeditor/cropeditor.hpp \
|
cropeditor/cropeditor.hpp \
|
||||||
@ -74,7 +75,8 @@ HEADERS += mainwindow.hpp \
|
|||||||
cropeditor/drawing/textitem.hpp \
|
cropeditor/drawing/textitem.hpp \
|
||||||
colorpicker/colorpickerscene.hpp \
|
colorpicker/colorpickerscene.hpp \
|
||||||
platformbackend.hpp \
|
platformbackend.hpp \
|
||||||
gif-h/gif.h
|
gif-h/gif.h \
|
||||||
|
worker/worker.hpp
|
||||||
|
|
||||||
mac {
|
mac {
|
||||||
SOURCES += $$PWD/platformspecifics/mac/macbackend.cpp
|
SOURCES += $$PWD/platformspecifics/mac/macbackend.cpp
|
||||||
|
15
main.cpp
15
main.cpp
@ -3,28 +3,30 @@
|
|||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QCommandLineParser>
|
#include <QCommandLineParser>
|
||||||
#include <QtGlobal>
|
#include <QtGlobal>
|
||||||
|
#include <iostream>
|
||||||
#include <notifications.hpp>
|
#include <notifications.hpp>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <worker/worker.hpp>
|
||||||
|
|
||||||
bool verbose = false;
|
bool verbose = false;
|
||||||
|
|
||||||
void handler(QtMsgType type, const QMessageLogContext &, const QString &msg) {
|
void handler(QtMsgType type, const QMessageLogContext &, const QString &msg) {
|
||||||
QByteArray localMsg = msg.toLocal8Bit();
|
std::string stdMsg = msg.toStdString();
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case QtDebugMsg:
|
case QtDebugMsg:
|
||||||
if (verbose) fprintf(stdout, "DEBUG: %s\n", localMsg.constData());
|
if (verbose) std::cout << "DEBUG: " << stdMsg << "\n";
|
||||||
break;
|
break;
|
||||||
case QtInfoMsg:
|
case QtInfoMsg:
|
||||||
fprintf(stdout, "INFO: %s\n", localMsg.constData());
|
std::cout << "INFO: " << stdMsg << "\n";
|
||||||
break;
|
break;
|
||||||
case QtWarningMsg:
|
case QtWarningMsg:
|
||||||
fprintf(stderr, "WARN: %s\n", localMsg.constData());
|
std::cerr << "WARN: " << stdMsg << "\n";
|
||||||
break;
|
break;
|
||||||
case QtCriticalMsg:
|
case QtCriticalMsg:
|
||||||
fprintf(stderr, "CRIT: %s\n", localMsg.constData());
|
std::cerr << "CRIT: " << stdMsg << "\n";
|
||||||
break;
|
break;
|
||||||
case QtFatalMsg:
|
case QtFatalMsg:
|
||||||
fprintf(stderr, "FATAL: %s\n", localMsg.constData());
|
std::cerr << "FATAL: " << stdMsg << "\n";
|
||||||
notifications::notify("KShare Fatal Error", msg, QSystemTrayIcon::Critical);
|
notifications::notify("KShare Fatal Error", msg, QSystemTrayIcon::Critical);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -57,6 +59,7 @@ int main(int argc, char *argv[]) {
|
|||||||
verbose = parser.isSet(v);
|
verbose = parser.isSet(v);
|
||||||
|
|
||||||
MainWindow w;
|
MainWindow w;
|
||||||
|
Worker::init();
|
||||||
if (!parser.isSet(h)) w.show();
|
if (!parser.isSet(h)) w.show();
|
||||||
return a.exec();
|
return a.exec();
|
||||||
}
|
}
|
||||||
|
54
worker/worker.cpp
Normal file
54
worker/worker.cpp
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#include "worker.hpp"
|
||||||
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
Worker *Worker::inst = 0;
|
||||||
|
QMutex Worker::workerLock;
|
||||||
|
|
||||||
|
void Worker::queue(WorkerContext *context) {
|
||||||
|
QMutexLocker ml(&lock);
|
||||||
|
qqueue.enqueue(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Worker::init() {
|
||||||
|
QMutexLocker ml(&workerLock);
|
||||||
|
if (!inst) inst = new Worker;
|
||||||
|
}
|
||||||
|
|
||||||
|
Worker::Worker() : QObject() {
|
||||||
|
thr = new QThread;
|
||||||
|
moveToThread(thr);
|
||||||
|
connect(thr, &QThread::started, this, &Worker::process);
|
||||||
|
connect(thr, &QThread::finished, thr, &QThread::deleteLater);
|
||||||
|
connect(this, &Worker::finished, thr, &QThread::quit);
|
||||||
|
connect(this, &Worker::finished, this, &Worker::deleteLater);
|
||||||
|
connect(thr, &QThread::finished, thr, &QThread::deleteLater);
|
||||||
|
thr->start();
|
||||||
|
}
|
||||||
|
|
||||||
|
Worker::~Worker() {
|
||||||
|
end();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Worker::end() {
|
||||||
|
QMutexLocker ml(&endLock);
|
||||||
|
_ended = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Worker::ended() {
|
||||||
|
QMutexLocker ml(&endLock);
|
||||||
|
return _ended;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Worker::process() {
|
||||||
|
while (!ended()) {
|
||||||
|
lock.lock();
|
||||||
|
if (!qqueue.isEmpty()) {
|
||||||
|
WorkerContext *c = qqueue.dequeue();
|
||||||
|
c->consumer(c->pixmap.toImage().convertToFormat(c->targetFormat));
|
||||||
|
}
|
||||||
|
lock.unlock();
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // STL likes it's scopes
|
||||||
|
}
|
||||||
|
emit finished();
|
||||||
|
}
|
47
worker/worker.hpp
Normal file
47
worker/worker.hpp
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
#ifndef WORKER_HPP
|
||||||
|
#define WORKER_HPP
|
||||||
|
|
||||||
|
#include <QImage>
|
||||||
|
#include <QMutex>
|
||||||
|
#include <QObject>
|
||||||
|
#include <QPixmap>
|
||||||
|
#include <QQueue>
|
||||||
|
#include <QThread>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
struct WorkerContext {
|
||||||
|
QPixmap pixmap;
|
||||||
|
QImage::Format targetFormat;
|
||||||
|
std::function<void(QImage)> consumer;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Worker : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
void queue(WorkerContext *context);
|
||||||
|
static void init();
|
||||||
|
|
||||||
|
private:
|
||||||
|
Worker();
|
||||||
|
~Worker();
|
||||||
|
QMutex lock;
|
||||||
|
QMutex endLock;
|
||||||
|
QThread *thr;
|
||||||
|
QQueue<WorkerContext *> qqueue; // Say that ten times as fast
|
||||||
|
bool _ended;
|
||||||
|
|
||||||
|
void _queue(WorkerContext *context);
|
||||||
|
void end();
|
||||||
|
bool ended();
|
||||||
|
|
||||||
|
static Worker *inst;
|
||||||
|
static QMutex workerLock;
|
||||||
|
signals:
|
||||||
|
void error(QString err);
|
||||||
|
void finished();
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void process();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // WORKER_HPP
|
Loading…
Reference in New Issue
Block a user