calculatorrunner.cpp 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 *   Copyright (C) 2007 Barış Metin <baris@pardus.org.tr>
 *   Copyright (C) 2006 David Faure <faure@kde.org>
 *   Copyright (C) 2007 Richard Moore <rich@kde.org>
 *   Copyright (C) 2010 Matteo Agostinelli <agostinelli@gmail.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License version 2 as
 *   published by the Free Software Foundation
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "calculatorrunner.h"

#ifdef ENABLE_QALCULATE
#include "qalculate_engine.h"
#else
27
#include <QJSEngine>
28 29
#include <QGuiApplication>
#include <QClipboard>
30 31
#endif

32
#include <QIcon>
33 34
#include <QDebug>

35
#include <KLocalizedString>
36 37
#include <krunner/querymatch.h>

38 39
static const QString s_copyToClipboardId = QStringLiteral("copyToClipboard");

40
K_EXPORT_PLASMA_RUNNER(calculatorrunner, CalculatorRunner)
41 42 43 44 45 46 47 48 49 50 51

CalculatorRunner::CalculatorRunner( QObject* parent, const QVariantList &args )
    : Plasma::AbstractRunner(parent, args)
{
    Q_UNUSED(args)

    #ifdef ENABLE_QALCULATE
    m_engine = new QalculateEngine;
    setSpeed(SlowSpeed);
    #endif

52
    setObjectName( QStringLiteral("Calculator" ));
53 54 55 56 57 58
    setIgnoredTypes(Plasma::RunnerContext::Directory | Plasma::RunnerContext::File |
                         Plasma::RunnerContext::NetworkLocation | Plasma::RunnerContext::Executable |
                         Plasma::RunnerContext::ShellCommand);

    QString description = i18n("Calculates the value of :q: when :q: is made up of numbers and "
                               "mathematical symbols such as +, -, /, * and ^.");
59 60 61
    addSyntax(Plasma::RunnerSyntax(QStringLiteral(":q:"), description));
    addSyntax(Plasma::RunnerSyntax(QStringLiteral("=:q:"), description));
    addSyntax(Plasma::RunnerSyntax(QStringLiteral(":q:="), description));
62 63

    addAction(s_copyToClipboardId, QIcon::fromTheme(QStringLiteral("edit-copy")), i18n("Copy to Clipboard"));
64 65 66 67 68 69 70 71 72 73 74
}

CalculatorRunner::~CalculatorRunner()
{
    #ifdef ENABLE_QALCULATE
    delete m_engine;
    #endif
}

void CalculatorRunner::powSubstitutions(QString& cmd)
{
75 76
    if (cmd.contains(QStringLiteral("e+"), Qt::CaseInsensitive)) {
        cmd = cmd.replace(QLatin1String("e+"), QLatin1String("*10^"), Qt::CaseInsensitive);
77 78
    }

79 80
    if (cmd.contains(QStringLiteral("e-"), Qt::CaseInsensitive)) {
        cmd = cmd.replace(QLatin1String("e-"), QLatin1String("*10^-"), Qt::CaseInsensitive);
81 82 83 84 85 86 87 88 89 90 91
    }

    // the below code is scary mainly because we have to honor priority
    // honor decimal numbers and parenthesis.
    while (cmd.contains('^')) {
        int where = cmd.indexOf('^');
        cmd = cmd.replace(where, 1, ',');
        int preIndex = where - 1;
        int postIndex = where + 1;
        int count = 0;

92
        QChar decimalSymbol = QLocale().decimalPoint();
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        //avoid out of range on weird commands
        preIndex = qMax(0, preIndex);
        postIndex = qMin(postIndex, cmd.length()-1);

        //go backwards looking for the beginning of the number or expression
        while (preIndex != 0) {
            QChar current = cmd.at(preIndex);
            QChar next = cmd.at(preIndex-1);
            //qDebug() << "index " << preIndex << " char " << current;
            if (current == ')') {
                count++;
            } else if (current == '(') {
                count--;
            } else {
                if (((next <= '9' ) && (next >= '0')) || next == decimalSymbol) {
                    preIndex--;
                    continue;
                }
            }
            if (count == 0) {
                //check for functions
                if (!((next <= 'z' ) && (next >= 'a'))) {
                    break;
                }
            }
            preIndex--;
        }

       //go forwards looking for the end of the number or expression
        count = 0;
        while (postIndex != cmd.size() - 1) {
            QChar current=cmd.at(postIndex);
            QChar next=cmd.at(postIndex + 1);

            //check for functions
            if ((count == 0) && (current <= 'z') && (current >= 'a')) {
                postIndex++;
                continue;
            }

            if (current == '(') {
                count++;
            } else if (current == ')') {
                count--;
            } else {
                if (((next <= '9' ) && (next >= '0')) || next == decimalSymbol) {
                    postIndex++;
                    continue;
                 }
            }
            if (count == 0) {
                break;
            }
            postIndex++;
        }

        preIndex = qMax(0, preIndex);
        postIndex = qMin(postIndex, cmd.length());

152
        cmd.insert(preIndex,QLatin1String("pow("));
153 154 155 156 157 158 159 160
        // +1 +4 == next position to the last number after we add 4 new characters pow(
        cmd.insert(postIndex + 1 + 4, ')');
        //qDebug() << "from" << preIndex << " to " << postIndex << " got: " << cmd;
    }
}

