-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.cpp
3322 lines (2819 loc) · 105 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
* (c) 2014 Dame Diongue -- baydamd(@)gmail(.)com
*
* 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 "PreferenceDialog.h"
#include "AboutDialog.h"
#include "Commands.h"
#include "ProjectWriter.h"
#include "ProjectReader.h"
#include <sstream>
#include <string>
namespace mmp {
MainWindow::MainWindow()
{
// Create model.
#if QT_VERSION >= 0x050500
QMessageLogger(__FILE__, __LINE__, 0).info() << "Video support: " <<
(Video::hasVideoSupport() ? "yes" : "no");
#else
QMessageLogger(__FILE__, __LINE__, 0).debug() << "Video support: " <<
(Video::hasVideoSupport() ? "yes" : "no");
#endif
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;
// Frames per second.
_framesPerSecond = (-1);
// Play state.
_isPlaying = false;
// Editing toggles.
_displayControls = true;
_displayPaintControls = true;
_stickyVertices = true;
_displayUndoStack = false;
_showMenuBar = true; // Show menubar by default
// UndoStack
undoStack = new QUndoStack(this);
// Create everything.
createLayout();
createActions();
createMenus();
createMappingContextMenu();
createPaintContextMenu();
createToolBars();
createStatusBar();
updateRecentFileActions();
updateRecentVideoActions();
// Load settings.
readSettings();
// Start osc.
startOscReceiver();
// Defaults.
setWindowIcon(QIcon(":/mapmap-logo"));
setCurrentFile("");
// Create and start timer.
videoTimer = new QTimer(this);
connect(videoTimer, SIGNAL(timeout()), this, SLOT(processFrame()));
setFramesPerSecond(MM::DEFAULT_FRAMES_PER_SECOND);
videoTimer->start();
// Create elapsed timer.
systemTimer = new QElapsedTimer;
systemTimer->start();
// Start playing by default.
play();
}
MainWindow::~MainWindow()
{
delete mappingManager;
// delete _facade;
#ifdef HAVE_OSC
delete osc_timer;
#endif // ifdef
delete systemTimer;
}
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);
// Enable some menus and buttons
sourceCanvasToolbar->enableZoomToolBar(paintItemSelected);
sourceMenu->setEnabled(paintItemSelected);
// Update canvases.
updateCanvases();
}
void MainWindow::handleMappingItemSelectionChanged(const QModelIndex &index)
{
// Set current paint and mappings.
uid mappingId = mappingListModel->getItemId(index);
Mapping::ptr mapping = mappingManager->getMappingById(mappingId);
uid paintId = mapping->getPaint()->getId();
// Set current mapping and paint
setCurrentMapping(mappingId);
setCurrentPaint(paintId);
// Enable destination zoom toolbar buttons and avoid loop
if (!destinationCanvasToolbar->buttonsAreEnable())
destinationCanvasToolbar->enableZoomToolBar(true);
// Update canvases.
updateCanvases();
}
void MainWindow::handleMappingItemChanged(const QModelIndex &index)
{
// Get item.
uid mappingId = mappingListModel->getItemId(index);
// Sync name.
Mapping::ptr mapping = mappingManager->getMappingById(mappingId);
Q_CHECK_PTR(mapping);
// Change properties.
mapping->setName(index.data(Qt::EditRole).toString());
mapping->setVisible(index.data(Qt::CheckStateRole).toBool());
mapping->setSolo(index.data(Qt::CheckStateRole + 1).toBool());
mapping->setLocked(index.data(Qt::CheckStateRole + 2).toBool());
updatePlayingState();
}
void MainWindow::handleMappingIndexesMoved()
{
// Reorder mappings.
QVector<uid> newOrder;
for (int row=mappingListModel->rowCount()-1; row>=0; row--)
{
uid layerId = mappingListModel->getIndexFromRow(row).data(Qt::UserRole).toInt();
newOrder.push_back(layerId);
}
mappingManager->reorderMappings(newOrder);
// Update canvases according to new order.
updateCanvases();
// Update playing state.
updatePlayingState();
}
void MainWindow::handlePaintItemSelected(QListWidgetItem* item)
{
Q_UNUSED(item);
// Change currently selected item.
currentSelectedItem = item;
}
void MainWindow::handlePaintChanged(Paint::ptr paint)
{
// Change currently selected item.
uid curMappingId = getCurrentMappingId();
removeCurrentMapping();
removeCurrentPaint();
uid paintId = mappingManager->getPaintId(paint);
if (paint->getType() == "media")
{
QSharedPointer<Video> media = qSharedPointerCast<Video>(paint);
Q_CHECK_PTR(media);
updatePaintItem(paintId, media->getIcon(), 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")
{
QSharedPointer<Image> image = qSharedPointerCast<Image>(paint);
Q_CHECK_PTR(image);
updatePaintItem(paintId, image->getIcon(), 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.
QSharedPointer<Color> color = qSharedPointerCast<Color>(paint);
Q_CHECK_PTR(color);
updatePaintItem(paintId, color->getIcon(), strippedName(color->getColor().name()));
}
if (curMappingId != NULL_UID)
{
setCurrentMapping(curMappingId);
}
updatePlayingState();
}
void MainWindow::mappingPropertyChanged(uid id, QString propertyName, QVariant value)
{
// Retrieve mapping.
Mapping::ptr mapping = mappingManager->getMappingById(id);
Q_CHECK_PTR(mapping);
// Send to mapping gui.
MappingGui::ptr mappingGui = getMappingGuiByMappingId(id);
Q_CHECK_PTR(mappingGui);
mappingGui->setValue(propertyName, value);
// Send to actions.
if (mapping == getCurrentMapping())
{
if (propertyName == "visible")
{
mappingHideAction->setChecked(!value.toBool());
}
else if (propertyName == "solo")
{
mappingSoloAction->setChecked(value.toBool());
}
else if (propertyName == "locked")
{
mappingLockedAction->setChecked(value.toBool());
}
}
// Send to list items.
const QModelIndex& index = mappingListModel->getIndexFromId(mapping->getId());
if (propertyName == "name")
{
mappingListModel->setData(index, mapping->getName(), Qt::EditRole);
}
else if (propertyName == "visible")
{
mappingListModel->setData(index, mapping->isVisible(), Qt::CheckStateRole);
}
else if (propertyName == "solo")
{
mappingListModel->setData(index, mapping->isSolo(), Qt::CheckStateRole + 1);
}
else if (propertyName == "locked")
{
mappingListModel->setData(index, mapping->isLocked(), Qt::CheckStateRole + 2);
}
updatePlayingState();
}
void MainWindow::paintPropertyChanged(uid id, QString propertyName, QVariant value)
{
// Retrieve paint.
Paint::ptr paint = mappingManager->getPaintById(id);
Q_CHECK_PTR(paint);
// Send to paint gui.
PaintGui::ptr paintGui = getPaintGuiByPaintId(id);
Q_CHECK_PTR(paintGui);
paintGui->setValue(propertyName, value);
// Send to list items.
QListWidgetItem* paintItem = getItemFromId(*paintList, id);
if (propertyName == "name")
paintItem->setText(paint->getName());
updatePlayingState();
}
void MainWindow::closeEvent(QCloseEvent *event)
{
// Stop video playback to avoid lags. XXX Hack
pause(false);
// Popup dialog allowing the user to save before closing.
if (okToContinue())
{
// Save settings
writeSettings();
// Close all top level widgets
for (QWidget *widget: QApplication::topLevelWidgets()) {
if (widget != this) { // Avoid recursion
widget->close();
}
}
event->accept();
}
else
{
event->ignore();
}
// Restart video playback. XXX Hack
play(false);
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
#ifdef Q_OS_OSX // On Mac OS X
Q_UNUSED(event);
// Do nothing
#endif
#ifdef Q_OS_LINUX // On Linux
if (event->modifiers() & Qt::AltModifier) {
QString currentDesktop = QString(getenv("XDG_CURRENT_DESKTOP")).toLower();
if (currentDesktop != "unity" && !_showMenuBar) {
menuBar()->setHidden(!menuBar()->isHidden());
menuBar()->setFocus(Qt::MenuBarFocusReason);
}
}
#endif
#ifdef Q_OS_WIN32
if (event->modifiers() & Qt::AltModifier) {
if (!_showMenuBar) {
menuBar()->setHidden(!menuBar()->isHidden());
menuBar()->setFocus(Qt::MenuBarFocusReason);
}
}
#endif
}
bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
QMenu *menu = static_cast<QMenu*>(object);
if (menu && (event->type() == QEvent::MouseButtonPress
|| event->type() == QEvent::MouseButtonDblClick))
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
// Disable right click on context menu actions
if (mouseEvent->buttons() & Qt::RightButton) {
mouseEvent->ignore();
return true;
}
return false;
}
return QMainWindow::eventFilter(object, event);
}
void MainWindow::setOutputWindowFullScreen(bool enable)
{
outputWindow->setFullScreen(enable);
// setCheckState
displayControlsAction->setChecked(enable);
displayPaintControlsAction->setChecked(enable);
}
void MainWindow::newFile()
{
// Stop video playback to avoid lags. XXX Hack
pause(false);
// Popup dialog allowing the user to save before creating a new file.
if (okToContinue())
{
clearWindow();
setCurrentFile("");
undoStack->clear();
}
// Restart video playback. XXX Hack
play(false);
}
void MainWindow::open()
{
// Stop video playback to avoid lags. XXX Hack
pause(false);
// 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
play(false);
}
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
pause(false);
// 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
play(false);
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::importMedia()
{
// Stop video playback to avoid lags. XXX Hack
pause(false);
// 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("Media files (%1 %2);;All files (*)")
.arg(MM::VIDEO_FILES_FILTER)
.arg(MM::IMAGE_FILES_FILTER));
// Restart video playback. XXX Hack
play(false);
// Check if file is image or not
// according to file extension
if (!fileName.isEmpty()) {
if (!QFileInfo(fileName).suffix().isEmpty() && MM::IMAGE_FILES_FILTER.contains(QFileInfo(fileName).suffix(), Qt::CaseInsensitive))
importMediaFile(fileName, true);
else
importMediaFile(fileName, false);
}
}
void MainWindow::openCameraDevice()
{
#if QT_VERSION >= 0x050500
QString device;
QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
if (cameras.count() > 1)
{
QStringList devicesList;
QMap<QString, QString> devices;
for (const QCameraInfo &cameraInfo: cameras)
{
devicesList << cameraInfo.description();
devices.insert(cameraInfo.description(), cameraInfo.deviceName());
}
bool ok;
QString deviceName = QInputDialog::getItem(this, tr("Camera device"),
tr("Select camera"), devicesList, 0, false, &ok);
if (ok && !deviceName.isEmpty())
{
if (devices.contains(deviceName))
device = devices.value(deviceName);
}
}
else if (QCameraInfo::defaultCamera().isNull())
{
QMessageBox::warning(this, tr("No camera available"), tr("You can not use this feature!\nNo camera available in your system"));
}
else
{
device = QCameraInfo::defaultCamera().deviceName();
}
if (!device.isEmpty())
importMediaFile(device, false);
#else
QMessageBox::warning(this, tr("No camera available"), tr("You can not use this feature!\nNo camera available in your system"));
#endif
}
void MainWindow::addColor()
{
// Stop video playback to avoid lags. XXX Hack
pause(false);
// Pop-up color-choosing dialog to choose color paint.
// FIXME: we use a static variable to store the last chosen color
// it should rather be a member of this class, or so.
static QColor color = QColor(0, 255, 0, 255);
color = QColorDialog::getColor(color, this, tr("Select Color"),
// QColorDialog::DontUseNativeDialog |
QColorDialog::ShowAlphaChannel);
if (color.isValid())
{
addColorPaint(color);
}
// Restart video playback. XXX Hack
play(false);
}
void MainWindow::addMesh()
{
// A paint must be selected to add a mapping.
if (getCurrentPaintId() == NULL_UID)
return;
// 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")
{
MShape::ptr outputQuad = MShape::ptr(Util::createMeshForColor(sourceCanvas->width(), sourceCanvas->height()));
mappingPtr = new ColorMapping(paint, outputQuad);
}
else
{
QSharedPointer<Texture> texture = qSharedPointerCast<Texture>(paint);
Q_CHECK_PTR(texture);
MShape::ptr outputQuad = MShape::ptr(Util::createMeshForTexture(texture.data(), sourceCanvas->width(), sourceCanvas->height()));
MShape::ptr inputQuad = MShape::ptr(Util::createMeshForTexture(texture.data(), sourceCanvas->width(), sourceCanvas->height()));
mappingPtr = new TextureMapping(paint, outputQuad, inputQuad);
}
// Create texture mapping.
Mapping::ptr mapping(mappingPtr);
uint mappingId = mappingManager->addMapping(mapping);
// Lets the undo-stack handle Undo/Redo the adding of mapping item.
undoStack->push(new AddShapesCommand(this, mappingId));
}
void MainWindow::addTriangle()
{
// A paint must be selected to add a mapping.
if (getCurrentPaintId() == NULL_UID)
return;
// 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")
{
MShape::ptr outputTriangle = MShape::ptr(Util::createTriangleForColor(sourceCanvas->width(), sourceCanvas->height()));
mappingPtr = new ColorMapping(paint, outputTriangle);
}
else
{
QSharedPointer<Texture> texture = qSharedPointerCast<Texture>(paint);
Q_CHECK_PTR(texture);
MShape::ptr outputTriangle = MShape::ptr(Util::createTriangleForTexture(texture.data(), sourceCanvas->width(), sourceCanvas->height()));
MShape::ptr inputTriangle = MShape::ptr(Util::createTriangleForTexture(texture.data(), sourceCanvas->width(), sourceCanvas->height()));
mappingPtr = new TextureMapping(paint, inputTriangle, outputTriangle);
}
// Create mapping.
Mapping::ptr mapping(mappingPtr);
uint mappingId = mappingManager->addMapping(mapping);
// Lets undo-stack handle Undo/Redo the adding of mapping item.
undoStack->push(new AddShapesCommand(this, mappingId));
}
void MainWindow::addEllipse()
{
// A paint must be selected to add a mapping.
if (getCurrentPaintId() == NULL_UID)
return;
// 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")
{
MShape::ptr outputEllipse = MShape::ptr(Util::createEllipseForColor(sourceCanvas->width(), sourceCanvas->height()));
mappingPtr = new ColorMapping(paint, outputEllipse);
}
else
{
QSharedPointer<Texture> texture = qSharedPointerCast<Texture>(paint);
Q_CHECK_PTR(texture);
MShape::ptr outputEllipse = MShape::ptr(Util::createEllipseForTexture(texture.data(), sourceCanvas->width(), sourceCanvas->height()));
MShape::ptr inputEllipse = MShape::ptr(Util::createEllipseForTexture(texture.data(), sourceCanvas->width(), sourceCanvas->height()));
mappingPtr = new TextureMapping(paint, inputEllipse, outputEllipse);
}
// Create mapping.
Mapping::ptr mapping(mappingPtr);
uint mappingId = mappingManager->addMapping(mapping);
// Lets undo-stack handle Undo/Redo the adding of mapping item.
undoStack->push(new AddShapesCommand(this, mappingId));
}
void MainWindow::about()
{
// Stop video playback to avoid lags. XXX Hack
pause(false);
// // 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));
_aboutDialog = new AboutDialog(this);
_aboutDialog->setAttribute(Qt::WA_DeleteOnClose); // Important for ressource management
_aboutDialog->show();
// Restart video playback. XXX Hack
play(false);
}
void MainWindow::updateStatusBar()
{
QPointF mousePos = destinationCanvas->mapToScene(destinationCanvas->mapFromGlobal(destinationCanvas->cursor().pos()));
if (currentSelectedItem) // Show mouse coordinate only if mappingList is not empty
mousePosLabel->setText("Mouse coordinate: X " + QString::number(mousePos.x()) + " Y " + QString::number(mousePos.y()));
else
mousePosLabel->setText(""); // Otherwise set empty text.
currentMessageLabel->setText(statusBar()->currentMessage());
sourceZoomLabel->setText("Source: " + QString::number(int(sourceCanvas->getZoomFactor() * 100)).append(QChar('%')));
destinationZoomLabel->setText("Destination: " + QString::number(int(destinationCanvas->getZoomFactor() * 100)).append(QChar('%')));
lastActionLabel->setText(undoStack->text(undoStack->count() - 1));
}
void MainWindow::showMenuBar(bool shown)
{
_showMenuBar = shown;
#ifdef Q_OS_OSX // On Mac OS X
// Do nothing
#endif
#ifdef Q_OS_LINUX // On Linux
QString currentDesktop = QString(getenv("XDG_CURRENT_DESKTOP")).toLower();
if (currentDesktop != "unity")
menuBar()->setVisible(shown);
#endif
#ifdef Q_OS_WIN32 // On Windows
menuBar()->setVisible(shown);
#endif
}
/**
* 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.
undoStack->push(new DeleteMappingCommand(this, getCurrentMappingId()));
//currentSelectedItem = NULL;
}
else if (isPaintTabSelected) //currentSelectedItem->listWidget() == paintList)
{
// Delete paint.
undoStack->push(new RemovePaintCommand(this, getItemId(*paintList->currentItem())));
//currentSelectedItem = NULL;
}
else
{
qCritical() << "Selected item neither a mapping nor a paint." << endl;
}
}
}
void MainWindow::duplicateMappingItem()
{
if (currentSelectedIndex.isValid())
{
duplicateMapping(currentMappingItemId());
}
else
{
qCritical() << "No selected mapping" << endl;
}
}
void MainWindow::deleteMappingItem()
{
if (currentSelectedIndex.isValid())
{
undoStack->push(new DeleteMappingCommand(this, currentMappingItemId()));
}
else
{
qCritical() << "No selected mapping" << endl;
}
}
void MainWindow::renameMappingItem()
{
// Set current item editable and rename it
QModelIndex index = mappingList->currentIndex();
// Used by context menu
mappingList->edit(index);
// Switch to mapping tab.
contentTab->setCurrentWidget(mappingSplitter);
}
void MainWindow::setMappingItemLocked(bool locked)
{
setMappingLocked(currentMappingItemId(), locked);
}
void MainWindow::setMappingItemHide(bool hide)
{
setMappingVisible(currentMappingItemId(), !hide);
}
void MainWindow::setMappingItemSolo(bool solo)
{
setMappingSolo(currentMappingItemId(), solo);
}
void MainWindow::renameMapping(uid mappingId, const QString &name)
{
Mapping::ptr mapping = mappingManager->getMappingById(mappingId);
Q_CHECK_PTR(mapping);
if (!mapping.isNull()) {
QModelIndex index = mappingListModel->getIndexFromId(mappingId);
mappingListModel->setData(index, name, Qt::EditRole);
mapping->setName(name);
}
}
//void MainWindow::mappingListEditEnd(QWidget *editor)
//{
// QString name = reinterpret_cast<QLineEdit*>(editor)->text();
// renameMapping(getItemId(*mappingList->currentItem()), name);
//}
void MainWindow::deletePaintItem()
{
if(currentSelectedItem)
{
undoStack->push(new RemovePaintCommand(this, getItemId(*paintList->currentItem())));
}
else
{
qCritical() << "No selected paint" << endl;
}
}
void MainWindow::renamePaintItem()
{
// Set current item editable and rename it
QListWidgetItem* item = paintList->currentItem();
item->setFlags(item->flags() | Qt::ItemIsEditable);
// Used by context menu
paintList->editItem(item);
// Switch to paint tab
contentTab->setCurrentWidget(paintSplitter);
}
void MainWindow::renamePaint(uid paintId, const QString &name)
{
Paint::ptr paint = mappingManager->getPaintById(paintId);
Q_CHECK_PTR(paint);
if (!paint.isNull()) {
paint->setName(name);
}
}
void MainWindow::paintListEditEnd(QWidget *editor)
{
QString name = reinterpret_cast<QLineEdit*>(editor)->text();
renamePaint(getItemId(*paintList->currentItem()), name);
}
void MainWindow::setupOutputScreen()
{
QAction *actionSender = qobject_cast<QAction *>(sender());
if (actionSender)
outputWindow->setPreferredScreen(actionSender->data().toInt());
// If want that the changes take effect immediatelly
// when the output is in fullscreen mode
if (outputFullScreenAction->isChecked()) {
// XXX: Close and reopen // It's not the best way to do
outputFullScreenAction->toggle();
outputFullScreenAction->trigger();
}
}
void MainWindow::updateScreenCount()
{
// Clear action list before
screenActions.clear();
// Refresh screen action
updateScreenActions();
// Update Output menu
outputScreenMenu->clear();
outputScreenMenu->addActions(screenActions);
}
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.
mappingListModel->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, VideoType type, double rate)
{
// Cannot create image with already existing id.
if (Paint::getUidAllocator().exists(paintId))
return NULL_UID;
else
{
// Check if file exists before
//if (! fileExists(uri))
//uri = locateMediaFile(uri, isImage);
Texture* tex = 0;
if (isImage)
tex = new Image(uri, paintId);
else {
tex = new Video(uri, type, rate, paintId);
}
// Create new image with corresponding ID.
tex->setPosition(x, y);
// Add it to the manager.
Paint::ptr paint(tex);
paint->setName(strippedName(uri));
// Add paint to model and return its uid.
uid id = mappingManager->addPaint(paint);
// Add paint widget item.
undoStack->push(new AddPaintCommand(this, id, paint->getIcon(), paint->getName()));
return id;
}
}