From 021dad072d5db15f79cc11b44ee4708258503c05 Mon Sep 17 00:00:00 2001 From: Ken Moore Date: Thu, 20 Oct 2016 14:36:40 -0400 Subject: 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. --- .../desktop-utils/lumina-calculator/EqValidator.h | 39 ++++++++++++++++++ .../lumina-calculator/lumina-calculator.pro | 3 +- src-qt5/desktop-utils/lumina-calculator/mainUI.cpp | 47 +++++++++++++--------- src-qt5/desktop-utils/lumina-calculator/mainUI.h | 2 + src-qt5/desktop-utils/lumina-calculator/mainUI.ui | 37 +++++++++-------- 5 files changed, 91 insertions(+), 37 deletions(-) create mode 100644 src-qt5/desktop-utils/lumina-calculator/EqValidator.h (limited to 'src-qt5') 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 +#include +#include + +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 #include - -//#define VALIDSYMBOLS QString("+-*x/.") +#include "EqValidator.h" #include #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 #include #include +#include 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 @@ Qt::NoFocus + + QAbstractItemView::NoSelection + @@ -46,7 +49,7 @@ Qt::NoFocus - C + C false @@ -65,7 +68,7 @@ Qt::NoFocus - 7 + 7 @@ -75,7 +78,7 @@ Qt::NoFocus - 8 + 8 @@ -85,7 +88,7 @@ Qt::NoFocus - 9 + 9 @@ -95,7 +98,7 @@ Qt::NoFocus - / + / @@ -105,7 +108,7 @@ Qt::NoFocus - 4 + 4 @@ -115,7 +118,7 @@ Qt::NoFocus - 5 + 5 @@ -125,7 +128,7 @@ Qt::NoFocus - 6 + 6 @@ -135,7 +138,7 @@ Qt::NoFocus - * + * @@ -145,7 +148,7 @@ Qt::NoFocus - 1 + 1 @@ -155,7 +158,7 @@ Qt::NoFocus - 2 + 2 @@ -165,7 +168,7 @@ Qt::NoFocus - 3 + 3 @@ -175,7 +178,7 @@ Qt::NoFocus - - + - @@ -185,7 +188,7 @@ Qt::NoFocus - 0 + 0 @@ -195,7 +198,7 @@ Qt::NoFocus - . + . @@ -205,7 +208,7 @@ Qt::NoFocus - = + = @@ -215,7 +218,7 @@ Qt::NoFocus - + + + -- cgit