From d6663fe7b71db340b3c7a1d069c473f725caa3a8 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Mon, 29 Aug 2022 12:19:25 -0400 Subject: Trying to support the superior operating system. Also reformat a bit of legacy code. Smells like mold. --- mingui/main.cpp | 32 +++++++++++++++++--------------- mingui/mingui.cpp | 47 +++++++++++++++++++++++++++++++---------------- mingui/mingui.hpp | 13 ++++++++----- 3 files changed, 56 insertions(+), 36 deletions(-) (limited to 'mingui') diff --git a/mingui/main.cpp b/mingui/main.cpp index 8274b6f..6856cad 100644 --- a/mingui/main.cpp +++ b/mingui/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -11,9 +12,10 @@ #include "mingui.hpp" using std::size_t; +namespace fs = std::filesystem; -std::unordered_map p; -std::vector fns; +std::unordered_map fnmap; +std::vector fns; std::map, double> dist; std::vector par; std::vector> lists; @@ -42,25 +44,25 @@ void load_result(const char* rp) { int l; double d; - std::string s1, s2; + fs::path::string_type s1, s2; if (feof(f)) break; fread(&l, sizeof(int), 1, f); s1.resize(l); - fread(s1.data(), 1, l, f); - p.try_emplace(s1, p.size() + 1); + fread(s1.data(), sizeof(fs::path::value_type), l, f); + fnmap.try_emplace(s1, fnmap.size() + 1); fread(&l, sizeof(int), 1, f); s2.resize(l); - fread(s2.data(), 1, l, f); - p.try_emplace(s2, p.size() + 1); + fread(s2.data(), sizeof(fs::path::value_type), l, f); + fnmap.try_emplace(s2, fnmap.size() + 1); fread(&d, sizeof(double), 1, f); - dist[std::make_pair(p[s1], p[s2])] = d; + dist[std::make_pair(fnmap[s1], fnmap[s2])] = d; } fclose(f); } -std::vector build_list(const std::vector &l) +std::vector build_list(const std::vector &l) { - std::vector ret; + std::vector ret; for (auto &x : l) ret.push_back(fns[x]); return ret; @@ -88,12 +90,12 @@ int main(int argc, char **argv) if (argc < 2) return 1; load_result(argv[1]); - printf("%lu known files\n", p.size()); + printf("%lu known files\n", fnmap.size()); - par.resize(p.size() + 1); - fns.resize(p.size() + 1); - lists.resize(p.size() + 1); - for (auto &kp : p) + par.resize(fnmap.size() + 1); + fns.resize(fnmap.size() + 1); + lists.resize(fnmap.size() + 1); + for (auto &kp : fnmap) fns[kp.second] = kp.first; for (size_t i = 1; i < par.size(); ++i) diff --git a/mingui/mingui.cpp b/mingui/mingui.cpp index b421512..58a6ca5 100644 --- a/mingui/mingui.cpp +++ b/mingui/mingui.cpp @@ -1,7 +1,8 @@ #include "mingui.hpp" #include -#include +#include +#include #include #include @@ -28,6 +29,15 @@ const std::vector keys = { Qt::Key::Key_U, Qt::Key::Key_I, Qt::Key::Key_O, Qt::Key::Key_P }; +QString fsstr_to_qstring(const fs::path::string_type &s) +{ +#ifdef _WIN32 //the degenerate platform + return QString::fromStdWString(s); +#else + return QString::fromStdString(s); +#endif +} + MinGuiWidget::MinGuiWidget() { this->setFont(QFontDatabase::systemFont(QFontDatabase::SystemFont::FixedFont)); @@ -52,7 +62,7 @@ MinGuiWidget::MinGuiWidget() infopanel->setSizePolicy(QSizePolicy::Policy::Minimum, QSizePolicy::Policy::Minimum); } -void MinGuiWidget::show_images(const std::vector &fns) +void MinGuiWidget::show_images(const std::vector &fns) { current_set = fns; marks.clear(); @@ -61,14 +71,14 @@ void MinGuiWidget::show_images(const std::vector &fns) imgcontainer->setLayout(new QVBoxLayout(imgcontainer)); int max_height = (this->screen()->size().height() / fns.size() * 0.75 - 24) * this->screen()->devicePixelRatio(); int max_width = this->screen()->size().width() * 0.8 * this->screen()->devicePixelRatio(); - std::string common_pfx = common_prefix(fns); + fs::path::string_type common_pfx = common_prefix(fns); size_t idx = 0; if (fns.size() > keys.size()) QMessageBox::warning(this, "Too many duplicates", "Too many duplicates found. Some couldn't be assigned a hotkey."); for (auto &f : fns) { marks.push_back(marked.find(f) != marked.end()); - ImageWidget *tw = new ImageWidget(f, f.substr(common_pfx.length()), idx, max_width, max_height, this); + ImageWidget *tw = new ImageWidget(f, f.native().substr(common_pfx.length()), idx, max_width, max_height, this); QObject::connect(tw, &ImageWidget::clicked, [this, idx] { this->mark_toggle(idx); }); imgw.push_back(tw); imgcontainer->layout()->addWidget(tw); @@ -103,7 +113,11 @@ void MinGuiWidget::save_list() QString fn = QFileDialog::getSaveFileName(this, "Save list", QString(), "*.txt"); FILE *f = fopen(fn.toStdString().c_str(), "w"); for (auto &x : this->marked) +#ifdef _WIN32 + fwprintf(f, L"%ls\n", x.c_str()); +#else fprintf(f, "%s\n", x.c_str()); +#endif fclose(f); } @@ -180,23 +194,24 @@ void MinGuiWidget::mark_view_update(bool update_msg) sb->showMessage(QString("%1 of %2 marked for deletion").arg(m).arg(current_set.size()), 1000); } -std::string MinGuiWidget::common_prefix(const std::vector &fns) +fs::path::string_type MinGuiWidget::common_prefix(const std::vector &fns) { - std::string ret; - std::string shortest = *std::min_element(fns.begin(), fns.end(), [](auto &a, auto &b){return a.length() < b.length();}); + using fsstr = fs::path::string_type; + fsstr ret; + fsstr shortest = *std::min_element(fns.begin(), fns.end(), [](auto &a, auto &b){return a.native().length() < b.native().length();}); for (size_t i = 0; i < shortest.length(); ++i) { - char c = shortest[i]; + fs::path::value_type c = shortest[i]; bool t = true; - for (auto &s : fns) if (s[i] != c) {t = false; break;} + for (auto &s : fns) if (s.c_str()[i] != c) {t = false; break;} if (!t) break; ret.push_back(c); } if (!ret.empty()) { - auto p = ret.rfind((char)std::filesystem::path::preferred_separator); - if (p != std::string::npos) - return ret.substr(0, p + 1); + auto p = ret.rfind(std::filesystem::path::preferred_separator); + if (p != fsstr::npos) + return fs::path(ret.substr(0, p + 1)); } return ret; } @@ -227,15 +242,15 @@ void MinGuiWidget::keyReleaseEvent(QKeyEvent *e) } } -ImageWidget::ImageWidget(std::string f, std::string dispf, size_t _idx, int max_width, int max_height, QWidget *par) +ImageWidget::ImageWidget(fs::path f, fs::path::string_type dispf, size_t _idx, int max_width, int max_height, QWidget *par) : QWidget(par), fn(QString::fromStdString(f)), idx(_idx) { this->setLayout(new QVBoxLayout(this)); this->layout()->setMargin(10); im = new QLabel(this); this->layout()->addWidget(im); - QFile imgf(QString::fromStdString(f)); - QPixmap pm(QString::fromStdString(f)); + QFile imgf(fsstr_to_qstring(f.native())); + QPixmap pm(fsstr_to_qstring(f.native())); int imw = pm.width(); int imh = pm.height(); pm.setDevicePixelRatio(this->screen()->devicePixelRatio()); @@ -247,7 +262,7 @@ ImageWidget::ImageWidget(std::string f, std::string dispf, size_t _idx, int max_ this->layout()->addWidget(lb); QString s = QString("<%1>: %2, %3 x %4, %5") .arg(idx < keys.size() ? QKeySequence(keys[idx]).toString(): QString("(No hotkey available)")) - .arg(QString::fromStdString(dispf/*f.substr(common_pfx.length())*/)) + .arg(fsstr_to_qstring(dispf)) .arg(imw).arg(imh) .arg(QLocale::system().formattedDataSize(imgf.size(), 3)); lb->setTextFormat(Qt::TextFormat::PlainText); diff --git a/mingui/mingui.hpp b/mingui/mingui.hpp index 7ed0eb1..3a5dddc 100644 --- a/mingui/mingui.hpp +++ b/mingui/mingui.hpp @@ -1,6 +1,7 @@ #ifndef MINGUI_HPP #define MINGUI_HPP +#include #include #include #include @@ -11,6 +12,8 @@ class QHBoxLayout; class QLabel; class QStatusBar; +namespace fs = std::filesystem; + class MinGuiWidget : public QWidget { Q_OBJECT @@ -25,17 +28,17 @@ private: void mark_all(); void mark_none(); void mark_view_update(bool update_msg = true); - std::string common_prefix(const std::vector &fns); + fs::path::string_type common_prefix(const std::vector &fns); std::vector imgw; std::vector marks; - std::unordered_set marked; - std::vector current_set; + std::unordered_set marked; + std::vector current_set; protected: void keyPressEvent(QKeyEvent *e) override; void keyReleaseEvent(QKeyEvent *e) override; public: MinGuiWidget(); - void show_images(const std::vector &fns); + void show_images(const std::vector &fns); void update_distances(const std::map, double> &d); void update_permamsg(std::size_t cur, std::size_t size); void save_list(); @@ -55,7 +58,7 @@ private: protected: void mouseReleaseEvent(QMouseEvent *event) override; public: - ImageWidget(std::string f, std::string dispfn, std::size_t _idx, int max_width, int max_height, QWidget *par); + ImageWidget(fs::path f, fs::path::string_type dispfn, std::size_t _idx, int max_width, int max_height, QWidget *par); void set_marked(bool marked); Q_SIGNALS: void clicked(); -- cgit v1.2.3