OSDN Git Service

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