Sort HaldCLUT directories (fixes #4123)

This commit is contained in:
Flössie
2017-10-02 07:49:12 +02:00
parent 02807fc1de
commit c6c9ad5eef

View File

@@ -1,3 +1,6 @@
#include <map>
#include <set>
#include "filmsimulation.h" #include "filmsimulation.h"
#include <chrono> #include <chrono>
@@ -56,8 +59,6 @@ bool notifySlowParseDir (const std::chrono::system_clock::time_point& startedAt)
} }
typedef std::vector<Glib::ustring> Strings;
FilmSimulation::FilmSimulation() FilmSimulation::FilmSimulation()
: FoldableToolPanel( this, "filmsimulation", M("TP_FILMSIMULATION_LABEL"), false, true ) : FoldableToolPanel( this, "filmsimulation", M("TP_FILMSIMULATION_LABEL"), false, true )
{ {
@@ -216,13 +217,28 @@ int ClutComboBox::fillFromDir (const Glib::ustring& path)
return result; return result;
} }
int ClutComboBox::parseDir (const Glib::ustring& path) int ClutComboBox::parseDir(const Glib::ustring& path)
{ {
if (path.empty () || !Glib::file_test (path, Glib::FILE_TEST_IS_DIR)) { if (path.empty() || !Glib::file_test(path, Glib::FILE_TEST_IS_DIR)) {
return 0; return 0;
} }
const auto startedAt = std::chrono::system_clock::now (); const auto sorted_dir_dirs = [](const Glib::ustring& path) -> std::map<std::string, std::string>
{
std::map<std::string, std::string> res;
for (const auto& dir : Glib::Dir(path)) {
const std::string full_path = Glib::build_filename(path, dir);
if (Glib::file_test(full_path, Glib::FILE_TEST_IS_DIR)) {
res.emplace(dir, full_path);
}
}
return res;
};
const auto startedAt = std::chrono::system_clock::now();
// Build menu of limited directory structure using breadth-first search // Build menu of limited directory structure using breadth-first search
using Dirs = std::vector<std::pair<Glib::ustring, Gtk::TreeModel::Row>>; using Dirs = std::vector<std::pair<Glib::ustring, Gtk::TreeModel::Row>>;
@@ -232,89 +248,77 @@ int ClutComboBox::parseDir (const Glib::ustring& path)
Dirs currDirs; Dirs currDirs;
Dirs nextDirs; Dirs nextDirs;
currDirs.emplace_back (path, Gtk::TreeModel::Row ()); currDirs.emplace_back(path, Gtk::TreeModel::Row());
while (!currDirs.empty ()) {
while (!currDirs.empty()) {
for (auto& dir : currDirs) { for (auto& dir : currDirs) {
const auto& path = dir.first; const auto& path = dir.first;
const auto& row = dir.second; const auto& row = dir.second;
try { try {
for (const auto& entry : Glib::Dir (path)) { for (const auto& entry : sorted_dir_dirs(path)) {
auto newRow = row ? *m_model->append(row.children()) : *m_model->append();
newRow[m_columns.label] = entry.first;
const auto entryPath = Glib::build_filename (path, entry); nextDirs.emplace_back(entry.second, newRow);
if (!Glib::file_test (entryPath, Glib::FILE_TEST_IS_DIR)) {
continue;
}
auto newRow = row ? *m_model->append (row.children ()) : *m_model->append ();
newRow[m_columns.label] = entry;
nextDirs.emplace_back (entryPath, newRow);
} }
} catch (Glib::Exception&) {} } catch (Glib::Exception&) {}
dirs.push_back (std::move (dir)); dirs.push_back(std::move(dir));
if (!notifySlowParseDir (startedAt)) { if (!notifySlowParseDir(startedAt)) {
m_model->clear (); m_model->clear();
return 0; return 0;
} }
} }
currDirs.clear (); currDirs.clear();
currDirs.swap (nextDirs); currDirs.swap(nextDirs);
} }
} }
// Fill menu structure with CLUT files // Fill menu structure with CLUT files
Strings entries; std::set<Glib::ustring> entries;
auto fileCount = 0; unsigned long fileCount = 0;
for (const auto& dir : dirs) { for (const auto& dir : dirs) {
const auto& path = dir.first; const auto& path = dir.first;
const auto& row = dir.second; const auto& row = dir.second;
entries.clear (); entries.clear();
try { try {
for (const auto& entry : Glib::Dir (path)) { for (const auto& entry : Glib::Dir(path)) {
const auto entryPath = Glib::build_filename(path, entry);
const auto entryPath = Glib::build_filename (path, entry); if (!Glib::file_test(entryPath, Glib::FILE_TEST_IS_REGULAR)) {
if (!Glib::file_test (entryPath, Glib::FILE_TEST_IS_REGULAR)) {
continue; continue;
} }
entries.push_back (entryPath); entries.insert(entryPath);
} }
} catch (Glib::Exception&) {} } catch (Glib::Exception&) {}
std::sort (entries.begin (), entries.end ());
for (const auto& entry : entries) { for (const auto& entry : entries) {
Glib::ustring name;
Glib::ustring name, extension, profileName; Glib::ustring extension;
Glib::ustring profileName;
HaldCLUT::splitClutFilename (entry, name, extension, profileName); HaldCLUT::splitClutFilename (entry, name, extension, profileName);
extension = extension.casefold (); extension = extension.casefold();
if (extension.compare ("tif") != 0 && extension.compare ("png") != 0) { if (extension.compare("tif") != 0 && extension.compare("png") != 0) {
continue; continue;
} }
auto newRow = row ? *m_model->append (row.children ()) : *m_model->append (); auto newRow = row ? *m_model->append(row.children()) : *m_model->append();
newRow[m_columns.label] = name; newRow[m_columns.label] = name;
newRow[m_columns.clutFilename] = entry; newRow[m_columns.clutFilename] = entry;
++fileCount; ++fileCount;
if (!notifySlowParseDir (startedAt)) { if (!notifySlowParseDir(startedAt)) {
m_model->clear (); m_model->clear();
return 0; return 0;
} }
} }