void CalculatorRunner::hexSubstitutions(QString& cmd)
{
161
    if (cmd.contains(QStringLiteral("0x"))) {
162 163 164 165 166 167
        //Append +0 so that the calculator can serve also as a hex converter
        cmd.append("+0");
        bool ok;
        int pos = 0;
        QString hex;

168
        while (cmd.contains(QStringLiteral("0x"))) {
169
            hex.clear();
170
            pos = cmd.indexOf(QStringLiteral("0x"), pos);
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

            for (int q = 0; q < cmd.size(); q++) {//find end of hex number
                QChar current = cmd[pos+q+2];
                if (((current <= '9' ) && (current >= '0')) || ((current <= 'F' ) && (current >= 'A')) || ((current <= 'f' ) && (current >= 'a'))) { //Check if valid hex sign
                    hex[q] = current;
                } else {
                    break;
                }
            }
            cmd = cmd.replace(pos, 2+hex.length(), QString::number(hex.toInt(&ok,16))); //replace hex with decimal
        }
    }
}

void CalculatorRunner::userFriendlySubstitutions(QString& cmd)
{
187 188
    if (cmd.contains(QLocale().decimalPoint(), Qt::CaseInsensitive)) {
         cmd=cmd.replace(QLocale().decimalPoint(), QChar('.'), Qt::CaseInsensitive);
189 190 191 192 193 194 195
    }

    // the following substitutions are not needed with libqalculate
    #ifndef ENABLE_QALCULATE
    hexSubstitutions(cmd);
    powSubstitutions(cmd);

196 197
    if (cmd.contains(QRegExp(QStringLiteral("\\d+and\\d+")))) {
         cmd = cmd.replace(QRegExp(QStringLiteral("(\\d+)and(\\d+)")), QStringLiteral("\\1&\\2"));
198
    }
199 200
    if (cmd.contains(QRegExp(QStringLiteral("\\d+or\\d+")))) {
         cmd = cmd.replace(QRegExp(QStringLiteral("(\\d+)or(\\d+)")), QStringLiteral("\\1|\\2"));
201
    }
202 203
    if (cmd.contains(QRegExp(QStringLiteral("\\d+xor\\d+")))) {
         cmd = cmd.replace(QRegExp(QStringLiteral("(\\d+)xor(\\d+)")), QStringLiteral("\\1^\\2"));
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    }
    #endif
}


