leocad/qt/lc_renderdialog.cpp

214 lines
4.5 KiB
C++
Raw Normal View History

2017-09-22 19:08:02 +02:00
#include "lc_global.h"
#include "lc_renderdialog.h"
#include "ui_lc_renderdialog.h"
#include "project.h"
#include "lc_application.h"
2017-11-03 01:35:12 +01:00
#include "lc_profile.h"
2017-09-22 19:08:02 +02:00
lcRenderDialog::lcRenderDialog(QWidget* Parent)
: QDialog(Parent),
ui(new Ui::lcRenderDialog)
{
mProcess = nullptr;
2017-09-22 23:04:50 +02:00
#ifdef Q_OS_WIN
2017-09-22 19:08:02 +02:00
mMapFile = NULL;
mBuffer = nullptr;
2017-09-22 23:04:50 +02:00
#endif
2017-09-22 19:08:02 +02:00
ui->setupUi(this);
2017-11-04 00:01:30 +01:00
ui->WidthEdit->setText(QString::number(lcGetProfileInt(LC_PROFILE_POVRAY_WIDTH)));
ui->WidthEdit->setValidator(new QIntValidator(16, INT_MAX));
ui->HeightEdit->setText(QString::number(lcGetProfileInt(LC_PROFILE_POVRAY_HEIGHT)));
ui->HeightEdit->setValidator(new QIntValidator(16, INT_MAX));
2017-09-22 19:08:02 +02:00
connect(&mUpdateTimer, SIGNAL(timeout()), this, SLOT(Update()));
mUpdateTimer.start(100);
}
lcRenderDialog::~lcRenderDialog()
{
CloseSharedMemory();
delete ui;
}
2017-11-03 01:35:12 +01:00
QString lcRenderDialog::GetPOVFileName() const
{
return QDir(QDir::tempPath()).absoluteFilePath("leocad-render.pov");
}
2017-11-04 00:01:30 +01:00
/*
void lcRenderDialog::reject()
{
// todo: cancel
}
*/
2017-09-22 19:08:02 +02:00
void lcRenderDialog::on_RenderButton_clicked()
{
if (mProcess)
{
// todo: cancel
return;
}
2017-11-03 01:35:12 +01:00
QString FileName = GetPOVFileName();
2017-09-22 19:08:02 +02:00
if (!lcGetActiveProject()->ExportPOVRay(FileName))
return;
QStringList Arguments;
Arguments.append(QString::fromLatin1("+I%1").arg(FileName));
2017-11-03 01:35:12 +01:00
// Arguments.append("+OC:\\Users\\leo\\Projects\\leocad\\test.png");
2017-11-04 00:01:30 +01:00
Arguments.append(QString::fromLatin1("+W%1").arg(ui->WidthEdit->text()));
Arguments.append(QString::fromLatin1("+H%1").arg(ui->HeightEdit->text()));
2017-09-22 19:08:02 +02:00
Arguments.append("+Q11");
Arguments.append("+R3");
Arguments.append("+A0.1");
Arguments.append("+J0.5");
/*
if (!LGEOPath.isEmpty())
{
Arguments.append(QString::fromLatin1("+L%1lg/").arg(LGEOPath));
Arguments.append(QString::fromLatin1("+L%1ar/").arg(LGEOPath));
}
Arguments.append(QString::fromLatin1("/EXIT"));
*/
2017-11-03 01:35:12 +01:00
QString POVRayPath;
#ifdef Q_OS_WIN
POVRayPath = QDir::cleanPath(QCoreApplication::applicationDirPath() + QLatin1String("/povray/povconsole32-sse2.exe"));
#endif
#ifdef Q_OS_LINUX
POVRayPath = lcGetProfileString(LC_PROFILE_POVRAY_PATH);
Arguments.append("+FC");
Arguments.append("-O-");
Arguments.append("-D");
#endif
#ifdef Q_OS_MACOS
POVRayPath = QDir::cleanPath(QCoreApplication::applicationDirPath() + QLatin1String("/povray/povconsole"));
#endif
2017-09-22 19:08:02 +02:00
mProcess = new QProcess(this);
mProcess->start(POVRayPath, Arguments);
2017-11-03 01:35:12 +01:00
if (!mProcess->waitForStarted())
{
QMessageBox::warning(this, tr("Error"), tr("Error starting POV-Ray."));
delete mProcess;
mProcess = nullptr;
QFile::remove(FileName);
}
2017-11-04 00:01:30 +01:00
else
ui->RenderButton->setText(tr("Cancel"));
2017-09-22 19:08:02 +02:00
}
void lcRenderDialog::Update()
{
if (mProcess)
{
if (mProcess->state() == QProcess::NotRunning)
{
2017-11-03 01:35:12 +01:00
// QString Output = mProcess->readAllStandardError();
// QMessageBox::information(this, "LeoCAD", Output);
#ifdef Q_OS_LINUX
QByteArray Output = mProcess->readAllStandardOutput();
QImage Image = QImage::fromData(Output);
2017-11-04 00:01:30 +01:00
ui->label->setPixmap(QPixmap::fromImage(Image.scaled(768, 432, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
2017-11-03 01:35:12 +01:00
#endif
2017-09-22 19:08:02 +02:00
delete mProcess;
mProcess = nullptr;
2017-11-03 01:35:12 +01:00
QFile::remove(GetPOVFileName());
2017-11-04 00:01:30 +01:00
ui->RenderButton->setText(tr("Render"));
2017-09-22 19:08:02 +02:00
}
}
2017-11-03 01:35:12 +01:00
2017-09-22 23:04:50 +02:00
#ifdef Q_OS_WIN
2017-09-22 19:08:02 +02:00
if (mMapFile == NULL)
{
mMapFile = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, "leocad-povray");
if (!mMapFile)
return;
mBuffer = MapViewOfFile(mMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if (!mBuffer)
{
CloseHandle(mMapFile);
mMapFile = NULL;
return;
}
uint32_t* Header = (uint32_t*)mBuffer;
if (Header[0] != 1)
{
CloseSharedMemory();
return;
}
}
if (!mBuffer)
return;
struct lcSharedMemoryHeader
{
uint32_t Version;
uint32_t Width;
uint32_t Height;
uint32_t PixelsWritten;
uint32_t PixelsRead;
};
lcSharedMemoryHeader* Header = (lcSharedMemoryHeader*)mBuffer;
int Width = Header->Width;
int Height = Header->Height;
int PixelsRead = Header->PixelsWritten;
// if (width != expected) ...
QImage Image(Width, Height, QImage::Format_ARGB32);
uint8_t* Pixels = (uint8_t*)(Header + 1);
for (int y = 0; y < Height; y++)
{
for (int x = 0; x < Width; x++)
{
Image.setPixelColor(x, y, QColor::fromRgb(Pixels[0], Pixels[1], Pixels[2], Pixels[3]));
Pixels += 4;
}
}
Header->PixelsRead = PixelsRead;
ui->label->setPixmap(QPixmap::fromImage(Image));
2017-09-22 23:04:50 +02:00
#endif
2017-09-22 19:08:02 +02:00
}
void lcRenderDialog::CloseSharedMemory()
{
2017-09-22 23:04:50 +02:00
#ifdef Q_OS_WIN
2017-09-22 19:08:02 +02:00
if (mBuffer)
{
UnmapViewOfFile(mBuffer);
mBuffer = NULL;
}
if (mMapFile)
{
CloseHandle(mMapFile);
mMapFile = NULL;
}
2017-09-22 23:04:50 +02:00
#endif
2017-09-22 19:08:02 +02:00
}