forked from mapmapteam/mapmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cpp
2434 lines (2061 loc) · 73.9 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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* MainWindow.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MainWindow.h"
#include "ProjectWriter.h"
#include "ProjectReader.h"
#include <sstream>
#include <string>
MainWindow::MainWindow()
{
// Create model.
if (Media::hasVideoSupport())
std::cout << "Video support: yes" << std::endl;
else
std::cout << "Video support: no" << std::endl;
mappingManager = new MappingManager;
// Initialize internal variables.
currentPaintId = NULL_UID;
currentMappingId = NULL_UID;
// TODO: not sure we need this anymore since we have NULL_UID
_hasCurrentPaint = false;
_hasCurrentMapping = false;
currentSelectedItem = NULL;
// Play state.
_isPlaying = false;
// Create everything.
createLayout();
createActions();
createMenus();
createContextMenu();
createToolBars();
createStatusBar();
updateRecentFileActions();
updateRecentVideoActions();
// Load settings.
readSettings();
// Start osc.
startOscReceiver();
// Defaults.
//setWindowIcon(QIcon(":/images/icon.png"));
setCurrentFile("");
// Create and start timer.
videoTimer = new QTimer(this);
videoTimer->setInterval( int( 1000 / MM::FRAMES_PER_SECOND ) );
connect(videoTimer, SIGNAL(timeout()), this, SLOT(updateCanvases()));
videoTimer->start();
// Start playing by default.
play();
// after readSettings():
_preferences_dialog = new PreferencesDialog(this, this);
}
MainWindow::~MainWindow()
{
delete mappingManager;
// delete _facade;
#ifdef HAVE_OSC
delete osc_timer;
#endif // ifdef
}
void MainWindow::handlePaintItemSelectionChanged()
{
// Set current paint.
QListWidgetItem* item = paintList->currentItem();
currentSelectedItem = item;
// Is a paint item selected?
bool paintItemSelected = (item ? true : false);
if (paintItemSelected)
{
// Set current paint.
uid paintId = getItemId(*item);
// Unselect current mapping.
if (currentPaintId != paintId)
removeCurrentMapping();
// Set current paint.
setCurrentPaint(paintId);
}
else
removeCurrentPaint();
// Enable/disable creation of mappings depending on whether a paint is selected.
addMeshAction->setEnabled(paintItemSelected);
addTriangleAction->setEnabled(paintItemSelected);
addEllipseAction->setEnabled(paintItemSelected);
// Update canvases.
updateCanvases();
}
void MainWindow::handleMappingItemSelectionChanged()
{
if (mappingList->selectedItems().empty())
{
removeCurrentMapping();
}
else
{
QListWidgetItem* item = mappingList->currentItem();
currentSelectedItem = item;
// Set current paint and mappings.
uid mappingId = getItemId(*item);
Mapping::ptr mapping = mappingManager->getMappingById(mappingId);
uid paintId = mapping->getPaint()->getId();
setCurrentMapping(mappingId);
setCurrentPaint(paintId);
}
// Update canvases.
updateCanvases();
}
void MainWindow::setMappingItemVisibility(uid mappingId, bool visible)
{
Mapping::ptr mapping = mappingManager->getMappingById(mappingId);
mapping->setVisible(visible);
// Update canvases.
updateCanvases();
}
void MainWindow::handleMappingItemChanged(QListWidgetItem* item)
{
// Toggle visibility of mapping depending on checkbox of item.
uid mappingId = getItemId(*item);
setMappingItemVisibility(mappingId, item->checkState() == Qt::Checked);
}
void MainWindow::handleMappingIndexesMoved()
{
// Reorder mappings.
QVector<uid> newOrder;
for (int row=mappingList->count()-1; row>=0; row--)
{
uid layerId = mappingList->item(row)->data(Qt::UserRole).toInt();
newOrder.push_back(layerId);
}
mappingManager->reorderMappings(newOrder);
// Update canvases according to new order.
updateCanvases();
}
void MainWindow::handleItemSelected(QListWidgetItem* item)
{
Q_UNUSED(item);
// Change currently selected item.
currentSelectedItem = item;
}
//void MainWindow::handleItemDoubleClicked(QListWidgetItem* item)
//{
// // Change currently selected item.
// Paint::ptr paint = mappingManager->getPaintById(getItemId(*item));
// uid curMappingId = getCurrentMappingId();
// removeCurrentMapping();
// removeCurrentPaint();
//
// //qDebug() << "DOUBLE CLICK! " << endl;
// videoTimer->stop();
// if (paint->getType() == "media") {
// QString fileName = QFileDialog::getOpenFileName(this,
// tr("Import media source file"), ".");
// // Restart video playback. XXX Hack
// videoTimer->start();
// if (!fileName.isEmpty())
// importMediaFile(fileName, paint, false);
// }
// if (paint->getType() == "image") {
// QString fileName = QFileDialog::getOpenFileName(this,
// tr("Import media source file"), ".");
// // Restart video playback. XXX Hack
// videoTimer->start();
// if (!fileName.isEmpty())
// importMediaFile(fileName, paint, true);
// }
// else if (paint->getType() == "color") {
// // Pop-up color-choosing dialog to choose color paint.
// QColor initialColor;
// QColor color = QColorDialog::getColor(initialColor, this);
// videoTimer->start();
// if (color.isValid())
// addColorPaint(color, paint);
// }
//
// if (curMappingId != NULL_UID)
// setCurrentMapping(curMappingId);
//}
void MainWindow::handlePaintChanged(Paint::ptr paint) {
// Change currently selected item.
uid curMappingId = getCurrentMappingId();
removeCurrentMapping();
removeCurrentPaint();
uid paintId = mappingManager->getPaintId(paint);
if (paint->getType() == "media") {
std::tr1::shared_ptr<Media> media = std::tr1::static_pointer_cast<Media>(paint);
Q_CHECK_PTR(media);
updatePaintItem(paintId, createFileIcon(media->getUri()), strippedName(media->getUri()));
// QString fileName = QFileDialog::getOpenFileName(this,
// tr("Import media source file"), ".");
// // Restart video playback. XXX Hack
// if (!fileName.isEmpty())
// importMediaFile(fileName, paint, false);
}
if (paint->getType() == "image") {
std::tr1::shared_ptr<Image> image = std::tr1::static_pointer_cast<Image>(paint);
Q_CHECK_PTR(image);
updatePaintItem(paintId, createImageIcon(image->getUri()), strippedName(image->getUri()));
// QString fileName = QFileDialog::getOpenFileName(this,
// tr("Import media source file"), ".");
// // Restart video playback. XXX Hack
// if (!fileName.isEmpty())
// importMediaFile(fileName, paint, true);
}
else if (paint->getType() == "color") {
// Pop-up color-choosing dialog to choose color paint.
std::tr1::shared_ptr<Color> color = std::tr1::static_pointer_cast<Color>(paint);
Q_CHECK_PTR(color);
updatePaintItem(paintId, createColorIcon(color->getColor()), strippedName(color->getColor().name()));
}
if (curMappingId != NULL_UID)
setCurrentMapping(curMappingId);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
// Stop video playback to avoid lags. XXX Hack
videoTimer->stop();
// Popup dialog allowing the user to save before closing.
if (okToContinue())
{
writeSettings();
event->accept();
}
else
{
event->ignore();
}
// Restart video playback. XXX Hack
videoTimer->start();
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
bool eventKey = false;
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
eventKey = true;
// Menubar shortcut
if (keyEvent->modifiers() == Qt::CTRL)
{
switch (keyEvent->key()) {
case Qt::Key_F:
outputWindow->setFullScreen(true);
break;
case Qt::Key_N:
newFile();
break;
case Qt::Key_O:
open();
break;
case Qt::Key_S:
save();
break;
case Qt::Key_Q:
close();
break;
case Qt::Key_Delete:
deleteItem();
break;
case Qt::Key_M:
addMesh();
break;
case Qt::Key_T:
addTriangle();
break;
case Qt::Key_E:
addEllipse();
break;
case Qt::Key_D:
outputWindow->setVisible(true);
break;
case Qt::Key_P:
if (_isPlaying) pause(); else play();
break;
case Qt::Key_R:
rewind();
break;
}
}
else if (keyEvent->key() == Qt::Key_Escape) outputWindow->setFullScreen(false);
eventKey = false;
return eventKey;
}
else
{
// standard event processing
return QObject::eventFilter(obj, event);
}
}
void MainWindow::newFile()
{
// Stop video playback to avoid lags. XXX Hack
videoTimer->stop();
// Popup dialog allowing the user to save before creating a new file.
if (okToContinue())
{
clearWindow();
setCurrentFile("");
}
// Restart video playback. XXX Hack
videoTimer->start();
}
void MainWindow::open()
{
// Stop video playback to avoid lags. XXX Hack
videoTimer->stop();
// Popup dialog allowing the user to save before opening a new file.
if (okToContinue())
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open project"),
settings.value("defaultProjectDir").toString(),
tr("MapMap files (*.%1)").arg(MM::FILE_EXTENSION));
if (! fileName.isEmpty())
loadFile(fileName);
}
// Restart video playback. XXX Hack
videoTimer->start();
}
void MainWindow::preferences()
{
this->_preferences_dialog->show();
}
bool MainWindow::save()
{
// Popup save-as dialog if file has never been saved.
if (curFile.isEmpty())
{
return saveAs();
}
else
{
return saveFile(curFile);
}
}
bool MainWindow::saveAs()
{
// Stop video playback to avoid lags. XXX Hack
videoTimer->stop();
// Popul file dialog to choose filename.
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save project"), settings.value("defaultProjectDir").toString(),
tr("MapMap files (*.%1)").arg(MM::FILE_EXTENSION));
// Restart video playback. XXX Hack
videoTimer->start();
if (fileName.isEmpty())
return false;
if (! fileName.endsWith(MM::FILE_EXTENSION))
{
std::cout << "filename doesn't end with expected extension: " <<
fileName.toStdString() << std::endl;
fileName.append(".");
fileName.append(MM::FILE_EXTENSION);
}
// Save to filename.
return saveFile(fileName);
}
void MainWindow::importVideo()
{
// Stop video playback to avoid lags. XXX Hack
videoTimer->stop();
// Pop-up file-choosing dialog to choose media file.
// TODO: restrict the type of files that can be imported
QString fileName = QFileDialog::getOpenFileName(this, tr("Import media source file"), settings.value("defaultVideoDir").toString(), tr("Video files (%1);;All files (*)").arg(MM::VIDEO_FILES_FILTER));
// Restart video playback. XXX Hack
videoTimer->start();
if (!fileName.isEmpty())
importMediaFile(fileName, false);
}
void MainWindow::importImage()
{
// Stop video playback to avoid lags. XXX Hack
videoTimer->stop();
// Pop-up file-choosing dialog to choose media file.
// TODO: restrict the type of files that can be imported
QString fileName = QFileDialog::getOpenFileName(this,
tr("Import media source file"), settings.value("defaultImageDir").toString(), tr("Image files (%1);;All files (*)").arg(MM::IMAGE_FILES_FILTER));
// Restart video playback. XXX Hack
videoTimer->start();
if (!fileName.isEmpty())
importMediaFile(fileName, true);
}
void MainWindow::addColor()
{
// Stop video playback to avoid lags. XXX Hack
videoTimer->stop();
// Pop-up color-choosing dialog to choose color paint.
QColor initialColor;
QColor color = QColorDialog::getColor(initialColor, this);
if (color.isValid())
addColorPaint(color);
// Restart video playback. XXX Hack
videoTimer->start();
}
void MainWindow::addMesh()
{
// A paint must be selected to add a mapping.
if (getCurrentPaintId() == NULL_UID)
return;
// Disable Test signal when add Mesh
outputWindow->getCanvas()->enableTestSignal(false);
// Retrieve current paint (as texture).
Paint::ptr paint = getMappingManager().getPaintById(getCurrentPaintId());
Q_CHECK_PTR(paint);
// Create input and output quads.
Mapping* mappingPtr;
if (paint->getType() == "color")
{
Shape::ptr outputQuad = Shape::ptr(Util::createQuadForColor(sourceCanvas->width(), sourceCanvas->height()));
mappingPtr = new ColorMapping(paint, outputQuad);
}
else
{
std::tr1::shared_ptr<Texture> texture = std::tr1::static_pointer_cast<Texture>(paint);
Q_CHECK_PTR(texture);
Shape::ptr outputQuad = Shape::ptr(Util::createMeshForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height()));
Shape::ptr inputQuad = Shape::ptr(Util::createMeshForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height()));
mappingPtr = new TextureMapping(paint, outputQuad, inputQuad);
}
// Create texture mapping.
Mapping::ptr mapping(mappingPtr);
uint mappingId = mappingManager->addMapping(mapping);
addMappingItem(mappingId);
}
void MainWindow::addTriangle()
{
// A paint must be selected to add a mapping.
if (getCurrentPaintId() == NULL_UID)
return;
// Disable Test signal when add Triangle
outputWindow->getCanvas()->enableTestSignal(false);
// Retrieve current paint (as texture).
Paint::ptr paint = getMappingManager().getPaintById(getCurrentPaintId());
Q_CHECK_PTR(paint);
// Create input and output quads.
Mapping* mappingPtr;
if (paint->getType() == "color")
{
Shape::ptr outputTriangle = Shape::ptr(Util::createTriangleForColor(sourceCanvas->width(), sourceCanvas->height()));
mappingPtr = new ColorMapping(paint, outputTriangle);
}
else
{
std::tr1::shared_ptr<Texture> texture = std::tr1::static_pointer_cast<Texture>(paint);
Q_CHECK_PTR(texture);
Shape::ptr outputTriangle = Shape::ptr(Util::createTriangleForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height()));
Shape::ptr inputTriangle = Shape::ptr(Util::createTriangleForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height()));
mappingPtr = new TextureMapping(paint, inputTriangle, outputTriangle);
}
// Create mapping.
Mapping::ptr mapping(mappingPtr);
uint mappingId = mappingManager->addMapping(mapping);
addMappingItem(mappingId);
}
void MainWindow::addEllipse()
{
// A paint must be selected to add a mapping.
if (getCurrentPaintId() == NULL_UID)
return;
// Disable Test signal when add Ellipse
outputWindow->getCanvas()->enableTestSignal(false);
// Retrieve current paint (as texture).
Paint::ptr paint = getMappingManager().getPaintById(getCurrentPaintId());
Q_CHECK_PTR(paint);
// Create input and output ellipses.
Mapping* mappingPtr;
if (paint->getType() == "color")
{
Shape::ptr outputEllipse = Shape::ptr(Util::createEllipseForColor(sourceCanvas->width(), sourceCanvas->height()));
mappingPtr = new ColorMapping(paint, outputEllipse);
}
else
{
std::tr1::shared_ptr<Texture> texture = std::tr1::static_pointer_cast<Texture>(paint);
Q_CHECK_PTR(texture);
Shape::ptr outputEllipse = Shape::ptr(Util::createEllipseForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height()));
Shape::ptr inputEllipse = Shape::ptr(Util::createEllipseForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height()));
mappingPtr = new TextureMapping(paint, inputEllipse, outputEllipse);
}
// Create mapping.
Mapping::ptr mapping(mappingPtr);
uint mappingId = mappingManager->addMapping(mapping);
addMappingItem(mappingId);
}
void MainWindow::play()
{
// Update buttons.
playAction->setVisible(false);
pauseAction->setVisible(true);
_isPlaying = true;
// Start all paints.
for (int i=0; i<mappingManager->nPaints(); i++)
mappingManager->getPaint(i)->play();
}
void MainWindow::pause()
{
// Update buttons.
playAction->setVisible(true);
pauseAction->setVisible(false);
_isPlaying = false;
// Pause all paints.
for (int i=0; i<mappingManager->nPaints(); i++)
mappingManager->getPaint(i)->pause();
}
void MainWindow::rewind()
{
// Rewind all paints.
for (int i=0; i<mappingManager->nPaints(); i++)
mappingManager->getPaint(i)->rewind();
}
void MainWindow::about()
{
// Stop video playback to avoid lags. XXX Hack
videoTimer->stop();
// Pop-up about dialog.
QMessageBox::about(this, tr("About MapMap"),
tr("<h2><img src=\":mapmap-title\"/> %1</h2>"
"<p>Copyright © 2013 %2.</p>"
"<p>MapMap is a free software for video mapping.</p>"
"<p>Projection mapping, also known as video mapping and spatial augmented reality, "
"is a projection technology used to turn objects, often irregularly shaped, into "
"a display surface for video projection. These objects may be complex industrial "
"landscapes, such as buildings. By using specialized software, a two or three "
"dimensional object is spatially mapped on the virtual program which mimics the "
"real environment it is to be projected on. The software can interact with a "
"projector to fit any desired image onto the surface of that object. This "
"technique is used by artists and advertisers alike who can add extra dimensions, "
"optical illusions, and notions of movement onto previously static objects. The "
"video is commonly combined with, or triggered by, audio to create an "
"audio-visual narrative."
"This project was made possible by the support of the International Organization of "
"La Francophonie.</p>"
"<p>http://mapmap.info<br />"
"http://www.francophonie.org</p>"
).arg(MM::VERSION, MM::COPYRIGHT_OWNERS));
// Restart video playback. XXX Hack
videoTimer->start();
}
void MainWindow::updateStatusBar()
{
// Nothing to do for now.
// locationLabel->setText(spreadsheet->currentLocation());
// formulaLabel->setText(spreadsheet->currentFormula());
}
/**
* Called when the user wants to delete an item.
*
* Deletes either a Paint or a Mapping.
*/
void MainWindow::deleteItem()
{
bool isMappingTabSelected = (mappingSplitter == contentTab->currentWidget());
bool isPaintTabSelected = (paintSplitter == contentTab->currentWidget());
if (currentSelectedItem)
{
if (isMappingTabSelected) //currentSelectedItem->listWidget() == mappingList)
{
// Delete mapping.
deleteMapping( getItemId(*mappingList->currentItem()) );
//currentSelectedItem = NULL;
}
else if (isPaintTabSelected) //currentSelectedItem->listWidget() == paintList)
{
// Delete paint.
deletePaint( getItemId(*paintList->currentItem()), false );
//currentSelectedItem = NULL;
}
else
{
qCritical() << "Selected item neither a mapping nor a paint." << endl;
}
}
}
void MainWindow::openRecentFile()
{
QAction *action = qobject_cast<QAction *>(sender());
if (action)
loadFile(action->data().toString());
}
void MainWindow::openRecentVideo()
{
QAction *action = qobject_cast<QAction *>(sender());
if (action)
importMediaFile(action->data().toString(),false);
}
bool MainWindow::clearProject()
{
// Disconnect signals to avoid problems when clearning mappingList and paintList.
disconnectProjectWidgets();
// Clear current paint / mapping.
removeCurrentPaint();
removeCurrentMapping();
// Empty list widgets.
mappingList->clear();
paintList->clear();
// Clear property panel.
for (int i=mappingPropertyPanel->count()-1; i>=0; i--)
mappingPropertyPanel->removeWidget(mappingPropertyPanel->widget(i));
// Disable property panel.
mappingPropertyPanel->setDisabled(true);
// Clear list of mappers.
mappers.clear();
// Clear list of paint guis.
paintGuis.clear();
// Clear model.
mappingManager->clearAll();
// Refresh GL canvases to clear them out.
sourceCanvas->repaint();
destinationCanvas->repaint();
// Reconnect everything.
connectProjectWidgets();
// Window was modified.
windowModified();
return true;
}
uid MainWindow::createMediaPaint(uid paintId, QString uri, float x, float y,
bool isImage, bool live, double rate)
{
// Cannot create image with already existing id.
if (Paint::getUidAllocator().exists(paintId))
return NULL_UID;
else
{
Texture* tex = 0;
if (isImage)
tex = new Image(uri, paintId);
else {
tex = new Media(uri, live, rate, paintId);
}
// Create new image with corresponding ID.
tex->setPosition(x, y);
// Add it to the manager.
Paint::ptr paint(tex);
// Add paint to model and return its uid.
uid id = mappingManager->addPaint(paint);
// Add paint widget item.
addPaintItem(id, isImage ? createImageIcon(uri) : createFileIcon(uri), strippedName(uri));
return id;
}
}
uid MainWindow::createColorPaint(uid paintId, QColor color)
{
// Cannot create image with already existing id.
if (Paint::getUidAllocator().exists(paintId))
return NULL_UID;
else
{
Color* img = new Color(color, paintId);
// Add it to the manager.
Paint::ptr paint(img);
// Add paint to model and return its uid.
uid id = mappingManager->addPaint(paint);
// Add paint widget item.
addPaintItem(id, createColorIcon(color), strippedName(color.name()));
return id;
}
}
uid MainWindow::createMeshTextureMapping(uid mappingId,
uid paintId,
int nColumns, int nRows,
const QVector<QPointF> &src, const QVector<QPointF> &dst)
{
// Cannot create element with already existing id or element for which no paint exists.
if (Mapping::getUidAllocator().exists(mappingId) ||
!Paint::getUidAllocator().exists(paintId) ||
paintId == NULL_UID)
return NULL_UID;
else
{
Paint::ptr paint = mappingManager->getPaintById(paintId);
int nVertices = nColumns * nRows;
qDebug() << nVertices << " vs " << nColumns << "x" << nRows << " vs " << src.size() << " " << dst.size() << endl;
Q_ASSERT(src.size() == nVertices && dst.size() == nVertices);
Shape::ptr inputMesh( new Mesh(src, nColumns, nRows));
Shape::ptr outputMesh(new Mesh(dst, nColumns, nRows));
// Add it to the manager.
Mapping::ptr mapping(new TextureMapping(paint, outputMesh, inputMesh, mappingId));
uid id = mappingManager->addMapping(mapping);
// Add it to the GUI.
addMappingItem(mappingId);
// Return the id.
return id;
}
}
uid MainWindow::createTriangleTextureMapping(uid mappingId,
uid paintId,
const QVector<QPointF> &src, const QVector<QPointF> &dst)
{
// Cannot create element with already existing id or element for which no paint exists.
if (Mapping::getUidAllocator().exists(mappingId) ||
!Paint::getUidAllocator().exists(paintId) ||
paintId == NULL_UID)
return NULL_UID;
else
{
Paint::ptr paint = mappingManager->getPaintById(paintId);
Q_ASSERT(src.size() == 3 && dst.size() == 3);
Shape::ptr inputTriangle( new Triangle(src[0], src[1], src[2]));
Shape::ptr outputTriangle(new Triangle(dst[0], dst[1], dst[2]));
// Add it to the manager.
Mapping::ptr mapping(new TextureMapping(paint, outputTriangle, inputTriangle, mappingId));
uid id = mappingManager->addMapping(mapping);
// Add it to the GUI.
addMappingItem(mappingId);
// Return the id.
return id;
}
}
uid MainWindow::createEllipseTextureMapping(uid mappingId,
uid paintId,
const QVector<QPointF> &src, const QVector<QPointF> &dst)
{
// Cannot create element with already existing id or element for which no paint exists.
if (Mapping::getUidAllocator().exists(mappingId) ||
!Paint::getUidAllocator().exists(paintId) ||
paintId == NULL_UID)
return NULL_UID;
else
{
Paint::ptr paint = mappingManager->getPaintById(paintId);
Q_ASSERT(src.size() == 5 && dst.size() == 5);
Shape::ptr inputEllipse( new Ellipse(src[0], src[1], src[2], src[3], src[4]));
Shape::ptr outputEllipse(new Ellipse(dst[0], dst[1], dst[2], dst[3], dst[4]));
// Add it to the manager.
Mapping::ptr mapping(new TextureMapping(paint, outputEllipse, inputEllipse, mappingId));
uid id = mappingManager->addMapping(mapping);
// Add it to the GUI.
addMappingItem(mappingId);
// Return the id.
return id;
}
}
uid MainWindow::createQuadColorMapping(uid mappingId,
uid paintId,
const QVector<QPointF> &dst)
{
// Cannot create element with already existing id or element for which no paint exists.
if (Mapping::getUidAllocator().exists(mappingId) ||
!Paint::getUidAllocator().exists(paintId) ||
paintId == NULL_UID)
return NULL_UID;
else
{
Paint::ptr paint = mappingManager->getPaintById(paintId);
Q_ASSERT(dst.size() == 4);
Shape::ptr outputQuad(new Quad(dst[0], dst[1], dst[2], dst[3]));
// Add it to the manager.
Mapping::ptr mapping(new ColorMapping(paint, outputQuad, mappingId));
uid id = mappingManager->addMapping(mapping);
// Add it to the GUI.
addMappingItem(mappingId);
// Return the id.
return id;
}
}
uid MainWindow::createTriangleColorMapping(uid mappingId,
uid paintId,
const QVector<QPointF> &dst)
{
// Cannot create element with already existing id or element for which no paint exists.
if (Mapping::getUidAllocator().exists(mappingId) ||
!Paint::getUidAllocator().exists(paintId) ||
paintId == NULL_UID)
return NULL_UID;
else
{
Paint::ptr paint = mappingManager->getPaintById(paintId);
Q_ASSERT(dst.size() == 3);
Shape::ptr outputTriangle(new Triangle(dst[0], dst[1], dst[2]));
// Add it to the manager.
Mapping::ptr mapping(new ColorMapping(paint, outputTriangle, mappingId));
uid id = mappingManager->addMapping(mapping);
// Add it to the GUI.
addMappingItem(mappingId);
// Return the id.
return id;
}
}
uid MainWindow::createEllipseColorMapping(uid mappingId,
uid paintId,
const QVector<QPointF> &dst)
{
// Cannot create element with already existing id or element for which no paint exists.
if (Mapping::getUidAllocator().exists(mappingId) ||
!Paint::getUidAllocator().exists(paintId) ||
paintId == NULL_UID)
return NULL_UID;
else
{
Paint::ptr paint = mappingManager->getPaintById(paintId);
Q_ASSERT(dst.size() == 4);
Shape::ptr outputEllipse(new Ellipse(dst[0], dst[1], dst[2], dst[3]));
// Add it to the manager.
Mapping::ptr mapping(new ColorMapping(paint, outputEllipse, mappingId));
uid id = mappingManager->addMapping(mapping);
// Add it to the GUI.
addMappingItem(mappingId);
// Return the id.
return id;
}
}
void MainWindow::setMappingVisible(uid mappingId, bool visible)
{
QListWidgetItem* item = getItemFromId(*mappingList, mappingId);
Q_ASSERT( item );
item->setCheckState(visible ? Qt::Checked : Qt::Unchecked );
updateCanvases();
}
void MainWindow::setMappingSolo(uid mappingId, bool solo)
{
Q_UNUSED(mappingId);
Q_UNUSED(solo);
}
void MainWindow::setMappingLocked(uid mappingId, bool locked)
{
Q_UNUSED(mappingId);
Q_UNUSED(locked);
}
void MainWindow::deleteMapping(uid mappingId)
{
// Cannot delete unexisting mapping.
if (Mapping::getUidAllocator().exists(mappingId))
{
removeMappingItem(mappingId);
}
}
/// Deletes/removes a paint and all associated mappigns.
void MainWindow::deletePaint(uid paintId, bool replace)