private (int, int) ResolveDuplicates() { if (db.duplicates.Count == 0) { return(0, 0); } int replaced = 0, removed = 0; CompareView compare = new CompareView(db, true); compare.Owner = this; Enabled = false; bool cancel = false; while (db.duplicates.Count > 0 && !cancel) { Duplicate duplicate = db.duplicates[0]; int? complex = null; if (db.duplicates.Any(x => x != duplicate && x.IsSameImage(duplicate))) { complex = db.duplicates.Count(x => x != duplicate && x.IsSameImage(duplicate)); } DialogResult result = compare.Show(duplicate.image1, duplicate.image2, duplicate.dist, complex); if (result != DialogResult.OK) { break; } switch (compare.CompareResult) { case CompareView.Result.Left: // replace existing if (duplicate.image2.score != 0) { duplicate.image1.score = duplicate.image2.score; int index = db.sortedImages.IndexOf(duplicate.image2); db.sortedImages[index] = duplicate.image1; db.newImages.Remove(duplicate.image1); db.imagesDict.Remove(duplicate.image2.Filename); Utility.SafeDelete(this, duplicate.image2.path); db.duplicates.RemoveAll(x => x.image1 == duplicate.image2 || x.image2 == duplicate.image2); ++replaced; } else { db.newImages.Remove(duplicate.image2); db.imagesDict.Remove(duplicate.image2.Filename); Utility.SafeDelete(this, duplicate.image2.path); db.duplicates.RemoveAll(x => x.image1 == duplicate.image2 || x.image2 == duplicate.image2); ++removed; } break; case CompareView.Result.Right: // keep existing db.newImages.Remove(duplicate.image1); db.imagesDict.Remove(duplicate.image1.Filename); Utility.SafeDelete(this, duplicate.image1.path); db.duplicates.RemoveAll(x => x.image1 == duplicate.image1 || x.image2 == duplicate.image1); ++removed; break; case CompareView.Result.Both: // keep both db.duplicates.RemoveAt(0); break; case CompareView.Result.Complex: { List <Duplicate> dups = db.duplicates.Where(x => x.IsSameImage(duplicate)).ToList(); List <Image> dupImages = new List <Image>(); foreach (Duplicate dup in dups) { if (!dupImages.Contains(dup.image1)) { dupImages.Add(dup.image1); } if (!dupImages.Contains(dup.image2)) { dupImages.Add(dup.image2); } } List <DuplicateItem> dupItems = dupImages .Select(x => new DuplicateItem { image = x }) .ToList(); compare.Hide(); using (MultiCompareView view = new MultiCompareView(db, dupItems)) { DialogResult result2 = view.ShowDialog(this); if (result2 == DialogResult.Cancel) { cancel = true; } else if (result2 == DialogResult.OK) { db.duplicates.RemoveAll(x => dups.Contains(x)); replaced += view.replaced; removed += view.removed; } } } break; } } compare.Close(); Enabled = true; Activate(); return(replaced, removed); }
private void sortToolStripMenuItem_Click(object sender, System.EventArgs e) { if (db.newImages.Count == 0) { MessageBox.Show("All sorted!"); return; } CompareView compare = new CompareView(db, false); Enabled = false; compare.Owner = this; bool anythingDone = false, cancel = false; while (db.newImages.Count != 0 && !cancel) { int index = Utility.Rand % db.newImages.Count; Image image = db.newImages[index]; if (db.sortedImages.Count == 0) { image.score = 1; db.sortedImages.Add(image); db.newImages.RemoveAt(index); anythingDone = true; continue; } int L = 0, R = db.sortedImages.Count - 1; while (true) { int mid = (L + R) / 2; Image image2 = db.sortedImages[mid]; DialogResult result = compare.Show(image, image2); if (result == DialogResult.OK) { if (L == R || L == mid) { image.score = 1; db.sortedImages.Insert(compare.CompareResult == CompareView.Result.Right ? L : L + 1, image); db.newImages.RemoveAt(index); anythingDone = true; break; } if (compare.CompareResult == CompareView.Result.Left) { L = mid + 1; } else { R = mid - 1; } } else { cancel = true; break; } } } compare.Close(); Enabled = true; Activate(); if (anythingDone) { UpdateStatus(); } if (!cancel) { MessageBox.Show("All sorted!"); } }