diff options
author | Ken Moore <ken@ixsystems.com> | 2016-10-20 14:36:40 -0400 |
---|---|---|
committer | Ken Moore <ken@ixsystems.com> | 2016-10-20 14:36:40 -0400 |
commit | 021dad072d5db15f79cc11b44ee4708258503c05 (patch) | |
tree | b852af9845bf6f551f635b05ff0d1892d10065af /src-qt5 | |
parent | Add the ability to acknowledge the "power" symbol (^) within an equation (diff) | |
download | lumina-021dad072d5db15f79cc11b44ee4708258503c05.tar.gz lumina-021dad072d5db15f79cc11b44ee4708258503c05.tar.bz2 lumina-021dad072d5db15f79cc11b44ee4708258503c05.zip |
Finish cleaning up some of the calculator:
1) Add an input validator to the line edit to ensure only valid characters get added
2) Add the ability to click a history item to copy that equation into the line edit.
3) Turn off some debugging messages.
Diffstat (limited to 'src-qt5')
5 files changed, 91 insertions, 37 deletions
diff --git a/src-qt5/desktop-utils/lumina-calculator/EqValidator.h b/src-qt5/desktop-utils/lumina-calculator/EqValidator.h new file mode 100644 index 00000000..462a330d --- /dev/null +++ b/src-qt5/desktop-utils/lumina-calculator/EqValidator.h @@ -0,0 +1,39 @@ +//=========================================== +// 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_VALIDATOR_H +#define _LUMINA_CALCULATOR_VALIDATOR_H + +#define VALIDCHARS QString("x*+-/^eE().0123456789") +#define NOSTARTCHARS QString("x*/^)eE.") +#define NOENDCHARS QString("x*/^(eE.") +#define NOCHANGE QString("().") + +#include <QValidator> +#include <QString> +#include <QDebug> + +class EqValidator : public QValidator{ + Q_OBJECT +public: + EqValidator(QObject *parent = 0) : QValidator(parent){} + ~EqValidator(){} + + virtual void fixup(QString &input) const{ + if(input.isEmpty()){ return; } + if( NOSTARTCHARS.contains(input.left(1)) && !NOCHANGE.contains(input.left(1)) ){ input.prepend("1"); } + if( NOENDCHARS.contains(input.right(1)) && !NOCHANGE.contains(input.right(1)) ){ input.append("1"); } + } + + virtual QValidator::State validate(QString &input, int&pos) const { + //qDebug() << "Got validate:" << input << pos; + if(pos>0 && !VALIDCHARS.contains(input[pos-1])){ return QValidator::Invalid; } + if(!input.isEmpty() && NOSTARTCHARS.contains(input.left(1))){ return QValidator::Intermediate; } + if(!input.isEmpty() && NOENDCHARS.contains(input.right(1))){ return QValidator::Intermediate; } + return QValidator::Acceptable; + } +}; +#endif diff --git a/src-qt5/desktop-utils/lumina-calculator/lumina-calculator.pro b/src-qt5/desktop-utils/lumina-calculator/lumina-calculator.pro index 0d797d70..15578f4e 100644 --- a/src-qt5/desktop-utils/lumina-calculator/lumina-calculator.pro +++ b/src-qt5/desktop-utils/lumina-calculator/lumina-calculator.pro @@ -5,7 +5,8 @@ QT += core gui widgets TARGET = lumina-calculator target.path = $${L_BINDIR} -HEADERS += mainUI.h +HEADERS += mainUI.h \ + EqValidator.h SOURCES += main.cpp \ mainUI.cpp diff --git a/src-qt5/desktop-utils/lumina-calculator/mainUI.cpp b/src-qt5/desktop-utils/lumina-calculator/mainUI.cpp index 78d071a0..cdb33414 100644 --- a/src-qt5/desktop-utils/lumina-calculator/mainUI.cpp +++ b/src-qt5/desktop-utils/lumina-calculator/mainUI.cpp @@ -9,8 +9,7 @@ #include <QDebug> #include <LuminaXDG.h> - -//#define VALIDSYMBOLS QString("+-*x/.") +#include "EqValidator.h" #include <math.h> #define BADVALUE NAN @@ -35,10 +34,11 @@ mainUI::mainUI() : QMainWindow(), ui(new Ui::mainUI()){ connect(ui->button_Multiply, SIGNAL (clicked()), this, SLOT (captureButtonMultiply())); connect(ui->button_Decimal, SIGNAL (clicked()), this, SLOT (captureButtonDecimal())); connect(ui->button_Equal, SIGNAL (clicked()), this, SLOT (start_calc())); - + connect(ui->list_results, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(insert_history(QListWidgetItem*)) ); this->setWindowTitle(tr("Calculator")); updateIcons(); ui->line_eq->setFocus(); + ui->line_eq->setValidator(new EqValidator(this) ); } mainUI::~mainUI(){ @@ -46,6 +46,7 @@ mainUI::~mainUI(){ void mainUI::updateIcons(){ this->setWindowIcon( LXDG::findIcon("accessories-calculator","") ); + ui->tool_clear->setIcon( LXDG::findIcon("edit-clear-locationbar-rtl","dialog-cancel") ); } void mainUI::start_calc(){ @@ -63,23 +64,31 @@ void mainUI::clear_calc(){ ui->line_eq->setFocus(); } -void mainUI::captureButton1(){ ui->line_eq->setText(ui->line_eq->text() += ui->button_1->text()); } -void mainUI::captureButton2(){ ui->line_eq->setText(ui->line_eq->text() += ui->button_2->text()); } -void mainUI::captureButton3(){ ui->line_eq->setText(ui->line_eq->text() += ui->button_3->text()); } -void mainUI::captureButton4(){ ui->line_eq->setText(ui->line_eq->text() += ui->button_4->text()); } -void mainUI::captureButton5(){ ui->line_eq->setText(ui->line_eq->text() += ui->button_5->text()); } -void mainUI::captureButton6(){ ui->line_eq->setText(ui->line_eq->text() += ui->button_6->text()); } -void mainUI::captureButton7(){ ui->line_eq->setText(ui->line_eq->text() += ui->button_7->text()); } -void mainUI::captureButton8(){ ui->line_eq->setText(ui->line_eq->text() += ui->button_8->text()); } -void mainUI::captureButton9(){ ui->line_eq->setText(ui->line_eq->text() += ui->button_9->text()); } -void mainUI::captureButton0(){ ui->line_eq->setText(ui->line_eq->text() += ui->button_0->text()); } -void mainUI::captureButtonSubtract(){ ui->line_eq->setText(ui->line_eq->text() += ui->button_Subtract->text()); } -void mainUI::captureButtonAdd(){ ui->line_eq->setText(ui->line_eq->text() += ui->button_Add->text()); } -void mainUI::captureButtonDivide(){ ui->line_eq->setText(ui->line_eq->text() += ui->button_Divide->text()); } -void mainUI::captureButtonMultiply(){ ui->line_eq->setText(ui->line_eq->text() += ui->button_Multiply->text()); } +void mainUI::captureButton1(){ ui->line_eq->insert(ui->button_1->text()); } +void mainUI::captureButton2(){ ui->line_eq->insert(ui->button_2->text()); } +void mainUI::captureButton3(){ ui->line_eq->insert(ui->button_3->text()); } +void mainUI::captureButton4(){ui->line_eq->insert(ui->button_4->text()); } +void mainUI::captureButton5(){ ui->line_eq->insert(ui->button_5->text()); } +void mainUI::captureButton6(){ ui->line_eq->insert(ui->button_6->text()); } +void mainUI::captureButton7(){ ui->line_eq->insert(ui->button_7->text()); } +void mainUI::captureButton8(){ ui->line_eq->insert(ui->button_8->text()); } +void mainUI::captureButton9(){ ui->line_eq->insert(ui->button_9->text()); } +void mainUI::captureButton0(){ ui->line_eq->insert(ui->button_0->text()); } +void mainUI::captureButtonSubtract(){ ui->line_eq->insert(ui->button_Subtract->text()); } +void mainUI::captureButtonAdd(){ ui->line_eq->insert(ui->button_Add->text()); } +void mainUI::captureButtonDivide(){ ui->line_eq->insert(ui->button_Divide->text()); } +void mainUI::captureButtonMultiply(){ ui->line_eq->insert(ui->button_Multiply->text()); } //void mainUI::captureButtonEqual(){ ui->line_eq->setText(ui->line_eq->text() += ui->button_Equal->text()); } -void mainUI::captureButtonDecimal(){ ui->line_eq->setText(ui->line_eq->text() += ui->button_Decimal->text()); } +void mainUI::captureButtonDecimal(){ ui->line_eq->insert(ui->button_Decimal->text()); } + +void mainUI::insert_history(QListWidgetItem *it){ + QString txt = it->text().section("[",1,-1).section("]",0,0).simplified(); + ui->line_eq->insert("("+txt+")"); +} +// ===================== +// PRIVATE FUNCTIONS +// ===================== double mainUI::performOperation(double LHS, double RHS, QChar symbol){ //qDebug() << "Perform Operation:" << LHS << symbol << RHS; if(symbol== '+'){ return (LHS+RHS); } @@ -131,7 +140,7 @@ double mainUI::strToNumber(QString str){ } 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 + //Now look for multiply/divide/power symbols.clear(); symbols << "x" << "*" << "/" << "^" ; for(int i=0; i<symbols.length(); i++){ int tmp = str.indexOf(symbols[i]); diff --git a/src-qt5/desktop-utils/lumina-calculator/mainUI.h b/src-qt5/desktop-utils/lumina-calculator/mainUI.h index 5bab1ffc..4192b250 100644 --- a/src-qt5/desktop-utils/lumina-calculator/mainUI.h +++ b/src-qt5/desktop-utils/lumina-calculator/mainUI.h @@ -10,6 +10,7 @@ #include <QMainWindow> #include <QString> #include <QChar> +#include <QListWidgetItem> namespace Ui{ class mainUI; @@ -44,6 +45,7 @@ private slots: // void captureButtonEqual(); void captureButtonDecimal(); + void insert_history(QListWidgetItem *it); private: Ui::mainUI *ui; diff --git a/src-qt5/desktop-utils/lumina-calculator/mainUI.ui b/src-qt5/desktop-utils/lumina-calculator/mainUI.ui index 0bdc4001..27d84ac0 100644 --- a/src-qt5/desktop-utils/lumina-calculator/mainUI.ui +++ b/src-qt5/desktop-utils/lumina-calculator/mainUI.ui @@ -33,6 +33,9 @@ <property name="focusPolicy"> <enum>Qt::NoFocus</enum> </property> + <property name="selectionMode"> + <enum>QAbstractItemView::NoSelection</enum> + </property> </widget> </item> <item> @@ -46,7 +49,7 @@ <enum>Qt::NoFocus</enum> </property> <property name="text"> - <string>C</string> + <string notr="true">C</string> </property> <property name="autoRaise"> <bool>false</bool> @@ -65,7 +68,7 @@ <enum>Qt::NoFocus</enum> </property> <property name="text"> - <string>7</string> + <string notr="true">7</string> </property> </widget> </item> @@ -75,7 +78,7 @@ <enum>Qt::NoFocus</enum> </property> <property name="text"> - <string>8</string> + <string notr="true">8</string> </property> </widget> </item> @@ -85,7 +88,7 @@ <enum>Qt::NoFocus</enum> </property> <property name="text"> - <string>9</string> + <string notr="true">9</string> </property> </widget> </item> @@ -95,7 +98,7 @@ <enum>Qt::NoFocus</enum> </property> <property name="text"> - <string>/</string> + <string notr="true">/</string> </property> </widget> </item> @@ -105,7 +108,7 @@ <enum>Qt::NoFocus</enum> </property> <property name="text"> - <string>4</string> + <string notr="true">4</string> </property> </widget> </item> @@ -115,7 +118,7 @@ <enum>Qt::NoFocus</enum> </property> <property name="text"> - <string>5</string> + <string notr="true">5</string> </property> </widget> </item> @@ -125,7 +128,7 @@ <enum>Qt::NoFocus</enum> </property> <property name="text"> - <string>6</string> + <string notr="true">6</string> </property> </widget> </item> @@ -135,7 +138,7 @@ <enum>Qt::NoFocus</enum> </property> <property name="text"> - <string>*</string> + <string notr="true">*</string> </property> </widget> </item> @@ -145,7 +148,7 @@ <enum>Qt::NoFocus</enum> </property> <property name="text"> - <string>1</string> + <string notr="true">1</string> </property> </widget> </item> @@ -155,7 +158,7 @@ <enum>Qt::NoFocus</enum> </property> <property name="text"> - <string>2</string> + <string notr="true">2</string> </property> </widget> </item> @@ -165,7 +168,7 @@ <enum>Qt::NoFocus</enum> </property> <property name="text"> - <string>3</string> + <string notr="true">3</string> </property> </widget> </item> @@ -175,7 +178,7 @@ <enum>Qt::NoFocus</enum> </property> <property name="text"> - <string>-</string> + <string notr="true">-</string> </property> </widget> </item> @@ -185,7 +188,7 @@ <enum>Qt::NoFocus</enum> </property> <property name="text"> - <string>0</string> + <string notr="true">0</string> </property> </widget> </item> @@ -195,7 +198,7 @@ <enum>Qt::NoFocus</enum> </property> <property name="text"> - <string>.</string> + <string notr="true">.</string> </property> </widget> </item> @@ -205,7 +208,7 @@ <enum>Qt::NoFocus</enum> </property> <property name="text"> - <string>=</string> + <string notr="true">=</string> </property> </widget> </item> @@ -215,7 +218,7 @@ <enum>Qt::NoFocus</enum> </property> <property name="text"> - <string>+</string> + <string notr="true">+</string> </property> </widget> </item> |