OSDN Git Service

OBJ読み込み条件を保存するように変更
[qtgeoviewer/QtGeoViewer.git] / Src / QtGeoViewer / FormMain.cpp
1 #include "stdafx.h"
2 #include "FormMain.h"
3
4 #include <QFileInfo>
5 #include <fstream>
6 #include <sstream>
7
8 #include <cassert>
9 #include <QSettings>
10
11 #include "FormVertexBuilder.h"
12 #include "FullScreenPanel.h"
13
14 #include <Qt5Utility/QGui.h>
15
16 #include <QMessageBox>
17 #include <QFileDialog>
18 #include <QPrintDialog>
19 #include <QPrintPreviewDialog>
20
21 #include "BuildInfo.h"
22 #include "PathInfo.h"
23 #include "PostprocLibrary.h"
24 #include "SampleShapeBuilder.h"
25 #include "ShaderLibrary.h"
26 #include "QGVAboutDlg.h"
27
28 #include "FormCustomShader.h"
29 #include "FormPyScript.h"
30 #include "MatcapSelectDlg.h"
31 #include "EnvmapSelectDlg.h"
32 #include "FileDlgUtil.h"
33 #include "MaterialPresetDlg.h"
34
35
36
37 QColor ToQColor(const lgr::color4f& c)
38 {
39         int r = lm::clamp(0, (int)(c.r() * 255.0f), 255);
40         int g = lm::clamp(0, (int)(c.g() * 255.0f), 255);
41         int b = lm::clamp(0, (int)(c.b() * 255.0f), 255);
42         int a = lm::clamp(0, (int)(c.a() * 255.0f), 255);
43
44         return QColor(r, g, b, a);
45 }
46
47
48
49 FormMain::FormMain(QWidget *parent)
50         : QMainWindow(parent)
51         , m_MatcapDlg(NULL)
52         , m_EnvmapDlg(NULL)
53         , m_CustomShaderDlg(NULL)
54         , m_PyScriptDlg(NULL)
55         , m_MatPresetDlg(NULL)
56 {
57         ui.setupUi(this);
58
59         ui.widgetObjectList->setMainForm(this);
60         ui.widgetCamera->setMainForm(this);
61
62         m_EnableAllFeatures = false;
63         m_LastFocusedDockDlg = NULL;
64
65         setAcceptDrops(true);
66
67         setWindowTitle("QtGeoViewer");
68
69         InitializeGLView();
70         InitializeStatusBar();
71         InitializeMenu();
72         InitializeCoordinateCombobox();
73         Initialize2DViewSrcCombobox();
74         InitializeEventFilter();
75         InitContextMenu();
76
77         InitDataBinding();
78
79         ApplyGeomStateFromGUI();
80         SyncShaderSettingsToGUI();
81         updateView_All();
82
83         ui.toolBar_View->setVisible( ui.actionWindow_ToolBarView->isChecked() );
84
85         UpdateViewTypeFromMenu();
86
87         CenteringSplitter();
88
89         ui.widgetObjectList->RebuildObjectList();
90
91         InitializeSubDlg();
92
93         InitializeAnimation();
94
95         InitializeFromConfigFiles();
96
97         HideFeatures();
98
99         ResetHoleRange();
100
101         this->show();
102
103         m_InitRect = rect();
104         m_InitRect.translate(pos());
105
106         LoadLastWinPos();
107
108         ProcArguments();
109 }
110
111 FormMain::~FormMain()
112 {
113 }
114
115
116 void FormMain::LoadLastWinPos(void)
117 {
118         QString file = PathInfo::GetPosConfigFilePath();
119         QSettings conf(file, QSettings::IniFormat);
120
121         QVariant v;
122         v = conf.value("main/WindowPos");
123         if (v.isValid())
124         {
125                 QRect r = v.toRect();
126                 int dl = r.left();
127                 int dt = r.top();
128                 int dw = r.width();
129                 int dh = r.height();
130
131                 resize(dw, dh);
132
133                 move(QPoint(dl, dt));
134         }
135
136         v = conf.value("main/WinState");
137         if (v.isValid())
138         {
139                 if (v.toBool())
140                         setWindowState(Qt::WindowMaximized);
141         }
142 }
143
144 void FormMain::SaveWinPos(void)
145 {
146         QString file = PathInfo::GetPosConfigFilePath();
147         QSettings conf(file, QSettings::IniFormat);
148
149         conf.setValue("main/WinState", isMaximized());
150
151         if (isMaximized() || isMinimized())
152         {
153                 setWindowState(Qt::WindowNoState);
154                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
155         }
156
157         QRect r = rect();
158         r.translate(pos());
159
160         conf.setValue("main/WindowPos", r);
161 }
162
163 void FormMain::HideFeatures(void)
164 {
165         bool b = m_EnableAllFeatures;
166
167         ui.actionVertexBuilder->setVisible(b);
168         ui.menuPrintRoot->menuAction()->setVisible(b);
169         ui.actionShadowmap->setVisible(b);
170         ui.actionEnvmap->setVisible(b);
171         ui.actionShaderCustom->setVisible(b);
172         ui.actionWindowCustomShader->setVisible(b);
173         ui.actionPyScript->setVisible(b);
174         ui.actionConsole->setVisible(b);
175         ui.actionReleaseShader->setVisible(b);
176 }
177
178 void FormMain::InitializeEventFilter(void)
179 {
180         ui.editEnvMap->installEventFilter(this);
181         ui.editMatcap->installEventFilter(this);
182
183         ui.editCurrentTexName->installEventFilter(this);
184         ui.editCurrentMatNormalMap->installEventFilter(this);
185         ui.editMatCapEachMaterial->installEventFilter(this);
186
187         ui.colorAmbient->installEventFilter(this);
188         ui.colorEmission->installEventFilter(this);
189         ui.colorDiffuse->installEventFilter(this);
190         ui.colorSpecular->installEventFilter(this);
191 }
192
193 void FormMain::InitDataBinding(void)
194 {
195         InitializeVisiblStateMenu();
196         InitializeUVStateMenu();
197         InitializeTexStateMenu();
198         InitializeSceneStateMenu();
199         InitializeGeomStateMenu();
200         InitializeCursorMenu();
201
202         SyncViewSettingsFromGUI();
203         m_Binder_Scene.UpdateAllData();
204         m_Binder_UVConfig.UpdateAllData();
205         m_Binder_TexConfig.UpdateAllData();
206         SyncTexConfigToData();
207 }
208
209 void FormMain::InitializeVisiblStateMenu(void)
210 {
211         DataBinderMap& dbmap = m_Binder_ViewConfig;
212
213         View3DConfig& cfg_3d = m_View3d.m_Config;
214
215         dbmap.AddBinder(new MenuBinder(ui.actionDrawFace              , &cfg_3d.m_DrawFace              ));
216         dbmap.AddBinder(new MenuBinder(ui.actionDrawWire              , &cfg_3d.m_DrawWire              ));
217         dbmap.AddBinder(new MenuBinder(ui.actionFlatShading           , &cfg_3d.m_EnableFlatShade       ));
218         dbmap.AddBinder(new MenuBinder(ui.actionVertexColor           , &cfg_3d.m_EnableVertexColor     ));
219         dbmap.AddBinder(new MenuBinder(ui.actionVertexColor_0_1       , &cfg_3d.m_VertColorTo01         ));
220         dbmap.AddBinder(new MenuBinder(ui.actionEnableTexture3D       , &cfg_3d.m_EnableTexture         ));
221         dbmap.AddBinder(new MenuBinder(ui.actionDrawBBox              , &cfg_3d.m_DrawBBox              ));
222         dbmap.AddBinder(new MenuBinder(ui.actionFaceVBO               , &cfg_3d.m_EnableFaceVBO         ));
223         dbmap.AddBinder(new MenuBinder(ui.actionVidTopMost            , &cfg_3d.m_ShowVidTopMost        ));
224         dbmap.AddBinder(new MenuBinder(ui.actionDrawBBoxRange         , &cfg_3d.m_DrawBBoxRange         ));
225         dbmap.AddBinder(new MenuBinder(ui.actionWireDispList          , &cfg_3d.m_EnableWireDispList    ));
226         dbmap.AddBinder(new MenuBinder(ui.actionMultisample           , &cfg_3d.m_EnableMultisample     ));
227         dbmap.AddBinder(new MenuBinder(ui.actionCoverageTransparent   , &cfg_3d.m_EnableCoverageTrans   ));
228         dbmap.AddBinder(new MenuBinder(ui.actionSeparateSpecular      , &cfg_3d.m_SeparateSpecular      ));
229         dbmap.AddBinder(new MenuBinder(ui.actionShowSelVertCoord      , &cfg_3d.m_ShowSelVertCoord      ));
230         dbmap.AddBinder(new MenuBinder(ui.actionHighlightSelected     , &cfg_3d.m_HighlightSelected     ));
231         dbmap.AddBinder(new MenuBinder(ui.actionHighlightSelectedWire , &cfg_3d.m_HighlightSelectedWire ));
232         dbmap.AddBinder(new MenuBinder(ui.actionFixMaterialSilver     , &cfg_3d.m_UseFixMaterial        ));
233         dbmap.AddBinder(new MenuBinder(ui.actionShadowmapBuffer       , &cfg_3d.m_DrawShadowmapBuf      ));
234         dbmap.AddBinder(new MenuBinder(ui.actionEnableLighting        , &cfg_3d.m_EnableLighting        ));
235         dbmap.AddBinder(new MenuBinder(ui.actionEnableCulling         , &cfg_3d.m_EnableCullFace        ));
236         dbmap.AddBinder(new MenuBinder(ui.actionDrawVid               , &cfg_3d.m_DrawVid               ));
237         dbmap.AddBinder(new MenuBinder(ui.actionDrawVertNormal        , &cfg_3d.m_DrawVertNormal        ));
238         dbmap.AddBinder(new MenuBinder(ui.actionDrawVert              , &cfg_3d.m_DrawVert              ));
239         dbmap.AddBinder(new MenuBinder(ui.actionWireVBO               , &cfg_3d.m_EnableWireVBO         ));
240         dbmap.AddBinder(new MenuBinder(ui.actionIndexColorMode        , &cfg_3d.m_IndexMaterial         ));
241         dbmap.AddBinder(new MenuBinder(ui.actionFBMaterial            , &cfg_3d.m_FBMaterial            ));
242         dbmap.AddBinder(new MenuBinder(ui.actionDrawMiniAxis          , &cfg_3d.m_DrawMiniAxis          ));
243         dbmap.AddBinder(new MenuBinder(ui.actionDrawLookPos           , &cfg_3d.m_DrawLookPos           ));
244         dbmap.AddBinder(new MenuBinder(ui.actionDrawLightPos          , &cfg_3d.m_DrawLightPosition     ));
245         dbmap.AddBinder(new MenuBinder(ui.actionDrawGround            , &cfg_3d.m_DrawGround            ));
246         dbmap.AddBinder(new MenuBinder(ui.actionDrawFid               , &cfg_3d.m_DrawFid               ));
247         dbmap.AddBinder(new MenuBinder(ui.actionCameraRecords         , &cfg_3d.m_DrawCameraRecord      ));
248         dbmap.AddBinder(new MenuBinder(ui.actionDrawAxis              , &cfg_3d.m_DrawAxis              ));
249         dbmap.AddBinder(new MenuBinder(ui.actionDoubleSideShading     , &cfg_3d.m_DoubleSideShading     ));
250         dbmap.AddBinder(new MenuBinder(ui.actionDirectionalLight      , &cfg_3d.m_LightIsDirectional    ));
251         dbmap.AddBinder(new MenuBinder(ui.actionCullFrontOrBack       , &cfg_3d.m_CullAngle_F           ));
252         dbmap.AddBinder(new MenuBinder(ui.actionHighlightMaterial     , &cfg_3d.m_HighlightMaterial     ));
253         dbmap.AddBinder(new MenuBinder(ui.actionCloseVert             , &cfg_3d.m_ShowCloseVertInfo     ));
254         dbmap.AddBinder(new MenuBinder(ui.actionCameraRange           , &cfg_3d.m_DrawRenderRange       ));
255         dbmap.AddBinder(new MenuBinder(ui.actionRenderTime            , &cfg_3d.m_ShowRenderTime        ));
256         dbmap.AddBinder(new MenuBinder(ui.actionUseFixTexture         , &cfg_3d.m_UseFixTexture         ));
257         dbmap.AddBinder(new MenuBinder(ui.actionShowSelVertIdx        , &cfg_3d.m_ShowSelVertIdx        ));
258         dbmap.AddBinder(new MenuBinder(ui.actionShowMeshBound         , &cfg_3d.m_ShowMeshBound         ));
259         dbmap.AddBinder(new MenuBinder(ui.actionPickTransparent       , &cfg_3d.m_PickTransparent       ));
260         dbmap.AddBinder(new MenuBinder(ui.actionDrawPolylineLenWhl    , &cfg_3d.m_DrawPolylineLenWhl    ));
261         dbmap.AddBinder(new MenuBinder(ui.actionDrawPolylineLen       , &cfg_3d.m_DrawPolylineLen       ));
262         dbmap.AddBinder(new MenuBinder(ui.actionDrawPolylineIdx       , &cfg_3d.m_DrawPolylineIdx       ));
263         dbmap.AddBinder(new MenuBinder(ui.actionDrawPolyline          , &cfg_3d.m_DrawPolyline          ));
264         dbmap.AddBinder(new MenuBinder(ui.actionDrawBone              , &cfg_3d.m_DrawBone              ));
265         dbmap.AddBinder(new MenuBinder(ui.actionShowMeasure           , &cfg_3d.m_DrawCameraMeasure     ));
266         dbmap.AddBinder(new MenuBinder(ui.actionFlipMouseMR           , &cfg_3d.m_FlipMouseMR           ));
267         dbmap.AddBinder(new MenuBinder(ui.actionSyncLightToCamera     , &cfg_3d.m_SyncLightTocamera     ));
268
269         for (GuiBinder& binder : dbmap.m_BinderMap)
270         {
271                 MenuBinder* mb = dynamic_cast<MenuBinder*>(&binder);
272                 if (mb != NULL)
273                 {
274                         QAction* a = mb->GetMenu();
275                         connect(
276                                 a,
277                                 SIGNAL(triggered(bool)),
278                                 this,
279                                 SLOT(actionVisibleStates_Triggered(bool)));
280                 }
281         }
282 }
283
284 void FormMain::InitializeUVStateMenu(void)
285 {
286         DataBinderMap& dbmap = m_Binder_UVConfig;
287
288         dbmap.AddBinder(new MenuBinder(ui.actionUVRepeat         , &m_View2d.m_RepeatTexture             ));
289         dbmap.AddBinder(new MenuBinder(ui.actionUseFixTexture2D  , &m_View2d.m_UseFixTexute              ));
290         dbmap.AddBinder(new MenuBinder(ui.actionShowVertex2d     , &m_View2d.m_DrawVerts                 ));
291         dbmap.AddBinder(new MenuBinder(ui.actionShowImage2d      , &m_View2d.m_EnableTexture             ));
292
293         for (GuiBinder& binder : dbmap.m_BinderMap)
294         {
295                 MenuBinder* mb = dynamic_cast<MenuBinder*>(&binder);
296                 if (mb != NULL)
297                 {
298                         QAction* a = mb->GetMenu();
299                         connect(
300                                 a,
301                                 SIGNAL(triggered(bool)),
302                                 this,
303                                 SLOT(actionVisibleStatesUV_Triggered(bool)));
304                 }
305         }
306 }
307
308 void FormMain::InitializeTexStateMenu(void)
309 {
310         DataBinderMap& dbmap = m_Binder_TexConfig;
311
312         static bool t = false;
313
314         dbmap.AddBinder(new MenuBinder(ui.actionUVFlipY         , &m_Scene.m_TextureTransform.m_FlipY   ));
315         dbmap.AddBinder(new MenuBinder(ui.actionTextureCompress , &m_Scene.m_TexConfig.m_EnableCompress ));
316
317         for (GuiBinder& binder : dbmap.m_BinderMap)
318         {
319                 MenuBinder* mb = dynamic_cast<MenuBinder*>(&binder);
320                 if (mb != NULL)
321                 {
322                         QAction* a = mb->GetMenu();
323                         connect(
324                                 a,
325                                 SIGNAL(triggered(bool)),
326                                 this,
327                                 SLOT(actionVisibleStatesTex_Triggered(bool)));
328                 }
329         }
330 }
331
332 void FormMain::on_actionTextureMipmap_toggled(bool b)
333 {
334         m_Scene.m_TexConfig.m_SampleMode = gl::TexSampleMode::Mipmap;
335         updateView_All();
336 }
337
338 void FormMain::on_actionTextureLinear_toggled(bool b)
339 {
340         m_Scene.m_TexConfig.m_SampleMode = gl::TexSampleMode::Linear;
341         updateView_All();
342 }
343
344 void FormMain::on_actionTextureNearest_toggled(bool b)
345 {
346         m_Scene.m_TexConfig.m_SampleMode = gl::TexSampleMode::Nearest;
347         updateView_All();
348 }
349
350 void FormMain::SyncTexConfigToGUI(bool silent)
351 {
352         gl::TexSampleMode mode = m_Scene.m_TexConfig.m_SampleMode;
353
354         if (silent)
355         {
356                 SetActioncheck(ui.actionTextureMipmap, true, false);
357                 SetActioncheck(ui.actionTextureLinear, true, false);
358                 SetActioncheck(ui.actionTextureNearest, true, false);
359         }
360
361         QAction* a = NULL;
362         if (mode == gl::TexSampleMode::Mipmap)
363                 a = ui.actionTextureMipmap;
364         else if (mode == gl::TexSampleMode::Linear)
365                 a = ui.actionTextureLinear;
366         else if (mode == gl::TexSampleMode::Nearest)
367                 a = ui.actionTextureNearest;
368
369         SetActioncheck(a, silent, true);
370 }
371
372 void FormMain::SetActioncheck(QAction* a, bool silent, bool check)
373 {
374         assert(a != NULL);
375         if (silent)
376                 a->blockSignals(true);
377
378         a->setChecked(check);
379
380         if (silent)
381                 a->blockSignals(false);
382 }
383
384 void FormMain::SyncTexConfigToData(void)
385 {
386         if (ui.actionTextureMipmap->isChecked())
387                 m_Scene.m_TexConfig.m_SampleMode = gl::TexSampleMode::Mipmap;
388         else if (ui.actionTextureLinear->isChecked())
389                 m_Scene.m_TexConfig.m_SampleMode = gl::TexSampleMode::Linear;
390         else if (ui.actionTextureNearest->isChecked())
391                 m_Scene.m_TexConfig.m_SampleMode = gl::TexSampleMode::Nearest;
392 }
393
394 void FormMain::InitializeSceneStateMenu(void)
395 {
396         DataBinderMap& dbmap = m_Binder_Scene;
397
398         dbmap.AddBinder(new CheckboxBinder( ui.checkEnvMap         , &m_Scene.m_EnvImg.m_VisibleEnvSphere       ));
399         dbmap.AddBinder(new CheckboxBinder( ui.checkEnvReflection  , &m_Scene.m_EnvImg.m_IsEnableReflection     ));
400         dbmap.AddBinder(new CheckboxBinder( ui.checkShadowEnabled  , &m_Scene.m_ShadowConfig.m_EnableShadow     ));
401         dbmap.AddBinder(new CheckboxBinder( ui.checkSoftShadow     , &m_Scene.m_ShadowConfig.m_EnableSoftShadow ));
402
403         for (GuiBinder& binder : dbmap.m_BinderMap)
404         {
405                 CheckboxBinder* mb = dynamic_cast<CheckboxBinder*>(&binder);
406                 if (mb != NULL)
407                 {
408                         QCheckBox* c = mb->GetCheckBox();
409                         connect(
410                                 c,
411                                 SIGNAL(toggled(bool)),
412                                 this,
413                                 SLOT(actionSceneStates_Toggled(bool)));
414                 }
415         }
416 }
417
418 void FormMain::InitializeCursorMenu(void)
419 {
420         DataBinderMap& dbmap = m_Binder_Cursor;
421
422         Cursor3D& cursor = m_Scene.m_Cursor3d;
423         dbmap.AddBinder(new MenuBinder( ui.actionShowCursor        , &cursor.ShowCursor     ));
424         dbmap.AddBinder(new MenuBinder( ui.actionCursorToDepth     , &cursor.CursorDepth    ));
425         dbmap.AddBinder(new MenuBinder( ui.actionCursorCoord       , &cursor.ShowCoord      ));
426         dbmap.AddBinder(new MenuBinder( ui.actionCheckBaryCoord    , &cursor.CheckBaryCoord ));
427         dbmap.AddBinder(new MenuBinder( ui.actionSelectCloseMat    , &cursor.SelCloseMat    ));
428         dbmap.AddBinder(new MenuBinder( ui.actionCursorAxis        , &cursor.ShowAxis       ));
429         dbmap.AddBinder(new MenuBinder( ui.actionMeasure           , &cursor.ShowMeasure    ));
430         dbmap.AddBinder(new MenuBinder( ui.actionMeasureLen        , &cursor.ShowMeasureLen ));
431         dbmap.AddBinder(new MenuBinder( ui.actionMeasureXYZ        , &cursor.ShowMeasureXYZ ));
432         dbmap.AddBinder(new MenuBinder( ui.actionRecordStroke      , &cursor.RecordStroke   ));
433         dbmap.AddBinder(new MenuBinder( ui.actionShowStrokeLength  , &cursor.ShowStrokeLen  ));
434         dbmap.AddBinder(new MenuBinder( ui.actionTransparentStroke , &cursor.TranspStorke   ));
435
436         for (GuiBinder& binder : dbmap.m_BinderMap)
437         {
438                 MenuBinder* mb = dynamic_cast<MenuBinder*>(&binder);
439                 if (mb != NULL)
440                 {
441                         QAction* a = mb->GetMenu();
442                         connect(
443                                 a,
444                                 SIGNAL(triggered(bool)),
445                                 this,
446                                 SLOT(actionCursorMenuStates_Toggled(bool)));
447                 }
448         }
449 }
450
451
452 void FormMain::InitializeGeomStateMenu(void)
453 {
454         AddGeomStateAction( ui.actionCentering  );
455         AddGeomStateAction( ui.actionAutoResize );
456 }
457
458 void FormMain::AddGeomStateAction(const QAction* action)
459 {
460         connect(
461                 action,
462                 SIGNAL(triggered(bool)),
463                 this,
464                 SLOT(actionGeomStates_Triggered(bool)));
465 }
466
467
468 void FormMain::InitializeGLView(void)
469 {
470         ui.GLWidgetMain->setMouseTracking(true);
471         ui.GLWidgetUV->setMouseTracking(true);
472
473         ui.GLWidgetMain->SetViewer(&m_View3d);
474         m_View3d.m_Widgets = &m_Widgets;
475         m_View3d.RegisterScene(&m_Scene);
476         m_View3d.InitializeView(ui.GLWidgetMain);
477
478         ui.GLWidgetUV->SetViewer(&m_View2d);
479         m_View2d.m_Widgets = &m_Widgets;
480         m_View2d.RegisterScene(&m_Scene);
481         m_View2d.InitializeView(ui.GLWidgetUV);
482
483         m_Widgets.AddWidget(ui.GLWidgetMain);
484         m_Widgets.AddWidget(ui.GLWidgetUV);
485
486         // OpenGL\82Ì\83\8a\83X\83g\82Ì\8b¤\97L\89»
487         SetContextShare();
488
489         m_ContextShare.BeginDrawTop();
490
491         m_Scene.Initialize();
492
493         m_ContextShare.EndDrawTop();
494
495         connect(
496                 &m_View3d,
497                 SIGNAL(SelectedObjectChanged(int, int)),
498                 SLOT(View3D_SelectedObjectChanged(int, int)));
499         connect(
500                 &m_View3d,
501                 SIGNAL(SelectedMatChanged(int)),
502                 SLOT(View3D_SelectedMatChanged(int)));
503         connect(
504                 &m_View3d,
505                 SIGNAL(CameraMoved(void)),
506                 SLOT(View3D_CameraMoved(void)));
507         connect(
508                 &m_View3d,
509                 SIGNAL(StatusTipChanged(QString)),
510                 SLOT(View3D_StatusTipChanged(QString)));
511         connect(
512                 &m_View3d,
513                 SIGNAL(SequenceStepped(int)),
514                 SLOT(View3D_SequenceStepped(int)));
515         connect(
516                 &m_View3d,
517                 SIGNAL(CursorMoved()),
518                 SLOT(View3D_CursorMoved()));
519 }
520
521 // OpenGL\82Ì\83\8a\83X\83g\82Ì\8b¤\97L\89»
522 void FormMain::SetContextShare(void)
523 {
524         ui.GLWidgetUV->makeCurrent();
525         m_ContextShare.AddContext( GetDC((HWND)ui.GLWidgetUV->winId()) , wglGetCurrentContext() );
526         ui.GLWidgetUV->doneCurrent();
527
528         ui.GLWidgetMain->makeCurrent();
529         m_ContextShare.AddContext( GetDC((HWND)ui.GLWidgetUV->winId()) , wglGetCurrentContext() );
530         ui.GLWidgetMain->doneCurrent();
531
532         m_ContextShare.MakeShare();
533 }
534
535 void FormMain::InitializeCoordinateCombobox(void)
536 {
537         ui.comboCoordinate->blockSignals(true);
538         ui.comboCoordinate->addItem("Yup RH"); // RUF
539         ui.comboCoordinate->addItem("Zup RH"); // FRU
540         ui.comboCoordinate->addItem("Xup RH"); // UFR
541         ui.comboCoordinate->addItem("Yup LH"); // RUB
542         ui.comboCoordinate->addItem("Zup LH"); // BRU
543         ui.comboCoordinate->addItem("Xup LH"); // UBR
544         ui.comboCoordinate->blockSignals(false);
545 }
546
547 void FormMain::Initialize2DViewSrcCombobox(void)
548 {
549         ui.combo2DViewSrcType->blockSignals(true);
550         ui.combo2DViewSrcType->addItem("Color");
551         ui.combo2DViewSrcType->addItem("Normal");
552         ui.combo2DViewSrcType->blockSignals(false);
553 }
554
555 void FormMain::InitializeStatusBar(void)
556 {
557         m_StatusBar0 = new QLabel(this);
558         m_StatusBar1 = new QLabel(this);
559         ui.statusBar->addWidget(m_StatusBar0, 1);
560         ui.statusBar->addWidget(m_StatusBar1, 0);
561
562         m_StatusBar0->setText("");
563
564         m_StatusBar1->setMinimumWidth(290);
565         m_StatusBar1->setText("");
566 }
567
568 void FormMain::InitializeFromConfigFiles(void)
569 {
570         if (!InitializeConfig(PathInfo::GetGuiConfigFilePath()))
571         {
572                 InitializeConfig(PathInfo::GetDefaultGuiConfigFilePath());
573         }
574
575         ui.widgetCamera->OpenCameraFile(PathInfo::GetCameraLogFilePath());
576
577         if (!LoadWindowLayout(PathInfo::GetWindowLayoutConfigFilePath()))
578         {
579                 LoadWindowLayout(PathInfo::GetDefaultWindowLayoutConfigFilePath());
580         }
581
582         if (!QGui::LoadGUIState(this, PathInfo::GetLayoutConfigFilePath()))
583         {
584                 QGui::LoadGUIState(this, PathInfo::GetDefaultLayoutConfigFilePath());
585         }
586 }
587
588 void FormMain::LoadDefaultConfig(void)
589 {
590         InitializeConfig(PathInfo::GetDefaultGuiConfigFilePath());
591         QGui::LoadGUIState(this, PathInfo::GetDefaultLayoutConfigFilePath());
592         LoadWindowLayout(PathInfo::GetDefaultWindowLayoutConfigFilePath());
593 }
594
595 void FormMain::SaveConfig(void)
596 {
597         PathInfo::CreateConfiDirIfNotExist();
598
599         SaveConfigFile(PathInfo::GetGuiConfigFilePath());
600
601         m_View3d.m_CameraRecord.SaveCamera(PathInfo::GetCameraLogFilePath().toLocal8Bit().data());
602
603         QGui::SaveGUIState(this, PathInfo::GetLayoutConfigFilePath());
604
605         SaveWindowLayout(PathInfo::GetWindowLayoutConfigFilePath());
606 }
607
608 bool FormMain::InitializeConfig(QString path)
609 {
610         View3DConfig& config_3d = m_View3d.m_Config;
611
612         GuiConfig cfg;
613         cfg.m_Config3D = &config_3d;
614         cfg.m_Scene    = &m_Scene;
615
616         if (!cfg.LoadConfigT(path.toLocal8Bit().data()))
617                 return false;
618
619         ui.actionUVAutoFit->setChecked(cfg.m_AutFitUVView);
620
621         ui.comboCoordinate->setCurrentIndex(cfg.m_CoordType);
622
623         CreateRecentFilesMenu(cfg.m_RecentFiles);
624
625         m_Binder_ViewConfig.UpdateAllGUI(true);
626         m_Binder_UVConfig.UpdateAllGUI(true);
627         m_Binder_Scene.UpdateAllGUI(true);
628         m_Binder_Cursor.UpdateAllGUI(true);
629         m_Binder_TexConfig.UpdateAllGUI(true);
630         SyncTexConfigToGUI(true);
631
632         ApplyGeomStateDataoToGUI();
633
634         SyncViewSettingsFromGUI();
635         m_Binder_UVConfig.UpdateAllData();
636         m_Binder_TexConfig.UpdateAllData();
637         SyncTexConfigToData();
638         SyncShaderSettingsToGUI();
639
640         ui.widgetIOOption->setConfig(m_Scene.m_IOConfig);
641
642         updateView_All();
643
644         m_EnableAllFeatures = cfg.m_EnableAllFeatures;
645
646         return true;
647 }
648
649 void FormMain::SaveConfigFile(QString path)
650 {
651         GuiConfig cfg;
652         cfg.m_Config3D = &m_View3d.m_Config;
653         cfg.m_Scene    = &m_Scene;
654
655         ui.widgetIOOption->getConfig(m_Scene.m_IOConfig);
656
657         cfg.m_EnableAllFeatures = m_EnableAllFeatures;
658
659         cfg.m_AutFitUVView = ui.actionUVAutoFit ->isChecked();
660
661         cfg.m_CoordType = ui.comboCoordinate->currentIndex();
662
663         CreateRecentFilesFromMenu(cfg.m_RecentFiles);
664
665         cfg.SaveConfigT(path.toLocal8Bit().data());
666 }
667
668
669 void FormMain::InitializeSubDlg(void)
670 {
671         connect(
672                 &m_WireColorSelDlg,
673                 SIGNAL(currentColorChanged(const QColor &)),
674                 this,
675                 SLOT(WireOverlayColorChanged(const QColor &)));
676         connect(
677                 &m_DiffuseColorSelDlg,
678                 SIGNAL(currentColorChanged(const QColor &)),
679                 this,
680                 SLOT(FaceDiffuseColorChanged(const QColor &)));
681         connect(
682                 &m_BGColorSelDlg,
683                 SIGNAL(currentColorChanged(const QColor &)),
684                 this,
685                 SLOT(BGColorChanged(const QColor &)));
686         connect(
687                 &m_SelMatColorSelDlg,
688                 SIGNAL(currentColorChanged(const QColor &)),
689                 this,
690                 SLOT(SelMatColorChanged(const QColor &)));
691
692         m_SelMatColorWidget = NULL;
693
694         m_ViewConfigDlg.InitializeConfigDlg(&m_View3d.m_Config);
695         connect(
696                 &m_ViewConfigDlg,
697                 SIGNAL(ConfigChanged()),
698                 this,
699                 SLOT(ConfigChangedDlg_ConfigChanged()));
700
701         m_CrossSectionDlg.InitializeConfigDlg(&m_View3d);
702         connect(
703                 &m_CrossSectionDlg,
704                 SIGNAL(ConfigChanged()),
705                 this,
706                 SLOT(CrossSectionDlg_ConfigChanged()));
707 }
708
709 void FormMain::InitializeAnimation(void)
710 {
711         connect(
712                 &m_AnimationTimer,
713                 SIGNAL(timeout()),
714                 this,
715                 SLOT(OnAnimationTimerUpdated()));
716         m_AnimationTimer.setInterval(20);
717 }
718
719 void FormMain::InitializeMenu(void)
720 {
721         m_AGroup_Window = new QActionGroup(this);
722         m_AGroup_Window->setExclusive(true);
723         m_AGroup_Window->addAction( ui.actionWindow_3DView    );
724         m_AGroup_Window->addAction( ui.actionWindow_UVView    );
725         m_AGroup_Window->addAction( ui.actionWindows_DualView );
726
727         m_AGroup_Shader = new QActionGroup(this);
728         m_AGroup_Shader->setExclusive(true);
729         m_AGroup_Shader->addAction( ui.actionShaderDefault   );
730         m_AGroup_Shader->addAction( ui.actionShaderPhong     );
731         m_AGroup_Shader->addAction( ui.actionShaderCustom    );
732         m_AGroup_Shader->addAction( ui.actionNormalColor     );
733         m_AGroup_Shader->addAction( ui.actionDepthColor      );
734         m_AGroup_Shader->addAction( ui.actionShadowmap       );
735         m_AGroup_Shader->addAction( ui.actionEnvmap          );
736         m_AGroup_Shader->addAction( ui.actionIntegrateShader );
737         m_AGroup_Shader->addAction( ui.actionMatcapShader    );
738
739         m_AGroup_PProc = new QActionGroup(this);
740         m_AGroup_PProc->addAction( ui.actionPostProcNone            );
741         m_AGroup_PProc->addAction( ui.actionPostProcAntialias       );
742         m_AGroup_PProc->addAction( ui.actionPostProcBorder          );
743         m_AGroup_PProc->addAction( ui.actionPostProcDepthLayerColor );
744         m_AGroup_PProc->addAction( ui.actionPostProcDepthColor      );
745         m_AGroup_PProc->addAction( ui.actionPostProcDepthOfField    );
746
747         m_AGroup_Texture = new QActionGroup(this);
748         m_AGroup_Texture->addAction( ui.actionTextureMipmap  );
749         m_AGroup_Texture->addAction( ui.actionTextureNearest );
750         m_AGroup_Texture->addAction( ui.actionTextureLinear  );
751 }
752
753 void FormMain::InitContextMenu(void)
754 {
755         ui.widgetObjectList->RegisterContextMenu();
756 }
757
758 void FormMain::ProcArguments(void)
759 {
760         QStringList args = QApplication::arguments();
761         for (int i = 1; i < args.size(); ++i)
762         {
763                 QString s = args[i];
764                 OpenGeomFileToLast(s);
765         }
766 }
767
768 void FormMain::closeEvent(QCloseEvent *e)
769 {
770         SaveWinPos();
771
772         SaveConfig();
773
774         e->accept();
775
776         m_ViewConfigDlg.close();
777         m_CrossSectionDlg.close();
778         m_FullscreenPanel.close();
779
780         if (m_CustomShaderDlg != NULL)
781                 m_CustomShaderDlg->close();
782         if (m_PyScriptDlg != NULL)
783                 m_PyScriptDlg->close();
784
785         if (m_MatcapDlg != NULL)
786                 m_MatcapDlg->close();
787         if (m_EnvmapDlg != NULL)
788                 m_EnvmapDlg->close();
789         if (m_MatPresetDlg != NULL)
790                 m_MatPresetDlg->close();
791
792         m_ContextShare.BeginDrawTop();
793         m_Scene.FinalizeScene();
794         m_ContextShare.EndDrawTop();
795 }
796
797
798 bool FormMain::eventFilter(QObject * filterobj, QEvent * filterevt)
799 {
800         if (filterobj == ui.editEnvMap)
801         {
802                 if (filterevt->type() == QEvent::DragEnter)
803                         return AcceptDropFilterIfUrl(filterevt);
804
805                 if (filterevt->type() == QEvent::Drop)
806                         return OnDropFile_editEnvMap(filterevt);
807         }
808         else if (filterobj == ui.editMatcap)
809         {
810                 if (filterevt->type() == QEvent::DragEnter)
811                         return AcceptDropFilterIfUrl(filterevt);
812
813                 if (filterevt->type() == QEvent::Drop)
814                         return OnDropFile_editMatcap(filterevt);
815         }
816         else if (filterobj == ui.editMatCapEachMaterial)
817         {
818                 if (filterevt->type() == QEvent::DragEnter)
819                         return AcceptDropFilterIfUrl(filterevt);
820
821                 if (filterevt->type() == QEvent::Drop)
822                         return OnDropFile_editMatcapEachMat(filterevt);
823         }
824         else if (filterobj == ui.editCurrentTexName)
825         {
826                 if (filterevt->type() == QEvent::DragEnter)
827                         return AcceptDropFilterIfUrl(filterevt);
828
829                 if (filterevt->type() == QEvent::Drop)
830                         return OnDropFile_editCurrentTexName(filterevt);
831         }
832         else if (filterobj == ui.editCurrentMatNormalMap)
833         {
834                 if (filterevt->type() == QEvent::DragEnter)
835                         return AcceptDropFilterIfUrl(filterevt);
836
837                 if (filterevt->type() == QEvent::Drop)
838                         return OnDropFile_editCurrentMatNormalMap(filterevt);
839         }
840         else if (filterobj == ui.colorAmbient)
841         {
842                 if (filterevt->type() == QEvent::MouseButtonPress)
843                         return colorAmbient_OnButtonDown(filterevt);
844         }
845         else if (filterobj == ui.colorEmission)
846         {
847                 if (filterevt->type() == QEvent::MouseButtonPress)
848                         return colorEmission_OnButtonDown(filterevt);
849         }
850         else if (filterobj == ui.colorDiffuse)
851         {
852                 if (filterevt->type() == QEvent::MouseButtonPress)
853                         return colorDiffuse_OnButtonDown(filterevt);
854         }
855         else if (filterobj == ui.colorSpecular)
856         {
857                 if (filterevt->type() == QEvent::MouseButtonPress)
858                         return colorSpecular_OnButtonDown(filterevt);
859         }
860
861         return QMainWindow::eventFilter(filterobj, filterevt);
862 }
863
864 bool FormMain::OnDropFile_editEnvMap(QEvent * filterevt)
865 {
866         QString path = GetFirstLoadableImagePathFromDragEvent(filterevt);
867         if (!path.isEmpty())
868                 LoadEnvMap(path);
869
870         return true;
871 }
872
873 bool FormMain::OnDropFile_editMatcap(QEvent * filterevt)
874 {
875         QString path = GetFirstLoadableImagePathFromDragEvent(filterevt);
876         if (!path.isEmpty())
877                 LoadMatcapImage(path);
878
879         return true;
880 }
881
882 bool FormMain::OnDropFile_editMatcapEachMat(QEvent * filterevt)
883 {
884         QString path = GetFirstLoadableImagePathFromDragEvent(filterevt);
885         if (!path.isEmpty())
886                 LoadMatcapImageForCurrentMat(path);
887
888         return true;
889 }
890
891 bool FormMain::OnDropFile_editCurrentTexName(QEvent * filterevt)
892 {
893         QString path = GetFirstLoadableImagePathFromDragEvent(filterevt);
894         if (!path.isEmpty())
895                 LoadCurSelMatTexture(TextureType::Color, path);
896
897         return true;
898 }
899
900 bool FormMain::OnDropFile_editCurrentMatNormalMap(QEvent * filterevt)
901 {
902         QString path = GetFirstLoadableImagePathFromDragEvent(filterevt);
903         if (!path.isEmpty())
904                 LoadCurSelMatTexture(TextureType::Normal, path);
905
906         return true;
907 }
908
909 bool FormMain::AcceptDropFilterIfUrl(QEvent * filterevt)
910 {
911         QDragEnterEvent* e = dynamic_cast<QDragEnterEvent*>(filterevt);
912         if (e == NULL)
913                 return false;
914
915         if (e->mimeData()->hasUrls())
916                 e->acceptProposedAction();
917
918         return true;
919 }
920
921 QString FormMain::GetFirstLoadableImagePathFromDragEvent(QEvent* filterevt)
922 {
923         QDropEvent* e = dynamic_cast<QDropEvent*>(filterevt);
924         if (e == NULL)
925                 return false;
926
927         if (!e->mimeData()->hasUrls())
928                 return false;
929
930         QList<QUrl> urls = e->mimeData()->urls();
931         for (QUrl& url : urls)
932         {
933                 QString path = url.toLocalFile();
934                 if (IsSupportedTextureExt(path))
935                         return path;
936         }
937
938         return QString();
939 }
940
941 bool FormMain::colorAmbient_OnButtonDown(QEvent * filterevt)
942 {
943         QMouseEvent * e = dynamic_cast<QMouseEvent *>(filterevt);
944         if (e->buttons() == Qt::LeftButton)
945                 ChangeMatlemColor(ui.colorAmbient);
946         return true;
947 }
948
949 bool FormMain::colorEmission_OnButtonDown(QEvent * filterevt)
950 {
951         QMouseEvent * e = dynamic_cast<QMouseEvent *>(filterevt);
952         if (e->buttons() == Qt::LeftButton)
953                 ChangeMatlemColor(ui.colorEmission);
954         return true;
955 }
956
957 bool FormMain::colorDiffuse_OnButtonDown(QEvent * filterevt)
958 {
959         QMouseEvent * e = dynamic_cast<QMouseEvent *>(filterevt);
960         if (e->buttons() == Qt::LeftButton)
961                 ChangeMatlemColor(ui.colorDiffuse);
962         return true;
963 }
964
965 bool FormMain::colorSpecular_OnButtonDown(QEvent * filterevt)
966 {
967         QMouseEvent * e = dynamic_cast<QMouseEvent *>(filterevt);
968         if (e->buttons() == Qt::LeftButton)
969                 ChangeMatlemColor(ui.colorSpecular);
970         return true;
971 }
972
973 void FormMain::ChangeMatlemColor(QWidget* col_widget)
974 {
975         lib_geo::BaseMaterial* mat = m_Scene.GetSelectedMaterial();
976         if (mat == NULL)
977                 return;
978
979         lgr::color4f* col = NULL;
980         if (col_widget == ui.colorAmbient  ) col = &mat->m_Ambient  ;
981         if (col_widget == ui.colorEmission ) col = &mat->m_Emission ;
982         if (col_widget == ui.colorDiffuse  ) col = &mat->m_Diffuse  ;
983         if (col_widget == ui.colorSpecular ) col = &mat->m_Specular ;
984
985         lgr::color4f src = *col;
986
987         QColor dc = ToQColor(*col);
988
989         m_SelMatColorWidget = col_widget;
990
991         m_SelMatColorSelDlg.setOption(QColorDialog::ShowAlphaChannel);
992         m_SelMatColorSelDlg.setCurrentColor(dc);
993         if (m_SelMatColorSelDlg.exec() == QDialog::Rejected)
994         {
995                 *col = src;
996                 SetColorPalleteBG(col_widget, dc);
997         }
998 }
999
1000
1001 void FormMain::CenteringSplitter(void)
1002 {
1003         int w = ui.splitter->width();
1004         QList<int> s;
1005         s.append(w / 2);
1006         s.append(w / 2);
1007         ui.splitter->setSizes(s);
1008 }
1009
1010 void FormMain::updateView_All(void)
1011 {
1012         updateView_3D();
1013         updateView_2D();
1014 }
1015
1016 void FormMain::updateView_2D(void)
1017 {
1018         ui.GLWidgetUV->update();
1019 }
1020
1021 void FormMain::updateView_3D(void)
1022 {
1023         ui.GLWidgetMain->update();
1024 }
1025
1026 void FormMain::on_actionOpen_triggered()
1027 {
1028         FileDlgFilterList exts;
1029         exts.Add( "Wavefront"   , "obj" );
1030         exts.Add( "STL"         , "stl" );
1031         exts.Add( "PLY"         , "ply" );
1032         exts.Add( "Metasequoia" , "mqo" );
1033         exts.Add( "Pmd"         , "pmd" );
1034         exts.Add( "Collada"     , "dae" );
1035         exts.Add( "DirectX"     , "x"   );
1036
1037         QString filter = FileDlgUtil::ExtListToDlgFilter("Geometry", exts);
1038         QString title = "Open file";
1039         QString fileName = GetFilePathFromOpenDlg(title, filter);
1040
1041         if (fileName.isEmpty())
1042                 return;
1043
1044         if (IsResetSceneOnBeforeLoadFile())
1045                 ClearAllObjects();
1046
1047         if (OpenGeomFileToLast(fileName))
1048                 return;
1049 }
1050
1051 void FormMain::on_actionExit_triggered()
1052 {
1053         close();
1054 }
1055
1056 void FormMain::actionVisibleStates_Triggered(bool checked)
1057 {
1058         QAction* a = dynamic_cast<QAction*>(QObject::sender());
1059         if (checked)
1060         {
1061                 if (a == ui.actionIndexColorMode)
1062                 {
1063                         ui.actionFixMaterialSilver->setChecked(false);
1064                         ui.actionFBMaterial->setChecked(false);
1065                 }
1066                 else if (a == ui.actionFixMaterialSilver)
1067                 {
1068                         ui.actionIndexColorMode->setChecked(false);
1069                         ui.actionFBMaterial->setChecked(false);
1070                 }
1071                 else if (a == ui.actionFBMaterial)
1072                 {
1073                         ui.actionFixMaterialSilver->setChecked(false);
1074                         ui.actionIndexColorMode->setChecked(false);
1075                 }
1076         }
1077
1078         SyncViewSettingsFromGUI();
1079         updateView_All();
1080 }
1081
1082 void FormMain::SyncViewSettingsFromGUI(void)
1083 {
1084         m_Binder_ViewConfig.UpdateAllData();
1085
1086         QAction* menu = GetMenuFromShader(m_View3d.m_Config.m_ShaderMode);
1087         if (menu != NULL)
1088                 menu->setChecked(true);
1089 }
1090
1091 QAction* FormMain::GetMenuFromShader(ShaderType type)
1092 {
1093         switch(m_View3d.m_Config.m_ShaderMode)
1094         {
1095         case ShaderType::Phong      : return ui.actionShaderPhong;
1096         case ShaderType::Custom     : return ui.actionShaderCustom;
1097         case ShaderType::NormalColor: return ui.actionNormalColor;
1098         case ShaderType::DepthColor : return ui.actionDepthColor;
1099         case ShaderType::Shadowmap  : return ui.actionShadowmap;
1100         case ShaderType::Envmap     : return ui.actionEnvmap;
1101         case ShaderType::Integrate  : return ui.actionIntegrateShader;
1102         case ShaderType::Matcap     : return ui.actionMatcapShader;
1103         case ShaderType::Default    : return ui.actionShaderDefault;
1104
1105         default:
1106                 assert(false);
1107                 return NULL;
1108         };
1109 }
1110
1111 bool FormMain::SaveWindowLayout(const QString& filepath)
1112 {
1113         WindowConfig config;
1114
1115         config.m_MainWinLeft   = geometry().x();
1116         config.m_MainWinTop    = geometry().y();
1117         config.m_MainWinWidth  = geometry().width();
1118         config.m_MainWinHeight = geometry().height();
1119         config.m_IsMaximized   = isMaximized();
1120
1121         return config.SaveConfig(filepath.toLocal8Bit().data());
1122 }
1123
1124 bool FormMain::LoadWindowLayout(const QString& filepath)
1125 {
1126         WindowConfig config;
1127         if (!config.LoadConfig(filepath.toLocal8Bit().data()))
1128                 return false;
1129
1130         if (config.m_IsMaximized)
1131                 showMaximized();
1132
1133         int l = config.m_MainWinLeft;
1134         int t = config.m_MainWinTop;
1135         int w = config.m_MainWinWidth;
1136         int h = config.m_MainWinHeight;
1137         setGeometry(l, t, w, h);
1138
1139         return true;
1140 }
1141
1142 void FormMain::actionVisibleStatesUV_Triggered(bool)
1143 {
1144         m_Binder_UVConfig.UpdateAllData();
1145         updateView_All();
1146 }
1147
1148 void FormMain::actionVisibleStatesTex_Triggered(bool)
1149 {
1150         m_Binder_TexConfig.UpdateAllData();
1151         updateView_All();
1152 }
1153
1154 void FormMain::actionSceneStates_Toggled(bool)
1155 {
1156         m_Binder_Scene.UpdateAllData();
1157         updateView_All();
1158 }
1159
1160 void FormMain::on_actionWindow_ToolBarView_triggered(bool checked)
1161 {
1162         ui.toolBar_View->setVisible( ui.actionWindow_ToolBarView->isChecked() );
1163 }
1164
1165 void FormMain::on_actionToolBar_Options_toggled(bool checked)
1166 {
1167         ui.toolBar_Option->setVisible(checked);
1168 }
1169
1170 void FormMain::on_actionToolBar_Operation_toggled(bool checked)
1171 {
1172         ui.toolBar_Operation->setVisible(checked);
1173 }
1174
1175 void FormMain::on_actionCameraDist_triggered()
1176 {
1177         m_View3d.AdjustCameraDistAuto();
1178 }
1179
1180 void FormMain::on_actionCameraFront_triggered()
1181 {
1182         m_View3d.MoveCaemraTo(ViewPoint::Front);
1183 }
1184
1185 void FormMain::on_actionCameraBack_triggered()
1186 {
1187         m_View3d.MoveCaemraTo(ViewPoint::Back);
1188 }
1189
1190 void FormMain::on_actionCameraLeft_triggered()
1191 {
1192         m_View3d.MoveCaemraTo(ViewPoint::Left);
1193 }
1194
1195 void FormMain::on_actionCameraRight_triggered()
1196 {
1197         m_View3d.MoveCaemraTo(ViewPoint::Right);
1198 }
1199
1200 void FormMain::on_actionCameraTop_triggered()
1201 {
1202         m_View3d.MoveCaemraTo(ViewPoint::Top);
1203 }
1204
1205 void FormMain::on_actionCameraBottom_triggered()
1206 {
1207         m_View3d.MoveCaemraTo(ViewPoint::Bottom);
1208 }
1209
1210 void FormMain::on_actionCameraPers_triggered()
1211 {
1212         m_View3d.MoveCaemraTo(ViewPoint::Perse);
1213 }
1214
1215 void FormMain::on_actionCameraLookOrigin_triggered()
1216 {
1217         m_View3d.MoveLookPosToOrigin();
1218 }
1219
1220 void FormMain::on_actionCameraLookCenter_triggered()
1221 {
1222         m_View3d.MoveLookPosToCenter();
1223 }
1224
1225 void FormMain::on_actionWindow_3DView_triggered(bool checked)
1226 {
1227         UpdateViewTypeFromMenu();
1228 }
1229
1230 void FormMain::on_actionWindows_DualView_triggered(bool checked)
1231 {
1232         UpdateViewTypeFromMenu();
1233 }
1234
1235 void FormMain::on_actionWindow_UVView_triggered(bool checked)
1236 {
1237         UpdateViewTypeFromMenu();
1238 }
1239
1240 void FormMain::UpdateViewTypeFromMenu(void)
1241 {
1242         if (ui.actionWindows_DualView->isChecked())
1243         {
1244                 ui.frameView3D->setVisible(true);
1245                 ui.frameView2D->setVisible(true);
1246
1247                 // TODO: \89¼. \8bN\93®\8e\9e\82É\83r\83\85\81[\82ð1:1\82É\90Ý\92è\82Å\82«\82È\82¢\96â\91è\82Ì\89¼\91Î\89\9e.
1248                 static bool firsttime = true;
1249                 if (firsttime)
1250                 {
1251                         firsttime = false;
1252                         CenteringSplitter();
1253                 }
1254         }
1255         if (ui.actionWindow_3DView->isChecked())
1256         {
1257                 ui.frameView3D->setVisible(true);
1258                 ui.frameView2D->setVisible(false);
1259         }
1260         if (ui.actionWindow_UVView->isChecked())
1261         {
1262                 ui.frameView3D->setVisible(false);
1263                 ui.frameView2D->setVisible(true);
1264         }
1265 }
1266
1267 void FormMain::on_actionWindowMaterialList_triggered()
1268 {
1269         ShowAndActivateAndRaise(ui.dockMaterial);
1270         ui.listMaterial->setFocus();
1271 }
1272
1273 void FormMain::on_actionWindowObjectList_triggered()
1274 {
1275         ShowAndActivateAndRaise(ui.dockObject);
1276         ui.widgetObjectList->focusList();
1277 }
1278
1279 void FormMain::on_actionWindowCameraList_triggered()
1280 {
1281         ShowAndActivateAndRaise(ui.dockCamera);
1282         ui.widgetCamera->focusList();
1283 }
1284
1285 void FormMain::on_actionWindowScenePanel_triggered()
1286 {
1287         ShowAndActivateAndRaise(ui.dockScene);
1288 }
1289
1290 void FormMain::on_actionCursorPanel_triggered()
1291 {
1292         ShowAndActivateAndRaise(ui.dockCursor);
1293 }
1294
1295 void FormMain::on_actionIOOptionPanel_triggered()
1296 {
1297         ShowAndActivateAndRaise(ui.dockIOOption);
1298 }
1299
1300 void FormMain::on_listMaterial_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
1301 {
1302         MeshBuf* mbuf = m_Scene.GetSelOrFirstMeshbuf();
1303         if (mbuf == NULL)
1304                 return;
1305
1306         int idx = ui.listMaterial->currentRow();
1307         mbuf->SetSelMatIdx(idx);
1308         OnChangedSelectedMaterial();
1309         updateView_All();
1310 }
1311
1312 void FormMain::ShowAndActivateAndRaise(QDockWidget* widget)
1313 {
1314         widget->setVisible(true);
1315         widget->activateWindow();
1316         widget->raise();
1317 }
1318
1319 void FormMain::OnChangedSelectedMaterial(void)
1320 {
1321         if (IsAutUVFit())
1322         {
1323                 m_View2d.FitView();
1324         }
1325
1326         ui.editCurrentTexName->clear();
1327
1328         GeomTextureSet* ts = m_Scene.GetSelectedTexture();
1329         if (ts == NULL)
1330         {
1331                 ui.editCurrentTexName->setText("");
1332                 ui.editMatCapEachMaterial->setText("");
1333         }
1334         else
1335         {
1336                 QString name_c, name_n;
1337
1338                 gl::GlTexture* tex_c = ts->GetTexColor();
1339                 if (tex_c != NULL)
1340                         name_c = QString::fromLocal8Bit(tex_c->GetName().c_str());
1341
1342                 gl::GlTexture* tex_n = ts->GetTexNormal();
1343                 if (tex_n != NULL)
1344                         name_n = QString::fromLocal8Bit(tex_n->GetName().c_str());
1345
1346                 ui.editCurrentTexName->setText(name_c);
1347                 ui.editCurrentMatNormalMap->setText(name_n);
1348
1349                 MatcapImage& matcap = ts->TexMatcap;
1350                 ui.editMatCapEachMaterial->setText(matcap.GetName());
1351         }
1352
1353         lib_geo::BaseMaterial* mat = m_Scene.GetSelectedMaterial();
1354         if (mat != NULL)
1355         {
1356                 SetColorPalleteBG(ui.colorAmbient  , ToQColor(mat->m_Ambient));
1357                 SetColorPalleteBG(ui.colorEmission , ToQColor(mat->m_Emission));
1358                 SetColorPalleteBG(ui.colorDiffuse  , ToQColor(mat->m_Diffuse));
1359                 SetColorPalleteBG(ui.colorSpecular , ToQColor(mat->m_Specular));
1360                 SetMatShininessSliderPos(mat->m_Shininess);
1361         }
1362         else
1363         {
1364                 QColor w(255, 255, 255);
1365                 SetColorPalleteBG( ui.colorAmbient  , w );
1366                 SetColorPalleteBG( ui.colorEmission , w );
1367                 SetColorPalleteBG( ui.colorDiffuse  , w );
1368                 SetColorPalleteBG( ui.colorSpecular , w );
1369                 SetMatShininessSliderPos(0);
1370         }
1371
1372         updateView_All();
1373 }
1374
1375 void FormMain::SetMatShininessSliderPos(float shininess)
1376 {
1377         int val_max = ui.sliderShininess->maximum();
1378         int slider_pos = (int)(shininess * (float)val_max / 200.0f);
1379         slider_pos = (lm::min)(val_max, slider_pos);
1380         ui.sliderShininess->blockSignals(true);
1381         ui.sliderShininess->setValue(slider_pos);
1382         ui.sliderShininess->blockSignals(false);
1383         ui.labelShininessVal->setText(QString::number(shininess, 'f', 2));
1384 }
1385
1386 void FormMain::keyPressEvent(QKeyEvent *e)
1387 {
1388         Modifier m(e);
1389         if (m.Flag_C() && e->key() == Qt::Key_Tab)
1390         {
1391                 FocusNextSubDlg();
1392         }
1393 }
1394
1395 void FormMain::FocusNextSubDlg(void)
1396 {
1397         std::vector<QDockWidget*> widgets;
1398
1399         if (ui.dockObject   ->isVisible()) widgets.push_back( ui.dockObject   );
1400         if (ui.dockCamera   ->isVisible()) widgets.push_back( ui.dockCamera   );
1401         if (ui.dockScene    ->isVisible()) widgets.push_back( ui.dockScene    );
1402         if (ui.dockMaterial ->isVisible()) widgets.push_back( ui.dockMaterial );
1403
1404         if (widgets.empty())
1405                 return;
1406
1407         QDockWidget* next_widget = NULL;
1408
1409         for (size_t i = 0; i < widgets.size(); ++i)
1410         {
1411                 if (widgets[i] == m_LastFocusedDockDlg)
1412                 {
1413                         next_widget = widgets[(i + 1) % widgets.size()];
1414                         break;
1415                 }
1416         }
1417
1418         if (next_widget == NULL)
1419                 next_widget = widgets.front();
1420
1421         m_LastFocusedDockDlg = next_widget;
1422
1423         next_widget->activateWindow();
1424         next_widget->raise();
1425         next_widget->setFocus();
1426 }
1427
1428 void FormMain::dragEnterEvent(QDragEnterEvent* e)
1429 {
1430         if (e->mimeData()->hasUrls())
1431                 e->acceptProposedAction();
1432 }
1433
1434 void FormMain::dropEvent(QDropEvent* e)
1435 {
1436         if (e->mimeData()->hasUrls())
1437         {
1438                 QList<QUrl> urls = e->mimeData()->urls();
1439
1440                 std::vector<QString> geom_files;
1441
1442                 for (QUrl& url : urls)
1443                 {
1444                         QString path = url.toLocalFile();
1445                         if (FormatType::IsGeomFileExt(path))
1446                                 geom_files.push_back(path);
1447                 }
1448
1449                 if (!geom_files.empty())
1450                 {
1451                         if (IsResetSceneOnBeforeLoadFile())
1452                                 ClearAllObjects();
1453
1454                         for (QString& f : geom_files)
1455                         {
1456                                 if (OpenGeomFileToLast(f))
1457                                         continue;
1458                         }
1459                 }
1460
1461                 for (QUrl& url : urls)
1462                 {
1463                         QString path = url.toLocalFile();
1464
1465                         if (IsSupportedTextureExt(path))
1466                         {
1467                                 DropTextureFile(path, e->pos());
1468                                 continue;
1469                         }
1470
1471                         if (IsCameraFile(path))
1472                         {
1473                                 ui.widgetCamera->OpenCameraFile(path);
1474                                 continue;
1475                         }
1476                 }
1477         }
1478 }
1479
1480 bool FormMain::IsResetSceneOnBeforeLoadFile(void) const
1481 {
1482         if (QApplication::keyboardModifiers() & Qt::ShiftModifier)
1483                 return true;
1484
1485         return false;
1486 }
1487
1488 //! \83I\83u\83W\83F\83N\83g\82É\89æ\91\9c\82ð\83h\83\8d\83b\83v\82³\82ê\82½\8fê\8d\87\82Í\82»\82Ì\83}\83e\83\8a\83A\83\8b\82Ì\83J\83\89\81[\83e\83N\83X\83`\83\83\82ð\83Z\83b\83g
1489 //! \89½\82à\96³\82¢\8fê\8f\8a\82É\83h\83\8d\83b\83v\82³\82ê\82é\82Æ\83f\83t\83H\83\8b\83g\83e\83N\83X\83`\83\83\82ð\83Z\83b\83g
1490 void FormMain::DropTextureFile(const QString& path, const QPoint& pos)
1491 {
1492         std::string fname = path.toLocal8Bit().data();
1493
1494         QPoint pl = ui.GLWidgetMain->mapFrom(this, pos);
1495
1496         PickedMeshSubface picked;
1497         m_View3d.BeginRender();
1498         bool suc = m_View3d.PickFace2D(picked, pl.x(), pl.y());
1499         m_View3d.EndRender();
1500
1501         if (suc)
1502         {
1503                 m_View3d.BeginRender();
1504                 MeshBuf* mbuf = picked.mbuf;
1505                 lib_geo::BaseFace& f = mbuf->m_Mesh.m_Faces[picked.subf.fid];
1506                 GeomTextureSet* ts = mbuf->GetTexture(f.m_MatIdx);
1507                 if (ts != NULL)
1508                 {
1509                         std::string fp = path.toLocal8Bit().data();
1510
1511                         QString name_qs = QFileInfo(path).fileName();
1512                         std::string name = name_qs.toLocal8Bit().data();
1513
1514                         mbuf->ReleaseTextureUnit(ts, TextureType::Color);
1515                         gl::GlTexture* tex = mbuf->GetInitTexture(fp, name, m_Scene.m_TexConfig);
1516                         ts->SetTextureUnit(TextureType::Color, tex);
1517                 }
1518                 m_View3d.EndRender();
1519         }
1520         else
1521         {
1522                 m_ContextShare.BeginDrawTop();
1523                 m_Scene.LoadDefaultMatTexture(fname);
1524                 m_ContextShare.EndDrawTop();
1525         }
1526
1527         updateView_All();
1528 }
1529
1530 bool FormMain::OpenGeomFileToLast(const QString& path)
1531 {
1532         ui.widgetIOOption->getConfig(m_Scene.m_IOConfig);
1533
1534         m_ContextShare.BeginDrawTop();
1535
1536         bool suc = true;
1537         try
1538         {
1539                 m_Scene.ImportFileAutoFmt(path);
1540         }
1541         catch(const FileLoadErrorException& ex)
1542         {
1543                 QString msg;
1544                 msg += "Failed Load ";
1545                 msg += path + "\n\n";
1546                 msg += QString::fromLocal8Bit(ex.what());
1547
1548                 QMessageBox::warning(this, "", msg);
1549                 suc = false;
1550         }
1551
1552         m_ContextShare.EndDrawTop();
1553
1554         if (suc)
1555                 ui.widgetObjectList->OnDoneAddGeom();
1556
1557         ResetSequenceSliderRange();
1558
1559         updateView_All();
1560
1561         if (!suc)
1562                 return false;
1563
1564         AddRecentFile(path);
1565
1566         return true;
1567 }
1568
1569 void FormMain::ClearAllObjects(void)
1570 {
1571         m_ContextShare.BeginDrawTop();
1572
1573         m_Scene.ClearObjects();
1574
1575         m_ContextShare.EndDrawTop();
1576
1577         m_View3d.ClearRenderMap();
1578
1579         ui.widgetObjectList->ClearTree();
1580         RefreshObjectSelectState();
1581
1582         updateView_All();
1583 }
1584
1585
1586 bool FormMain::IsCameraFile(const QString& path) const
1587 {
1588         QFileInfo fInfo( path );
1589         QString ext = fInfo.suffix().toLower();
1590         return (ext == "camera");
1591 }
1592
1593
1594 void FormMain::actionGeomStates_Triggered(bool)
1595 {
1596         ApplyGeomStateFromGUI();
1597
1598         m_Scene.UpdateTransform();
1599
1600         updateView_All();
1601 }
1602
1603 void FormMain::ApplyGeomStateFromGUI(void)
1604 {
1605         m_Scene.m_Config.m_EnableAutoCentering = ui.actionCentering->isChecked();
1606         m_Scene.m_Config.m_EnableAutoReisze    = ui.actionAutoResize->isChecked();
1607 }
1608
1609 void FormMain::ApplyGeomStateDataoToGUI(void)
1610 {
1611         ui.actionAutoResize ->setChecked( m_Scene.m_Config.m_EnableAutoReisze    );
1612         ui.actionCentering  ->setChecked( m_Scene.m_Config.m_EnableAutoCentering );
1613
1614         ui.widgetObjectList->applyConfig();
1615 }
1616
1617 void FormMain::on_actionGeomClear_triggered()
1618 {
1619         ClearAllObjects();
1620 }
1621
1622
1623 void FormMain::on_actionUVFitView_triggered()
1624 {
1625         m_View2d.FitView();
1626         updateView_All();
1627 }
1628
1629 void FormMain::on_actionSelObjectDelete_triggered()
1630 {
1631         DeleteSelectedObject();
1632 }
1633
1634 void FormMain::RebuildAllGLBuf(void)
1635 {
1636         ui.GLWidgetMain->makeCurrent();
1637         std::vector<MeshBuf*> mv = m_Scene.GetCurSelMeshes();
1638         for (MeshBuf* m : mv)
1639         {
1640                 m_View3d.ReleaseRenderbuffer(m);
1641         }
1642         ui.GLWidgetMain->doneCurrent();
1643
1644         updateView_All();
1645 }
1646
1647 void FormMain::on_actionShowOnlySelected_triggered()
1648 {
1649         ShowObjectOnlySelected();
1650 }
1651
1652
1653 void FormMain::on_actionAddSample_triggered()
1654 {
1655         SampleShapeBuilder::CreateSphere(m_Scene);
1656         ui.widgetObjectList->OnDoneAddGeom();
1657
1658         ResetSequenceSliderRange();
1659
1660         updateView_All();
1661 }
1662
1663 void FormMain::on_actionAddGroundPlane_triggered()
1664 {
1665         SampleShapeBuilder::CreateGroundPlane(m_Scene);
1666         ui.widgetObjectList->OnDoneAddGeom();
1667
1668         ResetSequenceSliderRange();
1669
1670         updateView_All();
1671 }
1672
1673 void FormMain::on_actionShaderDefault_triggered(bool)
1674 {
1675         ChangeShaderMenuMain(ShaderType::Default);
1676 }
1677
1678 void FormMain::on_actionShaderPhong_triggered(bool)
1679 {
1680         ChangeShaderMenuMain(ShaderType::Phong);
1681 }
1682
1683 void FormMain::on_actionShaderCustom_triggered(bool)
1684 {
1685         ChangeShaderMenuMain(ShaderType::Custom);
1686 }
1687
1688 void FormMain::on_actionNormalColor_triggered(bool)
1689 {
1690         ChangeShaderMenuMain(ShaderType::NormalColor);
1691 }
1692
1693 void FormMain::on_actionDepthColor_triggered(bool)
1694 {
1695         ChangeShaderMenuMain(ShaderType::DepthColor);
1696 }
1697
1698 void FormMain::on_actionShadowmap_triggered(bool)
1699 {
1700         ChangeShaderMenuMain(ShaderType::Shadowmap);
1701 }
1702
1703 void FormMain::on_actionEnvmap_triggered(bool)
1704 {
1705         ChangeShaderMenuMain(ShaderType::Envmap);
1706 }
1707
1708 void FormMain::on_actionIntegrateShader_triggered(bool)
1709 {
1710         ChangeShaderMenuMain(ShaderType::Integrate);
1711 }
1712
1713 void FormMain::on_actionMatcapShader_triggered(bool)
1714 {
1715         ChangeShaderMenuMain(ShaderType::Matcap);
1716 }
1717
1718 void FormMain::SyncShaderSettingsToGUI(void)
1719 {
1720         ChangeShaderMenuMain(m_View3d.m_Config.m_ShaderMode);
1721 }
1722
1723 void FormMain::ChangeShaderMenuMain(ShaderType type)
1724 {
1725         m_View3d.m_Config.m_ShaderMode = type;
1726         updateView_All();
1727 }
1728
1729 void FormMain::CreateSampleTextureMain(SampleTextureBuilder::TextureType tex_type)
1730 {
1731         m_ContextShare.BeginDrawTop();
1732         m_Scene.CreateSampleTexture(tex_type);
1733         m_ContextShare.EndDrawTop();
1734
1735         updateView_All();
1736 }
1737
1738 bool FormMain::IsAutUVFit(void) const
1739 {
1740         return ui.actionUVAutoFit->isChecked();
1741 }
1742
1743
1744 void FormMain::RefreshObjectSelectState(void)
1745 {
1746         ui.widgetObjectList->SetPrimayrSelectObjectToScene();
1747
1748         RebuildMatList();
1749
1750         updateView_All();
1751 }
1752
1753 void FormMain::RebuildMatList(void)
1754 {
1755         MeshBuf* mbuf = m_Scene.GetSelOrFirstMeshbuf();
1756
1757         QListWidget* list_mat = ui.listMaterial;
1758
1759         list_mat->blockSignals(true);
1760         list_mat->clear();
1761
1762         if (mbuf != NULL)
1763         {
1764                 const lib_geo::BaseMesh& mesh = mbuf->m_Mesh;
1765                 for (const lib_geo::BaseMaterial& mat : mesh.m_Materials)
1766                 {
1767                         const std::string& name = mat.m_Name;
1768
1769                         if (name.empty())
1770                                 list_mat->addItem("--");
1771                         else
1772                                 list_mat->addItem(QString::fromLocal8Bit(name.c_str()));
1773                 }
1774         }
1775
1776         list_mat->blockSignals(false);
1777
1778         if (mbuf != NULL)
1779         {
1780                 mbuf->SelectMatAuto();
1781                 list_mat->setCurrentRow(mbuf->GetSelMatIdx());
1782         }
1783         else
1784         {
1785                 list_mat->setCurrentRow(-1);
1786         }
1787
1788         OnChangedSelectedMaterial();
1789 }
1790
1791
1792 void FormMain::DeleteSelectedObject()
1793 {
1794         int sel_idx = ui.widgetObjectList->getSelObjIdx();
1795         if (sel_idx < 0)
1796                 return;
1797
1798         m_ContextShare.BeginDrawTop();
1799
1800         m_Scene.RemoveItem(sel_idx);
1801
1802         m_ContextShare.EndDrawTop();
1803
1804         m_View3d.ClearRenderMap();
1805
1806         ui.widgetObjectList->removeObject(sel_idx);
1807
1808         int sel_idx2 = ui.widgetObjectList->getSelObjIdxWhenSelObjNode();
1809
1810         m_Scene.m_Sels.SelectObject(sel_idx2);
1811
1812         m_Scene.UpdateTransform();
1813
1814         RefreshObjectSelectState();
1815
1816         ResetSequenceSliderRange();
1817
1818         updateView_All();
1819 }
1820
1821 void FormMain::ShowObjectOnlySelected(void)
1822 {
1823         GeomObject* sel_obj = m_Scene.GetPrimaryObject();
1824         if (sel_obj == NULL)
1825                 return;
1826
1827         for (GeomObject& o : m_Scene.m_Objects)
1828         {
1829                 o.m_Visible = (sel_obj == &o);
1830         }
1831
1832         ui.widgetObjectList->SyncViewMark();
1833         updateView_All();
1834 }
1835
1836 void FormMain::on_actionUVResetView_triggered()
1837 {
1838         m_View2d.ResetView();
1839
1840         updateView_All();
1841 }
1842
1843
1844 //! \83\8f\83C\83\84\81[\83I\81[\83o\81[\83\8c\83C\95\\8e¦\8e\9e\82Ì\90F\90Ý\92è
1845 void FormMain::on_actionWireColor_triggered()
1846 {
1847         m_WireColorSelDlg.exec();
1848 }
1849
1850 //! \83f\83t\83H\83\8b\83g\83}\83e\83\8a\83A\83\8b\82Ì\90F\90Ý\92è
1851 void FormMain::on_actionFaceColor_triggered()
1852 {
1853         const lgr::color4f& c = m_Scene.m_DefaultMaterial.m_Diffuse;
1854
1855         m_DiffuseColorSelDlg.setOption(QColorDialog::ShowAlphaChannel);
1856         m_DiffuseColorSelDlg.setCurrentColor(ToQColor(c));
1857         m_DiffuseColorSelDlg.exec();
1858 }
1859
1860 //! \94w\8ci\90F\82Ì\90Ý\92è
1861 void FormMain::on_actionBackGroundColor_triggered()
1862 {
1863         const lgr::color4f& c = m_View3d.m_BGColor;
1864
1865         m_BGColorSelDlg.setCurrentColor(ToQColor(c));
1866         m_BGColorSelDlg.exec();
1867 }
1868
1869 void FormMain::WireOverlayColorChanged(const QColor & color)
1870 {
1871         lgr::color3b& c = m_View3d.m_Config.m_WireColor;
1872         c.set(color.red(), color.green(), color.blue());
1873
1874         m_View3d.ReleaseAllRenderBuffer();
1875
1876         updateView_All();
1877 }
1878
1879 void FormMain::CopyRGB(lgr::color4f& c, const QColor & color)
1880 {
1881         c.r() = (float)color.red()   / 255.0f;
1882         c.g() = (float)color.green() / 255.0f;
1883         c.b() = (float)color.blue()  / 255.0f;
1884 }
1885
1886 void FormMain::CopyRGBA(lgr::color4f& c, const QColor & color)
1887 {
1888         c.r() = (float)color.red()   / 255.0f;
1889         c.g() = (float)color.green() / 255.0f;
1890         c.b() = (float)color.blue()  / 255.0f;
1891         c.a() = (float)color.alpha() / 255.0f;
1892 }
1893
1894 void FormMain::FaceDiffuseColorChanged(const QColor & color)
1895 {
1896         lgr::color4f& c = m_Scene.m_DefaultMaterial.m_Diffuse;
1897         CopyRGB(c, color);
1898
1899         m_View3d.ReleaseAllRenderBuffer();
1900         updateView_All();
1901 }
1902
1903 void FormMain::BGColorChanged(const QColor & color)
1904 {
1905         lgr::color4f& c = m_View3d.m_BGColor;
1906         CopyRGB(c, color);
1907
1908         updateView_All();
1909 }
1910
1911 void FormMain::SetSelectedMaterialColor(QColor col)
1912 {
1913         lib_geo::BaseMaterial* mat = m_Scene.GetSelectedMaterial();
1914         if (mat == NULL)
1915                 return;
1916
1917         if (m_SelMatColorWidget == ui.colorAmbient  ) CopyRGBA(mat->m_Ambient  , col);
1918         if (m_SelMatColorWidget == ui.colorEmission ) CopyRGBA(mat->m_Emission , col);
1919         if (m_SelMatColorWidget == ui.colorDiffuse  ) CopyRGBA(mat->m_Diffuse  , col);
1920         if (m_SelMatColorWidget == ui.colorSpecular ) CopyRGBA(mat->m_Specular , col);
1921
1922         SetColorPalleteBG(m_SelMatColorWidget, col);
1923 }
1924
1925 void FormMain::SelMatColorChanged(const QColor & color)
1926 {
1927         SetSelectedMaterialColor(color);
1928         m_View3d.ResetPrimaryObjectLists();
1929         updateView_All();
1930 }
1931
1932
1933 void FormMain::on_actionWindowOptions_triggered()
1934 {
1935         m_ViewConfigDlg.showNormal();
1936         m_ViewConfigDlg.show();
1937         m_ViewConfigDlg.activateWindow();
1938 }
1939
1940 void FormMain::on_actionWindowCustomShader_triggered()
1941 {
1942         if (m_CustomShaderDlg == NULL)
1943         {
1944                 m_CustomShaderDlg = new FormCustomShader(this);
1945                 m_CustomShaderDlg->SetShaderLibrary(&m_View3d.m_ShaderLib);
1946                 m_CustomShaderDlg->SetParentWidget(ui.GLWidgetMain);
1947                 m_CustomShaderDlg->LoadOrSetDefaultCode();
1948                 connect(
1949                         m_CustomShaderDlg,
1950                         SIGNAL(ShaderBuild()),
1951                         this,
1952                         SLOT(CustomShaderDlg_ShaderBuild()));
1953         }
1954
1955         m_CustomShaderDlg->showNormal();
1956         m_CustomShaderDlg->show();
1957         m_CustomShaderDlg->activateWindow();
1958 }
1959
1960
1961 void FormMain::ConfigChangedDlg_ConfigChanged()
1962 {
1963         m_View3d.ReleaseAllRenderBuffer();
1964         updateView_All();
1965 }
1966
1967 void FormMain::CrossSectionDlg_ConfigChanged()
1968 {
1969         updateView_All();
1970 }
1971
1972
1973 void FormMain::OnPaintImage(QPrinter *printer)
1974 {
1975         QPainter painter(printer);
1976
1977         QRect rect = painter.viewport();
1978         QSize size = this->size();
1979         size.scale(rect.size(), Qt::KeepAspectRatio);//\8fc\89¡\94ä\88Û\8e\9d
1980
1981         painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
1982         painter.setWindow(this->rect());
1983
1984         this->render(&painter);
1985 }
1986
1987 void FormMain::on_actionPrint_triggered()
1988 {
1989         PrintImage(false);
1990 }
1991
1992 void FormMain::on_actionPrintPreview_triggered()
1993 {
1994         PrintImage(true);
1995 }
1996
1997 void FormMain::PrintImage(bool WithPreview)
1998 {
1999         //QPrinter printer(QPrinter::ScreenResolution);
2000         QPrinter printer(QPrinter::ScreenResolution);
2001
2002         if(WithPreview)
2003         {
2004                 QPrintPreviewDialog preview(&printer, this);
2005                 preview.setWindowFlags( Qt::Window );
2006                 connect(
2007                         &preview,
2008                         SIGNAL(paintRequested(QPrinter *)),
2009                         SLOT(OnPaintImage(QPrinter *)));
2010
2011                 preview.showMaximized();
2012                 preview.exec();
2013         }
2014         else
2015         {
2016                 QPrintDialog printDialog(&printer, this);
2017                 if (printDialog.exec() == QDialog::Accepted)
2018                         OnPaintImage(&printer);
2019         }
2020 }
2021
2022
2023 void FormMain::on_actionSaveCamera_triggered()
2024 {
2025         QString title = "camera";
2026         QString filter = "Camera File(*.camera)";
2027         QString fileName = GetFilePathFromSaveDlg(title, filter);
2028         if (fileName.isEmpty())
2029                 return;
2030
2031         m_View3d.m_CameraRecord.SaveCamera(fileName.toLocal8Bit().data());
2032 }
2033
2034 void FormMain::on_actionProjectionOrtho_triggered()
2035 {
2036         ui.actionProjectionOrtho->setChecked(true);
2037         ui.actionProjectionPers->setChecked(false);
2038
2039         m_Scene.m_Camera.m_ProjMode = gl::Camera::PROJ_ORTHO;
2040
2041         updateView_All();
2042 }
2043
2044 void FormMain::on_actionProjectionPers_triggered()
2045 {
2046         ui.actionProjectionPers->setChecked(true);
2047         ui.actionProjectionOrtho->setChecked(false);
2048
2049         m_Scene.m_Camera.m_ProjMode = gl::Camera::PROJ_PERS;
2050
2051         updateView_All();
2052 }
2053
2054
2055 void FormMain::CustomShaderDlg_ShaderBuild()
2056 {
2057         updateView_All();
2058 }
2059
2060 void FormMain::OnAnimationTimerUpdated()
2061 {
2062         static const float rot_speed = 0.005f;
2063
2064         if (ui.actionRotateCamera->isChecked())
2065         {
2066                 m_Scene.m_Camera.m_Manip.Tumble(rot_speed, 0.0f);
2067                 UpdateCameraStatus();
2068         }
2069
2070         if (ui.actionRotateLight->isChecked())
2071                 m_View3d.m_ControlLight.m_Position.rotate_y(rot_speed);
2072
2073         if (ui.actionAnimation->isChecked())
2074         {
2075                 StepSequence(1);
2076                 if (IsLastSequence())
2077                         ui.actionAnimation->setChecked(false);
2078         }
2079         else if (ui.actionAnimationRev->isChecked())
2080         {
2081                 StepSequence(-1);
2082                 if (IsTopSequence())
2083                         ui.actionAnimationRev->setChecked(false);
2084         }
2085
2086         updateView_3D();
2087 }
2088
2089 void FormMain::on_actionRotateCamera_toggled(bool checked)
2090 {
2091         UpdateAnimState();
2092 }
2093
2094 void FormMain::on_actionRotateLight_toggled(bool checked)
2095 {
2096         UpdateAnimState();
2097 }
2098
2099 void FormMain::on_actionAnimation_toggled(bool checked)
2100 {
2101         if (checked)
2102         {
2103                 ui.actionAnimationRev->setChecked(false);
2104                 if (IsLastSequence())
2105                 {
2106                         QSlider* s = ui.sliderSequence;
2107                         s->setValue(0);
2108                 }
2109         }
2110
2111         ui.butonAnimationFwd->blockSignals(true);
2112         ui.butonAnimationFwd->setChecked(checked);
2113         ui.butonAnimationFwd->blockSignals(false);
2114
2115         UpdateAnimState();
2116 }
2117
2118 void FormMain::on_actionAnimationRev_toggled(bool checked)
2119 {
2120         if (checked)
2121         {
2122                 ui.actionAnimation->setChecked(false);
2123                 if (IsTopSequence())
2124                 {
2125                         QSlider* s = ui.sliderSequence;
2126                         s->setValue(s->maximum());
2127                 }
2128         }
2129
2130         ui.butonAnimationRev->blockSignals(true);
2131         ui.butonAnimationRev->setChecked(checked);
2132         ui.butonAnimationRev->blockSignals(false);
2133
2134         UpdateAnimState();
2135 }
2136
2137 void FormMain::on_butonAnimationFwd_toggled(bool checked)
2138 {
2139         ui.actionAnimation->setChecked(checked);
2140 }
2141
2142 void FormMain::on_butonAnimationRev_toggled(bool checked)
2143 {
2144         ui.actionAnimationRev->setChecked(checked);
2145 }
2146
2147 void FormMain::UpdateAnimState(void)
2148 {
2149         if (IsEnableAnimation())
2150                 m_AnimationTimer.start();
2151         else
2152                 m_AnimationTimer.stop();
2153 }
2154
2155 bool FormMain::IsEnableAnimation(void)
2156 {
2157         if (ui.actionRotateCamera->isChecked())
2158                 return true;
2159         if (ui.actionRotateLight->isChecked())
2160                 return true;
2161         if (ui.actionAnimation->isChecked())
2162                 return true;
2163         if (ui.actionAnimationRev->isChecked())
2164                 return true;
2165
2166         return false;
2167 }
2168
2169 void FormMain::on_actionCrossSectionDlg_triggered()
2170 {
2171         m_CrossSectionDlg.showNormal();
2172         m_CrossSectionDlg.show();
2173         m_CrossSectionDlg.activateWindow();
2174 }
2175
2176 void FormMain::on_actionSelectNext_triggered()
2177 {
2178         ui.widgetObjectList->AddSelectObjectIdx(1);
2179 }
2180
2181 void FormMain::on_actionSelectPrev_triggered()
2182 {
2183         ui.widgetObjectList->AddSelectObjectIdx(-1);
2184 }
2185
2186 void FormMain::on_actionSwitchVisible_triggered()
2187 {
2188         ui.widgetObjectList->FlipVisibleSelectedObject();
2189 }
2190
2191 void FormMain::on_actionHideAll_triggered()
2192 {
2193         m_Scene.HideAllObject();
2194
2195         ui.widgetObjectList->SyncViewMark();
2196         updateView_All();
2197 }
2198
2199 void FormMain::on_actionShowAll_triggered()
2200 {
2201         m_Scene.ShowAllObject();
2202
2203         ui.widgetObjectList->SyncViewMark();
2204         updateView_All();
2205 }
2206
2207 void FormMain::on_actionVertexBuilder_triggered()
2208 {
2209         FormVertexBuilder builder;
2210         builder.CreateVertex(m_Scene);
2211
2212         ui.widgetObjectList->RebuildObjectList();
2213         updateView_All();
2214 }
2215
2216 void FormMain::on_actionFullScreen_triggered()
2217 {
2218         m_FullscreenPanel.ShowWidgetAsFullscreen(ui.splitter, ui.gridLayout_4);
2219 }
2220
2221 void FormMain::on_actionResetConfig_triggered()
2222 {
2223         QString msg = QString::fromLocal8Bit("\90Ý\92è\82ð\83\8a\83Z\83b\83g\82µ\82Ü\82·");
2224         if (QMessageBox::question(this, "", msg, QMessageBox::Ok|QMessageBox::Cancel) != QMessageBox::Ok)
2225                 return;
2226
2227         setGeometry(m_InitRect);
2228
2229         LoadDefaultConfig();
2230
2231         SyncViewSettingsFromGUI();
2232         m_Binder_UVConfig.UpdateAllData();
2233         m_Binder_TexConfig.UpdateAllData();
2234         SyncTexConfigToData();
2235         ApplyGeomStateFromGUI();
2236         SyncShaderSettingsToGUI();
2237
2238         updateView_All();
2239 }
2240
2241 QString FormMain::GetFilePathFromOpenDlg(const QString& title, const QString& filter)
2242 {
2243         QString default_path = GetNextDefaultPathForFileDlg();
2244         QString s = QFileDialog::getOpenFileName(this, title, default_path, filter);
2245
2246         if (!s.isEmpty())
2247                 m_LastFileDialogDir = QFileInfo(s).absolutePath();
2248
2249         return s;
2250 }
2251
2252 QString FormMain::GetFilePathFromSaveDlg(const QString& title, const QString& filter)
2253 {
2254         QString default_path = GetNextDefaultPathForFileDlg();
2255         QString s = QFileDialog::getSaveFileName(this, title, default_path, filter);
2256
2257         if (!s.isEmpty())
2258                 m_LastFileDialogDir = QFileInfo(s).absolutePath();
2259
2260         return s;
2261 }
2262
2263 bool FormMain::IsSupportedTextureExt(const QString& path) const
2264 {
2265         QString ext = QFileInfo(path).suffix().toLower();
2266
2267         std::map<QString, bool> types;
2268         types[ "png"  ] = true;
2269         types[ "jpg"  ] = true;
2270         types[ "jepg" ] = true;
2271         types[ "bmp"  ] = true;
2272         types[ "tif"  ] = true;
2273         types[ "tiff" ] = true;
2274         types[ "spa"  ] = true;
2275
2276         return types[ext];
2277 }
2278
2279 QString FormMain::GetSupportedImageFilePathFromDlg(const QString& title)
2280 {
2281         FileDlgFilterList exts;
2282         exts.Add( "Png" , "png"  );
2283         exts.Add( "Jpg" , "jpg"  );
2284         exts.Add( "Jpg" , "jpeg" );
2285         exts.Add( "Bmp" , "bmp"  );
2286         exts.Add( "Tif" , "tif"  );
2287         exts.Add( "Tif" , "tiff" );
2288
2289         QString filter = FileDlgUtil::ExtListToDlgFilter("Image", exts);
2290         return GetFilePathFromOpenDlg(title, filter);
2291 }
2292
2293
2294 QString FormMain::GetNextDefaultPathForFileDlg(void)
2295 {
2296         if (m_LastFileDialogDir.isEmpty())
2297                 return PathInfo::GetMyDocDirPath();
2298
2299         return m_LastFileDialogDir;
2300 }
2301
2302
2303 void FormMain::on_toolLoadEnvMap_clicked()
2304 {
2305         QString fileName = GetSupportedImageFilePathFromDlg("Open envmap");
2306         if (fileName.isEmpty())
2307                 return;
2308
2309         LoadEnvMap(fileName);
2310 }
2311
2312 void FormMain::LoadEnvMap(QString& path)
2313 {
2314         m_ContextShare.BeginDrawTop();
2315         m_Scene.m_EnvImg.LoadTexture(path.toLocal8Bit().data());
2316         m_ContextShare.EndDrawTop();
2317
2318         QString title = QFileInfo(path).fileName();
2319         ui.editEnvMap->setText(title);
2320
2321         updateView_All();
2322 }
2323
2324 void FormMain::on_buttonReleaseEnvImg_clicked()
2325 {
2326         m_ContextShare.BeginDrawTop();
2327         m_Scene.m_EnvImg.ClearEnv();
2328         m_ContextShare.EndDrawTop();
2329
2330         ui.editEnvMap->clear();
2331
2332         updateView_All();
2333 }
2334
2335 void FormMain::on_buttonPresetEnvMap_clicked()
2336 {
2337         if (m_EnvmapDlg == NULL)
2338         {
2339                 m_EnvmapDlg = new EnvmapSelectDlg(this);
2340                 connect(
2341                         m_EnvmapDlg,
2342                         SIGNAL(ListSelectChanged()),
2343                         this,
2344                         SLOT(EnvmapDlg_ListSelectChanged()));
2345         }
2346
2347         m_EnvmapDlg->show();
2348 }
2349
2350 void FormMain::EnvmapDlg_ListSelectChanged()
2351 {
2352         QString p = m_EnvmapDlg->GetSelectedItemPath();
2353         if (p.isEmpty())
2354                 return;
2355
2356         LoadEnvMap(p);
2357 }
2358
2359 void FormMain::on_toolLoadMatcap_clicked()
2360 {
2361         QString fileName = GetSupportedImageFilePathFromDlg("Open matcap");
2362         if (fileName.isEmpty())
2363                 return;
2364
2365         LoadMatcapImage(fileName);
2366 }
2367
2368 void FormMain::on_buttonReleaseMatcap_clicked()
2369 {
2370         m_ContextShare.BeginDrawTop();
2371         m_Scene.m_MatcapImg.ClearEnv();
2372         m_ContextShare.EndDrawTop();
2373
2374         ui.editMatcap->clear();
2375
2376         updateView_All();
2377 }
2378
2379 void FormMain::on_buttonLoadMatcapPreset_clicked()
2380 {
2381         if (m_MatcapDlg == NULL)
2382         {
2383                 m_MatcapDlg = new MatcapSelectDlg(this);
2384                 connect(
2385                         m_MatcapDlg,
2386                         SIGNAL(ListSelectChanged()),
2387                         this,
2388                         SLOT(MatcapDlg_ListSelectChanged()));
2389         }
2390
2391         m_MatcapDlg->show();
2392 }
2393
2394 void FormMain::MatcapDlg_ListSelectChanged()
2395 {
2396         QString p = m_MatcapDlg->GetSelectedItemPath();
2397         if (p.isEmpty())
2398                 return;
2399
2400         LoadMatcapImage(p);
2401 }
2402
2403 void FormMain::on_sliderEnvReflection_valueChanged(int value)
2404 {
2405         float r = GetSliderNormalizedPos(ui.sliderEnvReflection);
2406         m_Scene.m_EnvImg.m_EnvReflection = r;
2407
2408         ui.labelEnvReflection->setText(QString::number(r, 'f', 2));
2409
2410         updateView_All();
2411 }
2412
2413 void FormMain::on_sliderEnvRefract_valueChanged(int value)
2414 {
2415         float r = ui.sliderEnvRefract->value() / 1000.0;
2416         m_Scene.m_EnvImg.m_EnvRefract = r;
2417
2418         ui.labelEnvRefract->setText(QString::number(r, 'f', 2));
2419         updateView_All();
2420 }
2421
2422
2423 void FormMain::on_comboCoordinate_currentIndexChanged(int index)
2424 {
2425         SceneTransform& transform = m_Scene.m_WorldTransform;
2426
2427         int idx = ui.comboCoordinate->currentIndex();
2428         switch (idx)
2429         {
2430         case 0: transform.SetCoordType(geom::SceneTransform::COORD_RUF); break;
2431         case 1: transform.SetCoordType(geom::SceneTransform::COORD_FRU); break;
2432         case 2: transform.SetCoordType(geom::SceneTransform::COORD_UFR); break;
2433         case 3: transform.SetCoordType(geom::SceneTransform::COORD_RUB); break;
2434         case 4: transform.SetCoordType(geom::SceneTransform::COORD_BRU); break;
2435         case 5: transform.SetCoordType(geom::SceneTransform::COORD_UBR); break;
2436         default:
2437                 break;
2438         }
2439
2440         updateView_All();
2441 }
2442
2443 void FormMain::on_sliderShadowDarkness_valueChanged(int value)
2444 {
2445         int b = ui.sliderShadowDarkness->minimum();
2446         int t = ui.sliderShadowDarkness->maximum();
2447
2448         float r = (float)(value - b) / (float)(t - b);
2449         m_Scene.m_ShadowConfig.m_ShadowDarkness = r;
2450
2451         updateView_All();
2452 }
2453
2454
2455 void FormMain::on_actionOpenAppdir_triggered()
2456 {
2457         PathInfo::OpenAppDir();
2458 }
2459
2460 void FormMain::on_actionOpenConfigDir_triggered()
2461 {
2462         PathInfo::OpenConfigDir();
2463 }
2464
2465 void FormMain::on_buttonDecGridAxis_clicked()
2466 {
2467         m_View3d.m_GridAxisScale /= 10.0f;
2468         updateView_3D();
2469 }
2470
2471 void FormMain::on_buttonIncGridAxis_clicked()
2472 {
2473         m_View3d.m_GridAxisScale *= 10.0f;
2474         updateView_3D();
2475 }
2476
2477 void FormMain::on_buttonResetGridAxis_clicked()
2478 {
2479         m_View3d.m_GridAxisScale = 1.0f;
2480         updateView_3D();
2481 }
2482
2483
2484 void FormMain::View3D_SelectedObjectChanged(int sel_obj, int sel_mesh)
2485 {
2486         ui.widgetObjectList->OnSelectedObjectChanged(sel_obj, sel_mesh);
2487 }
2488
2489 void FormMain::View3D_SelectedMatChanged(int sel_idx)
2490 {
2491         ui.listMaterial->setCurrentRow(sel_idx);
2492 }
2493
2494 void FormMain::View3D_CameraMoved(void)
2495 {
2496         UpdateCameraStatus();
2497 }
2498
2499 void FormMain::View3D_StatusTipChanged(QString msg)
2500 {
2501         //ui.statusBar->showMessage(msg, 1500);
2502 }
2503
2504 void FormMain::View3D_SequenceStepped(int step)
2505 {
2506         StepSequence(step);
2507 }
2508
2509 void FormMain::View3D_CursorMoved()
2510 {
2511         Cursor3D& c = m_Scene.m_Cursor3d;
2512         QString x = QString::number(c.CursorPos.x, 'f', 4);
2513         QString y = QString::number(c.CursorPos.y, 'f', 4);
2514         QString z = QString::number(c.CursorPos.z, 'f', 4);
2515         QString s;
2516         if (x.at(0) != '-')
2517                 s += " ";
2518         s += x;
2519         if (y.at(0) != '-')
2520                 s += " ";
2521         s += " ";
2522         s += y;
2523         if (z.at(0) != '-')
2524                 s += " ";
2525         s += " ";
2526         s += z;
2527
2528         ui.editCursorPos->setText(s);
2529 }
2530
2531 void FormMain::StepSequence(int step)
2532 {
2533         QSlider* s = ui.sliderSequence;
2534         s->setValue(s->value() + step);
2535 }
2536
2537 bool FormMain::IsLastSequence(void)
2538 {
2539         QSlider* s = ui.sliderSequence;
2540         return s->value() == s->maximum();
2541 }
2542
2543 bool FormMain::IsTopSequence(void)
2544 {
2545         QSlider* s = ui.sliderSequence;
2546         return s->value() == 0;
2547 }
2548
2549 void FormMain::UpdateCameraStatus(void)
2550 {
2551         const Camera& camera = m_Scene.m_Camera;
2552         const lib_gl::CameraManipulator& manip = camera.m_Manip;
2553         const lm::vec3f& vp = manip.m_ViewPos;
2554
2555         float a, b;
2556         manip.GetViewAngles(a, b);
2557         a = a * 180.0f / M_PI;
2558         b = b * 180.0f / M_PI;
2559
2560         QString msg;
2561         msg += "(";
2562         msg += QString::number(vp.x, 'f', 2) + " ";
2563         msg += QString::number(vp.y, 'f', 2) + " ";
2564         msg += QString::number(vp.z, 'f', 2);
2565         msg += ")";
2566
2567         msg += "(";
2568         msg += QString::number(a, 'f', 2) + " ";
2569         msg += QString::number(b, 'f', 2);
2570         msg += ")";
2571
2572         msg += "(";
2573         msg += QString::number(camera.m_Projection.m_Near, 'f', 2) + " ";
2574         msg += QString::number(camera.m_Projection.m_Far, 'f', 2);
2575         msg += ")";
2576
2577         m_StatusBar1->setText(msg);
2578 }
2579
2580 void FormMain::on_actionCameraFPSMode_toggled(bool arg1)
2581 {
2582         m_View3d.m_FpsMode = arg1;
2583 }
2584
2585
2586 void FormMain::actionCursorMenuStates_Toggled(bool)
2587 {
2588         m_Binder_Cursor.UpdateAllData();
2589
2590         if (QObject::sender() == ui.actionRecordStroke)
2591         {
2592                 m_Scene.m_Cursor3d.CutStroke();
2593         }
2594
2595         updateView_3D();
2596 }
2597
2598 void FormMain::on_actionResetCursor_triggered()
2599 {
2600         m_Scene.m_Cursor3d.ResetCursorPos();
2601         updateView_3D();
2602 }
2603
2604 void FormMain::on_actionResetMeasure_triggered()
2605 {
2606         m_View3d.ResetCursorMeasure();
2607         updateView_3D();
2608 }
2609
2610
2611 void FormMain::on_actionPyScript_triggered()
2612 {
2613         if (m_PyScriptDlg == NULL)
2614         {
2615                 m_PyScriptDlg = new FormPyScript(this);
2616                 m_PyScriptDlg->m_Scene = &m_Scene;
2617                 m_PyScriptDlg->m_View3d = &m_View3d;
2618                 m_PyScriptDlg->m_View2d = &m_View2d;
2619         }
2620
2621         m_PyScriptDlg->showNormal();
2622         m_PyScriptDlg->show();
2623         m_PyScriptDlg->activateWindow();
2624 }
2625
2626 void FormMain::on_actionConsole_triggered()
2627 {
2628         ::AllocConsole();
2629         freopen( "CON", "r", stdin  );
2630         freopen( "CON", "w", stdout );
2631 }
2632
2633
2634 void FormMain::on_actionClearVertSelect_triggered()
2635 {
2636         m_Scene.ClearAllVertSelect();
2637         updateView_All();
2638 }
2639
2640
2641 void FormMain::on_action_About_triggered()
2642 {
2643         QGVAboutDlg dlg;
2644         dlg.exec();
2645 }
2646
2647 void FormMain::on_action_Association_triggered()
2648 {
2649         m_AssociationDlg.exec();
2650 }
2651
2652 void FormMain::on_actionLookDepth_triggered()
2653 {
2654         m_View3d.LookDepth();
2655 }
2656
2657 void FormMain::on_actionLook3DCursor_triggered()
2658 {
2659         m_View3d.Look3DCursor();
2660 }
2661
2662 void FormMain::on_sliderLightPowerDS_valueChanged(int value)
2663 {
2664         float n = 2.0f * GetSliderNormalizedPos(ui.sliderLightPowerDS);
2665         m_View3d.SetDfSpLightPower(n);
2666
2667         UpdateLightPowerText();
2668
2669         updateView_All();
2670 }
2671
2672 void FormMain::on_buttonResetLightPowerDS_clicked()
2673 {
2674         ui.sliderLightPowerDS->setValue(1000);
2675 }
2676
2677 void FormMain::on_sliderLightPowerA_valueChanged(int value)
2678 {
2679         float n = 2.0f * GetSliderNormalizedPos(ui.sliderLightPowerA);
2680         m_View3d.SetAmbientLightPower(n);
2681
2682         UpdateLightPowerText();
2683
2684         updateView_All();
2685 }
2686
2687 void FormMain::on_buttonResetLightPowerA_clicked()
2688 {
2689         ui.sliderLightPowerA->setValue(1000);
2690 }
2691
2692 void FormMain::UpdateLightPowerText(void)
2693 {
2694         float ld = 2.0f * GetSliderNormalizedPos(ui.sliderLightPowerDS);
2695         float la = 2.0f * GetSliderNormalizedPos(ui.sliderLightPowerA);
2696
2697         QString s = "";
2698         s += QString("Light");
2699         s += QString(" DS=") + QString::number(ld, 'f', 2);
2700         s += QString(" A=") + QString::number(la, 'f', 2);
2701         ui.labelLightPower->setText(s);
2702 }
2703
2704 void FormMain::on_checkHoleAroundCursor_clicked(bool checked)
2705 {
2706         if (checked)
2707                 ui.checkShowOnlyAroundCursor->setChecked(false);
2708
2709         if (checked)
2710                 m_Scene.m_Cursor3d.SphereClip = SphereClipType::Hole;
2711         else
2712                 m_Scene.m_Cursor3d.SphereClip = SphereClipType::None;
2713
2714         updateView_All();
2715 }
2716
2717 void FormMain::on_checkShowOnlyAroundCursor_toggled(bool checked)
2718 {
2719         if (checked)
2720                 ui.checkHoleAroundCursor->setChecked(false);
2721
2722         if (checked)
2723                 m_Scene.m_Cursor3d.SphereClip = SphereClipType::ShowAround;
2724         else
2725                 m_Scene.m_Cursor3d.SphereClip = SphereClipType::None;
2726
2727         updateView_All();
2728 }
2729
2730 void FormMain::on_sliderCursorHoleRange_valueChanged(int value)
2731 {
2732         float val = (float)ui.sliderCursorHoleRange->value();
2733         float maxval = (float)ui.sliderCursorHoleRange->maximum();
2734         float n = val / maxval;
2735
2736         m_Scene.m_Cursor3d.HoleRangeRatio = n;
2737         updateView_All();
2738 }
2739
2740 void FormMain::on_buttonResetCursorHoleRange_clicked()
2741 {
2742         ResetHoleRange();
2743 }
2744
2745 void FormMain::ResetHoleRange(void)
2746 {
2747         ui.sliderCursorHoleRange->setValue(500);
2748 }
2749
2750 void FormMain::on_actionSaveImageToMydoc_triggered()
2751 {
2752         QString path = PathInfo::GetMyDocDirPath();
2753         QString basename = path + "/" + "qgv_snap_";
2754
2755         QDateTime dt = QDateTime::currentDateTime();
2756
2757         QString dst;
2758         for(;;)
2759         {
2760                 QString tp = dt.toString("yyyyMMdd_hhmmss_zzz");
2761                 dst = basename + tp + ".png";
2762                 if (!QFile(dst).exists())
2763                         break;
2764         }
2765
2766         ui.GLWidgetMain->grabFrameBuffer().save(dst);
2767 }
2768
2769 void FormMain::on_actionPostProcNone_triggered()
2770 {
2771         ChangePostprocMode(PostProcType::None);
2772 }
2773
2774 void FormMain::on_actionPostProcAntialias_triggered()
2775 {
2776         ChangePostprocMode(PostProcType::Antialias);
2777 }
2778
2779 void FormMain::on_actionPostProcBorder_triggered()
2780 {
2781         ChangePostprocMode(PostProcType::Border);
2782 }
2783
2784 void FormMain::on_actionPostProcDepthLayerColor_triggered()
2785 {
2786         ChangePostprocMode(PostProcType::DepthLayerColor);
2787 }
2788
2789 void FormMain::on_actionPostProcDepthColor_triggered()
2790 {
2791         ChangePostprocMode(PostProcType::DepthColor);
2792 }
2793
2794 void FormMain::on_actionPostProcDepthOfField_triggered()
2795 {
2796         ChangePostprocMode(PostProcType::DepthOfField);
2797 }
2798
2799 void FormMain::ChangePostprocMode(PostProcType type)
2800 {
2801         m_View3d.m_Config.m_PPMode = type;
2802         updateView_3D();
2803 }
2804
2805 void FormMain::on_sliderDOFPint_valueChanged(int value)
2806 {
2807         float n = GetSliderNormalizedPos(ui.sliderDOFPint);
2808         m_View3d.m_PPContext.m_DOFPintPos = n;
2809
2810         updateView_3D();
2811 }
2812
2813 void FormMain::on_sliderDOFRange_valueChanged(int value)
2814 {
2815         float n = GetSliderNormalizedPos(ui.sliderDOFRange);
2816         m_View3d.m_PPContext.m_DOFRange = 10.0f * n;
2817
2818         updateView_3D();
2819 }
2820
2821 float FormMain::GetSliderNormalizedPos(const QSlider* slider) const
2822 {
2823         float t = (float)slider->maximum();
2824         float b = (float)slider->minimum();
2825         float v = (float)slider->value();
2826
2827         return (v - b) / (t - b);
2828 }
2829
2830 void FormMain::on_buttonResetDOFPint_clicked()
2831 {
2832         ui.sliderDOFPint->setValue(1000);
2833 }
2834
2835 void FormMain::on_buttonResetDOFRange_clicked()
2836 {
2837         ui.sliderDOFRange->setValue(1000);
2838 }
2839
2840 void FormMain::on_actionAddCrossSectionRecord_triggered()
2841 {
2842         m_Scene.AddCrossSectionRecord();
2843         updateView_All();
2844 }
2845
2846 void FormMain::on_actionClearCrossSectionRecord_triggered()
2847 {
2848         m_Scene.ClearCrossSectionRecord();
2849         updateView_All();
2850 }
2851
2852 void FormMain::LoadCurSelMatTexture(TextureType type)
2853 {
2854         QString path = GetSupportedImageFilePathFromDlg("Load Texture");
2855         if (path.isEmpty())
2856                 return;
2857
2858         LoadCurSelMatTexture(type, path);
2859 }
2860
2861 void FormMain::LoadCurSelMatTexture(TextureType type, QString filepath)
2862 {
2863         GeomTextureSet* ts = m_Scene.GetSelectedTexture();
2864         if (ts == NULL)
2865                 return;
2866
2867         MeshBuf* mbuf = ts->GetParent();
2868         std::string fp = filepath.toLocal8Bit().data();
2869
2870         QString name_qs = QFileInfo(filepath).fileName();
2871         std::string name = name_qs.toLocal8Bit().data();
2872
2873         m_ContextShare.BeginDrawTop();
2874
2875         mbuf->ReleaseTextureUnit(ts, type);
2876         gl::GlTexture* tex = mbuf->GetInitTexture(fp, name, m_Scene.m_TexConfig);
2877         ts->SetTextureUnit(type, tex);
2878
2879         if (type == TextureType::Normal)
2880                 m_View3d.ReleaseAllRenderBuffer();
2881
2882         m_ContextShare.EndDrawTop();
2883
2884         OnChangedSelectedMaterial();
2885         updateView_All();
2886 }
2887
2888 void FormMain::ReleaseCurSelTexture(TextureType type)
2889 {
2890         GeomTextureSet* ts = m_Scene.GetSelectedTexture();
2891         if (ts == NULL)
2892                 return;
2893
2894         MeshBuf* mbuf = ts->GetParent();
2895         int mat_idx = mbuf->GetSelMatIdx();
2896
2897         m_ContextShare.BeginDrawTop();
2898         mbuf->ReleaseTextureUnit(mat_idx, type);
2899         m_ContextShare.EndDrawTop();
2900
2901         OnChangedSelectedMaterial();
2902         updateView_All();
2903 }
2904
2905 void FormMain::LoadMatcapImageForCurrentMat(QString& path)
2906 {
2907         GeomTextureSet* ts = m_Scene.GetSelectedTexture();
2908         if (ts == NULL)
2909                 return;
2910
2911         QString title = QFileInfo(path).fileName();
2912
2913         m_ContextShare.BeginDrawTop();
2914         ts->TexMatcap.LoadTexture(path.toLocal8Bit().data());
2915         ts->TexMatcap.SetName(title);
2916         m_ContextShare.EndDrawTop();
2917
2918         OnChangedSelectedMaterial();
2919         updateView_All();
2920 }
2921
2922 void FormMain::LoadMatcapImage(QString& path)
2923 {
2924         MatcapImage& mc = m_Scene.m_MatcapImg;
2925
2926         QString title = QFileInfo(path).fileName();
2927
2928         m_ContextShare.BeginDrawTop();
2929         mc.LoadTexture(path.toLocal8Bit().data());
2930         mc.SetName(title);
2931         m_ContextShare.EndDrawTop();
2932
2933         ui.editMatcap->setText(mc.GetName());
2934
2935         updateView_All();
2936 }
2937
2938 void FormMain::on_buttonOpenMatCapEachMaterial_clicked()
2939 {
2940         QString path = GetSupportedImageFilePathFromDlg("Load Texture");
2941         if (path.isEmpty())
2942                 return;
2943
2944         LoadMatcapImage(path);
2945 }
2946
2947 void FormMain::on_buttonClearMatCapEachMaterial_clicked()
2948 {
2949         geom::GeomTextureSet* tex = m_Scene.GetSelectedTexture();
2950         if (tex == NULL)
2951                 return;
2952
2953         m_ContextShare.BeginDrawTop();
2954         tex->TexMatcap.ClearEnv();
2955         m_ContextShare.EndDrawTop();
2956
2957         ui.editMatCapEachMaterial->clear();
2958
2959         updateView_All();
2960 }
2961
2962 void FormMain::on_buttonOpenCurrentMatColorMap_clicked()
2963 {
2964         LoadCurSelMatTexture(TextureType::Color);
2965 }
2966
2967 void FormMain::on_buttonClearCurrentMatColorMap_clicked()
2968 {
2969         ReleaseCurSelTexture(TextureType::Color);
2970 }
2971
2972 void FormMain::on_buttonOpenCurrentMatNormalMap_clicked()
2973 {
2974         LoadCurSelMatTexture(TextureType::Normal);
2975 }
2976
2977 void FormMain::on_buttonClearCurrentMatNormalMap_clicked()
2978 {
2979         ReleaseCurSelTexture(TextureType::Normal);
2980 }
2981
2982 void FormMain::SetColorPalleteBG(QWidget* w, QColor col)
2983 {
2984         QColor border(0, 0, 0);
2985         SetWidgetColor(w, col, border);
2986 }
2987
2988 void FormMain::SetWidgetColor(QWidget* w, QColor col)
2989 {
2990         QString ss = QString("background-color: %1").arg(col.name());
2991         w->setStyleSheet(ss);
2992 }
2993
2994 void FormMain::SetWidgetColor(QWidget* w, QColor col, QColor col_border)
2995 {
2996         QString ss;
2997         ss += QString("background-color: %1;").arg(col.name());
2998         ss += QString("border: 1px solid %1;").arg(col_border.name());
2999         w->setStyleSheet(ss);
3000 }
3001
3002 void FormMain::on_sliderShininess_valueChanged(int value)
3003 {
3004         lib_geo::BaseMaterial* mat = m_Scene.GetSelectedMaterial();
3005         if (mat == NULL)
3006                 return;
3007
3008         float val = (float)ui.sliderShininess->value();
3009         float val_max = (float)ui.sliderShininess->maximum();
3010
3011         float real_val = 200.0f * val / val_max;
3012
3013         mat->m_Shininess = real_val;
3014
3015         ui.labelShininessVal->setText(QString::number(real_val, 'f', 2));
3016
3017         updateView_All();
3018 }
3019
3020 void FormMain::CreateRecentFilesMenu(const std::vector<QString>& path)
3021 {
3022         QMenu* recents = ui.menuOpenRecent;
3023         recents->actions().clear();
3024
3025         for (size_t i = 0; i < path.size(); ++i)
3026         {
3027                 AddRecentFile(path[path.size() - i - 1]);
3028         }
3029 }
3030
3031 void FormMain::CreateRecentFilesFromMenu(std::vector<QString>& path)
3032 {
3033         QMenu* recents = ui.menuOpenRecent;
3034         for (QAction* a : recents->actions())
3035         {
3036                 path.push_back(a->text());
3037         }
3038 }
3039
3040 void FormMain::AddRecentFile(const QString& path)
3041 {
3042         QString path_local = QDir::toNativeSeparators(path);
3043
3044         static const int max_files = 10;
3045
3046         QMenu* recents = ui.menuOpenRecent;
3047
3048         for (QAction* a : recents->actions())
3049         {
3050                 if (a->text() == path_local)
3051                 {
3052                         recents->removeAction(a);
3053                         break;
3054                 }
3055         }
3056
3057         QAction* new_path = new QAction(path_local, this);
3058         connect(
3059                 new_path,
3060                 SIGNAL(triggered()),
3061                 this,
3062                 SLOT(OnOpenRecent()));
3063
3064         if (recents->actions().empty())
3065                 recents->addAction(new_path);
3066         else
3067                 recents->insertAction(recents->actions().front(), new_path);
3068
3069         if (recents->actions().size() > max_files)
3070         {
3071                 QAction* a = recents->actions().back();
3072                 recents->removeAction(a);
3073         }
3074 }
3075
3076 void FormMain::OnOpenRecent()
3077 {
3078         QAction* a = dynamic_cast<QAction*>(QObject::sender());
3079         assert(a != NULL);
3080         if (a == NULL)
3081                 return;
3082
3083         QString path = a->text();
3084
3085         if (IsResetSceneOnBeforeLoadFile())
3086                 ClearAllObjects();
3087
3088         if (OpenGeomFileToLast(path))
3089                 return;
3090 }
3091
3092 void FormMain::on_actionReleaseShader_triggered()
3093 {
3094         m_ContextShare.BeginDrawTop();
3095
3096         m_View3d.m_ShaderLib.ReleaseAllShaders();
3097
3098         m_ContextShare.EndDrawTop();
3099
3100         updateView_All();
3101 }
3102
3103 void FormMain::on_combo2DViewSrcType_currentIndexChanged(int index)
3104 {
3105         switch (index)
3106         {
3107         case 0 : return m_View2d.ChangeTextureSource(View2DTexSrc::Color);
3108         case 1 : return m_View2d.ChangeTextureSource(View2DTexSrc::Normal);
3109         default:
3110                 assert(false);
3111                 break;
3112         }
3113 }
3114
3115 void FormMain::on_sliderSequence_valueChanged(int value)
3116 {
3117         UpdateSequence();
3118 }
3119
3120 void FormMain::ResetSequenceSliderRange(void)
3121 {
3122         int m = m_Scene.GetKeyframeMax();
3123         ui.sliderSequence->setMaximum(m);
3124         UpdateSequence();
3125 }
3126
3127 void FormMain::UpdateSequence(void)
3128 {
3129         int v = ui.sliderSequence->value();
3130         int m = ui.sliderSequence->maximum();
3131
3132         QString s = QString("%1 / %2").arg(v).arg(m);
3133         ui.labelSequencePos->setText(s);
3134
3135         m_Scene.SetFrame(v);
3136
3137         m_View3d.ReleaseAllRenderBuffer();
3138         updateView_All();
3139 }
3140
3141 void FormMain::on_actionNewStroke_triggered()
3142 {
3143         m_Scene.m_Cursor3d.CutStroke();
3144         updateView_All();
3145 }
3146
3147 void FormMain::on_actionClearStroke_triggered()
3148 {
3149         m_Scene.m_Cursor3d.ClearStroke();
3150         updateView_All();
3151 }
3152
3153 void FormMain::on_buttonMatPreset_clicked()
3154 {
3155         lib_geo::BaseMaterial* mat = m_Scene.GetSelectedMaterial();
3156         if (mat == NULL)
3157                 return;
3158
3159         lib_geo::BaseMaterial mat_backup = (*mat);
3160
3161         if (m_MatPresetDlg == NULL)
3162         {
3163                 m_MatPresetDlg = new MaterialPresetDlg(this);
3164                 connect(
3165                         m_MatPresetDlg,
3166                         SIGNAL(OnMatChanged()),
3167                         this,
3168                         SLOT(MatPresetDlg_OnMatChanged()));
3169         }
3170
3171         MatPresetDlg_OnMatChanged();
3172
3173         if (m_MatPresetDlg->exec() != QDialog::Accepted)
3174         {
3175                 (*mat) = mat_backup;
3176                 OnChangedSelectedMaterial();
3177                 updateView_All();
3178                 return;
3179         }
3180
3181         lgr::MaterialSamples::MaterialSampleType t = m_MatPresetDlg->GetSelectedType();
3182
3183         if (t != lgr::MaterialSamples::MAT_NONE)
3184                 (*mat) = lgr::MaterialSamples::GetMaterial(t);
3185         else
3186                 (*mat) = mat_backup;
3187
3188         OnChangedSelectedMaterial();
3189         updateView_All();
3190 }
3191
3192 void FormMain::MatPresetDlg_OnMatChanged()
3193 {
3194         lib_geo::BaseMaterial* mat = m_Scene.GetSelectedMaterial();
3195         if (mat == NULL)
3196                 return;
3197
3198         lgr::MaterialSamples::MaterialSampleType t = m_MatPresetDlg->GetSelectedType();
3199         if (t == lgr::MaterialSamples::MAT_NONE)
3200                 return;
3201
3202         (*mat) = lgr::MaterialSamples::GetMaterial(t);
3203
3204         OnChangedSelectedMaterial();
3205         updateView_All();
3206 }
3207
3208 void FormMain::on_checkEnableIndexRange_toggled(bool checked)
3209 {
3210         IndexRangeConfig& ir = m_Scene.m_IndexVisrange;
3211         ir.enableRange = checked;
3212
3213         m_View3d.ReleaseAllIndexList();
3214         updateView_All();
3215 }
3216
3217 void FormMain::on_editIndexLimitOffset_textChanged(const QString &arg1)
3218 {
3219         IndexRangeConfig& ir = m_Scene.m_IndexVisrange;
3220
3221         bool b;
3222         int i = arg1.toInt(&b);
3223         if (!b)
3224                 return;
3225
3226         ir.beginIdx = i;
3227
3228         if (ir.enableRange)
3229         {
3230                 m_View3d.ReleaseAllIndexList();
3231                 updateView_All();
3232         }
3233 }
3234
3235 void FormMain::on_editIndexLimitLen_textChanged(const QString &arg1)
3236 {
3237         IndexRangeConfig& ir = m_Scene.m_IndexVisrange;
3238
3239         bool b;
3240         int i = arg1.toInt(&b);
3241         if (!b)
3242                 return;
3243
3244         ir.idxLen = i;
3245
3246         if (ir.enableRange)
3247         {
3248                 m_View3d.ReleaseAllIndexList();
3249                 updateView_All();
3250         }
3251 }
3252
3253 void FormMain::on_fscrollIndexLimitOffset_onScroll(int step)
3254 {
3255         IndexRangeConfig& ir = m_Scene.m_IndexVisrange;
3256
3257         int n = ir.beginIdx + step;
3258         n = (std::max)(n, 0);
3259         ui.editIndexLimitOffset->setText(QString::number(n));
3260 }
3261
3262 void FormMain::on_fscrollIndexLimitOffset_onReset()
3263 {
3264         ui.editIndexLimitOffset->setText("0");
3265 }
3266
3267 void FormMain::on_fscrollIndexLimitLen_onScroll(int step)
3268 {
3269         IndexRangeConfig& ir = m_Scene.m_IndexVisrange;
3270
3271         int n = ir.idxLen + step;
3272         n = (std::max)(n, 1);
3273         ui.editIndexLimitLen->setText(QString::number(n));
3274 }
3275
3276 void FormMain::on_fscrollIndexLimitLen_onReset()
3277 {
3278         ui.editIndexLimitLen->setText("1");
3279 }
3280
3281 void FormMain::on_buttonTextureScaleDiv10_clicked()
3282 {
3283         MulTexScaleMain(1.0 / 10.0);
3284 }
3285
3286 void FormMain::on_buttonTextureScaleMul10_clicked()
3287 {
3288         MulTexScaleMain(10.0);
3289 }
3290
3291 void FormMain::on_fscrollTextureScale_onScroll(int step)
3292 {
3293         double s = pow(2.0, (double)step / 100.0);
3294         MulTexScaleMain(s);
3295 }
3296
3297 void FormMain::MulTexScaleMain(double s)
3298 {
3299         m_Scene.m_TextureTransform.m_TexScale *= s;
3300         float ts = m_Scene.m_TextureTransform.m_TexScale;
3301         SetTextSilent(ui.editTextureScale, QString::number(ts));
3302         updateView_All();
3303 }
3304
3305 void FormMain::on_fscrollTextureScale_onReset()
3306 {
3307         ui.editTextureScale->setText("1.0");
3308 }
3309
3310 void FormMain::on_editTextureScale_textChanged(const QString &arg1)
3311 {
3312         bool b;
3313         double d = arg1.toDouble(&b);
3314         if (!b)
3315                 return;
3316
3317         m_Scene.m_TextureTransform.m_TexScale = d;
3318
3319         updateView_All();
3320 }
3321
3322 void FormMain::SetTextSilent(QLineEdit* edit, const QString& text)
3323 {
3324         edit->blockSignals(true);
3325         edit->setText(text);
3326         edit->blockSignals(false);
3327 }
3328
3329 void FormMain::on_sliderMatcapRate_valueChanged(int value)
3330 {
3331         float r = GetSliderNormalizedPos(ui.sliderMatcapRate);
3332         m_Scene.m_MatcapImg.m_BlendRate = r;
3333
3334         ui.labelMatcapRate->setText(QString::number(r, 'f', 2));
3335
3336         updateView_All();
3337 }