Commit 668feb3e authored by Alexander Lohnau's avatar Alexander Lohnau 💬 Committed by Nate Graham
Browse files

Allow x and multiplication symbol instead of *

FEATURE: 350066
FIXED-IN: 5.20

This will allow us to run queries like:
4x4
4x4,2
4x,2

But if we explicitly want the x to be interpreted as a
variable by qalculate we can run:
=4x*4
parent 5a147825
......@@ -27,9 +27,9 @@
#include <QJSEngine>
#include <QGuiApplication>
#include <QClipboard>
#include <QRegularExpression>
#endif
#include <QRegularExpression>
#include <QIcon>
#include <QDebug>
......@@ -228,6 +228,8 @@ void CalculatorRunner::match(Plasma::RunnerContext &context)
bool toHex = cmd.startsWith(QLatin1String("hex="));
bool startsWithEquals = !toHex && cmd[0] == QLatin1Char('=');
userFriendlyMultiplication(cmd);
if (toHex || startsWithEquals) {
cmd.remove(0, cmd.indexOf(QLatin1Char('=')) + 1);
} else if (cmd.endsWith(QLatin1Char('='))) {
......@@ -349,4 +351,24 @@ QMimeData * CalculatorRunner::mimeDataForMatch(const Plasma::QueryMatch &match)
return result;
}
void CalculatorRunner::userFriendlyMultiplication(QString &cmd)
{
// convert multiplication sign to *
cmd.replace(QChar(U'\u00D7'), QChar('*'));
for (int i = 0; i < cmd.length(); ++i) {
if (i == 0 || i == cmd.length() - 1) {
continue;
}
const QChar prev = cmd.at(i - 1);
const QChar current = cmd.at(i);
const QChar next = cmd.at(i + 1);
if (current == QLatin1Char('x')) {
if (prev.isDigit() && (next.isDigit() || next == QLatin1Char(',') || next == QLatin1Char('.'))) {
cmd[i] = '*';
}
}
}
}
#include "calculatorrunner.moc"
......@@ -48,6 +48,7 @@ class CalculatorRunner : public Plasma::AbstractRunner
private:
QString calculate(const QString &term, bool *isApproximate);
void userFriendlyMultiplication(QString &cmd);
void userFriendlySubstitutions(QString &cmd);
void powSubstitutions(QString &cmd);
void hexSubstitutions(QString &cmd);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment