2012-07-13 12:23:58 +00:00
# include "dialogheightmap.h"
2012-07-15 14:42:50 +00:00
# include <QLabel>
2012-07-13 12:23:58 +00:00
# include <QBoxLayout>
# include <QGridLayout>
# include <QPushButton>
2012-07-16 15:48:05 +00:00
# include <QComboBox>
2012-07-13 12:23:58 +00:00
# include <QSlider>
2012-08-08 13:30:40 +00:00
# include <QFileDialog>
2012-08-08 14:25:01 +00:00
# include <QInputDialog>
2012-07-13 21:24:19 +00:00
# include <math.h>
2012-08-08 13:30:40 +00:00
# include "../lib_paysages/scenery.h"
2012-07-13 21:24:19 +00:00
# include "widgetheightmap.h"
2012-07-13 12:23:58 +00:00
/**************** Dialog form ****************/
2012-12-09 17:49:28 +00:00
DialogHeightMap : : DialogHeightMap ( QWidget * parent , HeightMap * heightmap , void * canvas ) : DialogWithPreview ( parent )
2012-07-13 12:23:58 +00:00
{
QWidget * mainarea ;
QWidget * buttons ;
QWidget * panel ;
QWidget * viewer ;
QGridLayout * viewer_layout ;
2012-12-09 17:49:28 +00:00
2012-07-15 14:42:50 +00:00
QLabel * label ;
2012-07-13 12:23:58 +00:00
QSlider * slider ;
QPushButton * button ;
2012-07-16 15:48:05 +00:00
QComboBox * combobox ;
2012-12-09 17:49:28 +00:00
2012-07-13 12:23:58 +00:00
_value_original = heightmap ;
_value_modified = heightmapCreate ( ) ;
heightmapCopy ( _value_original , & _value_modified ) ;
setLayout ( new QVBoxLayout ( ) ) ;
2012-12-09 17:49:28 +00:00
2012-07-13 12:23:58 +00:00
// Dialog layout (main area + buttons)
mainarea = new QWidget ( this ) ;
mainarea - > setLayout ( new QHBoxLayout ( ) ) ;
this - > layout ( ) - > addWidget ( mainarea ) ;
2012-12-09 17:49:28 +00:00
2012-07-13 12:23:58 +00:00
buttons = new QWidget ( this ) ;
buttons - > setLayout ( new QHBoxLayout ( ) ) ;
buttons - > layout ( ) - > setAlignment ( buttons , Qt : : AlignBottom ) ;
this - > layout ( ) - > addWidget ( buttons ) ;
2012-12-09 17:49:28 +00:00
2012-07-13 12:23:58 +00:00
// Main area layout (viewer + panel)
viewer = new QWidget ( mainarea ) ;
viewer_layout = new QGridLayout ( ) ;
viewer - > setLayout ( viewer_layout ) ;
mainarea - > layout ( ) - > addWidget ( viewer ) ;
2012-12-09 17:49:28 +00:00
2012-07-13 12:23:58 +00:00
panel = new QWidget ( mainarea ) ;
panel - > setLayout ( new QVBoxLayout ( ) ) ;
mainarea - > layout ( ) - > addWidget ( panel ) ;
2012-07-15 14:42:50 +00:00
mainarea - > layout ( ) - > setAlignment ( panel , Qt : : AlignTop ) ;
2012-12-09 17:49:28 +00:00
2012-07-13 12:23:58 +00:00
// Viewer layout (3d display + sliders)
_3dview = new WidgetHeightMap ( viewer , & _value_modified ) ;
viewer_layout - > addWidget ( _3dview , 0 , 0 ) ;
slider = new QSlider ( Qt : : Horizontal , viewer ) ;
2012-07-13 21:24:19 +00:00
slider - > setRange ( 0 , 1000 ) ;
connect ( slider , SIGNAL ( valueChanged ( int ) ) , this , SLOT ( angleHChanged ( int ) ) ) ;
2012-07-13 12:23:58 +00:00
viewer_layout - > addWidget ( slider , 1 , 0 ) ;
slider = new QSlider ( Qt : : Vertical , viewer ) ;
2012-07-13 21:24:19 +00:00
slider - > setRange ( - 300 , 700 ) ;
connect ( slider , SIGNAL ( valueChanged ( int ) ) , this , SLOT ( angleVChanged ( int ) ) ) ;
2012-07-13 12:23:58 +00:00
viewer_layout - > addWidget ( slider , 0 , 1 ) ;
2012-12-09 17:49:28 +00:00
2012-07-13 12:23:58 +00:00
// Panel layout
2012-08-08 13:30:40 +00:00
if ( canvas )
{
button = new QPushButton ( tr ( " Reset to terrain height " ) , panel ) ;
connect ( button , SIGNAL ( clicked ( ) ) , this , SLOT ( resetToTerrain ( ) ) ) ;
panel - > layout ( ) - > addWidget ( button ) ;
}
button = new QPushButton ( tr ( " Load from picture file " ) , panel ) ;
connect ( button , SIGNAL ( clicked ( ) ) , this , SLOT ( loadFromFile ( ) ) ) ;
2012-07-13 12:23:58 +00:00
panel - > layout ( ) - > addWidget ( button ) ;
2012-08-08 14:25:01 +00:00
button = new QPushButton ( tr ( " Change resolution " ) , panel ) ;
connect ( button , SIGNAL ( clicked ( ) ) , this , SLOT ( changeResolution ( ) ) ) ;
panel - > layout ( ) - > addWidget ( button ) ;
2012-12-09 17:49:28 +00:00
2012-08-08 14:25:01 +00:00
_resolution_label = new QLabel ( " " , panel ) ;
panel - > layout ( ) - > addWidget ( _resolution_label ) ;
updateResolutionLabel ( ) ;
2012-12-09 17:49:28 +00:00
2012-07-16 15:48:05 +00:00
combobox = new QComboBox ( panel ) ;
combobox - > addItem ( tr ( " Raise / lower " ) ) ;
2012-07-26 11:55:19 +00:00
combobox - > addItem ( tr ( " Add noise / smooth " ) ) ;
2012-07-16 15:48:05 +00:00
connect ( combobox , SIGNAL ( currentIndexChanged ( int ) ) , this , SLOT ( brushModeChanged ( int ) ) ) ;
panel - > layout ( ) - > addWidget ( combobox ) ;
2012-12-09 17:49:28 +00:00
2012-07-15 14:42:50 +00:00
label = new QLabel ( tr ( " Brush size " ) , panel ) ;
panel - > layout ( ) - > addWidget ( label ) ;
2012-12-09 17:49:28 +00:00
2012-07-15 14:42:50 +00:00
slider = new QSlider ( Qt : : Horizontal , panel ) ;
2012-07-26 11:55:19 +00:00
slider - > setRange ( 6 , 300 ) ;
2012-07-15 14:42:50 +00:00
connect ( slider , SIGNAL ( valueChanged ( int ) ) , this , SLOT ( brushSizeChanged ( int ) ) ) ;
panel - > layout ( ) - > addWidget ( slider ) ;
2012-07-26 11:55:19 +00:00
slider - > setValue ( 60 ) ;
2012-07-13 12:23:58 +00:00
2012-07-15 14:42:50 +00:00
label = new QLabel ( tr ( " Brush smoothing " ) , panel ) ;
panel - > layout ( ) - > addWidget ( label ) ;
2012-12-09 17:49:28 +00:00
2012-07-15 14:42:50 +00:00
slider = new QSlider ( Qt : : Horizontal , panel ) ;
slider - > setRange ( 0 , 1000 ) ;
connect ( slider , SIGNAL ( valueChanged ( int ) ) , this , SLOT ( brushSmoothingChanged ( int ) ) ) ;
panel - > layout ( ) - > addWidget ( slider ) ;
2012-07-16 20:34:01 +00:00
slider - > setValue ( 600 ) ;
2012-12-09 17:49:28 +00:00
2012-07-16 20:34:01 +00:00
label = new QLabel ( tr ( " Brush strength " ) , panel ) ;
panel - > layout ( ) - > addWidget ( label ) ;
2012-12-09 17:49:28 +00:00
2012-07-16 20:34:01 +00:00
slider = new QSlider ( Qt : : Horizontal , panel ) ;
slider - > setRange ( 0 , 1000 ) ;
connect ( slider , SIGNAL ( valueChanged ( int ) ) , this , SLOT ( brushStrengthChanged ( int ) ) ) ;
panel - > layout ( ) - > addWidget ( slider ) ;
2012-07-15 14:42:50 +00:00
slider - > setValue ( 200 ) ;
2012-12-09 17:49:28 +00:00
2012-07-13 12:23:58 +00:00
// Buttons layout
2012-08-13 19:19:19 +00:00
button = new QPushButton ( tr ( " Cancel " ) , buttons ) ;
button - > setIcon ( QIcon ( " images/cancel.png " ) ) ;
2012-07-13 12:23:58 +00:00
buttons - > layout ( ) - > addWidget ( button ) ;
2012-08-13 19:19:19 +00:00
QObject : : connect ( button , SIGNAL ( clicked ( ) ) , this , SLOT ( reject ( ) ) ) ;
2012-07-13 12:23:58 +00:00
button = new QPushButton ( tr ( " Revert " ) , buttons ) ;
2012-08-13 19:19:19 +00:00
button - > setIcon ( QIcon ( " images/revert.png " ) ) ;
2012-07-13 12:23:58 +00:00
buttons - > layout ( ) - > addWidget ( button ) ;
QObject : : connect ( button , SIGNAL ( clicked ( ) ) , this , SLOT ( revert ( ) ) ) ;
2012-08-13 19:19:19 +00:00
button = new QPushButton ( tr ( " Validate " ) , buttons ) ;
button - > setIcon ( QIcon ( " images/apply.png " ) ) ;
2012-07-13 12:23:58 +00:00
buttons - > layout ( ) - > addWidget ( button ) ;
2012-08-13 19:19:19 +00:00
QObject : : connect ( button , SIGNAL ( clicked ( ) ) , this , SLOT ( accept ( ) ) ) ;
2012-07-13 12:23:58 +00:00
setWindowTitle ( tr ( " Paysages 3D - Height map painting " ) ) ;
}
2012-12-09 17:49:28 +00:00
bool DialogHeightMap : : editHeightMap ( QWidget * parent , HeightMap * heightmap , void * canvas )
2012-07-13 12:23:58 +00:00
{
int result ;
2012-08-08 13:30:40 +00:00
DialogHeightMap * dialog = new DialogHeightMap ( parent , heightmap , canvas ) ;
2012-07-13 12:23:58 +00:00
result = dialog - > exec ( ) ;
delete dialog ;
return ( result ! = 0 ) ? true : false ;
}
void DialogHeightMap : : accept ( )
{
2012-07-16 20:34:01 +00:00
heightmapCopy ( & _value_modified , _value_original ) ;
2012-07-13 12:23:58 +00:00
QDialog : : accept ( ) ;
}
void DialogHeightMap : : revert ( )
{
2012-07-16 20:34:01 +00:00
heightmapCopy ( _value_original , & _value_modified ) ;
_3dview - > revert ( ) ;
2012-08-08 14:25:01 +00:00
updateResolutionLabel ( ) ;
2012-07-13 12:23:58 +00:00
}
2012-07-13 21:24:19 +00:00
void DialogHeightMap : : angleHChanged ( int value )
{
_3dview - > setHorizontalViewAngle ( M_PI * ( ( double ) value ) / 500.0 ) ;
}
void DialogHeightMap : : angleVChanged ( int value )
{
_3dview - > setVerticalViewAngle ( M_PI_2 * ( ( double ) value ) / 1000.0 ) ;
}
2012-07-15 14:42:50 +00:00
2012-07-16 15:48:05 +00:00
void DialogHeightMap : : brushModeChanged ( int value )
{
_3dview - > setBrushMode ( ( HeightMapBrushMode ) value ) ;
}
2012-07-15 14:42:50 +00:00
void DialogHeightMap : : brushSizeChanged ( int value )
{
_3dview - > setBrushSize ( ( double ) value / 10.0 ) ;
}
void DialogHeightMap : : brushSmoothingChanged ( int value )
{
_3dview - > setBrushSmoothing ( ( double ) value / 1000.0 ) ;
}
2012-07-16 20:34:01 +00:00
void DialogHeightMap : : brushStrengthChanged ( int value )
{
_3dview - > setBrushStrength ( ( double ) value / 2000.0 ) ;
}
2012-08-08 13:30:40 +00:00
void DialogHeightMap : : loadFromFile ( )
{
QString filepath = QFileDialog : : getOpenFileName ( this , tr ( " Paysages 3D - Choose a picture to load " ) , QString ( ) , tr ( " Images (*.jpg *.jpeg *.bmp *.png) " ) ) ;
if ( ! filepath . isNull ( ) )
{
heightmapImportFromPicture ( & _value_modified , ( char * ) filepath . toStdString ( ) . c_str ( ) ) ;
_3dview - > revert ( ) ;
2012-08-08 14:25:01 +00:00
updateResolutionLabel ( ) ;
2012-08-08 13:30:40 +00:00
}
}
void DialogHeightMap : : resetToTerrain ( )
{
2012-12-09 17:49:28 +00:00
/*if (_canvas)
2012-08-08 13:30:40 +00:00
{
TerrainDefinition terrain ;
terrain = terrainCreateDefinition ( ) ;
sceneryGetTerrain ( & terrain ) ;
heightmapRevertToTerrain ( & _value_modified , & terrain , & _canvas - > area ) ;
terrainDeleteDefinition ( & terrain ) ;
2012-12-09 17:49:28 +00:00
2012-08-08 13:30:40 +00:00
_3dview - > revert ( ) ;
2012-12-09 17:49:28 +00:00
} */
2012-08-08 13:30:40 +00:00
}
2012-08-08 14:25:01 +00:00
void DialogHeightMap : : changeResolution ( )
{
QString result ;
QStringList items ;
int current ;
2012-12-09 17:49:28 +00:00
2012-08-08 14:25:01 +00:00
items < < QString ( " 64 x 64 " ) < < QString ( " 128 x 128 " ) < < QString ( " 256 x 256 " ) < < QString ( " 512 x 512 " ) ;
current = 1 ;
if ( _value_modified . resolution_x = = 64 & & _value_modified . resolution_z = = 64 )
{
current = 0 ;
}
else if ( _value_modified . resolution_x = = 256 & & _value_modified . resolution_z = = 256 )
{
current = 2 ;
}
else if ( _value_modified . resolution_x = = 512 & & _value_modified . resolution_z = = 512 )
{
current = 3 ;
}
2012-08-09 17:45:39 +00:00
result = QInputDialog : : getItem ( this , tr ( " Paysages 3D - Change heightmap resolution " ) , tr ( " Choose the new heightmap resolution. \n Beware that lowering the resolution may imply a loss of accuracy. " ) , items , current , false ) ;
2012-08-08 14:25:01 +00:00
if ( ! result . isEmpty ( ) )
{
2012-08-08 14:44:37 +00:00
int new_res_x , new_res_z ;
2012-08-08 14:25:01 +00:00
if ( result = = QString ( " 64 x 64 " ) )
{
2012-08-08 14:44:37 +00:00
new_res_x = new_res_z = 64 ;
2012-08-08 14:25:01 +00:00
}
else if ( result = = QString ( " 256 x 256 " ) )
{
2012-08-08 14:44:37 +00:00
new_res_x = new_res_z = 256 ;
2012-08-08 14:25:01 +00:00
}
else if ( result = = QString ( " 512 x 512 " ) )
{
2012-08-08 14:44:37 +00:00
new_res_x = new_res_z = 512 ;
2012-08-08 14:25:01 +00:00
}
else
{
2012-08-08 14:44:37 +00:00
new_res_x = new_res_z = 128 ;
2012-08-08 14:25:01 +00:00
}
2012-12-09 17:49:28 +00:00
2012-08-08 14:44:37 +00:00
if ( new_res_x ! = _value_modified . resolution_x or new_res_z ! = _value_modified . resolution_z )
2012-08-08 14:25:01 +00:00
{
2012-08-08 14:44:37 +00:00
heightmapChangeResolution ( & _value_modified , new_res_x , new_res_z ) ;
2012-08-08 14:25:01 +00:00
_3dview - > revert ( ) ;
updateResolutionLabel ( ) ;
}
}
}
void DialogHeightMap : : updateResolutionLabel ( )
{
_resolution_label - > setText ( tr ( " Map resolution : %1 x %2 " ) . arg ( _value_modified . resolution_x ) . arg ( _value_modified . resolution_z ) ) ;
}