diff --git a/MainWindow.cpp b/MainWindow.cpp index 18c4ecf..341034b 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -37,6 +37,7 @@ MainWindow::MainWindow() // TODO: not sure we need this anymore since we have NULL_UID _hasCurrentPaint = false; _hasCurrentMapping = false; + currentSelectedItem = NULL; // Create everything. createLayout(); @@ -82,14 +83,19 @@ void MainWindow::handlePaintItemSelectionChanged() { // Set current paint. QListWidgetItem* item = paintList->currentItem(); - uid idx = getItemId(*item); - setCurrentPaint(idx); - removeCurrentMapping(); + currentSelectedItem = item; - // Enable creation of mappings when a paint is selected. - addMeshAction->setEnabled(true); - addTriangleAction->setEnabled(true); - addEllipseAction->setEnabled(true); + if (item) + { + uid idx = getItemId(*item); + setCurrentPaint(idx); + removeCurrentMapping(); + + // Enable creation of mappings when a paint is selected. + addMeshAction->setEnabled(true); + addTriangleAction->setEnabled(true); + addEllipseAction->setEnabled(true); + } // Update canvases. updateCanvases(); @@ -99,6 +105,7 @@ void MainWindow::handleMappingItemSelectionChanged() { // Get current mapping. QListWidgetItem* item = mappingList->currentItem(); + currentSelectedItem = item; if (item) { // Get index. @@ -140,6 +147,12 @@ void MainWindow::handleMappingIndexesMoved() updateCanvases(); } +void MainWindow::handleItemSelected(QListWidgetItem* item) +{ + // Change currently selected item. + currentSelectedItem = item; +} + void MainWindow::closeEvent(QCloseEvent *event) { // Popup dialog allowing the user to save before closing. @@ -353,14 +366,22 @@ void MainWindow::updateStatusBar() void MainWindow::deleteItem() { - if (getCurrentMappingId() != NULL_UID) + if (currentSelectedItem) { - // Delete mapping. - deleteMapping(getCurrentMappingId()); - } - else if (getCurrentPaintId() != NULL_UID) - { - // Delete paint. + if (currentSelectedItem->listWidget() == mappingList) + { + // Delete mapping. + deleteMapping( getItemId(*mappingList->currentItem()) ); + //currentSelectedItem = NULL; + } + else if (currentSelectedItem->listWidget() == paintList) + { + // Delete paint. + deletePaint( getItemId(*paintList->currentItem()) ); + //currentSelectedItem = NULL; + } + else + qCritical() << "Selected item neither a mapping nor a paint." << endl; } } @@ -659,7 +680,11 @@ void MainWindow::deleteMapping(uid mappingId) /// Deletes/removes a paint and all associated mappigns. void MainWindow::deletePaint(uid paintId) { - + // Cannot delete unexisting paint. + if (Paint::getUidAllocator().exists(paintId)) + { + removePaintItem(paintId); + } } @@ -1144,7 +1169,7 @@ bool MainWindow::addColorPaint(const QColor& color) return true; } -void MainWindow::addMappingItem(uint mappingId) +void MainWindow::addMappingItem(uid mappingId) { Mapping::ptr mapping = mappingManager->getMappingById(mappingId); Q_CHECK_PTR(mapping); @@ -1233,7 +1258,7 @@ void MainWindow::addMappingItem(uint mappingId) mappingList->setCurrentItem(item); } -void MainWindow::removeMappingItem(uint mappingId) +void MainWindow::removeMappingItem(uid mappingId) { Mapping::ptr mapping = mappingManager->getMappingById(mappingId); Q_CHECK_PTR(mapping); @@ -1245,11 +1270,15 @@ void MainWindow::removeMappingItem(uint mappingId) propertyPanel->removeWidget(mappers[mappingId]->getPropertiesEditor()); mappers.remove(mappingId); - // Remove widget from layerList. - qDebug() << "Current mapping: " << mappingList->currentItem()->data(Qt::UserRole).toInt() << endl; - qDebug() << "Trying to remove " << mappingId << endl; - Q_ASSERT(mappingList->currentItem()->data(Qt::UserRole).toUInt() == mappingId); - delete mappingList->takeItem(mappingList->row(mappingList->currentItem())); + // Remove widget from mappingList. + int row = getItemRowFromId(*mappingList, mappingId); + Q_ASSERT( row >= 0 ); + QListWidgetItem* item = mappingList->takeItem(row); + if (item == currentSelectedItem) + currentSelectedItem = NULL; + delete item; + + // Update list. mappingList->update(); // Reset current mapping. @@ -1259,6 +1288,38 @@ void MainWindow::removeMappingItem(uint mappingId) updateCanvases(); } +void MainWindow::removePaintItem(uid paintId) +{ + Paint::ptr paint = mappingManager->getPaintById(paintId); + Q_CHECK_PTR(paint); + + // Remove all mappings associated with paint. + QMap paintMappings = mappingManager->getPaintMappings(paint); + for (QMap::const_iterator it = paintMappings.constBegin(); + it != paintMappings.constEnd(); ++it) + removeMappingItem(it.key()); + + // Remove paint from model. + Q_ASSERT( mappingManager->removePaint(paintId) ); + + // Remove widget from paintList. + int row = getItemRowFromId(*paintList, paintId); + Q_ASSERT( row >= 0 ); + QListWidgetItem* item = paintList->takeItem(row); + if (item == currentSelectedItem) + currentSelectedItem = NULL; + delete item; + + // Update list. + paintList->update(); + + // Reset current paint. + removeCurrentPaint(); + + // Update everything. + updateCanvases(); +} + void MainWindow::clearWindow() { clearProject(); @@ -1279,31 +1340,55 @@ QString MainWindow::strippedName(const QString &fullFileName) void MainWindow::connectProjectWidgets() { connect(paintList, SIGNAL(itemSelectionChanged()), - this, SLOT(handlePaintItemSelectionChanged())); + this, SLOT(handlePaintItemSelectionChanged())); + + connect(paintList, SIGNAL(itemPressed(QListWidgetItem*)), + this, SLOT(handleItemSelected(QListWidgetItem*))); + + connect(paintList, SIGNAL(itemActivated(QListWidgetItem*)), + this, SLOT(handleItemSelected(QListWidgetItem*))); connect(mappingList, SIGNAL(itemSelectionChanged()), - this, SLOT(handleMappingItemSelectionChanged())); + this, SLOT(handleMappingItemSelectionChanged())); connect(mappingList, SIGNAL(itemChanged(QListWidgetItem*)), - this, SLOT(handleMappingItemChanged(QListWidgetItem*))); + this, SLOT(handleMappingItemChanged(QListWidgetItem*))); + + connect(mappingList, SIGNAL(itemPressed(QListWidgetItem*)), + this, SLOT(handleItemSelected(QListWidgetItem*))); + + connect(mappingList, SIGNAL(itemActivated(QListWidgetItem*)), + this, SLOT(handleItemSelected(QListWidgetItem*))); connect(mappingList->model(), SIGNAL(layoutChanged()), - this, SLOT(handleMappingIndexesMoved())); + this, SLOT(handleMappingIndexesMoved())); } void MainWindow::disconnectProjectWidgets() { disconnect(paintList, SIGNAL(itemSelectionChanged()), - this, SLOT(handlePaintItemSelectionChanged())); + this, SLOT(handlePaintItemSelectionChanged())); + + disconnect(paintList, SIGNAL(itemPressed(QListWidgetItem*)), + this, SLOT(handleItemSelected(QListWidgetItem*))); + + disconnect(paintList, SIGNAL(itemActivated(QListWidgetItem*)), + this, SLOT(handleItemSelected(QListWidgetItem*))); disconnect(mappingList, SIGNAL(itemSelectionChanged()), - this, SLOT(handleMappingItemSelectionChanged())); + this, SLOT(handleMappingItemSelectionChanged())); disconnect(mappingList, SIGNAL(itemChanged(QListWidgetItem*)), - this, SLOT(handleMappingItemChanged(QListWidgetItem*))); + this, SLOT(handleMappingItemChanged(QListWidgetItem*))); + + disconnect(mappingList, SIGNAL(itemPressed(QListWidgetItem*)), + this, SLOT(handleItemSelected(QListWidgetItem*))); + + disconnect(mappingList, SIGNAL(itemActivated(QListWidgetItem*)), + this, SLOT(handleItemSelected(QListWidgetItem*))); disconnect(mappingList->model(), SIGNAL(layoutChanged()), - this, SLOT(handleMappingIndexesMoved())); + this, SLOT(handleMappingIndexesMoved())); } uid MainWindow::getItemId(const QListWidgetItem& item) @@ -1316,6 +1401,19 @@ void MainWindow::setItemId(QListWidgetItem& item, uid id) item.setData(Qt::UserRole, id); } +int MainWindow::getItemRowFromId(const QListWidget& list, uid id) +{ + for (int row=0; rowgetId(); } +bool MappingManager::removePaint(uid paintId) +{ + // Make sure the paint to which this paint refers to exists in the manager. + Paint::ptr paint = getPaintById(paintId); + if (paint) + { + // Remove all mappings associated with paint. + QMap paintMappings = getPaintMappings(paint); + for (QMap::const_iterator it = paintMappings.constBegin(); + it != paintMappings.constEnd(); ++it) + removeMapping(it.key()); + + // Remove paint. + int idx = paintVector.lastIndexOf(paint); + Q_ASSERT( idx != -1 ); // Q_ASSERT(mappingVector.contains(mapping)); + paintVector.remove(idx); + paintMap.remove(paint->getId()); + + return true; + } + + return false; +} + uid MappingManager::addMapping(Mapping::ptr mapping) { // Make sure the paint to which this mapping refers to exists in the manager. @@ -61,7 +85,7 @@ uid MappingManager::addMapping(Mapping::ptr mapping) return mapping->getId(); } -void MappingManager::removeMapping(uid mappingId) +bool MappingManager::removeMapping(uid mappingId) { // Make sure the paint to which this mapping refers to exists in the manager. Mapping::ptr mapping = getMappingById(mappingId); @@ -71,7 +95,11 @@ void MappingManager::removeMapping(uid mappingId) Q_ASSERT( idx != -1 ); // Q_ASSERT(mappingVector.contains(mapping)); mappingVector.remove(idx); mappingMap.remove(mapping->getId()); + + return true; } + + return false; } QVector MappingManager::getVisibleMappings() const diff --git a/MappingManager.h b/MappingManager.h index 09c238a..7d20595 100644 --- a/MappingManager.h +++ b/MappingManager.h @@ -49,7 +49,7 @@ public: QMap getPaintMappingsById(uid paintId) const; uid addPaint(Paint::ptr paint); -// bool removePaint(Paint::ptr paint); + bool removePaint(uid paintId); int nPaints() const { return paintVector.size(); } Paint::ptr getPaint(int i) { return paintVector[i]; } Paint::ptr getPaintById(uid id) { return paintMap[id]; } @@ -57,7 +57,7 @@ public: uid addImage(const QString imagePath, int frameWidth, int frameHeight); uid addMapping(Mapping::ptr mapping); - void removeMapping(uid mappingId); + bool removeMapping(uid mappingId); // bool removeMapping(Mapping::ptr mapping); int nMappings() const { return mappingVector.size(); } Mapping::ptr getMapping(int i) { return mappingVector[i]; }