Phosphor
Qt6 / Wayland library suite for window-management tools
Loading...
Searching...
No Matches
CalculatorProvider.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3#pragma once
4
6#include <PhosphorShellLauncher/phosphorshelllauncher_export.h>
7
8#include <QList>
9#include <QString>
10#include <QStringView>
11
12#include <optional>
13
14namespace PhosphorShellLauncher {
15
16// Answers arithmetic typed into the launcher: "2*(3+4)" yields a single
17// row "14", and Enter copies it to the clipboard.
18//
19// A hand-written recursive-descent evaluator rather than a JS engine:
20// the query is user input, and evaluating it as a script would be a
21// footgun for the sake of saving a hundred lines. Supported: decimal and
22// exponent literals, + - * / % ^, parentheses, unary minus, and the sqrt() and abs() functions.
23//
24// A bare number yields nothing ("5" is not a calculation, and a row
25// reading "5" under every query that happens to be digits is noise); the
26// query has to contain an operator, parentheses or a function call.
27//
28// The row scores above any fuzzy match (FuzzyMatcher::perfectScore for
29// the query's length, plus one), since an exact answer to what the user
30// typed is by definition the best result for it.
31class PHOSPHORSHELLLAUNCHER_EXPORT CalculatorProvider : public PhosphorRegistry::ILauncherProvider
32{
33 Q_OBJECT
34
35public:
36 explicit CalculatorProvider(QObject* parent = nullptr);
38
39 [[nodiscard]] QString id() const override;
40 [[nodiscard]] QString displayName() const override;
41 [[nodiscard]] QString iconName() const override;
42
43 void setQuery(const QString& query) override;
44 [[nodiscard]] QList<PhosphorRegistry::LauncherResult> results() const override;
45 [[nodiscard]] bool activate(const QString& resultId, Activation activation) override;
46
47 // The evaluator, public and static so it is testable without a
48 // provider and reusable by anything else that wants safe arithmetic.
49 // Returns nullopt for anything that is not a complete, well-formed
50 // expression, for division by zero, and for a result that is not
51 // finite.
52 [[nodiscard]] static std::optional<double> evaluate(QStringView expression);
53
54 // Whether `expression` is worth answering at all: parses, and is not
55 // a bare literal. Public for the same reason as evaluate().
56 [[nodiscard]] static bool isCalculation(QStringView expression);
57
58 // The display form. An exact integer inside double's exact range prints
59 // in FULL and without a decimal point, however many digits that takes;
60 // everything else prints to at most 10 significant digits with trailing
61 // zeros trimmed. Truncating an exact integer would be a wrong answer
62 // rather than a rounded one.
63 //
64 // Always the C locale, even where the user's locale groups digits or
65 // uses a decimal comma. This string is both what the row shows and what
66 // activation copies, and a grouped "1 234,5" is not something that can be
67 // pasted into a shell or another calculator. Localising the display would
68 // mean carrying two strings; the pasteable one wins.
69 //
70 // The INPUT side is not symmetric: the parser accepts the reader's own
71 // decimal separator as well as '.', so typing what a French or German
72 // keyboard and locale produce is understood. Only the answer is
73 // normalised.
74 [[nodiscard]] static QString format(double value);
75
76private:
77 QList<PhosphorRegistry::LauncherResult> m_results;
78 // The formatted answer, kept for activate() to copy. The query itself
79 // is not: unlike the other providers, nothing here re-reads it after
80 // setQuery, because the answer already carries everything activation
81 // needs.
82 QString m_answer;
83};
84
85} // namespace PhosphorShellLauncher
Definition ILauncherProvider.h:106
Activation
Definition ILauncherProvider.h:112
CalculatorProvider(QObject *parent=nullptr)
QList< PhosphorRegistry::LauncherResult > results() const override
void setQuery(const QString &query) override
QString displayName() const override
static QString format(double value)
static std::optional< double > evaluate(QStringView expression)
bool activate(const QString &resultId, Activation activation) override
static bool isCalculation(QStringView expression)
Definition AppsProvider.h:19