Warning, file /utilities/mangonel/providers/Calculator.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * Copyright 2010-2012 Bart Kroon <bart@tarmack.eu>
0003  * 
0004  * Redistribution and use in source and binary forms, with or without
0005  * modification, are permitted provided that the following conditions
0006  * are met:
0007  * 
0008  * 1. Redistributions of source code must retain the above copyright
0009  *   notice, this list of conditions and the following disclaimer.
0010  * 2. Redistributions in binary form must reproduce the above copyright
0011  *   notice, this list of conditions and the following disclaimer in the
0012  *   documentation and/or other materials provided with the distribution.
0013  * 
0014  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0015  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0016  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0017  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0018  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0019  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0020  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0021  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0022  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0023  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0024  */
0025 
0026 #include "Calculator.h"
0027 
0028 #include <assert.h>
0029 #include <math.h>
0030 #include <QApplication>
0031 #include <QClipboard>
0032 #include <QStringList>
0033 #include <klocalizedstring.h>
0034 #include <QDebug>
0035 #include <QLocale>
0036 #include <QRegularExpression>
0037 #include <QRegularExpressionMatch>
0038 
0039 #include "calculator/evaluator.h"
0040 #include "calculator/numberformatter.h"
0041 #include "calculator/settings.h"
0042 
0043 #include <iostream>
0044 
0045 Calculator::Calculator(QObject *parent) :
0046     Provider(parent)
0047 {
0048     Settings::instance()->resultPrecision = 5;
0049     Evaluator::instance();
0050 }
0051 
0052 Calculator::~Calculator()
0053 {}
0054 
0055 QList<ProviderResult*> Calculator::getResults(QString query)
0056 {
0057     // Replace #x# with #*#, where # are numbers, consider e. g. 32x32 the same as 32*32
0058     QRegularExpressionMatchIterator xMatches = QRegularExpression(R"([0-9]+\s*(x)\s*[0-9]+)").globalMatch(query);
0059     while (xMatches.hasNext()) {
0060         const QRegularExpressionMatch match = xMatches.next();
0061         query.replace(match.capturedStart(1), 1, '*');
0062     }
0063 
0064     Evaluator *ev = Evaluator::instance();
0065     query = ev->autoFix(query);
0066     if (query.isEmpty()) {
0067         return {};
0068     }
0069 
0070     ev->setExpression(query);
0071 
0072     const Quantity quantity = ev->evalNoAssign();
0073 
0074     if (!ev->error().isEmpty()) {
0075         return {};
0076     }
0077 
0078     ProviderResult *app = new ProviderResult;
0079     if (ev->error().isEmpty()) {
0080         app->name = NumberFormatter::format(quantity);
0081 
0082         if (app->name.compare(query.trimmed(), Qt::CaseInsensitive) == 0) {
0083             return {};
0084         }
0085         app->program = app->name;
0086     } else {
0087         app->name = query + ":\n" + ev->error();
0088     }
0089 
0090     app->completion = app->name;
0091     app->icon = "accessories-calculator";
0092     app->object = this;
0093     app->type = i18n("Calculation");
0094     app->isCalculation = true;
0095 
0096     return {app};
0097 }
0098 
0099 int Calculator::launch(const QString &exec)
0100 {
0101     QClipboard* clipboard = QApplication::clipboard();
0102     clipboard->setText(exec);
0103     return 0;
0104 }
0105 
0106 // kate: indent-mode cstyle; space-indent on; indent-width 4;