From 8300c1059925d8727c0e4e5d283eaef66e7d31eb Mon Sep 17 00:00:00 2001 From: baydam Date: Mon, 30 Mar 2015 20:54:15 +0000 Subject: [PATCH] Add context menu for duplicate, delete and rename mapping --- MainWindow.cpp | 208 +++++++++++++++++++++++++++------------------ MainWindow.h | 20 +++-- MapperGLCanvas.cpp | 11 ++- MappingManager.cpp | 2 - 4 files changed, 141 insertions(+), 100 deletions(-) diff --git a/MainWindow.cpp b/MainWindow.cpp index 2c2edc1..941d09d 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -52,7 +52,6 @@ MainWindow::MainWindow() createMenus(); createContextMenu(); createToolBars(); - createStatusBar(); // Load settings. readSettings(); @@ -669,13 +668,6 @@ void MainWindow::about() 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. * @@ -704,7 +696,27 @@ void MainWindow::deleteItem() { qCritical() << "Selected item neither a mapping nor a paint." << endl; } + } +} + +void MainWindow::cloneItem() +{ + if (currentSelectedItem) + { + cloneMappingItem(getItemId(*mappingList->currentItem())); } + else + { + qCritical() << "No selected mapping" << endl; + } +} + +void MainWindow::renameItem() +{ + // Set current item editable and rename it + QListWidgetItem* item = mappingList->currentItem(); + item->setFlags(item->flags() | Qt::ItemIsEditable); + mappingList->editItem(item); } void MainWindow::openRecentFile() @@ -1025,7 +1037,68 @@ void MainWindow::deleteMapping(uid mappingId) if (Mapping::getUidAllocator().exists(mappingId)) { removeMappingItem(mappingId); + } +} + +void MainWindow::cloneMappingItem(uid mappingId) +{ + // Current Mapping + Mapping::ptr mappingPtr = mappingManager->getMappingById(mappingId); + + // Get Mapping Paint and Shape + Paint::ptr paint = mappingPtr->getPaint(); + Shape::ptr shape = mappingPtr->getShape(); + // Temporary shared pointers + Shape::ptr shapePtr; + // Create new mapping + Mapping *mapping; + + QString shapeType = shape->getType(); + + // Code below need to be improved it's feel like duplicated + if (paint->getType() == "color") // Color paint + { + if (shapeType == "quad") + shapePtr = Shape::ptr(new Quad(shape->getVertex(0), shape->getVertex(1), + shape->getVertex(2), shape->getVertex(3))); + + if (shapeType == "triangle") + shapePtr = Shape::ptr(new Triangle(shape->getVertex(0), shape->getVertex(1), shape->getVertex(2))); + + if (shapeType == "ellipse") + shapePtr = Shape::ptr(new Ellipse(shape->getVertex(0), shape->getVertex(1), shape->getVertex(2), + shape->getVertex(3))); + + mapping = new ColorMapping(paint, shapePtr); } + else // Or Texture Paint + { + Shape::ptr inputShape = mappingPtr->getInputShape(); + + if (shapeType == "mesh") + shapePtr = Shape::ptr(new Mesh(shape->getVertex(0), shape->getVertex(1), + shape->getVertex(3), shape->getVertex(2))); + + if (shapeType == "triangle") + shapePtr = Shape::ptr(new Triangle(shape->getVertex(0), shape->getVertex(1), shape->getVertex(2))); + + if (shapeType == "ellipse") + shapePtr = Shape::ptr(new Ellipse(shape->getVertex(0), shape->getVertex(1), shape->getVertex(2), + shape->getVertex(3), shape->getVertex(4))); + + mapping = new TextureMapping(paint, shapePtr, inputShape); + } + + // Scaling of duplicated mapping + if (shapeType == "quad" || shapeType == "mesh") + shapePtr->translate(20, 20); + else + shapePtr->translate(0, 20); + + // Create new duplicated mapping item + Mapping::ptr clonedMapping(mapping); + uint cloneId = mappingManager->addMapping(clonedMapping); + addMappingItem(cloneId); } /// Deletes/removes a paint and all associated mappigns. @@ -1051,7 +1124,6 @@ void MainWindow::deletePaint(uid paintId, bool replace) void MainWindow::windowModified() { setWindowModified(true); - updateStatusBar(); } void MainWindow::createLayout() @@ -1250,25 +1322,6 @@ void MainWindow::createActions() exitAction->setIconVisibleInMenu(false); 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())); -// - // Undo action undoAction = undoStack->createUndoAction(this, tr("&Undo")); undoAction->setShortcut(QKeySequence::Undo); @@ -1287,6 +1340,13 @@ void MainWindow::createActions() aboutAction->setIconVisibleInMenu(false); connect(aboutAction, SIGNAL(triggered()), this, SLOT(about())); + // Duplicate. + cloneAction = new QAction(tr("Duplicate"), this); + cloneAction->setShortcut(tr("CTRL+V")); + cloneAction->setStatusTip(tr("Duplicate item")); + cloneAction->setIconVisibleInMenu(false); + connect(cloneAction, SIGNAL(triggered()), this, SLOT(cloneItem())); + // Delete. deleteAction = new QAction(tr("Delete"), this); deleteAction->setShortcut(tr("CTRL+DEL")); @@ -1294,6 +1354,13 @@ void MainWindow::createActions() deleteAction->setIconVisibleInMenu(false); connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem())); + // Rename. + renameAction = new QAction(tr("Rename"), this); + renameAction->setShortcut(Qt::Key_F2); + renameAction->setStatusTip(tr("Rename item")); + renameAction->setIconVisibleInMenu(false); + connect(renameAction, SIGNAL(triggered()), this, SLOT(renameItem())); + // Preferences... preferencesAction = new QAction(tr("&Preferences..."), this); //preferencesAction->setIcon(QIcon(":/preferences")); @@ -1383,14 +1450,6 @@ void MainWindow::createActions() // Output window should be displayed for full screen option to be available. connect(displayOutputWindow, SIGNAL(toggled(bool)), outputWindowFullScreen, SLOT(setEnabled(bool))); - - // outputWindowHasCursor = new QAction(tr("O&utput window has cursor"), this); - // outputWindowHasCursor->setStatusTip(tr("Show cursor in output window")); - // outputWindowHasCursor->setIconVisibleInMenu(false); - // outputWindowHasCursor->setCheckable(true); - // outputWindowHasCursor->setChecked(true); - // connect(outputWindowHasCursor, SIGNAL(toggled(bool)), outputWindow, SLOT(setFullScreen(bool))); - // Toggle display of canvas controls. displayCanvasControls = new QAction(tr("&Display canvas controls"), this); // displayCanvasControls->setShortcut(tr("Ctrl+E")); @@ -1485,9 +1544,6 @@ void MainWindow::createMenus() editMenu = menuBar->addMenu(tr("&Edit")); editMenu->addAction(undoAction); editMenu->addAction(redoAction); - // editMenu->addAction(cutAction); - // editMenu->addAction(copyAction); - // editMenu->addAction(pasteAction); editMenu->addAction(deleteAction); editMenu->addAction(preferencesAction); @@ -1498,7 +1554,6 @@ void MainWindow::createMenus() viewMenu->addAction(displayCanvasControls); viewMenu->addAction(stickyVertices); viewMenu->addAction(displayTestSignal); - //viewMenu->addAction(outputWindowHasCursor); // Run. runMenu = menuBar->addMenu(tr("&Run")); @@ -1506,35 +1561,32 @@ void MainWindow::createMenus() runMenu->addAction(pauseAction); runMenu->addAction(rewindAction); -// 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); - // Help. 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); + // Context menu. + contextMenu = new QMenu(); + + // Add different Action + contextMenu->addAction(cloneAction); + contextMenu->addAction(deleteAction); + contextMenu->addAction(renameAction); + + // Set context menu policy + mappingList->setContextMenuPolicy(Qt::CustomContextMenu); + destinationCanvas->setContextMenuPolicy(Qt::CustomContextMenu); + outputWindow->setContextMenuPolicy(Qt::CustomContextMenu); + + // Context Menu Connexions + connect(mappingList, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showMappingContextMenu(const QPoint&))); + connect(destinationCanvas, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showMappingContextMenu(const QPoint&))); + connect(outputWindow, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showMappingContextMenu(const QPoint&))); } void MainWindow::createToolBars() @@ -1580,31 +1632,9 @@ void MainWindow::createToolBars() addToolBar(Qt::TopToolBarArea, runToolBar); // editToolBar = addToolBar(tr("&Edit")); -// editToolBar->addAction(cutAction); -// editToolBar->addAction(copyAction); -// editToolBar->addAction(pasteAction); +// editToolBar->addAction(cloneAction); +// editToolBar->addAction(deleteAction); // 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() @@ -2166,6 +2196,14 @@ void MainWindow::updateCanvases() outputWindow->getCanvas()->update(); } +void MainWindow::showMappingContextMenu(const QPoint &point) +{ + QWidget *objectSender = dynamic_cast(sender()); + + if (objectSender != NULL && mappingList->count() > 0) + contextMenu->exec(objectSender->mapToGlobal(point)); +} + QString MainWindow::strippedName(const QString &fullFileName) { return QFileInfo(fullFileName).fileName(); diff --git a/MainWindow.h b/MainWindow.h index 5b70ebf..89ab364 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -88,12 +88,13 @@ private slots: void importImage(); void addColor(); void about(); - void updateStatusBar(); void openRecentFile(); void clearRecentFileList(); void openRecentVideo(); // Edit menu. void deleteItem(); + void cloneItem(); + void renameItem(); // Widget callbacks. void handlePaintItemSelectionChanged(); @@ -172,12 +173,18 @@ public slots: /// Deletes/removes a mapping. void deleteMapping(uid mappingId); + /// Clone/duplicate a mapping + void cloneMappingItem(uid mappingId); + /// Deletes/removes a paint and all associated mappigns. void deletePaint(uid paintId, bool replace); /// Updates all canvases. void updateCanvases(); + // Show Context Menu + void showMappingContextMenu(const QPoint &point); + public: bool setTextureUri(int texture_id, const std::string &uri); bool setTextureRate(int texture_id, double rate); @@ -191,7 +198,6 @@ private: void createMenus(); void createContextMenu(); void createToolBars(); - void createStatusBar(); void updateRecentFileActions(); void updateRecentVideoActions(); @@ -240,16 +246,13 @@ private: // Menu actions. QMenu *fileMenu; -// QMenu *editMenu; -// QMenu *selectSubMenu; -// QMenu *toolsMenu; -// QMenu *optionsMenu; QMenu *editMenu; QMenu *viewMenu; QMenu *runMenu; QMenu *helpMenu; QMenu *recentFileMenu; QMenu *recentVideoMenu; + QMenu *contextMenu; // Toolbar. QToolBar *mainToolBar; @@ -267,10 +270,9 @@ private: QAction *exitAction; QAction *undoAction; QAction *redoAction; -// QAction *cutAction; -// QAction *copyAction; -// QAction *pasteAction; + QAction *cloneAction; QAction *deleteAction; + QAction *renameAction; QAction *preferencesAction; QAction *aboutAction; QAction *clearRecentFileActions; diff --git a/MapperGLCanvas.cpp b/MapperGLCanvas.cpp index 9723200..cb13ddb 100644 --- a/MapperGLCanvas.cpp +++ b/MapperGLCanvas.cpp @@ -138,7 +138,7 @@ void MapperGLCanvas::mousePressEvent(QMouseEvent* event) return; // Select a shape with a click. - if (event->buttons() & Qt::LeftButton) + if (event->buttons() & Qt::LeftButton || Qt::RightButton) // Add Right click for context menu { Shape* orig = getCurrentShape(); MappingManager manager = getMainWindow()->getMappingManager(); @@ -162,10 +162,13 @@ void MapperGLCanvas::mousePressEvent(QMouseEvent* event) } // Grab the shape. - if (orig && orig->includesPoint(_mousePressedPosition)) + if (event->buttons() & Qt::LeftButton) // This preserve me from duplicate code above { - _shapeGrabbed = true; - _shapeFirstGrab = true; + if (orig && orig->includesPoint(_mousePressedPosition)) + { + _shapeGrabbed = true; + _shapeFirstGrab = true; + } } } diff --git a/MappingManager.cpp b/MappingManager.cpp index 14c8735..11c03a3 100644 --- a/MappingManager.cpp +++ b/MappingManager.cpp @@ -71,8 +71,6 @@ bool MappingManager::removePaint(uid paintId) return true; } - - return false; }