Page MenuHomec4science

helper.cpp
No OneTemporary

File Metadata

Created
Tue, Aug 20, 04:13

helper.cpp

#include "helper.hh"
#include <QBrush>
#include <QColor>
#include <QDebug>
#include <QImage>
#include <QPaintEvent>
#include <QPainter>
#include <QWidget>
#include <QPoint>
#include <chrono>
#include <unistd.h>
//#define DELAY 50000us
Helper::Helper(int cell_size) : cell_size(cell_size) {
border_pen = QPen(Qt::black);
border_pen.setWidth(1);
colors.push_back(QColor("darkslateblue"));
colors.push_back(Qt::white);
colors.push_back(Qt::cyan);
colors.push_back(Qt::green);
colors.push_back(Qt::red);
colors.push_back(Qt::yellow);
colors.push_back(Qt::blue);
colors.push_back(Qt::magenta);
colors.push_back(QColor("orange"));
qDebug() << "Defined" << colors.size() << "colors";
for(int t = 0; t < 8; ++t) {
for(size_t i = 0; i < colors.size(); ++i) {
cell_brushes.push_back(QBrush(colors[i].lighter(20+20*t)));
}
}
}
void Helper::sendGrid(const QPoint & pos, const QSize & size) {
if(grid.size(0) == 0 || grid.size(1) == 0) return;
int origin[2] = {pos.y() * int(grid.size(0)) / size.height(),
pos.x() * int(grid.size(1)) / size.width()};
qDebug() << "CLICK " << pos << origin[0]<< "-" << origin[1];
int ssize[2] = {3,3};
SendGrid sgrid(origin, ssize);
sgrid(1, 0) = 1;
sgrid(2, 1) = 1;
sgrid(0, 2) = 1;
sgrid(1, 2) = 1;
sgrid(2, 2) = 1;
grid.dataToSend(sgrid);
}
void Helper::paint(QPainter * painter, QPaintEvent * event,
__attribute__((unused)) int elapsed) {
#if defined(DELAY)
static auto last_call = std::chrono::high_resolution_clock::now();
auto now = std::chrono::high_resolution_clock::now();
using namespace std::chrono_literals;
std::chrono::duration<double, std::micro> diff = now - last_call;
if (diff < DELAY) {
qDebug() << "wait" << (100ms - diff).count() << "us";
usleep((DELAY - diff).count());
}
#endif
int tids_perm[8] = {3, 6, 2, 4, 1, 7, 0, 5};
qDebug() << "Drawing image" << grid.size(1) << "x" << grid.size(0);
painter->fillRect(event->rect(), cell_brushes[0]);
if (grid.size(1) != 0 && grid.size(0) != 0) {
// QImage bitmap(5 * grid.size(1) + 1, 5 * grid.size(0) + 1,
// QImage::Format_RGB32);
QImage bitmap(cell_size * grid.size(1), cell_size * grid.size(0), QImage::Format_RGB32);
QPainter painting(&bitmap);
// painting.setPen(border_pen);
for (size_t i = 0; i < grid.size(0); ++i) {
for (size_t j = 0; j < grid.size(1); ++j) {
int val = grid(i,j);
int pid = val & 0x0F;
int tid = tids_perm[(val >> 4) % 8];
int bid = pid + tid * colors.size();
painting.setBrush(cell_brushes[bid]);
painting.drawRect(cell_size * j, cell_size * i, cell_size, cell_size);
}
}
painter->drawImage(event->rect(), bitmap, bitmap.rect());
}
#if defined(DELAY)
last_call = std::chrono::system_clock::now();
#endif
grid.releasePieces();
qDebug() << "Image drawn";
}

Event Timeline