MainWindow.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. #include "MainWindow.hpp"
  2. #include "ImageHasher.hpp"
  3. #include <QDir>
  4. #include <QDirIterator>
  5. #include <QFile>
  6. #include <QFileDialog>
  7. #include <QFileInfo>
  8. #include <QHBoxLayout>
  9. #include <QMessageBox>
  10. #include <QPixmap>
  11. #include <QVBoxLayout>
  12. #include <chrono>
  13. #include <filesystem>
  14. #include <QAction>
  15. #include <QApplication>
  16. #include <QClipboard>
  17. #include <QContextMenuEvent>
  18. #include <QCoreApplication>
  19. #include <QDesktopServices>
  20. #include <QImageReader>
  21. #include <QMenu>
  22. #include <QPointer>
  23. #include <QSettings>
  24. #include <QStandardPaths>
  25. #include <QUrl>
  26. #include <QtConcurrent>
  27. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
  28. QString dataPath =
  29. QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
  30. if (!QDir().exists(dataPath)) {
  31. QDir().mkpath(dataPath);
  32. }
  33. QString dbPath = dataPath + "/dupfind_cache.db";
  34. m_dbManager = std::make_unique<DatabaseManager>(dbPath.toStdString());
  35. m_dbManager->open();
  36. m_scanWatcher = new QFutureWatcher<void>(this);
  37. m_searchWatcher = new QFutureWatcher<std::vector<DuplicateGroup>>(this);
  38. m_searchTimer = new QTimer(this);
  39. m_searchTimer->setSingleShot(true);
  40. m_searchTimer->setInterval(300);
  41. connect(m_searchTimer, &QTimer::timeout, this,
  42. &MainWindow::performAsyncSearch);
  43. connect(m_searchWatcher,
  44. &QFutureWatcher<std::vector<DuplicateGroup>>::finished, this,
  45. &MainWindow::onSearchFinished);
  46. // スタイルの適用
  47. QFile styleFile("resources/style.qss");
  48. if (styleFile.open(QFile::ReadOnly)) {
  49. QString styleSheet = QLatin1String(styleFile.readAll());
  50. setStyleSheet(styleSheet);
  51. }
  52. loadSettings(); // setupUi の前に読み込む
  53. setupUi();
  54. }
  55. MainWindow::~MainWindow() {}
  56. std::vector<ImageData> MainWindow::getFilteredImages() {
  57. auto allImages = m_dbManager->getAllImages();
  58. if (m_dirList->count() == 0)
  59. return {};
  60. std::vector<ImageData> filtered;
  61. for (const auto &img : allImages) {
  62. bool match = false;
  63. for (int i = 0; i < m_dirList->count(); ++i) {
  64. std::string dirPath = m_dirList->item(i)->text().toStdString();
  65. std::string dirPathWithSlash = dirPath;
  66. if (!dirPathWithSlash.empty() && dirPathWithSlash.back() != '/' &&
  67. dirPathWithSlash.back() != '\\') {
  68. dirPathWithSlash += "/";
  69. }
  70. if (img.path.find(dirPathWithSlash) == 0 || img.path == dirPath) {
  71. match = true;
  72. break;
  73. }
  74. }
  75. if (match && m_ignoredPaths.find(img.path) == m_ignoredPaths.end()) {
  76. filtered.push_back(img);
  77. }
  78. }
  79. return filtered;
  80. }
  81. void MainWindow::setupUi() {
  82. auto *central = new QWidget();
  83. auto *mainLayout = new QVBoxLayout(central);
  84. // Toolbar-like top section
  85. auto *toolLayout = new QHBoxLayout();
  86. m_addDirBtn = new QPushButton(tr("Add Directory"));
  87. m_removeDirBtn = new QPushButton(tr("Remove Selected"));
  88. m_startScanBtn = new QPushButton(tr("Start Scan"));
  89. m_deleteBtn = new QPushButton(tr("Delete Selected Duplicates"));
  90. m_deleteBtn->setObjectName("deleteBtn"); // QSS で赤くするため
  91. toolLayout->addWidget(m_addDirBtn);
  92. toolLayout->addWidget(m_removeDirBtn);
  93. toolLayout->addStretch();
  94. // Slider section
  95. toolLayout->addWidget(new QLabel(tr("Similarity Threshold:")));
  96. m_thresholdSlider = new QSlider(Qt::Horizontal);
  97. m_thresholdSlider->setRange(0, 32);
  98. m_thresholdSlider->setValue(m_currentThreshold);
  99. m_thresholdSlider->setFixedWidth(150);
  100. m_thresholdLabel = new QLabel(QString::number(m_currentThreshold));
  101. toolLayout->addWidget(m_thresholdSlider);
  102. toolLayout->addWidget(m_thresholdLabel);
  103. m_strictCheckBox = new QCheckBox(tr("Strict"));
  104. m_strictCheckBox->setChecked(m_strictMode);
  105. toolLayout->addWidget(m_strictCheckBox);
  106. toolLayout->addWidget(m_startScanBtn);
  107. m_clearBtn = new QPushButton(tr("Clear Results"));
  108. toolLayout->addWidget(m_clearBtn);
  109. m_deselectBtn = new QPushButton(tr("Deselect All"));
  110. toolLayout->addWidget(m_deselectBtn);
  111. toolLayout->addWidget(m_deleteBtn);
  112. mainLayout->addLayout(toolLayout);
  113. // Split view
  114. auto *splitLayout = new QHBoxLayout();
  115. // Directory List
  116. m_dirList = new QListWidget();
  117. m_dirList->setMaximumWidth(250);
  118. // m_dirList->addItems(m_loadedDirs); // ここで復元
  119. for (const QString &dir : m_loadedDirs) {
  120. auto *item = new QListWidgetItem(dir, m_dirList);
  121. item->setToolTip(dir);
  122. }
  123. splitLayout->addWidget(m_dirList);
  124. // Results section (Search box + List View)
  125. auto *resultLayout = new QVBoxLayout();
  126. m_searchBox = new QLineEdit();
  127. m_searchBox->setPlaceholderText(
  128. tr("Filter results by path or filename... (Press Esc to close)"));
  129. m_searchBox->setVisible(false);
  130. resultLayout->addWidget(m_searchBox);
  131. // Results List View
  132. m_resultView = new QListView();
  133. m_model = new ResultListModel(this);
  134. m_proxyModel = new ResultFilterProxyModel(this);
  135. m_proxyModel->setSourceModel(m_model);
  136. m_delegate = new ResultItemDelegate(this);
  137. m_resultView->setModel(m_proxyModel);
  138. m_resultView->setItemDelegate(m_delegate);
  139. m_resultView->setSelectionMode(QAbstractItemView::NoSelection);
  140. m_resultView->setSpacing(5);
  141. m_resultView->installEventFilter(this);
  142. m_searchBox->installEventFilter(this);
  143. resultLayout->addWidget(m_resultView);
  144. splitLayout->addLayout(resultLayout);
  145. mainLayout->addLayout(splitLayout);
  146. // Progress Bar
  147. m_progressBar = new QProgressBar();
  148. m_progressBar->setVisible(false);
  149. mainLayout->addWidget(m_progressBar);
  150. setCentralWidget(central);
  151. // Connections
  152. connect(m_addDirBtn, &QPushButton::clicked, this,
  153. &MainWindow::onAddDirectory);
  154. connect(m_removeDirBtn, &QPushButton::clicked, this,
  155. &MainWindow::onRemoveDirectory);
  156. connect(m_startScanBtn, &QPushButton::clicked, this,
  157. &MainWindow::onStartScan);
  158. connect(m_searchBox, &QLineEdit::textChanged, this,
  159. &MainWindow::onSearchTextChanged);
  160. connect(m_clearBtn, &QPushButton::clicked, this, &MainWindow::onClearResults);
  161. connect(m_deselectBtn, &QPushButton::clicked, this,
  162. [this]() { m_model->clearAllChecks(); });
  163. connect(m_deleteBtn, &QPushButton::clicked, this,
  164. &MainWindow::onDeleteSelected);
  165. connect(m_thresholdSlider, &QSlider::valueChanged, this,
  166. &MainWindow::onThresholdChanged);
  167. connect(m_strictCheckBox, &QCheckBox::stateChanged, this,
  168. &MainWindow::onStrictChanged);
  169. connect(m_scanWatcher, &QFutureWatcher<void>::finished, this,
  170. &MainWindow::onScanFinished);
  171. connect(m_delegate, &ResultItemDelegate::contextMenuRequested, this,
  172. &MainWindow::onContextMenuRequested);
  173. connect(m_delegate, &ResultItemDelegate::fileDoubleClicked, this,
  174. &MainWindow::onFileDoubleClicked);
  175. }
  176. void MainWindow::loadSettings() {
  177. // QString iniPath = QCoreApplication::applicationDirPath() + "/DupFind.ini";
  178. QString dataPath =
  179. QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
  180. if (!QDir().exists(dataPath)) {
  181. QDir().mkpath(dataPath);
  182. }
  183. QString iniPath = dataPath + "/settings.ini";
  184. QSettings settings(iniPath, QSettings::IniFormat);
  185. m_currentThreshold = settings.value("threshold", 5).toInt();
  186. m_strictMode = settings.value("strict_mode", false).toBool();
  187. m_loadedDirs = settings.value("directories").toStringList();
  188. }
  189. void MainWindow::saveSettings() {
  190. // QString iniPath = QCoreApplication::applicationDirPath() + "/DupFind.ini";
  191. QString dataPath =
  192. QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
  193. if (!QDir().exists(dataPath)) {
  194. QDir().mkpath(dataPath);
  195. }
  196. QString iniPath = dataPath + "/settings.ini";
  197. QSettings settings(iniPath, QSettings::IniFormat);
  198. settings.setValue("threshold", m_currentThreshold);
  199. settings.setValue("strict_mode", m_strictMode);
  200. QStringList dirs;
  201. for (int i = 0; i < m_dirList->count(); ++i) {
  202. dirs << m_dirList->item(i)->text();
  203. }
  204. if (dirs.isEmpty()) {
  205. settings.remove("directories");
  206. } else {
  207. settings.setValue("directories", dirs);
  208. }
  209. }
  210. void MainWindow::closeEvent(QCloseEvent *event) {
  211. saveSettings();
  212. QMainWindow::closeEvent(event);
  213. }
  214. void MainWindow::onAddDirectory() {
  215. QString dir =
  216. QFileDialog::getExistingDirectory(this, tr("Select Directory to Scan"));
  217. if (!dir.isEmpty()) {
  218. auto *item = new QListWidgetItem(dir, m_dirList);
  219. item->setToolTip(dir);
  220. saveSettings();
  221. }
  222. }
  223. void MainWindow::onRemoveDirectory() {
  224. auto *item = m_dirList->currentItem();
  225. if (item) {
  226. QString dirPath = item->text();
  227. m_dbManager->setDirectorySearchedStatus(dirPath.toStdString(), false);
  228. delete item;
  229. // キャッシュ再読み込み
  230. m_lastScannedImages = getFilteredImages();
  231. saveSettings();
  232. }
  233. }
  234. // 「Start Scan」ボタン押下時の処理
  235. // 選択されたディレクトリを再帰的に走査し、並列処理によって高速に画像ハッシュを分散計算し、DB保存する
  236. void MainWindow::onStartScan() {
  237. if (m_dirList->count() == 0) {
  238. QMessageBox::warning(this, tr("No Directory"),
  239. tr("Please add at least one directory to scan."));
  240. return;
  241. }
  242. m_progressBar->setVisible(true);
  243. m_progressBar->setRange(0, 0); // 準備中
  244. m_startScanBtn->setEnabled(false);
  245. // 1. スキャン対象ディレクトリの収集
  246. std::vector<QString> dirPaths;
  247. for (int i = 0; i < m_dirList->count(); ++i) {
  248. dirPaths.push_back(m_dirList->item(i)->text());
  249. }
  250. // キャッシュ(DBの既存データ)を読み込み
  251. auto cachedList = m_dbManager->getAllImages();
  252. std::unordered_map<std::string, ImageData> cache;
  253. for (const auto &img : cachedList) {
  254. cache[img.path] = img;
  255. }
  256. // 2. 非同期で一連の処理を実行
  257. auto future = QtConcurrent::run([this, dirPaths, cache]() {
  258. // 全ファイルパスをリストアップ
  259. std::vector<std::string> allFiles;
  260. const QStringList filters = {"*.jpg", "*.png", "*.jpeg", "*.bmp",
  261. "*.webp", "*.tiff", "*.heic", "*.heif"};
  262. for (const auto &dirPath : dirPaths) {
  263. QDirIterator it(dirPath, filters, QDir::Files | QDir::NoSymLinks,
  264. QDirIterator::Subdirectories);
  265. while (it.hasNext()) {
  266. allFiles.push_back(it.next().toStdString());
  267. }
  268. }
  269. // 3. 並列ハッシュ計算 (mapped)
  270. auto processFunc = [&cache](const std::string &stdPath) -> ImageData {
  271. try {
  272. std::error_code ec;
  273. auto currentSize = std::filesystem::file_size(stdPath, ec);
  274. auto currentMtime = std::chrono::duration_cast<std::chrono::seconds>(
  275. std::filesystem::last_write_time(stdPath, ec)
  276. .time_since_epoch())
  277. .count();
  278. auto it = cache.find(stdPath);
  279. if (it != cache.end()) {
  280. if (it->second.file_size == static_cast<int64_t>(currentSize) &&
  281. it->second.timestamp == static_cast<int64_t>(currentMtime)) {
  282. return {}; // キャッシュと一致する場合はDBの再書き込みを避けるため空を返す
  283. }
  284. }
  285. cv::Mat img = ImageHasher::loadImage(stdPath);
  286. ImageData data;
  287. data.path = stdPath;
  288. data.is_searched = false;
  289. if (!img.empty()) {
  290. data.dhash = ImageHasher::calculateDHash(img);
  291. data.phash = ImageHasher::calculatePHash(img);
  292. }
  293. data.timestamp = currentMtime;
  294. data.file_size = currentSize;
  295. return data;
  296. } catch (...) {
  297. return {};
  298. }
  299. };
  300. // 並列実行
  301. auto results = QtConcurrent::blockingMapped(allFiles, processFunc);
  302. // 4. DBへの一括保存 (メインスレッドの管理外のDB接続で行う)
  303. QString dataPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
  304. QString dbPath = dataPath + "/dupfind_cache.db";
  305. DatabaseManager db(dbPath.toStdString());
  306. if (db.open()) {
  307. db.cleanupStaleEntries(); // DBに存在して実体がないファイルを削除
  308. db.beginTransaction();
  309. for (const auto &data : results) {
  310. if (data.path.empty())
  311. continue; // 全く変更がないファイルはスキップ
  312. db.addImage(data);
  313. }
  314. db.commitTransaction();
  315. }
  316. });
  317. m_scanWatcher->setFuture(future);
  318. }
  319. void MainWindow::onScanFinished() {
  320. m_startScanBtn->setEnabled(true);
  321. auto images = getFilteredImages();
  322. m_lastScannedImages = images; // キャッシュを更新
  323. // スキャン後は同期処理で固まらないように非同期検索を起動する
  324. performAsyncSearch();
  325. }
  326. void MainWindow::onThresholdChanged(int value) {
  327. m_currentThreshold = value;
  328. m_thresholdLabel->setText(QString::number(value));
  329. // Hide and clear search to reset filter on new search condition
  330. if (m_searchBox->isVisible() || !m_searchBox->text().isEmpty()) {
  331. m_searchBox->clear();
  332. m_searchBox->setVisible(false);
  333. m_resultView->setFocus();
  334. }
  335. // フリーズして強制終了しても次回起動時に閾値が復元されるように保存
  336. saveSettings();
  337. // 操作が止まるまで待機(デバウンス)
  338. m_searchTimer->start();
  339. }
  340. void MainWindow::onStrictChanged(int state) {
  341. m_strictMode = (state == Qt::Checked);
  342. // Hide and clear search to reset filter on new search condition
  343. if (m_searchBox->isVisible() || !m_searchBox->text().isEmpty()) {
  344. m_searchBox->clear();
  345. m_searchBox->setVisible(false);
  346. m_resultView->setFocus();
  347. }
  348. m_searchTimer->start();
  349. }
  350. // ハミング距離などに基づき、メモリ上の全画像データから重複グループを非同期で抽出・クラスタリングする
  351. void MainWindow::performAsyncSearch() {
  352. if (m_lastScannedImages.empty()) {
  353. m_lastScannedImages = getFilteredImages();
  354. }
  355. if (m_lastScannedImages.empty())
  356. return;
  357. m_progressBar->setVisible(true);
  358. m_progressBar->setRange(0, 0);
  359. m_progressBar->setFormat(tr("Searching duplicates... %p%"));
  360. // 現在実行中の検索があればキャンセルはできないが、WatcherのFutureを上書きすることで最新のみを追う
  361. auto future = QtConcurrent::run([images = m_lastScannedImages,
  362. threshold = m_currentThreshold,
  363. strict = m_strictMode]() {
  364. return SimilaritySearch::findDuplicates(images, threshold, strict);
  365. });
  366. m_searchWatcher->setFuture(future);
  367. }
  368. void MainWindow::onSearchFinished() {
  369. m_progressBar->setVisible(false);
  370. m_currentGroups = m_searchWatcher->result();
  371. updateResultGrid(m_currentGroups);
  372. // サムネイルが表示された時点で検索対象ディレクトリ群を検索済みとする
  373. for (int i = 0; i < m_dirList->count(); ++i) {
  374. QString dirPath = m_dirList->item(i)->text();
  375. m_dbManager->setDirectorySearchedStatus(dirPath.toStdString(), true);
  376. }
  377. // メモリ上のキャッシュも更新する
  378. m_lastScannedImages = getFilteredImages();
  379. }
  380. void MainWindow::removeGroupFromView(int groupId) {
  381. if (groupId >= 0 && groupId < static_cast<int>(m_currentGroups.size())) {
  382. for (const auto &img : m_currentGroups[groupId].images) {
  383. m_ignoredPaths.insert(img.path);
  384. }
  385. m_lastScannedImages = getFilteredImages();
  386. m_currentGroups.erase(m_currentGroups.begin() + groupId);
  387. updateResultGrid(m_currentGroups, true);
  388. }
  389. }
  390. void MainWindow::onClearResults() { m_model->clear(); }
  391. bool MainWindow::eventFilter(QObject *obj, QEvent *event) {
  392. if (event->type() == QEvent::KeyPress) {
  393. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
  394. if (obj == m_resultView) {
  395. if ((keyEvent->modifiers() & Qt::ControlModifier) &&
  396. keyEvent->key() == Qt::Key_F) {
  397. m_searchBox->setVisible(true);
  398. m_searchBox->setFocus();
  399. return true;
  400. } else if (keyEvent->key() == Qt::Key_F &&
  401. keyEvent->modifiers() == Qt::NoModifier) {
  402. m_searchBox->setVisible(true);
  403. m_searchBox->setFocus();
  404. return true;
  405. }
  406. } else if (obj == m_searchBox) {
  407. if (keyEvent->key() == Qt::Key_Escape) {
  408. m_searchBox->clear();
  409. m_searchBox->setVisible(false);
  410. m_resultView->setFocus();
  411. return true;
  412. }
  413. }
  414. }
  415. return QMainWindow::eventFilter(obj, event);
  416. }
  417. void MainWindow::onSearchTextChanged(const QString &text) {
  418. if (m_proxyModel) {
  419. m_proxyModel->setSearchText(text);
  420. }
  421. }
  422. void MainWindow::onFileDoubleClicked(const std::string &path) {
  423. if (!path.empty()) {
  424. QDesktopServices::openUrl(
  425. QUrl::fromLocalFile(QString::fromStdString(path)));
  426. }
  427. }
  428. void MainWindow::onContextMenuRequested(const std::string &path, int groupId,
  429. const QPoint &globalPos) {
  430. if (!path.empty()) {
  431. QMenu menu;
  432. QAction *copyAction = menu.addAction(tr("Copy Full Path(&C)"));
  433. menu.addSeparator();
  434. QAction *removeAction = menu.addAction(tr("Remove from List(&R)"));
  435. QAction *selectedAction = menu.exec(globalPos);
  436. if (selectedAction == copyAction) {
  437. QApplication::clipboard()->setText(QString::fromStdString(path));
  438. } else if (selectedAction == removeAction) {
  439. removeGroupFromView(groupId);
  440. }
  441. }
  442. }
  443. // 同一・類似と判定された画像のグループを受け取り、UI上のグリッドレイアウトへ動的にサムネイルと削除候補のチェックボックスを描画する
  444. void MainWindow::updateResultGrid(const std::vector<DuplicateGroup> &groups,
  445. bool preserveState) {
  446. m_model->setGroups(groups, preserveState);
  447. }
  448. void MainWindow::onDeleteSelected() {
  449. std::unordered_set<int> visibleGroupIds;
  450. for (int i = 0; i < m_proxyModel->rowCount(); ++i) {
  451. QModelIndex proxyIndex = m_proxyModel->index(i, 0);
  452. QModelIndex sourceIndex = m_proxyModel->mapToSource(proxyIndex);
  453. const auto &item = m_model->getItem(sourceIndex.row());
  454. if (item.type == ResultListItem::Header) {
  455. visibleGroupIds.insert(item.groupId);
  456. }
  457. }
  458. int count = 0;
  459. std::map<int, int> groupTotalCount;
  460. std::map<int, int> groupCheckedCount;
  461. std::vector<std::string> pathsToDelete;
  462. const auto &checkStates = m_model->getCheckStates();
  463. int groupId = 0;
  464. for (const auto &group : m_currentGroups) {
  465. if (visibleGroupIds.find(groupId) != visibleGroupIds.end()) {
  466. for (const auto &img : group.images) {
  467. groupTotalCount[groupId]++;
  468. auto it = checkStates.find(img.path);
  469. if (it != checkStates.end() && it->second) {
  470. groupCheckedCount[groupId]++;
  471. count++;
  472. pathsToDelete.push_back(img.path);
  473. }
  474. }
  475. }
  476. groupId++;
  477. }
  478. if (count == 0)
  479. return;
  480. bool allCheckedInSomeGroup = false;
  481. for (auto const &[gId, total] : groupTotalCount) {
  482. if (groupCheckedCount[gId] == total && total > 0) {
  483. allCheckedInSomeGroup = true;
  484. break;
  485. }
  486. }
  487. if (allCheckedInSomeGroup) {
  488. auto warnRes = QMessageBox::warning(
  489. this, tr("Warning: All images selected"),
  490. tr("一部の類似画像グループで、すべての画像が削除対象としてチェックされてい"
  491. "ます。\n"
  492. "このまま削除すると、それらの画像ファイルはすべて失われます。\n\n"
  493. "本当に削除を実行しますか?"),
  494. QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Cancel);
  495. if (warnRes != QMessageBox::Ok) {
  496. return;
  497. }
  498. }
  499. auto res = QMessageBox::question(
  500. this, tr("Confirm Deletion"),
  501. tr("Are you sure you want to move %1 images to Trash?").arg(count));
  502. if (res == QMessageBox::Yes) {
  503. std::vector<QString> failures;
  504. for (const auto &path : pathsToDelete) {
  505. QString qPath = QString::fromStdString(path);
  506. if (QFile::moveToTrash(qPath)) {
  507. m_dbManager->removeImage(path);
  508. } else {
  509. failures.push_back(qPath);
  510. }
  511. }
  512. if (!failures.empty()) {
  513. QString msg = tr("The following files could not be moved to trash and were "
  514. "NOT deleted:\n\n");
  515. for (const auto &f : failures) {
  516. msg += f + "\n";
  517. }
  518. QMessageBox::warning(this, tr("Deletion Error"), msg);
  519. }
  520. // リストをリフレッシュ
  521. auto images = getFilteredImages();
  522. m_currentGroups = SimilaritySearch::findDuplicates(
  523. images, m_currentThreshold, m_strictMode);
  524. updateResultGrid(m_currentGroups);
  525. }
  526. }