-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.cpp
738 lines (607 loc) · 22 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
/*
* MainWindow.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)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"
MainWindow::MainWindow()
{
if (!instance)
setInstance(this);
mappingManager = new MappingManager;
currentPaintId = 0;
currentMappingId = 0;
_hasCurrentPaint = false;
_hasCurrentMapping = false;
createLayout();
createActions();
createMenus();
createContextMenu();
createToolBars();
createStatusBar();
readSettings();
//setWindowIcon(QIcon(":/images/icon.png"));
setCurrentFile("");
}
MainWindow& MainWindow::getInstance()
{
Q_ASSERT(instance);
return *instance;
}
void MainWindow::setInstance(MainWindow* inst)
{
instance = inst;
}
MainWindow::~MainWindow()
{
delete mappingManager;
}
void MainWindow::handleSourceItemSelectionChanged()
{
std::cout << "selection changed" << std::endl;
QListWidgetItem* item = sourceList->currentItem();
uint idx = item->data(Qt::UserRole).toUInt();
std::cout << "idx=" << idx << std::endl;
setCurrentPaint(idx);
removeCurrentMapping();
// Update canvases.
updateAll();
//sourceCanvas->switchImage(idx);
//sourceCanvas->repaint();
//destinationCanvas->repaint();
}
void MainWindow::handleLayerItemSelectionChanged()
{
std::cout << "shape selection changed" << std::endl;
QListWidgetItem* item = layerList->currentItem();
uint idx = item->data(Qt::UserRole).toUInt();
Mapping::ptr mapping = mappingManager->getLayerById(idx)->getMapping();
setCurrentPaint(mapping->getPaint()->getId());
setCurrentMapping(mapping->getId());
updateAll();
//sourceCanvas->switchImage(idx);
//sourceCanvas->repaint();
//destinationCanvas->repaint();
}
void MainWindow::handleLayerItemChanged(QListWidgetItem* item)
{
uint layerId = item->data(Qt::UserRole).toUInt();
Layer::ptr layer = mappingManager->getLayerById(layerId);
layer->setVisible(item->checkState() == Qt::Checked);
updateAll();
}
void MainWindow::handleLayerIndexesMoved()
{
qDebug() << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1Moved!!!" << endl;
std::vector<uint> newOrder;
for (int row=layerList->count()-1; row>=0; row--)
{
uint layerId = layerList->item(row)->data(Qt::UserRole).toUInt();
newOrder.push_back(layerId);
}
mappingManager->reorderLayers(newOrder);
updateAll();
}
void MainWindow::handleLayerOpacityChanged(int value)
{
float opacity = (float)value/100.0f;
}
//void MainWindow::handleSourceSelectionChanged(const QItemSelection& selection)
//{
// std::cout << "selection changed" << std::endl;
// QModelIndex& index = selection.indexes().first();
// int idx = index.row();
// std::cout << "idx=" << idx << std::endl;
// sourceCanvas->switchImage(idx);
//}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (okToContinue())
{
writeSettings();
event->accept();
}
else
{
event->ignore();
}
}
void MainWindow::newFile()
{
if (okToContinue())
{
clearWindow();
setCurrentFile("");
}
}
void MainWindow::open()
{
if (okToContinue())
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open mapping"), ".", tr("Libremapping files (*.lmp)"));
if (!fileName.isEmpty())
loadFile(fileName);
}
}
bool MainWindow::save()
{
if (curFile.isEmpty())
{
return saveAs();
}
else
{
return saveFile(curFile);
}
}
bool MainWindow::saveAs()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save mapping"),
".", tr("Libremapping files (*.lmp)"));
if (fileName.isEmpty())
return false;
return saveFile(fileName);
}
void MainWindow::import()
{
if (okToContinue())
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Import resource"), ".");
if (!fileName.isEmpty())
importFile(fileName);
}
}
void MainWindow::addQuad()
{
// Create default quad.
// Retrieve current paint (as texture).
Paint::ptr paint = MainWindow::getInstance().getMappingManager().getPaint(getCurrentPaintId());
Q_CHECK_PTR(paint);
std::tr1::shared_ptr<Texture> texture = std::tr1::static_pointer_cast<Texture>(paint);
Q_CHECK_PTR(texture);
// Create input and output quads.
Shape::ptr outputQuad = Shape::ptr(Util::createQuadForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height()));
Shape::ptr inputQuad = Shape::ptr(Util::createQuadForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height()));
// Create texture mapping.
Mapping::ptr mapping(new TextureMapping(paint, outputQuad, inputQuad));
uint layerId = mappingManager->addLayer(mapping);
addLayerItem(layerId);
}
void MainWindow::addTriangle()
{
// Create default quad.
// Retrieve current paint (as texture).
Paint::ptr paint = MainWindow::getInstance().getMappingManager().getPaint(getCurrentPaintId());
Q_CHECK_PTR(paint);
std::tr1::shared_ptr<Texture> texture = std::tr1::static_pointer_cast<Texture>(paint);
Q_CHECK_PTR(texture);
// Create input and output quads.
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()));
// Create texture mapping.
Mapping::ptr mapping(new TextureMapping(paint, inputTriangle, outputTriangle));
uint layerId = mappingManager->addLayer(mapping);
addLayerItem(layerId);
}
void MainWindow::about()
{
QMessageBox::about(this, tr("About Libremapping"),
tr("<h2>Libremapping "
LIBREMAPPING_VERSION
"</h2>"
"<p>Copyright © 2013 Office International de la Francophonie"
"<p>Libremapping is a free software for video mapping."));
}
void MainWindow::updateStatusBar()
{
// TODO
// locationLabel->setText(spreadsheet->currentLocation());
// formulaLabel->setText(spreadsheet->currentFormula());
}
void MainWindow::windowModified()
{
setWindowModified(true);
updateStatusBar();
}
void MainWindow::createLayout()
{
sourceList = new QListWidget;
sourceList->setSelectionMode(QAbstractItemView::SingleSelection);
sourceList->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
layerList = new QListWidget;
layerList->setSelectionMode(QAbstractItemView::SingleSelection);
layerList->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
//layerList->setDragDropMode(QAbstractItemView::DragDrop);
layerList->setDefaultDropAction(Qt::MoveAction);
layerList->setDragDropMode(QAbstractItemView::InternalMove);
propertyPanel = new QStackedWidget;
propertyPanel->setDisabled(true);
sourceCanvas = new SourceGLCanvas;
destinationCanvas = new DestinationGLCanvas(0, sourceCanvas);
connect(sourceCanvas, SIGNAL(shapeChanged(Shape*)),
destinationCanvas, SLOT(updateCanvas()));
// connect(destinationCanvas, SIGNAL(imageChanged()),
// sourceCanvas, SLOT(updateCanvas()));
sourceCanvas->setFocusPolicy(Qt::ClickFocus);
destinationCanvas->setFocusPolicy(Qt::ClickFocus);
sourceCanvas->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
destinationCanvas->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sourceCanvas->setMinimumSize(CANVAS_MINIMUM_WIDTH, CANVAS_MINIMUM_HEIGHT);
destinationCanvas->setMinimumSize(CANVAS_MINIMUM_WIDTH, CANVAS_MINIMUM_HEIGHT);
mainSplitter = new QSplitter(Qt::Vertical);
resourceSplitter = new QSplitter(Qt::Horizontal);
resourceSplitter->addWidget(sourceList);
resourceSplitter->addWidget(layerList);
resourceSplitter->addWidget(propertyPanel);
canvasSplitter = new QSplitter(Qt::Horizontal);
canvasSplitter->addWidget(sourceCanvas);
canvasSplitter->addWidget(destinationCanvas);
mainSplitter->addWidget(canvasSplitter);
mainSplitter->addWidget(resourceSplitter);
// Initialize size to 2:1 proportions.
QSize sz = mainSplitter->size();
QList<int> sizes;
sizes.append(sz.height() * 2 / 3);
sizes.append(sz.height() - sizes.at(0));
mainSplitter->setSizes(sizes);
// Upon resizing window, give some extra stretch expansion to canvasSplitter.
//mainSplitter->setStretchFactor(0, 1);
setWindowTitle(tr("Libremapping"));
resize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setCentralWidget(mainSplitter);
// Common::initializeLibremapper(sourceCanvas->width(), sourceCanvas->height());
//
// for (int i = 0; i < Common::nImages(); i++)
// {
// std::tr1::shared_ptr<Image> img = std::tr1::static_pointer_cast<Image>(
// Common::mappings[i]->getPaint());
// Q_CHECK_PTR(img);
//
// QListWidgetItem* item = new QListWidgetItem(strippedName(img->getImagePath()));
// item->setData(Qt::UserRole, i);
//
// sourceList->addItem(item);
// }
connect(sourceList, SIGNAL(itemSelectionChanged()),
this, SLOT(handleSourceItemSelectionChanged()));
connect(layerList, SIGNAL(itemSelectionChanged()),
this, SLOT(handleLayerItemSelectionChanged()));
connect(layerList, SIGNAL(itemChanged(QListWidgetItem*)),
this, SLOT(handleLayerItemChanged(QListWidgetItem*)));
connect(layerList->model(), SIGNAL(layoutChanged()),
this, SLOT(handleLayerIndexesMoved()));
// sourceList->setModel(sourcesModel);
//
// connect(sourceList->selectionModel(),
// SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
// this, SLOT(handleSourceSelectionChanged(QItemSelection)));
}
void MainWindow::createActions()
{
newAction = new QAction(tr("&New"), this);
newAction->setIcon(QIcon(":/images/document-new-4.png"));
newAction->setShortcut(QKeySequence::New);
newAction->setStatusTip(tr("Create a new mapping file"));
connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));
openAction = new QAction(tr("&Open..."), this);
openAction->setIcon(QIcon(":/images/document-open-3.png"));
openAction->setShortcut(QKeySequence::Open);
openAction->setStatusTip(tr("Open an existing mapping file"));
connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
saveAction = new QAction(tr("&Save"), this);
saveAction->setIcon(QIcon(":/images/document-save-2.png"));
saveAction->setShortcut(QKeySequence::Save);
saveAction->setStatusTip(tr("Save the mapping to disk"));
connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
saveAsAction = new QAction(tr("Save &As..."), this);
saveAsAction->setIcon(QIcon(":/images/document-save-as-2.png"));
saveAsAction->setStatusTip(tr("Save the mapping under a new name"));
connect(saveAsAction, SIGNAL(triggered()), this, SLOT(saveAs()));
importAction = new QAction(tr("&Import media source..."), this);
importAction->setIcon(QIcon(":/images/document-import-2.png"));
importAction->setShortcut(QKeySequence::Open);
importAction->setStatusTip(tr("Import a media source file"));
connect(importAction, SIGNAL(triggered()), this, SLOT(import()));
exitAction = new QAction(tr("E&xit"), this);
exitAction->setShortcut(tr("Ctrl+Q"));
exitAction->setStatusTip(tr("Exit the application"));
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
// cutAction = new QAction(tr("Cu&t"), this);
// cutAction->setIcon(QIcon(":/images/cut.png"));
// cutAction->setShortcut(QKeySequence::Cut);
// cutAction->setStatusTip(tr("Cut the current selection's contents to the clipboard"));
// connect(cutAction, SIGNAL(triggered()), spreadsheet, SLOT(cut()));
//
// copyAction = new QAction(tr("&Copy"), this);
// copyAction->setIcon(QIcon(":/images/copy.png"));
// copyAction->setShortcut(QKeySequence::Copy);
// copyAction->setStatusTip(tr("Copy the current selection's contents to the clipboard"));
// connect(copyAction, SIGNAL(triggered()), spreadsheet, SLOT(copy()));
//
// pasteAction = new QAction(tr("&Paste"), this);
// pasteAction->setIcon(QIcon(":/images/paste.png"));
// pasteAction->setShortcut(QKeySequence::Paste);
// pasteAction->setStatusTip(tr("Paste the clipboard's contents into the current selection"));
// connect(pasteAction, SIGNAL(triggered()), spreadsheet, SLOT(paste()));
//
// deleteAction = new QAction(tr("&Delete"), this);
// deleteAction->setShortcut(QKeySequence::Delete);
// deleteAction->setStatusTip(tr("Delete the current selection's contents"));
// connect(deleteAction, SIGNAL(triggered()), spreadsheet, SLOT(del()));
aboutAction = new QAction(tr("&About"), this);
aboutAction->setStatusTip(tr("Show the application's About box"));
connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
addQuadAction = new QAction(tr("&Add quad"), this);
addQuadAction->setIcon(QIcon(":/images/draw-rectangle-2.png"));
addQuadAction->setStatusTip(tr("Add quad"));
connect(addQuadAction, SIGNAL(triggered()), this, SLOT(addQuad()));
addTriangleAction = new QAction(tr("&Add triangle"), this);
addTriangleAction->setIcon(QIcon(":/images/draw-triangle.png"));
addTriangleAction->setStatusTip(tr("Add triangle"));
connect(addTriangleAction, SIGNAL(triggered()), this, SLOT(addTriangle()));
}
void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAction);
fileMenu->addAction(openAction);
fileMenu->addAction(importAction);
fileMenu->addAction(saveAction);
fileMenu->addAction(saveAsAction);
separatorAction = fileMenu->addSeparator();
fileMenu->addAction(exitAction);
// editMenu = menuBar()->addMenu(tr("&Edit"));
// editMenu->addAction(cutAction);
// editMenu->addAction(copyAction);
// editMenu->addAction(pasteAction);
// editMenu->addAction(deleteAction);
// selectSubMenu = editMenu->addMenu(tr("&Select"));
// selectSubMenu->addAction(selectRowAction);
// selectSubMenu->addAction(selectColumnAction);
// selectSubMenu->addAction(selectAllAction);
// editMenu->addSeparator();
// editMenu->addAction(findAction);
// editMenu->addAction(goToCellAction);
// toolsMenu = menuBar()->addMenu(tr("&Tools"));
// toolsMenu->addAction(recalculateAction);
// toolsMenu->addAction(sortAction);
//
// optionsMenu = menuBar()->addMenu(tr("&Options"));
// optionsMenu->addAction(showGridAction);
// optionsMenu->addAction(autoRecalcAction);
menuBar()->addSeparator();
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAction);
// helpMenu->addAction(aboutQtAction);
}
void MainWindow::createContextMenu()
{
// spreadsheet->addAction(cutAction);
// spreadsheet->addAction(copyAction);
// spreadsheet->addAction(pasteAction);
// spreadsheet->setContextMenuPolicy(Qt::ActionsContextMenu);
}
void MainWindow::createToolBars()
{
fileToolBar = addToolBar(tr("&File"));
fileToolBar->addAction(importAction);
fileToolBar->addAction(newAction);
fileToolBar->addAction(openAction);
fileToolBar->addAction(saveAction);
fileToolBar->addSeparator();
fileToolBar->addAction(addQuadAction);
fileToolBar->addAction(addTriangleAction);
// editToolBar = addToolBar(tr("&Edit"));
// editToolBar->addAction(cutAction);
// editToolBar->addAction(copyAction);
// editToolBar->addAction(pasteAction);
// editToolBar->addSeparator();
// editToolBar->addAction(findAction);
// editToolBar->addAction(goToCellAction);
}
void MainWindow::createStatusBar()
{
// locationLabel = new QLabel(" W999 ");
// locationLabel->setAlignment(Qt::AlignHCenter);
// locationLabel->setMinimumSize(locationLabel->sizeHint());
//
// formulaLabel = new QLabel;
// formulaLabel->setIndent(3);
//
// statusBar()->addWidget(locationLabel);
// statusBar()->addWidget(formulaLabel, 1);
//
// connect(spreadsheet, SIGNAL(currentCellChanged(int, int, int, int)), this,
// SLOT(updateStatusBar()));
// connect(spreadsheet, SIGNAL(modified()), this, SLOT(spreadsheetModified()));
updateStatusBar();
}
void MainWindow::readSettings()
{
QSettings settings("OIF", "Libremapping");
restoreGeometry(settings.value("geometry").toByteArray());
// mainSplitter->restoreState(settings.value("mainSplitter").toByteArray());
// sourceSplitter->restoreState(settings.value("sourceSplitter").toByteArray());
// canvasSplitter->restoreState(settings.value("canvasSplitter").toByteArray());
}
void MainWindow::writeSettings()
{
QSettings settings("OIF", "Libremapping");
settings.setValue("geometry", saveGeometry());
settings.setValue("mainSplitter", mainSplitter->saveState());
settings.setValue("sourceSplitter", resourceSplitter->saveState());
settings.setValue("canvasSplitter", canvasSplitter->saveState());
}
bool MainWindow::okToContinue()
{
if (isWindowModified())
{
int r = QMessageBox::warning(this, tr("Libremapping"),
tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
if (r == QMessageBox::Yes)
{
return save();
}
else if (r == QMessageBox::Cancel)
{
return false;
}
}
return true;
}
bool MainWindow::loadFile(const QString &fileName)
{
// TODO: Try to read file.
// if (!spreadsheet->readFile(fileName))
// {
// statusBar()->showMessage(tr("Loading canceled"), 2000);
// return false;
// }
setCurrentFile(fileName);
statusBar()->showMessage(tr("File loaded"), 2000);
return true;
}
bool MainWindow::saveFile(const QString &fileName)
{
// TODO: Try to write file.
// if (!spreadsheet->writeFile(fileName))
// {
// statusBar()->showMessage(tr("Saving canceled"), 2000);
// return false;
// }
setCurrentFile(fileName);
statusBar()->showMessage(tr("File saved"), 2000);
return true;
}
void MainWindow::setCurrentFile(const QString &fileName)
{
curFile = fileName;
setWindowModified(false);
QString shownName = tr("Untitled");
if (!curFile.isEmpty())
{
shownName = strippedName(curFile);
}
setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Spreadsheet")));
}
bool MainWindow::importFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::warning(this, tr("Spreadsheet"),
tr("Cannot read file %1:\n%2.")
.arg(file.fileName())
.arg(file.errorString()));
return false;
}
QApplication::setOverrideCursor(Qt::WaitCursor);
// Add image to model.
uint imageId = mappingManager->addImage(fileName, sourceCanvas->width(), sourceCanvas->height());
// Add image to sourceList widget.
QListWidgetItem* item = new QListWidgetItem(strippedName(fileName));
item->setData(Qt::UserRole, imageId); // TODO: could possibly be replaced by a Paint pointer
item->setIcon(QIcon(fileName));
item->setSizeHint(QSize(item->sizeHint().width(), MainWindow::SOURCE_LIST_ITEM_HEIGHT));
sourceList->addItem(item);
sourceList->setCurrentItem(item);
// update();
QApplication::restoreOverrideCursor();
statusBar()->showMessage(tr("File imported"), 2000);
return true;
}
void MainWindow::addLayerItem(uint layerId)
{
Layer::ptr layer = mappingManager->getLayerById(layerId);
Q_CHECK_PTR(layer);
Mapping::ptr mapping = layer->getMapping();
uint mappingId = mapping->getId();
QString label;
QIcon icon;
// Triangle
// TODO: change this: we can't determine which one it is based on number of vertices
if (mapping->getShape()->nVertices() == 3)
{
label = QString("Triangle %1").arg(mappingId);
icon = QIcon(":/images/draw-triangle.png");
}
else if (mapping->getShape()->nVertices() == 4)
{
label = QString("Quad %1").arg(mappingId);
icon = QIcon(":/images/draw-rectangle-2.png");
}
else
{
label = QString("Polygon %1").arg(mappingId);
icon = QIcon(":/images/draw-polygon-2.png");
}
// Add mapper.
// XXX hardcoded for textures
std::tr1::shared_ptr<TextureMapping> textureMapping = std::tr1::static_pointer_cast<TextureMapping>(mapping);
Q_CHECK_PTR(textureMapping);
Mapper::ptr mapper( new TextureMapper(textureMapping) );
mappers[mappingId] = mapper;
QGroupBox* layerEditor = new QGroupBox;
QVBoxLayout* layerEditorLayout = new QVBoxLayout;
QLabel* layerOpacityLabel = new QLabel(tr("Opacity"));
QSlider* layerOpacitySlider = new QSlider(Qt::Horizontal);
layerOpacitySlider->setRange(0, 100);
layerEditorLayout->addWidget(layerOpacityLabel);
layerEditorLayout->addWidget(layerOpacitySlider);
QWidget* mapperEditor = mapper->getPropertiesEditor();
layerEditorLayout->addWidget(mapperEditor);
layerEditor->setLayout(layerEditorLayout);
propertyPanel->addWidget(layerEditor);
propertyPanel->setCurrentWidget(mapperEditor);
propertyPanel->setEnabled(true);
connect(layerOpacitySlider, SIGNAL(valueChanged(int)),
layer.get(), SLOT(setOpacityPercentage(int)));
connect(layerOpacitySlider, SIGNAL(valueChanged(int)),
this, SLOT(updateAll()));
// When mapper value is changed, update canvases.
connect(mapper.get(), SIGNAL(valueChanged()),
this, SLOT(updateAll()));
connect(sourceCanvas, SIGNAL(shapeChanged(Shape*)),
mapper.get(), SLOT(updateShape(Shape*)));
connect(destinationCanvas, SIGNAL(shapeChanged(Shape*)),
mapper.get(), SLOT(updateShape(Shape*)));
// Add item to layerList widget.
QListWidgetItem* item = new QListWidgetItem(label);
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(Qt::Checked);
item->setData(Qt::UserRole, layerId); // TODO: could possibly be replaced by a Paint pointer
item->setIcon(icon);
item->setSizeHint(QSize(item->sizeHint().width(), MainWindow::SHAPE_LIST_ITEM_HEIGHT));
layerList->insertItem(0, item);
layerList->setCurrentItem(item);
}
void MainWindow::clearWindow()
{
// TODO: implement clearWindow()
}
void MainWindow::updateAll()
{
sourceCanvas->update();
destinationCanvas->update();
}
QString MainWindow::strippedName(const QString &fullFileName)
{
return QFileInfo(fullFileName).fileName();
}
MainWindow* MainWindow::instance = 0;