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
2017-11-05 02:54:12 +01:00
# define LC_POVRAY_PREVIEW_WIDTH 768
# define LC_POVRAY_PREVIEW_HEIGHT 432
2017-09-22 19:08:02 +02:00
lcRenderDialog : : lcRenderDialog ( QWidget * Parent )
: QDialog ( Parent ) ,
ui ( new Ui : : lcRenderDialog )
{
2017-12-07 07:08:56 +01:00
# ifndef QT_NO_PROCESS
2017-09-22 19:08:02 +02:00
mProcess = nullptr ;
2017-12-07 07:08:56 +01:00
# endif
2017-11-05 02:54:12 +01:00
mSharedMemory . setNativeKey ( " leocad-povray " ) ;
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-11-05 02:54:12 +01:00
QImage Image ( LC_POVRAY_PREVIEW_WIDTH , LC_POVRAY_PREVIEW_HEIGHT , QImage : : Format_RGB32 ) ;
Image . fill ( QColor ( 255 , 255 , 255 ) ) ;
ui - > label - > setPixmap ( QPixmap : : fromImage ( Image ) ) ;
2017-09-22 19:08:02 +02:00
connect ( & mUpdateTimer , SIGNAL ( timeout ( ) ) , this , SLOT ( Update ( ) ) ) ;
2017-12-10 01:27:56 +01:00
mUpdateTimer . start ( 500 ) ;
2017-09-22 19:08:02 +02:00
}
lcRenderDialog : : ~ lcRenderDialog ( )
{
2017-11-05 02:54:12 +01:00
mSharedMemory . detach ( ) ;
2017-09-22 19:08:02 +02:00
delete ui ;
}
2017-11-03 01:35:12 +01:00
QString lcRenderDialog : : GetPOVFileName ( ) const
{
return QDir ( QDir : : tempPath ( ) ) . absoluteFilePath ( " leocad-render.pov " ) ;
}
2017-11-05 02:54:12 +01:00
void lcRenderDialog : : CloseProcess ( )
2017-11-04 00:01:30 +01:00
{
2017-12-07 07:08:56 +01:00
# ifndef QT_NO_PROCESS
2017-11-05 02:54:12 +01:00
delete mProcess ;
mProcess = nullptr ;
2017-12-07 07:08:56 +01:00
# endif
2017-11-05 02:54:12 +01:00
QFile : : remove ( GetPOVFileName ( ) ) ;
ui - > RenderButton - > setText ( tr ( " Render " ) ) ;
2017-11-04 00:01:30 +01:00
}
2017-11-05 02:54:12 +01:00
bool lcRenderDialog : : PromptCancel ( )
2017-09-22 19:08:02 +02:00
{
2017-12-07 07:08:56 +01:00
# ifndef QT_NO_PROCESS
2017-09-22 19:08:02 +02:00
if ( mProcess )
{
2017-11-05 02:54:12 +01:00
if ( QMessageBox : : question ( this , tr ( " Cancel Render " ) , tr ( " Are you sure you want to cancel the current render? " ) , QMessageBox : : Yes | QMessageBox : : No ) = = QMessageBox : : Yes )
{
if ( mProcess )
{
mProcess - > kill ( ) ;
CloseProcess ( ) ;
}
}
else
return false ;
2017-09-22 19:08:02 +02:00
}
2017-12-07 07:08:56 +01:00
# endif
2017-11-05 02:54:12 +01:00
return true ;
}
void lcRenderDialog : : reject ( )
{
if ( PromptCancel ( ) )
QDialog : : reject ( ) ;
}
void lcRenderDialog : : on_RenderButton_clicked ( )
{
2017-12-07 07:08:56 +01:00
# ifndef QT_NO_PROCESS
2017-12-10 01:27:56 +01:00
if ( mProcess )
{
PromptCancel ( ) ;
2017-11-05 02:54:12 +01:00
return ;
2017-12-10 01:27:56 +01:00
}
2017-11-05 02:54:12 +01:00
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-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-11-05 02:54:12 +01:00
Arguments . append ( " -O- " ) ;
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 ) ) ;
}
*/
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 ( " -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
2017-11-05 02:54:12 +01:00
if ( mProcess - > waitForStarted ( ) )
ui - > RenderButton - > setText ( tr ( " Cancel " ) ) ;
else
2017-11-03 01:35:12 +01:00
{
QMessageBox : : warning ( this , tr ( " Error " ) , tr ( " Error starting POV-Ray. " ) ) ;
2017-11-05 02:54:12 +01:00
CloseProcess ( ) ;
2017-11-03 01:35:12 +01:00
}
2017-12-07 07:08:56 +01:00
# endif
2017-09-22 19:08:02 +02:00
}
void lcRenderDialog : : Update ( )
{
2017-12-07 07:08:56 +01:00
# ifndef QT_NO_PROCESS
2017-12-10 01:27:56 +01:00
if ( ! mProcess )
return ;
if ( mProcess - > state ( ) = = QProcess : : NotRunning )
2017-09-22 19:08:02 +02:00
{
2017-12-10 01:27:56 +01:00
// QString Output = mProcess->readAllStandardError();
// QMessageBox::information(this, "LeoCAD", Output);
2017-11-03 01:35:12 +01:00
# ifdef Q_OS_LINUX
2017-12-10 01:27:56 +01:00
QByteArray Output = mProcess - > readAllStandardOutput ( ) ;
QImage Image = QImage : : fromData ( Output ) ;
ui - > label - > setPixmap ( QPixmap : : fromImage ( Image . scaled ( LC_POVRAY_PREVIEW_WIDTH , LC_POVRAY_PREVIEW_HEIGHT , Qt : : KeepAspectRatio , Qt : : SmoothTransformation ) ) ) ;
2017-11-03 01:35:12 +01:00
# endif
2017-12-10 01:27:56 +01:00
CloseProcess ( ) ;
2017-09-22 19:08:02 +02:00
}
2017-12-07 07:08:56 +01:00
# endif
2017-11-03 01:35:12 +01:00
2017-09-22 23:04:50 +02:00
# ifdef Q_OS_WIN
2017-11-05 02:54:12 +01:00
if ( ! mSharedMemory . isAttached ( ) & & ! mSharedMemory . attach ( ) )
return ;
2017-09-22 19:08:02 +02:00
2017-11-05 02:54:12 +01:00
void * Buffer = mSharedMemory . data ( ) ;
2017-09-22 19:08:02 +02:00
2017-11-05 02:54:12 +01:00
if ( ! Buffer )
{
mSharedMemory . detach ( ) ;
2017-09-22 19:08:02 +02:00
return ;
2017-11-05 02:54:12 +01:00
}
2017-09-22 19:08:02 +02:00
struct lcSharedMemoryHeader
{
2017-12-02 21:22:04 +01:00
quint32 Version ;
quint32 Width ;
quint32 Height ;
quint32 PixelsWritten ;
quint32 PixelsRead ;
2017-09-22 19:08:02 +02:00
} ;
2017-11-05 02:54:12 +01:00
lcSharedMemoryHeader * Header = ( lcSharedMemoryHeader * ) Buffer ;
2017-09-22 19:08:02 +02:00
2017-12-10 01:27:56 +01:00
if ( Header - > PixelsWritten = = Header - > PixelsRead )
return ;
2017-09-22 19:08:02 +02:00
int Width = Header - > Width ;
int Height = Header - > Height ;
2017-12-10 01:27:56 +01:00
int PixelsWritten = Header - > PixelsWritten ;
2017-09-22 19:08:02 +02:00
2017-12-10 01:27:56 +01:00
if ( ! Header - > PixelsRead )
mImage = QImage ( Width , Height , QImage : : Format_ARGB32 ) ;
2017-09-22 19:08:02 +02:00
2017-12-02 21:22:04 +01:00
quint8 * Pixels = ( quint8 * ) ( Header + 1 ) ;
2017-09-22 19:08:02 +02:00
for ( int y = 0 ; y < Height ; y + + )
{
for ( int x = 0 ; x < Width ; x + + )
{
2017-12-21 03:18:22 +01:00
mImage . setPixel ( x , y , qRgba ( Pixels [ 0 ] , Pixels [ 1 ] , Pixels [ 2 ] , Pixels [ 3 ] ) ) ;
2017-09-22 19:08:02 +02:00
Pixels + = 4 ;
}
}
2017-12-10 01:27:56 +01:00
Header - > PixelsRead = PixelsWritten ;
2017-09-22 19:08:02 +02:00
2017-12-10 01:27:56 +01:00
ui - > label - > setPixmap ( QPixmap : : fromImage ( mImage . scaled ( LC_POVRAY_PREVIEW_WIDTH , LC_POVRAY_PREVIEW_HEIGHT , Qt : : KeepAspectRatio , Qt : : SmoothTransformation ) ) ) ;
2017-09-22 23:04:50 +02:00
# endif
2017-09-22 19:08:02 +02:00
}