diff options
author | Ken Moore <ken@pcbsd.org> | 2016-10-05 17:52:52 -0400 |
---|---|---|
committer | Ken Moore <ken@pcbsd.org> | 2016-10-05 17:52:52 -0400 |
commit | babd84407a6a3475694268a3966f47a683e4d3cd (patch) | |
tree | 5ffa74d79a98e8324c596bd920f9b9c6ad2b32c2 /src-qt5 | |
parent | Fix up some instability within lumina-fileinfo. (diff) | |
download | lumina-babd84407a6a3475694268a3966f47a683e4d3cd.tar.gz lumina-babd84407a6a3475694268a3966f47a683e4d3cd.tar.bz2 lumina-babd84407a6a3475694268a3966f47a683e4d3cd.zip |
Add a new Lumina project: desktop-utils/lumina-calculator
This is just a very basic calculator at the moment. It still need more buttons/icons/features/etc added to it, but the algorithm and mathematics are already functional for evaluation expressions containing:
Addition/Subtraction (+/-)
Multiplication/Division( * or x, and /)
Order of operations when mixing operations, including expressions within parentheses
Diffstat (limited to 'src-qt5')
-rw-r--r-- | src-qt5/desktop-utils/lumina-calculator/lumina-calculator.pro | 92 | ||||
-rw-r--r-- | src-qt5/desktop-utils/lumina-calculator/main.cpp | 19 | ||||
-rw-r--r-- | src-qt5/desktop-utils/lumina-calculator/mainUI.cpp | 92 | ||||
-rw-r--r-- | src-qt5/desktop-utils/lumina-calculator/mainUI.h | 33 | ||||
-rw-r--r-- | src-qt5/desktop-utils/lumina-calculator/mainUI.ui | 54 |
5 files changed, 290 insertions, 0 deletions
diff --git a/src-qt5/desktop-utils/lumina-calculator/lumina-calculator.pro b/src-qt5/desktop-utils/lumina-calculator/lumina-calculator.pro new file mode 100644 index 00000000..8c36c516 --- /dev/null +++ b/src-qt5/desktop-utils/lumina-calculator/lumina-calculator.pro @@ -0,0 +1,92 @@ +include("$${PWD}/../../OS-detect.pri") + +QT += core gui widgets + +TARGET = lumina-calculator +target.path = $${L_BINDIR} + +HEADERS += mainUI.h + +SOURCES += main.cpp \ + mainUI.cpp + +FORMS += mainUI.ui + +LIBS += -lLuminaUtils + +DEPENDPATH += ../../libLumina + +TRANSLATIONS = i18n/l-calc_af.ts \ + i18n/l-calc_ar.ts \ + i18n/l-calc_az.ts \ + i18n/l-calc_bg.ts \ + i18n/l-calc_bn.ts \ + i18n/l-calc_bs.ts \ + i18n/l-calc_ca.ts \ + i18n/l-calc_cs.ts \ + i18n/l-calc_cy.ts \ + i18n/l-calc_da.ts \ + i18n/l-calc_de.ts \ + i18n/l-calc_el.ts \ + i18n/l-calc_en_GB.ts \ + i18n/l-calc_en_ZA.ts \ + i18n/l-calc_es.ts \ + i18n/l-calc_et.ts \ + i18n/l-calc_eu.ts \ + i18n/l-calc_fa.ts \ + i18n/l-calc_fi.ts \ + i18n/l-calc_fr.ts \ + i18n/l-calc_fr_CA.ts \ + i18n/l-calc_gl.ts \ + i18n/l-calc_he.ts \ + i18n/l-calc_hi.ts \ + i18n/l-calc_hr.ts \ + i18n/l-calc_hu.ts \ + i18n/l-calc_id.ts \ + i18n/l-calc_is.ts \ + i18n/l-calc_it.ts \ + i18n/l-calc_ja.ts \ + i18n/l-calc_ka.ts \ + i18n/l-calc_ko.ts \ + i18n/l-calc_lt.ts \ + i18n/l-calc_lv.ts \ + i18n/l-calc_mk.ts \ + i18n/l-calc_mn.ts \ + i18n/l-calc_ms.ts \ + i18n/l-calc_mt.ts \ + i18n/l-calc_nb.ts \ + i18n/l-calc_nl.ts \ + i18n/l-calc_pa.ts \ + i18n/l-calc_pl.ts \ + i18n/l-calc_pt.ts \ + i18n/l-calc_pt_BR.ts \ + i18n/l-calc_ro.ts \ + i18n/l-calc_ru.ts \ + i18n/l-calc_sk.ts \ + i18n/l-calc_sl.ts \ + i18n/l-calc_sr.ts \ + i18n/l-calc_sv.ts \ + i18n/l-calc_sw.ts \ + i18n/l-calc_ta.ts \ + i18n/l-calc_tg.ts \ + i18n/l-calc_th.ts \ + i18n/l-calc_tr.ts \ + i18n/l-calc_uk.ts \ + i18n/l-calc_uz.ts \ + i18n/l-calc_vi.ts \ + i18n/l-calc_zh_CN.ts \ + i18n/l-calc_zh_HK.ts \ + i18n/l-calc_zh_TW.ts \ + i18n/l-calc_zu.ts + +dotrans.path=$${L_SHAREDIR}/lumina-desktop/i18n/ +dotrans.extra=cd i18n && $${LRELEASE} -nounfinished *.ts && cp *.qm $(INSTALL_ROOT)$${L_SHAREDIR}/lumina-desktop/i18n/ + +#desktop.files=lumina-calculator.desktop +#desktop.path=$${L_SHAREDIR}/applications/ + +INSTALLS += target + +WITH_I18N{ + INSTALLS += dotrans +} diff --git a/src-qt5/desktop-utils/lumina-calculator/main.cpp b/src-qt5/desktop-utils/lumina-calculator/main.cpp new file mode 100644 index 00000000..af252cb3 --- /dev/null +++ b/src-qt5/desktop-utils/lumina-calculator/main.cpp @@ -0,0 +1,19 @@ +//=========================================== +// Lumina Desktop source code +// Copyright (c) 2016, Ken Moore +// Available under the 3-clause BSD license +// See the LICENSE file for full details +//=========================================== +#include <QApplication> +#include <QDebug> + +#include "mainUI.h" + +int main(int argc, char *argv[]) { + QApplication a(argc, argv); + + //Now start the window + mainUI W; + W.show(); + return a.exec(); +} diff --git a/src-qt5/desktop-utils/lumina-calculator/mainUI.cpp b/src-qt5/desktop-utils/lumina-calculator/mainUI.cpp new file mode 100644 index 00000000..18d2cfdd --- /dev/null +++ b/src-qt5/desktop-utils/lumina-calculator/mainUI.cpp @@ -0,0 +1,92 @@ +//=========================================== +// Lumina Desktop source code +// Copyright (c) 2016, Ken Moore +// Available under the 3-clause BSD license +// See the LICENSE file for full details +//=========================================== +#include "mainUI.h" +#include "ui_mainUI.h" + +#include <QDebug> + +#define VALIDSYMBOLS QString("+-*x/.") + +#include <math.h> +#define BADVALUE NAN + +mainUI::mainUI() : QMainWindow(), ui(new Ui::mainUI()){ + ui->setupUi(this); + connect(ui->tool_start, SIGNAL(clicked()), this, SLOT(start_calc()) ); + connect(ui->line_eq, SIGNAL(returnPressed()), this, SLOT(start_calc()) ); + ui->line_eq->setFocus(); +} + +mainUI::~mainUI(){ + +} + +void mainUI::start_calc(){ + if(ui->line_eq->text().isEmpty()){ return; } //nothing to do + double result = strToNumber(ui->line_eq->text()); + if(result!=result){ return; } //bad calculation - NaN's values are special in that they don't equal itself + QString res = "%1 \t(%2)"; + ui->list_results->addItem(res.arg(QString::number(result), ui->line_eq->text())); + ui->list_results->scrollToItem( ui->list_results->item( ui->list_results->count()-1) ); + ui->line_eq->clear(); +} + +double mainUI::performOperation(double LHS, double RHS, QChar symbol){ + if(symbol== '+'){ return (LHS+RHS); } + else if(symbol== '-'){ return (LHS-RHS); } + else if(symbol== '*' || symbol=='x'){ return (LHS*RHS); } + else if(symbol== '/'){ return (LHS/RHS); } + //else if(symbol== '%'){ return (LHS%RHS); } + qDebug() << "Invalid Symbol:" << symbol; + return BADVALUE; +} + +double mainUI::strToNumber(QString str){ + //Look for perentheses first + qDebug() << "String to Number: " << str; + if(str.indexOf("(")>=0){ + //qDebug() << "Found Parenthesis"; + int start = str.indexOf("("); + int need = 1; + int end = -1; + for(int i=start+1; i<str.length() && need>0; i++){ + if(str[i]=='('){ need++; } + else if(str[i]==')'){ need--; } + //qDebug() << "Check char:" << str[i] << need; + if(need==0){ end = i; } + } + if(end<start){ return BADVALUE; } + //qDebug() << "Replace value:" << str << start << end << str.mid(start+1,end-start); + str.replace(start, end-start+1, QString::number( strToNumber( str.mid(start+1, end-start-1)) ) ); + //qDebug() << "Replaced:" << str; + } + // ------------------------------------- + // NOTE: the order of operations appears reversed here due to the recursive nature of the algorithm + // the first operation *found* is actually the last one *evaluated* + // -------------------------------------- + //Now look for add/subtract + int sym = -1; + QStringList symbols; symbols << "+" << "-"; + for(int i=0; i<symbols.length(); i++){ + int tmp = str.indexOf(symbols[i]); + if(sym < tmp){ sym = tmp; } + } + if(sym>0){ return performOperation( strToNumber(str.left(sym)), strToNumber(str.right(str.length()-sym-1)), str[sym]); } + if(sym==0){ return BADVALUE; } + //Now look for multiply/divide + symbols.clear(); symbols << "x" << "*" << "/"; + for(int i=0; i<symbols.length(); i++){ + int tmp = str.indexOf(symbols[i]); + if(sym < tmp){ sym = tmp; } + } + if(sym>0){ return performOperation( strToNumber(str.left(sym)), strToNumber(str.right(str.length()-sym-1)), str[sym]); } + if(sym==0){ return BADVALUE; } + + //Could not find any operations - must be a raw number + //qDebug() << "Found Number:" << str.toDouble(); + return str.toDouble(); +} diff --git a/src-qt5/desktop-utils/lumina-calculator/mainUI.h b/src-qt5/desktop-utils/lumina-calculator/mainUI.h new file mode 100644 index 00000000..96414252 --- /dev/null +++ b/src-qt5/desktop-utils/lumina-calculator/mainUI.h @@ -0,0 +1,33 @@ +//=========================================== +// Lumina Desktop source code +// Copyright (c) 2016, Ken Moore +// Available under the 3-clause BSD license +// See the LICENSE file for full details +//=========================================== +#ifndef _LUMINA_CALCULATOR_MAIN_UI_H +#define _LUMINA_CALCULATOR_MAIN_UI_H + +#include <QMainWindow> +#include <QString> +#include <QChar> + +namespace Ui{ + class mainUI; +}; + +class mainUI : public QMainWindow{ + Q_OBJECT +public: + mainUI(); + ~mainUI(); + +private slots: + void start_calc(); + +private: + Ui::mainUI *ui; + + double performOperation(double LHS, double RHS, QChar symbol); + double strToNumber(QString str); //this is highly-recursive +}; +#endif diff --git a/src-qt5/desktop-utils/lumina-calculator/mainUI.ui b/src-qt5/desktop-utils/lumina-calculator/mainUI.ui new file mode 100644 index 00000000..0e52f4db --- /dev/null +++ b/src-qt5/desktop-utils/lumina-calculator/mainUI.ui @@ -0,0 +1,54 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>mainUI</class> + <widget class="QMainWindow" name="mainUI"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>245</width> + <height>149</height> + </rect> + </property> + <property name="windowTitle"> + <string>Calculator</string> + </property> + <widget class="QWidget" name="centralwidget"> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QListWidget" name="list_results"/> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <widget class="QLineEdit" name="line_eq"/> + </item> + <item> + <widget class="QToolButton" name="tool_start"> + <property name="text"> + <string>...</string> + </property> + </widget> + </item> + </layout> + </item> + <item> + <spacer name="verticalSpacer"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>40</height> + </size> + </property> + </spacer> + </item> + </layout> + </widget> + <widget class="QStatusBar" name="statusbar"/> + </widget> + <resources/> + <connections/> +</ui> |