Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ------- worker.h ------------
- #ifndef WORKER_H
- #define WORKER_H
- #include <QObject>
- #include "CL/clmain.h"
- class Worker : public QObject
- {
- Q_OBJECT
- public:
- explicit Worker(QObject *parent = nullptr);
- protected:
- CLMain clMain;
- signals:
- void error(const std::string msg);
- public slots:
- void run();
- };
- #endif // WORKER_H
- // ----- worker.cpp -----------
- #include "worker.h"
- #include <iostream>
- Worker::Worker(QObject *parent) : QObject(parent) {}
- void Worker::run() {
- std::cout << "run()\n" << std::flush;
- try {
- clMain.getDevice();
- clMain.loadProgram();
- std::cout << "emit OK\n" << std::flush;
- emit error(std::string("OK"));
- } catch (std::string msg) {
- std::cout << "emit " << msg << "\n" << std::flush;
- emit error(msg);
- }
- }
- // ------------ mywidget.h ----------------
- #ifndef MYWIDGET_H
- #define MYWIDGET_H
- #include <QWidget>
- #include <QTimer>
- #include <QImage>
- #include <QThread>
- #include <cmath>
- #include "worker.h"
- class MyWidget : public QWidget
- {
- Q_OBJECT
- public:
- explicit MyWidget(QWidget *parent = nullptr);
- virtual ~MyWidget();
- virtual void paintEvent(QPaintEvent*);
- signals:
- void start();
- public slots:
- void clError(std::string msg);
- private:
- uint32_t getOffs(float x, float y) {
- return round(x) + round(y) * width();
- }
- float ang = 0;
- Worker worker;
- QThread workerThread;
- QTimer * timer;
- QImage * img;
- uint32_t * buffer = nullptr;
- void updateImage();
- };
- #endif // MYWIDGET_H
- // ------------- mywidget.cpp -------------
- #include "mywidget.h"
- #include <QPainter>
- #include <QMessageBox>
- #include <iostream>
- MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
- {
- std::cout << "ctor\n" << std::flush;
- timer = new QTimer(this); // destruction is made by the Qt framework
- connect(timer, SIGNAL(timeout()), this, SLOT(repaint()));
- timer->start(50);
- img = nullptr;
- worker.moveToThread(&workerThread);
- connect(&worker, SIGNAL(error()), this, SLOT(clError()));
- // connect(&worker, &Worker::error, this, &MyWidget::clError);
- connect(this, SIGNAL(start()), &worker, SLOT(run()));
- workerThread.start();
- emit start();
- }
- MyWidget::~MyWidget() {
- workerThread.quit();
- workerThread.wait();
- delete img;
- delete[] buffer;
- }
- void MyWidget::paintEvent(QPaintEvent *e) {
- updateImage();
- QPainter p(this);
- p.drawImage(QPointF(0, 0), *img);
- }
- void MyWidget::updateImage() {
- if (img == nullptr) {
- int w = width(); int h = height();
- buffer = new uint32_t[w * h];
- for(int i = 0; i < w * h; i++) {
- buffer[i] = 0xff000000;
- }
- img = new QImage((unsigned char *)buffer, w, h, QImage::Format_RGB32);
- }
- uint32_t * data = (uint32_t*)(uchar *)img->bits();
- float cx = 0.5f * width();
- float cy = 0.5f * height();
- float pAng = ang - 0.25 * M_PI;
- data[getOffs(cx + 100 * cos(ang), cy + 100 * sin(ang))] = 0xff8080;
- data[getOffs(cx + 100 * cos(pAng), cy + 100 * sin(pAng))] = 0x0;
- ang += 0.01 * M_PI;
- if (ang > 2 * M_PI) {
- ang -= 2 * M_PI;
- }
- }
- void MyWidget::clError(const std::string msg) {
- std::cout << "clError('" << msg << "')\n" << std::flush;
- QMessageBox::critical(this, QString("OpenCL error"), QString(msg.c_str()));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement