aboutsummaryrefslogtreecommitdiff
path: root/qt/adapter.cpp
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-04-17 16:27:38 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2025-04-17 16:27:38 +0200
commitf91e5c4c83a071ab720417d490fd44a8f4678bc1 (patch)
tree9d38fad279c015bea0bf256184b6936171310aa7 /qt/adapter.cpp
parentc182f3f16e56e9533060ffa06b25391e239a28ee (diff)
downloadnissy-core-f91e5c4c83a071ab720417d490fd44a8f4678bc1.tar.gz
nissy-core-f91e5c4c83a071ab720417d490fd44a8f4678bc1.zip
Improvements to QT UI
Diffstat (limited to 'qt/adapter.cpp')
-rw-r--r--qt/adapter.cpp33
1 files changed, 27 insertions, 6 deletions
diff --git a/qt/adapter.cpp b/qt/adapter.cpp
index 3711fa4..cb690a4 100644
--- a/qt/adapter.cpp
+++ b/qt/adapter.cpp
@@ -9,20 +9,25 @@
9 9
10void logWrapper(const char *str, void *data) 10void logWrapper(const char *str, void *data)
11{ 11{
12 auto f = *reinterpret_cast<std::function<void(const char*)>*>(data); 12 auto f = *reinterpret_cast<std::function<void(std::string)>*>(data);
13 f(str); 13 f(std::string{str});
14} 14}
15 15
16NissyAdapter::NissyAdapter() 16NissyAdapter::NissyAdapter()
17{ 17{
18 // TODO: this list must be kept in sync with UI code, it is a bit ugly
18 std::vector<std::string> solverNames { 19 std::vector<std::string> solverNames {
19 "h48h3k2" 20 "h48h0k4",
21 "h48h1k2",
22 "h48h2k2",
23 "h48h3k2",
24 "h48h7k2",
20 }; 25 };
21 26
22 for (auto s : solverNames) 27 for (auto s : solverNames)
23 initSolver(s); 28 initSolver(s);
24 29
25 writeLog = [&](const char *str) { 30 writeLog = [&](std::string str) {
26 emit appendLog(QString::fromStdString(str)); 31 emit appendLog(QString::fromStdString(str));
27 }; 32 };
28 33
@@ -45,6 +50,8 @@ bool NissyAdapter::loadSolverData(nissy::solver& solver) {
45 50
46 std::filesystem::path filePath("./tables/" + solver.id); 51 std::filesystem::path filePath("./tables/" + solver.id);
47 if (!std::filesystem::exists(filePath)) { 52 if (!std::filesystem::exists(filePath)) {
53 logLine("Data file for solver " + solver.name + " not found, "
54 "generating it...");
48 auto err = solver.generate_data(); 55 auto err = solver.generate_data();
49 if (!err.ok()) { 56 if (!err.ok()) {
50 emit solverError(QString("Error generating data!")); 57 emit solverError(QString("Error generating data!"));
@@ -55,16 +62,23 @@ bool NissyAdapter::loadSolverData(nissy::solver& solver) {
55 ofs.write(reinterpret_cast<char *>(solver.data.data()), 62 ofs.write(reinterpret_cast<char *>(solver.data.data()),
56 solver.size); 63 solver.size);
57 ofs.close(); 64 ofs.close();
65 logLine("Data generated succesfully");
58 } else { 66 } else {
67 logLine("Reading data for solver " + solver.name +
68 " from file");
59 std::ifstream ifs(filePath, std::ios::binary); 69 std::ifstream ifs(filePath, std::ios::binary);
60 solver.read_data(ifs); 70 solver.read_data(ifs);
61 ifs.close(); 71 ifs.close();
72 logLine("Data loaded");
62 } 73 }
63 74
75 logLine("Checking data integrity "
76 "(this is done only once per solver per session)...");
64 if (!solver.check_data().ok()) { 77 if (!solver.check_data().ok()) {
65 emit solverError(QString("Error reading data!")); 78 emit solverError(QString("Error reading data!"));
66 return false; 79 return false;
67 } 80 }
81 logLine("Data checked");
68 82
69 return true; 83 return true;
70} 84}
@@ -102,7 +116,7 @@ Q_INVOKABLE void NissyAdapter::requestSolve(
102 } 116 }
103 117
104 SolveOptions opts{c, ss, (unsigned)minmoves, (unsigned)maxmoves, 118 SolveOptions opts{c, ss, (unsigned)minmoves, (unsigned)maxmoves,
105 (unsigned)maxsolutions, optimal}; 119 (unsigned)maxsolutions, (unsigned)optimal};
106 auto _ = QtConcurrent::run(&NissyAdapter::startSolve, this, opts); 120 auto _ = QtConcurrent::run(&NissyAdapter::startSolve, this, opts);
107 return; 121 return;
108} 122}
@@ -132,9 +146,16 @@ void NissyAdapter::startSolve(SolveOptions opts)
132 std::stringstream ss; 146 std::stringstream ss;
133 for (auto s : sols) { 147 for (auto s : sols) {
134 auto n = nissy::count_moves(s).value; 148 auto n = nissy::count_moves(s).value;
135 ss << s << "(" << n << ")" << std::endl; // TODO: remove last newline 149 ss << s << " (" << n << ")" << std::endl; // TODO: remove last newline
136 } 150 }
137 emit solutionsReady(QString::fromStdString(hs.str()), 151 emit solutionsReady(QString::fromStdString(hs.str()),
138 QString::fromStdString(ss.str())); 152 QString::fromStdString(ss.str()));
139 } 153 }
140} 154}
155
156void NissyAdapter::logLine(std::string str)
157{
158 std::stringstream ss;
159 ss << str << std::endl;
160 writeLog(ss.str());
161}

Generated with cgit - Back to sebastiano.tronto.net