void CalculatorRunner::match(Plasma::RunnerContext &context)
{
    const QString term = context.query();
    QString cmd = term;

    //no meanless space between friendly guys: helps simplify code
    cmd = cmd.trimmed().remove(' ');

    if (cmd.length() < 3) {
        return;
    }

221
    if (cmd.toLower() == QLatin1String("universe") || cmd.toLower() == QLatin1String("life")) {
222 223
        Plasma::QueryMatch match(this);
        match.setType(Plasma::QueryMatch::InformationalMatch);
224
        match.setIconName(QStringLiteral("accessories-calculator"));
225
        match.setText(QStringLiteral("42"));
226 227
        match.setData("42");
        match.setId(term);
228
        context.addMatch(match);
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
        return;
    }

    bool toHex = cmd.startsWith(QLatin1String("hex="));
    bool startsWithEquals = !toHex && cmd[0] == '=';

    if (toHex || startsWithEquals) {
        cmd.remove(0, cmd.indexOf('=') + 1);
    } else if (cmd.endsWith('=')) {
        cmd.chop(1);
    } else {
        bool foundDigit = false;
        for (int i = 0; i < cmd.length(); ++i) {
            QChar c = cmd.at(i);
            if (c.isLetter()) {
                // not just numbers and symbols, so we return
                return;
            }
            if (c.isDigit()) {
                foundDigit = true;
            }
        }
        if (!foundDigit) {
            return;
        }
    }

    if (cmd.isEmpty()) {
        return;
    }

    userFriendlySubstitutions(cmd);
    #ifndef ENABLE_QALCULATE
262
    cmd.replace(QRegExp(QStringLiteral("([a-zA-Z]+)")), QStringLiteral("Math.\\1")); //needed for accessing math funktions like sin(),....
263 264
    #endif

265 266
    bool isApproximate = false;
    QString result = calculate(cmd, &isApproximate);
267 268 269 270 271 272 273
    if (!result.isEmpty() && result != cmd) {
        if (toHex) {
            result = "0x" + QString::number(result.toInt(), 16).toUpper();
        }

        Plasma::QueryMatch match(this);
        match.setType(Plasma::QueryMatch::InformationalMatch);
274
        match.setIconName(QStringLiteral("accessories-calculator"));
275
        match.setText(result);
276 277 278
        if (isApproximate) {
            match.setSubtext(i18nc("The result of the calculation is only an approximation", "Approximation"));
        }
279 280
        match.setData(result);
        match.setId(term);
281
        context.addMatch(match);
282 283 284
    }
}

285
QString CalculatorRunner::calculate(const QString& term, bool *isApproximate)
286 287 288 289 290
{
    #ifdef ENABLE_QALCULATE
    QString result;

    try {
291
        result = m_engine->evaluate(term, isApproximate);
292 293 294 295
    } catch(std::exception& e) {
        qDebug() << "qalculate error: " << e.what();
    }

296
    return result.replace('.', QLocale().decimalPoint(), Qt::CaseInsensitive);
297
    #else
298
    Q_UNUSED(isApproximate);
299
    //qDebug() << "calculating" << term;
300 301
    QJSEngine eng;
    QJSValue result = eng.evaluate(QStringLiteral("var result = %1; result").arg(term));
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

    if (result.isError()) {
        return QString();
    }

    const QString resultString = result.toString();
    if (resultString.isEmpty()) {
        return QString();
    }

    if (!resultString.contains('.')) {
        return resultString;
    }

    //ECMAScript has issues with the last digit in simple rational computations
    //This script rounds off the last digit; see bug 167986
318
    QString roundedResultString = eng.evaluate(QStringLiteral("var exponent = 14-(1+Math.floor(Math.log(Math.abs(result))/Math.log(10)));\
319
                                                var order=Math.pow(10,exponent);\
320
                                                (order > 0? Math.round(result*order)/order : 0)")).toString();
321

322
    roundedResultString.replace('.', QLocale().decimalPoint(), Qt::CaseInsensitive);
323 324 325 326 327

    return roundedResultString;
    #endif
}

328 329 330
void CalculatorRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
{
    Q_UNUSED(context);
331
    if (match.selectedAction() == action(s_copyToClipboardId)) {
332
#ifdef ENABLE_QALCULATE
333
        m_engine->copyToClipboard();
334
#else
335
        QGuiApplication::clipboard()->setText(match.text());
336
#endif
337
    }
338 339 340 341 342 343
}

QList<QAction *> CalculatorRunner::actionsForMatch(const Plasma::QueryMatch &match)
{
    Q_UNUSED(match)

344
    return {action(s_copyToClipboardId)};
345 346
}

347
QMimeData * CalculatorRunner::mimeDataForMatch(const Plasma::QueryMatch &match)
348 349 350
{
    //qDebug();
    QMimeData *result = new QMimeData();
351
    result->setText(match.text());
352 353 354 355
    return result;
}

#include "calculatorrunner.moc"