diff options
| author | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-09-04 15:40:31 +1000 |
|---|---|---|
| committer | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-09-04 15:40:31 +1000 |
| commit | 050ddf7c3e4ea6aa7a18087d17aee2a43781d0af (patch) | |
| tree | 58a93f727211aa79604891d6536b5e01b56ef795 /plugin/src/Caelestia/qalculator.cpp | |
| parent | plugin/cim: use QtConcurrent (diff) | |
| download | caelestia-shell-050ddf7c3e4ea6aa7a18087d17aee2a43781d0af.tar.gz caelestia-shell-050ddf7c3e4ea6aa7a18087d17aee2a43781d0af.tar.bz2 caelestia-shell-050ddf7c3e4ea6aa7a18087d17aee2a43781d0af.zip | |
plugin: add qalculator
For launcher >calc instead of qalc proc
Diffstat (limited to '')
| -rw-r--r-- | plugin/src/Caelestia/qalculator.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/plugin/src/Caelestia/qalculator.cpp b/plugin/src/Caelestia/qalculator.cpp new file mode 100644 index 0000000..2e18033 --- /dev/null +++ b/plugin/src/Caelestia/qalculator.cpp @@ -0,0 +1,49 @@ +#include "qalculator.hpp" + +#include <QObject> +#include <libqalculate/qalculate.h> + +Qalculator::Qalculator(QObject* parent) + : QObject(parent) { + if (!CALCULATOR) { + new Calculator(); + CALCULATOR->loadExchangeRates(); + CALCULATOR->loadGlobalDefinitions(); + CALCULATOR->loadLocalDefinitions(); + } +} + +QString Qalculator::eval(const QString& expr, bool printExpr) const { + if (expr.isEmpty()) { + return QString(); + } + + EvaluationOptions eo; + PrintOptions po; + + std::string parsed; + std::string result = CALCULATOR->calculateAndPrint( + CALCULATOR->unlocalizeExpression(expr.toStdString(), eo.parse_options), 100, eo, po, &parsed); + + std::string error; + while (CALCULATOR->message()) { + if (!CALCULATOR->message()->message().empty()) { + if (CALCULATOR->message()->type() == MESSAGE_ERROR) { + error += "error: "; + } else if (CALCULATOR->message()->type() == MESSAGE_WARNING) { + error += "warning: "; + } + error += CALCULATOR->message()->message(); + } + CALCULATOR->nextMessage(); + } + if (!error.empty()) { + return QString::fromStdString(error); + } + + if (printExpr) { + return QString("%1 = %2").arg(parsed).arg(result); + } + + return QString::fromStdString(result); +} |