forked from emilk/sproxel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cpp
986 lines (799 loc) · 31.2 KB
/
MainWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
#include <QtGui>
#include <iostream>
#include "Global.h"
#include "GLCamera.h"
#include "MainWindow.h"
#include "GLModelWidget.h"
#include "PreferencesDialog.h"
#include "ConsoleWidget.h"
#include "pyConsole.h"
#include "ImportExport.h"
#include <QFileDialog>
#include <QColorDialog>
#include <QDockWidget>
#include <QMenuBar>
#include <QStatusBar>
#include <QMessageBox>
#define DEFAULT_VOXGRID_SZ (8)
MainWindow::MainWindow(const QString& initialFilename, QWidget *parent) :
QMainWindow(parent),
m_appSettings("OpenSource", "Sproxel"),
m_activeFilename(""),
m_project(new SproxelProject())
{
// Project
VoxelGridGroupPtr sprite(new VoxelGridGroup(Imath::V3i(DEFAULT_VOXGRID_SZ, DEFAULT_VOXGRID_SZ, DEFAULT_VOXGRID_SZ),
ColorPalettePtr()));
sprite->setName("unnamed");
m_project->sprites.push_back(sprite);
// Windows
m_glModelWidget = new GLModelWidget(this, &m_appSettings, &m_undoManager, sprite);
setCentralWidget(m_glModelWidget);
// The docking palette widget
m_paletteDocker = new QDockWidget(tr("Palette"), this);
m_paletteDocker->setObjectName("paletteDocker");
m_paletteDocker->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
m_paletteWidget = new PaletteWidget(this, &m_undoManager);
m_paletteDocker->setWidget(m_paletteWidget);
m_paletteWidget->setPalette(m_project->mainPalette);
addDockWidget(Qt::RightDockWidgetArea, m_paletteDocker);
// The docking project widget
m_projectDocker=new QDockWidget(tr("Project"), this);
m_projectDocker->setObjectName("projectDocker");
m_projectWidget=new ProjectWidget(this, &m_undoManager, &m_appSettings);
m_projectDocker->setWidget(m_projectWidget);
m_projectWidget->setProject(m_project);
addDockWidget(Qt::RightDockWidgetArea, m_projectDocker);
// The docking layers widget
//m_layersDocker = new QDockWidget(tr("Layers"), this);
//m_layersDocker->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
//m_layersWidget = new LayersWidget(this);
//m_layersDocker->setWidget(m_layersWidget);
//addDockWidget(Qt::RightDockWidgetArea, m_layersDocker);
// Connect some window signals together
QObject::connect(m_paletteWidget, SIGNAL(lightColorChanged(Imath::Color4f)),
m_glModelWidget, SLOT(setLightColor(Imath::Color4f)));
QObject::connect(m_paletteWidget, SIGNAL(activeColorChanged(Imath::Color4f, int)),
m_glModelWidget, SLOT(setActiveColor(Imath::Color4f, int)));
QObject::connect(m_glModelWidget, SIGNAL(colorSampled(Imath::Color4f, int)),
m_paletteWidget, SLOT(setActiveColor(Imath::Color4f, int)));
QObject::connect(&m_undoManager, SIGNAL(cleanChanged(bool)),
this, SLOT(reactToModified(bool)));
QObject::connect(m_projectWidget, SIGNAL(spriteSelected(VoxelGridGroupPtr)),
m_glModelWidget, SLOT(setSprite(VoxelGridGroupPtr)));
// Toolbar
m_toolbar = new QToolBar("Tools", this);
m_toolbar->setObjectName("toolbar");
m_toolbar->setOrientation(Qt::Vertical);
addToolBar(Qt::LeftToolBarArea, m_toolbar);
// Actions & Menus
menuBar()->show();
m_menuFile = menuBar()->addMenu("Fi&le");
m_actFileNew = new QAction("&New", this);
m_actFileNew->setShortcut(Qt::CTRL + Qt::Key_N);
m_menuFile->addAction(m_actFileNew);
connect(m_actFileNew, SIGNAL(triggered()),
this, SLOT(newGrid()));
m_menuFile->addSeparator();
m_actFileOpen = new QAction("&Open", this);
m_actFileOpen->setShortcut(Qt::CTRL + Qt::Key_O);
m_menuFile->addAction(m_actFileOpen);
connect(m_actFileOpen, SIGNAL(triggered()),
this, SLOT(openFile()));
m_actFileSave = new QAction("&Save", this);
m_actFileSave->setShortcut(Qt::CTRL + Qt::Key_S);
m_menuFile->addAction(m_actFileSave);
connect(m_actFileSave, SIGNAL(triggered()),
this, SLOT(saveFile()));
m_actFileSaveAs = new QAction("Save &As", this);
m_menuFile->addAction(m_actFileSaveAs);
connect(m_actFileSaveAs, SIGNAL(triggered()),
this, SLOT(saveFileAs()));
m_menuFile->addSeparator();
m_actFileImport = new QAction("&Import...", this);
m_menuFile->addAction(m_actFileImport);
connect(m_actFileImport, SIGNAL(triggered()),
this, SLOT(import()));
m_actFileExportGrid = new QAction("&Export...", this);
m_menuFile->addAction(m_actFileExportGrid);
connect(m_actFileExportGrid, SIGNAL(triggered()),
this, SLOT(exportGrid()));
m_menuFile->addSeparator();
m_actQuit = new QAction("&Quit", this);
m_actQuit->setShortcut(Qt::CTRL + Qt::Key_Q);
m_menuFile->addAction(m_actQuit);
connect(m_actQuit, SIGNAL(triggered()),
this, SLOT(close()));
// ------ edit menu
m_menuEdit = menuBar()->addMenu("&Edit");
m_actUndo=m_undoManager.createUndoAction(this, "Undo");
m_actUndo->setShortcut(Qt::CTRL + Qt::Key_Z);
m_menuEdit->addAction(m_actUndo);
m_actRedo=m_undoManager.createRedoAction(this, "Redo");
m_actRedo->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_Z);
m_menuEdit->addAction(m_actRedo);
m_menuEdit->addSeparator();
m_actShiftUp = new QAction("Shift up", this);
m_actShiftUp->setShortcut(Qt::CTRL + Qt::Key_BracketRight);
m_menuEdit->addAction(m_actShiftUp);
connect(m_actShiftUp, SIGNAL(triggered()),
this, SLOT(shiftUp()));
m_actShiftDown = new QAction("Shift down", this);
m_actShiftDown->setShortcut(Qt::CTRL + Qt::Key_BracketLeft);
m_menuEdit->addAction(m_actShiftDown);
connect(m_actShiftDown, SIGNAL(triggered()),
this, SLOT(shiftDown()));
m_actShiftWrap = new QAction("Wrap shift ops", this);
m_actShiftWrap->setCheckable(true);
m_actShiftWrap->setChecked(m_glModelWidget->shiftWrap());
m_menuEdit->addAction(m_actShiftWrap);
connect(m_actShiftWrap, SIGNAL(toggled(bool)),
m_glModelWidget, SLOT(setShiftWrap(bool)));
m_actRotateCw = new QAction("Rotate clockwise", this);
m_actRotateCw->setShortcut(Qt::CTRL + Qt::Key_Greater);
m_menuEdit->addAction(m_actRotateCw);
connect(m_actRotateCw, SIGNAL(triggered()),
this, SLOT(rotateCw()));
m_actRotateCcw = new QAction("Rotate counter-clockwise", this);
m_actRotateCcw->setShortcut(Qt::CTRL + Qt::Key_Less);
m_menuEdit->addAction(m_actRotateCcw);
connect(m_actRotateCcw, SIGNAL(triggered()),
this, SLOT(rotateCcw()));
m_actMirror = new QAction("Mirror", this);
m_actMirror->setShortcut(Qt::CTRL + Qt::Key_M);
m_menuEdit->addAction(m_actMirror);
connect(m_actMirror, SIGNAL(triggered()),
this, SLOT(mirror()));
m_menuEdit->addSeparator();
m_actPreferences = new QAction("Preferences...", this);
m_menuEdit->addAction(m_actPreferences);
connect(m_actPreferences, SIGNAL(triggered()),
this, SLOT(editPreferences()));
// ------ grid menu
m_menuGrid = menuBar()->addMenu("&Grid");
m_actExtendUp = new QAction("Extend grid dimension up", this);
m_actExtendUp->setShortcut(Qt::CTRL + Qt::Key_Plus);
m_menuGrid->addAction(m_actExtendUp);
connect(m_actExtendUp, SIGNAL(triggered()), this, SLOT(extendUp()));
m_actExtendDown = new QAction("Extend grid dimension down", this);
m_actExtendDown->setShortcut(Qt::CTRL + Qt::Key_Minus);
m_menuGrid->addAction(m_actExtendDown);
connect(m_actExtendDown, SIGNAL(triggered()), this, SLOT(extendDown()));
QAction* extendRight = new QAction("Extend grid dimension right", this);
m_menuGrid->addAction(extendRight);
connect(extendRight, SIGNAL(triggered()), this, SLOT(extendRight()));
QAction* extendLeft = new QAction("Extend grid dimension left", this);
m_menuGrid->addAction(extendLeft);
connect(extendLeft, SIGNAL(triggered()), this, SLOT(extendLeft()));
QAction* extendFront = new QAction("Extend grid dimension front", this);
m_menuGrid->addAction(extendFront);
connect(extendFront, SIGNAL(triggered()), this, SLOT(extendFront()));
QAction* extendBack = new QAction("Extend grid dimension back", this);
m_menuGrid->addAction(extendBack);
connect(extendBack, SIGNAL(triggered()), this, SLOT(extendBack()));
m_menuGrid->addSeparator();
m_actContractUp = new QAction("Contract grid dimension from above", this);
m_menuGrid->addAction(m_actContractUp);
connect(m_actContractUp, SIGNAL(triggered()), this, SLOT(contractUp()));
m_actContractDown = new QAction("Contract grid dimension from below", this);
m_menuGrid->addAction(m_actContractDown);
connect(m_actContractDown, SIGNAL(triggered()), this, SLOT(contractDown()));
QAction* contractRight = new QAction("Contract grid dimension right", this);
m_menuGrid->addAction(contractRight);
connect(contractRight, SIGNAL(triggered()), this, SLOT(contractRight()));
QAction* contractLeft = new QAction("Contract grid dimension left", this);
m_menuGrid->addAction(contractLeft);
connect(contractLeft, SIGNAL(triggered()), this, SLOT(contractLeft()));
QAction* contractFront = new QAction("Contract grid dimension front", this);
m_menuGrid->addAction(contractFront);
connect(contractFront, SIGNAL(triggered()), this, SLOT(contractFront()));
QAction* contractBack = new QAction("Contract grid dimension back", this);
m_menuGrid->addAction(contractBack);
connect(contractBack, SIGNAL(triggered()), this, SLOT(contractBack()));
m_menuGrid->addSeparator();
m_actUpRes = new QAction("Double grid resolution", this);
m_menuGrid->addAction(m_actUpRes);
connect(m_actUpRes, SIGNAL(triggered()),
this, SLOT(upRes()));
m_actDownRes = new QAction("Half grid resolution", this);
m_menuGrid->addAction(m_actDownRes);
connect(m_actDownRes, SIGNAL(triggered()),
this, SLOT(downRes()));
// ------ view menu
m_menuView = menuBar()->addMenu("&View");
QAction *action=new QAction(tr("Frame sprite"), this);
action->setShortcut(Qt::Key_Z);
m_menuView->addAction(action);
connect(action, SIGNAL(triggered()),
m_glModelWidget, SLOT(frameFull()));
m_actViewGrid = new QAction("View Grid", this);
m_actViewGrid->setShortcut(Qt::CTRL + Qt::Key_G);
m_actViewGrid->setCheckable(true);
m_actViewGrid->setChecked(m_glModelWidget->drawGrid());
m_menuView->addAction(m_actViewGrid);
connect(m_actViewGrid, SIGNAL(toggled(bool)),
m_glModelWidget, SLOT(setDrawGrid(bool)));
m_actViewVoxgrid = new QAction("Voxel Grid", this);
m_actViewVoxgrid->setShortcut(Qt::Key_G);
m_actViewVoxgrid->setCheckable(true);
m_actViewVoxgrid->setChecked(m_glModelWidget->drawVoxelGrid());
m_menuView->addAction(m_actViewVoxgrid);
connect(m_actViewVoxgrid, SIGNAL(toggled(bool)),
m_glModelWidget, SLOT(setDrawVoxelGrid(bool)));
m_actViewBBox = new QAction("Bounding Box", this);
m_actViewBBox->setShortcut(Qt::CTRL + Qt::Key_B);
m_actViewBBox->setCheckable(true);
m_actViewBBox->setChecked(m_glModelWidget->drawBoundingBox());
m_menuView->addAction(m_actViewBBox);
connect(m_actViewBBox, SIGNAL(toggled(bool)),
m_glModelWidget, SLOT(setDrawBoundingBox(bool)));
action=new QAction("Sprite Bounds", this);
action->setShortcut(Qt::Key_B);
action->setCheckable(true);
action->setChecked(m_glModelWidget->drawSpriteBounds());
m_menuView->addAction(action);
connect(action, SIGNAL(toggled(bool)),
m_glModelWidget, SLOT(setDrawSpriteBounds(bool)));
// ------ window menu
m_menuWindow = menuBar()->addMenu("&Window");
m_menuWindow->addAction(m_toolbar->toggleViewAction());
m_menuWindow->addAction(m_paletteDocker->toggleViewAction());
m_menuWindow->addAction(m_projectDocker->toggleViewAction());
//m_menuWindow->addAction(m_layersDocker->toggleViewAction());
#ifdef SPROXEL_USE_PYTHON
m_menuWindow->addAction(get_python_console_widget()->toggleViewAction());
get_python_console_widget()->toggleViewAction()->setChecked(false);
#endif
// ------ toolbar hookups
// Icons from the brilliant icon pack located at : http://pen-art.ru/
m_toolbarActionGroup = new QActionGroup(this);
m_actToolSplat = new QAction("Splat", m_toolbarActionGroup);
m_actToolSplat->setIcon(QIcon(QPixmap(":/icons/splat.png")));
m_actToolSplat->setCheckable(true);
connect(m_actToolSplat, SIGNAL(toggled(bool)), this, SLOT(setToolSplat(bool)));
m_actToolReplace = new QAction("Replace", m_toolbarActionGroup);
m_actToolReplace->setIcon(QIcon(QPixmap(":/icons/pencil.png")));
m_actToolReplace->setCheckable(true);
connect(m_actToolReplace, SIGNAL(toggled(bool)), this, SLOT(setToolReplace(bool)));
m_actToolFlood = new QAction("Flood", m_toolbarActionGroup);
m_actToolFlood->setIcon(QIcon(QPixmap(":/icons/paintBucket.png")));
m_actToolFlood->setCheckable(true);
connect(m_actToolFlood, SIGNAL(toggled(bool)), this, SLOT(setToolFlood(bool)));
m_actToolDropper = new QAction("Dropper", m_toolbarActionGroup);
m_actToolDropper->setIcon(QIcon(QPixmap(":/icons/eyeDropper.png")));
m_actToolDropper->setCheckable(true);
connect(m_actToolDropper, SIGNAL(toggled(bool)), this, SLOT(setToolDropper(bool)));
m_actToolEraser = new QAction("Eraser", m_toolbarActionGroup);
m_actToolEraser->setIcon(QIcon(QPixmap(":/icons/eraser.png")));
m_actToolEraser->setCheckable(true);
connect(m_actToolEraser, SIGNAL(toggled(bool)), this, SLOT(setToolEraser(bool)));
m_actToolSlab = new QAction("Slab", m_toolbarActionGroup);
m_actToolSlab->setIcon(QIcon(QPixmap(":/icons/slab.png")));
m_actToolSlab->setCheckable(true);
connect(m_actToolSlab, SIGNAL(toggled(bool)), this, SLOT(setToolSlab(bool)));
m_actToolLine = new QAction("Line", m_toolbarActionGroup);
m_actToolLine->setIcon(QIcon(QPixmap(":/icons/line.png")));
m_actToolLine->setCheckable(true);
connect(m_actToolLine, SIGNAL(toggled(bool)), this, SLOT(setToolLine(bool)));
m_actToolBox = new QAction("Box", m_toolbarActionGroup);
m_actToolBox->setIcon(QIcon(QPixmap(":/icons/box.png")));
m_actToolBox->setCheckable(true);
connect(m_actToolBox, SIGNAL(toggled(bool)), this, SLOT(setToolBox(bool)));
m_actToolExtrude = new QAction("Extrude", m_toolbarActionGroup);
m_actToolExtrude->setIcon(QIcon(QPixmap(":/icons/extrude.png")));
m_actToolExtrude->setCheckable(true);
connect(m_actToolExtrude, SIGNAL(toggled(bool)), this, SLOT(setToolExtrude(bool)));
//m_actToolRay = new QAction("Ray", this);
m_actToolSplat->setChecked(true);
m_toolbar->addActions(m_toolbarActionGroup->actions());
// Toolbar widgets for slicing
m_toolbar->addSeparator();
m_minSliceBox=new QSpinBox();
m_maxSliceBox=new QSpinBox();
m_toolbar->addWidget(m_maxSliceBox);
m_toolbar->addWidget(m_minSliceBox);
connect(m_glModelWidget, SIGNAL(sliceChanged(int, int, int)), this, SLOT(updateSlice(int, int, int)));
connect(m_minSliceBox, SIGNAL(valueChanged(int)), m_glModelWidget, SLOT(setMinSlice(int)));
connect(m_maxSliceBox, SIGNAL(valueChanged(int)), m_glModelWidget, SLOT(setMaxSlice(int)));
// axis selection
QActionGroup *axisGroup=new QActionGroup(this);
QAction *a=new QAction("X", axisGroup);
a->setCheckable(true);
connect(a, SIGNAL(toggled(bool)), m_glModelWidget, SLOT(setAxisX()));
m_actAxisX=a;
a=new QAction("Y", axisGroup);
a->setCheckable(true);
connect(a, SIGNAL(toggled(bool)), m_glModelWidget, SLOT(setAxisY()));
m_actAxisY=a;
a=new QAction("Z", axisGroup);
a->setCheckable(true);
connect(a, SIGNAL(toggled(bool)), m_glModelWidget, SLOT(setAxisZ()));
m_actAxisZ=a;
m_toolbar->addSeparator();
m_toolbar->addActions(axisGroup->actions());
m_actAxisY->setChecked(true);
// Remaining verbosity
setWindowTitle(BASE_WINDOW_TITLE);
statusBar()->showMessage(tr("Ready"));
// Load up some settings
if (m_appSettings.value("saveUILayout", true).toBool())
{
resize(m_appSettings.value("MainWindow/size", QSize(546, 427)).toSize());
move(m_appSettings.value("MainWindow/position", QPoint(200, 200)).toPoint());
setWindowState((Qt::WindowStates)m_appSettings.value("MainWindow/windowState", Qt::WindowActive).toInt());
restoreState(m_appSettings.value("MainWindow/widgetsState").toByteArray());
m_toolbar->setVisible(m_appSettings.value("toolbar/visibility", true).toBool());
m_paletteDocker->setVisible(m_appSettings.value("paletteWindow/visibility", true).toBool());
m_projectDocker->setVisible(m_appSettings.value("projectWindow/visibility", true).toBool());
//m_layersDocker->setVisible(m_appSettings.value("layersWindow/visibility", true).toBool());
}
// Load the commandline supplied filename
if (initialFilename != "")
{
openFile(initialFilename);
}
// Better way to keep the state in one place
//std::cout << (m_toolbarActionGroup->checkedAction()->text() == "Splat") << std::endl;
//std::cout << qPrintable(m_toolbarActionGroup->checkedAction()->text()) << std::endl;
// Start things off focused on the GLWidget
m_glModelWidget->setFocus();
m_glModelWidget->frame(true);
}
void MainWindow::updateSlice(int mino, int maxo, int range)
{
m_minSliceBox->setRange(0, range);
m_maxSliceBox->setRange(0, range);
m_minSliceBox->setValue(mino);
m_maxSliceBox->setValue(maxo);
}
void MainWindow::closeEvent(QCloseEvent* event)
{
// Confirmation dialog
if (!m_undoManager.isClean())
{
switch (fileModifiedDialog())
{
case QMessageBox::Save: saveFile(); event->accept(); break;
case QMessageBox::Discard: event->accept(); break;
case QMessageBox::Cancel: event->ignore(); break;
default: event->ignore(); break;
}
}
else
{
event->accept();
}
m_glModelWidget->saveSettings();
// Save some window settings on exit (if requested)
if (m_appSettings.value("saveUILayout", true).toBool())
{
m_appSettings.setValue("MainWindow/size", size());
m_appSettings.setValue("MainWindow/position", pos());
m_appSettings.setValue("MainWindow/windowState", (int)windowState());
m_appSettings.setValue("MainWindow/widgetsState", saveState());
m_appSettings.setValue("toolbar/visibility", m_toolbar->isVisible());
m_appSettings.setValue("paletteWindow/visibility", m_paletteDocker->isVisible());
m_appSettings.setValue("projectWindow/visibility", m_projectDocker->isVisible());
//m_appSettings.setValue("layersWindow/visibility", m_layersDocker->isVisible());
}
#ifdef SPROXEL_USE_PYTHON
if (event->isAccepted()) close_python_console();
#endif
}
void MainWindow::keyPressEvent(QKeyEvent* event)
{
const bool altDown = event->modifiers() & Qt::AltModifier;
const bool ctrlDown = event->modifiers() & Qt::ControlModifier;
//const bool shiftDown = event->modifiers() & Qt::ShiftModifier;
if (altDown && event->key() == Qt::Key_X)
{
m_actAxisX->setChecked(true);
}
else if (altDown && event->key() == Qt::Key_Y)
{
m_actAxisY->setChecked(true);
}
else if (altDown && event->key() == Qt::Key_Z)
{
m_actAxisZ->setChecked(true);
}
else if (ctrlDown && event->key() == Qt::Key_C)
{
QColor color = QColorDialog::getColor(Qt::white, this);
m_paletteWidget->setActiveColor(Imath::Color4f((float)color.red()/255.0f,
(float)color.green()/255.0f,
(float)color.blue()/255.0f,
(float)color.alpha()/255.0f),
-1);
}
else if (ctrlDown && event->key() == Qt::Key_F)
{
// Frame the full extents no matter what
m_glModelWidget->frame(true);
}
else if (event->key() == Qt::Key_F)
{
// Frame the data if it exists
m_glModelWidget->frame(false);
}
else if (event->key() == Qt::Key_X)
{
m_paletteWidget->swapColors();
}
//else if (event->key() == Qt::Key_D)
//{
// m_paletteWidget->setDefaultColors();
//}
else if (event->key() == Qt::Key_Q) m_actToolSplat->setChecked(true);
else if (event->key() == Qt::Key_W) m_actToolReplace->setChecked(true);
else if (event->key() == Qt::Key_E) m_actToolFlood->setChecked(true);
else if (event->key() == Qt::Key_R) m_actToolDropper->setChecked(true);
else if (event->key() == Qt::Key_T) m_actToolEraser->setChecked(true);
else if (event->key() == Qt::Key_Y) m_actToolSlab->setChecked(true);
else if (event->key() == Qt::Key_U) m_actToolLine->setChecked(true);
else if (event->key() == Qt::Key_I) m_actToolBox->setChecked(true);
else if (event->key() == Qt::Key_O) m_actToolExtrude->setChecked(true);
else if (event->key() >= Qt::Key_Left && event->key() <= Qt::Key_PageDown)
{
m_glModelWidget->handleArrows(event);
}
else if (event->key() == Qt::Key_Space)
{
// It's okay to call setVoxelColor once on the model widget, but any more requires an internal wrapper
m_glModelWidget->setVoxelColor(m_glModelWidget->activeVoxel(),
m_glModelWidget->activeColor(),
m_glModelWidget->activeIndex());
m_glModelWidget->updateGL();
}
else if (event->key() == Qt::Key_Delete)
{
// It's okay to call setVoxelColor once on the model widget, but any more requires an internal wrapper
m_glModelWidget->setVoxelColor(m_glModelWidget->activeVoxel(),
Imath::Color4f(0.0f, 0.0f, 0.0f, 0.0f),
0);
m_glModelWidget->updateGL();
}
}
int MainWindow::fileModifiedDialog()
{
QMessageBox msgBox;
msgBox.setText("The document has been modified.");
msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
return msgBox.exec();
}
void MainWindow::newGrid()
{
// Confirmation dialog
if (!m_undoManager.isClean())
{
switch (fileModifiedDialog())
{
case QMessageBox::Save: saveFile(); break;
case QMessageBox::Discard: break;
case QMessageBox::Cancel: return; break;
}
}
NewGridDialog dlg(this);
dlg.setModal(true);
if (dlg.exec())
{
m_activeFilename = "";
m_undoManager.clear();
setWindowTitle(BASE_WINDOW_TITLE + " - " + m_activeFilename); // TODO: Functionize (resetWindowTitle)
// create new project
m_project=new SproxelProject();
VoxelGridGroupPtr sprite(new VoxelGridGroup(dlg.getVoxelSize(),
dlg.isIndexed()?m_project->mainPalette:ColorPalettePtr()));
sprite->setName("unnamed");
m_project->sprites.push_back(sprite);
m_glModelWidget->setSprite(sprite);
m_paletteWidget->setPalette(m_project->mainPalette);
m_projectWidget->setProject(m_project);
}
}
void MainWindow::saveFile()
{
if (m_undoManager.isClean())
return;
if (m_activeFilename == "")
return saveFileAs();
#ifdef SPROXEL_USE_PYTHON
bool success = save_project(m_activeFilename, m_project);
#else
bool success = false;
#endif
if (success)
m_undoManager.setClean();
else
QMessageBox::critical(this, "Sproxel Error", QString("Error saving project to file ")+m_activeFilename);
}
void MainWindow::saveFileAs()
{
QFileDialog fd(this, "Save voxel file as...");
fd.setNameFilter(tr("Sproxel project (*.sxl)"));
fd.setAcceptMode(QFileDialog::AcceptSave);
fd.exec();
QStringList qsl = fd.selectedFiles();
if (qsl.isEmpty()) return;
if (QFileInfo(qsl[0]).isDir()) return; // It returns the directory if you press Cancel
QString filename = qsl[0];
QString activeFilter = fd.selectedNameFilter();
// Switch on save type
bool success = false;
if (!filename.endsWith(".sxl", Qt::CaseInsensitive))
filename.append(".sxl");
#ifdef SPROXEL_USE_PYTHON
success = save_project(filename, m_project);
#endif
if (success)
{
m_undoManager.setClean();
m_activeFilename = filename;
setWindowTitle(BASE_WINDOW_TITLE + " - " + m_activeFilename); // TODO: Functionize (resetWindowTitle)
}
else
QMessageBox::critical(this, "Sproxel Error", QString("Error saving project to file ")+filename);
}
void MainWindow::openFile()
{
#if 0
QString filename = QFileDialog::getOpenFileName(this,
tr("Select file to Open..."),
QString(),
tr("Sproxel projects (*.sxl)"));
#else
// Allow opening .png etc
const QList<Importer*> &importers=get_importers();
QStringList filters;
filters += tr("Sproxel projects (*.sxl)");
foreach (Importer *imp, importers) filters += imp->name()+" ("+imp->filter()+")";
QFileDialog fd(this, "Select file to Open...");
fd.setNameFilters(filters);
fd.setAcceptMode(QFileDialog::AcceptOpen);
fd.setFileMode(QFileDialog::ExistingFiles);
if (!fd.exec()) return;
QStringList files=fd.selectedFiles();
if (files.isEmpty()) return;
QString filename = files[0];
#endif
if (filename.isEmpty())
return;
openFile(filename);
}
void MainWindow::openFile(QString filename)
{
// Confirmation dialog
if (!m_undoManager.isClean())
{
switch (fileModifiedDialog())
{
case QMessageBox::Save: saveFile(); break;
case QMessageBox::Discard: break;
case QMessageBox::Cancel: return; break;
}
}
bool success = false;
#ifdef SPROXEL_USE_PYTHON
SproxelProjectPtr project = load_project(filename);
#else
SproxelProjectPtr project = SproxelProjectPtr(new SproxelProject());
const QList<Importer*> &importers=get_importers();
Importer* importer = NULL;
foreach (Importer *imp, importers) {
if (imp->filter() == "*.png") { // HACK FIXME
importer = imp;
break;
}
}
importer->doImport(filename, &m_undoManager, project, m_glModelWidget->getSprite());
#endif
if (project) { m_project=project; success=true; }
if (success)
{
m_undoManager.clear();
if (m_project->sprites.empty())
{
VoxelGridGroupPtr sprite(new VoxelGridGroup(
Imath::V3i(DEFAULT_VOXGRID_SZ, DEFAULT_VOXGRID_SZ, DEFAULT_VOXGRID_SZ),
ColorPalettePtr()));
sprite->setName("unnamed");
m_project->sprites.push_back(sprite);
}
m_glModelWidget->setSprite(m_project->sprites[0]);
m_paletteWidget->setPalette(m_project->mainPalette);
m_projectWidget->setProject(m_project);
m_activeFilename = filename;
setWindowTitle(BASE_WINDOW_TITLE + " - " + m_activeFilename); // TODO: Functionize (resetWindowTitle)
if (m_appSettings.value("frameOnOpen", false).toBool())
m_glModelWidget->frame(true);
}
else
QMessageBox::critical(this, "Sproxel Error", QString("Error loading project from file ")+filename);
}
void MainWindow::import()
{
const QList<Importer*> &importers=get_importers();
QStringList filters;
filters.reserve(importers.size());
foreach (Importer *imp, importers) filters += imp->name()+" ("+imp->filter()+")";
QFileDialog fd(this, "Import file...");
fd.setNameFilters(filters);
fd.setAcceptMode(QFileDialog::AcceptOpen);
fd.setFileMode(QFileDialog::ExistingFiles);
if (!fd.exec()) return;
QStringList files=fd.selectedFiles();
if (files.isEmpty()) return;
QString activeFilter=fd.selectedNameFilter();
Importer *activeImporter=NULL;
for (int i=0; i<filters.size(); ++i)
if (filters[i]==activeFilter) { activeImporter=importers[i]; break; }
if (!activeImporter) return;
foreach (const QString &filename, files)
{
if (QFileInfo(filename).isDir()) continue;
//import(activeImporter, filename);
activeImporter->doImport(filename, &m_undoManager, m_project, m_glModelWidget->getSprite());
}
}
void MainWindow::import(Importer* activeImporter, QString filename)
{
m_project = SproxelProjectPtr(new SproxelProject());
m_undoManager.clear();
if (m_project->sprites.empty())
{
VoxelGridGroupPtr sprite(new VoxelGridGroup(
Imath::V3i(DEFAULT_VOXGRID_SZ, DEFAULT_VOXGRID_SZ, DEFAULT_VOXGRID_SZ),
ColorPalettePtr()));
sprite->setName("unnamed");
m_project->sprites.push_back(sprite);
}
m_glModelWidget->setSprite(m_project->sprites[0]);
m_paletteWidget->setPalette(m_project->mainPalette);
m_projectWidget->setProject(m_project);
m_activeFilename = filename;
setWindowTitle(BASE_WINDOW_TITLE + " - " + m_activeFilename); // TODO: Functionize (resetWindowTitle)
if (m_appSettings.value("frameOnOpen", false).toBool())
m_glModelWidget->frame(true);
activeImporter->doImport(filename, &m_undoManager, m_project, m_glModelWidget->getSprite());
}
void MainWindow::exportGrid()
{
const QList<Exporter*> &exporters=get_exporters();
QStringList filters;
filters.reserve(exporters.size());
foreach (Exporter *exp, exporters) filters += exp->name()+" ("+exp->filter()+")";
QFileDialog fd(this, "Export file...");
fd.setNameFilters(filters);
fd.setAcceptMode(QFileDialog::AcceptSave);
fd.selectNameFilter(m_appSettings.value("lastExportFilter", QString()).toString());
fd.selectFile(m_appSettings.value("lastExportFile", QString()).toString());
if (!fd.exec()) return;
QStringList files=fd.selectedFiles();
if (files.isEmpty()) return;
QString filename=files[0];
if (QFileInfo(filename).isDir()) return;
QString activeFilter=fd.selectedNameFilter();
Exporter *activeExporter=NULL;
for (int i=0; i<filters.size(); ++i)
if (filters[i]==activeFilter) { activeExporter=exporters[i]; break; }
if (!activeExporter) return;
if (!activeExporter->doExport(filename, m_project, m_glModelWidget->getSprite()))
QMessageBox::critical(this, "Sproxel Error", QString("Failed to export ")+filename);
m_appSettings.setValue("lastExportFile", filename);
m_appSettings.setValue("lastExportFilter", activeFilter);
}
void MainWindow::editPreferences()
{
PreferencesDialog dlg(this, &m_appSettings);
dlg.setModal(true);
QObject::connect(&dlg, SIGNAL(preferenceChanged()), m_glModelWidget, SLOT(updateGL()));
dlg.exec();
}
// Trampoline functions because QSignalMapper can't do complex args
// Search for QBoundMethod for a custom approach, but I'm too lazy to include it for now.
void MainWindow::shiftUp()
{
m_glModelWidget->shiftVoxels(m_glModelWidget->currentAxis(),
true, m_glModelWidget->shiftWrap());
}
void MainWindow::shiftDown()
{
m_glModelWidget->shiftVoxels(m_glModelWidget->currentAxis(),
false, m_glModelWidget->shiftWrap());
}
void MainWindow::rotateCw()
{
m_glModelWidget->rotateVoxels(m_glModelWidget->currentAxis(), 1);
}
void MainWindow::rotateCcw()
{
m_glModelWidget->rotateVoxels(m_glModelWidget->currentAxis(), -1);
}
void MainWindow::mirror()
{
m_glModelWidget->mirrorVoxels(m_glModelWidget->currentAxis());
}
void MainWindow::extendContractShift(int axis, int incr, int shift)
{
Imath::V3i shiftVec(0,0,0);
Imath::V3i sizeInc(0,0,0);
switch ((m_glModelWidget->currentAxis() + axis) % 3)
{
case 0: sizeInc.x += incr; shiftVec.x += shift; break;
case 1: sizeInc.y += incr; shiftVec.y += shift; break;
case 2: sizeInc.z += incr; shiftVec.z += shift; break;
}
m_glModelWidget->resizeAndShiftVoxelGrid(sizeInc, shiftVec);
}
void MainWindow::extendUp() {
extendContractShift(0, 1, 0);
}
void MainWindow::extendDown() {
extendContractShift(0, 1, 1);
}
void MainWindow::extendLeft() {
extendContractShift(1, 1, 0);
}
void MainWindow::extendRight() {
extendContractShift(1, 1, 1);
}
void MainWindow::extendFront() {
extendContractShift(2, 1, 0);
}
void MainWindow::extendBack() {
extendContractShift(2, 1, 1);
}
void MainWindow::contractUp() {
extendContractShift(0, -1, 0);
}
void MainWindow::contractDown() {
extendContractShift(0, -1, -1);
}
void MainWindow::contractLeft() {
extendContractShift(1, -1, 0);
}
void MainWindow::contractRight() {
extendContractShift(1, -1, -1);
}
void MainWindow::contractFront() {
extendContractShift(2, -1, 0);
}
void MainWindow::contractBack() {
extendContractShift(2, -1, -1);
}
void MainWindow::upRes()
{
m_glModelWidget->reresVoxelGrid(2.0f);
m_glModelWidget->frame(true);
}
void MainWindow::downRes() {
m_glModelWidget->reresVoxelGrid(0.5f);
m_glModelWidget->frame(true);
}
void MainWindow::setToolSplat(bool stat) { if (stat) m_glModelWidget->setActiveTool(TOOL_SPLAT); }
void MainWindow::setToolFlood(bool stat) { if (stat) m_glModelWidget->setActiveTool(TOOL_FLOOD); }
void MainWindow::setToolRay(bool stat) { if (stat) m_glModelWidget->setActiveTool(TOOL_RAY); }
void MainWindow::setToolDropper(bool stat) { if (stat) m_glModelWidget->setActiveTool(TOOL_DROPPER); }
void MainWindow::setToolEraser(bool stat) { if (stat) m_glModelWidget->setActiveTool(TOOL_ERASER); }
void MainWindow::setToolReplace(bool stat) { if (stat) m_glModelWidget->setActiveTool(TOOL_REPLACE); }
void MainWindow::setToolSlab(bool stat) { if (stat) m_glModelWidget->setActiveTool(TOOL_SLAB); }
void MainWindow::setToolLine(bool stat) { if (stat) m_glModelWidget->setActiveTool(TOOL_LINE); }
void MainWindow::setToolBox(bool stat) { if (stat) m_glModelWidget->setActiveTool(TOOL_BOX); }
void MainWindow::setToolExtrude(bool stat) { if (stat) m_glModelWidget->setActiveTool(TOOL_EXTRUDE); }
void MainWindow::reactToModified(bool clean)
{
QString current = windowTitle();
if (!clean)
{
if (current.endsWith("*"))
return;
setWindowTitle(current + "*");
}
else
{
current.chop(1);
setWindowTitle(current);
}
}