diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-04-16 09:56:55 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-04-16 09:56:55 +0200 |
| commit | 9589072687726dd51776b0e952625ae1996f4721 (patch) | |
| tree | 2dbc63de0b3e5805b5dca2919d05b4356c1194a4 /qt/adapter.cpp | |
| parent | dc5e8188a7e29909cbdf661e9b2a80700fe0491d (diff) | |
| download | nissy-core-9589072687726dd51776b0e952625ae1996f4721.tar.gz nissy-core-9589072687726dd51776b0e952625ae1996f4721.zip | |
Added rudimentary QT UI
Diffstat (limited to 'qt/adapter.cpp')
| -rw-r--r-- | qt/adapter.cpp | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/qt/adapter.cpp b/qt/adapter.cpp new file mode 100644 index 0000000..3711fa4 --- /dev/null +++ b/qt/adapter.cpp | |||
| @@ -0,0 +1,140 @@ | |||
| 1 | #include "adapter.h" | ||
| 2 | |||
| 3 | #include <fstream> | ||
| 4 | #include <sstream> | ||
| 5 | #include <string> | ||
| 6 | #include <vector> | ||
| 7 | #include <QDebug> | ||
| 8 | #include <QtConcurrent/QtConcurrent> | ||
| 9 | |||
| 10 | void logWrapper(const char *str, void *data) | ||
| 11 | { | ||
| 12 | auto f = *reinterpret_cast<std::function<void(const char*)>*>(data); | ||
| 13 | f(str); | ||
| 14 | } | ||
| 15 | |||
| 16 | NissyAdapter::NissyAdapter() | ||
| 17 | { | ||
| 18 | std::vector<std::string> solverNames { | ||
| 19 | "h48h3k2" | ||
| 20 | }; | ||
| 21 | |||
| 22 | for (auto s : solverNames) | ||
| 23 | initSolver(s); | ||
| 24 | |||
| 25 | writeLog = [&](const char *str) { | ||
| 26 | emit appendLog(QString::fromStdString(str)); | ||
| 27 | }; | ||
| 28 | |||
| 29 | nissy::set_logger(&logWrapper, &writeLog); | ||
| 30 | } | ||
| 31 | |||
| 32 | void NissyAdapter::initSolver(const std::string& s) { | ||
| 33 | auto se = nissy::solver::get(s); | ||
| 34 | if (std::holds_alternative<nissy::error>(se)) { | ||
| 35 | qDebug("Error loading solver!"); | ||
| 36 | return; | ||
| 37 | } | ||
| 38 | auto ss = std::get<nissy::solver>(se); | ||
| 39 | solvers.push_back(ss); | ||
| 40 | } | ||
| 41 | |||
| 42 | bool NissyAdapter::loadSolverData(nissy::solver& solver) { | ||
| 43 | if (solver.data_checked) | ||
| 44 | return true; | ||
| 45 | |||
| 46 | std::filesystem::path filePath("./tables/" + solver.id); | ||
| 47 | if (!std::filesystem::exists(filePath)) { | ||
| 48 | auto err = solver.generate_data(); | ||
| 49 | if (!err.ok()) { | ||
| 50 | emit solverError(QString("Error generating data!")); | ||
| 51 | return false; | ||
| 52 | } | ||
| 53 | std::filesystem::create_directory("./tables/"); | ||
| 54 | std::ofstream ofs(filePath, std::ios::binary); | ||
| 55 | ofs.write(reinterpret_cast<char *>(solver.data.data()), | ||
| 56 | solver.size); | ||
| 57 | ofs.close(); | ||
| 58 | } else { | ||
| 59 | std::ifstream ifs(filePath, std::ios::binary); | ||
| 60 | solver.read_data(ifs); | ||
| 61 | ifs.close(); | ||
| 62 | } | ||
| 63 | |||
| 64 | if (!solver.check_data().ok()) { | ||
| 65 | emit solverError(QString("Error reading data!")); | ||
| 66 | return false; | ||
| 67 | } | ||
| 68 | |||
| 69 | return true; | ||
| 70 | } | ||
| 71 | |||
| 72 | Q_INVOKABLE bool NissyAdapter::isValidScramble(QString qscr) | ||
| 73 | { | ||
| 74 | nissy::cube c; | ||
| 75 | return c.move(qscr.toStdString()).ok(); | ||
| 76 | } | ||
| 77 | |||
| 78 | Q_INVOKABLE void NissyAdapter::requestSolve( | ||
| 79 | QString scramble, | ||
| 80 | QString solver, | ||
| 81 | int minmoves, | ||
| 82 | int maxmoves, | ||
| 83 | int maxsolutions, | ||
| 84 | int optimal | ||
| 85 | ) | ||
| 86 | { | ||
| 87 | nissy::cube c; | ||
| 88 | if (!c.move(scramble.toStdString()).ok()) { | ||
| 89 | emit solverError(QString("Unexpected error: invalid scramble")); | ||
| 90 | return; | ||
| 91 | } | ||
| 92 | |||
| 93 | nissy::solver *ss = nullptr; | ||
| 94 | for (auto& s : solvers) | ||
| 95 | if (s.name == solver) | ||
| 96 | ss = &s; | ||
| 97 | if (ss == nullptr) { | ||
| 98 | std::string msg = "Error: solver '" + solver.toStdString() | ||
| 99 | + "' not available"; | ||
| 100 | emit solverError(QString::fromStdString(msg)); | ||
| 101 | return; | ||
| 102 | } | ||
| 103 | |||
| 104 | SolveOptions opts{c, ss, (unsigned)minmoves, (unsigned)maxmoves, | ||
| 105 | (unsigned)maxsolutions, optimal}; | ||
| 106 | auto _ = QtConcurrent::run(&NissyAdapter::startSolve, this, opts); | ||
| 107 | return; | ||
| 108 | } | ||
| 109 | |||
| 110 | void NissyAdapter::startSolve(SolveOptions opts) | ||
| 111 | { | ||
| 112 | loadSolverData(*opts.solver); | ||
| 113 | |||
| 114 | auto result = opts.solver->solve(opts.cube, nissy::nissflag::NORMAL, | ||
| 115 | opts.minmoves, opts.maxmoves, opts.maxsolutions, opts.optimal, 8); | ||
| 116 | |||
| 117 | if (!result.err.ok()) { | ||
| 118 | std::string msg = "Error computing solutions: " + | ||
| 119 | std::to_string(result.err.value); | ||
| 120 | emit solverError(QString::fromStdString(msg)); | ||
| 121 | return; | ||
| 122 | } | ||
| 123 | |||
| 124 | auto& sols = result.solutions; | ||
| 125 | if (sols.size() == 0) { | ||
| 126 | emit solutionsReady("No solution found", ""); | ||
| 127 | } else { | ||
| 128 | std::stringstream hs; | ||
| 129 | hs << "Found " << sols.size() << " solution" | ||
| 130 | << (sols.size() > 1 ? "s:" : ":"); | ||
| 131 | |||
| 132 | std::stringstream ss; | ||
| 133 | for (auto s : sols) { | ||
| 134 | auto n = nissy::count_moves(s).value; | ||
| 135 | ss << s << "(" << n << ")" << std::endl; // TODO: remove last newline | ||
| 136 | } | ||
| 137 | emit solutionsReady(QString::fromStdString(hs.str()), | ||
| 138 | QString::fromStdString(ss.str())); | ||
| 139 | } | ||
| 140 | } | ||
