Delete a custom icon with multiple entries using it (#357)

* Made it possible to delete a custom icon with multiple entries using it
This commit is contained in:
Jonathan White 2017-02-28 22:45:40 -05:00 committed by GitHub
parent 8f15ad06f3
commit 4061fc7cf8

View File

@ -273,50 +273,74 @@ void EditWidgetIcons::removeCustomIcon()
QModelIndex index = m_ui->customIconsView->currentIndex(); QModelIndex index = m_ui->customIconsView->currentIndex();
if (index.isValid()) { if (index.isValid()) {
Uuid iconUuid = m_customIconModel->uuidFromIndex(index); Uuid iconUuid = m_customIconModel->uuidFromIndex(index);
int iconUsedCount = 0;
const QList<Entry*> allEntries = m_database->rootGroup()->entriesRecursive(true); const QList<Entry*> allEntries = m_database->rootGroup()->entriesRecursive(true);
QList<Entry*> entriesWithSameIcon;
QList<Entry*> historyEntriesWithSameIcon; QList<Entry*> historyEntriesWithSameIcon;
for (Entry* entry : allEntries) { for (Entry* entry : allEntries) {
bool isHistoryEntry = !entry->group();
if (iconUuid == entry->iconUuid()) { if (iconUuid == entry->iconUuid()) {
if (isHistoryEntry) { // Check if this is a history entry (no assigned group)
if (!entry->group()) {
historyEntriesWithSameIcon << entry; historyEntriesWithSameIcon << entry;
} } else if (m_currentUuid != entry->uuid()) {
else if (m_currentUuid != entry->uuid()) { entriesWithSameIcon << entry;
iconUsedCount++;
} }
} }
} }
const QList<Group*> allGroups = m_database->rootGroup()->groupsRecursive(true); const QList<Group*> allGroups = m_database->rootGroup()->groupsRecursive(true);
for (const Group* group : allGroups) { QList<Group*> groupsWithSameIcon;
for (Group* group : allGroups) {
if (iconUuid == group->iconUuid() && m_currentUuid != group->uuid()) { if (iconUuid == group->iconUuid() && m_currentUuid != group->uuid()) {
iconUsedCount++; groupsWithSameIcon << group;
} }
} }
if (iconUsedCount == 0) { int iconUseCount = entriesWithSameIcon.size() + groupsWithSameIcon.size();
for (Entry* entry : asConst(historyEntriesWithSameIcon)) { if (iconUseCount > 0) {
entry->setUpdateTimeinfo(false); QMessageBox::StandardButton ans = MessageBox::question(this, tr("Confirm Delete"),
entry->setIcon(0); tr("This icon is used by %1 entries, and will be replaced "
entry->setUpdateTimeinfo(true); "by the default icon. Are you sure you want to delete it?")
} .arg(iconUseCount), QMessageBox::Yes | QMessageBox::No);
m_database->metadata()->removeCustomIcon(iconUuid); if (ans == QMessageBox::No) {
m_customIconModel->setIcons(m_database->metadata()->customIconsScaledPixmaps(), // Early out, nothing is changed
m_database->metadata()->customIconsOrder()); return;
if (m_customIconModel->rowCount() > 0) { } else {
m_ui->customIconsView->setCurrentIndex(m_customIconModel->index(0, 0)); // Revert matched entries to the default entry icon
} for (Entry* entry : asConst(entriesWithSameIcon)) {
else { entry->setIcon(Entry::DefaultIconNumber);
updateRadioButtonDefaultIcons(); }
// Revert matched groups to the default group icon
for (Group* group : asConst(groupsWithSameIcon)) {
group->setIcon(Group::DefaultIconNumber);
}
} }
} }
else {
Q_EMIT messageEditEntry(
tr("Can't delete icon. Still used by %1 items.").arg(iconUsedCount), MessageWidget::Error); // Remove the icon from history entries
for (Entry* entry : asConst(historyEntriesWithSameIcon)) {
entry->setUpdateTimeinfo(false);
entry->setIcon(0);
entry->setUpdateTimeinfo(true);
}
// Remove the icon from the database
m_database->metadata()->removeCustomIcon(iconUuid);
m_customIconModel->setIcons(m_database->metadata()->customIconsScaledPixmaps(),
m_database->metadata()->customIconsOrder());
// Reset the current icon view
updateRadioButtonDefaultIcons();
if (m_database->resolveEntry(m_currentUuid) != nullptr) {
m_ui->defaultIconsView->setCurrentIndex(m_defaultIconModel->index(Entry::DefaultIconNumber));
} else {
m_ui->defaultIconsView->setCurrentIndex(m_defaultIconModel->index(Group::DefaultIconNumber));
} }
} }
} }