UI to learn hashes of local image files

This addes a menu entry for the user to select a directory that is recursively
traversed to look for image files and compute the hashes of those images (for
those images to be available to be displayed in dives according to their hash values).

This traversal and hash computation happens in and independend thread and so far
the only feedback to the user is that upon completion the dispayed images are updated.

Signed-off-by: Robert C. Helling <helling@atdotde.de>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Robert C. Helling 2015-02-26 14:44:27 +01:00 committed by Dirk Hohndel
parent b02bf002a6
commit e6482bbdc8
5 changed files with 51 additions and 1 deletions

View file

@ -319,6 +319,24 @@ void MainWindow::on_actionSaveAs_triggered()
file_save_as(); file_save_as();
} }
void MainWindow::on_actionHash_images_triggered()
{
QFileDialog dialog(this, tr("Traverse image directories"), lastUsedDir(), filter());
dialog.setFileMode(QFileDialog::Directory);
dialog.setViewMode(QFileDialog::Detail);
dialog.setLabelText(QFileDialog::Accept, tr("Scan"));
dialog.setLabelText(QFileDialog::Reject, tr("Cancel"));
QStringList dirnames;
if (dialog.exec())
dirnames = dialog.selectedFiles();
if (dirnames.isEmpty())
return;
foreach (QString dir, dirnames) {
QtConcurrent::run(learnImages, QDir(dir), 10, false);
}
}
ProfileWidget2 *MainWindow::graphics() const ProfileWidget2 *MainWindow::graphics() const
{ {
return qobject_cast<ProfileWidget2*>(applicationState["Default"].topRight->layout()->itemAt(1)->widget()); return qobject_cast<ProfileWidget2*>(applicationState["Default"].topRight->layout()->itemAt(1)->widget());

View file

@ -104,6 +104,7 @@ slots:
void on_actionPrint_triggered(); void on_actionPrint_triggered();
void on_actionPreferences_triggered(); void on_actionPreferences_triggered();
void on_actionQuit_triggered(); void on_actionQuit_triggered();
void on_actionHash_images_triggered();
/* log menu actions */ /* log menu actions */
void on_actionDownloadDC_triggered(); void on_actionDownloadDC_triggered();

View file

@ -50,7 +50,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>861</width> <width>861</width>
<height>25</height> <height>22</height>
</rect> </rect>
</property> </property>
<widget class="QMenu" name="menuFile"> <widget class="QMenu" name="menuFile">
@ -67,6 +67,7 @@
<addaction name="actionPrint"/> <addaction name="actionPrint"/>
<addaction name="actionPreferences"/> <addaction name="actionPreferences"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="actionHash_images"/>
<addaction name="actionConfigure_Dive_Computer"/> <addaction name="actionConfigure_Dive_Computer"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="actionRecent1"/> <addaction name="actionRecent1"/>
@ -690,6 +691,11 @@
<string>Ctrl+Shift+Z</string> <string>Ctrl+Shift+Z</string>
</property> </property>
</action> </action>
<action name="actionHash_images">
<property name="text">
<string>Hash images</string>
</property>
</action>
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget> <customwidget>

View file

@ -849,3 +849,27 @@ void updateHash(struct picture *picture) {
picture->hash = strdup(hash.toHex()); picture->hash = strdup(hash.toHex());
free(old); free(old);
} }
void learnImages(const QDir dir, int max_recursions, bool recursed)
{
QDir current(dir);
QStringList filters, files;
if (max_recursions) {
foreach (QString dirname, dir.entryList(QStringList(), QDir::NoDotAndDotDot | QDir::Dirs)) {
learnImages(QDir(dir.filePath(dirname)), max_recursions - 1, true);
}
}
foreach (QString format, QImageReader::supportedImageFormats()) {
filters.append(QString("*.").append(format));
}
foreach (QString file, dir.entryList(filters, QDir::Files)) {
files.append(dir.absoluteFilePath(file));
}
QtConcurrent::blockingMap(files, hashFile);
if (!recursed)
DivePictureModel::instance()->updateDivePictures();
}

View file

@ -21,6 +21,7 @@ void read_hashes();
void write_hashes(); void write_hashes();
void updateHash(struct picture *picture); void updateHash(struct picture *picture);
QByteArray hashFile(const QString filename); QByteArray hashFile(const QString filename);
void learnImages(const QDir dir, int max_recursions, bool recursed);
void add_hash(const QString filename, QByteArray &hash); void add_hash(const QString filename, QByteArray &hash);
QString localFilePath(const QString originalFilename); QString localFilePath(const QString originalFilename);
QString fileFromHash(char *hash); QString fileFromHash(char *hash);