From 050ddf7c3e4ea6aa7a18087d17aee2a43781d0af Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Thu, 4 Sep 2025 15:40:31 +1000 Subject: plugin: add qalculator For launcher >calc instead of qalc proc --- plugin/src/Caelestia/CMakeLists.txt | 7 +++++- plugin/src/Caelestia/qalculator.cpp | 49 +++++++++++++++++++++++++++++++++++++ plugin/src/Caelestia/qalculator.hpp | 15 ++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 plugin/src/Caelestia/qalculator.cpp create mode 100644 plugin/src/Caelestia/qalculator.hpp (limited to 'plugin/src') diff --git a/plugin/src/Caelestia/CMakeLists.txt b/plugin/src/Caelestia/CMakeLists.txt index d00247e..6461027 100644 --- a/plugin/src/Caelestia/CMakeLists.txt +++ b/plugin/src/Caelestia/CMakeLists.txt @@ -1,3 +1,6 @@ +find_package(PkgConfig REQUIRED) +pkg_check_modules(QALCULATE REQUIRED libqalculate) + qt_add_qml_module(caelestia URI Caelestia VERSION 0.1 @@ -5,6 +8,7 @@ qt_add_qml_module(caelestia cutils.hpp cutils.cpp cachingimagemanager.hpp cachingimagemanager.cpp filesystemmodel.hpp filesystemmodel.cpp + qalculator.hpp qalculator.cpp ) qt_query_qml_module(caelestia @@ -24,4 +28,5 @@ install(TARGETS "${module_plugin_target}" LIBRARY DESTINATION "${module_dir}" RU install(FILES "${module_qmldir}" DESTINATION "${module_dir}") install(FILES "${module_typeinfo}" DESTINATION "${module_dir}") -target_link_libraries(caelestia PRIVATE Qt::Core Qt::Qml Qt::Gui Qt::Concurrent) +target_include_directories(caelestia SYSTEM PRIVATE ${QALCULATE_INCLUDE_DIRS}) +target_link_libraries(caelestia PRIVATE Qt::Core Qt::Qml Qt::Gui Qt::Concurrent ${QALCULATE_LIBRARIES}) 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 +#include + +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); +} diff --git a/plugin/src/Caelestia/qalculator.hpp b/plugin/src/Caelestia/qalculator.hpp new file mode 100644 index 0000000..266699f --- /dev/null +++ b/plugin/src/Caelestia/qalculator.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +class Qalculator : public QObject { + Q_OBJECT + QML_ELEMENT + QML_SINGLETON + +public: + explicit Qalculator(QObject* parent = nullptr); + + Q_INVOKABLE QString eval(const QString& expr, bool printExpr = true) const; +}; -- cgit v1.2.3-freya