2012-01-06 16:09:03 +00:00
|
|
|
#include "dialogrender.h"
|
|
|
|
|
2012-06-15 09:31:11 +00:00
|
|
|
#include <math.h>
|
2012-01-06 16:09:03 +00:00
|
|
|
#include <QVBoxLayout>
|
|
|
|
#include <QImage>
|
|
|
|
#include <QColor>
|
|
|
|
#include <QPainter>
|
2012-06-15 09:31:11 +00:00
|
|
|
#include <QMessageBox>
|
2012-01-29 21:45:58 +00:00
|
|
|
#include "tools.h"
|
2012-01-06 16:09:03 +00:00
|
|
|
|
2012-01-24 13:16:20 +00:00
|
|
|
#include "../lib_paysages/scenery.h"
|
|
|
|
#include "../lib_paysages/auto.h"
|
2012-01-06 16:09:03 +00:00
|
|
|
|
|
|
|
static DialogRender* _current_dialog;
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
static void _renderStart(int width, int height, Color background)
|
2012-01-06 16:09:03 +00:00
|
|
|
{
|
2012-04-02 19:38:59 +00:00
|
|
|
delete _current_dialog->pixbuf;
|
|
|
|
_current_dialog->pixbuf = new QImage(width, height, QImage::Format_ARGB32);
|
2012-01-29 21:45:58 +00:00
|
|
|
_current_dialog->pixbuf->fill(colorToQColor(background).rgb());
|
2012-06-13 20:38:01 +00:00
|
|
|
_current_dialog->tellRenderSize(width, height);
|
2012-01-06 16:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _renderDraw(int x, int y, Color col)
|
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
_current_dialog->pixbuf->setPixel(x, _current_dialog->pixbuf->height() - 1 - y, colorToQColor(col).rgb());
|
2012-01-06 16:09:03 +00:00
|
|
|
}
|
|
|
|
|
2012-06-15 09:31:11 +00:00
|
|
|
static void _renderUpdate(float progress)
|
2012-01-06 16:09:03 +00:00
|
|
|
{
|
|
|
|
_current_dialog->area->update();
|
2012-06-15 09:31:11 +00:00
|
|
|
_current_dialog->tellProgressChange(progress);
|
2012-01-06 16:09:03 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
class RenderThread:public QThread
|
|
|
|
{
|
|
|
|
public:
|
2012-06-13 15:38:11 +00:00
|
|
|
RenderThread(Renderer* renderer, RenderParams params):QThread()
|
2012-01-29 21:45:58 +00:00
|
|
|
{
|
|
|
|
_renderer = renderer;
|
2012-06-13 15:38:11 +00:00
|
|
|
_params = params;
|
2012-01-29 21:45:58 +00:00
|
|
|
}
|
|
|
|
void run()
|
|
|
|
{
|
2012-06-13 15:38:11 +00:00
|
|
|
rendererStart(_renderer, _params);
|
2012-01-29 21:45:58 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
Renderer* _renderer;
|
2012-06-13 15:38:11 +00:00
|
|
|
RenderParams _params;
|
2012-01-29 21:45:58 +00:00
|
|
|
};
|
|
|
|
|
2012-01-06 16:09:03 +00:00
|
|
|
class RenderArea:public QWidget
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RenderArea(QWidget* parent):
|
|
|
|
QWidget(parent)
|
|
|
|
{
|
|
|
|
setMinimumSize(800, 600);
|
|
|
|
}
|
|
|
|
|
|
|
|
void paintEvent(QPaintEvent* event)
|
|
|
|
{
|
|
|
|
QPainter painter(this);
|
|
|
|
painter.drawImage(0, 0, *_current_dialog->pixbuf);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-04-02 19:38:59 +00:00
|
|
|
DialogRender::DialogRender(QWidget *parent, Renderer* renderer):
|
2012-01-26 18:20:19 +00:00
|
|
|
QDialog(parent, Qt::WindowTitleHint | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint)
|
2012-01-06 16:09:03 +00:00
|
|
|
{
|
|
|
|
pixbuf = new QImage(1, 1, QImage::Format_ARGB32);
|
|
|
|
_current_dialog = this;
|
2012-06-15 09:31:11 +00:00
|
|
|
_render_thread = NULL;
|
2012-04-02 19:38:59 +00:00
|
|
|
_renderer = renderer;
|
2012-06-13 20:38:01 +00:00
|
|
|
|
2012-01-06 16:09:03 +00:00
|
|
|
setModal(true);
|
2012-02-28 13:45:11 +00:00
|
|
|
setWindowTitle(tr("Paysages 3D - Render"));
|
2012-01-06 16:09:03 +00:00
|
|
|
setLayout(new QVBoxLayout());
|
|
|
|
|
2012-06-15 09:31:11 +00:00
|
|
|
_scroll = new QScrollArea(this);
|
|
|
|
_scroll->setAlignment(Qt::AlignCenter);
|
|
|
|
area = new RenderArea(_scroll);
|
|
|
|
_scroll->setWidget(area);
|
|
|
|
layout()->addWidget(_scroll);
|
2012-04-11 19:59:50 +00:00
|
|
|
|
2012-06-15 09:31:11 +00:00
|
|
|
_info = new QWidget(this);
|
|
|
|
_info->setLayout(new QHBoxLayout());
|
|
|
|
layout()->addWidget(_info);
|
|
|
|
|
|
|
|
_timer = new QLabel(QString("0:00.00"), _info);
|
|
|
|
_info->layout()->addWidget(_timer);
|
|
|
|
|
|
|
|
_progress = new QProgressBar(_info);
|
|
|
|
_progress->setMaximumHeight(12);
|
|
|
|
_progress->setMinimum(0);
|
|
|
|
_progress->setMaximum(1000);
|
|
|
|
_progress->setValue(0);
|
|
|
|
_info->layout()->addWidget(_progress);
|
2012-06-13 20:38:01 +00:00
|
|
|
|
2012-06-15 09:31:11 +00:00
|
|
|
connect(this, SIGNAL(renderSizeChanged(int, int)), this, SLOT(applyRenderSize(int, int)));
|
|
|
|
connect(this, SIGNAL(progressChanged(float)), this, SLOT(applyProgress(float)));
|
2012-01-18 16:20:14 +00:00
|
|
|
}
|
2012-01-06 16:09:03 +00:00
|
|
|
|
2012-01-25 17:31:36 +00:00
|
|
|
DialogRender::~DialogRender()
|
2012-01-18 16:20:14 +00:00
|
|
|
{
|
2012-06-15 09:31:11 +00:00
|
|
|
if (_render_thread)
|
2012-01-25 17:31:36 +00:00
|
|
|
{
|
2012-04-02 19:38:59 +00:00
|
|
|
rendererInterrupt(_renderer);
|
2012-06-15 09:31:11 +00:00
|
|
|
_render_thread->wait();
|
2012-01-25 17:31:36 +00:00
|
|
|
|
2012-06-15 09:31:11 +00:00
|
|
|
delete _render_thread;
|
2012-01-25 17:31:36 +00:00
|
|
|
}
|
|
|
|
delete pixbuf;
|
|
|
|
}
|
|
|
|
|
2012-06-13 20:38:01 +00:00
|
|
|
void DialogRender::tellRenderSize(int width, int height)
|
|
|
|
{
|
|
|
|
emit renderSizeChanged(width, height);
|
|
|
|
}
|
|
|
|
|
2012-06-15 09:31:11 +00:00
|
|
|
void DialogRender::tellProgressChange(float value)
|
|
|
|
{
|
|
|
|
emit progressChanged(value);
|
|
|
|
}
|
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
void DialogRender::startRender(RenderParams params)
|
2012-01-25 17:31:36 +00:00
|
|
|
{
|
2012-06-15 09:31:11 +00:00
|
|
|
_started = time(NULL);
|
|
|
|
|
2012-06-13 20:38:01 +00:00
|
|
|
//applyRenderSize(params.width, params.height);
|
2012-04-02 19:38:59 +00:00
|
|
|
rendererSetPreviewCallbacks(_renderer, _renderStart, _renderDraw, _renderUpdate);
|
2012-04-02 19:10:34 +00:00
|
|
|
|
2012-06-15 09:31:11 +00:00
|
|
|
_render_thread = new RenderThread(_renderer, params);
|
|
|
|
_render_thread->start();
|
2012-01-24 13:16:20 +00:00
|
|
|
|
2012-01-18 16:20:14 +00:00
|
|
|
exec();
|
2012-01-06 16:09:03 +00:00
|
|
|
}
|
2012-01-10 20:51:27 +00:00
|
|
|
|
2012-01-18 16:20:14 +00:00
|
|
|
void DialogRender::loadLastRender()
|
2012-01-10 20:51:27 +00:00
|
|
|
{
|
2012-06-13 20:38:01 +00:00
|
|
|
//applyRenderSize(_renderer->render_width, _renderer->render_height);
|
2012-06-15 09:31:11 +00:00
|
|
|
_info->hide();
|
2012-04-02 19:38:59 +00:00
|
|
|
rendererSetPreviewCallbacks(_renderer, _renderStart, _renderDraw, _renderUpdate);
|
2012-01-24 13:16:20 +00:00
|
|
|
|
2012-01-18 16:20:14 +00:00
|
|
|
exec();
|
2012-01-10 20:51:27 +00:00
|
|
|
}
|
2012-04-02 19:38:59 +00:00
|
|
|
|
|
|
|
void DialogRender::applyRenderSize(int width, int height)
|
|
|
|
{
|
|
|
|
area->setMinimumSize(width, height);
|
|
|
|
area->setMaximumSize(width, height);
|
|
|
|
area->resize(width, height);
|
2012-06-15 09:31:11 +00:00
|
|
|
_scroll->setMinimumSize(width > 800 ? 820 : width + 20, height > 600 ? 620 : height + 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogRender::applyProgress(float value)
|
|
|
|
{
|
|
|
|
double diff = difftime(time(NULL), _started);
|
|
|
|
int hours = (int)floor(diff / 3600.0);
|
|
|
|
int minutes = (int)floor((diff - 3600.0 * hours) / 60.0);
|
|
|
|
int seconds = (int)floor(diff - 3600.0 * hours - 60.0 * minutes);
|
|
|
|
_timer->setText(tr("%1:%2.%3").arg(hours).arg(minutes, 2, 10, QLatin1Char('0')).arg(seconds, 2, 10, QLatin1Char('0')));
|
|
|
|
_progress->setValue((int)(value * 1000.0));
|
|
|
|
_progress->update();
|
2012-04-02 19:38:59 +00:00
|
|
|
}
|
2012-06-15 09:31:11 +00:00
|
|
|
|