From aa50f0049e6f81470fcda0935ca3da661bc7e37e Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Fri, 23 Sep 2022 12:58:55 -0400 Subject: Load database into in-memory database. Show the progress dialog while loading. --- qdeduper/mingui.cpp | 39 +++++++++++++++++++++++++++++---------- qdeduper/sigdb_qt.cpp | 11 +++++++++-- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/qdeduper/mingui.cpp b/qdeduper/mingui.cpp index 4f5b854..c9447c5 100644 --- a/qdeduper/mingui.cpp +++ b/qdeduper/mingui.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -201,15 +202,31 @@ void DeduperMainWindow::setup_menu() QString dbpath = QFileDialog::getOpenFileName(this, "Load Database", QString(), "Signature database (*.sigdb)"); if (!dbpath.isNull()) { - this->sdb = new SignatureDB(qstring_to_path(dbpath)); - if (!this->sdb->valid()) { - delete this->sdb; - this->sdb = nullptr; - QMessageBox::critical(this, "Error", "Error loading database."); - return; - } - curgroup = 0; - show_group(0); + if (this->sdb) delete this->sdb; + this->sdb = new SignatureDB(); + pd->setMaximum(0); + pd->setMinimum(0); + pd->setLabelText("Loading database..."); + pd->open(); + pd->setCancelButton(nullptr); + auto f = QtConcurrent::run([this, dbpath]() -> bool { + return this->sdb->load(qstring_to_path(dbpath)); + }); + QFutureWatcher *fw = new QFutureWatcher(this); + fw->setFuture(f); + QObject::connect(fw, &QFutureWatcher::finished, this, [this, fw] { + pd->close(); + if (!fw->result()) { + delete this->sdb; + this->sdb = nullptr; + QMessageBox::critical(this, "Error", "Error loading database."); + fw->deleteLater(); + return; + } + curgroup = 0; + show_group(0); + fw->deleteLater(); + }, Qt::ConnectionType::QueuedConnection); } }); menuact["load_db"] = load_db; @@ -474,6 +491,7 @@ void DeduperMainWindow::scan_dirs(std::vector> paths) this->pd->setLabelText("Preparing for database creation..."); this->pd->setMinimum(0); this->pd->setMaximum(0); + this->pd->setCancelButton(new QPushButton("Cancel")); auto f = QtConcurrent::run([this, paths] { FileScanner *fs = new FileScanner(); this->fsc = fs; @@ -528,12 +546,13 @@ void DeduperMainWindow::scan_dirs(std::vector> paths) }); QFutureWatcher *fw = new QFutureWatcher(this); fw->setFuture(f); - QObject::connect(fw, &QFutureWatcher::finished, this, [this] { + QObject::connect(fw, &QFutureWatcher::finished, this, [this, fw] { this->pd->reset(); this->pd->close(); this->curgroup = 0; this->vm = ViewMode::view_normal; this->show_group(this->curgroup); + fw->deleteLater(); }, Qt::ConnectionType::QueuedConnection); QObject::connect(pd, &QProgressDialog::canceled, [this] { if (this->fsc) this->fsc->interrupt(); diff --git a/qdeduper/sigdb_qt.cpp b/qdeduper/sigdb_qt.cpp index 733a198..45fc55a 100644 --- a/qdeduper/sigdb_qt.cpp +++ b/qdeduper/sigdb_qt.cpp @@ -48,6 +48,10 @@ SignatureDB::~SignatureDB() void SignatureDB::create_priv_struct() { if (!valid()) return; + fmap.clear(); + frmap.clear(); + distmap.clear(); + this->groups.clear(); auto ids = sdb->get_image_ids(); sdb->batch_get_signature_begin(); for (auto &id : ids) @@ -63,7 +67,6 @@ void SignatureDB::create_priv_struct() for (auto &dupe : dupes) distmap[std::make_pair(dupe.id1, dupe.id2)] = dupe.distance; - sdb->group_similar(); auto gps = sdb->groups_get(); gps.erase(std::remove_if(gps.begin(), gps.end(), [](std::vector v){ return v.size() < 2; }), gps.end()); this->groups = std::move(gps); @@ -89,6 +92,8 @@ void SignatureDB::scan_files(const std::vector &files, int njobs) Q_EMIT image_scanned(~size_t(0)); + sdb->group_similar(); + create_priv_struct(); } void SignatureDB::interrupt_scan() @@ -156,7 +161,9 @@ size_t SignatureDB::get_path_id(const fs::path& p) bool SignatureDB::load(const fs::path &p) { - return sdb->from_db_file(p); + bool ret = sdb->from_db_file(p); + create_priv_struct(); + return ret; } bool SignatureDB::save(const fs::path &p) -- cgit v1.2.3